lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Committer:
bmazzeo
Date:
Tue Dec 31 22:45:31 2019 +0000
Revision:
23:d938f87dd1ee
Parent:
22:f36d7a53bb7e
Child:
25:5412779376a7
Successful inclusion of signal_processing.cpp to handle all of the processing for both blocks simply.

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