
lab 1 code
Dependencies: CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG
Diff: main.cpp
- Revision:
- 30:debea332cdfe
- Parent:
- 26:023edf8cea6c
- Child:
- 31:5a0235b66851
--- a/main.cpp Wed Jan 01 23:20:43 2020 +0000 +++ b/main.cpp Fri Jan 03 20:15:03 2020 +0000 @@ -21,49 +21,62 @@ #include "stm32746g_discovery_lcd.h" #include "signal_processing.h" +/* The following type definitions are used to control the + * buffering of the audio data using a double buffering technique. + * Most of the transactions between the WM8994 and the microcontroller + * are handled by other code - but this signals what the buffering state + * is, so the data can be appropriately processed. */ typedef enum { BUFFER_OFFSET_NONE = 0, BUFFER_OFFSET_HALF = 1, BUFFER_OFFSET_FULL = 2, } BUFFER_StateTypeDef; +/* These audio block samples define the size of the buffering */ #define AUDIO_BLOCK_SAMPLES ((uint32_t)128) // Number of samples (L and R) in audio block (each samples is 16 bits) #define AUDIO_BLOCK_SIZE ((uint32_t)512) // Number of bytes in audio block (4 * AUDIO_BLOCK_SAMPLES) +/* These RAM addresses are important to determine where the audio data is stored. */ #define SDRAM_DEVICE_ADDR_AUDIO_MEM ((uint32_t)0xC0400000) #define AUDIO_BUFFER_IN SDRAM_DEVICE_ADDR_AUDIO_MEM #define AUDIO_BUFFER_OUT (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE * 2)) +/* These definitions define the size of the oscilloscope that is used to display data. */ #define OSC_START_X_POS 20 #define OSC_LINE_SIZE 256 #define OSC_Y_POS 110 #define AUDIO_DRAW_LIMIT 30 +/* This define a timer that is then used to record the timing of the different processing stages. */ Timer timer; +/* This variable is important because it define the audio buffer recording state. */ volatile uint32_t audio_rec_buffer_state = BUFFER_OFFSET_NONE; + +/* Function declarations */ static void Erase_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t Length); static void Draw_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t* Mem_start, uint16_t Length); static void Audio_to_Float(uint16_t* buffer_in, float* L_out, float* R_out, uint16_t Length); static void Float_to_Audio(float* L_in, float* R_in, uint16_t* buffer_out, uint16_t Length); -/* To do conversion to float */ +/* These memory blocks are important for converting to floating point representation. */ float L_channel_float[AUDIO_BLOCK_SAMPLES]; float R_channel_float[AUDIO_BLOCK_SAMPLES]; float *L_channel_float_p = &L_channel_float[0]; float *R_channel_float_p = &R_channel_float[0]; -/* Back conversion to integer */ +/* These memory blocks are where the information is stored to send back out to the WM8994 chip. */ uint16_t Processed_audio[AUDIO_BLOCK_SAMPLES]; uint16_t *Processed_audio_p = &Processed_audio[0]; /* Useful variables during looping */ -uint32_t counter = 0; -char buf[40]; -int first_half_time = 0; -int second_half_time = 0; -int total_time = 0; +uint32_t counter = 0; // Loop counter +char buf[40]; // Character buffer for sprintf statements to the LCD +int first_half_time = 0; // Time of first processing block +int second_half_time = 0; // Time of second processing block +int total_time = 0; // Time of total loop (first and second blocks) +/* Main Function */ int main() { /* Initialize the LCD Screen and display information */ @@ -71,41 +84,57 @@ BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); + /* Clear the LCD and set the font to be default */ BSP_LCD_Clear(LCD_COLOR_BLACK); BSP_LCD_SetFont(&LCD_DEFAULT_FONT); - + + /* Set the backcolor to be black and the textcolor to be orange. */ BSP_LCD_SetBackColor(LCD_COLOR_BLACK); BSP_LCD_SetTextColor(LCD_COLOR_ORANGE); - BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"487 Mic Audio Test Code", LEFT_MODE); + + /* The following are static display elements that will remain on the screen. */ + BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"487 Demo Code (Mazzeo)", LEFT_MODE); + + /* Display the L and R colors for the channels */ BSP_LCD_SetTextColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAt(0, OSC_Y_POS - 20, (uint8_t *)"L", LEFT_MODE); BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAt(0, OSC_Y_POS, (uint8_t *)"R", LEFT_MODE); - - /* Initialize the Audio Interface */ - 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); - - /* Initialize SDRAM buffers */ - BSP_SDRAM_Init(); - memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2); - memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2); + /* The following code should not be code that you need to worry about - it sets up the audio interfaces. */ + /* Initialize the Audio Interface */ + 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); + + /* Initialize SDRAM buffers */ + BSP_SDRAM_Init(); + memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2); + memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2); + + /* Start Recording */ + if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) { printf("BSP_AUDIO_IN_Record error\n"); } + + /* Start Playback */ + BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02); + if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) { printf("BSP_AUDIO_OUT_Play error\n"); } + + /* The audio interfaces are all now working. + * Importantly - AUDIO_BUFFER_IN is the pointer to the incoming data from the WM8994 + * AUDIO_BUFFER_OUT is the pointer to the outgoing data to the WM8994 */ - /* Start Recording */ - if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) { printf("BSP_AUDIO_IN_Record error\n"); } - - /* Start Playback */ - BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02); - if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) { printf("BSP_AUDIO_OUT_Play error\n"); } - - /* Initialize signal processing filters */ + /* Initialize signal processing filters - usually there are variables that + * need to be set up or that there are arrays you need to precompute - this + * function call allows you to do that. */ initalize_signal_processing(); + /* Hardware timer starts. Set to zero */ timer.start(); + + /* Main signal processing while loop */ while (1) { + /* First Half */ - /* Wait end of half block recording before going on in the first half cycle*/ + /* Wait until end of half block recording before going on in the first half cycle*/ while (audio_rec_buffer_state != BUFFER_OFFSET_HALF) {} /* This captures the time of an entire cycle */ @@ -121,11 +150,13 @@ /* Convert data to floating point representation for processing */ Audio_to_Float((uint16_t *) AUDIO_BUFFER_IN, L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES); - /* Here is where signal processing can be done on the floating point arrays */ + /* ------------------------------------------------------------------------ */ + /* Here is where signal processing can be done on the floating point arrays */ - process_audio_channel_signals(L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES); + process_audio_channel_signals(L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES); - /* Here is where signal processing can end on the floating point arrays */ + /* Here is where signal processing can end on the floating point arrays */ + /* -------------------------------------------------------------------- */ /* Covert floating point data back to fixed point audio format */ Float_to_Audio(L_channel_float_p, R_channel_float_p, (uint16_t *) Processed_audio, AUDIO_BLOCK_SAMPLES); @@ -154,12 +185,14 @@ /* Convert data to floating point representation for processing */ Audio_to_Float((uint16_t *) (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES); - /* Here is where signal processing can be done on the floating point arrays */ + /* ------------------------------------------------------------------------ */ + /* Here is where signal processing can be done on the floating point arrays */ - process_audio_channel_signals(L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES); + process_audio_channel_signals(L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES); - /* Here is where signal processing can end on the floating point arrays */ - + /* Here is where signal processing can end on the floating point arrays */ + /* -------------------------------------------------------------------- */ + /* Covert floating point data back to fixed point audio format */ Float_to_Audio(L_channel_float_p, R_channel_float_p, (uint16_t *) Processed_audio, AUDIO_BLOCK_SAMPLES);