本帖最后由 dawei360 于 2017-4-5 11:03 编辑
要实现音频的播放,简单的方式就是找个带耳机插孔的ST开发板,比如STM32F769I_Discovery, 该板子自带SAI接口,而且还有WM8994 解码芯片;先来看看STM32F769上硬件电路的实现;
代码实现:
- int main(void)
- {
- uint32_t PlaybackPosition = PLAY_BUFF_SIZE + PLAY_HEADER;
- CPU_CACHE_Enable();
- HAL_Init();
- SystemClock_Config();
- Playback_Init();
- /* Initialize the data buffer */
- for(int i=0; i < PLAY_BUFF_SIZE; i+=2)
- {
- PlayBuff[i]=*((__IO uint16_t *)(AUDIO_FILE_ADDRESS + PLAY_HEADER + i));
- }
-
- /* Start the playback */
- if(0 != audio_drv->Play(AUDIO_I2C_ADDRESS, NULL, 0))
- {
- Error_Handler();
- }
- if(HAL_OK != HAL_SAI_Transmit_DMA(&SaiHandle, (uint8_t *)PlayBuff, PLAY_BUFF_SIZE))
- {
- Error_Handler();
- }
-
- /* Start loopback */
- while(1)
- {
- BSP_LED_Toggle(LED2);
- /* Wait a callback event */
- while(UpdatePointer==-1);
-
- int position = UpdatePointer;
- UpdatePointer = -1;
- /* Upate the first or the second part of the buffer */
- for(int i = 0; i < PLAY_BUFF_SIZE/2; i++)
- {
- PlayBuff[i+position] = *(uint16_t *)(AUDIO_FILE_ADDRESS + PlaybackPosition);
- PlaybackPosition+=2;
- }
- /* check the end of the file */
- if((PlaybackPosition+PLAY_BUFF_SIZE/2) > AUDIO_FILE_SIZE)
- {
- PlaybackPosition = PLAY_HEADER;
- }
-
- if(UpdatePointer != -1)
- {
- /* Buffer update time is too long compare to the data transfer time */
- Error_Handler();
- }
- }
- }
实现效果:当LED2开始闪烁时候,就可以通过CN7的耳机孔听到播放的声音了。
|