lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Committer:
bmazzeo
Date:
Sun Dec 29 06:46:19 2019 +0000
Revision:
11:4256dbbb0c89
Parent:
10:a82b64ea1d11
Child:
12:e44766b61346
Cleaned up a few lines

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bmazzeo 9:f5b37c71856d 1 /**
bmazzeo 9:f5b37c71856d 2 ******************************************************************************
bmazzeo 9:f5b37c71856d 3 * @file main.c
bmazzeo 9:f5b37c71856d 4 * @author Brian Mazzeo
bmazzeo 9:f5b37c71856d 5 * @brief This file provides a set of code for signal processing in 487.
bmazzeo 9:f5b37c71856d 6 * Parts are taken from example code from STMIcroelectronics
bmazzeo 9:f5b37c71856d 7 ******************************************************************************
bmazzeo 9:f5b37c71856d 8 * @attention
bmazzeo 9:f5b37c71856d 9 * This code was specifically developed for 487 signal processing.
bmazzeo 9:f5b37c71856d 10 *
bmazzeo 9:f5b37c71856d 11 *
bmazzeo 9:f5b37c71856d 12 ******************************************************************************
bmazzeo 9:f5b37c71856d 13 */
bmazzeo 9:f5b37c71856d 14
bmazzeo 9:f5b37c71856d 15
adustm 0:da04816fb411 16 #include "mbed.h"
Jerome Coutant 5:66c230f74325 17 #include "stm32746g_discovery_audio.h"
Jerome Coutant 5:66c230f74325 18 #include "stm32746g_discovery_sdram.h"
bmazzeo 6:e689075b04ed 19 #include "stm32746g_discovery_lcd.h"
adustm 0:da04816fb411 20
Jerome Coutant 5:66c230f74325 21 typedef enum {
adustm 0:da04816fb411 22 BUFFER_OFFSET_NONE = 0,
adustm 0:da04816fb411 23 BUFFER_OFFSET_HALF = 1,
adustm 0:da04816fb411 24 BUFFER_OFFSET_FULL = 2,
Jerome Coutant 5:66c230f74325 25 } BUFFER_StateTypeDef;
Jerome Coutant 5:66c230f74325 26
adustm 0:da04816fb411 27
bmazzeo 6:e689075b04ed 28 #define AUDIO_BLOCK_SIZE ((uint32_t)256)
bmazzeo 6:e689075b04ed 29
bmazzeo 9:f5b37c71856d 30 #define SDRAM_DEVICE_ADDR_AUDIO_MEM ((uint32_t)0xC0400000)
bmazzeo 9:f5b37c71856d 31 #define AUDIO_BUFFER_IN SDRAM_DEVICE_ADDR_AUDIO_MEM
bmazzeo 9:f5b37c71856d 32 #define AUDIO_BUFFER_OUT (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE * 2))
Jerome Coutant 5:66c230f74325 33
bmazzeo 8:d1c41eca57f0 34 #define OSC_START_X_POS 20
bmazzeo 6:e689075b04ed 35 #define OSC_LINE_SIZE 256
bmazzeo 6:e689075b04ed 36 #define L_CHANNEL_Y_POS 120
bmazzeo 6:e689075b04ed 37 #define R_CHANNEL_Y_POS 220
bmazzeo 7:e1dfd64eba81 38 #define AUDIO_DRAW_LIMIT 30
bmazzeo 6:e689075b04ed 39
bmazzeo 9:f5b37c71856d 40 Timer timer;
bmazzeo 9:f5b37c71856d 41
Jerome Coutant 5:66c230f74325 42 volatile uint32_t audio_rec_buffer_state = BUFFER_OFFSET_NONE;
bmazzeo 7:e1dfd64eba81 43 static void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length);
bmazzeo 8:d1c41eca57f0 44 static void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length);
Jerome Coutant 5:66c230f74325 45
bmazzeo 9:f5b37c71856d 46 uint32_t counter = 0;
bmazzeo 10:a82b64ea1d11 47 char buf[40];
bmazzeo 9:f5b37c71856d 48 int first_half_time, second_half_time, total_time;
bmazzeo 9:f5b37c71856d 49
adustm 0:da04816fb411 50 int main()
adustm 0:da04816fb411 51 {
bmazzeo 6:e689075b04ed 52
bmazzeo 6:e689075b04ed 53 BSP_LCD_Init();
bmazzeo 6:e689075b04ed 54 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
bmazzeo 6:e689075b04ed 55 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
bmazzeo 6:e689075b04ed 56
bmazzeo 6:e689075b04ed 57 BSP_LCD_Clear(LCD_COLOR_BLACK);
bmazzeo 6:e689075b04ed 58 BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
bmazzeo 6:e689075b04ed 59
bmazzeo 6:e689075b04ed 60 BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
bmazzeo 6:e689075b04ed 61 BSP_LCD_SetTextColor(LCD_COLOR_ORANGE);
bmazzeo 7:e1dfd64eba81 62 BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"487 Mic Audio Test Code", LEFT_MODE);
bmazzeo 8:d1c41eca57f0 63 BSP_LCD_DisplayStringAt(0, L_CHANNEL_Y_POS, (uint8_t *)"L", LEFT_MODE);
bmazzeo 8:d1c41eca57f0 64 BSP_LCD_DisplayStringAt(0, R_CHANNEL_Y_POS, (uint8_t *)"R", LEFT_MODE);
bmazzeo 6:e689075b04ed 65
Jerome Coutant 5:66c230f74325 66 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);
adustm 0:da04816fb411 67
adustm 0:da04816fb411 68 /* Initialize SDRAM buffers */
Jerome Coutant 5:66c230f74325 69 BSP_SDRAM_Init();
Jerome Coutant 5:66c230f74325 70 memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2);
Jerome Coutant 5:66c230f74325 71 memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2);
Jerome Coutant 5:66c230f74325 72 printf("SDRAM init done\n");
Jerome Coutant 5:66c230f74325 73
adustm 0:da04816fb411 74 audio_rec_buffer_state = BUFFER_OFFSET_NONE;
adustm 0:da04816fb411 75
adustm 0:da04816fb411 76 /* Start Recording */
Jerome Coutant 5:66c230f74325 77 if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) {
Jerome Coutant 5:66c230f74325 78 printf("BSP_AUDIO_IN_Record error\n");
Jerome Coutant 5:66c230f74325 79 }
adustm 0:da04816fb411 80
adustm 0:da04816fb411 81 /* Start Playback */
Jerome Coutant 5:66c230f74325 82 BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
Jerome Coutant 5:66c230f74325 83 if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) {
Jerome Coutant 5:66c230f74325 84 printf("BSP_AUDIO_OUT_Play error\n");
Jerome Coutant 5:66c230f74325 85 }
adustm 0:da04816fb411 86
bmazzeo 9:f5b37c71856d 87 timer.start();
adustm 0:da04816fb411 88 while (1) {
bmazzeo 6:e689075b04ed 89 /* First Half */
bmazzeo 9:f5b37c71856d 90 /* Wait end of half block recording */
bmazzeo 11:4256dbbb0c89 91 while (audio_rec_buffer_state != BUFFER_OFFSET_HALF) {}
bmazzeo 9:f5b37c71856d 92
bmazzeo 9:f5b37c71856d 93 total_time = timer.read_us();
bmazzeo 9:f5b37c71856d 94 timer.reset();
Jerome Coutant 5:66c230f74325 95
adustm 0:da04816fb411 96 /* Copy recorded 1st half block */
adustm 0:da04816fb411 97 memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
Jerome Coutant 5:66c230f74325 98
bmazzeo 8:d1c41eca57f0 99 /* Plot trace of first half block recording */
bmazzeo 9:f5b37c71856d 100 Erase_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, 300);
bmazzeo 9:f5b37c71856d 101 Draw_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, (uint16_t *) AUDIO_BUFFER_IN, 300);
bmazzeo 9:f5b37c71856d 102
bmazzeo 9:f5b37c71856d 103 first_half_time = timer.read_us();
bmazzeo 8:d1c41eca57f0 104
bmazzeo 6:e689075b04ed 105 /* Second Half */
bmazzeo 6:e689075b04ed 106
bmazzeo 8:d1c41eca57f0 107
adustm 0:da04816fb411 108 /* Wait end of one block recording */
bmazzeo 11:4256dbbb0c89 109 while (audio_rec_buffer_state != BUFFER_OFFSET_FULL) {}
bmazzeo 9:f5b37c71856d 110
adustm 0:da04816fb411 111 /* Copy recorded 2nd half block */
adustm 0:da04816fb411 112 memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
bmazzeo 6:e689075b04ed 113
bmazzeo 8:d1c41eca57f0 114 /* Plot second half recording */
bmazzeo 9:f5b37c71856d 115 //Erase_Trace(OSC_START_X_POS+OSC_LINE_SIZE, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, 50);
bmazzeo 9:f5b37c71856d 116 //Draw_Trace(OSC_START_X_POS+OSC_LINE_SIZE, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, (uint16_t *) AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE), 50);
bmazzeo 6:e689075b04ed 117
bmazzeo 10:a82b64ea1d11 118
bmazzeo 10:a82b64ea1d11 119
bmazzeo 6:e689075b04ed 120 counter++;
bmazzeo 10:a82b64ea1d11 121 sprintf(buf, "Cycles: %9d", counter);
bmazzeo 6:e689075b04ed 122 BSP_LCD_SetTextColor(LCD_COLOR_RED);
bmazzeo 10:a82b64ea1d11 123 BSP_LCD_DisplayStringAt(0, 46, (uint8_t *) buf, LEFT_MODE);
bmazzeo 10:a82b64ea1d11 124 sprintf(buf, "1:%6d 2:%6d T:%6d", first_half_time, second_half_time, total_time);
bmazzeo 10:a82b64ea1d11 125 BSP_LCD_DisplayStringAt(0, 20, (uint8_t *) buf, LEFT_MODE);
bmazzeo 9:f5b37c71856d 126
bmazzeo 10:a82b64ea1d11 127 audio_rec_buffer_state = BUFFER_OFFSET_NONE;
bmazzeo 9:f5b37c71856d 128 second_half_time = timer.read_us();
bmazzeo 9:f5b37c71856d 129
adustm 0:da04816fb411 130 }
adustm 0:da04816fb411 131 }
Jerome Coutant 5:66c230f74325 132
bmazzeo 7:e1dfd64eba81 133 /**
bmazzeo 7:e1dfd64eba81 134 * @brief Draws a trace of the data line.
bmazzeo 7:e1dfd64eba81 135 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 136 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 137 * @param R_Ypos: Right channel Y position
bmazzeo 7:e1dfd64eba81 138 * @param Mem_start: Start of memory location
bmazzeo 7:e1dfd64eba81 139 * @param Length: length of trace
bmazzeo 7:e1dfd64eba81 140 * @retval None
bmazzeo 7:e1dfd64eba81 141 */
bmazzeo 7:e1dfd64eba81 142 void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length)
bmazzeo 7:e1dfd64eba81 143 {
bmazzeo 7:e1dfd64eba81 144 BSP_LCD_SetTextColor(LCD_COLOR_BROWN);
bmazzeo 7:e1dfd64eba81 145 BSP_LCD_FillRect(Xpos, L_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 146 BSP_LCD_FillRect(Xpos, L_Ypos+1, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 147 BSP_LCD_FillRect(Xpos, R_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 148 BSP_LCD_FillRect(Xpos, R_Ypos+1, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 149
bmazzeo 7:e1dfd64eba81 150 BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
bmazzeo 8:d1c41eca57f0 151 BSP_LCD_DrawHLine(Xpos, L_CHANNEL_Y_POS, Length);
bmazzeo 8:d1c41eca57f0 152 BSP_LCD_DrawHLine(Xpos, R_CHANNEL_Y_POS, Length);
bmazzeo 7:e1dfd64eba81 153
bmazzeo 7:e1dfd64eba81 154 }
bmazzeo 7:e1dfd64eba81 155
Jerome Coutant 5:66c230f74325 156
bmazzeo 6:e689075b04ed 157 /**
bmazzeo 6:e689075b04ed 158 * @brief Draws a trace of the data line.
bmazzeo 6:e689075b04ed 159 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 160 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 161 * @param R_Ypos: Right channel Y position
bmazzeo 6:e689075b04ed 162 * @param Mem_start: Start of memory location
bmazzeo 6:e689075b04ed 163 * @param Length: length of trace
bmazzeo 6:e689075b04ed 164 * @retval None
bmazzeo 6:e689075b04ed 165 */
bmazzeo 8:d1c41eca57f0 166 void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length)
bmazzeo 6:e689075b04ed 167 {
bmazzeo 7:e1dfd64eba81 168 uint16_t i;
bmazzeo 7:e1dfd64eba81 169 uint32_t data_value;
bmazzeo 7:e1dfd64eba81 170 uint32_t* mem_address;
bmazzeo 7:e1dfd64eba81 171 char buf[10];
bmazzeo 7:e1dfd64eba81 172 int16_t L_audio_value;
bmazzeo 7:e1dfd64eba81 173 int16_t R_audio_value;
bmazzeo 6:e689075b04ed 174
bmazzeo 7:e1dfd64eba81 175 data_value = *((uint32_t*) Mem_start);
bmazzeo 7:e1dfd64eba81 176 L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 7:e1dfd64eba81 177 R_audio_value = (int16_t) (data_value & 0xFFFF);
bmazzeo 7:e1dfd64eba81 178
bmazzeo 8:d1c41eca57f0 179 //sprintf(buf, "%12d", data_value);
bmazzeo 8:d1c41eca57f0 180 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 8:d1c41eca57f0 181
bmazzeo 7:e1dfd64eba81 182 //sprintf(buf, "%12d", L_audio_value);
bmazzeo 7:e1dfd64eba81 183 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 6:e689075b04ed 184
bmazzeo 7:e1dfd64eba81 185 //sprintf(buf, "%12d", R_audio_value);
bmazzeo 7:e1dfd64eba81 186 //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 187
bmazzeo 8:d1c41eca57f0 188 for (i=0; i<Length; i++)
bmazzeo 6:e689075b04ed 189 {
bmazzeo 7:e1dfd64eba81 190 mem_address = (uint32_t*) Mem_start + i;
bmazzeo 7:e1dfd64eba81 191 data_value = *((uint32_t*) mem_address);
bmazzeo 7:e1dfd64eba81 192 L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 7:e1dfd64eba81 193 R_audio_value = (int16_t) (data_value & 0xFFFF);
bmazzeo 7:e1dfd64eba81 194
bmazzeo 7:e1dfd64eba81 195 L_audio_value = L_audio_value / 50;
bmazzeo 7:e1dfd64eba81 196 R_audio_value = R_audio_value / 50;
bmazzeo 7:e1dfd64eba81 197
bmazzeo 7:e1dfd64eba81 198 //sprintf(buf, "%12d", L_audio_value);
bmazzeo 7:e1dfd64eba81 199 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 200
bmazzeo 7:e1dfd64eba81 201 //sprintf(buf, "%12d", R_audio_value);
bmazzeo 7:e1dfd64eba81 202 //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 203
bmazzeo 7:e1dfd64eba81 204
bmazzeo 7:e1dfd64eba81 205 //L_audio_value = -i;
bmazzeo 7:e1dfd64eba81 206 if (L_audio_value > AUDIO_DRAW_LIMIT) {L_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 207 else if (L_audio_value < -AUDIO_DRAW_LIMIT) {L_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 208
bmazzeo 7:e1dfd64eba81 209 if (R_audio_value > AUDIO_DRAW_LIMIT) {R_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 210 else if (R_audio_value < -AUDIO_DRAW_LIMIT) {R_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 211
bmazzeo 7:e1dfd64eba81 212 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) L_Ypos + L_audio_value), LCD_COLOR_WHITE);
bmazzeo 7:e1dfd64eba81 213 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) R_Ypos + R_audio_value), LCD_COLOR_WHITE);
bmazzeo 6:e689075b04ed 214 }
bmazzeo 6:e689075b04ed 215
bmazzeo 6:e689075b04ed 216 }
bmazzeo 6:e689075b04ed 217
bmazzeo 6:e689075b04ed 218 /* Read data value from SDRAM memory */
bmazzeo 6:e689075b04ed 219 // ret = *(__IO uint32_t*) (hLtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (4*(Ypos*BSP_LCD_GetXSize() + Xpos)));
bmazzeo 6:e689075b04ed 220
bmazzeo 6:e689075b04ed 221
adustm 0:da04816fb411 222 /*-------------------------------------------------------------------------------------
adustm 0:da04816fb411 223 Callbacks implementation:
adustm 0:da04816fb411 224 the callbacks API are defined __weak in the stm32746g_discovery_audio.c file
adustm 0:da04816fb411 225 and their implementation should be done in the user code if they are needed.
adustm 0:da04816fb411 226 Below some examples of callback implementations.
adustm 0:da04816fb411 227 -------------------------------------------------------------------------------------*/
adustm 0:da04816fb411 228 /**
adustm 0:da04816fb411 229 * @brief Manages the DMA Transfer complete interrupt.
adustm 0:da04816fb411 230 * @param None
adustm 0:da04816fb411 231 * @retval None
adustm 0:da04816fb411 232 */
adustm 0:da04816fb411 233 void BSP_AUDIO_IN_TransferComplete_CallBack(void)
adustm 0:da04816fb411 234 {
Jerome Coutant 5:66c230f74325 235 audio_rec_buffer_state = BUFFER_OFFSET_FULL;
adustm 0:da04816fb411 236 }
adustm 0:da04816fb411 237
adustm 0:da04816fb411 238 /**
adustm 0:da04816fb411 239 * @brief Manages the DMA Half Transfer complete interrupt.
adustm 0:da04816fb411 240 * @param None
adustm 0:da04816fb411 241 * @retval None
adustm 0:da04816fb411 242 */
adustm 0:da04816fb411 243 void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
adustm 0:da04816fb411 244 {
Jerome Coutant 5:66c230f74325 245 audio_rec_buffer_state = BUFFER_OFFSET_HALF;
adustm 0:da04816fb411 246 }
adustm 0:da04816fb411 247
Jerome Coutant 5:66c230f74325 248 /**
Jerome Coutant 5:66c230f74325 249 * @brief Audio IN Error callback function.
Jerome Coutant 5:66c230f74325 250 * @param None
Jerome Coutant 5:66c230f74325 251 * @retval None
Jerome Coutant 5:66c230f74325 252 */
Jerome Coutant 5:66c230f74325 253 void BSP_AUDIO_IN_Error_CallBack(void)
adustm 0:da04816fb411 254 {
Jerome Coutant 5:66c230f74325 255 printf("BSP_AUDIO_IN_Error_CallBack\n");
adustm 0:da04816fb411 256 }