lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Committer:
bmazzeo
Date:
Mon Dec 30 20:18:17 2019 +0000
Revision:
15:83e40ccc1b1f
Parent:
14:18f159d48340
Child:
16:b7dca59ab076
Some things working with floats and such - but also many things broke - so this is committed before working with previous revision.

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 15:83e40ccc1b1f 47 static void Audio_to_Float(uint32_t* buffer_in, float* L_out, float* 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 15:83e40ccc1b1f 115 Audio_to_Float((uint32_t *) AUDIO_BUFFER_IN, (float*) &L_channel_float, (float*) &R_channel_float, HALF_AUDIO_BLOCK_SIZE);
bmazzeo 14:18f159d48340 116
bmazzeo 15:83e40ccc1b1f 117 Float_to_Audio((uint16_t *) &L_channel_float, (uint16_t *) &R_channel_float, (uint16_t *) Processed_audio, HALF_AUDIO_BLOCK_SIZE);
bmazzeo 15:83e40ccc1b1f 118
bmazzeo 15:83e40ccc1b1f 119 uint32_t data_value = *((uint32_t*) AUDIO_BUFFER_IN);
bmazzeo 15:83e40ccc1b1f 120 int16_t L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 15:83e40ccc1b1f 121 //L_channel_float[1] = 10;
bmazzeo 15:83e40ccc1b1f 122 sprintf(buf, "Lf:%10.2f Li:%6d", L_channel_float[0], L_audio_value);
bmazzeo 15:83e40ccc1b1f 123 BSP_LCD_DisplayStringAt(0, 155, (uint8_t *) buf, LEFT_MODE);
bmazzeo 15:83e40ccc1b1f 124
bmazzeo 14:18f159d48340 125
bmazzeo 13:61131aac4031 126 /* Copy recorded 1st half block into the audio buffer that goes out */
bmazzeo 15:83e40ccc1b1f 127 //memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
bmazzeo 15:83e40ccc1b1f 128 memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(Processed_audio), AUDIO_BLOCK_SIZE);
bmazzeo 13:61131aac4031 129
bmazzeo 13:61131aac4031 130 /* Capture the timing of the first half processing */
bmazzeo 9:f5b37c71856d 131 first_half_time = timer.read_us();
bmazzeo 13:61131aac4031 132 /* End First Half */
bmazzeo 8:d1c41eca57f0 133
bmazzeo 6:e689075b04ed 134 /* Second Half */
adustm 0:da04816fb411 135 /* Wait end of one block recording */
bmazzeo 11:4256dbbb0c89 136 while (audio_rec_buffer_state != BUFFER_OFFSET_FULL) {}
bmazzeo 9:f5b37c71856d 137
bmazzeo 13:61131aac4031 138 /* Plot traces of second half block recording */
bmazzeo 12:e44766b61346 139 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 140 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 141
bmazzeo 13:61131aac4031 142 /* Compute important cycle information and display it*/
bmazzeo 6:e689075b04ed 143 counter++;
bmazzeo 10:a82b64ea1d11 144 sprintf(buf, "Cycles: %9d", counter);
bmazzeo 6:e689075b04ed 145 BSP_LCD_SetTextColor(LCD_COLOR_RED);
bmazzeo 10:a82b64ea1d11 146 BSP_LCD_DisplayStringAt(0, 46, (uint8_t *) buf, LEFT_MODE);
bmazzeo 10:a82b64ea1d11 147 sprintf(buf, "1:%6d 2:%6d T:%6d", first_half_time, second_half_time, total_time);
bmazzeo 10:a82b64ea1d11 148 BSP_LCD_DisplayStringAt(0, 20, (uint8_t *) buf, LEFT_MODE);
bmazzeo 13:61131aac4031 149
bmazzeo 13:61131aac4031 150 /* Copy recorded 2nd half block into audio output buffer */
bmazzeo 13:61131aac4031 151 memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
bmazzeo 13:61131aac4031 152
bmazzeo 13:61131aac4031 153 /* Change the recording buffer state to reflect the status of the buffer */
bmazzeo 13:61131aac4031 154 audio_rec_buffer_state = BUFFER_OFFSET_NONE;
bmazzeo 9:f5b37c71856d 155
bmazzeo 13:61131aac4031 156 /* Measures the amount of time to process the second half */
bmazzeo 9:f5b37c71856d 157 second_half_time = timer.read_us();
bmazzeo 9:f5b37c71856d 158
bmazzeo 13:61131aac4031 159 /* End Second Half */
adustm 0:da04816fb411 160 }
adustm 0:da04816fb411 161 }
Jerome Coutant 5:66c230f74325 162
bmazzeo 7:e1dfd64eba81 163 /**
bmazzeo 7:e1dfd64eba81 164 * @brief Draws a trace of the data line.
bmazzeo 7:e1dfd64eba81 165 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 166 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 167 * @param R_Ypos: Right channel Y position
bmazzeo 7:e1dfd64eba81 168 * @param Mem_start: Start of memory location
bmazzeo 7:e1dfd64eba81 169 * @param Length: length of trace
bmazzeo 7:e1dfd64eba81 170 * @retval None
bmazzeo 7:e1dfd64eba81 171 */
bmazzeo 7:e1dfd64eba81 172 void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length)
bmazzeo 7:e1dfd64eba81 173 {
bmazzeo 7:e1dfd64eba81 174 BSP_LCD_SetTextColor(LCD_COLOR_BROWN);
bmazzeo 7:e1dfd64eba81 175 BSP_LCD_FillRect(Xpos, L_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 176 BSP_LCD_FillRect(Xpos, L_Ypos+1, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 177 BSP_LCD_FillRect(Xpos, R_Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 178 BSP_LCD_FillRect(Xpos, R_Ypos+1, Length, AUDIO_DRAW_LIMIT);
bmazzeo 7:e1dfd64eba81 179
bmazzeo 7:e1dfd64eba81 180 BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
bmazzeo 8:d1c41eca57f0 181 BSP_LCD_DrawHLine(Xpos, L_CHANNEL_Y_POS, Length);
bmazzeo 8:d1c41eca57f0 182 BSP_LCD_DrawHLine(Xpos, R_CHANNEL_Y_POS, Length);
bmazzeo 7:e1dfd64eba81 183
bmazzeo 7:e1dfd64eba81 184 }
bmazzeo 7:e1dfd64eba81 185
Jerome Coutant 5:66c230f74325 186
bmazzeo 6:e689075b04ed 187 /**
bmazzeo 6:e689075b04ed 188 * @brief Draws a trace of the data line.
bmazzeo 6:e689075b04ed 189 * @param Xpos: X position
bmazzeo 7:e1dfd64eba81 190 * @param L_Ypos: Left channel Y position
bmazzeo 7:e1dfd64eba81 191 * @param R_Ypos: Right channel Y position
bmazzeo 6:e689075b04ed 192 * @param Mem_start: Start of memory location
bmazzeo 6:e689075b04ed 193 * @param Length: length of trace
bmazzeo 6:e689075b04ed 194 * @retval None
bmazzeo 6:e689075b04ed 195 */
bmazzeo 8:d1c41eca57f0 196 void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length)
bmazzeo 6:e689075b04ed 197 {
bmazzeo 7:e1dfd64eba81 198 uint16_t i;
bmazzeo 7:e1dfd64eba81 199 uint32_t data_value;
bmazzeo 7:e1dfd64eba81 200 uint32_t* mem_address;
bmazzeo 7:e1dfd64eba81 201 char buf[10];
bmazzeo 7:e1dfd64eba81 202 int16_t L_audio_value;
bmazzeo 7:e1dfd64eba81 203 int16_t R_audio_value;
bmazzeo 6:e689075b04ed 204
bmazzeo 7:e1dfd64eba81 205 data_value = *((uint32_t*) Mem_start);
bmazzeo 7:e1dfd64eba81 206 L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 7:e1dfd64eba81 207 R_audio_value = (int16_t) (data_value & 0xFFFF);
bmazzeo 7:e1dfd64eba81 208
bmazzeo 8:d1c41eca57f0 209 //sprintf(buf, "%12d", data_value);
bmazzeo 8:d1c41eca57f0 210 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 8:d1c41eca57f0 211
bmazzeo 7:e1dfd64eba81 212 //sprintf(buf, "%12d", L_audio_value);
bmazzeo 7:e1dfd64eba81 213 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 6:e689075b04ed 214
bmazzeo 7:e1dfd64eba81 215 //sprintf(buf, "%12d", R_audio_value);
bmazzeo 7:e1dfd64eba81 216 //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 217
bmazzeo 8:d1c41eca57f0 218 for (i=0; i<Length; i++)
bmazzeo 6:e689075b04ed 219 {
bmazzeo 7:e1dfd64eba81 220 mem_address = (uint32_t*) Mem_start + i;
bmazzeo 7:e1dfd64eba81 221 data_value = *((uint32_t*) mem_address);
bmazzeo 7:e1dfd64eba81 222 L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 7:e1dfd64eba81 223 R_audio_value = (int16_t) (data_value & 0xFFFF);
bmazzeo 7:e1dfd64eba81 224
bmazzeo 7:e1dfd64eba81 225 L_audio_value = L_audio_value / 50;
bmazzeo 7:e1dfd64eba81 226 R_audio_value = R_audio_value / 50;
bmazzeo 7:e1dfd64eba81 227
bmazzeo 7:e1dfd64eba81 228 //sprintf(buf, "%12d", L_audio_value);
bmazzeo 7:e1dfd64eba81 229 //BSP_LCD_DisplayStringAt(0, 60, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 230
bmazzeo 7:e1dfd64eba81 231 //sprintf(buf, "%12d", R_audio_value);
bmazzeo 7:e1dfd64eba81 232 //BSP_LCD_DisplayStringAt(0, 80, (uint8_t *) buf, LEFT_MODE);
bmazzeo 7:e1dfd64eba81 233
bmazzeo 7:e1dfd64eba81 234
bmazzeo 7:e1dfd64eba81 235 //L_audio_value = -i;
bmazzeo 7:e1dfd64eba81 236 if (L_audio_value > AUDIO_DRAW_LIMIT) {L_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 237 else if (L_audio_value < -AUDIO_DRAW_LIMIT) {L_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 238
bmazzeo 7:e1dfd64eba81 239 if (R_audio_value > AUDIO_DRAW_LIMIT) {R_audio_value = AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 240 else if (R_audio_value < -AUDIO_DRAW_LIMIT) {R_audio_value = -AUDIO_DRAW_LIMIT;}
bmazzeo 7:e1dfd64eba81 241
bmazzeo 7:e1dfd64eba81 242 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) L_Ypos + L_audio_value), LCD_COLOR_WHITE);
bmazzeo 7:e1dfd64eba81 243 BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) R_Ypos + R_audio_value), LCD_COLOR_WHITE);
bmazzeo 6:e689075b04ed 244 }
bmazzeo 6:e689075b04ed 245
bmazzeo 6:e689075b04ed 246 }
bmazzeo 14:18f159d48340 247
bmazzeo 14:18f159d48340 248 /**
bmazzeo 14:18f159d48340 249 * @brief Converts audio data in buffer to floating point representation.
bmazzeo 14:18f159d48340 250 * @param buffer_in: Pointer to Audio buffer start location
bmazzeo 14:18f159d48340 251 * @param L_out: Pointer to Left channel out data (float)
bmazzeo 14:18f159d48340 252 * @param R_out: Pointer to Right channel out data (float)
bmazzeo 14:18f159d48340 253 * @param Length: length of data to convert
bmazzeo 14:18f159d48340 254 * @retval None
bmazzeo 14:18f159d48340 255 */
bmazzeo 15:83e40ccc1b1f 256 void Audio_to_Float(uint32_t* buffer_in, float* L_out, float* R_out, uint16_t Length)
bmazzeo 14:18f159d48340 257 {
bmazzeo 14:18f159d48340 258 uint16_t i;
bmazzeo 14:18f159d48340 259 uint32_t data_value;
bmazzeo 15:83e40ccc1b1f 260 uint32_t* audio_mem_address;
bmazzeo 15:83e40ccc1b1f 261 float* L_chan_mem_address;
bmazzeo 15:83e40ccc1b1f 262 float* R_chan_mem_address;
bmazzeo 14:18f159d48340 263 float L_audio_value;
bmazzeo 14:18f159d48340 264 float R_audio_value;
bmazzeo 6:e689075b04ed 265
bmazzeo 15:83e40ccc1b1f 266 int16_t L_audio_value_int;
bmazzeo 15:83e40ccc1b1f 267
bmazzeo 14:18f159d48340 268 for (i=0; i<Length; i++)
bmazzeo 14:18f159d48340 269 {
bmazzeo 15:83e40ccc1b1f 270 audio_mem_address = (uint32_t*) buffer_in + i;
bmazzeo 15:83e40ccc1b1f 271 L_chan_mem_address = (float*) L_out + i;
bmazzeo 15:83e40ccc1b1f 272 R_chan_mem_address = (float*) R_out + i;
bmazzeo 15:83e40ccc1b1f 273 data_value = *((uint32_t*) audio_mem_address);
bmazzeo 14:18f159d48340 274 L_audio_value = (float) ((int16_t) ((data_value >> 16) & 0xFFFF));
bmazzeo 15:83e40ccc1b1f 275 L_audio_value_int = ((int16_t) ((data_value >> 16) & 0xFFFF));
bmazzeo 14:18f159d48340 276 R_audio_value = (float) ((int16_t) (data_value & 0xFFFF));
bmazzeo 14:18f159d48340 277
bmazzeo 15:83e40ccc1b1f 278 *L_chan_mem_address = 2 * L_audio_value;
bmazzeo 15:83e40ccc1b1f 279 //*L_chan_mem_address = 20;
bmazzeo 14:18f159d48340 280 *R_chan_mem_address = R_audio_value;
bmazzeo 14:18f159d48340 281 }
bmazzeo 15:83e40ccc1b1f 282
bmazzeo 15:83e40ccc1b1f 283 //sprintf(buf, "Lf:%10.2f Li:%6d", L_audio_value, data_value);
bmazzeo 15:83e40ccc1b1f 284 //L_audio_value_int = ((int16_t) (data_value & 0xFFFF));
bmazzeo 15:83e40ccc1b1f 285 //L_audio_value_int = (int16_t) ((data_value >> 16) & 0xFFFF);
bmazzeo 15:83e40ccc1b1f 286 //sprintf(buf, "Lavi:%6d Li:%6d", L_audio_value_int, data_value);
bmazzeo 15:83e40ccc1b1f 287 //BSP_LCD_DisplayStringAt(0, 155, (uint8_t *) buf, LEFT_MODE);
bmazzeo 15:83e40ccc1b1f 288
bmazzeo 14:18f159d48340 289 }
bmazzeo 14:18f159d48340 290
bmazzeo 14:18f159d48340 291 /**
bmazzeo 14:18f159d48340 292 * @brief Converts audio data in buffer to floating point representation.
bmazzeo 14:18f159d48340 293 * @param L_out: Pointer to Left channel in data (float)
bmazzeo 14:18f159d48340 294 * @param R_out: Pointer to Right channel in data (float)
bmazzeo 14:18f159d48340 295 * @param buffer_out: Pointer to combined 32 bit (two 16-bit int samples)
bmazzeo 14:18f159d48340 296 * @param Length: length of data to convert
bmazzeo 14:18f159d48340 297 * @retval None
bmazzeo 14:18f159d48340 298 */
bmazzeo 14:18f159d48340 299 void Float_to_Audio(uint16_t* L_in, uint16_t * R_in, uint16_t* buffer_out, uint16_t Length)
bmazzeo 14:18f159d48340 300 {
bmazzeo 14:18f159d48340 301 uint16_t i;
bmazzeo 14:18f159d48340 302 uint32_t data_value;
bmazzeo 14:18f159d48340 303 uint16_t* audio_mem_address;
bmazzeo 14:18f159d48340 304 uint16_t* L_chan_mem_address;
bmazzeo 14:18f159d48340 305 uint16_t* R_chan_mem_address;
bmazzeo 14:18f159d48340 306 float L_audio_value;
bmazzeo 14:18f159d48340 307 float R_audio_value;
bmazzeo 14:18f159d48340 308
bmazzeo 14:18f159d48340 309 for (i=0; i<Length; i++)
bmazzeo 14:18f159d48340 310 {
bmazzeo 14:18f159d48340 311 L_chan_mem_address = (uint16_t*) L_in + i;
bmazzeo 14:18f159d48340 312 R_chan_mem_address = (uint16_t*) R_in + i;
bmazzeo 14:18f159d48340 313 audio_mem_address = (uint16_t*) buffer_out + i;
bmazzeo 14:18f159d48340 314
bmazzeo 14:18f159d48340 315 L_audio_value = *((uint16_t*) L_chan_mem_address);
bmazzeo 14:18f159d48340 316 R_audio_value = *((uint16_t*) R_chan_mem_address);
bmazzeo 14:18f159d48340 317
bmazzeo 14:18f159d48340 318 data_value = (((uint32_t) ((int16_t) L_audio_value)) << 16) | ((uint32_t) ((int16_t) R_audio_value));
bmazzeo 14:18f159d48340 319 *audio_mem_address = data_value;
bmazzeo 14:18f159d48340 320 }
bmazzeo 14:18f159d48340 321 }
bmazzeo 14:18f159d48340 322
bmazzeo 14:18f159d48340 323
bmazzeo 14:18f159d48340 324
bmazzeo 6:e689075b04ed 325
bmazzeo 6:e689075b04ed 326
adustm 0:da04816fb411 327 /*-------------------------------------------------------------------------------------
adustm 0:da04816fb411 328 Callbacks implementation:
adustm 0:da04816fb411 329 the callbacks API are defined __weak in the stm32746g_discovery_audio.c file
adustm 0:da04816fb411 330 and their implementation should be done in the user code if they are needed.
adustm 0:da04816fb411 331 Below some examples of callback implementations.
adustm 0:da04816fb411 332 -------------------------------------------------------------------------------------*/
adustm 0:da04816fb411 333 /**
adustm 0:da04816fb411 334 * @brief Manages the DMA Transfer complete interrupt.
adustm 0:da04816fb411 335 * @param None
adustm 0:da04816fb411 336 * @retval None
adustm 0:da04816fb411 337 */
adustm 0:da04816fb411 338 void BSP_AUDIO_IN_TransferComplete_CallBack(void)
adustm 0:da04816fb411 339 {
Jerome Coutant 5:66c230f74325 340 audio_rec_buffer_state = BUFFER_OFFSET_FULL;
adustm 0:da04816fb411 341 }
adustm 0:da04816fb411 342
adustm 0:da04816fb411 343 /**
adustm 0:da04816fb411 344 * @brief Manages the DMA Half Transfer complete interrupt.
adustm 0:da04816fb411 345 * @param None
adustm 0:da04816fb411 346 * @retval None
adustm 0:da04816fb411 347 */
adustm 0:da04816fb411 348 void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
adustm 0:da04816fb411 349 {
Jerome Coutant 5:66c230f74325 350 audio_rec_buffer_state = BUFFER_OFFSET_HALF;
adustm 0:da04816fb411 351 }
adustm 0:da04816fb411 352
Jerome Coutant 5:66c230f74325 353 /**
Jerome Coutant 5:66c230f74325 354 * @brief Audio IN Error callback function.
Jerome Coutant 5:66c230f74325 355 * @param None
Jerome Coutant 5:66c230f74325 356 * @retval None
Jerome Coutant 5:66c230f74325 357 */
Jerome Coutant 5:66c230f74325 358 void BSP_AUDIO_IN_Error_CallBack(void)
adustm 0:da04816fb411 359 {
Jerome Coutant 5:66c230f74325 360 printf("BSP_AUDIO_IN_Error_CallBack\n");
adustm 0:da04816fb411 361 }