Skip to content

Commit

Permalink
Merge pull request #61 from jacobeva/ble
Browse files Browse the repository at this point in the history
Add NRF52 BLE serial support
  • Loading branch information
markqvist authored Feb 10, 2024
2 parents d9a982b + 80769f1 commit 5a87095
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 544 deletions.
154 changes: 154 additions & 0 deletions Bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#if MCU_VARIANT == MCU_ESP32
#include "BluetoothSerial.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"

#elif MCU_VARIANT == MCU_NRF52
#include <bluefruit.h>
#include <math.h>
#endif

#if MCU_VARIANT == MCU_ESP32
BluetoothSerial SerialBT;
#elif MCU_VARIANT == MCU_NRF52
BLEUart SerialBT;
BLEDis bledis;
BLEBas blebas;
#endif

#define BT_PAIRING_TIMEOUT 35000
uint32_t bt_pairing_started = 0;

Expand Down Expand Up @@ -138,4 +151,145 @@ char bt_devname[11];
}
}

#elif MCU_VARIANT == MCU_NRF52
uint8_t eeprom_read(uint32_t mapped_addr);

void bt_stop() {
if (bt_state != BT_STATE_OFF) {
bt_allow_pairing = false;
bt_state = BT_STATE_OFF;
}
}

void bt_disable_pairing() {
bt_allow_pairing = false;
bt_ssp_pin = 0;
bt_state = BT_STATE_ON;
}

void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
bt_disable_pairing();
} else {
bt_ssp_pin = 0;
}
}

bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) {
for (int i = 0; i < 6; i++) {
// multiply by tens however many times needed to make numbers appear in order
bt_ssp_pin += ((int)passkey[i] - 48) * pow(10, 5-i);
}
kiss_indicate_btpin();
if (match_request) {
if (bt_allow_pairing) {
return true;
}
}
return false;
}

void bt_connect_callback(uint16_t conn_handle) {
bt_state = BT_STATE_CONNECTED;
cable_state = CABLE_STATE_DISCONNECTED;
}

void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
bt_state = BT_STATE_ON;
}

bool bt_setup_hw() {
if (!bt_ready) {
#if HAS_EEPROM
if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
#else
if (eeprom_read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
#endif
bt_enabled = true;
} else {
bt_enabled = false;
}
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.autoConnLed(false);
if (Bluefruit.begin()) {
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.Security.setIOCaps(true, true, false);
Bluefruit.Security.setPairPasskeyCallback(bt_passkey_callback);
Bluefruit.Periph.setConnectCallback(bt_connect_callback);
Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
Bluefruit.Security.setIOCaps(true, true, false);
Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
const ble_gap_addr_t gap_addr = Bluefruit.getAddr();
char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
data[i] = gap_addr.addr[i];
}
#if HAS_EEPROM
data[BT_DEV_ADDR_LEN] = EEPROM.read(eeprom_addr(ADDR_SIGNATURE));
#else
data[BT_DEV_ADDR_LEN] = eeprom_read(eeprom_addr(ADDR_SIGNATURE));
#endif
unsigned char *hash = MD5::make_hash(data, BT_DEV_ADDR_LEN);
memcpy(bt_dh, hash, BT_DEV_HASH_LEN);
sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]);
free(data);

bt_ready = true;
return true;

} else { return false; }
} else { return false; }
}

void bt_start() {
if (bt_state == BT_STATE_OFF) {
Bluefruit.setName(bt_devname);
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather52");
// start device information service
bledis.begin();

SerialBT.begin();

blebas.begin();

// non-connectable advertising
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();

// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(SerialBT);

// There is no room for Name in Advertising packet
// Use Scan response for Name
Bluefruit.ScanResponse.addName();

Bluefruit.Advertising.start(0);

bt_state = BT_STATE_ON;
}
}

bool bt_init() {
bt_state = BT_STATE_OFF;
if (bt_setup_hw()) {
if (bt_enabled && !console_active) bt_start();
return true;
} else {
return false;
}
}

void bt_enable_pairing() {
if (bt_state == BT_STATE_OFF) bt_start();
bt_allow_pairing = true;
bt_pairing_started = millis();
bt_state = BT_STATE_PAIRING;
}

void update_bt() {
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
bt_disable_pairing();
}
}
#endif
7 changes: 4 additions & 3 deletions Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
#if BOARD_MODEL == BOARD_RAK4630
#define HAS_EEPROM false
#define HAS_DISPLAY false // set for debugging
#define HAS_BLUETOOTH false
#define HAS_BLUETOOTH true
#define HAS_CONSOLE false
#define HAS_PMU false
#define HAS_NP false
Expand All @@ -324,8 +324,9 @@
#define CONFIG_UART_BUFFER_SIZE 6144
#define CONFIG_QUEUE_SIZE 6144
#define CONFIG_QUEUE_MAX_LENGTH 200
#define EEPROM_SIZE 4096
#define EEPROM_OFFSET EEPROM_SIZE+0xED000-EEPROM_RESERVED
#define EEPROM_SIZE 200
//#define EEPROM_OFFSET EEPROM_SIZE+0xED000-EEPROM_RESERVED
#define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED

// following pins are for the sx1262
const int pin_rxen = 37;
Expand Down
8 changes: 7 additions & 1 deletion RNode_Firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ void setup() {
Serial.setRxBufferSize(CONFIG_UART_BUFFER_SIZE);
#endif

#if MCU_VARIANT == MCU_NRF52
if (!eeprom_begin()) {
Serial.write("EEPROM initialisation failed.\r\n");
}
#endif

// Seed the PRNG
randomSeed(analogRead(0));

Expand Down Expand Up @@ -140,7 +146,7 @@ void setup() {
update_display();
#endif

#if MCU_VARIANT == MCU_ESP32
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
#if HAS_PMU == true
pmu_ready = init_pmu();
#endif
Expand Down
64 changes: 54 additions & 10 deletions Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
#if HAS_EEPROM
#include <EEPROM.h>
#elif PLATFORM == PLATFORM_NRF52
#include "flash_nrf5x.h"
int written_bytes = 0;
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
using namespace Adafruit_LittleFS_Namespace;
#define EEPROM_FILE "eeprom"
bool file_exists = false;
int written_bytes = 4;
File file(InternalFS);
#endif
#include <stddef.h>

Expand Down Expand Up @@ -1104,10 +1109,33 @@ void promisc_disable() {
}

#if !HAS_EEPROM && MCU_VARIANT == MCU_NRF52
bool eeprom_begin() {
InternalFS.begin();

file.open(EEPROM_FILE, FILE_O_READ);

// if file doesn't exist
if (!file) {
if (file.open(EEPROM_FILE, FILE_O_WRITE)) {
// initialise the file with empty content
uint8_t empty_content[EEPROM_SIZE] = {0};
file.write(empty_content, EEPROM_SIZE);
return true;
} else {
return false;
}
} else {
file.close();
file.open(EEPROM_FILE, FILE_O_WRITE);
return true;
}
}

uint8_t eeprom_read(uint32_t mapped_addr) {
uint8_t byte;
void* byte_ptr = &byte;
flash_nrf5x_read(byte_ptr, mapped_addr, 1);
file.seek(mapped_addr);
file.read(byte_ptr, 1);
return byte;
}
#endif
Expand Down Expand Up @@ -1176,22 +1204,30 @@ void eeprom_update(int mapped_addr, uint8_t byte) {
#elif !HAS_EEPROM && MCU_VARIANT == MCU_NRF52
uint8_t read_byte;
void* read_byte_ptr = &read_byte;
void const * byte_ptr = &byte;
flash_nrf5x_read(read_byte_ptr, mapped_addr, 1);
file.seek(mapped_addr);
file.read(read_byte_ptr, 1);
file.seek(mapped_addr);
if (read_byte != byte) {
flash_nrf5x_write(mapped_addr, byte_ptr, 1);
file.write(byte);
}

written_bytes++;

// flush the cache every 4 bytes to make sure everything is synced
if (written_bytes == 4) {
if (written_bytes >= 8) {
file.close();
file.open(EEPROM_FILE, FILE_O_WRITE);
written_bytes = 0;
flash_nrf5x_flush();
}
#endif
}

#if !HAS_EEPROM && MCU_VARIANT == MCU_NRF52
void eeprom_flush() {
// sync file contents to flash
file.close();
file.open(EEPROM_FILE, FILE_O_WRITE);
written_bytes = 0;
}
#endif

void eeprom_write(uint8_t addr, uint8_t byte) {
if (!eeprom_info_locked() && addr >= 0 && addr < EEPROM_RESERVED) {
Expand Down Expand Up @@ -1329,8 +1365,16 @@ bool eeprom_checksum_valid() {
void bt_conf_save(bool is_enabled) {
if (is_enabled) {
eeprom_update(eeprom_addr(ADDR_CONF_BT), BT_ENABLE_BYTE);
#if !HAS_EEPROM && MCU_VARIANT == MCU_NRF52
// have to do a flush because we're only writing 1 byte and it syncs after 8
eeprom_flush();
#endif
} else {
eeprom_update(eeprom_addr(ADDR_CONF_BT), 0x00);
#if !HAS_EEPROM && MCU_VARIANT == MCU_NRF52
// have to do a flush because we're only writing 1 byte and it syncs after 8
eeprom_flush();
#endif
}
}

Expand Down
Loading

0 comments on commit 5a87095

Please sign in to comment.