
lab 1 code
Dependencies: CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG
main.cpp
- Committer:
- bmazzeo
- Date:
- 2019-12-30
- Revision:
- 14:18f159d48340
- Parent:
- 13:61131aac4031
- Child:
- 15:83e40ccc1b1f
File content as of revision 14:18f159d48340:
/** ****************************************************************************** * @file main.c * @author Brian Mazzeo * @brief This file provides a set of code for signal processing in 487. * Parts are taken from example code from STMIcroelectronics ****************************************************************************** * @attention * This code was specifically developed for BYU ECEn 487 course * Introduction to Digital Signal Processing. * * ****************************************************************************** */ #include "mbed.h" #include "stm32746g_discovery_audio.h" #include "stm32746g_discovery_sdram.h" #include "stm32746g_discovery_lcd.h" typedef enum { BUFFER_OFFSET_NONE = 0, BUFFER_OFFSET_HALF = 1, BUFFER_OFFSET_FULL = 2, } BUFFER_StateTypeDef; #define HALF_AUDIO_BLOCK_SIZE ((uint32_t)128) // Number of samples (L and R) @ Frequency #define AUDIO_BLOCK_SIZE ((uint32_t)256) #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)) #define OSC_START_X_POS 20 #define OSC_LINE_SIZE 256 #define L_CHANNEL_Y_POS 120 #define R_CHANNEL_Y_POS 220 #define AUDIO_DRAW_LIMIT 30 Timer timer; volatile uint32_t audio_rec_buffer_state = BUFFER_OFFSET_NONE; static void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length); static void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length); static void Audio_to_Float(uint16_t* buffer_in, uint16_t* L_out, uint16_t * R_out, uint16_t Length); static void Float_to_Audio(uint16_t* L_in, uint16_t * R_in, uint16_t* buffer_out, uint16_t Length); /* To do converstion to float */ float L_channel_float[HALF_AUDIO_BLOCK_SIZE]; float R_channel_float[HALF_AUDIO_BLOCK_SIZE]; /* Back conversion to integer */ int32_t Processed_audio[HALF_AUDIO_BLOCK_SIZE]; /* 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; int main() { /* Initialize the LCD Screen and display information */ BSP_LCD_Init(); BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); BSP_LCD_Clear(LCD_COLOR_BLACK); BSP_LCD_SetFont(&LCD_DEFAULT_FONT); 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); BSP_LCD_DisplayStringAt(0, L_CHANNEL_Y_POS, (uint8_t *)"L", LEFT_MODE); BSP_LCD_DisplayStringAt(0, R_CHANNEL_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); /* 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"); } timer.start(); while (1) { /* First Half */ /* Wait 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 */ total_time = timer.read_us(); /* Reset the timer counter to zero */ timer.reset(); /* Plot traces of first half block recording */ Erase_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, HALF_AUDIO_BLOCK_SIZE); Draw_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, (uint16_t *) AUDIO_BUFFER_IN, HALF_AUDIO_BLOCK_SIZE); /* Convert data to floating point representation for processing */ Audio_to_Float((uint16_t *) AUDIO_BUFFER_IN, (uint16_t *) L_channel_float, (uint16_t *) R_channel_float, HALF_AUDIO_BLOCK_SIZE); Float_to_Audio((uint16_t *) L_channel_float, (uint16_t *) R_channel_float, (uint16_t *) Processed_audio, HALF_AUDIO_BLOCK_SIZE); /* Copy recorded 1st half block into the audio buffer that goes out */ memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE); /* Capture the timing of the first half processing */ first_half_time = timer.read_us(); /* End First Half */ /* Second Half */ /* Wait end of one block recording */ while (audio_rec_buffer_state != BUFFER_OFFSET_FULL) {} /* Plot traces of second half block recording */ Erase_Trace(OSC_START_X_POS+HALF_AUDIO_BLOCK_SIZE, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, HALF_AUDIO_BLOCK_SIZE); Draw_Trace(OSC_START_X_POS+HALF_AUDIO_BLOCK_SIZE, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, (uint16_t *) AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE), HALF_AUDIO_BLOCK_SIZE); /* Compute important cycle information and display it*/ counter++; sprintf(buf, "Cycles: %9d", counter); BSP_LCD_SetTextColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAt(0, 46, (uint8_t *) buf, LEFT_MODE); sprintf(buf, "1:%6d 2:%6d T:%6d", first_half_time, second_half_time, total_time); BSP_LCD_DisplayStringAt(0, 20, (uint8_t *) buf, LEFT_MODE); /* Copy recorded 2nd half block into audio output buffer */ memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE); /* Change the recording buffer state to reflect the status of the buffer */ audio_rec_buffer_state = BUFFER_OFFSET_NONE; /* Measures the amount of time to process the second half */ second_half_time = timer.read_us(); /* End Second Half */ } } /** * @brief Draws a trace of the data line. * @param Xpos: X position * @param L_Ypos: Left channel Y position * @param R_Ypos: Right channel Y position * @param Mem_start: Start of memory location * @param Length: length of trace * @retval None */ void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length) { BSP_LCD_SetTextColor(LCD_COLOR_BROWN); BSP_LCD_FillRect(Xpos, L_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT); BSP_LCD_FillRect(Xpos, L_Ypos+1, Length, AUDIO_DRAW_LIMIT); BSP_LCD_FillRect(Xpos, R_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT); BSP_LCD_FillRect(Xpos, R_Ypos+1, Length, AUDIO_DRAW_LIMIT); BSP_LCD_SetTextColor(LCD_COLOR_BLUE); BSP_LCD_DrawHLine(Xpos, L_CHANNEL_Y_POS, Length); BSP_LCD_DrawHLine(Xpos, R_CHANNEL_Y_POS, Length); } /** * @brief Draws a trace of the data line. * @param Xpos: X position * @param L_Ypos: Left channel Y position * @param R_Ypos: Right channel Y position * @param Mem_start: Start of memory location * @param Length: length of trace * @retval None */ void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length) { uint16_t i; uint32_t data_value; uint32_t* mem_address; char buf[10]; int16_t L_audio_value; int16_t R_audio_value; data_value = *((uint32_t*) Mem_start); L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF); R_audio_value = (int16_t) (data_value & 0xFFFF); //sprintf(buf, "%12d", data_value); //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE); //sprintf(buf, "%12d", L_audio_value); //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE); //sprintf(buf, "%12d", R_audio_value); //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE); for (i=0; i<Length; i++) { mem_address = (uint32_t*) Mem_start + i; data_value = *((uint32_t*) mem_address); L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF); R_audio_value = (int16_t) (data_value & 0xFFFF); L_audio_value = L_audio_value / 50; R_audio_value = R_audio_value / 50; //sprintf(buf, "%12d", L_audio_value); //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE); //sprintf(buf, "%12d", R_audio_value); //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE); //L_audio_value = -i; if (L_audio_value > AUDIO_DRAW_LIMIT) {L_audio_value = AUDIO_DRAW_LIMIT;} else if (L_audio_value < -AUDIO_DRAW_LIMIT) {L_audio_value = -AUDIO_DRAW_LIMIT;} if (R_audio_value > AUDIO_DRAW_LIMIT) {R_audio_value = AUDIO_DRAW_LIMIT;} else if (R_audio_value < -AUDIO_DRAW_LIMIT) {R_audio_value = -AUDIO_DRAW_LIMIT;} BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) L_Ypos + L_audio_value), LCD_COLOR_WHITE); BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) R_Ypos + R_audio_value), LCD_COLOR_WHITE); } } /** * @brief Converts audio data in buffer to floating point representation. * @param buffer_in: Pointer to Audio buffer start location * @param L_out: Pointer to Left channel out data (float) * @param R_out: Pointer to Right channel out data (float) * @param Length: length of data to convert * @retval None */ void Audio_to_Float(uint16_t* buffer_in, uint16_t* L_out, uint16_t * R_out, uint16_t Length) { uint16_t i; uint32_t data_value; uint16_t* audio_mem_address; uint16_t* L_chan_mem_address; uint16_t* R_chan_mem_address; float L_audio_value; float R_audio_value; for (i=0; i<Length; i++) { audio_mem_address = (uint16_t*) buffer_in + i; L_chan_mem_address = (uint16_t*) L_out + i; R_chan_mem_address = (uint16_t*) R_out + i; data_value = *((uint16_t*) audio_mem_address); L_audio_value = (float) ((int16_t) ((data_value >> 16) & 0xFFFF)); R_audio_value = (float) ((int16_t) (data_value & 0xFFFF)); *L_chan_mem_address = L_audio_value; *R_chan_mem_address = R_audio_value; } } /** * @brief Converts audio data in buffer to floating point representation. * @param L_out: Pointer to Left channel in data (float) * @param R_out: Pointer to Right channel in data (float) * @param buffer_out: Pointer to combined 32 bit (two 16-bit int samples) * @param Length: length of data to convert * @retval None */ void Float_to_Audio(uint16_t* L_in, uint16_t * R_in, uint16_t* buffer_out, uint16_t Length) { uint16_t i; uint32_t data_value; uint16_t* audio_mem_address; uint16_t* L_chan_mem_address; uint16_t* R_chan_mem_address; float L_audio_value; float R_audio_value; for (i=0; i<Length; i++) { L_chan_mem_address = (uint16_t*) L_in + i; R_chan_mem_address = (uint16_t*) R_in + i; audio_mem_address = (uint16_t*) buffer_out + i; L_audio_value = *((uint16_t*) L_chan_mem_address); R_audio_value = *((uint16_t*) R_chan_mem_address); data_value = (((uint32_t) ((int16_t) L_audio_value)) << 16) | ((uint32_t) ((int16_t) R_audio_value)); *audio_mem_address = data_value; } } /*------------------------------------------------------------------------------------- Callbacks implementation: the callbacks API are defined __weak in the stm32746g_discovery_audio.c file and their implementation should be done in the user code if they are needed. Below some examples of callback implementations. -------------------------------------------------------------------------------------*/ /** * @brief Manages the DMA Transfer complete interrupt. * @param None * @retval None */ void BSP_AUDIO_IN_TransferComplete_CallBack(void) { audio_rec_buffer_state = BUFFER_OFFSET_FULL; } /** * @brief Manages the DMA Half Transfer complete interrupt. * @param None * @retval None */ void BSP_AUDIO_IN_HalfTransfer_CallBack(void) { audio_rec_buffer_state = BUFFER_OFFSET_HALF; } /** * @brief Audio IN Error callback function. * @param None * @retval None */ void BSP_AUDIO_IN_Error_CallBack(void) { printf("BSP_AUDIO_IN_Error_CallBack\n"); }