lab 1 code
Dependencies: CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG
main.cpp
- Committer:
- bmazzeo
- Date:
- 2019-12-31
- Revision:
- 19:479f611941a8
- Parent:
- 18:255d15af49f2
- Child:
- 20:2ecdf687a2d1
File content as of revision 19:479f611941a8:
/**
******************************************************************************
* @file main.c
* @author Brian Mazzeo
* @brief This file provides a set of code for signal processing in 487.
* Parts are taken from example code from STMIcroelectronics
******************************************************************************
* @attention
* This code was specifically developed for BYU ECEn 487 course
* Introduction to Digital Signal Processing.
*
*
******************************************************************************
*/
#include "mbed.h"
#include "stm32746g_discovery_audio.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32746g_discovery_lcd.h"
typedef enum {
BUFFER_OFFSET_NONE = 0,
BUFFER_OFFSET_HALF = 1,
BUFFER_OFFSET_FULL = 2,
} BUFFER_StateTypeDef;
#define AUDIO_BLOCK_SAMPLES ((uint32_t)128) // Number of samples (L and R) in audio block (each samples is 16 bits)
#define AUDIO_BLOCK_SIZE ((uint32_t)512) // Number of bytes in audio block (4 * AUDIO_BLOCK_SAMPLES)
#define SDRAM_DEVICE_ADDR_AUDIO_MEM ((uint32_t)0xC0400000)
#define AUDIO_BUFFER_IN SDRAM_DEVICE_ADDR_AUDIO_MEM
#define AUDIO_BUFFER_OUT (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE * 2))
#define OSC_START_X_POS 20
#define OSC_LINE_SIZE 256
#define OSC_Y_POS 110
#define AUDIO_DRAW_LIMIT 30
Timer timer;
volatile uint32_t audio_rec_buffer_state = BUFFER_OFFSET_NONE;
static void Erase_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t Length);
static void Draw_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t* Mem_start, uint16_t Length);
static void Audio_to_Float(uint16_t* buffer_in, float* L_out, float* R_out, uint16_t Length);
static void Float_to_Audio(float* L_in, float* R_in, uint16_t* buffer_out, uint16_t Length);
/* To do converstion to float */
float L_channel_float[AUDIO_BLOCK_SAMPLES];
float R_channel_float[AUDIO_BLOCK_SAMPLES];
/* Back conversion to integer */
uint16_t Processed_audio[AUDIO_BLOCK_SAMPLES * 2];
/* Useful variables during looping */
uint32_t counter = 0;
char buf[40];
int first_half_time = 0;
int second_half_time = 0;
int total_time = 0;
int main()
{
/* Initialize the LCD Screen and display information */
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
BSP_LCD_SetTextColor(LCD_COLOR_ORANGE);
BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"487 Mic Audio Test Code", LEFT_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAt(0, OSC_Y_POS - 20, (uint8_t *)"L", LEFT_MODE);
BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
BSP_LCD_DisplayStringAt(0, OSC_Y_POS, (uint8_t *)"R", LEFT_MODE);
/* Initialize the Audio Interface */
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);
/* Initialize SDRAM buffers */
BSP_SDRAM_Init();
memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2);
memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2);
/* Start Recording */
if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) { printf("BSP_AUDIO_IN_Record error\n"); }
/* Start Playback */
BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) { printf("BSP_AUDIO_OUT_Play error\n"); }
timer.start();
while (1) {
/* First Half */
/* Wait end of half block recording before going on in the first half cycle*/
while (audio_rec_buffer_state != BUFFER_OFFSET_HALF) {}
/* This captures the time of an entire cycle */
total_time = timer.read_us();
/* Reset the timer counter to zero */
timer.reset();
/* Plot traces of first half block recording */
Erase_Trace(OSC_START_X_POS, OSC_Y_POS, AUDIO_BLOCK_SAMPLES);
Draw_Trace(OSC_START_X_POS, OSC_Y_POS, (uint16_t *) AUDIO_BUFFER_IN, AUDIO_BLOCK_SAMPLES);
/* Convert data to floating point representation for processing */
//Audio_to_Float((uint16_t *) AUDIO_BUFFER_IN, (float *) L_channel_float, (float *) R_channel_float, AUDIO_BLOCK_SAMPLES);
//Float_to_Audio((float *) L_channel_float, (float *) R_channel_float, (uint16_t *) Processed_audio, AUDIO_BLOCK_SAMPLES);
/* Copy recorded 1st half block into the audio buffer that goes out */
memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
/* Capture the timing of the first half processing */
first_half_time = timer.read_us();
/* End First Half */
/* Second Half */
/* Wait end of one block recording */
while (audio_rec_buffer_state != BUFFER_OFFSET_FULL) {}
/* Plot traces of second half block recording */
Erase_Trace(OSC_START_X_POS+AUDIO_BLOCK_SAMPLES, OSC_Y_POS, AUDIO_BLOCK_SAMPLES);
Draw_Trace( OSC_START_X_POS+AUDIO_BLOCK_SAMPLES, OSC_Y_POS, (uint16_t *) (AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SAMPLES);
/* Compute important cycle information and display it*/
counter++;
sprintf(buf, "Cycles: %9d", counter);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(0, 46, (uint8_t *) buf, LEFT_MODE);
sprintf(buf, "1:%6d 2:%6d T:%6d", first_half_time, second_half_time, total_time);
BSP_LCD_DisplayStringAt(0, 20, (uint8_t *) buf, LEFT_MODE);
/* Copy recorded 2nd half block into audio output buffer */
memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
/* Change the recording buffer state to reflect the status of the buffer */
audio_rec_buffer_state = BUFFER_OFFSET_NONE;
/* Measures the amount of time to process the second half */
second_half_time = timer.read_us();
/* End Second Half */
}
}
/**
* @brief Draws a trace of the data line.
* @param Xpos: X position
* @param L_Ypos: Left channel Y position
* @param R_Ypos: Right channel Y position
* @param Mem_start: Start of memory location
* @param Length: length of trace
* @retval None
*/
void Erase_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t Length)
{
BSP_LCD_SetTextColor(LCD_COLOR_BROWN);
BSP_LCD_FillRect(Xpos, Ypos - AUDIO_DRAW_LIMIT, Length, AUDIO_DRAW_LIMIT);
BSP_LCD_FillRect(Xpos, Ypos+1, Length, AUDIO_DRAW_LIMIT);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_DrawHLine(Xpos, Ypos, Length);
}
/**
* @brief Draws a trace of the data line.
* @param Xpos: X position
* @param L_Ypos: Left channel Y position
* @param R_Ypos: Right channel Y position
* @param Mem_start: Start of memory location
* @param Length: length of trace
* @retval None
*/
void Draw_Trace(uint16_t Xpos, uint16_t Ypos, uint16_t* Mem_start, uint16_t Length)
{
uint16_t i;
uint32_t data_value;
uint16_t* mem_address;
char buf[10];
int16_t L_audio_value;
int16_t R_audio_value;
//data_value = *((uint32_t*) Mem_start);
//L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
//R_audio_value = (int16_t) (data_value & 0xFFFF);
mem_address = Mem_start;
for (i=0; i<Length; i++)
{
//mem_address = (uint32_t*) Mem_start + i;
//data_value = *((uint16_t*) mem_address);
//L_audio_value = (int16_t) ((data_value >> 16) & 0xFFFF);
//R_audio_value = (int16_t) (data_value & 0xFFFF);
L_audio_value = (int16_t) *mem_address;
mem_address++;
R_audio_value = (int16_t) *mem_address;
mem_address++;
L_audio_value = L_audio_value / 100;
R_audio_value = R_audio_value / 100;
if (L_audio_value > AUDIO_DRAW_LIMIT) {L_audio_value = AUDIO_DRAW_LIMIT;}
else if (L_audio_value < -AUDIO_DRAW_LIMIT) {L_audio_value = -AUDIO_DRAW_LIMIT;}
if (R_audio_value > AUDIO_DRAW_LIMIT) {R_audio_value = AUDIO_DRAW_LIMIT;}
else if (R_audio_value < -AUDIO_DRAW_LIMIT) {R_audio_value = -AUDIO_DRAW_LIMIT;}
BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) Ypos + L_audio_value), LCD_COLOR_BLUE);
BSP_LCD_DrawPixel(Xpos + i, (uint16_t) ((int16_t) Ypos + R_audio_value), LCD_COLOR_GREEN);
}
}
/**
* @brief Converts audio data in buffer to floating point representation.
* @param buffer_in: Pointer to Audio buffer start location
* @param L_out: Pointer to Left channel out data (float)
* @param R_out: Pointer to Right channel out data (float)
* @param Length: length of data to convert
* @retval None
*/
void Audio_to_Float(uint16_t* buffer_in, float* L_out, float* R_out, uint16_t Length)
{
uint16_t i;
uint32_t data_value;
uint16_t* audio_mem_address;
uint16_t* L_chan_mem_address;
uint16_t* R_chan_mem_address;
float L_audio_value;
float R_audio_value;
for (i=0; i<Length; i++)
{
audio_mem_address = (uint16_t*) buffer_in + i;
L_chan_mem_address = (uint16_t*) L_out + i;
R_chan_mem_address = (uint16_t*) R_out + i;
data_value = *((uint16_t*) audio_mem_address);
L_audio_value = (float) ((int16_t) ((data_value >> 16) & 0xFFFF));
R_audio_value = (float) ((int16_t) (data_value & 0xFFFF));
*L_chan_mem_address = L_audio_value;
*R_chan_mem_address = R_audio_value;
}
}
/**
* @brief Converts audio data in buffer to floating point representation.
* @param L_out: Pointer to Left channel in data (float)
* @param R_out: Pointer to Right channel in data (float)
* @param buffer_out: Pointer to combined 32 bit (two 16-bit int samples)
* @param Length: length of data to convert
* @retval None
*/
void Float_to_Audio(float* L_in, float* R_in, uint16_t* buffer_out, uint16_t Length)
{
uint16_t i;
uint32_t data_value;
uint16_t* audio_mem_address;
uint16_t* L_chan_mem_address;
uint16_t* R_chan_mem_address;
float L_audio_value;
float R_audio_value;
for (i=0; i<Length; i++)
{
L_chan_mem_address = (uint16_t*) L_in + i;
R_chan_mem_address = (uint16_t*) R_in + i;
audio_mem_address = (uint16_t*) buffer_out + i;
L_audio_value = *((uint16_t*) L_chan_mem_address);
R_audio_value = *((uint16_t*) R_chan_mem_address);
data_value = (((uint32_t) ((int16_t) L_audio_value)) << 16) | ((uint32_t) ((int16_t) R_audio_value));
*audio_mem_address = data_value;
}
}
/*-------------------------------------------------------------------------------------
Callbacks implementation:
the callbacks API are defined __weak in the stm32746g_discovery_audio.c file
and their implementation should be done in the user code if they are needed.
Below some examples of callback implementations.
-------------------------------------------------------------------------------------*/
/**
* @brief Manages the DMA Transfer complete interrupt.
* @param None
* @retval None
*/
void BSP_AUDIO_IN_TransferComplete_CallBack(void)
{
audio_rec_buffer_state = BUFFER_OFFSET_FULL;
}
/**
* @brief Manages the DMA Half Transfer complete interrupt.
* @param None
* @retval None
*/
void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
{
audio_rec_buffer_state = BUFFER_OFFSET_HALF;
}
/**
* @brief Audio IN Error callback function.
* @param None
* @retval None
*/
void BSP_AUDIO_IN_Error_CallBack(void)
{
printf("BSP_AUDIO_IN_Error_CallBack\n");
}