void audio_init() { if (audio_initialized) { return; } // Check EEPROM #if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) if (!eeconfig_is_enabled()) { eeconfig_init(); } audio_config.raw = eeconfig_read_audio(); #else // ARM EEPROM audio_config.enable = true; #ifdef AUDIO_CLICKY_ON audio_config.clicky_enable = true; #endif #endif // ARM EEPROM /* * Starting DAC1 driver, setting up the output pin as analog as suggested * by the Reference Manual. */ palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG); dacStart(&DACD1, &dac1cfg1); dacStart(&DACD2, &dac1cfg2); /* * Starting GPT6/7 driver, it is used for triggering the DAC. */ START_CHANNEL_1(); START_CHANNEL_2(); /* * Starting a continuous conversion. */ dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE); dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE); audio_initialized = true; if (audio_config.enable) { PLAY_SONG(startup_song); } else { stop_all_notes(); } }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Starting the DAC driver. */ dacStart(&DACD1, &daccfg1); /* * Sending the dac_buffer */ dacStartConversion(&DACD1, &dacconvgrp1, sine_wave, NSAMPLES); while (TRUE) { chThdSleepMilliseconds(1000); } }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Starting DAC1 driver, setting up the output pins as analog as suggested * by the Reference Manual. */ palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG); dacStart(&DACD1, &dac1cfg1); /* * Starting GPT6 driver, it is used for triggering the DAC. */ gptStart(&GPTD6, &gpt6cfg1); /* * Starting a continuous conversion. * Note, the buffer size is divided by two because two elements are fetched * for each transfer. */ dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE / 2U); gptStartContinuous(&GPTD6, 2U); /* * Normal main() thread activity, if the button is pressed then the DAC * transfer is stopped. */ while (true) { if (palReadPad(GPIOA, GPIOA_BUTTON)) { gptStopTimer(&GPTD6); dacStopConversion(&DACD1); } chThdSleepMilliseconds(500); } return 0; }
static void adctodac(void *arg) { uint16_t temp = 0; adcStart(&ADCD1, &adccfg); dacStart(&DACD1, &dac1cfg1); gptStart(&GPTD6, &gpt6cfg1); while(!0) { adcStartConversion(&ADCD1, &adccg, samples_buf, ADC_BUF_DEPTH); temp = samples_buf[0]; //dacPutChannelX(&DACD1, 1U, temp); dacStartConversion(&DACD1, &dacgrpcfg1, samples_buf, ADC_BUF_DEPTH); gptStartContinuous(&GPTD6, 2U); chThdSleepMilliseconds(1); } }
/* * Application entry point. */ int main(void) { halInit(); chSysInit(); /* * Set PA3 - PA1 to Analog (DAC1_CH1, OPAMP1_INP) * You will have to connect these with a jumper wire */ palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_ANALOG); /* * Start peripherals */ dacStart(&DACD1, &dac1cfg1); opampStart(&OPAMPD1, &opamp1_conf); gptStart(&GPTD6, &gpt6cfg1); /* * Starting a continuous conversion. */ dacStartConversion(&DACD1, &dacgrpcfg1, dac_buffer, DAC_BUFFER_SIZE); gptStartContinuous(&GPTD6, 2U); opampEnable(&OPAMPD1); opampCalibrate(); /* * Normal main() thread activity. */ while (true) { chThdSleepMilliseconds(250); palToggleLine(LINE_LED3_RED); } return 0; }
/****************** Thread main loop ***********************************/ msg_t analogue_thread(void *args) { (void)args; chRegSetThreadName("Analogue"); chBSemObjectInit(&bsAnalogueInst, true); chBSemObjectInit(&bsAnalogueFX, true); adcInit(); adcStart(&ADCD1, NULL); adcStart(&ADCD2, NULL); adcStartConversion(&ADCD1, &adc_con_group_1, (adcsample_t*)buffer1, INST_BUF_DEPTH); adcStartConversion(&ADCD2, &adc_con_group_2, (adcsample_t*)fx_samples, FX_BUF_DEPTH); dacInit(); dacStart(&DACD1, &dac_cfg); dacStartConversion(&DACD1, &dac_conv_grp, (dacsample_t*)buffer3, INST_BUF_DEPTH); // Enable DAC output buffer: DACD1.params->dac->CR |= DAC_CR_BOFF1; /* Start the GPT timers. They reload at after reaching 1 such that * TRGO frequency equals timer frequency. */ gptStart(&GPTD3, &gpt_inst_config); GPTD3.tim->CR2 |= STM32_TIM_CR2_MMS(2); gptStartContinuous(&GPTD3, 2); GPTD3.tim->DIER &= ~STM32_TIM_DIER_UIE; gptStart(&GPTD8, &gpt_fx_config); GPTD8.tim->CR2 |= STM32_TIM_CR2_MMS(2); gptStartContinuous(&GPTD8, 2); GPTD8.tim->DIER &= ~STM32_TIM_DIER_UIE; state = 1; // States: // 1 - ADC:buf1, DSP:buf2, DAC:buf3 // 2 - DSP:buf1, DAC:buf2, ADC:buf3 // 3 - DAC:buf1, ADC:buf2, DSP:buf3 /* Wait until the ADC callback boops the semaphore. */ volatile uint16_t *dsp_buf; while(true) { chSysLock(); chBSemWaitS(&bsAnalogueInst); chSysUnlock(); state += 1; switch(state) { case 1: dmaSetOtherMemory(ADCD1.dmastp, buffer1); dsp_buf = buffer2; dmaSetOtherMemory(DACD1.params->dma, buffer3); break; case 2: dmaSetOtherMemory(ADCD1.dmastp, buffer3); dsp_buf = buffer1; dmaSetOtherMemory(DACD1.params->dma, buffer2); break; case 3: dmaSetOtherMemory(ADCD1.dmastp, buffer2); dsp_buf = buffer3; dmaSetOtherMemory(DACD1.params->dma, buffer1); break; default: state = 1; dmaSetOtherMemory(ADCD1.dmastp, buffer1); dsp_buf = buffer2; dmaSetOtherMemory(DACD1.params->dma, buffer3); } dsp_stuff(dsp_buf); } }