ST / Mbed OS DISCO-F746NG_AUDIO_demo

Dependencies:   BSP_DISCO_F746NG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "stm32746g_discovery_audio.h"
00003 #include "stm32746g_discovery_sdram.h"
00004 
00005 
00006 typedef enum {
00007     BUFFER_OFFSET_NONE = 0,
00008     BUFFER_OFFSET_HALF = 1,
00009     BUFFER_OFFSET_FULL = 2,
00010 } BUFFER_StateTypeDef;
00011 
00012 
00013 #define AUDIO_BLOCK_SIZE   ((uint32_t)512)
00014 #define AUDIO_BUFFER_IN     SDRAM_DEVICE_ADDR
00015 #define AUDIO_BUFFER_OUT   (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE * 2))
00016 
00017 volatile uint32_t  audio_rec_buffer_state = BUFFER_OFFSET_NONE;
00018 
00019 
00020 int main()
00021 {
00022     printf("\n\nAUDIO LOOPBACK EXAMPLE START:\n");
00023 
00024     BSP_AUDIO_IN_OUT_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, OUTPUT_DEVICE_HEADPHONE, DEFAULT_AUDIO_IN_FREQ, DEFAULT_AUDIO_IN_BIT_RESOLUTION, DEFAULT_AUDIO_IN_CHANNEL_NBR);
00025     printf("AUDIO loop from digital micro (U20 & U21 components on board) to headphone (CN10 jack connector)\n");
00026 
00027 
00028     /* Initialize SDRAM buffers */
00029     BSP_SDRAM_Init();
00030     memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2);
00031     memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2);
00032     printf("SDRAM init done\n");
00033 
00034     audio_rec_buffer_state = BUFFER_OFFSET_NONE;
00035 
00036     /* Start Recording */
00037     if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) {
00038         printf("BSP_AUDIO_IN_Record error\n");
00039     }
00040 
00041     /* Start Playback */
00042     BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
00043     if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) {
00044         printf("BSP_AUDIO_OUT_Play error\n");
00045     }
00046 
00047     while (1) {
00048         /* Wait end of half block recording */
00049         while (audio_rec_buffer_state == BUFFER_OFFSET_HALF) {
00050         }
00051         audio_rec_buffer_state = BUFFER_OFFSET_NONE;
00052 
00053         /* Copy recorded 1st half block */
00054         memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
00055 
00056         /* Wait end of one block recording */
00057         while (audio_rec_buffer_state == BUFFER_OFFSET_FULL) {
00058         }
00059         audio_rec_buffer_state = BUFFER_OFFSET_NONE;
00060 
00061         /* Copy recorded 2nd half block */
00062         memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
00063     }
00064 }
00065 
00066 
00067 /*-------------------------------------------------------------------------------------
00068        Callbacks implementation:
00069            the callbacks API are defined __weak in the stm32746g_discovery_audio.c file
00070            and their implementation should be done in the user code if they are needed.
00071            Below some examples of callback implementations.
00072   -------------------------------------------------------------------------------------*/
00073 /**
00074   * @brief Manages the DMA Transfer complete interrupt.
00075   * @param None
00076   * @retval None
00077   */
00078 void BSP_AUDIO_IN_TransferComplete_CallBack(void)
00079 {
00080     audio_rec_buffer_state = BUFFER_OFFSET_FULL;
00081 }
00082 
00083 /**
00084   * @brief  Manages the DMA Half Transfer complete interrupt.
00085   * @param  None
00086   * @retval None
00087   */
00088 void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
00089 {
00090     audio_rec_buffer_state = BUFFER_OFFSET_HALF;
00091 }
00092 
00093 /**
00094   * @brief  Audio IN Error callback function.
00095   * @param  None
00096   * @retval None
00097   */
00098 void BSP_AUDIO_IN_Error_CallBack(void)
00099 {
00100     printf("BSP_AUDIO_IN_Error_CallBack\n");
00101 }