//------------------------------------------------------------------- // Musicplayer_Init //------------------------------------------------------------------- void vInitMusicplayer (void) { // Initialize Audio interface EVAL_AUDIO_SetAudioInterface( AUDIO_INTERFACE_I2S ); EVAL_AUDIO_Init( OUTPUT_DEVICE_BOTH, 60, I2S_AudioFreq_48k ); // Volume 80%, 44Khz samplefrequentie Audio_MAL_Play((uint32_t)AUDIO_SAMPLE, sizeof(AUDIO_SAMPLE) ); // Speel stilte, waarover de toongenerator de toon speelt EVAL_AUDIO_PauseResume(AUDIO_RESUME); // Start met spelen // Motor init motor_init(); // User button init GPIO_InitTypeDef GPIO_InitStructure; #if 0 EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; #endif /* Enable the BUTTON Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* Configure Button pin as input */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOA, &GPIO_InitStructure); #if 0 /* Connect Button EXTI Line to Button GPIO Pin */ SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0); /* Configure Button EXTI line */ EXTI_InitStructure.EXTI_Line = EXTI_Line2; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable and set Button EXTI Interrupt to the lowest priority */ NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif // Motor test // Dit staat hier omdat een van de andere init's niet werkt zonder evdk //motor_init(); //setLedBlink(); //{int i; for(i=0;i<1000000;i++);} // Small delay for the motor control board to accept the previous command //// Motor ייn rondje //SMC_step(1600,1,1000,1); }
/** * @brief AudioCmd * Play, Stop, Pause or Resume current file. * @param pbuf: address from which file shoud be played. * @param size: size of the current buffer/file. * @param cmd: command to be executed, can be AUDIO_CMD_PLAY , AUDIO_CMD_PAUSE, * AUDIO_CMD_RESUME or AUDIO_CMD_STOP. * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else. */ static uint8_t AudioCmd(uint8_t* pbuf, uint32_t size, uint8_t cmd) { /* Check the current state */ if ((AudioState == AUDIO_STATE_INACTIVE) || (AudioState == AUDIO_STATE_ERROR)) { AudioState = AUDIO_STATE_ERROR; return AUDIO_FAIL; } switch (cmd) { /* Process the PLAY command ----------------------------*/ case AUDIO_CMD_PLAY: /* If current state is Active or Stopped */ if ((AudioState == AUDIO_STATE_ACTIVE) || \ (AudioState == AUDIO_STATE_STOPPED) || \ (AudioState == AUDIO_STATE_PLAYING)) { Audio_MAL_Play((uint32_t)pbuf, (size/2)); AudioState = AUDIO_STATE_PLAYING; return AUDIO_OK; } /* If current state is Paused */ else if (AudioState == AUDIO_STATE_PAUSED) { if (EVAL_AUDIO_PauseResume(AUDIO_RESUME /*, (uint32_t)pbuf, (size/2)*/) != 0) { AudioState = AUDIO_STATE_ERROR; return AUDIO_FAIL; } else { AudioState = AUDIO_STATE_PLAYING; return AUDIO_OK; } } else /* Not allowed command */ { return AUDIO_FAIL; } /* Process the STOP command ----------------------------*/ case AUDIO_CMD_STOP: if (AudioState != AUDIO_STATE_PLAYING) { /* Unsupported command */ return AUDIO_FAIL; } else if (EVAL_AUDIO_Stop(CODEC_PDWN_SW) != 0) { AudioState = AUDIO_STATE_ERROR; return AUDIO_FAIL; } else { AudioState = AUDIO_STATE_STOPPED; return AUDIO_OK; } /* Process the PAUSE command ---------------------------*/ case AUDIO_CMD_PAUSE: if (AudioState != AUDIO_STATE_PLAYING) { /* Unsupported command */ return AUDIO_FAIL; } else if (EVAL_AUDIO_PauseResume(AUDIO_PAUSE /*, (uint32_t)pbuf, (size/2)*/) != 0) { AudioState = AUDIO_STATE_ERROR; return AUDIO_FAIL; } else { AudioState = AUDIO_STATE_PAUSED; return AUDIO_OK; } /* Unsupported command ---------------------------------*/ default: return AUDIO_FAIL; } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* Initialize LEDs, Key Button, LCD available on STM324xG-EVAL board ******************************************************/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Initialize the Push buttons */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); /* Key button for Pause/Resume */ STM_EVAL_PBInit(BUTTON_WAKEUP, BUTTON_MODE_GPIO); /* Key button for Volume High */ STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO); /* Key button for Volume Low */ /* Initialize the LCD */ STM324xG_LCD_Init(); /* Display message on STM324xG-EVAL LCD *************************************/ /* Clear the LCD */ LCD_Clear(LCD_COLOR_BLUE); /* Set the LCD Back Color */ LCD_SetBackColor(Blue); /* Set the LCD Text Color */ LCD_SetTextColor(White); LCD_DisplayStringLine(Line0, MESSAGE1); LCD_DisplayStringLine(Line1, MESSAGE2); LCD_DisplayStringLine(Line2, MESSAGE3); /* Turn on leds available on STM324xG-EVAL **********************************/ STM_EVAL_LEDOn(LED1); STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */ if (EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, volume, I2S_AudioFreq_48k) == 0) { LCD_DisplayStringLine(Line3, "===================="); LCD_DisplayStringLine(Line4, "Key : Play/Pause "); LCD_DisplayStringLine(Line5, "Tamper: Vol+/Headph"); LCD_DisplayStringLine(Line6, "Wakeup: Vol-/Spkr "); LCD_DisplayStringLine(Line7, "===================="); LCD_DisplayStringLine(Line8, " AUDIO CODEC OK "); } else { LCD_DisplayStringLine(Line4, " AUDIO CODEC FAIL "); LCD_DisplayStringLine(Line5, " Try to reset board "); } /* Normal mode description: Start playing the audio file (using DMA stream) . Using this mode, the application can run other tasks in parallel since the DMA is handling the Audio Transfer instead of the CPU. The only task remaining for the CPU will be the management of the DMA Transfer Complete interrupt or the Half Transfer Complete interrupt in order to load again the buffer and to calculate the remaining data. Circular mode description: Start playing the file from a circular buffer, once the DMA is enabled it always run. User has to fill periodically the buffer with the audio data using Transfer complete and/or half transfer complete interrupts callbacks (EVAL_AUDIO_TransferComplete_CallBack() or EVAL_AUDIO_HalfTransfer_CallBack()... In this case the audio data file is smaller than the DMA max buffer size 65535 so there is no need to load buffer continuously or manage the transfer complete or Half transfer interrupts callbacks. */ EVAL_AUDIO_Play((uint16_t*)(AUDIO_SAMPLE + AUIDO_START_ADDRESS), (AUDIO_FILE_SZE - AUIDO_START_ADDRESS)); /* Display the state on the screen */ LCD_DisplayStringLine(Line8, " PLAYING "); /* Infinite loop */ while (1) { /* Check on the Pause/Resume button */ if (STM_EVAL_PBGetState(BUTTON_KEY) != Bit_SET) { /* wait to avoid rebound */ while (STM_EVAL_PBGetState(BUTTON_KEY) != Bit_SET); EVAL_AUDIO_PauseResume(cmd); if (cmd == AUDIO_PAUSE) { /* Display the current state of the player */ LCD_DisplayStringLine(Line8, " PAUSED "); /* Next time Resume command should be processed */ cmd = AUDIO_RESUME; /* Push buttons will be used to switch between Speaker and Headphone modes */ SpHpSwitch = 1; } else { /* Display the current state of the player */ LCD_DisplayStringLine(Line8, " PLAYING "); /* Next time Pause command should be processed */ cmd = AUDIO_PAUSE; /* Push buttons will be used to control volume level */ SpHpSwitch = 0; } } /* Check on the Volume high button */ if (STM_EVAL_PBGetState(BUTTON_WAKEUP) == Bit_SET) { /* Check if the current state is paused (push buttons are used for volume control or for speaker/headphone mode switching) */ if (SpHpSwitch) { /* Set output to Speaker */ Codec_SwitchOutput(OUTPUT_DEVICE_SPEAKER); /* Display the current state of the player */ LCD_DisplayStringLine(Line9, " SPEAKER "); } else { /* wait to avoid rebound */ while (STM_EVAL_PBGetState(BUTTON_WAKEUP) == Bit_SET); /* Decrease volume by 5% */ if (volume > 5) volume -= 5; else volume = 0; /* Apply the new volume to the codec */ EVAL_AUDIO_VolumeCtl(volume); LCD_DisplayStringLine(Line9, " VOL: - "); } } /* Check on the Volume high button */ if (STM_EVAL_PBGetState(BUTTON_TAMPER) != Bit_SET) { /* Check if the current state is paused (push buttons are used for volume control or for speaker/headphone mode switching) */ if (SpHpSwitch) { /* Set output to Headphone */ Codec_SwitchOutput(OUTPUT_DEVICE_HEADPHONE); /* Display the current state of the player */ LCD_DisplayStringLine(Line9, " HEADPHONE "); } else { /* wait to avoid rebound */ while (STM_EVAL_PBGetState(BUTTON_TAMPER) != Bit_SET); /* Increase volume by 5% */ if (volume < 95) volume += 5; else volume = 100; /* Apply the new volume to the codec */ EVAL_AUDIO_VolumeCtl(volume); LCD_DisplayStringLine(Line9, " VOL: + "); } } /* Toggle LD4 */ STM_EVAL_LEDToggle(LED3); /* Insert 50 ms delay */ Delay(5); /* Toggle LD2 */ STM_EVAL_LEDToggle(LED2); /* Insert 50 ms delay */ Delay(5); } }
void AudioTask ( void * pvParameters ) { struct AAudioCommandMessage CmdMsg; uint8_t rc = 0; FRESULT res; uint8_t pausing = 0; xAudioQueue = xQueueCreate ( 8, sizeof ( char ) ); res = f_mount ( &FatFs, "", 1 ); // mount the drive if ( res ) { led_state[0] = LED_STATE_BLINK_FAST; while ( 1 ); } while ( 1 ) { if ( xQueueReceive ( xAudioQueue, &CmdMsg, portMAX_DELAY ) ) { switch ( CmdMsg.Command ) { case 'h': // half transfer complete break; case 't': // transfer complete if ( audioContext.lastFrame ) { EVAL_AUDIO_Stop ( CODEC_PDWN_SW ); } else { AudioProvideSamplesFromFile ( ); } break; case 'p': // play file rc = AudioStartPlayFile ( CmdMsg.sFilename ); switch ( rc ) { case 0: led_state[1] = LED_STATE_OFF; led_state[2] = LED_STATE_OFF; break; case 1: led_state[1] = LED_STATE_BLINK_FAST; break; case 2: led_state[2] = LED_STATE_BLINK_FAST; break; } break; case 'r': // pause/resume EVAL_AUDIO_PauseResume ( pausing ? AUDIO_RESUME : AUDIO_PAUSE ); pausing ^= pausing; break; case 's': // stop playback EVAL_AUDIO_Stop ( CODEC_PDWN_SW ); break; case 'v': // set volume break; case '+': // volume up break; case '-': // volume down break; case 'n': // next file break; case 'l': // last file break; }; }; }; };
/** * @brief Pause or Resume a played wave * @param state: if it is equal to 0 pause Playing else resume playing * @retval None */ void WavePlayerPauseResume(uint8_t state) { EVAL_AUDIO_PauseResume(state); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Initialize LEDS */ /* Red Led On: buffer overflow */ STM_EVAL_LEDInit(LED3); /* Green Led On: fdmdv+codec2 enabled */ STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED5); /* Blue Led On: start of application */ STM_EVAL_LEDInit(LED6); STM_EVAL_LEDOn(LED6); /* transparent mode switcher */ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); if(Transparent_mode) STM_EVAL_LEDOff(LED4); else STM_EVAL_LEDOn(LED4); /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; STM_EVAL_USART2Init(&USART_InitStructure); // turn off buffers, so IO occurs immediately setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); /* Output a message on Hyperterminal using printf function */ printf("\r\nFloating-Point Based Codec2 encoder for Cortex-M4F\r\n"); /* Configure TIM4 Peripheral to manage LEDs lighting */ unsigned int idx = 0; Time_Rec_Base=0; int i, buffId; codec2_initialize_all(SPEAKER_FREQ == 48000 ? 1 : 0); /* fill output fifo */ fifoBufferFullness=0; fifoBufferCurrent=0; Switch = 0; /* modulate silence once for padding if happens */ memset(padBuffer, 0x00, (320*(SPEAKER_FREQ/MIC_FREQ)*2)*sizeof(short)); memset(fifoBuffer, 0x00, MODULATOR_QUEUE_SIZE * (SPEAKER_FREQ/MIC_FREQ)*320*2*sizeof(short)); //codec2_modulate((short *) padBuffer, (short *) padBuffer, Transparent_mode); /* Initialize I2S interface */ EVAL_AUDIO_SetAudioInterface(AUDIO_INTERFACE_I2S); /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */ //EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, 0, SPEAKER_FREQ); EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, 0, SPEAKER_FREQ); EVAL_AUDIO_PauseResume(AUDIO_PAUSE); Audio_MAL_Play((uint32_t) padBuffer, (320*(SPEAKER_FREQ/MIC_FREQ))*2*sizeof(short)); EVAL_AUDIO_PauseResume(AUDIO_RESUME); /* Start the record */ MicListenerInit(32000,16, 1); MicListenerStart(RecBuf_8Khz, PCM_OUT_SIZE); /* GLOBAL SCHEDULER * DO NOT USE LOOPS INSIDE IT! * */ while(1) { /* we have frame from mike */ if(Data_Status == 0) continue; /* Switch the buffers*/ if (Switch ==1) { pAudioRecBuf_8Khz = RecBuf_8Khz; writebuffer = RecBuf1_8Khz; Switch = 0; } else { pAudioRecBuf_8Khz = RecBuf1_8Khz; writebuffer = RecBuf_8Khz; Switch = 1; } #ifdef USE_ST_FILTER //Downsampling 16Khz => 8Khz (this is input for codec, it sampled with 8KHz) for(i=0; i<320; i++) writebuffer[i] = writebuffer[2*i]; #endif //TODO: modulate, even if no data from mike! if(fifoBufferFullness < MODULATOR_QUEUE_SIZE-1) { /* get the next free buffer */ buffId = fifoBufferCurrent + fifoBufferFullness; if(buffId >= MODULATOR_QUEUE_SIZE) buffId -= MODULATOR_QUEUE_SIZE; assert(buffId >= 0 && buffId < MODULATOR_QUEUE_SIZE); codec2_modulate((short *) writebuffer, (short *) fifoBuffer[buffId], Transparent_mode); fifoBufferFullness++; if(idx % 32 == 0) printf("."); if(idx % (32*32) == 0) printf("\r\n"); /* this is hack to remove loud noise at startup */ if(volume_set==1) { STM_EVAL_LEDOff(LED3); EVAL_AUDIO_VolumeCtl(90); volume_set=2; } } else { STM_EVAL_LEDToggle(LED3); printf("x"); if(idx % (32) == 0) printf("\r\n"); } idx++; Data_Status = 0; } EVAL_AUDIO_Mute(AUDIO_MUTE_ON); EVAL_AUDIO_Stop(CODEC_PDWN_HW); MicListenerStop(); //float samples_time = idx*samplesPerFrame/((float) WAVE_Format.NumChannels*WAVE_Format.SampleRate); #if 0 float samples_time = idx*mod->samplesPerFrame/((float) 1*MIC_FREQ); float cpu_time = Time_Rec_Base/((float ) 100); printf("\r\n%8.3f s audio file encoded in %8.3f s\r\n", (double) samples_time, (double) cpu_time); #endif while(1) { STM_EVAL_LEDToggle(LED5); Delay(10); } }