Skip to content

Commit 9b13106

Browse files
authored
Merge pull request #1201 from fschrempf/nrf52-board-deduplication
NRF52 Board Code Deduplication
2 parents d67f311 + 8eb229b commit 9b13106

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+192
-1398
lines changed

src/helpers/NRF52Board.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
#if defined(NRF52_PLATFORM)
22
#include "NRF52Board.h"
33

4+
#include <bluefruit.h>
5+
6+
static BLEDfu bledfu;
7+
8+
static void connect_callback(uint16_t conn_handle) {
9+
(void)conn_handle;
10+
MESH_DEBUG_PRINTLN("BLE client connected");
11+
}
12+
13+
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
14+
(void)conn_handle;
15+
(void)reason;
16+
17+
MESH_DEBUG_PRINTLN("BLE client disconnected");
18+
}
19+
20+
void NRF52Board::begin() {
21+
startup_reason = BD_STARTUP_NORMAL;
22+
}
23+
24+
void NRF52BoardDCDC::begin() {
25+
NRF52Board::begin();
26+
27+
// Enable DC/DC converter for improved power efficiency
28+
uint8_t sd_enabled = 0;
29+
sd_softdevice_is_enabled(&sd_enabled);
30+
if (sd_enabled) {
31+
sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);
32+
} else {
33+
NRF_POWER->DCDCEN = 1;
34+
}
35+
}
36+
437
// Temperature from NRF52 MCU
538
float NRF52Board::getMCUTemperature() {
639
NRF_TEMP->TASKS_START = 1; // Start temperature measurement
@@ -20,4 +53,52 @@ float NRF52Board::getMCUTemperature() {
2053

2154
return temp * 0.25f; // Convert to *C
2255
}
56+
57+
bool NRF52BoardOTA::startOTAUpdate(const char *id, char reply[]) {
58+
// Config the peripheral connection with maximum bandwidth
59+
// more SRAM required by SoftDevice
60+
// Note: All config***() function must be called before begin()
61+
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
62+
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
63+
64+
Bluefruit.begin(1, 0);
65+
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
66+
Bluefruit.setTxPower(4);
67+
// Set the BLE device name
68+
Bluefruit.setName(ota_name);
69+
70+
Bluefruit.Periph.setConnectCallback(connect_callback);
71+
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
72+
73+
// To be consistent OTA DFU should be added first if it exists
74+
bledfu.begin();
75+
76+
// Set up and start advertising
77+
// Advertising packet
78+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
79+
Bluefruit.Advertising.addTxPower();
80+
Bluefruit.Advertising.addName();
81+
82+
/* Start Advertising
83+
- Enable auto advertising if disconnected
84+
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
85+
- Timeout for fast mode is 30 seconds
86+
- Start(timeout) with timeout = 0 will advertise forever (until connected)
87+
88+
For recommended advertising interval
89+
https://developer.apple.com/library/content/qa/qa1931/_index.html
90+
*/
91+
Bluefruit.Advertising.restartOnDisconnect(true);
92+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
93+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
94+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
95+
96+
uint8_t mac_addr[6];
97+
memset(mac_addr, 0, sizeof(mac_addr));
98+
Bluefruit.getAddr(mac_addr);
99+
sprintf(reply, "OK - mac: %02X:%02X:%02X:%02X:%02X:%02X", mac_addr[5], mac_addr[4], mac_addr[3],
100+
mac_addr[2], mac_addr[1], mac_addr[0]);
101+
102+
return true;
103+
}
23104
#endif

src/helpers/NRF52Board.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,34 @@
66
#if defined(NRF52_PLATFORM)
77

88
class NRF52Board : public mesh::MainBoard {
9+
protected:
10+
uint8_t startup_reason;
11+
12+
public:
13+
virtual void begin();
14+
virtual uint8_t getStartupReason() const override { return startup_reason; }
15+
virtual float getMCUTemperature() override;
16+
virtual void reboot() override { NVIC_SystemReset(); }
17+
};
18+
19+
/*
20+
* The NRF52 has an internal DC/DC regulator that allows increased efficiency
21+
* compared to the LDO regulator. For being able to use it, the module/board
22+
* needs to have the required inductors and and capacitors populated. If the
23+
* hardware requirements are met, this subclass can be used to enable the DC/DC
24+
* regulator.
25+
*/
26+
class NRF52BoardDCDC : virtual public NRF52Board {
27+
public:
28+
virtual void begin() override;
29+
};
30+
31+
class NRF52BoardOTA : virtual public NRF52Board {
32+
private:
33+
char *ota_name;
34+
935
public:
10-
float getMCUTemperature() override;
36+
NRF52BoardOTA(char *name) : ota_name(name) {}
37+
virtual bool startOTAUpdate(const char *id, char reply[]) override;
1138
};
1239
#endif
Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
#include <Arduino.h>
2-
#include "MeshSolarBoard.h"
3-
4-
#include <bluefruit.h>
52
#include <Wire.h>
63

7-
static BLEDfu bledfu;
8-
9-
static void connect_callback(uint16_t conn_handle)
10-
{
11-
(void)conn_handle;
12-
MESH_DEBUG_PRINTLN("BLE client connected");
13-
}
14-
15-
static void disconnect_callback(uint16_t conn_handle, uint8_t reason)
16-
{
17-
(void)conn_handle;
18-
(void)reason;
19-
20-
MESH_DEBUG_PRINTLN("BLE client disconnected");
21-
}
4+
#include "MeshSolarBoard.h"
225

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

279
meshSolarStart();
2810

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

3315
Wire.begin();
3416
}
35-
36-
bool MeshSolarBoard::startOTAUpdate(const char* id, char reply[]) {
37-
// Config the peripheral connection with maximum bandwidth
38-
// more SRAM required by SoftDevice
39-
// Note: All config***() function must be called before begin()
40-
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
41-
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
42-
43-
Bluefruit.begin(1, 0);
44-
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
45-
Bluefruit.setTxPower(4);
46-
// Set the BLE device name
47-
Bluefruit.setName("MESH_SOLAR_OTA");
48-
49-
Bluefruit.Periph.setConnectCallback(connect_callback);
50-
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
51-
52-
// To be consistent OTA DFU should be added first if it exists
53-
bledfu.begin();
54-
55-
// Set up and start advertising
56-
// Advertising packet
57-
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
58-
Bluefruit.Advertising.addTxPower();
59-
Bluefruit.Advertising.addName();
60-
61-
/* Start Advertising
62-
- Enable auto advertising if disconnected
63-
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
64-
- Timeout for fast mode is 30 seconds
65-
- Start(timeout) with timeout = 0 will advertise forever (until connected)
66-
67-
For recommended advertising interval
68-
https://developer.apple.com/library/content/qa/qa1931/_index.html
69-
*/
70-
Bluefruit.Advertising.restartOnDisconnect(true);
71-
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
72-
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
73-
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
74-
75-
strcpy(reply, "OK - started");
76-
return true;
77-
}

variants/heltec_mesh_solar/MeshSolarBoard.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@
2020
#define SX126X_DIO2_AS_RF_SWITCH true
2121
#define SX126X_DIO3_TCXO_VOLTAGE 1.8
2222

23-
24-
class MeshSolarBoard : public NRF52Board {
25-
protected:
26-
uint8_t startup_reason;
27-
23+
class MeshSolarBoard : public NRF52BoardOTA {
2824
public:
25+
MeshSolarBoard() : NRF52BoardOTA("MESH_SOLAR_OTA") {}
2926
void begin();
30-
uint8_t getStartupReason() const override { return startup_reason; }
3127

3228
uint16_t getBattMilliVolts() override {
3329
return meshSolarGetBattVoltage();
@@ -36,10 +32,4 @@ class MeshSolarBoard : public NRF52Board {
3632
const char* getManufacturerName() const override {
3733
return "Heltec Mesh Solar";
3834
}
39-
40-
void reboot() override {
41-
NVIC_SystemReset();
42-
}
43-
44-
bool startOTAUpdate(const char* id, char reply[]) override;
4535
};

variants/heltec_t114/T114Board.cpp

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,9 @@
22

33
#include <Arduino.h>
44
#include <Wire.h>
5-
#include <bluefruit.h>
6-
7-
static BLEDfu bledfu;
8-
9-
static void connect_callback(uint16_t conn_handle) {
10-
(void)conn_handle;
11-
MESH_DEBUG_PRINTLN("BLE client connected");
12-
}
13-
14-
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
15-
(void)conn_handle;
16-
(void)reason;
17-
18-
MESH_DEBUG_PRINTLN("BLE client disconnected");
19-
}
205

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

2610
pinMode(PIN_VBAT_READ, INPUT);
@@ -39,47 +23,4 @@ void T114Board::begin() {
3923
pinMode(SX126X_POWER_EN, OUTPUT);
4024
digitalWrite(SX126X_POWER_EN, HIGH);
4125
delay(10); // give sx1262 some time to power up
42-
}
43-
44-
bool T114Board::startOTAUpdate(const char *id, char reply[]) {
45-
// Config the peripheral connection with maximum bandwidth
46-
// more SRAM required by SoftDevice
47-
// Note: All config***() function must be called before begin()
48-
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
49-
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
50-
51-
Bluefruit.begin(1, 0);
52-
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
53-
Bluefruit.setTxPower(4);
54-
// Set the BLE device name
55-
Bluefruit.setName("T114_OTA");
56-
57-
Bluefruit.Periph.setConnectCallback(connect_callback);
58-
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
59-
60-
// To be consistent OTA DFU should be added first if it exists
61-
bledfu.begin();
62-
63-
// Set up and start advertising
64-
// Advertising packet
65-
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
66-
Bluefruit.Advertising.addTxPower();
67-
Bluefruit.Advertising.addName();
68-
69-
/* Start Advertising
70-
- Enable auto advertising if disconnected
71-
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
72-
- Timeout for fast mode is 30 seconds
73-
- Start(timeout) with timeout = 0 will advertise forever (until connected)
74-
75-
For recommended advertising interval
76-
https://developer.apple.com/library/content/qa/qa1931/_index.html
77-
*/
78-
Bluefruit.Advertising.restartOnDisconnect(true);
79-
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
80-
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
81-
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
82-
83-
strcpy(reply, "OK - started");
84-
return true;
85-
}
26+
}

variants/heltec_t114/T114Board.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
#define PIN_BAT_CTL 6
1010
#define MV_LSB (3000.0F / 4096.0F) // 12-bit ADC with 3.0V input range
1111

12-
class T114Board : public NRF52Board {
13-
protected:
14-
uint8_t startup_reason;
15-
12+
class T114Board : public NRF52BoardOTA {
1613
public:
14+
T114Board() : NRF52BoardOTA("T114_OTA") {}
1715
void begin();
18-
uint8_t getStartupReason() const override { return startup_reason; }
1916

2017
#if defined(P_LORA_TX_LED)
2118
void onBeforeTransmit() override {
@@ -44,10 +41,6 @@ class T114Board : public NRF52Board {
4441
return "Heltec T114";
4542
}
4643

47-
void reboot() override {
48-
NVIC_SystemReset();
49-
}
50-
5144
void powerOff() override {
5245
#ifdef LED_PIN
5346
digitalWrite(LED_PIN, HIGH);
@@ -58,6 +51,4 @@ class T114Board : public NRF52Board {
5851
#endif
5952
sd_power_system_off();
6053
}
61-
62-
bool startOTAUpdate(const char* id, char reply[]) override;
6354
};

0 commit comments

Comments
 (0)