Respeaker Test Code

Dependencies:   BufferedSerial mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 ////////////////////////////////////////
00002 //      Tau_ReSpeaker_DSP_Test        //
00003 //  Arkadiraf@gmail.com - 29/05/2017  //
00004 ////////////////////////////////////////
00005 /*
00006  If button pressed --> generate sine wave else analog output = analog input
00007 */
00008 
00009 /*
00010    Board : Nucleo STM32F446RE
00011    Power Source : USB
00012 */
00013 
00014 /*
00015     Pinout:
00016     PC - Serial 2
00017     PA_2 (Tx) --> STLINK
00018     PA_3 (Rx) --> STLINK
00019 
00020     Switch - Serial 3
00021     PC_10 (Tx) --> SW_Rx
00022     PC_11 (Rx) --> SW_Tx
00023 
00024     I2C_Bus
00025     PB_8 --> SCL
00026     PB_9 --> SDA
00027 
00028     Digital output
00029     PA_5 --> led (DigitalOut)
00030 
00031     Digital Input
00032     PA_10 --> SW_Trigger
00033     PC_13 --> BTN (Blue)
00034 
00035     Analog Input
00036     PA_0 --> SIG_IN_DSP
00037 
00038     Analog Output
00039     PA_4 --> SIG_OUT_DSP
00040 
00041 */
00042 
00043 ///////////////
00044 // Libraries //
00045 ///////////////
00046 #include "mbed.h"
00047 #include "BufferedSerial.h"  // solves issues of loosing data. alternative doing it yourself
00048 
00049 ///////////////
00050 // #defines  //
00051 ///////////////
00052 
00053 #define DEBUG_MOD1
00054 
00055 // Sine generator
00056 #define PI        (3.141592653589793238462)
00057 #define AMPLITUDE (1.0)    // x * 3.3V
00058 #define PHASE     (PI * 1) // 2*pi is one period
00059 #define RANGE     (0x7FFF)
00060 #define OFFSET    (0x7FFF)
00061 #define BUFFER_SIZE (360)
00062 
00063 /////////////
00064 // Objects //
00065 /////////////
00066 
00067 // uart
00068 BufferedSerial pc(USBTX, USBRX);
00069 
00070 // digital
00071 DigitalIn user_button(PC_13);
00072 DigitalIn sw_trigger(PA_10);
00073 DigitalOut led(PA_5);
00074 
00075 // analog
00076 AnalogOut dsp_output(PA_4);
00077 AnalogIn dsp_input(PA_0);
00078 
00079 ///////////////
00080 // variables //
00081 ///////////////
00082 
00083 // analog input
00084 uint16_t adc_in=0;
00085 
00086 // sin wave buffer
00087 uint16_t buffer[BUFFER_SIZE];
00088 uint16_t buffer_index=0;
00089 ///////////////
00090 // Functions //
00091 ///////////////
00092 
00093 // Create the sinewave buffer
00094 void calculate_sinewave(void);
00095 
00096 ////////////////////////
00097 //  Main Code Setup : //
00098 ////////////////////////
00099 int main()
00100 {
00101     pc.baud(57600);
00102 #ifdef DEBUG_MOD1
00103     pc.printf("ReSpeaker Test \r\n");
00104 #endif
00105     // calculate sine wave buffer
00106     calculate_sinewave();
00107 
00108     ///////////////////////
00109     //  Main Code Loop : //
00110     ///////////////////////
00111     while(1) {
00112         // check button state
00113         if (user_button) {
00114             //adc_in=dsp_input.read_u16();
00115             dsp_output.write_u16(dsp_input.read_u16()); //adc_in
00116         } else {
00117             // sinewave output
00118             buffer_index++;
00119             if (buffer_index>=BUFFER_SIZE) buffer_index=0;
00120             dsp_output.write_u16(buffer[buffer_index]);
00121             wait_us(1);
00122         }
00123     }
00124 }
00125 
00126 ///////////////
00127 // Functions //
00128 ///////////////
00129 
00130 // Create the sinewave buffer
00131 void calculate_sinewave(void)
00132 {
00133     for (int i = 0; i < BUFFER_SIZE; i++) {
00134         double rads = (PI * i)/180.0; // Convert degree in radian
00135         buffer[i] = (uint16_t)(AMPLITUDE * (RANGE * (cos(rads + PHASE))) + OFFSET);
00136     }
00137 }