diff --git a/examples/02-Utility/SDWriter_wApp/SDWriter_wApp.ino b/examples/02-Utility/SDWriter_wApp/SDWriter_wApp.ino index 88cf07be..428a2af0 100644 --- a/examples/02-Utility/SDWriter_wApp/SDWriter_wApp.ino +++ b/examples/02-Utility/SDWriter_wApp/SDWriter_wApp.ino @@ -41,7 +41,7 @@ AudioConnection_F32 patchcord4(i2s_in, 1, audioSDWriter, 1); //conne // /////////// Create classes for controlling the system, espcially via USB Serial and via the App #include "SerialManager.h" #include "State.h" -BLE_UI ble(&Serial1); //create bluetooth BLE class +BLE_UI ble(&myTympan); //create bluetooth BLE class SerialManager serialManager(&ble); //create the serial manager for real-time control (via USB or App) State myState(&audio_settings, &myTympan, &serialManager); //keeping one's state is useful for the App's GUI diff --git a/examples/02-Utility/SoundLevelMeter/SoundLevelMeter.ino b/examples/02-Utility/SoundLevelMeter/SoundLevelMeter.ino index 6b0941bc..230b16a2 100644 --- a/examples/02-Utility/SoundLevelMeter/SoundLevelMeter.ino +++ b/examples/02-Utility/SoundLevelMeter/SoundLevelMeter.ino @@ -34,7 +34,7 @@ AudioConnection_F32 patchCord3(i2s_in, 0, i2s_out, 0); //echo the ori AudioConnection_F32 patchCord4(calcLevel1, 0, i2s_out, 1); //connect level to the right output //Create BLE and serialManager -BLE ble = BLE(&Serial1); //&Serial1 is the serial connected to the Bluetooth module +BLE ble(&myTympan); //&Serial1 is the serial connected to the Bluetooth module SerialManager serialManager(&ble); State myState(&audio_settings, &myTympan); diff --git a/examples/04-FrequencyDomain/FrequencyCompression_FD/FrequencyCompression_FD.ino b/examples/04-FrequencyDomain/FrequencyCompression_FD/FrequencyCompression_FD.ino index f7b2b229..b672eb52 100644 --- a/examples/04-FrequencyDomain/FrequencyCompression_FD/FrequencyCompression_FD.ino +++ b/examples/04-FrequencyDomain/FrequencyCompression_FD/FrequencyCompression_FD.ino @@ -87,7 +87,7 @@ AudioConnection_F32 patchCord30(gain_L, 0, i2s_out, 0); //connect to AudioConnection_F32 patchCord41(gain_R, 0, i2s_out, 1); //connect to the right output //Create BLE -BLE ble = BLE(&Serial1); +BLE ble(&myTympan); //control display and serial interaction SerialManager serialManager(&ble); diff --git a/examples/04-FrequencyDomain/NoiseReduction_FD/NoiseReduction_FD.ino b/examples/04-FrequencyDomain/NoiseReduction_FD/NoiseReduction_FD.ino index 55223f7a..21d51ca4 100644 --- a/examples/04-FrequencyDomain/NoiseReduction_FD/NoiseReduction_FD.ino +++ b/examples/04-FrequencyDomain/NoiseReduction_FD/NoiseReduction_FD.ino @@ -44,7 +44,7 @@ AudioConnection_F32 patchCord12(gain_L, 0, i2s_out, 1); //connect //Create BLE #define USE_BLE (false) const bool use_ble = USE_BLE; -BLE ble = BLE(&Serial1); +BLE ble(&myTympan); //control display and serial interaction SerialManager serialManager(&ble); diff --git a/examples/05-FullSystems/WDRC_8BandFIR_full/WDRC_8BandFIR_full.ino b/examples/05-FullSystems/WDRC_8BandFIR_full/WDRC_8BandFIR_full.ino index 9267f940..5174fd71 100644 --- a/examples/05-FullSystems/WDRC_8BandFIR_full/WDRC_8BandFIR_full.ino +++ b/examples/05-FullSystems/WDRC_8BandFIR_full/WDRC_8BandFIR_full.ino @@ -55,9 +55,9 @@ Tympan myTympan(TympanRev::E, audio_settings); //choose TympanRev::D or Tymp // Create classes for controlling the system #include "SerialManager.h" -#include "State.h" //must be after N_CHAN is defined -BLE_UI ble(&Serial1); //create bluetooth BLE class -SerialManager serialManager(&ble); //create the serial manager for real-time control (via USB or App) +#include "State.h" //must be after N_CHAN is defined +BLE_UI ble(&myTympan); //create bluetooth BLE class +SerialManager serialManager(&ble); //create the serial manager for real-time control (via USB or App) State myState(&audio_settings, &myTympan, &serialManager); //keeping one's state is useful for the App's GUI diff --git a/examples/06-AppTutorial/BasicGain_wApp/BasicGain_wApp.ino b/examples/06-AppTutorial/BasicGain_wApp/BasicGain_wApp.ino index 92d0ef79..1fa48b66 100644 --- a/examples/06-AppTutorial/BasicGain_wApp/BasicGain_wApp.ino +++ b/examples/06-AppTutorial/BasicGain_wApp/BasicGain_wApp.ino @@ -53,7 +53,7 @@ AudioConnection_F32 patchCord12(gain2, 0, i2s_out, 1); //connect the Righ TympanRemoteFormatter myGUI; //Creates the GUI-writing class for interacting with TympanRemote App //Create BLE -BLE ble = BLE(&Serial1); +BLE ble(&myTympan); // define the setup() function, the function that is called once when the device is booting diff --git a/examples/06-AppTutorial/TrebleBoost_wApp/TrebleBoost_wApp.ino b/examples/06-AppTutorial/TrebleBoost_wApp/TrebleBoost_wApp.ino index 6f41b132..7024644c 100644 --- a/examples/06-AppTutorial/TrebleBoost_wApp/TrebleBoost_wApp.ino +++ b/examples/06-AppTutorial/TrebleBoost_wApp/TrebleBoost_wApp.ino @@ -55,7 +55,7 @@ AudioConnection_F32 patchCord6(gain2, 0, i2s_out, 1); //connect the Ri TympanRemoteFormatter myGUI; //Creates the GUI-writing class for interacting with TympanRemote App //Create BLE -BLE ble = BLE(&Serial1); +BLE ble(&myTympan); // define the setup() function, the function that is called once when the device is booting const float input_gain_dB = 10.0f; //gain on the microphone diff --git a/examples/06-AppTutorial/TrebleBoost_wApp_Alt/TrebleBoost_wApp_Alt.ino b/examples/06-AppTutorial/TrebleBoost_wApp_Alt/TrebleBoost_wApp_Alt.ino index 7ed6b3c2..d8744936 100644 --- a/examples/06-AppTutorial/TrebleBoost_wApp_Alt/TrebleBoost_wApp_Alt.ino +++ b/examples/06-AppTutorial/TrebleBoost_wApp_Alt/TrebleBoost_wApp_Alt.ino @@ -59,7 +59,7 @@ AudioConnection_F32 patchCord6(gain2, 0, i2s_out, 1); //connect the Ri // Create classes for controlling the system #include "SerialManager.h" #include "State.h" -BLE ble(&Serial1); //create bluetooth BLE +BLE ble(&myTympan); //create bluetooth BLE SerialManager serialManager(&ble); //create the serial manager for real-time control (via USB or App) State myState(&audio_settings, &myTympan, &serialManager); //keeping one's state is useful for the App's GUI diff --git a/examples/06-AppTutorial/TrebleBoost_wComp_wApp/TrebleBoost_wComp_wApp.ino b/examples/06-AppTutorial/TrebleBoost_wComp_wApp/TrebleBoost_wComp_wApp.ino index 2b05691f..2abdf508 100644 --- a/examples/06-AppTutorial/TrebleBoost_wComp_wApp/TrebleBoost_wComp_wApp.ino +++ b/examples/06-AppTutorial/TrebleBoost_wComp_wApp/TrebleBoost_wComp_wApp.ino @@ -56,7 +56,7 @@ AudioConnection_F32 patchCord6(comp2, 0, i2s_out, 1); //connect to the // Create classes for controlling the system #include "SerialManager.h" #include "State.h" -BLE ble(&Serial1); //create bluetooth BLE +BLE ble(&myTympan); //create bluetooth BLE SerialManager serialManager(&ble); //create the serial manager for real-time control (via USB or App) State myState(&audio_settings, &myTympan, &serialManager); //keeping one's state is useful for the App's GUI diff --git a/examples/07-Bluetooth/BLELibraryTest/BLELibraryTest.ino b/examples/07-Bluetooth/BLELibraryTest/BLELibraryTest.ino index 46fc4530..3cd1ab02 100644 --- a/examples/07-Bluetooth/BLELibraryTest/BLELibraryTest.ino +++ b/examples/07-Bluetooth/BLELibraryTest/BLELibraryTest.ino @@ -57,7 +57,7 @@ void loop() void BLETest() { - BLE ble = BLE(&Serial1); + BLE ble(&myTympan); if (ble.isConnected()) { diff --git a/examples/07-Bluetooth/BasicGain_wApp_BLEdebug/BasicGain_wApp_BLEdebug.ino b/examples/07-Bluetooth/BasicGain_wApp_BLEdebug/BasicGain_wApp_BLEdebug.ino index 37b58e6e..f3a86dae 100644 --- a/examples/07-Bluetooth/BasicGain_wApp_BLEdebug/BasicGain_wApp_BLEdebug.ino +++ b/examples/07-Bluetooth/BasicGain_wApp_BLEdebug/BasicGain_wApp_BLEdebug.ino @@ -41,7 +41,7 @@ AudioConnection_F32 patchCord12(gain2, 0, i2s_out, 1); //connect the Righ TympanRemoteFormatter myGUI; //Creates the GUI-writing class for interacting with TympanRemote App //Create BLE -BLE ble = BLE(&Serial1); +BLE ble(&myTympan); bool flag_autoAdvertise = true; // define the setup() function, the function that is called once when the device is booting diff --git a/examples/07-Bluetooth/ChangeBaudrate/ChangeBaudrate.ino b/examples/07-Bluetooth/ChangeBaudrate/ChangeBaudrate.ino new file mode 100644 index 00000000..dac56aae --- /dev/null +++ b/examples/07-Bluetooth/ChangeBaudrate/ChangeBaudrate.ino @@ -0,0 +1,203 @@ +/* + ChangeBaudrate + + Created: Ira Ray Jenkins, Creare, August 2021 + Extended by Chip Audette, August 2021 + + Purpose: Demonstrate changing the baudrate of the BC127 module. + + WARNING: Be aware that this change in baud rate will persist on the BC127, even + after cycling the power. Being at a different baud rate will prevent you from a + ccessing the Bluetooth module from any other Tympan program. + + Therefore, if you change the BC127 baud rate, be sure that your other programs are + set to use the new speed. Or, if you want to change back to the "normal" BC127 + speed, use this program again to change the baud rate to the factory default of 9600. + + MIT License. Use at your own risk. Have fun! + +*/ + +#include +#include + +// Let the USER define some parameters +//const int TARGET_BAUDRATE = 115200; //change baudrate to be the fastest speed that we've tested +//const int TARGET_BAUDRATE = 115200/2; //change baudrate to be fast but not the fastest +const int TARGET_BAUDRATE = 9600; //change baudrate back to the factory speed + + +// //////////////////////////////////////////// Automatically define other setup parameters + +// Automatically choose which Tympan and which firmware (you can override!) +#ifdef KINETISK //this is set by the Arduino IDE when you choose Teensy 3.6 + //REV D...does not work reliably with RevD. I don't know why + Tympan myTympan(TympanRev::D); //sets all the control pins correctly + const int FIRMWARE_VERSION = 5; // what Melody firmware we are dealing with +#else + //REV E + Tympan myTympan(TympanRev::E); //sets all the control pins correctly + const int FIRMWARE_VERSION = 7; // what Melody firmware we are dealing with +#endif + +//define the pins that are used for the hardware reset +const int pin_PIO0 = myTympan.getTympanPins().BT_PIO0; //RevD = 56, RevE = 5 // Pin # for connection to BC127 PIO0 pin +const int pin_RST = myTympan.getTympanPins().BT_nReset; //RevD = 34, RevE = 9 // Pin # for connection to BC127 RST pin +//const int pin_VREGEN = myTympan.getTympanPins().BT_REGEN; + +const int baudrate_factory = 9600; // here is the factory default + +// //////////////////////////////////////////// Helper functions + + +void setSerial1BaudRate(int new_baud) { + Serial1.flush(); Serial1.end(); Serial1.begin(new_baud); +} + +//This function attempts to hold PIO 0 high and then reseting via the reset pin. +//See Melody Guide for v7 "Restoring the Default Configuration". +void hardwareReset() { + + //Serial.println("HardwareReset: RST Pin = " + String(pin_RST) + ", PIO0 Pin = " + String(pin_PIO0)); + + pinMode(pin_PIO0,OUTPUT); //prepare the PIO0 pin + digitalWrite(pin_PIO0,HIGH); //normally low. Swtich high + + pinMode(pin_RST,OUTPUT); //prepare the reset pin + digitalWrite(pin_RST,LOW); //pull low to reset + delay(20); //hold low for at least 5 msec + digitalWrite(pin_RST,HIGH); //pull high to start the boot + + //wait for boot to proceed far enough before changing anything + delay(400); //V7: works with 200 but not 175. V5: works with 350 but not 300 + + digitalWrite(pin_PIO0,LOW); //pull PIO0 back down low + pinMode(pin_PIO0,INPUT); //go high-impedance to make irrelevant + +} + +void changeBaudRateBC127(int target_rate, int firmware_ver) { + //Serial.println("setup: Firmware V" + String(firmware_ver) + ": Change baudrate on the BC127 to " + String(target_rate) + "..."); + if (firmware_ver > 5) { + //On version 7.3, the baud rate shift is instant. + Serial1.print("SET UART_CONFIG=");Serial1.print(target_rate);Serial1.print(" OFF 0");Serial1.print('\r'); + } else { + //On version 5, the baud rate shift is instant. + Serial1.print("SET BAUD=");Serial1.print(target_rate);Serial1.print('\r'); + } +} + +// Echo what's coming in on USB Serial out to Bluetooth Serial +void echoIncomingUSBSerial(void) { + while (Serial.available()) + { + char c = Serial.read(); + if (c == '|') { + Serial.println("Switching Serial1 to 115200..."); + setSerial1BaudRate(115200); + } else if (c =='\\') { + Serial.println("Switching Serial1 to 9600..."); + setSerial1BaudRate(9600); + } else if (c == ']') { + Serial.println("Hardware reset..."); + hardwareReset(); + } else { + Serial1.write(c); //send to Bluetooth + } + } +} + +// Echo what's coming in on Bluetooth Serial out to USB Serial +void echoIncomingBTSerial(void) { + static char prev_char; + while (Serial1.available()) { + char orig_c = Serial1.read(); + char c = orig_c; + if ((c == '\n') && (prev_char == '\r')) { + //don't send + } else if ((c == '\r') && (prev_char == '\n')) { + //don't send + } else { + //send the character (perhaps modified) + if (c == '\r') c = '\n'; //translate carriage return to newline + Serial.write(c); + } + prev_char = orig_c; + } +} + + +// /////////////////////////////////////////////////// Arduino setup() and loop() + +void setup() { + + //////////////////////////////////////////////// Factory reset to known baud rate (9600) + + Serial.println("setup(): Setting Teensy Serial1 at " + String(baudrate_factory)); + setSerial1BaudRate(baudrate_factory); + + //clear out buffers + //while (Serial.available()) Serial.read(); + //while (Serial1.available()) Serial1.read(); + + //Force a hardware reset...which should drop baudrate to factory setting + Serial.println("setup(): hardware reset..."); + hardwareReset(); + delay(2000); //delay of 1250 seems to work for V5 but 1000 occasionally fails. 1250 failed once. + echoIncomingBTSerial(); //should get Melody ID text (end with READY or OK) + Serial.println(); + + ///////////////////////////////////////////////// Change baud rate on the BC127 + + //Switch the baudrate on the BC127 + Serial.print("setup(): commanding BC127 to change baud rate to "); Serial.println(TARGET_BAUDRATE); + changeBaudRateBC127(TARGET_BAUDRATE, FIRMWARE_VERSION); + + //Switch Teensy serial to the matching speed + setSerial1BaudRate(TARGET_BAUDRATE); + Serial.println("setup(): switching Teensy Serial1 to new baud rate " + String(TARGET_BAUDRATE)); + + //Listen for response to the change in baud rate (or, at least, clear out the serial + delay(500); //500 seems to work on V5 + echoIncomingBTSerial(); //clear out the serial + + //send carriage return to induce "ERROR" message to clear out the serial link both ways + Serial.println("setup(): issuing Carriage Return to clear serial...should return ERROR..."); + Serial1.print('\r'); //should cause response of "ERROR" + delay(100); echoIncomingBTSerial(); //should get error + + //send WRITE to save the baud rate and send another writes just to clear up the Serial link + Serial.println("setup(): issuing WRITE command to save the new rate"); + Serial1.print("WRITE");Serial1.print('\r'); + delay(200); echoIncomingBTSerial(); //should get OK + + //Serial.println("setup(): issuing second WRITE command to save the new rate to ensure we're good"); + //Serial1.print("WRITE");Serial1.print('\r'); + //delay(200); echoIncomingBTSerial(); //should get OK + + ///////////////////////////////////////////////// Check to make sure that we're good + + Serial.println("setup(): Asking baud rate of BC127..."); + if (FIRMWARE_VERSION > 6) { + Serial1.print("GET UART_CONFIG");Serial1.print('\r'); + } else { + Serial1.print("GET BAUD");Serial1.print('\r'); + } + delay(200); echoIncomingBTSerial(); //should give the value of the new baud rate + + Serial.println(); + Serial.println("We are done! Now echoing commands with the BC127..."); + Serial.println(); + Serial.println("To change BC127 baud rate: for V5, send: SET BAUD=115200 for V7: send: SET UART_CONFIG=115200 OFF 0"); + Serial.println("To change Tympan baud rate, send '|' for 115200 or send '\\' for 9600"); + Serial.println("To force a hardware reset of the BC127, send ']'"); + Serial.println(); + Serial.println("Try BC127 commands like STATUS or VERSION or ADVERTISING ON...(with carriage return!)"); + Serial.println(); +} + +void loop() { + echoIncomingUSBSerial(); + echoIncomingBTSerial(); + delay(5); +} diff --git a/examples/08-AIC_Shield/SDWriter_Quad_wApp/SDWriter_Quad_wApp.ino b/examples/08-AIC_Shield/SDWriter_Quad_wApp/SDWriter_Quad_wApp.ino index 7d8a5ef1..c3465692 100644 --- a/examples/08-AIC_Shield/SDWriter_Quad_wApp/SDWriter_Quad_wApp.ino +++ b/examples/08-AIC_Shield/SDWriter_Quad_wApp/SDWriter_Quad_wApp.ino @@ -46,7 +46,7 @@ AudioConnection_F32 patchcord8(i2s_in, 3, audioSDWriter, 3); //conne // /////////// Create classes for controlling the system, espcially via USB Serial and via the App #include "SerialManager.h" #include "State.h" -BLE_UI ble(&Serial1); //create bluetooth BLE class +BLE_UI ble(&myTympan); //create bluetooth BLE class SerialManager serialManager(&ble); //create the serial manager for real-time control (via USB or App) State myState(&audio_settings, &myTympan, &serialManager); //keeping one's state is useful for the App's GUI diff --git a/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/AudioConnections.h b/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/AudioConnections.h index 8e40f8bd..187ffd49 100644 --- a/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/AudioConnections.h +++ b/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/AudioConnections.h @@ -1,12 +1,12 @@ // Instantiate the audio classess -AudioInputI2S_F32 i2s_in(audio_settings); //Digital audio in *from* the Teensy Audio Board ADC. +AudioInputI2SQuad_F32 i2s_in(audio_settings); //Digital audio in *from* the Teensy Audio Board ADC. EarpieceMixer_F32_UI earpieceMixer(audio_settings); //mixes earpiece mics, allows switching to analog inputs, mixes left+right, etc AudioFilterBiquad_F32 hp_filt1(audio_settings); //IIR filter doing a highpass filter. Left. AudioFilterBiquad_F32 hp_filt2(audio_settings); //IIR filter doing a highpass filter. Right. AudioEffectCompWDRC_F32_UI comp1(audio_settings); //Compresses the dynamic range of the audio. Left. UI enabled!!! AudioEffectCompWDRC_F32_UI comp2(audio_settings); //Compresses the dynamic range of the audio. Right. UI enabled!!! -AudioOutputI2S_F32 i2s_out(audio_settings); //Digital audio out *to* the Teensy Audio Board DAC. +AudioOutputI2SQuad_F32 i2s_out(audio_settings); //Digital audio out *to* the Teensy Audio Board DAC. AudioSDWriter_F32_UI audioSDWriter(audio_settings);//this is stereo by default diff --git a/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/TrebleBoost_wComp_wEarpieces_wApp.ino b/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/TrebleBoost_wComp_wEarpieces_wApp.ino index 3a57fd7d..a760eb46 100644 --- a/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/TrebleBoost_wComp_wEarpieces_wApp.ino +++ b/examples/09-Earpieces/TrebleBoost_wComp_wEarpieces_wApp/TrebleBoost_wComp_wEarpieces_wApp.ino @@ -46,7 +46,7 @@ EarpieceShield earpieceShield(TympanRev::E, AICShieldRev::A); //in // Create classes for controlling the system #include "SerialManager.h" #include "State.h" -BLE ble(&Serial1); //create bluetooth BLE +BLE ble(&myTympan); //create bluetooth BLE SerialManager serialManager(&ble); //create the serial manager for real-time control (via USB or App) State myState(&audio_settings, &myTympan, &serialManager); //keeping one's state is useful for the App's GUI @@ -117,7 +117,7 @@ void setup() { Serial.println("TrebleBoost_wComp_wEarpieces_wApp: Starting setup()..."); //allocate the dynamic memory for audio processing blocks - AudioMemory_F32(20,audio_settings); + AudioMemory_F32(30,audio_settings); //Enable the Tympan to start the audio flowing! myTympan.enable(); // activate the AIC on the main Tympan board diff --git a/src/BLE/bc127.cpp b/src/BLE/bc127.cpp index 9210aa6a..ababeed8 100644 --- a/src/BLE/bc127.cpp +++ b/src/BLE/bc127.cpp @@ -136,7 +136,7 @@ BC127::opResult BC127::send(String str) if (BLE_id_num >= 14) { return stdCmd("SEND " + String(BLE_id_num) + " " + str); } else { - Serial.println("BC127: send: *** ERROR*** no BLE connection ID. Not sending."); + //Serial.println("BC127: send: *** WARNING *** no BLE connection ID. Not sending."); } return MODULE_ERROR; //not really a module error, but we should return an error } else { @@ -170,9 +170,15 @@ BC127::opResult BC127::reset() // Restores a device to factory settings // Returns: SUCCESS | MODULE_ERROR | TIMEOUT_ERROR -BC127::opResult BC127::restore() +BC127::opResult BC127::restore(bool printResponse) { - return stdCmd("RESTORE"); + BC127::opResult ret_val = stdCmd("RESTORE"); + if (printResponse) { + Serial.print("BC127: restore response: "); + Serial.print(getCmdResponse()); //this should be CR terminated + if (BC127_firmware_ver > 6) Serial.println(); + } + return ret_val; } // Writes the current configuration registers to non-volatile memory @@ -306,7 +312,7 @@ BC127::opResult BC127::recv(String *msg) // Convenience method to handle the raw internal stream // Returns: _serialPort -Stream *BC127::getSerial() +HardwareSerial *BC127::getSerial() { return _serialPort; } @@ -371,4 +377,26 @@ int BC127::set_BC127_firmware_ver(int val) { EOL = String("\r"); } return BC127_firmware_ver; +} + +//make this a static method so that it can get called without instantiating a BC127 or BLE +int BC127::factoryResetViaPins(int pinPIO0, int pinRST) { + if ((pinPIO0 < 0) || (pinRST < 0)) return -1; //FAIL! + + //This was all worked by by WEA Aug 25, 2021 + + pinMode(pinPIO0,OUTPUT); //prepare the PIO0 pin + digitalWrite(pinPIO0,HIGH); //normally low. Swtich high + + pinMode(pinRST,OUTPUT); //prepare the reset pin + digitalWrite(pinRST,LOW); //pull low to reset + delay(20); //hold low for at least 5 msec + digitalWrite(pinRST,HIGH); //pull high to start the boot + + //wait for boot to proceed far enough before changing anything + delay(400); //V7: works with 200 but not 175. V5: works with 350 but not 300 + digitalWrite(pinPIO0,LOW); //pull PIO0 back down low + pinMode(pinPIO0,INPUT); //go high-impedance to make irrelevant + + return 0; } \ No newline at end of file diff --git a/src/BLE/bc127.h b/src/BLE/bc127.h index 3fa24e42..d892f547 100644 --- a/src/BLE/bc127.h +++ b/src/BLE/bc127.h @@ -26,7 +26,7 @@ class BC127 SUCCESS // everything is good }; - BC127(Stream *sp, unsigned long timeout = 4000) : _serialPort(sp), _timeout(timeout) + BC127(HardwareSerial *sp, unsigned long timeout = 4000) : _serialPort(sp), _timeout(timeout) { _cmdResponse = String(""); }; @@ -45,12 +45,12 @@ class BC127 opResult exitDataMode(int guardDelay); // exits Data mode opResult getConfig(String config = ""); // returns all or specific config options String getCmdResponse(); // returns the command response string - Stream *getSerial(); // returns _serialPort + HardwareSerial *getSerial(); // returns _serialPort opResult help(); // prints the help opResult power(bool mode = true); // power cycles the device opResult recv(String *msg); // grab serial response, by line opResult reset(); // resets the device - opResult restore(); // restores the device to factory defaults + opResult restore(bool printResponse = false); // restores the device to factory defaults opResult send(String str); // sends a string over the connection profile opResult status(bool printResopnse = false); // returns the connections status opResult stdCmd(String cmd); // executes a standard command option @@ -60,19 +60,35 @@ class BC127 int set_BC127_firmware_ver(int val); // user sets whether firmware is version 5, 6, or 7 int get_BC127_firmware_ver(void) { return BC127_firmware_ver; } + void setPins(int pinPIO0, int pinRST) { pin_PIO0 = pinPIO0; pin_RST = pinRST; } + + int factoryResetViaPins(void) { return factoryResetViaPins(pin_PIO0, pin_RST) ; } + static int factoryResetViaPins(int pinPIO0, int pinRST); + protected: // end-of-line delimiter - const String EOC = String("\r"); + const String EOC = String('\r'); // end-of-command delimiter - String EOL = String("\r"); //this is changed by set_BC127_firmware_ver() + String EOL = String('\r'); //this is changed by set_BC127_firmware_ver() - Stream *_serialPort; // port to talk on + HardwareSerial *_serialPort; // port to talk on String _cmdResponse; // response from commands unsigned long _timeout; // timeout for command wait int BC127_firmware_ver = 7; //can be 5, 6, 7 int BLE_id_num=-1; //can be 14, 24, 34? - + + //To do a hardware reset of the module, we need to know the pin numbers from the Tympan/Teensy that go to + //certain pins on the BC127. You really should provide those pins through the setPins() method. But, to avoid + //catastrophe, I also do the hack below to try to use the correct values. This is cheating. + #ifdef KINETISK //this is set by the Arduino IDE when you choose Teensy 3.6...ie Tympan RevD + int pin_PIO0 = 56; //set to -1 to defeat unless set manually. //RevD = 56, RevE = 5 // Pin # for connection to BC127 PIO0 pin + int pin_RST = 34; //set to -1 to defeat unless set manually. //RevD = 34, RevE = 9 // Pin # for connection to BC127 RST pin + #else //otherwise, assume RevE + int pin_PIO0 = 5; //set to -1 to defeat unless set manually. //RevD = 56, RevE = 5 // Pin # for connection to BC127 PIO0 pin + int pin_RST = 9; //set to -1 to defeat unless set manually. //RevD = 34, RevE = 9 // Pin # for connection to BC127 RST pin + #endif + private: opResult knownStart(); // baseline starting function opResult waitResponse(int timeout = -1); // wait for a full response diff --git a/src/BLE/ble.cpp b/src/BLE/ble.cpp index 57dca678..4d90c65d 100644 --- a/src/BLE/ble.cpp +++ b/src/BLE/ble.cpp @@ -1,67 +1,190 @@ #include "ble.h" -int BLE::begin(void) +const int factory_baudrate = 9600; //here is one possible starting baudrate for the BLE module +const int faster_baudrate = 115200; //here is the other possible starting baudrate for the BLE module + + +int BLE::begin(int doFactoryReset) //0 is no, 1 = hardware reset { + int ret_val = 0; //clear the incoming serial buffer while (_serialPort->available()) _serialPort->read(); //clear the serial buffer associated with the BT unit - //force into command mode (not needed if already in command mode...but historical Tympan units were preloaded to Data mode instead) - //myTympan.forceBTtoDataMode(false); - _serialPort->print("$"); delay(400); _serialPort->print("$$$"); - //now do the usual setup stuff - int ret_val; - ret_val = restore(); - if (ret_val != BC127::SUCCESS) { - Serial.print(F("BLE: begin: restore() returned error ")); - Serial.println(ret_val); - //return ret_val; + //do a factory reset + if (doFactoryReset == 1) { + Serial.println("BLE: begin: doing factory reset via hardware pins."); + hardwareFactoryReset(); //this also automatically changes the serial baudrate of the Tympan itself + + } else { + //force into command mode (not needed if already in command mode...but historical Tympan units were preloaded to Data mode instead) + _serialPort->print("$"); delay(400); _serialPort->print("$$$"); } + //switch BC127 to listen to the serial link from the Tympan to a faster baud rate + if (useFasterBaudRateUponBegin) { + Serial.println("BLE: begin: setting BC127 baudrate to " + String(faster_baudrate) + "."); + switchToNewBaudRate(faster_baudrate); + } else { + Serial.println("BLE: begin: keeping baudrate at default."); + } + //enable BT_Classic connectable and discoverable, always if (BC127_firmware_ver >= 7) { ret_val = setConfig("BT_STATE_CONFIG", "1 1"); if (ret_val != BC127::SUCCESS) { - Serial.print(F("BLE: begin: set BT_STATE_CONFIG returned error ")); - Serial.println(ret_val); - //return ret_val; + Serial.println(F("BLE: begin: set BT_STATE_CONFIG returned error ") + String(ret_val)); + + //try a different baud rate + Serial.println("BLE: begin: switching baudrate to BT module to " + String(faster_baudrate)); + setSerialBaudRate(faster_baudrate); + delay(100); _serialPort->print(EOC); delay(100); echoBTreply(); + + //try BT_Classic again + ret_val = setConfig("BT_STATE_CONFIG", "1 1"); + if (ret_val != BC127::SUCCESS) { + Serial.println(F("BLE: begin: set BT_STATE_CONFIG returned error ") + String(ret_val)); + + //switch back to the factory baud rate + Serial.println("BLE: begin: switching baudrate to BT module back to " + String(factory_baudrate)); + setSerialBaudRate(factory_baudrate); + delay(100); _serialPort->print(EOC); delay(100); echoBTreply(); + } } } //write the new configuration so that it exists on startup (such as an unexpected restart of the module) ret_val = writeConfig(); if (ret_val != BC127::SUCCESS) { - Serial.print(F("BLE: begin: writeConfig() returned error ")); - Serial.println(ret_val); - //return ret_val; - } + Serial.println(F("BLE: begin: writeConfig() returned error ") + String(ret_val)); + + //try a different baud rate + Serial.println("BLE: begin: switching baudrate to BT module to " + String(faster_baudrate)); + setSerialBaudRate(faster_baudrate); + delay(100); _serialPort->print(EOC); delay(100); echoBTreply(); + + ret_val = writeConfig(); + if (ret_val != BC127::SUCCESS) { + Serial.println(F("BLE: begin: writeConfig() returned error ") + String(ret_val)); + + //switch back to the factory baud rate + Serial.println("BLE: begin: switching baudrate to BT module back to " + String(factory_baudrate)); + setSerialBaudRate(factory_baudrate); + delay(100); _serialPort->print(EOC); delay(100); echoBTreply(); + } + } + //reset ret_val = reset(); - if (ret_val != BC127::SUCCESS) { - Serial.print(F("BLE: begin: reset() returned error ")); - Serial.println(ret_val); - //return ret_val; - } + if (ret_val != BC127::SUCCESS) Serial.println(F("BLE: begin: reset() returned error ") + String(ret_val)); - return ret_val; } +void BLE::setSerialBaudRate(int new_baud) { + _serialPort->flush(); + _serialPort->end(); + _serialPort->begin(new_baud); + delay(100); +} + +int BLE::hardwareFactoryReset(bool printDebug) { + if (printDebug) Serial.println("BLE: hardwareFactoryReset: starting hardware-induced reset..."); + + //before the hardware reset, change the serial baudrate to the expected new value + setSerialBaudRate(factory_baudrate); + + //do the hardware reset + int ret_val = factoryResetViaPins(); + if (ret_val < 0) { //factoryResetViaPins is in BC127 + Serial.println("BLE: hardwareFactoryReset: *** ERROR ***: could not do factory reset."); + return -1; //error! + } + + //wait for the reset info to be issued by the module over the serial link + delay(2000); + echoBTreply(printDebug); + + //check communication ability + if (printDebug) { + Serial.println("BLE: hardwareFactoryReset: asking BC127 Status: "); + status(true); //print version info again. + } + + return 0; //OK! +} + +void BLE::echoBTreply(const bool printDebug) { + bool if_any_received = false; + while (_serialPort->available()) { + if_any_received = true; + char c = _serialPort->read(); //clear the character out of the incoming buffer + //echo the character to the USB serial? + if (printDebug) Serial.print(c); + } + if (printDebug && if_any_received) { if (BC127_firmware_ver >= 7) Serial.println(); } +} + +void BLE::switchToNewBaudRate(int new_baudrate) { + const bool printDebug = false; + + //Send the command to increase the baud rate. Takes effect immediately + //Because it takes effect immediately, you can't use the setConfig() function due to setConfig() + //trying to listen or a reply. While the BC127 will give a reply, its reply will be at the new + //baud rate, which will be the wrong baud rate for the Tympan until we swap the Tympan's baud rate + //to match. So, we need to manually send the command, then swap the Tympan's baud rate, then listen + //new replies. + if (BC127_firmware_ver >= 7) { + if (printDebug) Serial.println("BLE: switchToNewBaudRate: V7: changing BC127 to " + String(new_baudrate)); + _serialPort->print("SET UART_CONFIG=" + String(new_baudrate) + " OFF 0" + EOC); + } else { + if (printDebug) Serial.println("BLE: switchToNewBaudRate: V5: changing BC127 to " + String(new_baudrate)); + _serialPort->print("SET BAUD=" + String(new_baudrate) + EOC); + } + + //Switch the serial link to the BC127 to the faster baud rate + setSerialBaudRate(new_baudrate); + if (printDebug) Serial.println("BLE: switchToNewBaudRate: setting Serial link to BC127 to " + String(new_baudrate)); + + + //give time for any replies from the module + delay(500); //500 seems to work on V5 + if (printDebug) Serial.println("BLE: switchToNewBaudRate: Reply from BC127 (if any)..."); + echoBTreply(printDebug); + + //clear the serial link by sending a CR...will return an error + if (printDebug) Serial.println("BLE: switchToNewBaudRate: Confirming asking status"); + _serialPort->print("STATUS" + EOC); + delay(100); + echoBTreply(printDebug); //should cause response of "ERROR" +} + void BLE::setupBLE(int BT_firmware, bool printDebug) { + int doFactoryReset = 1; + setupBLE(BT_firmware, printDebug, doFactoryReset); +} + +void BLE::setupBLE_noFactoryReset(int BT_firmware, bool printDebug) +{ + int doFactoryReset = 0; + setupBLE(BT_firmware, printDebug, doFactoryReset); +} +void BLE::setupBLE(int BT_firmware, bool printDebug, int doFactoryReset) +{ int ret_val; ret_val = set_BC127_firmware_ver(BT_firmware); if (ret_val != BT_firmware) { Serial.println("BLE: setupBLE: *** WARNING ***: given BT_firmware (" + String(BT_firmware) + ") not allowed."); Serial.println(" : assuming firmware " + String(ret_val) + " instead. Continuing..."); } - ret_val = begin(); //via BC127.h, success is a value of 1 + + ret_val = begin(doFactoryReset); //via BC127.h, success is a value of 1 if (ret_val != 1) { - Serial.print("BLE: setupBLE: ble did not begin correctly. error = "); - Serial.println(ret_val); + Serial.println("BLE: setupBLE: ble did not begin correctly. error = " + String(ret_val)); Serial.println(" : -1 = TIMEOUT ERROR"); Serial.println(" : 0 = GENERIC MODULE ERROR"); } @@ -72,9 +195,9 @@ void BLE::setupBLE(int BT_firmware, bool printDebug) //print version information...this is for debugging only if (printDebug) Serial.println("BLE: setupBLE: assuming BC127 firmware: " + String(BC127_firmware_ver) + ", Actual is:"); version(printDebug); - } + size_t BLE::sendByte(char c) { //Serial.print("BLE: sendBytle: "); Serial.println(c); @@ -131,7 +254,14 @@ size_t BLE::sendMessage(const String &orig_s) sprintf(buf, "%02X %02X %02X %02X %02X %02X %02X", header.charAt(0), header.charAt(1), header.charAt(2), header.charAt(3), header.charAt(4), header.charAt(5), header.charAt(6)); //Serial.println(buf); int a = sendString(header); - if (a != 7) Serial.println("BLE: sendMessage: Error in sending header... Sent: '" + String(a) + "'"); + if (a != 7) { + //only print if V5 or, if it is V7, if there is a valid connection (noted by a valid BLE_id_num) + if ((BC127_firmware_ver < 6) || (BLE_id_num > 0)) { + Serial.println("BLE: sendMessage: Error in sending header... Sent: '" + String(a) + "'"); + } + //if we really do get an error, should we really try to transmit all the packets below? Seems like we shouldn't. + } + //break up String into packets int numPackets = ceil(s.length() / (float)payloadLen); diff --git a/src/BLE/ble.h b/src/BLE/ble.h index af34b5b0..7ae584f1 100644 --- a/src/BLE/ble.h +++ b/src/BLE/ble.h @@ -10,10 +10,13 @@ class BLE : public BC127 { public: - BLE(Stream *sp) : BC127(sp) {} - int begin(void); - void setupBLE(int BT_firmware = 7, bool printDebug = true); //to be called from the Arduino sketch's setup() routine. Includes error reporting to Serial - size_t sendByte(char c); + BLE(HardwareSerial *sp) : BC127(sp) {} + BLE(TympanBase *tympan) : BC127(tympan->BT_Serial) { setPins(tympan->getPin_BT_PIO0(),tympan->getPin_BT_RST()); }; + int begin(int doFactoryReset = 1); + void setupBLE(int BT_firmware = 7, bool printDebug = true); //to be called from the Arduino sketch's setup() routine. Includes factory reset. + void setupBLE_noFactoryReset(int BT_firmware = 7, bool printDebug = true); //to be called from the Arduino sketch's setup() routine. Excludes factory reset. + void setupBLE(int BT_firmware, bool printDebug, int doFactoryReset); //to be called from the Arduino sketch's setup() routine. Must define all params + size_t sendByte(char c); size_t sendString(const String &s); size_t sendMessage(const String &s); //size_t sendMessage(const char* c_str, const int len); //use this if you need to send super long strings (more than 1797 characters) @@ -24,12 +27,21 @@ class BLE : public BC127 bool isConnected(bool printResponse = false); bool waitConnect(int time = -1); void updateAdvertising(unsigned long curTime_millis, unsigned long updatePeriod_millis = 5000, bool printDebugMsgs=false); + + void echoBTreply(bool printDebug = false); + bool setUseFasterBaudRateUponBegin(bool enable = true) { return useFasterBaudRateUponBegin = enable; } +protected: + bool useFasterBaudRateUponBegin = false; //default as to whether to use faster baud rate or not + void setSerialBaudRate(int new_baud); + int hardwareFactoryReset(bool printDebug = false); + void switchToNewBaudRate(int new_baudrate); }; class BLE_UI : public BLE, public SerialManager_UI { public: + BLE_UI(TympanBase *tympan) : BLE(tympan), SerialManager_UI() {} BLE_UI(HardwareSerial *sp) : BLE(sp), SerialManager_UI() {} // ///////// here are the methods that you must implement from SerialManager_UI diff --git a/src/EarpieceMixer_F32.cpp b/src/EarpieceMixer_F32.cpp index a0d44cc9..b47dd26d 100644 --- a/src/EarpieceMixer_F32.cpp +++ b/src/EarpieceMixer_F32.cpp @@ -5,8 +5,11 @@ void EarpieceMixerBase_F32::update(void) { //allocate the input and output audio memory...if successfull, we must remember to release all of these audio blocks - audio_block_f32_t *audio_in[4], *tmp[6], *audio_out[2]; - if (allocateAndGetAudioBlocks(audio_in, tmp, audio_out) == false) return; //If it fails, we stop and return. If success, we continue. + audio_block_f32_t *audio_in[4], *tmp[8], *audio_out[2]; + if (allocateAndGetAudioBlocks(audio_in, tmp, audio_out) == false) { + //Serial.println("EarpieceMixerBase_F32: update: failed to allocated audio blocks!"); + return; //If it fails, we stop and return. If success, we continue. + } //do the work processData(audio_in, tmp, audio_out); @@ -14,8 +17,8 @@ void EarpieceMixerBase_F32::update(void) { //transmit the output AudioStream_F32::transmit(audio_out[0], 0); AudioStream_F32::transmit(audio_out[1], 1); - //release all the memory - for (int i = 0; i < 6; i++) AudioStream_F32::release(tmp[i]); + //release all the memory that was allocated in allocateAndGetAudioBlocks() + for (int i = 0; i < 8; i++) AudioStream_F32::release(tmp[i]); for (int i = 0; i < 2; i++) AudioStream_F32::release(audio_out[i]); for (int i = 0; i < 4; i++) AudioStream_F32::release(audio_in[i]); @@ -23,9 +26,12 @@ void EarpieceMixerBase_F32::update(void) { } //the audio_in must be writable and the audio-out must also be writable -void EarpieceMixerBase_F32::processData(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[6], audio_block_f32_t *audio_out[2]) { - audio_block_f32_t *tmp_out_left = tmp[4]; - audio_block_f32_t *tmp_out_right = tmp[5]; +void EarpieceMixerBase_F32::processData(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[8], audio_block_f32_t *audio_out[2]) { + //tmp[0]-tmp[3] are used to temporarily hold the delayed versions of the four mic signals + audio_block_f32_t *pdm_mix_left = tmp[4]; + audio_block_f32_t *pdm_mix_right = tmp[5]; + audio_block_f32_t *tmp_out_left = tmp[6]; + audio_block_f32_t *tmp_out_right = tmp[7]; audio_block_f32_t *tmp_input[4]; //each mixer assumes that it's getting a 4-element array of audio pointers //apply delay to each mic channel @@ -45,9 +51,9 @@ void EarpieceMixerBase_F32::processData(audio_block_f32_t *audio_in[4], audio_bl //AudioConnection_F32 patchcord9(rearMicDelayR, 0, frontRearMixer[RIGHT], REAR); for (int i=0; i<4; i++) tmp_input[i]=NULL; //clear the temporary inputs tmp_input[FRONT] = tmp[PDM_LEFT_FRONT]; tmp_input[REAR] = tmp[PDM_LEFT_REAR]; //put the left-earpiece audio into the temp array - frontRearMixer[LEFT].processData(tmp_input, tmp_out_left); //process to make a mix from the left earpiece + frontRearMixer[LEFT].processData(tmp_input, pdm_mix_left); //process to make a mix from the left earpiece tmp_input[FRONT] = tmp[PDM_RIGHT_FRONT]; tmp_input[REAR] = tmp[PDM_RIGHT_REAR]; //put the right-earpiece audio into the temp array - frontRearMixer[RIGHT].processData(tmp_input, tmp_out_right); //process to make a mix from the right earpiece + frontRearMixer[RIGHT].processData(tmp_input, pdm_mix_right); //process to make a mix from the right earpiece //switch between digital inputs or analog inputs //AudioConnection_F32 patchcord11(i2s_in, LEFT, analogVsDigitalSwitch[LEFT], ANALOG_IN); @@ -55,9 +61,9 @@ void EarpieceMixerBase_F32::processData(audio_block_f32_t *audio_in[4], audio_bl //AudioConnection_F32 patchcord13(i2s_in, RIGHT, analogVsDigitalSwitch[RIGHT], ANALOG_IN); //AudioConnection_F32 patchcord14(frontRearMixer[RIGHT], 0, analogVsDigitalSwitch[RIGHT], PDM_IN); for (int i=0; i<4; i++) tmp_input[i]=NULL; //clear the temporary inputs - tmp_input[INPUT_ANALOG] = audio_in[LEFT]; tmp_input[INPUT_PDM] = tmp_out_left; + tmp_input[INPUT_ANALOG] = audio_in[LEFT]; tmp_input[INPUT_PDM] = pdm_mix_left; analogVsDigitalSwitch[LEFT].processData(tmp_input, tmp_out_left); //will overrite the tmp_out_left (which is also part of the tmp_input array) - tmp_input[INPUT_ANALOG] = audio_in[RIGHT]; tmp_input[INPUT_PDM] = tmp_out_right; + tmp_input[INPUT_ANALOG] = audio_in[RIGHT]; tmp_input[INPUT_PDM] = pdm_mix_right; analogVsDigitalSwitch[RIGHT].processData(tmp_input, tmp_out_right); //will overrite the tmp_out_right (which is also part of the tmp_input array) //do the left-right mixing @@ -71,7 +77,7 @@ void EarpieceMixerBase_F32::processData(audio_block_f32_t *audio_in[4], audio_bl leftRightMixer[RIGHT].processData(tmp_input, audio_out[RIGHT]); //process to make a mix for the left output }; -bool EarpieceMixerBase_F32::allocateAndGetAudioBlocks(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[6], audio_block_f32_t *audio_out[2]) { +bool EarpieceMixerBase_F32::allocateAndGetAudioBlocks(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[8], audio_block_f32_t *audio_out[2]) { bool any_data_present = false; if ((audio_out[0] = allocate_f32()) == NULL) return false; @@ -108,13 +114,13 @@ bool EarpieceMixerBase_F32::allocateAndGetAudioBlocks(audio_block_f32_t *audio_i //allocate a bunch of working memory bool any_allocate_fail = false; - for (int i = 0; i < 6; i++) { + for (int i = 0; i < 8; i++) { tmp[i] = allocate_f32(); if (tmp[i] == NULL) any_allocate_fail = true; } if (any_allocate_fail) { //we must be out of audio memory...so release any buffers that were allocated and then return - for (int i = 0; i < 6; i++) { if (tmp[i]) AudioStream_F32::release(tmp[i]); } + for (int i = 0; i < 8; i++) { if (tmp[i]) AudioStream_F32::release(tmp[i]); } for (int Ichan = 0; Ichan < 4; Ichan++) {if (audio_in[Ichan]) AudioStream_F32::release(audio_in[Ichan]);} for (int Ichan = 0; Ichan < 2; Ichan++) {if (audio_out[Ichan]) AudioStream_F32::release(audio_out[Ichan]);} return false; @@ -161,7 +167,7 @@ int EarpieceMixer_F32::setInputAnalogVsPDM(int input) { switch (input) { case EarpieceMixerState::INPUT_PDM: - myTympan->enableDigitalMicInputs(true); //two of the earpiece digital mics are routed here + myTympan->enableDigitalMicInputs(true); //two of the earpiece digital mics are routed here earpieceShield->enableDigitalMicInputs(true); //the other two of the earpiece digital mics are routed here analogVsDigitalSwitch[LEFT].switchChannel(input); analogVsDigitalSwitch[RIGHT].switchChannel(input); diff --git a/src/EarpieceMixer_F32.h b/src/EarpieceMixer_F32.h index 52a5332d..d478af8f 100644 --- a/src/EarpieceMixer_F32.h +++ b/src/EarpieceMixer_F32.h @@ -76,7 +76,7 @@ class EarpieceMixerBase_F32 : public AudioStream_F32 { //primary processing methods virtual void update(void); - virtual void processData(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[6], audio_block_f32_t *audio_out[2]); + virtual void processData(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[8], audio_block_f32_t *audio_out[2]); //Audio classes used here AudioEffectDelay_F32 frontMicDelay[2]; //delays front microphone (left and right) @@ -98,7 +98,7 @@ class EarpieceMixerBase_F32 : public AudioStream_F32 { audio_block_f32_t *inputQueueArray_f32[4]; //memory pointer for the input to this module //supporting methods - bool allocateAndGetAudioBlocks(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[6], audio_block_f32_t *audio_out[2]); + bool allocateAndGetAudioBlocks(audio_block_f32_t *audio_in[4], audio_block_f32_t *tmp[8], audio_block_f32_t *audio_out[2]); }; diff --git a/src/EarpieceMixer_F32_UI.cpp b/src/EarpieceMixer_F32_UI.cpp index a6d5fa8b..07d81a06 100644 --- a/src/EarpieceMixer_F32_UI.cpp +++ b/src/EarpieceMixer_F32_UI.cpp @@ -24,51 +24,51 @@ bool EarpieceMixer_F32_UI::processCharacterTriple(char mode_char, char chan_char return_val = true; switch (c) { case 'w': - Serial.println("Received: Listen to PCB mics"); + Serial.println("EarpieceMixer_F32_UI: Received: Listen to PCB mics"); setInputAnalogVsPDM(EarpieceMixerState::INPUT_ANALOG); setAnalogInputSource(EarpieceMixerState::INPUT_PCBMICS); setInputConfigButtons(); setButtonState_frontRearMixer(); break; case 'W': - Serial.println("Received: Mic jack as mic"); + Serial.println("EarpieceMixer_F32_UI: Received: Mic jack as mic"); setInputAnalogVsPDM(EarpieceMixerState::INPUT_ANALOG); setAnalogInputSource(EarpieceMixerState::INPUT_MICJACK_MIC); setInputConfigButtons(); setButtonState_frontRearMixer(); break; case 'e': - Serial.println("Received: Mic jack as line-in"); + Serial.println("EarpieceMixer_F32_UI: Received: Mic jack as line-in"); setInputAnalogVsPDM(EarpieceMixerState::INPUT_ANALOG); setAnalogInputSource(EarpieceMixerState::INPUT_MICJACK_LINEIN); setInputConfigButtons(); setButtonState_frontRearMixer(); break; case 'E': - Serial.println("Received: Switch to BT Audio"); + Serial.println("EarpieceMixer_F32_UI: Received: Switch to BT Audio"); setInputAnalogVsPDM(EarpieceMixerState::INPUT_ANALOG); setAnalogInputSource(EarpieceMixerState::INPUT_LINEIN_SE); setInputConfigButtons(); setButtonState_frontRearMixer(); break; case 'd': - Serial.println("Received: Listen to PDM mics"); //digital mics + Serial.println("EarpieceMixer_F32_UI: Received: Listen to PDM mics"); //digital mics setInputAnalogVsPDM(EarpieceMixerState::INPUT_PDM); setInputConfigButtons(); setButtonState_frontRearMixer(); break; case 't': - Serial.println("Received: Front Mics (if using earpieces)"); + Serial.println("EarpieceMixer_F32_UI: Received: Front Mics (if using earpieces)"); configureFrontRearMixer(EarpieceMixerState::MIC_FRONT); setButtonState_frontRearMixer(); break; case 'T': - Serial.println("Received: Mix of Front Mics + Inverted Rear Mics"); + Serial.println("EarpieceMixer_F32_UI: Received: Mix of Front Mics + Inverted Rear Mics"); configureFrontRearMixer(EarpieceMixerState::MIC_BOTH_INVERTED); setButtonState_frontRearMixer(); break; case 'H': - Serial.println("Received: Rear Mics (if using earpieces)"); + Serial.println("EarpieceMixer_F32_UI: Received: Rear Mics (if using earpieces)"); configureFrontRearMixer(EarpieceMixerState::MIC_REAR); setButtonState_frontRearMixer(); break; @@ -112,7 +112,7 @@ bool EarpieceMixer_F32_UI::processCharacterTriple(char mode_char, char chan_char break; case 'q': configureLeftRightMixer(EarpieceMixerState::INPUTMIX_MUTE); - Serial.println("Received: Muting audio."); + Serial.println("EarpieceMixer_F32_UI: Received: Muting audio."); setButtonState_inputMixer(); break; case 'Q': @@ -120,18 +120,18 @@ bool EarpieceMixer_F32_UI::processCharacterTriple(char mode_char, char chan_char setButtonState_inputMixer(); break; case 'B': - Serial.println("Received: Input: Mix L+R."); + Serial.println("EarpieceMixer_F32_UI: Received: Input: Mix L+R."); configureLeftRightMixer(EarpieceMixerState::INPUTMIX_MONO); setButtonState_inputMixer(); break; case 's': configureLeftRightMixer(EarpieceMixerState::INPUTMIX_BOTHLEFT); - Serial.println("Received: Input: Both Left."); + Serial.println("EarpieceMixer_F32_UI: Received: Input: Both Left."); setButtonState_inputMixer(); break; case 'S': configureLeftRightMixer(EarpieceMixerState::INPUTMIX_BOTHRIGHT); - Serial.println("Received: Input: Both Right."); + Serial.println("EarpieceMixer_F32_UI: Received: Input: Both Right."); setButtonState_inputMixer(); break; default: diff --git a/src/Tympan.cpp b/src/Tympan.cpp index c2a4a1ab..ef6f0b52 100644 --- a/src/Tympan.cpp +++ b/src/Tympan.cpp @@ -3,7 +3,36 @@ const int TympanBase::BT_uint8_buff_len; +int TympanBase::testTympanRev(TympanRev tympanRev) { + + #ifdef __IMXRT1062__ //check the processor flag given to the compiler (this is compiled for RevE) + //Serial.println("testTympanRev: compiled for RevE, given TympanRev is " + String(static_cast(tympanRev))); + if (static_cast(tympanRev) < static_cast(TympanRev::E)) { + Serial.println("TympanBase: testTympanRev: *** WARNING ***: You specified the wrong Tympan revision?"); + Serial.println(" : This code was compiled for Tympan Rev E"); + Serial.println(" : Yet your code said that it was for Tympan Rev C or D."); + Serial.println(" : This is unlikely to work. In your code, change to 'TympanRev::E'."); + return -1; + } + #endif + #ifdef __MK66FX1M0__ //check the processor flag given to the compiler (this is compiled for RevA - RevD) + //Serial.println("testTympanRev: compiled for RevD, given TympanRev is " + String(static_cast(tympanRev))); + if (static_cast(tympanRev) >= static_cast(TympanRev::E)) { + Serial.println("TympanBase: testTympanRev: *** WARNING ***: You specified the wrong Tympan revision?"); + Serial.println(" : TThis code was compiled for Tympan Rev D (or C)"); + Serial.println(" : Yet your code said that it was for Tympan Rev E."); + Serial.println(" : This is unlikely to work. In your code, change to 'TympanRev::D' (or C)"); + return -1; + } + #endif + + return 0; //OK! +} + + void TympanBase::setupPins(const TympanPins &_pins) { + testTympanRev(_pins.tympanRev); + AudioControlAIC3206::setResetPin(_pins.resetAIC); pins = _pins; //shallow copy to local version BT_mode = pins.default_BT_mode; @@ -60,16 +89,17 @@ void TympanBase::setupPins(const TympanPins &_pins) { //or leave it high...assuming high is normal } } + if (pins.BT_PIO0 != NOT_A_FEATURE) { + pinMode(pins.BT_PIO0,INPUT); //high impedance. If this switched to low impedance and is held high when the reset pin (below) is toggled, it forces a hardware reset on BC127. + } if (pins.BT_nReset != NOT_A_FEATURE) { //For RN51 and BC127 modules. (RevC, RevD, RevE) pinMode(pins.BT_nReset,OUTPUT); digitalWrite(pins.BT_nReset,LOW);delay(50); //reset the device (RevC used only 10 here, not 50. Is 50 OK for RevC?) digitalWrite(pins.BT_nReset,HIGH); //normal operation. } - if (pins.BT_PIO0 != NOT_A_FEATURE) { - pinMode(pins.BT_PIO0,INPUT); - } - forceBTtoDataMode(true); + + forceBTtoDataMode(true); //I don't think that we want this anymore? (Chip, Aug 26, 2021) }; @@ -105,8 +135,7 @@ int TympanBase::toggleLEDs(const bool useAmber, const bool useRed) { } if (!useAmber) setAmberLED(false); if (!useRed) setRedLED(false); - - return LED; + return (int)LED; } diff --git a/src/Tympan.h b/src/Tympan.h index cae984f9..42ea2da8 100644 --- a/src/Tympan.h +++ b/src/Tympan.h @@ -240,6 +240,7 @@ class TympanBase : public AudioControlAIC3206, public Print setAudioSettings(_as); } + int testTympanRev(TympanRev tympanRev); void setupPins(const TympanPins &_pins); void setAudioSettings(const AudioSettings_F32 &_aud_set) { audio_settings = _aud_set; } //shallow copy void forceBTtoDataMode(bool state); @@ -274,6 +275,9 @@ class TympanBase : public AudioControlAIC3206, public Print return digitalRead(pins.BT_PIO0); } } + int getPin_BT_PIO0(void) { return pins.BT_PIO0; } + int getPin_BT_RST(void) { return pins.BT_nReset; } + //#if defined(SEREMU_INTERFACE) // usb_seremu_class *getUSBSerial(void) { return USB_Serial; } //#else diff --git a/src/control_aic3206.cpp b/src/control_aic3206.cpp index 2cf420e5..6d52f747 100644 --- a/src/control_aic3206.cpp +++ b/src/control_aic3206.cpp @@ -356,10 +356,10 @@ bool AudioControlAIC3206::enableDigitalMicInputs(bool desired_state) { return true; } else { - //change the AIC's pin "MFP4" to clock input for digital microphone + //change the AIC's pin "MFP4" aic_writePage(0,55,0b00000010); //page 0, register 55, set to "disabled" ??? is this the default state? - //change the IAC's pin "MFP3" to Digital Microphone input + //change the AIC's pin "MFP3" aic_writePage(0,56,0b00000010); //page 0, register 56, set to "disabled" ??? is this the default state? //change the ADC to NOT use the digital mic