ReSpeaker DSP V02
Dependencies: mbed MbedJSONValue
main.cpp@13:c0a13a99905e, 2019-06-08 (annotated)
- Committer:
- Arkadi
- Date:
- Sat Jun 08 11:02:34 2019 +0000
- Revision:
- 13:c0a13a99905e
- Parent:
- 12:9d30df1529be
added play file option
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Arkadi | 0:bfac05b2d50f | 1 | //////////////////////////////////////// |
Arkadi | 13:c0a13a99905e | 2 | // Tau_ReSpeaker_DSP V02 // |
Arkadi | 13:c0a13a99905e | 3 | // Arkadiraf@gmail.com - 08/06/2019 // |
Arkadi | 0:bfac05b2d50f | 4 | //////////////////////////////////////// |
Arkadi | 0:bfac05b2d50f | 5 | /* |
Arkadi | 5:ec6f2323a263 | 6 | Json Format: json:{"name":"dsp","mode":"off"}\r\n |
Arkadi | 2:59c1a24aed7c | 7 | */ |
Arkadi | 1:574b54755983 | 8 | |
Arkadi | 3:48258b86e182 | 9 | |
Arkadi | 0:bfac05b2d50f | 10 | |
Arkadi | 0:bfac05b2d50f | 11 | /* |
Arkadi | 0:bfac05b2d50f | 12 | Board : Nucleo STM32F446RE |
Arkadi | 13:c0a13a99905e | 13 | Power Source : DC-DC 5V Regulator E5V Jumper |
Arkadi | 0:bfac05b2d50f | 14 | */ |
Arkadi | 0:bfac05b2d50f | 15 | |
Arkadi | 0:bfac05b2d50f | 16 | /* |
Arkadi | 0:bfac05b2d50f | 17 | Pinout: |
Arkadi | 0:bfac05b2d50f | 18 | PC - Serial 2 |
Arkadi | 0:bfac05b2d50f | 19 | PA_2 (Tx) --> STLINK |
Arkadi | 0:bfac05b2d50f | 20 | PA_3 (Rx) --> STLINK |
Arkadi | 0:bfac05b2d50f | 21 | |
Arkadi | 0:bfac05b2d50f | 22 | Switch - Serial 3 |
Arkadi | 0:bfac05b2d50f | 23 | PC_10 (Tx) --> SW_Rx |
Arkadi | 0:bfac05b2d50f | 24 | PC_11 (Rx) --> SW_Tx |
Arkadi | 0:bfac05b2d50f | 25 | |
Arkadi | 0:bfac05b2d50f | 26 | Digital output |
Arkadi | 0:bfac05b2d50f | 27 | PA_5 --> led (DigitalOut) |
Arkadi | 9:7e20c1e5a354 | 28 | PA_10 --> DSP_Trigger |
Arkadi | 11:07981b0d28d3 | 29 | |
Arkadi | 0:bfac05b2d50f | 30 | Digital Input |
Arkadi | 0:bfac05b2d50f | 31 | PC_13 --> BTN (Blue) |
Arkadi | 0:bfac05b2d50f | 32 | |
Arkadi | 1:574b54755983 | 33 | PA_6 --> Toggle Pin (Loop Freq D12) |
Arkadi | 1:574b54755983 | 34 | |
Arkadi | 0:bfac05b2d50f | 35 | Analog Input |
Arkadi | 1:574b54755983 | 36 | PA_0 --> SIG_IN_DSP (A0) |
Arkadi | 0:bfac05b2d50f | 37 | |
Arkadi | 0:bfac05b2d50f | 38 | Analog Output |
Arkadi | 1:574b54755983 | 39 | PA_4 --> SIG_OUT_DSP (A2) |
Arkadi | 0:bfac05b2d50f | 40 | |
Arkadi | 0:bfac05b2d50f | 41 | */ |
Arkadi | 0:bfac05b2d50f | 42 | |
Arkadi | 0:bfac05b2d50f | 43 | /////////////// |
Arkadi | 0:bfac05b2d50f | 44 | // Libraries // |
Arkadi | 0:bfac05b2d50f | 45 | /////////////// |
Arkadi | 0:bfac05b2d50f | 46 | #include "mbed.h" |
Arkadi | 1:574b54755983 | 47 | #include "MbedJSONValue.h" |
Arkadi | 13:c0a13a99905e | 48 | #include "chirp.h" |
Arkadi | 0:bfac05b2d50f | 49 | /////////////// |
Arkadi | 0:bfac05b2d50f | 50 | // #defines // |
Arkadi | 0:bfac05b2d50f | 51 | /////////////// |
Arkadi | 12:9d30df1529be | 52 | |
Arkadi | 12:9d30df1529be | 53 | //#define TOGGLE_IO_MODE // loop frequency indicator |
Arkadi | 12:9d30df1529be | 54 | |
Arkadi | 1:574b54755983 | 55 | // UART debug modes: |
Arkadi | 1:574b54755983 | 56 | //#define DEBUG_MOD1 // json packet recognise |
Arkadi | 12:9d30df1529be | 57 | //#define DEBUG_MOD2 // json parse |
Arkadi | 12:9d30df1529be | 58 | //#define DEBUG_MOD3 // dsp handler |
Arkadi | 12:9d30df1529be | 59 | //#define DEBUG_MOD10 // responsivity msges to gui |
Arkadi | 12:9d30df1529be | 60 | |
Arkadi | 12:9d30df1529be | 61 | // PI variable |
Arkadi | 1:574b54755983 | 62 | #define PI_DOUBLE (3.141592653589793238462) |
Arkadi | 1:574b54755983 | 63 | #define PI_FLOAT (3.14159f) |
Arkadi | 1:574b54755983 | 64 | |
Arkadi | 1:574b54755983 | 65 | // json commad |
Arkadi | 10:273127efdc6e | 66 | #define MSG_BUFFER_SIZE 1024 |
Arkadi | 1:574b54755983 | 67 | #define HEADER_SIZE 5 |
Arkadi | 1:574b54755983 | 68 | #define FOOTER_SIZE 2 |
Arkadi | 0:bfac05b2d50f | 69 | |
Arkadi | 9:7e20c1e5a354 | 70 | // Frequency counter |
Arkadi | 9:7e20c1e5a354 | 71 | #define TICKS2TOGGLE 1000000 |
Arkadi | 12:9d30df1529be | 72 | |
Arkadi | 0:bfac05b2d50f | 73 | ///////////// |
Arkadi | 0:bfac05b2d50f | 74 | // Objects // |
Arkadi | 0:bfac05b2d50f | 75 | ///////////// |
Arkadi | 1:574b54755983 | 76 | // json |
Arkadi | 1:574b54755983 | 77 | MbedJSONValue guiCmd; |
Arkadi | 0:bfac05b2d50f | 78 | |
Arkadi | 0:bfac05b2d50f | 79 | // uart |
Arkadi | 1:574b54755983 | 80 | Serial pc(USBTX, USBRX); |
Arkadi | 0:bfac05b2d50f | 81 | |
Arkadi | 2:59c1a24aed7c | 82 | // uart switch_dsp |
Arkadi | 2:59c1a24aed7c | 83 | Serial switch_dsp(PC_10, PC_11); |
Arkadi | 2:59c1a24aed7c | 84 | |
Arkadi | 0:bfac05b2d50f | 85 | // digital |
Arkadi | 0:bfac05b2d50f | 86 | DigitalIn user_button(PC_13); |
Arkadi | 9:7e20c1e5a354 | 87 | DigitalOut dsp_trigger(PA_10);// D2 |
Arkadi | 8:8a3e6241c104 | 88 | DigitalOut led(PA_5); // D13 |
Arkadi | 8:8a3e6241c104 | 89 | DigitalOut mytoggle(PA_6); // D12 |
Arkadi | 0:bfac05b2d50f | 90 | |
Arkadi | 1:574b54755983 | 91 | // analog / define to init at mbed initialization. work directly with registers |
Arkadi | 0:bfac05b2d50f | 92 | AnalogOut dsp_output(PA_4); |
Arkadi | 0:bfac05b2d50f | 93 | AnalogIn dsp_input(PA_0); |
Arkadi | 0:bfac05b2d50f | 94 | |
Arkadi | 1:574b54755983 | 95 | |
Arkadi | 1:574b54755983 | 96 | // ADC/DAC declarations |
Arkadi | 1:574b54755983 | 97 | ADC_HandleTypeDef hadc1; |
Arkadi | 1:574b54755983 | 98 | DAC_HandleTypeDef hdac1; |
Arkadi | 1:574b54755983 | 99 | |
Arkadi | 0:bfac05b2d50f | 100 | /////////////// |
Arkadi | 0:bfac05b2d50f | 101 | // variables // |
Arkadi | 0:bfac05b2d50f | 102 | /////////////// |
Arkadi | 0:bfac05b2d50f | 103 | |
Arkadi | 9:7e20c1e5a354 | 104 | // Frequency counter |
Arkadi | 9:7e20c1e5a354 | 105 | uint32_t toggleCounter = 0; |
Arkadi | 9:7e20c1e5a354 | 106 | // toogle pin state |
Arkadi | 9:7e20c1e5a354 | 107 | bool toggleCounterState = 0; |
Arkadi | 12:9d30df1529be | 108 | // toogle pin state |
Arkadi | 12:9d30df1529be | 109 | bool toggelState=0; |
Arkadi | 9:7e20c1e5a354 | 110 | |
Arkadi | 1:574b54755983 | 111 | // json buffer |
Arkadi | 1:574b54755983 | 112 | char json[MSG_BUFFER_SIZE]; |
Arkadi | 1:574b54755983 | 113 | |
Arkadi | 1:574b54755983 | 114 | // packet variables |
Arkadi | 1:574b54755983 | 115 | struct packetMSG_struct { |
Arkadi | 1:574b54755983 | 116 | // recieve message variables |
Arkadi | 1:574b54755983 | 117 | uint8_t header[HEADER_SIZE]; |
Arkadi | 1:574b54755983 | 118 | uint8_t footer[FOOTER_SIZE]; |
Arkadi | 1:574b54755983 | 119 | uint8_t syncIndex; // sync index for header / footer |
Arkadi | 1:574b54755983 | 120 | uint8_t syncFlag; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer |
Arkadi | 1:574b54755983 | 121 | // buffer |
Arkadi | 1:574b54755983 | 122 | uint16_t bufferIndex; // buffer index |
Arkadi | 1:574b54755983 | 123 | uint8_t buffer[MSG_BUFFER_SIZE]; |
Arkadi | 1:574b54755983 | 124 | } ; |
Arkadi | 1:574b54755983 | 125 | packetMSG_struct packetMSG; |
Arkadi | 1:574b54755983 | 126 | |
Arkadi | 1:574b54755983 | 127 | |
Arkadi | 1:574b54755983 | 128 | // Dac Register for direct method of setting DAC value`s |
Arkadi | 1:574b54755983 | 129 | __IO uint32_t Dac_Reg = 0; |
Arkadi | 1:574b54755983 | 130 | |
Arkadi | 1:574b54755983 | 131 | // create function pointer |
Arkadi | 1:574b54755983 | 132 | typedef void(*functionPtr)(void); |
Arkadi | 1:574b54755983 | 133 | functionPtr FilterFunction; |
Arkadi | 1:574b54755983 | 134 | |
Arkadi | 3:48258b86e182 | 135 | // alternative functuin selection (faster mcu process) |
Arkadi | 5:ec6f2323a263 | 136 | volatile uint8_t operationMode = 1; |
Arkadi | 4:59319802012b | 137 | // 0 - off |
Arkadi | 4:59319802012b | 138 | // 1 - passthrough - no filter |
Arkadi | 4:59319802012b | 139 | // 2 - highpass - High Pass filter |
Arkadi | 4:59319802012b | 140 | // 3 - hpf_trig - High Pass filter + Trigger mode |
Arkadi | 7:25c81cb23e42 | 141 | // 4 - gains_trig - highpass filter + trigger mode + Gains vector |
Arkadi | 3:48258b86e182 | 142 | |
Arkadi | 0:bfac05b2d50f | 143 | /////////////// |
Arkadi | 0:bfac05b2d50f | 144 | // Functions // |
Arkadi | 0:bfac05b2d50f | 145 | /////////////// |
Arkadi | 0:bfac05b2d50f | 146 | |
Arkadi | 1:574b54755983 | 147 | // init functions header //hadc1 needs to be defined preiod #incude |
Arkadi | 1:574b54755983 | 148 | #include "adc_init.h" // initialize adc/dac directly for continious sample |
Arkadi | 1:574b54755983 | 149 | #include "filters.h" |
Arkadi | 1:574b54755983 | 150 | |
Arkadi | 1:574b54755983 | 151 | // nop operation |
Arkadi | 1:574b54755983 | 152 | inline void NOP() |
Arkadi | 1:574b54755983 | 153 | { |
Arkadi | 1:574b54755983 | 154 | __ASM volatile ("nop"); // one tick operation, Use to adjust frequency by slowing down the proccess |
Arkadi | 1:574b54755983 | 155 | } |
Arkadi | 1:574b54755983 | 156 | |
Arkadi | 1:574b54755983 | 157 | // Serial Event function |
Arkadi | 1:574b54755983 | 158 | void rxCallback(void); |
Arkadi | 1:574b54755983 | 159 | |
Arkadi | 1:574b54755983 | 160 | // initialize packet struct |
Arkadi | 1:574b54755983 | 161 | void initPacket(void); |
Arkadi | 1:574b54755983 | 162 | |
Arkadi | 1:574b54755983 | 163 | // Packet Parser |
Arkadi | 1:574b54755983 | 164 | void parsePacket(void); |
Arkadi | 1:574b54755983 | 165 | |
Arkadi | 1:574b54755983 | 166 | // DSP Packet Handler |
Arkadi | 1:574b54755983 | 167 | void dspPacket(void); |
Arkadi | 1:574b54755983 | 168 | |
Arkadi | 8:8a3e6241c104 | 169 | // DSP Param Packet Handler |
Arkadi | 8:8a3e6241c104 | 170 | void dspParamPacket(void); |
Arkadi | 8:8a3e6241c104 | 171 | |
Arkadi | 10:273127efdc6e | 172 | // DSP Filter Packet Handler |
Arkadi | 10:273127efdc6e | 173 | void dspFilterPacket(void); |
Arkadi | 10:273127efdc6e | 174 | |
Arkadi | 13:c0a13a99905e | 175 | // DSP Play Packet Handler |
Arkadi | 13:c0a13a99905e | 176 | void dspPlayPacket(void); |
Arkadi | 13:c0a13a99905e | 177 | |
Arkadi | 1:574b54755983 | 178 | // initialize DSP |
Arkadi | 1:574b54755983 | 179 | void initDSP(void); |
Arkadi | 0:bfac05b2d50f | 180 | |
Arkadi | 0:bfac05b2d50f | 181 | //////////////////////// |
Arkadi | 0:bfac05b2d50f | 182 | // Main Code Setup : // |
Arkadi | 0:bfac05b2d50f | 183 | //////////////////////// |
Arkadi | 0:bfac05b2d50f | 184 | int main() |
Arkadi | 0:bfac05b2d50f | 185 | { |
Arkadi | 1:574b54755983 | 186 | // init packet: |
Arkadi | 1:574b54755983 | 187 | initPacket(); |
Arkadi | 1:574b54755983 | 188 | |
Arkadi | 1:574b54755983 | 189 | // initialize DSP |
Arkadi | 1:574b54755983 | 190 | initDSP(); |
Arkadi | 1:574b54755983 | 191 | |
Arkadi | 0:bfac05b2d50f | 192 | pc.baud(57600); |
Arkadi | 2:59c1a24aed7c | 193 | switch_dsp.baud(57600); |
Arkadi | 2:59c1a24aed7c | 194 | |
Arkadi | 2:59c1a24aed7c | 195 | |
Arkadi | 2:59c1a24aed7c | 196 | // pc is used for debbuging, in application mode the commands are from the switch_dsp. |
Arkadi | 1:574b54755983 | 197 | // attach serial event interrupt |
Arkadi | 1:574b54755983 | 198 | pc.attach(&rxCallback, Serial::RxIrq); |
Arkadi | 2:59c1a24aed7c | 199 | |
Arkadi | 2:59c1a24aed7c | 200 | // attach serial event interrupt |
Arkadi | 2:59c1a24aed7c | 201 | switch_dsp.attach(&rxCallback, Serial::RxIrq); |
Arkadi | 2:59c1a24aed7c | 202 | |
Arkadi | 0:bfac05b2d50f | 203 | #ifdef DEBUG_MOD1 |
Arkadi | 0:bfac05b2d50f | 204 | pc.printf("ReSpeaker Test \r\n"); |
Arkadi | 0:bfac05b2d50f | 205 | #endif |
Arkadi | 0:bfac05b2d50f | 206 | |
Arkadi | 0:bfac05b2d50f | 207 | /////////////////////// |
Arkadi | 0:bfac05b2d50f | 208 | // Main Code Loop : // |
Arkadi | 0:bfac05b2d50f | 209 | /////////////////////// |
Arkadi | 0:bfac05b2d50f | 210 | while(1) { |
Arkadi | 1:574b54755983 | 211 | // run selected filter |
Arkadi | 3:48258b86e182 | 212 | if (operationMode == 0) { |
Arkadi | 4:59319802012b | 213 | offMode(); |
Arkadi | 3:48258b86e182 | 214 | } else if (operationMode == 1) { |
Arkadi | 4:59319802012b | 215 | passthrough(); |
Arkadi | 4:59319802012b | 216 | } else if (operationMode == 2) { |
Arkadi | 4:59319802012b | 217 | highpass(); |
Arkadi | 4:59319802012b | 218 | } else if (operationMode == 3) { |
Arkadi | 4:59319802012b | 219 | highpassTrig(); |
Arkadi | 7:25c81cb23e42 | 220 | } else if (operationMode == 4) { |
Arkadi | 13:c0a13a99905e | 221 | GainsTrig(); |
Arkadi | 3:48258b86e182 | 222 | } |
Arkadi | 3:48258b86e182 | 223 | // more elegant but much slower option: |
Arkadi | 3:48258b86e182 | 224 | //FilterFunction(); |
Arkadi | 12:9d30df1529be | 225 | #ifdef TOGGLE_IO_MODE |
Arkadi | 1:574b54755983 | 226 | // toggle pin, Loop frequency indicator |
Arkadi | 1:574b54755983 | 227 | toggelState=!toggelState; |
Arkadi | 1:574b54755983 | 228 | mytoggle.write(toggelState); |
Arkadi | 1:574b54755983 | 229 | #endif |
Arkadi | 9:7e20c1e5a354 | 230 | // Frequency counter |
Arkadi | 9:7e20c1e5a354 | 231 | toggleCounter++; |
Arkadi | 11:07981b0d28d3 | 232 | if (toggleCounter == TICKS2TOGGLE) { |
Arkadi | 9:7e20c1e5a354 | 233 | toggleCounter=0; |
Arkadi | 9:7e20c1e5a354 | 234 | toggleCounterState = !toggleCounterState; |
Arkadi | 9:7e20c1e5a354 | 235 | dsp_trigger.write(toggleCounterState); |
Arkadi | 9:7e20c1e5a354 | 236 | } |
Arkadi | 3:48258b86e182 | 237 | } // end loop |
Arkadi | 3:48258b86e182 | 238 | } // end main |
Arkadi | 0:bfac05b2d50f | 239 | |
Arkadi | 0:bfac05b2d50f | 240 | /////////////// |
Arkadi | 0:bfac05b2d50f | 241 | // Functions // |
Arkadi | 0:bfac05b2d50f | 242 | /////////////// |
Arkadi | 0:bfac05b2d50f | 243 | |
Arkadi | 1:574b54755983 | 244 | // initialize DSP |
Arkadi | 1:574b54755983 | 245 | void initDSP(void) |
Arkadi | 1:574b54755983 | 246 | { |
Arkadi | 1:574b54755983 | 247 | // init dac |
Arkadi | 1:574b54755983 | 248 | ADC1_Init(); |
Arkadi | 1:574b54755983 | 249 | DAC1_Init(); |
Arkadi | 1:574b54755983 | 250 | |
Arkadi | 1:574b54755983 | 251 | HAL_ADC_Start(&hadc1); |
Arkadi | 1:574b54755983 | 252 | HAL_DAC_Start(&hdac1, DAC_CHANNEL_1); |
Arkadi | 1:574b54755983 | 253 | |
Arkadi | 1:574b54755983 | 254 | // define Dac Register for direct method of setting DAC value`s |
Arkadi | 1:574b54755983 | 255 | Dac_Reg = (uint32_t) (hdac1.Instance); |
Arkadi | 1:574b54755983 | 256 | Dac_Reg += __HAL_DHR12R1_ALIGNEMENT(DAC_ALIGN_12B_R); |
Arkadi | 1:574b54755983 | 257 | |
Arkadi | 1:574b54755983 | 258 | // intialize filter function pointer |
Arkadi | 13:c0a13a99905e | 259 | FilterFunction = offMode;//highpass_filter; |
Arkadi | 6:e8b4ca41c691 | 260 | operationMode = 0 ; |
Arkadi | 1:574b54755983 | 261 | } |
Arkadi | 1:574b54755983 | 262 | |
Arkadi | 1:574b54755983 | 263 | // Serial Event function |
Arkadi | 1:574b54755983 | 264 | void rxCallback(void) |
Arkadi | 0:bfac05b2d50f | 265 | { |
Arkadi | 2:59c1a24aed7c | 266 | while ((pc.readable()) || (switch_dsp.readable())) { |
Arkadi | 1:574b54755983 | 267 | // read icoming |
Arkadi | 2:59c1a24aed7c | 268 | uint8_t in_byte=0; |
Arkadi | 1:574b54755983 | 269 | //led = !led; |
Arkadi | 2:59c1a24aed7c | 270 | if (pc.readable()) { |
Arkadi | 2:59c1a24aed7c | 271 | in_byte = pc.getc(); |
Arkadi | 2:59c1a24aed7c | 272 | } else if (switch_dsp.readable()) { |
Arkadi | 2:59c1a24aed7c | 273 | in_byte = switch_dsp.getc(); |
Arkadi | 2:59c1a24aed7c | 274 | } |
Arkadi | 1:574b54755983 | 275 | #ifdef DEBUG_MOD1 |
Arkadi | 1:574b54755983 | 276 | pc.putc(in_byte); |
Arkadi | 1:574b54755983 | 277 | #endif |
Arkadi | 1:574b54755983 | 278 | // detect start message , end message |
Arkadi | 1:574b54755983 | 279 | switch (packetMSG.syncFlag) { |
Arkadi | 1:574b54755983 | 280 | // waiting for header |
Arkadi | 1:574b54755983 | 281 | case 0: { |
Arkadi | 1:574b54755983 | 282 | if (packetMSG.header[packetMSG.syncIndex] == in_byte) { |
Arkadi | 1:574b54755983 | 283 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 284 | if (packetMSG.syncIndex == HEADER_SIZE) { // finish header SYNC |
Arkadi | 1:574b54755983 | 285 | packetMSG.syncFlag = 1; // start collecting data, wait for footer |
Arkadi | 1:574b54755983 | 286 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 287 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 288 | } |
Arkadi | 1:574b54755983 | 289 | } else { // reinit sync |
Arkadi | 1:574b54755983 | 290 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 291 | } |
Arkadi | 1:574b54755983 | 292 | //pc.printf("case 0 , %d \r\n",packetMSG.syncIndex); |
Arkadi | 1:574b54755983 | 293 | break; |
Arkadi | 1:574b54755983 | 294 | } |
Arkadi | 1:574b54755983 | 295 | // waiting for footer |
Arkadi | 1:574b54755983 | 296 | case 1: { |
Arkadi | 1:574b54755983 | 297 | // add byte to buffer |
Arkadi | 1:574b54755983 | 298 | packetMSG.buffer[packetMSG.bufferIndex] = in_byte; |
Arkadi | 1:574b54755983 | 299 | packetMSG.bufferIndex++; |
Arkadi | 1:574b54755983 | 300 | if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow |
Arkadi | 1:574b54755983 | 301 | // reset buffer |
Arkadi | 1:574b54755983 | 302 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 303 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 304 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 305 | } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved |
Arkadi | 1:574b54755983 | 306 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 307 | packetMSG.syncFlag=2; // move to verify footer |
Arkadi | 1:574b54755983 | 308 | } |
Arkadi | 1:574b54755983 | 309 | //pc.printf("case 2 , %d \r\n",packetMSG.syncIndex); |
Arkadi | 1:574b54755983 | 310 | break; |
Arkadi | 1:574b54755983 | 311 | } |
Arkadi | 1:574b54755983 | 312 | // verify footer |
Arkadi | 1:574b54755983 | 313 | case 2: { |
Arkadi | 1:574b54755983 | 314 | // add byte to buffer |
Arkadi | 1:574b54755983 | 315 | packetMSG.buffer[packetMSG.bufferIndex] = in_byte; |
Arkadi | 1:574b54755983 | 316 | packetMSG.bufferIndex++; |
Arkadi | 1:574b54755983 | 317 | if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow |
Arkadi | 1:574b54755983 | 318 | // reset buffer |
Arkadi | 1:574b54755983 | 319 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 320 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 321 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 322 | } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved |
Arkadi | 1:574b54755983 | 323 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 324 | if (packetMSG.syncIndex == FOOTER_SIZE) { // finish footer SYNC |
Arkadi | 1:574b54755983 | 325 | packetMSG.syncFlag = 3; |
Arkadi | 1:574b54755983 | 326 | // copy packet to json buffer |
Arkadi | 1:574b54755983 | 327 | memcpy (&json, &packetMSG.buffer, packetMSG.bufferIndex); |
Arkadi | 1:574b54755983 | 328 | json[packetMSG.bufferIndex]=NULL; // end with NULL to indicate end of string |
Arkadi | 1:574b54755983 | 329 | // copy packet to json buffer with sprintf |
Arkadi | 1:574b54755983 | 330 | //sprintf(json, "%.*s", packetMSG.bufferIndex, packetMSG.buffer ); |
Arkadi | 1:574b54755983 | 331 | // send msg to parse. |
Arkadi | 1:574b54755983 | 332 | parsePacket(); |
Arkadi | 1:574b54755983 | 333 | // reset buffer |
Arkadi | 1:574b54755983 | 334 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 335 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 336 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 337 | } |
Arkadi | 1:574b54755983 | 338 | } else { // footer broke restart wait for footer |
Arkadi | 1:574b54755983 | 339 | packetMSG.syncFlag=1; |
Arkadi | 1:574b54755983 | 340 | // verify that it didnt broke on first footer char |
Arkadi | 1:574b54755983 | 341 | if (packetMSG.footer[0] == in_byte) { |
Arkadi | 1:574b54755983 | 342 | packetMSG.syncIndex=1; |
Arkadi | 1:574b54755983 | 343 | } else { |
Arkadi | 1:574b54755983 | 344 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 345 | } |
Arkadi | 1:574b54755983 | 346 | } |
Arkadi | 1:574b54755983 | 347 | break; |
Arkadi | 1:574b54755983 | 348 | } |
Arkadi | 1:574b54755983 | 349 | default: { |
Arkadi | 1:574b54755983 | 350 | pc.printf("Sonmething went wrong \r\n"); |
Arkadi | 1:574b54755983 | 351 | break; |
Arkadi | 1:574b54755983 | 352 | } |
Arkadi | 1:574b54755983 | 353 | } // end switch |
Arkadi | 1:574b54755983 | 354 | }// end uart readable |
Arkadi | 1:574b54755983 | 355 | } // end rxCallback |
Arkadi | 1:574b54755983 | 356 | |
Arkadi | 1:574b54755983 | 357 | // initialize packet struct |
Arkadi | 1:574b54755983 | 358 | void initPacket(void) |
Arkadi | 1:574b54755983 | 359 | { |
Arkadi | 1:574b54755983 | 360 | // init variables to default: |
Arkadi | 1:574b54755983 | 361 | packetMSG.header[0] = 'j'; |
Arkadi | 1:574b54755983 | 362 | packetMSG.header[1] = 's'; |
Arkadi | 1:574b54755983 | 363 | packetMSG.header[2] = 'o'; |
Arkadi | 1:574b54755983 | 364 | packetMSG.header[3] = 'n'; |
Arkadi | 1:574b54755983 | 365 | packetMSG.header[4] = ':'; |
Arkadi | 1:574b54755983 | 366 | |
Arkadi | 1:574b54755983 | 367 | packetMSG.footer[0]= 13; // /r |
Arkadi | 1:574b54755983 | 368 | packetMSG.footer[1]= 10; // /n |
Arkadi | 1:574b54755983 | 369 | |
Arkadi | 1:574b54755983 | 370 | packetMSG.syncIndex=0; // sync index for header / footer |
Arkadi | 1:574b54755983 | 371 | packetMSG.syncFlag=0; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer |
Arkadi | 1:574b54755983 | 372 | packetMSG.bufferIndex=0; // buffer index |
Arkadi | 1:574b54755983 | 373 | }// end init Packet struct |
Arkadi | 1:574b54755983 | 374 | |
Arkadi | 1:574b54755983 | 375 | |
Arkadi | 1:574b54755983 | 376 | |
Arkadi | 1:574b54755983 | 377 | // Packet Parser |
Arkadi | 1:574b54755983 | 378 | void parsePacket(void) |
Arkadi | 1:574b54755983 | 379 | { |
Arkadi | 1:574b54755983 | 380 | string targetName; |
Arkadi | 1:574b54755983 | 381 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 382 | // write buffer to screen |
Arkadi | 1:574b54755983 | 383 | //pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer ); |
Arkadi | 1:574b54755983 | 384 | pc.printf("%s", json); |
Arkadi | 1:574b54755983 | 385 | #endif |
Arkadi | 1:574b54755983 | 386 | |
Arkadi | 1:574b54755983 | 387 | // GUI message format Switch: {"name":"switch","mic":0, "spk": [0,1,0,0,0]} |
Arkadi | 1:574b54755983 | 388 | parse(guiCmd, json); |
Arkadi | 1:574b54755983 | 389 | |
Arkadi | 1:574b54755983 | 390 | // get target: |
Arkadi | 1:574b54755983 | 391 | targetName = guiCmd["name"].get<string>(); // switch / dsp |
Arkadi | 1:574b54755983 | 392 | |
Arkadi | 1:574b54755983 | 393 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 394 | // send parsed values |
Arkadi | 1:574b54755983 | 395 | pc.printf("targetName: %s \r\n", targetName.c_str()); |
Arkadi | 1:574b54755983 | 396 | #endif |
Arkadi | 1:574b54755983 | 397 | |
Arkadi | 1:574b54755983 | 398 | // select handler based on target mcu |
Arkadi | 1:574b54755983 | 399 | if (targetName == "dsp") { |
Arkadi | 1:574b54755983 | 400 | dspPacket(); |
Arkadi | 8:8a3e6241c104 | 401 | } else if (targetName == "dspParam") { |
Arkadi | 8:8a3e6241c104 | 402 | dspParamPacket(); |
Arkadi | 10:273127efdc6e | 403 | } else if (targetName == "dspFilter") { |
Arkadi | 10:273127efdc6e | 404 | dspFilterPacket(); |
Arkadi | 13:c0a13a99905e | 405 | } else if (targetName == "dspPlay") { |
Arkadi | 13:c0a13a99905e | 406 | dspPlayPacket(); |
Arkadi | 1:574b54755983 | 407 | } else { |
Arkadi | 1:574b54755983 | 408 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 409 | // unrecognised target |
Arkadi | 1:574b54755983 | 410 | pc.printf("unrecognised target: %s \r\n", targetName.c_str()); |
Arkadi | 1:574b54755983 | 411 | #endif |
Arkadi | 0:bfac05b2d50f | 412 | } |
Arkadi | 1:574b54755983 | 413 | // led blink |
Arkadi | 1:574b54755983 | 414 | led = !led; |
Arkadi | 1:574b54755983 | 415 | }// end parse |
Arkadi | 1:574b54755983 | 416 | |
Arkadi | 13:c0a13a99905e | 417 | |
Arkadi | 1:574b54755983 | 418 | // DSP Packet Handler |
Arkadi | 1:574b54755983 | 419 | void dspPacket(void) |
Arkadi | 1:574b54755983 | 420 | { |
Arkadi | 1:574b54755983 | 421 | string modeType; |
Arkadi | 4:59319802012b | 422 | // get operation mode |
Arkadi | 1:574b54755983 | 423 | modeType = guiCmd["mode"].get<string>(); |
Arkadi | 1:574b54755983 | 424 | |
Arkadi | 1:574b54755983 | 425 | #ifdef DEBUG_MOD10 |
Arkadi | 1:574b54755983 | 426 | // send parsed values |
Arkadi | 4:59319802012b | 427 | pc.printf("mode: %s\r\n", modeType.c_str()); |
Arkadi | 8:8a3e6241c104 | 428 | //switch_dsp.printf("mode: %s\r\n", modeType.c_str()); |
Arkadi | 1:574b54755983 | 429 | #endif |
Arkadi | 1:574b54755983 | 430 | // selected operation mode |
Arkadi | 4:59319802012b | 431 | if ( modeType == "off" ) { |
Arkadi | 4:59319802012b | 432 | FilterFunction = offMode; |
Arkadi | 3:48258b86e182 | 433 | operationMode = 0 ; |
Arkadi | 4:59319802012b | 434 | } else if( modeType == "passthrough" ) { |
Arkadi | 4:59319802012b | 435 | FilterFunction = passthrough; |
Arkadi | 3:48258b86e182 | 436 | operationMode = 1 ; |
Arkadi | 8:8a3e6241c104 | 437 | } else if( modeType == "highpass" ) { |
Arkadi | 4:59319802012b | 438 | FilterFunction = highpass; |
Arkadi | 4:59319802012b | 439 | operationMode = 2 ; |
Arkadi | 8:8a3e6241c104 | 440 | } else if( modeType == "hpf_trig" ) { |
Arkadi | 4:59319802012b | 441 | FilterFunction = highpassTrig; |
Arkadi | 4:59319802012b | 442 | operationMode = 3 ; |
Arkadi | 8:8a3e6241c104 | 443 | } else if( modeType == "gain_trig" ) { |
Arkadi | 7:25c81cb23e42 | 444 | FilterFunction = GainsTrig; |
Arkadi | 7:25c81cb23e42 | 445 | operationMode = 4 ; |
Arkadi | 1:574b54755983 | 446 | } else { |
Arkadi | 4:59319802012b | 447 | switch_dsp.printf("undefined mode %s \r\n", modeType.c_str()); |
Arkadi | 4:59319802012b | 448 | FilterFunction = offMode; |
Arkadi | 3:48258b86e182 | 449 | operationMode = 0 ; |
Arkadi | 1:574b54755983 | 450 | } |
Arkadi | 10:273127efdc6e | 451 | // succesfull parse |
Arkadi | 10:273127efdc6e | 452 | switch_dsp.printf("{\"Ack\":\"dsp\",\"mode\":\"%s\"}\r\n",modeType.c_str()); |
Arkadi | 8:8a3e6241c104 | 453 | }// end dspPacket |
Arkadi | 8:8a3e6241c104 | 454 | // DSP Param Packet Handler |
Arkadi | 8:8a3e6241c104 | 455 | void dspParamPacket(void) |
Arkadi | 11:07981b0d28d3 | 456 | { |
Arkadi | 8:8a3e6241c104 | 457 | // get values. |
Arkadi | 8:8a3e6241c104 | 458 | signalGain = ((float)guiCmd["gain"].get<int>())/1000; // issue in parsing doubles when the number is round |
Arkadi | 8:8a3e6241c104 | 459 | trigTresh = ((float)guiCmd["trigTresh"].get<int>()) / 100.0f; |
Arkadi | 8:8a3e6241c104 | 460 | trigDelaySet = (uint32_t)guiCmd["trigPass"].get<int>(); |
Arkadi | 8:8a3e6241c104 | 461 | trigPause = (uint32_t)guiCmd["trigPause"].get<int>(); |
Arkadi | 8:8a3e6241c104 | 462 | |
Arkadi | 8:8a3e6241c104 | 463 | trigDelay = trigDelaySet; |
Arkadi | 8:8a3e6241c104 | 464 | #ifdef DEBUG_MOD10 |
Arkadi | 8:8a3e6241c104 | 465 | // send parsed values |
Arkadi | 10:273127efdc6e | 466 | pc.printf("SignalGain: %.3f , trigTresh: %.3f , trigDelaySet %d , trigPause: %d\r\n", signalGain , trigTresh , trigDelaySet , trigPause); |
Arkadi | 8:8a3e6241c104 | 467 | #endif |
Arkadi | 10:273127efdc6e | 468 | // succesfull parse |
Arkadi | 10:273127efdc6e | 469 | switch_dsp.printf("{\"Ack\":\"dspParam\",\"SignalGain\":%.3f,\"trigTresh\":%.3f,\"trigDelaySet\":%d,\"trigPause\":%d}\r\n", signalGain , trigTresh , trigDelaySet , trigPause); |
Arkadi | 10:273127efdc6e | 470 | } // end dspParamPacket |
Arkadi | 8:8a3e6241c104 | 471 | |
Arkadi | 10:273127efdc6e | 472 | // DSP Filter Packet Handler |
Arkadi | 10:273127efdc6e | 473 | void dspFilterPacket(void) |
Arkadi | 11:07981b0d28d3 | 474 | { |
Arkadi | 10:273127efdc6e | 475 | // get values. |
Arkadi | 10:273127efdc6e | 476 | FilterSections = (guiCmd["Sections"].get<int>()); |
Arkadi | 10:273127efdc6e | 477 | Gscale = ((float)(guiCmd["Gscale"].get<int>()))/10000.0f; // issue in parsing doubles |
Arkadi | 11:07981b0d28d3 | 478 | |
Arkadi | 10:273127efdc6e | 479 | // get SOSMat |
Arkadi | 11:07981b0d28d3 | 480 | for (int jj=0 ; jj < FilterSections ; jj++) { |
Arkadi | 11:07981b0d28d3 | 481 | for (int ii=0 ; ii < 6 ; ii++) { |
Arkadi | 11:07981b0d28d3 | 482 | SOSMat[jj][ii] = ((float)guiCmd["SOSMat"][ii+jj*6].get<int>())/10000.0f; |
Arkadi | 10:273127efdc6e | 483 | } |
Arkadi | 10:273127efdc6e | 484 | } |
Arkadi | 10:273127efdc6e | 485 | #ifdef DEBUG_MOD10 |
Arkadi | 10:273127efdc6e | 486 | // send parsed values |
Arkadi | 10:273127efdc6e | 487 | pc.printf("FilterSections: %d , Gscale: %.3f\r\n", FilterSections , Gscale); |
Arkadi | 10:273127efdc6e | 488 | pc.printf("SOSMat \r\n"); |
Arkadi | 11:07981b0d28d3 | 489 | for (int jj=0 ; jj < FilterSections ; jj++) { |
Arkadi | 10:273127efdc6e | 490 | pc.printf("%.3f , %.3f , %.3f , %.3f , %.3f , %.3f \r\n",SOSMat[jj][0] ,SOSMat[jj][1],SOSMat[jj][2],SOSMat[jj][3] ,SOSMat[jj][4] ,SOSMat[jj][5]); |
Arkadi | 10:273127efdc6e | 491 | } |
Arkadi | 10:273127efdc6e | 492 | #endif |
Arkadi | 10:273127efdc6e | 493 | // succesfull parse |
Arkadi | 10:273127efdc6e | 494 | switch_dsp.printf("{\"Ack\":\"dspFilter\",\"FilterSections\":%d,\"Gscale\":%.3f}\r\n", FilterSections , Gscale); |
Arkadi | 13:c0a13a99905e | 495 | } // end dspFilterPacket |
Arkadi | 13:c0a13a99905e | 496 | |
Arkadi | 13:c0a13a99905e | 497 | // DSP Play Packet Handler |
Arkadi | 13:c0a13a99905e | 498 | void dspPlayPacket(void) |
Arkadi | 13:c0a13a99905e | 499 | { |
Arkadi | 13:c0a13a99905e | 500 | // get values. |
Arkadi | 13:c0a13a99905e | 501 | float playGain = ((float)guiCmd["gain"].get<int>())/1000; // issue in parsing doubles when the number is round |
Arkadi | 13:c0a13a99905e | 502 | uint32_t playFile = (uint32_t)guiCmd["file"].get<int>(); // file to play |
Arkadi | 13:c0a13a99905e | 503 | |
Arkadi | 13:c0a13a99905e | 504 | // succesfull parse |
Arkadi | 13:c0a13a99905e | 505 | switch_dsp.printf("{\"Ack\":\"dspPlay\",\"file\":%d,\"gain\":%.3f}\r\n", playFile, playGain); |
Arkadi | 13:c0a13a99905e | 506 | ////////////////////////////////////////////////////////////////// |
Arkadi | 13:c0a13a99905e | 507 | // Temp mode - need to update file selection , gain settings // |
Arkadi | 13:c0a13a99905e | 508 | ////////////////////////////////////////////////////////////////// |
Arkadi | 13:c0a13a99905e | 509 | __disable_irq(); // Disable Interrupts |
Arkadi | 13:c0a13a99905e | 510 | // generate chirp out |
Arkadi | 13:c0a13a99905e | 511 | for (int ii=0; ii<NUM_SAMPLES; ii++) { |
Arkadi | 13:c0a13a99905e | 512 | // toogle io for loop frequency |
Arkadi | 13:c0a13a99905e | 513 | toggelState=!toggelState; |
Arkadi | 13:c0a13a99905e | 514 | mytoggle.write(toggelState); |
Arkadi | 13:c0a13a99905e | 515 | // generate delay for 1MHz Sample rate |
Arkadi | 13:c0a13a99905e | 516 | for (int jj=0; jj<31; jj++) { |
Arkadi | 13:c0a13a99905e | 517 | NOP(); |
Arkadi | 13:c0a13a99905e | 518 | } |
Arkadi | 13:c0a13a99905e | 519 | // micro nops :) |
Arkadi | 13:c0a13a99905e | 520 | NOP(); |
Arkadi | 13:c0a13a99905e | 521 | NOP(); |
Arkadi | 13:c0a13a99905e | 522 | NOP(); |
Arkadi | 13:c0a13a99905e | 523 | NOP(); |
Arkadi | 13:c0a13a99905e | 524 | NOP(); |
Arkadi | 13:c0a13a99905e | 525 | NOP(); |
Arkadi | 13:c0a13a99905e | 526 | NOP(); |
Arkadi | 13:c0a13a99905e | 527 | // Output value using DAC |
Arkadi | 13:c0a13a99905e | 528 | // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut); |
Arkadi | 13:c0a13a99905e | 529 | *(__IO uint32_t *) Dac_Reg = chirpData[ii]; |
Arkadi | 13:c0a13a99905e | 530 | } |
Arkadi | 13:c0a13a99905e | 531 | // Output value using DAC |
Arkadi | 13:c0a13a99905e | 532 | // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut); |
Arkadi | 13:c0a13a99905e | 533 | *(__IO uint32_t *) Dac_Reg = (uint16_t)(4095/2); |
Arkadi | 13:c0a13a99905e | 534 | __enable_irq(); // Enable Interrupts |
Arkadi | 13:c0a13a99905e | 535 | |
Arkadi | 13:c0a13a99905e | 536 | // Option to reset play mode after file has been played |
Arkadi | 13:c0a13a99905e | 537 | //operationMode = 0; |
Arkadi | 13:c0a13a99905e | 538 | } // end dspPlayPacket |