lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Committer:
bmazzeo
Date:
Sat Dec 28 21:11:56 2019 +0000
Revision:
7:e1dfd64eba81
Parent:
6:e689075b04ed
Child:
8:d1c41eca57f0
Graphics (oscilloscope working) engaged and showing microphone data.

Who changed what in which revision?

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