lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Committer:
bmazzeo
Date:
Mon Dec 30 18:34:21 2019 +0000
Revision:
14:18f159d48340
Parent:
13:61131aac4031
Child:
15:83e40ccc1b1f
Audio to float, Float to audio function first prototyped here.

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 13:61131aac4031 9 * This code was specifically developed for BYU ECEn 487 course
bmazzeo 13:61131aac4031 10 * Introduction to Digital Signal Processing.
bmazzeo 9:f5b37c71856d 11 *
bmazzeo 9:f5b37c71856d 12 *
bmazzeo 9:f5b37c71856d 13 ******************************************************************************
bmazzeo 9:f5b37c71856d 14 */
bmazzeo 9:f5b37c71856d 15
bmazzeo 9:f5b37c71856d 16
adustm 0:da04816fb411 17 #include "mbed.h"
Jerome Coutant 5:66c230f74325 18 #include "stm32746g_discovery_audio.h"
Jerome Coutant 5:66c230f74325 19 #include "stm32746g_discovery_sdram.h"
bmazzeo 6:e689075b04ed 20 #include "stm32746g_discovery_lcd.h"
adustm 0:da04816fb411 21
Jerome Coutant 5:66c230f74325 22 typedef enum {
adustm 0:da04816fb411 23 BUFFER_OFFSET_NONE = 0,
adustm 0:da04816fb411 24 BUFFER_OFFSET_HALF = 1,
adustm 0:da04816fb411 25 BUFFER_OFFSET_FULL = 2,
Jerome Coutant 5:66c230f74325 26 } BUFFER_StateTypeDef;
Jerome Coutant 5:66c230f74325 27
adustm 0:da04816fb411 28
bmazzeo 14:18f159d48340 29 #define HALF_AUDIO_BLOCK_SIZE ((uint32_t)128) // Number of samples (L and R) @ Frequency
bmazzeo 12:e44766b61346 30 #define AUDIO_BLOCK_SIZE ((uint32_t)256)
bmazzeo 6:e689075b04ed 31
bmazzeo 9:f5b37c71856d 32 #define SDRAM_DEVICE_ADDR_AUDIO_MEM ((uint32_t)0xC0400000)
bmazzeo 9:f5b37c71856d 33 #define AUDIO_BUFFER_IN SDRAM_DEVICE_ADDR_AUDIO_MEM
bmazzeo 9:f5b37c71856d 34 #define AUDIO_BUFFER_OUT (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE * 2))
Jerome Coutant 5:66c230f74325 35
bmazzeo 8:d1c41eca57f0 36 #define OSC_START_X_POS 20
bmazzeo 6:e689075b04ed 37 #define OSC_LINE_SIZE 256
bmazzeo 6:e689075b04ed 38 #define L_CHANNEL_Y_POS 120
bmazzeo 6:e689075b04ed 39 #define R_CHANNEL_Y_POS 220
bmazzeo 7:e1dfd64eba81 40 #define AUDIO_DRAW_LIMIT 30
bmazzeo 6:e689075b04ed 41
bmazzeo 9:f5b37c71856d 42 Timer timer;
bmazzeo 9:f5b37c71856d 43
Jerome Coutant 5:66c230f74325 44 volatile uint32_t audio_rec_buffer_state = BUFFER_OFFSET_NONE;
bmazzeo 7:e1dfd64eba81 45 static void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length);
bmazzeo 8:d1c41eca57f0 46 static void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length);
bmazzeo 14:18f159d48340 47 static void Audio_to_Float(uint16_t* buffer_in, uint16_t* L_out, uint16_t * R_out, uint16_t Length);
bmazzeo 14:18f159d48340 48 static void Float_to_Audio(uint16_t* L_in, uint16_t * R_in, uint16_t* buffer_out, uint16_t Length);
Jerome Coutant 5:66c230f74325 49
bmazzeo 14:18f159d48340 50 /* To do converstion to float */
bmazzeo 14:18f159d48340 51 float L_channel_float[HALF_AUDIO_BLOCK_SIZE];
bmazzeo 14:18f159d48340 52 float R_channel_float[HALF_AUDIO_BLOCK_SIZE];
bmazzeo 14:18f159d48340 53
bmazzeo 14:18f159d48340 54 /* Back conversion to integer */
bmazzeo 14:18f159d48340 55 int32_t Processed_audio[HALF_AUDIO_BLOCK_SIZE];
bmazzeo 14:18f159d48340 56
bmazzeo 14:18f159d48340 57 /* Useful variables during looping */
bmazzeo 9:f5b37c71856d 58 uint32_t counter = 0;
bmazzeo 10:a82b64ea1d11 59 char buf[40];
bmazzeo 13:61131aac4031 60 int first_half_time = 0;
bmazzeo 13:61131aac4031 61 int second_half_time = 0;
bmazzeo 13:61131aac4031 62 int total_time = 0;
bmazzeo 9:f5b37c71856d 63
adustm 0:da04816fb411 64 int main()
adustm 0:da04816fb411 65 {
bmazzeo 13:61131aac4031 66 /* Initialize the LCD Screen and display information */
bmazzeo 6:e689075b04ed 67 BSP_LCD_Init();
bmazzeo 6:e689075b04ed 68 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
bmazzeo 6:e689075b04ed 69 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
bmazzeo 6:e689075b04ed 70
bmazzeo 6:e689075b04ed 71 BSP_LCD_Clear(LCD_COLOR_BLACK);
bmazzeo 6:e689075b04ed 72 BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
bmazzeo 6:e689075b04ed 73
bmazzeo 6:e689075b04ed 74 BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
bmazzeo 6:e689075b04ed 75 BSP_LCD_SetTextColor(LCD_COLOR_ORANGE);
bmazzeo 7:e1dfd64eba81 76 BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"487 Mic Audio Test Code", LEFT_MODE);
bmazzeo 8:d1c41eca57f0 77 BSP_LCD_DisplayStringAt(0, L_CHANNEL_Y_POS, (uint8_t *)"L", LEFT_MODE);
bmazzeo 8:d1c41eca57f0 78 BSP_LCD_DisplayStringAt(0, R_CHANNEL_Y_POS, (uint8_t *)"R", LEFT_MODE);
bmazzeo 6:e689075b04ed 79
bmazzeo 13:61131aac4031 80
bmazzeo 13:61131aac4031 81 /* Initialize the Audio Interface */
Jerome Coutant 5:66c230f74325 82 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 83
adustm 0:da04816fb411 84 /* Initialize SDRAM buffers */
Jerome Coutant 5:66c230f74325 85 BSP_SDRAM_Init();
Jerome Coutant 5:66c230f74325 86 memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2);
Jerome Coutant 5:66c230f74325 87 memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2);
Jerome Coutant 5:66c230f74325 88
adustm 0:da04816fb411 89
adustm 0:da04816fb411 90 /* Start Recording */
bmazzeo 13:61131aac4031 91 if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) { printf("BSP_AUDIO_IN_Record error\n"); }
adustm 0:da04816fb411 92
adustm 0:da04816fb411 93 /* Start Playback */
Jerome Coutant 5:66c230f74325 94 BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
bmazzeo 13:61131aac4031 95 if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) { printf("BSP_AUDIO_OUT_Play error\n"); }
bmazzeo 13:61131aac4031 96
adustm 0:da04816fb411 97
bmazzeo 9:f5b37c71856d 98 timer.start();
adustm 0:da04816fb411 99 while (1) {
bmazzeo 6:e689075b04ed 100 /* First Half */
bmazzeo 13:61131aac4031 101 /* Wait end of half block recording before going on in the first half cycle*/
bmazzeo 11:4256dbbb0c89 102 while (audio_rec_buffer_state != BUFFER_OFFSET_HALF) {}
bmazzeo 9:f5b37c71856d 103
bmazzeo 13:61131aac4031 104 /* This captures the time of an entire cycle */
bmazzeo 9:f5b37c71856d 105 total_time = timer.read_us();
bmazzeo 13:61131aac4031 106
bmazzeo 13:61131aac4031 107 /* Reset the timer counter to zero */
bmazzeo 9:f5b37c71856d 108 timer.reset();
Jerome Coutant 5:66c230f74325 109
bmazzeo 13:61131aac4031 110 /* Plot traces of first half block recording */
bmazzeo 12:e44766b61346 111 Erase_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, HALF_AUDIO_BLOCK_SIZE);
bmazzeo 12:e44766b61346 112 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 113
bmazzeo 14:18f159d48340 114 /* Convert data to floating point representation for processing */
bmazzeo 14:18f159d48340 115 Audio_to_Float((uint16_t *) AUDIO_BUFFER_IN, (uint16_t *) L_channel_float, (uint16_t *) R_channel_float, HALF_AUDIO_BLOCK_SIZE);
bmazzeo 14:18f159d48340 116
bmazzeo 14:18f159d48340 117 Float_to_Audio((uint16_t *) L_channel_float, (uint16_t *) R_channel_float, (uint16_t *) Processed_audio, HALF_AUDIO_BLOCK_SIZE);
bmazzeo 14:18f159d48340 118
bmazzeo 13:61131aac4031 119 /* Copy recorded 1st half block into the audio buffer that goes out */
bmazzeo 13:61131aac4031 120 memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
bmazzeo 13:61131aac4031 121
bmazzeo 13:61131aac4031 122 /* Capture the timing of the first half processing */
bmazzeo 9:f5b37c71856d 123 first_half_time = timer.read_us();
bmazzeo 13:61131aac4031 124 /* End First Half */
bmazzeo 8:d1c41eca57f0 125
bmazzeo 6:e689075b04ed 126 /* Second Half */
adustm 0:da04816fb411 127 /* Wait end of one block recording */
bmazzeo 11:4256dbbb0c89 128 while (audio_rec_buffer_state != BUFFER_OFFSET_FULL) {}
bmazzeo 9:f5b37c71856d 129
bmazzeo 13:61131aac4031 130 /* Plot traces of second half block recording */
bmazzeo 12:e44766b61346 131 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 132 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 13:61131aac4031 133
bmazzeo 13:61131aac4031 134 /* Compute important cycle information and display it*/
bmazzeo 6:e689075b04ed 135 counter++;
bmazzeo 10:a82b64ea1d11 136 sprintf(buf, "Cycles: %9d", counter);
bmazzeo 6:e689075b04ed 137 BSP_LCD_SetTextColor(LCD_COLOR_RED);
bmazzeo 10:a82b64ea1d11 138 BSP_LCD_DisplayStringAt(0, 46, (uint8_t *) buf, LEFT_MODE);
bmazzeo 10:a82b64ea1d11 139 sprintf(buf, "1:%6d 2:%6d T:%6d", first_half_time, second_half_time, total_time);
bmazzeo 10:a82b64ea1d11 140 BSP_LCD_DisplayStringAt(0, 20, (uint8_t *) buf, LEFT_MODE);
bmazzeo 13:61131aac4031 141
bmazzeo 13:61131aac4031 142 /* Copy recorded 2nd half block into audio output buffer */
bmazzeo 13:61131aac4031 143 memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
bmazzeo 13:61131aac4031 144
bmazzeo 13:61131aac4031 145 /* Change the recording buffer state to reflect the status of the buffer */
bmazzeo 13:61131aac4031 146 audio_rec_buffer_state = BUFFER_OFFSET_NONE;
bmazzeo 9:f5b37c71856d 147
bmazzeo 13:61131aac4031 148 /* Measures the amount of time to process the second half */
bmazzeo 9:f5b37c71856d 149 second_half_time = timer.read_us();
bmazzeo 9:f5b37c71856d 150
bmazzeo 13:61131aac4031 151 /* End Second Half */
adustm 0:da04816fb411 152 }
adustm 0:da04816fb411 153 }
Jerome Coutant 5:66c230f74325 154
bmazzeo 7:e1dfd64eba81 155 /**
bmazzeo 7:e1dfd64eba81 156 * @brief Draws a trace of the data line.
bmazzeo 7:e1dfd64eba81 157 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 158 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 159 * @param R_Ypos: Right channel Y position
bmazzeo 7:e1dfd64eba81 160 * @param Mem_start: Start of memory location
bmazzeo 7:e1dfd64eba81 161 * @param Length: length of trace
bmazzeo 7:e1dfd64eba81 162 * @retval None
bmazzeo 7:e1dfd64eba81 163 */
bmazzeo 7:e1dfd64eba81 164 void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length)
bmazzeo 7:e1dfd64eba81 165 {
bmazzeo 7:e1dfd64eba81 166 BSP_LCD_SetTextColor(LCD_COLOR_BROWN);
bmazzeo 7:e1dfd64eba81 167 BSP_LCD_FillRect(Xpos, L_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 168 BSP_LCD_FillRect(Xpos, L_Ypos+1, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 169 BSP_LCD_FillRect(Xpos, R_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 170 BSP_LCD_FillRect(Xpos, R_Ypos+1, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 171
bmazzeo 7:e1dfd64eba81 172 BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
bmazzeo 8:d1c41eca57f0 173 BSP_LCD_DrawHLine(Xpos, L_CHANNEL_Y_POS, Length);
bmazzeo 8:d1c41eca57f0 174 BSP_LCD_DrawHLine(Xpos, R_CHANNEL_Y_POS, Length);
bmazzeo 7:e1dfd64eba81 175
bmazzeo 7:e1dfd64eba81 176 }
bmazzeo 7:e1dfd64eba81 177
Jerome Coutant 5:66c230f74325 178
bmazzeo 6:e689075b04ed 179 /**
bmazzeo 6:e689075b04ed 180 * @brief Draws a trace of the data line.
bmazzeo 6:e689075b04ed 181 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 182 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 183 * @param R_Ypos: Right channel Y position
bmazzeo 6:e689075b04ed 184 * @param Mem_start: Start of memory location
bmazzeo 6:e689075b04ed 185 * @param Length: length of trace
bmazzeo 6:e689075b04ed 186 * @retval None
bmazzeo 6:e689075b04ed 187 */
bmazzeo 8:d1c41eca57f0 188 void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length)
bmazzeo 6:e689075b04ed 189 {
bmazzeo 7:e1dfd64eba81 190 uint16_t i;
bmazzeo 7:e1dfd64eba81 191 uint32_t data_value;
bmazzeo 7:e1dfd64eba81 192 uint32_t* mem_address;
bmazzeo 7:e1dfd64eba81 193 char buf[10];
bmazzeo 7:e1dfd64eba81 194 int16_t L_audio_value;
bmazzeo 7:e1dfd64eba81 195 int16_t R_audio_value;
bmazzeo 6:e689075b04ed 196
bmazzeo 7:e1dfd64eba81 197 data_value = *((uint32_t*) Mem_start);
bmazzeo 7:e1dfd64eba81 198 L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 7:e1dfd64eba81 199 R_audio_value = (int16_t) (data_value & 0xFFFF);
bmazzeo 7:e1dfd64eba81 200
bmazzeo 8:d1c41eca57f0 201 //sprintf(buf, "%12d", data_value);
bmazzeo 8:d1c41eca57f0 202 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 8:d1c41eca57f0 203
bmazzeo 7:e1dfd64eba81 204 //sprintf(buf, "%12d", L_audio_value);
bmazzeo 7:e1dfd64eba81 205 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 6:e689075b04ed 206
bmazzeo 7:e1dfd64eba81 207 //sprintf(buf, "%12d", R_audio_value);
bmazzeo 7:e1dfd64eba81 208 //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 209
bmazzeo 8:d1c41eca57f0 210 for (i=0; i<Length; i++)
bmazzeo 6:e689075b04ed 211 {
bmazzeo 7:e1dfd64eba81 212 mem_address = (uint32_t*) Mem_start + i;
bmazzeo 7:e1dfd64eba81 213 data_value = *((uint32_t*) mem_address);
bmazzeo 7:e1dfd64eba81 214 L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 7:e1dfd64eba81 215 R_audio_value = (int16_t) (data_value & 0xFFFF);
bmazzeo 7:e1dfd64eba81 216
bmazzeo 7:e1dfd64eba81 217 L_audio_value = L_audio_value / 50;
bmazzeo 7:e1dfd64eba81 218 R_audio_value = R_audio_value / 50;
bmazzeo 7:e1dfd64eba81 219
bmazzeo 7:e1dfd64eba81 220 //sprintf(buf, "%12d", L_audio_value);
bmazzeo 7:e1dfd64eba81 221 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 222
bmazzeo 7:e1dfd64eba81 223 //sprintf(buf, "%12d", R_audio_value);
bmazzeo 7:e1dfd64eba81 224 //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 225
bmazzeo 7:e1dfd64eba81 226
bmazzeo 7:e1dfd64eba81 227 //L_audio_value = -i;
bmazzeo 7:e1dfd64eba81 228 if (L_audio_value > AUDIO_DRAW_LIMIT) {L_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 229 else if (L_audio_value < -AUDIO_DRAW_LIMIT) {L_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 230
bmazzeo 7:e1dfd64eba81 231 if (R_audio_value > AUDIO_DRAW_LIMIT) {R_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 232 else if (R_audio_value < -AUDIO_DRAW_LIMIT) {R_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 233
bmazzeo 7:e1dfd64eba81 234 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) L_Ypos + L_audio_value), LCD_COLOR_WHITE);
bmazzeo 7:e1dfd64eba81 235 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) R_Ypos + R_audio_value), LCD_COLOR_WHITE);
bmazzeo 6:e689075b04ed 236 }
bmazzeo 6:e689075b04ed 237
bmazzeo 6:e689075b04ed 238 }
bmazzeo 14:18f159d48340 239
bmazzeo 14:18f159d48340 240 /**
bmazzeo 14:18f159d48340 241 * @brief Converts audio data in buffer to floating point representation.
bmazzeo 14:18f159d48340 242 * @param buffer_in: Pointer to Audio buffer start location
bmazzeo 14:18f159d48340 243 * @param L_out: Pointer to Left channel out data (float)
bmazzeo 14:18f159d48340 244 * @param R_out: Pointer to Right channel out data (float)
bmazzeo 14:18f159d48340 245 * @param Length: length of data to convert
bmazzeo 14:18f159d48340 246 * @retval None
bmazzeo 14:18f159d48340 247 */
bmazzeo 14:18f159d48340 248 void Audio_to_Float(uint16_t* buffer_in, uint16_t* L_out, uint16_t * R_out, uint16_t Length)
bmazzeo 14:18f159d48340 249 {
bmazzeo 14:18f159d48340 250 uint16_t i;
bmazzeo 14:18f159d48340 251 uint32_t data_value;
bmazzeo 14:18f159d48340 252 uint16_t* audio_mem_address;
bmazzeo 14:18f159d48340 253 uint16_t* L_chan_mem_address;
bmazzeo 14:18f159d48340 254 uint16_t* R_chan_mem_address;
bmazzeo 14:18f159d48340 255 float L_audio_value;
bmazzeo 14:18f159d48340 256 float R_audio_value;
bmazzeo 6:e689075b04ed 257
bmazzeo 14:18f159d48340 258 for (i=0; i<Length; i++)
bmazzeo 14:18f159d48340 259 {
bmazzeo 14:18f159d48340 260 audio_mem_address = (uint16_t*) buffer_in + i;
bmazzeo 14:18f159d48340 261 L_chan_mem_address = (uint16_t*) L_out + i;
bmazzeo 14:18f159d48340 262 R_chan_mem_address = (uint16_t*) R_out + i;
bmazzeo 14:18f159d48340 263 data_value = *((uint16_t*) audio_mem_address);
bmazzeo 14:18f159d48340 264 L_audio_value = (float) ((int16_t) ((data_value >> 16) & 0xFFFF));
bmazzeo 14:18f159d48340 265 R_audio_value = (float) ((int16_t) (data_value & 0xFFFF));
bmazzeo 14:18f159d48340 266
bmazzeo 14:18f159d48340 267 *L_chan_mem_address = L_audio_value;
bmazzeo 14:18f159d48340 268 *R_chan_mem_address = R_audio_value;
bmazzeo 14:18f159d48340 269 }
bmazzeo 14:18f159d48340 270 }
bmazzeo 14:18f159d48340 271
bmazzeo 14:18f159d48340 272 /**
bmazzeo 14:18f159d48340 273 * @brief Converts audio data in buffer to floating point representation.
bmazzeo 14:18f159d48340 274 * @param L_out: Pointer to Left channel in data (float)
bmazzeo 14:18f159d48340 275 * @param R_out: Pointer to Right channel in data (float)
bmazzeo 14:18f159d48340 276 * @param buffer_out: Pointer to combined 32 bit (two 16-bit int samples)
bmazzeo 14:18f159d48340 277 * @param Length: length of data to convert
bmazzeo 14:18f159d48340 278 * @retval None
bmazzeo 14:18f159d48340 279 */
bmazzeo 14:18f159d48340 280 void Float_to_Audio(uint16_t* L_in, uint16_t * R_in, uint16_t* buffer_out, uint16_t Length)
bmazzeo 14:18f159d48340 281 {
bmazzeo 14:18f159d48340 282 uint16_t i;
bmazzeo 14:18f159d48340 283 uint32_t data_value;
bmazzeo 14:18f159d48340 284 uint16_t* audio_mem_address;
bmazzeo 14:18f159d48340 285 uint16_t* L_chan_mem_address;
bmazzeo 14:18f159d48340 286 uint16_t* R_chan_mem_address;
bmazzeo 14:18f159d48340 287 float L_audio_value;
bmazzeo 14:18f159d48340 288 float R_audio_value;
bmazzeo 14:18f159d48340 289
bmazzeo 14:18f159d48340 290 for (i=0; i<Length; i++)
bmazzeo 14:18f159d48340 291 {
bmazzeo 14:18f159d48340 292 L_chan_mem_address = (uint16_t*) L_in + i;
bmazzeo 14:18f159d48340 293 R_chan_mem_address = (uint16_t*) R_in + i;
bmazzeo 14:18f159d48340 294 audio_mem_address = (uint16_t*) buffer_out + i;
bmazzeo 14:18f159d48340 295
bmazzeo 14:18f159d48340 296 L_audio_value = *((uint16_t*) L_chan_mem_address);
bmazzeo 14:18f159d48340 297 R_audio_value = *((uint16_t*) R_chan_mem_address);
bmazzeo 14:18f159d48340 298
bmazzeo 14:18f159d48340 299 data_value = (((uint32_t) ((int16_t) L_audio_value)) << 16) | ((uint32_t) ((int16_t) R_audio_value));
bmazzeo 14:18f159d48340 300 *audio_mem_address = data_value;
bmazzeo 14:18f159d48340 301 }
bmazzeo 14:18f159d48340 302 }
bmazzeo 14:18f159d48340 303
bmazzeo 14:18f159d48340 304
bmazzeo 14:18f159d48340 305
bmazzeo 6:e689075b04ed 306
bmazzeo 6:e689075b04ed 307
adustm 0:da04816fb411 308 /*-------------------------------------------------------------------------------------
adustm 0:da04816fb411 309 Callbacks implementation:
adustm 0:da04816fb411 310 the callbacks API are defined __weak in the stm32746g_discovery_audio.c file
adustm 0:da04816fb411 311 and their implementation should be done in the user code if they are needed.
adustm 0:da04816fb411 312 Below some examples of callback implementations.
adustm 0:da04816fb411 313 -------------------------------------------------------------------------------------*/
adustm 0:da04816fb411 314 /**
adustm 0:da04816fb411 315 * @brief Manages the DMA Transfer complete interrupt.
adustm 0:da04816fb411 316 * @param None
adustm 0:da04816fb411 317 * @retval None
adustm 0:da04816fb411 318 */
adustm 0:da04816fb411 319 void BSP_AUDIO_IN_TransferComplete_CallBack(void)
adustm 0:da04816fb411 320 {
Jerome Coutant 5:66c230f74325 321 audio_rec_buffer_state = BUFFER_OFFSET_FULL;
adustm 0:da04816fb411 322 }
adustm 0:da04816fb411 323
adustm 0:da04816fb411 324 /**
adustm 0:da04816fb411 325 * @brief Manages the DMA Half Transfer complete interrupt.
adustm 0:da04816fb411 326 * @param None
adustm 0:da04816fb411 327 * @retval None
adustm 0:da04816fb411 328 */
adustm 0:da04816fb411 329 void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
adustm 0:da04816fb411 330 {
Jerome Coutant 5:66c230f74325 331 audio_rec_buffer_state = BUFFER_OFFSET_HALF;
adustm 0:da04816fb411 332 }
adustm 0:da04816fb411 333
Jerome Coutant 5:66c230f74325 334 /**
Jerome Coutant 5:66c230f74325 335 * @brief Audio IN Error callback function.
Jerome Coutant 5:66c230f74325 336 * @param None
Jerome Coutant 5:66c230f74325 337 * @retval None
Jerome Coutant 5:66c230f74325 338 */
Jerome Coutant 5:66c230f74325 339 void BSP_AUDIO_IN_Error_CallBack(void)
adustm 0:da04816fb411 340 {
Jerome Coutant 5:66c230f74325 341 printf("BSP_AUDIO_IN_Error_CallBack\n");
adustm 0:da04816fb411 342 }