Skip to content

Commit

Permalink
Merge pull request #60 from Tympan/release_candidate1
Browse files Browse the repository at this point in the history
During Startup, Now Resets BC127 Bluetooth Unit to Factory Defaults
  • Loading branch information
chipaudette authored Sep 27, 2021
2 parents 26e2e9c + cc5cb17 commit 28c6442
Show file tree
Hide file tree
Showing 25 changed files with 527 additions and 99 deletions.
2 changes: 1 addition & 1 deletion examples/02-Utility/SDWriter_wApp/SDWriter_wApp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/02-Utility/SoundLevelMeter/SoundLevelMeter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion examples/06-AppTutorial/BasicGain_wApp/BasicGain_wApp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/07-Bluetooth/BLELibraryTest/BLELibraryTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void loop()

void BLETest()
{
BLE ble = BLE(&Serial1);
BLE ble(&myTympan);

if (ble.isConnected())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
203 changes: 203 additions & 0 deletions examples/07-Bluetooth/ChangeBaudrate/ChangeBaudrate.ino
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#include <Tympan_Library.h>

// 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 28c6442

Please sign in to comment.