initial

Dependencies:   mbed BSP_DISCO_F746NG mbed-dsp

Committer:
justenmg
Date:
Tue Mar 10 20:59:20 2020 +0000
Revision:
9:fb0eb0b2796c
Parent:
3:51e15bd15778
finished

Who changed what in which revision?

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