bugfixes for nRF examples

This commit is contained in:
morgan 2021-04-19 13:20:54 -04:00
parent 1500d07213
commit 888697bb79
3 changed files with 849 additions and 848 deletions

View File

@ -1,282 +1,283 @@
/* Hamshield /* Hamshield
* Example: HandyTalkie_nRF52840 * Example: HandyTalkie_nRF52840
* This is a simple example to demonstrate the HamShield working * This is a simple example to demonstrate the HamShield working
* with an Adafruit Feather nRF52840 Express * with an Adafruit Feather nRF52840 Express
* *
* HamShield to Feather Connections: * HamShield to Feather Connections:
* SPKR - Feather A0 * SPKR - Feather A0
* MIC - Feather D11 * MIC - Feather D11
* CLK - Feather D5 * CLK - Feather D5
* nCS - Feather D6 * nCS - Feather D6
* DAT - Feather D9 * DAT - Feather D9
* GND - Feather GND * GND - Feather GND
* VCC - Feather 3.3V * VCC - Feather 3.3V
* *
* Connect the HamShield to your Feather as above. * Connect the HamShield to your Feather as above.
* Screw the antenna into the HamShield RF jack. Plug a pair * Screw the antenna into the HamShield RF jack. Plug a pair
* of headphones into the HamShield. * of headphones into the HamShield.
* *
* Connect the Feather nRF52840 Express to your computer via * Connect the Feather nRF52840 Express to your computer via
* a USB Micro B cable. After uploading this program to * a USB Micro B cable. After uploading this program to
* your Feather, open the Serial Monitor. You should see some * your Feather, open the Serial Monitor. You should see some
* text displayed that documents the setup process. * text displayed that documents the setup process.
* *
* Once the Feather is set up and talking to the HamShield, * Once the Feather is set up and talking to the HamShield,
* you can control it over USB-Serial or BLE-Serial(UART). * you can control it over USB-Serial or BLE-Serial(UART).
* *
* Try using Adafruit's Bluefruit app to connect to the Feather. * Try using Adafruit's Bluefruit app to connect to the Feather.
* Once you're connected, you can control the HamShield using * Once you're connected, you can control the HamShield using
* the same commands you'd use over USB-Serial. The response to * the same commands you'd use over USB-Serial. The response to
* all commands will be echoed to both USB-Serial and BLE-Serial(UART). * all commands will be echoed to both USB-Serial and BLE-Serial(UART).
* *
* Serial UART commands: * Serial UART commands:
* t - change from Tx to Rx (or vice versa) * t - change from Tx to Rx (or vice versa)
* F123400 - set frequency to 123400 kHz * F123400 - set frequency to 123400 kHz
*/ */
#include <bluefruit.h> #include <bluefruit.h>
// BLE Service // BLE Service
BLEDis bledis; // device information BLEDis bledis; // device information
BLEUart bleuart; // uart over ble BLEUart bleuart; // uart over ble
BLEBas blebas; // battery BLEBas blebas; // battery
#include <HamShield.h> #include <HamShield.h>
// create object for radio // create object for radio
HamShield radio(9,5,6); HamShield radio(6,5,9);
// To use non-standard pins, use the following initialization // To use non-standard pins, use the following initialization
//HamShield radio(ncs_pin, clk_pin, dat_pin); //HamShield radio(ncs_pin, clk_pin, dat_pin);
#define LED_PIN 3 #define LED_PIN 3
#define RSSI_REPORT_RATE_MS 5000 #define RSSI_REPORT_RATE_MS 5000
#define MIC_PIN A1 #define MIC_PIN A1
bool blinkState = false; bool blinkState = false;
bool currently_tx; bool currently_tx;
uint32_t freq; uint32_t freq;
unsigned long rssi_timeout; unsigned long rssi_timeout;
void setup() { void setup() {
// NOTE: if not using PWM out, it should be held low to avoid tx noise // NOTE: if not using PWM out, it should be held low to avoid tx noise
pinMode(MIC_PIN, OUTPUT); pinMode(MIC_PIN, OUTPUT);
digitalWrite(MIC_PIN, LOW); digitalWrite(MIC_PIN, LOW);
// initialize serial communication // initialize serial communication
Serial.begin(115200); Serial.begin(115200);
while (!Serial) delay(10); while (!Serial) delay(10);
Serial.println("Setting up BLE"); Serial.println("Setting up BLE");
// Setup the BLE LED to be enabled on CONNECT // Setup the BLE LED to be enabled on CONNECT
// Note: This is actually the default behaviour, but provided // Note: This is actually the default behaviour, but provided
// here in case you want to control this LED manually via PIN 19 // here in case you want to control this LED manually via PIN 19
Bluefruit.autoConnLed(true); Bluefruit.autoConnLed(true);
// Config the peripheral connection with maximum bandwidth // Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice // more SRAM required by SoftDevice
// Note: All config***() function must be called before begin() // Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin(); Bluefruit.begin();
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4); Bluefruit.setTxPower(4);
Bluefruit.setName("MyBlueHam"); Bluefruit.setName("MyBlueHam");
//Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
Bluefruit.setConnectCallback(connect_callback); Bluefruit.setConnectCallback(connect_callback);
Bluefruit.setDisconnectCallback(disconnect_callback); Bluefruit.setDisconnectCallback(disconnect_callback);
// Configure and Start Device Information Service // Configure and Start Device Information Service
bledis.setManufacturer("Enhanced Radio Devices"); bledis.setManufacturer("Enhanced Radio Devices");
bledis.setModel("BlueHam"); bledis.setModel("BlueHam");
bledis.begin(); bledis.begin();
// Configure and Start BLE Uart Service // Configure and Start BLE Uart Service
bleuart.begin(); bleuart.begin();
// Start BLE Battery Service // Start BLE Battery Service
blebas.begin(); blebas.begin();
blebas.write(100); blebas.write(100);
// Set up and start advertising // Set up and start advertising
startAdv(); startAdv();
delay(100); delay(100);
Serial.println("beginning Ham radio setup"); Serial.println("beginning Ham radio setup");
// verify connection // verify connection
Serial.println("Testing device connections..."); Serial.println("Testing device connections...");
if (radio.testConnection()) { if (radio.testConnection()) {
Serial.println("HamShield connection successful"); Serial.println("HamShield connection successful");
} else { } else {
Serial.print("HamShield connection failed"); Serial.print("HamShield connection failed");
while(1) delay(100); while(1) delay(100);
} }
// initialize device // initialize device
Serial.println("Initializing radio device..."); Serial.println("Initializing radio device...");
radio.initialize(); // initializes automatically for UHF 12.5kHz channel radio.initialize(); // initializes automatically for UHF 12.5kHz channel
Serial.println("setting default Radio configuration"); Serial.println("setting default Radio configuration");
// set frequency // set frequency
Serial.println("changing frequency"); Serial.println("changing frequency");
radio.setSQOff(); radio.setSQOff();
freq = 432100; // 70cm calling frequency freq = 432100; // 70cm calling frequency
radio.frequency(freq); radio.frequency(freq);
// set to receive // set to receive
radio.setModeReceive(); radio.setModeReceive();
currently_tx = false; currently_tx = false;
Serial.print("config register is: "); Serial.print("config register is: ");
Serial.println(radio.readCtlReg()); Serial.println(radio.readCtlReg());
Serial.println(radio.readRSSI()); Serial.println(radio.readRSSI());
/* /*
// set to transmit // set to transmit
radio.setModeTransmit(); radio.setModeTransmit();
// maybe set PA bias voltage // maybe set PA bias voltage
Serial.println("configured for transmit"); Serial.println("configured for transmit");
radio.setTxSourceMic(); radio.setTxSourceMic();
*/ */
radio.setRfPower(0); radio.setRfPower(0);
// configure Arduino LED for // configure Arduino LED for
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH); digitalWrite(LED_PIN, HIGH);
rssi_timeout = 0; rssi_timeout = 0;
} }
void startAdv(void) void startAdv(void)
{ {
// Advertising packet // Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower(); Bluefruit.Advertising.addTxPower();
// Include bleuart 128-bit uuid // Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart); Bluefruit.Advertising.addService(bleuart);
// Secondary Scan Response packet (optional) // Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet // Since there is no room for 'Name' in Advertising packet
Bluefruit.ScanResponse.addName(); Bluefruit.ScanResponse.addName();
/* Start Advertising /* Start Advertising
* - Enable auto advertising if disconnected * - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms * - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds * - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected) * - Start(timeout) with timeout = 0 will advertise forever (until connected)
* *
* For recommended advertising interval * For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html * https://developer.apple.com/library/content/qa/qa1931/_index.html
*/ */
Bluefruit.Advertising.restartOnDisconnect(true); Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
} }
#define TEXT_BUF_LEN 64 // for serial output buffer on both interfaces
char text_buf[TEXT_BUF_LEN]; #define TEXT_BUF_LEN 64
void loop() { char text_buf[TEXT_BUF_LEN];
void loop() {
char c = 0;
bool ble_serial = false; char c = 0;
if (Serial.available()) { bool ble_serial = false;
Serial.readBytes(&c, 1); if (Serial.available()) {
} else if (bleuart.available()) { Serial.readBytes(&c, 1);
c = (char) bleuart.read(); } else if (bleuart.available()) {
ble_serial = true; c = (char) bleuart.read();
} ble_serial = true;
}
if (c != 0) {
if (c == 't') if (c != 0) {
{ if (c == 't')
if (!currently_tx) {
{ if (!currently_tx)
currently_tx = true; {
currently_tx = true;
// set to transmit
radio.setModeTransmit(); // set to transmit
radio.setModeTransmit();
Serial.println("Tx");
int str_len = snprintf(text_buf, TEXT_BUF_LEN, "Tx\n"); Serial.println("Tx");
bleuart.write(text_buf, str_len); int str_len = snprintf(text_buf, TEXT_BUF_LEN, "Tx\n");
//radio.setTxSourceMic(); bleuart.write(text_buf, str_len);
//radio.setRfPower(1); //radio.setTxSourceMic();
} else { //radio.setRfPower(1);
radio.setModeReceive(); } else {
currently_tx = false; radio.setModeReceive();
Serial.println("Rx"); currently_tx = false;
int str_len = snprintf(text_buf, TEXT_BUF_LEN, "Rx\n"); Serial.println("Rx");
bleuart.write(text_buf, str_len); int str_len = snprintf(text_buf, TEXT_BUF_LEN, "Rx\n");
} bleuart.write(text_buf, str_len);
} else if (c == 'F') { }
if (ble_serial == false) { } else if (c == 'F') {
Serial.setTimeout(40); if (ble_serial == false) {
freq = Serial.parseInt(); Serial.setTimeout(40);
Serial.flush(); freq = Serial.parseInt();
} else { Serial.flush();
int idx = 0; } else {
while (bleuart.available() && int idx = 0;
bleuart.peek() >= '0' && while (bleuart.available() &&
bleuart.peek() <= '9' && bleuart.peek() >= '0' &&
idx < TEXT_BUF_LEN) { bleuart.peek() <= '9' &&
idx < TEXT_BUF_LEN) {
text_buf[idx] = bleuart.read();
idx++; text_buf[idx] = bleuart.read();
} idx++;
text_buf[idx] = 0; // null terminate }
freq = atoi(text_buf); text_buf[idx] = 0; // null terminate
} freq = atoi(text_buf);
radio.frequency(freq); }
Serial.print("set frequency: "); radio.frequency(freq);
Serial.println(freq); Serial.print("set frequency: ");
int str_len = snprintf(text_buf, TEXT_BUF_LEN, "set frequency: %d\n", freq); Serial.println(freq);
bleuart.write(text_buf, str_len); int str_len = snprintf(text_buf, TEXT_BUF_LEN, "set frequency: %d\n", freq);
bleuart.write(text_buf, str_len);
}
} }
}
if (!currently_tx && (millis() - rssi_timeout) > RSSI_REPORT_RATE_MS)
{ if (!currently_tx && (millis() - rssi_timeout) > RSSI_REPORT_RATE_MS)
int rssi = radio.readRSSI(); {
Serial.println(rssi); int rssi = radio.readRSSI();
int str_len = snprintf(text_buf, TEXT_BUF_LEN, "rssi: %d\n", rssi); Serial.println(rssi);
bleuart.write(text_buf, str_len); int str_len = snprintf(text_buf, TEXT_BUF_LEN, "rssi: %d\n", rssi);
rssi_timeout = millis(); bleuart.write(text_buf, str_len);
} rssi_timeout = millis();
} }
}
// callback invoked when central connects
void connect_callback(uint16_t conn_handle) // callback invoked when central connects
{ void connect_callback(uint16_t conn_handle)
char central_name[32] = { 0 }; {
Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name)); char central_name[32] = { 0 };
Bluefruit.Gap.getPeerName(conn_handle, central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name); Serial.print("Connected to ");
} Serial.println(central_name);
}
/**
* Callback invoked when a connection is dropped /**
* @param conn_handle connection where this event happens * Callback invoked when a connection is dropped
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h * @param conn_handle connection where this event happens
* https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/ * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h
void disconnect_callback(uint16_t conn_handle, uint8_t reason) */
{ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
(void) conn_handle; {
(void) reason; (void) conn_handle;
(void) reason;
Serial.println();
Serial.println("Disconnected"); Serial.println();
} Serial.println("Disconnected");
}

File diff suppressed because it is too large Load Diff

View File

@ -165,7 +165,7 @@ void setup() {
delay(100); delay(100);
SerialWrite(";;;;;;;;;;;;;;;;;;;;;;;;;;"); SerialWrite(";;;;;;;;;;;;;;;;;;;;;;;;;;\n");
int result = radio.testConnection(); int result = radio.testConnection();
SerialWrite("*%d;\n", result); SerialWrite("*%d;\n", result);
@ -507,9 +507,9 @@ void getValue(bool ble_serial) {
for(;;) { for(;;) {
if((!ble_serial && Serial.available()) || (ble_serial && bleuart.available())) { if((!ble_serial && Serial.available()) || (ble_serial && bleuart.available())) {
if (ble_serial) { if (ble_serial) {
temp = Serial.read();
} else {
temp = bleuart.read(); temp = bleuart.read();
} else {
temp = Serial.read();
} }
if(temp == 59) { if(temp == 59) {
cmdbuff[p] = 0; cmdbuff[p] = 0;
@ -669,7 +669,7 @@ char text_buf[TEXT_BUF_LEN];
void SerialWrite(const char *fmt, ...) { void SerialWrite(const char *fmt, ...) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
int str_len = snprintf(text_buf, TEXT_BUF_LEN, fmt, args); int str_len = vsnprintf(text_buf, TEXT_BUF_LEN, fmt, args);
va_end(args); va_end(args);
bleuart.write(text_buf, str_len); bleuart.write(text_buf, str_len);