Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ build_flags = ${arduino_base.build_flags}
-D NRF52_PLATFORM
-D LFS_NO_ASSERT=1
-D EXTRAFS=1
build_src_filter = ${arduino_base.build_src_filter}
+<helpers/nrf52/NRF52Board.cpp>
lib_deps =
${arduino_base.lib_deps}
https://github.com/oltaco/CustomLFS @ 0.2.1
Expand Down
81 changes: 81 additions & 0 deletions src/helpers/nrf52/NRF52Board.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "NRF52Board.h"
#include <bluefruit.h>

static BLEDfu bledfu;

static void connect_callback(uint16_t conn_handle) {
(void)conn_handle;
MESH_DEBUG_PRINTLN("BLE client connected");
}

static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
(void)conn_handle;
(void)reason;

MESH_DEBUG_PRINTLN("BLE client disconnected");
}

void NRF52Board::begin() {
startup_reason = BD_STARTUP_NORMAL;
}

void NRF52BoardDCDC::begin() {
NRF52Board::begin();

// Enable DC/DC converter for improved power efficiency
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
if (sd_enabled) {
sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);
} else {
NRF_POWER->DCDCEN = 1;
}
}

bool NRF52BoardOTA::startOTAUpdate(const char *id, char reply[]) {
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);

Bluefruit.begin(1, 0);
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4);
// Set the BLE device name
Bluefruit.setName(ota_name);

Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

// To be consistent OTA DFU should be added first if it exists
bledfu.begin();

// Set up and start advertising
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addName();

/* Start Advertising
- Enable auto advertising if disconnected
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
- Timeout for fast mode is 30 seconds
- Start(timeout) with timeout = 0 will advertise forever (until connected)

For recommended advertising interval
https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds

uint8_t mac_addr[6];
memset(mac_addr, 0, sizeof(mac_addr));
Bluefruit.getAddr(mac_addr);
sprintf(reply, "OK - mac: %02X:%02X:%02X:%02X:%02X:%02X",
mac_addr[5], mac_addr[4], mac_addr[3], mac_addr[2], mac_addr[1], mac_addr[0]);

return true;
}
40 changes: 40 additions & 0 deletions src/helpers/nrf52/NRF52Board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <Arduino.h>
#include <MeshCore.h>

class NRF52Board : public mesh::MainBoard {
protected:
uint8_t startup_reason;

public:
virtual void begin();

virtual uint8_t getStartupReason() const override {
return startup_reason;
}

virtual void reboot() override {
NVIC_SystemReset();
}
};

/*
* The NRF52 has an internal DC/DC regulator that allows increased efficiency
* compared to the LDO regulator. For being able to use it, the module/board
* needs to have the required inductors and and capacitors populated. If the
* hardware requirements are met, this subclass can be used to enable the DC/DC
* regulator.
*/
class NRF52BoardDCDC : virtual public NRF52Board {
public:
virtual void begin() override;
};

class NRF52BoardOTA : virtual public NRF52Board {
private:
char *ota_name;
public:
NRF52BoardOTA(char *name) : ota_name(name) {}
virtual bool startOTAUpdate(const char *id, char reply[]) override;
};
65 changes: 2 additions & 63 deletions variants/heltec_mesh_solar/MeshSolarBoard.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
#include <Arduino.h>
#include "MeshSolarBoard.h"

#include <bluefruit.h>
#include <Wire.h>

static BLEDfu bledfu;

static void connect_callback(uint16_t conn_handle)
{
(void)conn_handle;
MESH_DEBUG_PRINTLN("BLE client connected");
}

static void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void)conn_handle;
(void)reason;

MESH_DEBUG_PRINTLN("BLE client disconnected");
}
#include "MeshSolarBoard.h"

void MeshSolarBoard::begin() {
// for future use, sub-classes SHOULD call this from their begin()
startup_reason = BD_STARTUP_NORMAL;
NRF52Board::begin();

meshSolarStart();

Expand All @@ -32,46 +14,3 @@ void MeshSolarBoard::begin() {

Wire.begin();
}

bool MeshSolarBoard::startOTAUpdate(const char* id, char reply[]) {
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);

Bluefruit.begin(1, 0);
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4);
// Set the BLE device name
Bluefruit.setName("MESH_SOLAR_OTA");

Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

// To be consistent OTA DFU should be added first if it exists
bledfu.begin();

// Set up and start advertising
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addName();

/* Start Advertising
- Enable auto advertising if disconnected
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
- Timeout for fast mode is 30 seconds
- Start(timeout) with timeout = 0 will advertise forever (until connected)

For recommended advertising interval
https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds

strcpy(reply, "OK - started");
return true;
}
14 changes: 3 additions & 11 deletions variants/heltec_mesh_solar/MeshSolarBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <MeshCore.h>
#include <Arduino.h>
#include <helpers/nrf52/NRF52Board.h>

#ifdef HELTEC_MESH_SOLAR
#include "meshSolarApp.h"
Expand All @@ -20,13 +21,10 @@
#define SX126X_DIO3_TCXO_VOLTAGE 1.8


class MeshSolarBoard : public mesh::MainBoard {
protected:
uint8_t startup_reason;

class MeshSolarBoard : public NRF52BoardOTA {
public:
MeshSolarBoard() : NRF52BoardOTA("MESH_SOLAR_OTA") {}
void begin();
uint8_t getStartupReason() const override { return startup_reason; }

uint16_t getBattMilliVolts() override {
return meshSolarGetBattVoltage();
Expand All @@ -35,10 +33,4 @@ class MeshSolarBoard : public mesh::MainBoard {
const char* getManufacturerName() const override {
return "Heltec Mesh Solar";
}

void reboot() override {
NVIC_SystemReset();
}

bool startOTAUpdate(const char* id, char reply[]) override;
};
63 changes: 2 additions & 61 deletions variants/heltec_t114/T114Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,9 @@

#include <Arduino.h>
#include <Wire.h>
#include <bluefruit.h>

static BLEDfu bledfu;

static void connect_callback(uint16_t conn_handle) {
(void)conn_handle;
MESH_DEBUG_PRINTLN("BLE client connected");
}

static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
(void)conn_handle;
(void)reason;

MESH_DEBUG_PRINTLN("BLE client disconnected");
}

void T114Board::begin() {
// for future use, sub-classes SHOULD call this from their begin()
startup_reason = BD_STARTUP_NORMAL;
NRF52Board::begin();
NRF_POWER->DCDCEN = 1;

pinMode(PIN_VBAT_READ, INPUT);
Expand All @@ -39,47 +23,4 @@ void T114Board::begin() {
pinMode(SX126X_POWER_EN, OUTPUT);
digitalWrite(SX126X_POWER_EN, HIGH);
delay(10); // give sx1262 some time to power up
}

bool T114Board::startOTAUpdate(const char *id, char reply[]) {
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);

Bluefruit.begin(1, 0);
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4);
// Set the BLE device name
Bluefruit.setName("T114_OTA");

Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

// To be consistent OTA DFU should be added first if it exists
bledfu.begin();

// Set up and start advertising
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addName();

/* Start Advertising
- Enable auto advertising if disconnected
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
- Timeout for fast mode is 30 seconds
- Start(timeout) with timeout = 0 will advertise forever (until connected)

For recommended advertising interval
https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds

strcpy(reply, "OK - started");
return true;
}
}
14 changes: 3 additions & 11 deletions variants/heltec_t114/T114Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

#include <MeshCore.h>
#include <Arduino.h>
#include <helpers/nrf52/NRF52Board.h>

// built-ins
#define PIN_VBAT_READ 4
#define PIN_BAT_CTL 6
#define MV_LSB (3000.0F / 4096.0F) // 12-bit ADC with 3.0V input range

class T114Board : public mesh::MainBoard {
protected:
uint8_t startup_reason;

class T114Board : public NRF52BoardOTA {
public:
T114Board() : NRF52BoardOTA("T114_OTA") {}
void begin();
uint8_t getStartupReason() const override { return startup_reason; }

#if defined(P_LORA_TX_LED)
void onBeforeTransmit() override {
Expand Down Expand Up @@ -43,10 +41,6 @@ class T114Board : public mesh::MainBoard {
return "Heltec T114";
}

void reboot() override {
NVIC_SystemReset();
}

void powerOff() override {
#ifdef LED_PIN
digitalWrite(LED_PIN, HIGH);
Expand All @@ -57,6 +51,4 @@ class T114Board : public mesh::MainBoard {
#endif
sd_power_system_off();
}

bool startOTAUpdate(const char* id, char reply[]) override;
};
Loading