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
538float 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
0 commit comments