lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Committer:
bmazzeo
Date:
Sun Dec 29 06:59:45 2019 +0000
Revision:
12:e44766b61346
Parent:
11:4256dbbb0c89
Child:
13:61131aac4031
Working with sampling and displaying.

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