lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Committer:
bmazzeo
Date:
Fri Jan 03 20:15:03 2020 +0000
Revision:
30:debea332cdfe
Parent:
26:023edf8cea6c
Child:
31:5a0235b66851
Code with more extensive commenting. Set to 2 kHz carrier. Working well.

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
bmazzeo 30:debea332cdfe 24 /* The following type definitions are used to control the
bmazzeo 30:debea332cdfe 25 * buffering of the audio data using a double buffering technique.
bmazzeo 30:debea332cdfe 26 * Most of the transactions between the WM8994 and the microcontroller
bmazzeo 30:debea332cdfe 27 * are handled by other code - but this signals what the buffering state
bmazzeo 30:debea332cdfe 28 * is, so the data can be appropriately processed. */
Jerome Coutant 5:66c230f74325 29 typedef enum {
adustm 0:da04816fb411 30 BUFFER_OFFSET_NONE = 0,
adustm 0:da04816fb411 31 BUFFER_OFFSET_HALF = 1,
adustm 0:da04816fb411 32 BUFFER_OFFSET_FULL = 2,
Jerome Coutant 5:66c230f74325 33 } BUFFER_StateTypeDef;
Jerome Coutant 5:66c230f74325 34
bmazzeo 30:debea332cdfe 35 /* These audio block samples define the size of the buffering */
bmazzeo 19:479f611941a8 36 #define AUDIO_BLOCK_SAMPLES ((uint32_t)128) // Number of samples (L and R) in audio block (each samples is 16 bits)
bmazzeo 19:479f611941a8 37 #define AUDIO_BLOCK_SIZE ((uint32_t)512) // Number of bytes in audio block (4 * AUDIO_BLOCK_SAMPLES)
bmazzeo 6:e689075b04ed 38
bmazzeo 30:debea332cdfe 39 /* These RAM addresses are important to determine where the audio data is stored. */
bmazzeo 9:f5b37c71856d 40 #define SDRAM_DEVICE_ADDR_AUDIO_MEM ((uint32_t)0xC0400000)
bmazzeo 9:f5b37c71856d 41 #define AUDIO_BUFFER_IN SDRAM_DEVICE_ADDR_AUDIO_MEM
bmazzeo 9:f5b37c71856d 42 #define AUDIO_BUFFER_OUT (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE * 2))
Jerome Coutant 5:66c230f74325 43
bmazzeo 30:debea332cdfe 44 /* These definitions define the size of the oscilloscope that is used to display data. */
bmazzeo 8:d1c41eca57f0 45 #define OSC_START_X_POS 20
bmazzeo 6:e689075b04ed 46 #define OSC_LINE_SIZE 256
bmazzeo 17:eb85a2387dd4 47 #define OSC_Y_POS 110
bmazzeo 7:e1dfd64eba81 48 #define AUDIO_DRAW_LIMIT 30
bmazzeo 6:e689075b04ed 49
bmazzeo 30:debea332cdfe 50 /* This define a timer that is then used to record the timing of the different processing stages. */
bmazzeo 9:f5b37c71856d 51 Timer timer;
bmazzeo 9:f5b37c71856d 52
bmazzeo 30:debea332cdfe 53 /* This variable is important because it define the audio buffer recording state. */
Jerome Coutant 5:66c230f74325 54 volatile uint32_t audio_rec_buffer_state = BUFFER_OFFSET_NONE;
bmazzeo 30:debea332cdfe 55
bmazzeo 30:debea332cdfe 56 /* Function declarations */
bmazzeo 17:eb85a2387dd4 57 static void Erase_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t Length);
bmazzeo 17:eb85a2387dd4 58 static void Draw_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t* Mem_start, uint16_t Length);
bmazzeo 16:b7dca59ab076 59 static void Audio_to_Float(uint16_t* buffer_in, float* L_out, float* R_out, uint16_t Length);
bmazzeo 16:b7dca59ab076 60 static void Float_to_Audio(float* L_in, float* R_in, uint16_t* buffer_out, uint16_t Length);
Jerome Coutant 5:66c230f74325 61
bmazzeo 30:debea332cdfe 62 /* These memory blocks are important for converting to floating point representation. */
bmazzeo 19:479f611941a8 63 float L_channel_float[AUDIO_BLOCK_SAMPLES];
bmazzeo 19:479f611941a8 64 float R_channel_float[AUDIO_BLOCK_SAMPLES];
bmazzeo 20:2ecdf687a2d1 65 float *L_channel_float_p = &L_channel_float[0];
bmazzeo 20:2ecdf687a2d1 66 float *R_channel_float_p = &R_channel_float[0];
bmazzeo 14:18f159d48340 67
bmazzeo 30:debea332cdfe 68 /* These memory blocks are where the information is stored to send back out to the WM8994 chip. */
bmazzeo 22:f36d7a53bb7e 69 uint16_t Processed_audio[AUDIO_BLOCK_SAMPLES];
bmazzeo 20:2ecdf687a2d1 70 uint16_t *Processed_audio_p = &Processed_audio[0];
bmazzeo 14:18f159d48340 71
bmazzeo 14:18f159d48340 72 /* Useful variables during looping */
bmazzeo 30:debea332cdfe 73 uint32_t counter = 0; // Loop counter
bmazzeo 30:debea332cdfe 74 char buf[40]; // Character buffer for sprintf statements to the LCD
bmazzeo 30:debea332cdfe 75 int first_half_time = 0; // Time of first processing block
bmazzeo 30:debea332cdfe 76 int second_half_time = 0; // Time of second processing block
bmazzeo 30:debea332cdfe 77 int total_time = 0; // Time of total loop (first and second blocks)
bmazzeo 9:f5b37c71856d 78
bmazzeo 30:debea332cdfe 79 /* Main Function */
adustm 0:da04816fb411 80 int main()
adustm 0:da04816fb411 81 {
bmazzeo 13:61131aac4031 82 /* Initialize the LCD Screen and display information */
bmazzeo 6:e689075b04ed 83 BSP_LCD_Init();
bmazzeo 6:e689075b04ed 84 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
bmazzeo 6:e689075b04ed 85 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
bmazzeo 6:e689075b04ed 86
bmazzeo 30:debea332cdfe 87 /* Clear the LCD and set the font to be default */
bmazzeo 6:e689075b04ed 88 BSP_LCD_Clear(LCD_COLOR_BLACK);
bmazzeo 6:e689075b04ed 89 BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
bmazzeo 30:debea332cdfe 90
bmazzeo 30:debea332cdfe 91 /* Set the backcolor to be black and the textcolor to be orange. */
bmazzeo 6:e689075b04ed 92 BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
bmazzeo 6:e689075b04ed 93 BSP_LCD_SetTextColor(LCD_COLOR_ORANGE);
bmazzeo 30:debea332cdfe 94
bmazzeo 30:debea332cdfe 95 /* The following are static display elements that will remain on the screen. */
bmazzeo 30:debea332cdfe 96 BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"487 Demo Code (Mazzeo)", LEFT_MODE);
bmazzeo 30:debea332cdfe 97
bmazzeo 30:debea332cdfe 98 /* Display the L and R colors for the channels */
bmazzeo 17:eb85a2387dd4 99 BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
bmazzeo 17:eb85a2387dd4 100 BSP_LCD_DisplayStringAt(0, OSC_Y_POS - 20, (uint8_t *)"L", LEFT_MODE);
bmazzeo 17:eb85a2387dd4 101 BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
bmazzeo 17:eb85a2387dd4 102 BSP_LCD_DisplayStringAt(0, OSC_Y_POS, (uint8_t *)"R", LEFT_MODE);
bmazzeo 6:e689075b04ed 103
bmazzeo 30:debea332cdfe 104 /* The following code should not be code that you need to worry about - it sets up the audio interfaces. */
bmazzeo 30:debea332cdfe 105 /* Initialize the Audio Interface */
bmazzeo 30:debea332cdfe 106 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);
bmazzeo 30:debea332cdfe 107
bmazzeo 30:debea332cdfe 108 /* Initialize SDRAM buffers */
bmazzeo 30:debea332cdfe 109 BSP_SDRAM_Init();
bmazzeo 30:debea332cdfe 110 memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2);
bmazzeo 30:debea332cdfe 111 memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2);
bmazzeo 30:debea332cdfe 112
bmazzeo 30:debea332cdfe 113 /* Start Recording */
bmazzeo 30:debea332cdfe 114 if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) { printf("BSP_AUDIO_IN_Record error\n"); }
bmazzeo 30:debea332cdfe 115
bmazzeo 30:debea332cdfe 116 /* Start Playback */
bmazzeo 30:debea332cdfe 117 BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
bmazzeo 30:debea332cdfe 118 if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) { printf("BSP_AUDIO_OUT_Play error\n"); }
bmazzeo 30:debea332cdfe 119
bmazzeo 30:debea332cdfe 120 /* The audio interfaces are all now working.
bmazzeo 30:debea332cdfe 121 * Importantly - AUDIO_BUFFER_IN is the pointer to the incoming data from the WM8994
bmazzeo 30:debea332cdfe 122 * AUDIO_BUFFER_OUT is the pointer to the outgoing data to the WM8994 */
Jerome Coutant 5:66c230f74325 123
adustm 0:da04816fb411 124
bmazzeo 30:debea332cdfe 125 /* Initialize signal processing filters - usually there are variables that
bmazzeo 30:debea332cdfe 126 * need to be set up or that there are arrays you need to precompute - this
bmazzeo 30:debea332cdfe 127 * function call allows you to do that. */
bmazzeo 25:5412779376a7 128 initalize_signal_processing();
adustm 0:da04816fb411 129
bmazzeo 30:debea332cdfe 130 /* Hardware timer starts. Set to zero */
bmazzeo 9:f5b37c71856d 131 timer.start();
bmazzeo 30:debea332cdfe 132
bmazzeo 30:debea332cdfe 133 /* Main signal processing while loop */
adustm 0:da04816fb411 134 while (1) {
bmazzeo 30:debea332cdfe 135
bmazzeo 6:e689075b04ed 136 /* First Half */
bmazzeo 30:debea332cdfe 137 /* Wait until end of half block recording before going on in the first half cycle*/
bmazzeo 11:4256dbbb0c89 138 while (audio_rec_buffer_state != BUFFER_OFFSET_HALF) {}
bmazzeo 9:f5b37c71856d 139
bmazzeo 13:61131aac4031 140 /* This captures the time of an entire cycle */
bmazzeo 9:f5b37c71856d 141 total_time = timer.read_us();
bmazzeo 13:61131aac4031 142
bmazzeo 13:61131aac4031 143 /* Reset the timer counter to zero */
bmazzeo 9:f5b37c71856d 144 timer.reset();
Jerome Coutant 5:66c230f74325 145
bmazzeo 13:61131aac4031 146 /* Plot traces of first half block recording */
bmazzeo 19:479f611941a8 147 Erase_Trace(OSC_START_X_POS, OSC_Y_POS, AUDIO_BLOCK_SAMPLES);
bmazzeo 19:479f611941a8 148 Draw_Trace(OSC_START_X_POS, OSC_Y_POS, (uint16_t *) AUDIO_BUFFER_IN, AUDIO_BLOCK_SAMPLES);
bmazzeo 18:255d15af49f2 149
bmazzeo 14:18f159d48340 150 /* Convert data to floating point representation for processing */
bmazzeo 20:2ecdf687a2d1 151 Audio_to_Float((uint16_t *) AUDIO_BUFFER_IN, L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES);
bmazzeo 22:f36d7a53bb7e 152
bmazzeo 30:debea332cdfe 153 /* ------------------------------------------------------------------------ */
bmazzeo 30:debea332cdfe 154 /* Here is where signal processing can be done on the floating point arrays */
bmazzeo 22:f36d7a53bb7e 155
bmazzeo 30:debea332cdfe 156 process_audio_channel_signals(L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES);
bmazzeo 20:2ecdf687a2d1 157
bmazzeo 30:debea332cdfe 158 /* Here is where signal processing can end on the floating point arrays */
bmazzeo 30:debea332cdfe 159 /* -------------------------------------------------------------------- */
bmazzeo 20:2ecdf687a2d1 160
bmazzeo 22:f36d7a53bb7e 161 /* Covert floating point data back to fixed point audio format */
bmazzeo 21:4dbfda694c0d 162 Float_to_Audio(L_channel_float_p, R_channel_float_p, (uint16_t *) Processed_audio, AUDIO_BLOCK_SAMPLES);
bmazzeo 14:18f159d48340 163
bmazzeo 13:61131aac4031 164 /* Copy recorded 1st half block into the audio buffer that goes out */
bmazzeo 21:4dbfda694c0d 165 memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(Processed_audio), AUDIO_BLOCK_SIZE);
bmazzeo 13:61131aac4031 166
bmazzeo 26:023edf8cea6c 167 /* Display useful cycle information (split up information display so the processing is more balanced) */
bmazzeo 26:023edf8cea6c 168 sprintf(buf, "Cycles: %9d", counter);
bmazzeo 26:023edf8cea6c 169 BSP_LCD_SetTextColor(LCD_COLOR_RED);
bmazzeo 26:023edf8cea6c 170 BSP_LCD_DisplayStringAt(0, 46, (uint8_t *) buf, LEFT_MODE);
bmazzeo 26:023edf8cea6c 171
bmazzeo 26:023edf8cea6c 172
bmazzeo 13:61131aac4031 173 /* Capture the timing of the first half processing */
bmazzeo 9:f5b37c71856d 174 first_half_time = timer.read_us();
bmazzeo 13:61131aac4031 175 /* End First Half */
bmazzeo 8:d1c41eca57f0 176
bmazzeo 6:e689075b04ed 177 /* Second Half */
adustm 0:da04816fb411 178 /* Wait end of one block recording */
bmazzeo 11:4256dbbb0c89 179 while (audio_rec_buffer_state != BUFFER_OFFSET_FULL) {}
bmazzeo 9:f5b37c71856d 180
bmazzeo 13:61131aac4031 181 /* Plot traces of second half block recording */
bmazzeo 19:479f611941a8 182 Erase_Trace(OSC_START_X_POS+AUDIO_BLOCK_SAMPLES, OSC_Y_POS, AUDIO_BLOCK_SAMPLES);
bmazzeo 19:479f611941a8 183 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 184
bmazzeo 22:f36d7a53bb7e 185 /* Convert data to floating point representation for processing */
bmazzeo 22:f36d7a53bb7e 186 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 187
bmazzeo 30:debea332cdfe 188 /* ------------------------------------------------------------------------ */
bmazzeo 30:debea332cdfe 189 /* Here is where signal processing can be done on the floating point arrays */
bmazzeo 22:f36d7a53bb7e 190
bmazzeo 30:debea332cdfe 191 process_audio_channel_signals(L_channel_float_p, R_channel_float_p, AUDIO_BLOCK_SAMPLES);
bmazzeo 22:f36d7a53bb7e 192
bmazzeo 30:debea332cdfe 193 /* Here is where signal processing can end on the floating point arrays */
bmazzeo 30:debea332cdfe 194 /* -------------------------------------------------------------------- */
bmazzeo 30:debea332cdfe 195
bmazzeo 22:f36d7a53bb7e 196 /* Covert floating point data back to fixed point audio format */
bmazzeo 22:f36d7a53bb7e 197 Float_to_Audio(L_channel_float_p, R_channel_float_p, (uint16_t *) Processed_audio, AUDIO_BLOCK_SAMPLES);
bmazzeo 22:f36d7a53bb7e 198
bmazzeo 22:f36d7a53bb7e 199 /* Copy recorded 2nd half block into the audio buffer that goes out */
bmazzeo 22:f36d7a53bb7e 200 memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *) (Processed_audio), AUDIO_BLOCK_SIZE);
bmazzeo 13:61131aac4031 201
bmazzeo 13:61131aac4031 202 /* Compute important cycle information and display it*/
bmazzeo 26:023edf8cea6c 203 sprintf(buf, "1:%6d 2:%6d T:%6d", first_half_time, second_half_time, total_time);
bmazzeo 6:e689075b04ed 204 BSP_LCD_SetTextColor(LCD_COLOR_RED);
bmazzeo 10:a82b64ea1d11 205 BSP_LCD_DisplayStringAt(0, 20, (uint8_t *) buf, LEFT_MODE);
bmazzeo 13:61131aac4031 206
bmazzeo 13:61131aac4031 207 /* Copy recorded 2nd half block into audio output buffer */
bmazzeo 22:f36d7a53bb7e 208 //memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
bmazzeo 18:255d15af49f2 209
bmazzeo 13:61131aac4031 210 /* Change the recording buffer state to reflect the status of the buffer */
bmazzeo 13:61131aac4031 211 audio_rec_buffer_state = BUFFER_OFFSET_NONE;
bmazzeo 26:023edf8cea6c 212
bmazzeo 26:023edf8cea6c 213 /* Increase the counter */
bmazzeo 26:023edf8cea6c 214 counter++;
bmazzeo 9:f5b37c71856d 215
bmazzeo 13:61131aac4031 216 /* Measures the amount of time to process the second half */
bmazzeo 9:f5b37c71856d 217 second_half_time = timer.read_us();
bmazzeo 9:f5b37c71856d 218
bmazzeo 13:61131aac4031 219 /* End Second Half */
adustm 0:da04816fb411 220 }
adustm 0:da04816fb411 221 }
Jerome Coutant 5:66c230f74325 222
bmazzeo 7:e1dfd64eba81 223 /**
bmazzeo 7:e1dfd64eba81 224 * @brief Draws a trace of the data line.
bmazzeo 7:e1dfd64eba81 225 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 226 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 227 * @param R_Ypos: Right channel Y position
bmazzeo 7:e1dfd64eba81 228 * @param Mem_start: Start of memory location
bmazzeo 7:e1dfd64eba81 229 * @param Length: length of trace
bmazzeo 7:e1dfd64eba81 230 * @retval None
bmazzeo 7:e1dfd64eba81 231 */
bmazzeo 17:eb85a2387dd4 232 void Erase_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t Length)
bmazzeo 7:e1dfd64eba81 233 {
bmazzeo 22:f36d7a53bb7e 234 /* Creates a brown rectangle above and below the axis */
bmazzeo 7:e1dfd64eba81 235 BSP_LCD_SetTextColor(LCD_COLOR_BROWN);
bmazzeo 17:eb85a2387dd4 236 BSP_LCD_FillRect(Xpos, Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
bmazzeo 17:eb85a2387dd4 237 BSP_LCD_FillRect(Xpos, Ypos+1, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 238
bmazzeo 22:f36d7a53bb7e 239 /* Draw axis for plotting */
bmazzeo 17:eb85a2387dd4 240 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
bmazzeo 17:eb85a2387dd4 241 BSP_LCD_DrawHLine(Xpos, Ypos, Length);
bmazzeo 7:e1dfd64eba81 242
bmazzeo 7:e1dfd64eba81 243 }
bmazzeo 7:e1dfd64eba81 244
Jerome Coutant 5:66c230f74325 245
bmazzeo 6:e689075b04ed 246 /**
bmazzeo 6:e689075b04ed 247 * @brief Draws a trace of the data line.
bmazzeo 6:e689075b04ed 248 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 249 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 250 * @param R_Ypos: Right channel Y position
bmazzeo 6:e689075b04ed 251 * @param Mem_start: Start of memory location
bmazzeo 6:e689075b04ed 252 * @param Length: length of trace
bmazzeo 6:e689075b04ed 253 * @retval None
bmazzeo 6:e689075b04ed 254 */
bmazzeo 17:eb85a2387dd4 255 void Draw_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t* Mem_start, uint16_t Length)
bmazzeo 6:e689075b04ed 256 {
bmazzeo 7:e1dfd64eba81 257 uint16_t i;
bmazzeo 18:255d15af49f2 258 uint16_t* mem_address;
bmazzeo 7:e1dfd64eba81 259 char buf[10];
bmazzeo 7:e1dfd64eba81 260 int16_t L_audio_value;
bmazzeo 7:e1dfd64eba81 261 int16_t R_audio_value;
bmazzeo 20:2ecdf687a2d1 262
bmazzeo 18:255d15af49f2 263 mem_address = Mem_start;
bmazzeo 7:e1dfd64eba81 264
bmazzeo 8:d1c41eca57f0 265 for (i=0; i<Length; i++)
bmazzeo 20:2ecdf687a2d1 266 {
bmazzeo 18:255d15af49f2 267 L_audio_value = (int16_t) *mem_address;
bmazzeo 18:255d15af49f2 268 mem_address++;
bmazzeo 18:255d15af49f2 269 R_audio_value = (int16_t) *mem_address;
bmazzeo 18:255d15af49f2 270 mem_address++;
bmazzeo 7:e1dfd64eba81 271
bmazzeo 17:eb85a2387dd4 272 L_audio_value = L_audio_value / 100;
bmazzeo 17:eb85a2387dd4 273 R_audio_value = R_audio_value / 100;
bmazzeo 7:e1dfd64eba81 274
bmazzeo 7:e1dfd64eba81 275 if (L_audio_value > AUDIO_DRAW_LIMIT) {L_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 276 else if (L_audio_value < -AUDIO_DRAW_LIMIT) {L_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 277
bmazzeo 7:e1dfd64eba81 278 if (R_audio_value > AUDIO_DRAW_LIMIT) {R_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 279 else if (R_audio_value < -AUDIO_DRAW_LIMIT) {R_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 280
bmazzeo 17:eb85a2387dd4 281 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) Ypos + L_audio_value), LCD_COLOR_BLUE);
bmazzeo 17:eb85a2387dd4 282 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) Ypos + R_audio_value), LCD_COLOR_GREEN);
bmazzeo 6:e689075b04ed 283 }
bmazzeo 6:e689075b04ed 284
bmazzeo 6:e689075b04ed 285 }
bmazzeo 14:18f159d48340 286
bmazzeo 14:18f159d48340 287 /**
bmazzeo 14:18f159d48340 288 * @brief Converts audio data in buffer to floating point representation.
bmazzeo 14:18f159d48340 289 * @param buffer_in: Pointer to Audio buffer start location
bmazzeo 14:18f159d48340 290 * @param L_out: Pointer to Left channel out data (float)
bmazzeo 14:18f159d48340 291 * @param R_out: Pointer to Right channel out data (float)
bmazzeo 14:18f159d48340 292 * @param Length: length of data to convert
bmazzeo 14:18f159d48340 293 * @retval None
bmazzeo 14:18f159d48340 294 */
bmazzeo 16:b7dca59ab076 295 void Audio_to_Float(uint16_t* buffer_in, float* L_out, float* R_out, uint16_t Length)
bmazzeo 14:18f159d48340 296 {
bmazzeo 14:18f159d48340 297 uint16_t i;
bmazzeo 16:b7dca59ab076 298 uint16_t* audio_mem_address;
bmazzeo 20:2ecdf687a2d1 299 float* L_chan_mem_address;
bmazzeo 20:2ecdf687a2d1 300 float* R_chan_mem_address;
bmazzeo 14:18f159d48340 301 float L_audio_value;
bmazzeo 14:18f159d48340 302 float R_audio_value;
bmazzeo 20:2ecdf687a2d1 303
bmazzeo 20:2ecdf687a2d1 304 audio_mem_address = buffer_in;
bmazzeo 20:2ecdf687a2d1 305 L_chan_mem_address = L_out;
bmazzeo 20:2ecdf687a2d1 306 R_chan_mem_address = R_out;
bmazzeo 6:e689075b04ed 307
bmazzeo 14:18f159d48340 308 for (i=0; i<Length; i++)
bmazzeo 14:18f159d48340 309 {
bmazzeo 20:2ecdf687a2d1 310 L_audio_value = (float) ((int16_t) *audio_mem_address);
bmazzeo 20:2ecdf687a2d1 311 audio_mem_address++;
bmazzeo 20:2ecdf687a2d1 312 R_audio_value = (float) ((int16_t) *audio_mem_address);
bmazzeo 20:2ecdf687a2d1 313 audio_mem_address++;
bmazzeo 14:18f159d48340 314
bmazzeo 16:b7dca59ab076 315 *L_chan_mem_address = L_audio_value;
bmazzeo 20:2ecdf687a2d1 316 L_chan_mem_address++;
bmazzeo 20:2ecdf687a2d1 317
bmazzeo 14:18f159d48340 318 *R_chan_mem_address = R_audio_value;
bmazzeo 20:2ecdf687a2d1 319 R_chan_mem_address++;
bmazzeo 14:18f159d48340 320 }
bmazzeo 14:18f159d48340 321 }
bmazzeo 14:18f159d48340 322
bmazzeo 14:18f159d48340 323 /**
bmazzeo 14:18f159d48340 324 * @brief Converts audio data in buffer to floating point representation.
bmazzeo 14:18f159d48340 325 * @param L_out: Pointer to Left channel in data (float)
bmazzeo 14:18f159d48340 326 * @param R_out: Pointer to Right channel in data (float)
bmazzeo 14:18f159d48340 327 * @param buffer_out: Pointer to combined 32 bit (two 16-bit int samples)
bmazzeo 14:18f159d48340 328 * @param Length: length of data to convert
bmazzeo 14:18f159d48340 329 * @retval None
bmazzeo 14:18f159d48340 330 */
bmazzeo 16:b7dca59ab076 331 void Float_to_Audio(float* L_in, float* R_in, uint16_t* buffer_out, uint16_t Length)
bmazzeo 14:18f159d48340 332 {
bmazzeo 14:18f159d48340 333 uint16_t i;
bmazzeo 14:18f159d48340 334 uint16_t* audio_mem_address;
bmazzeo 21:4dbfda694c0d 335 float* L_chan_mem_address;
bmazzeo 21:4dbfda694c0d 336 float* R_chan_mem_address;
bmazzeo 21:4dbfda694c0d 337 int16_t L_audio_value;
bmazzeo 21:4dbfda694c0d 338 int16_t R_audio_value;
bmazzeo 21:4dbfda694c0d 339
bmazzeo 21:4dbfda694c0d 340 audio_mem_address = buffer_out;
bmazzeo 21:4dbfda694c0d 341 L_chan_mem_address = L_in;
bmazzeo 21:4dbfda694c0d 342 R_chan_mem_address = R_in;
bmazzeo 14:18f159d48340 343
bmazzeo 14:18f159d48340 344 for (i=0; i<Length; i++)
bmazzeo 14:18f159d48340 345 {
bmazzeo 21:4dbfda694c0d 346 L_audio_value = (int16_t) ((float) *L_chan_mem_address);
bmazzeo 21:4dbfda694c0d 347 L_chan_mem_address++;
bmazzeo 14:18f159d48340 348
bmazzeo 21:4dbfda694c0d 349 R_audio_value = (int16_t) ((float) *R_chan_mem_address);
bmazzeo 21:4dbfda694c0d 350 R_chan_mem_address++;
bmazzeo 21:4dbfda694c0d 351
bmazzeo 21:4dbfda694c0d 352 *audio_mem_address = (uint16_t) L_audio_value;
bmazzeo 21:4dbfda694c0d 353 audio_mem_address++;
bmazzeo 21:4dbfda694c0d 354 *audio_mem_address = (uint16_t) R_audio_value;
bmazzeo 21:4dbfda694c0d 355 audio_mem_address++;
bmazzeo 14:18f159d48340 356 }
bmazzeo 14:18f159d48340 357 }
bmazzeo 14:18f159d48340 358
bmazzeo 14:18f159d48340 359
bmazzeo 14:18f159d48340 360
bmazzeo 6:e689075b04ed 361
bmazzeo 6:e689075b04ed 362
adustm 0:da04816fb411 363 /*-------------------------------------------------------------------------------------
adustm 0:da04816fb411 364 Callbacks implementation:
adustm 0:da04816fb411 365 the callbacks API are defined __weak in the stm32746g_discovery_audio.c file
adustm 0:da04816fb411 366 and their implementation should be done in the user code if they are needed.
adustm 0:da04816fb411 367 Below some examples of callback implementations.
adustm 0:da04816fb411 368 -------------------------------------------------------------------------------------*/
adustm 0:da04816fb411 369 /**
adustm 0:da04816fb411 370 * @brief Manages the DMA Transfer complete interrupt.
adustm 0:da04816fb411 371 * @param None
adustm 0:da04816fb411 372 * @retval None
adustm 0:da04816fb411 373 */
adustm 0:da04816fb411 374 void BSP_AUDIO_IN_TransferComplete_CallBack(void)
adustm 0:da04816fb411 375 {
Jerome Coutant 5:66c230f74325 376 audio_rec_buffer_state = BUFFER_OFFSET_FULL;
adustm 0:da04816fb411 377 }
adustm 0:da04816fb411 378
adustm 0:da04816fb411 379 /**
adustm 0:da04816fb411 380 * @brief Manages the DMA Half Transfer complete interrupt.
adustm 0:da04816fb411 381 * @param None
adustm 0:da04816fb411 382 * @retval None
adustm 0:da04816fb411 383 */
adustm 0:da04816fb411 384 void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
adustm 0:da04816fb411 385 {
Jerome Coutant 5:66c230f74325 386 audio_rec_buffer_state = BUFFER_OFFSET_HALF;
adustm 0:da04816fb411 387 }
adustm 0:da04816fb411 388
Jerome Coutant 5:66c230f74325 389 /**
Jerome Coutant 5:66c230f74325 390 * @brief Audio IN Error callback function.
Jerome Coutant 5:66c230f74325 391 * @param None
Jerome Coutant 5:66c230f74325 392 * @retval None
Jerome Coutant 5:66c230f74325 393 */
Jerome Coutant 5:66c230f74325 394 void BSP_AUDIO_IN_Error_CallBack(void)
adustm 0:da04816fb411 395 {
Jerome Coutant 5:66c230f74325 396 printf("BSP_AUDIO_IN_Error_CallBack\n");
adustm 0:da04816fb411 397 }