Skip to content

Commit 9c3ab01

Browse files
authored
Merge pull request #8 from edge-ml/feature/dfuFlash
Feature/dfu flash
2 parents bcacb00 + beeafef commit 9c3ab01

26 files changed

+372
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

src/nicla/Arduino_BHY2.cpp

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "Nicla_System.h"
1010
#endif
1111

12-
Arduino_BHY2::Arduino_BHY2()
12+
Arduino_BHY2::Arduino_BHY2() : _pingTime(0)
1313
{
1414
}
1515

@@ -18,9 +18,14 @@ Arduino_BHY2::~Arduino_BHY2()
1818
}
1919

2020
bool Arduino_BHY2::begin()
21-
2221
{
23-
#if defined(ARDUINO_NICLA)
22+
#if defined(ARDUINO_NICLA)
23+
24+
if (!dfuManager.begin())
25+
{
26+
return false;
27+
}
28+
2429
nicla::begin();
2530
nicla::enable3V3LDO();
2631
if (!sensortec.begin())
@@ -34,13 +39,54 @@ bool Arduino_BHY2::begin()
3439
_sensorDataCharacteristic = bleHandler.getSensorDataCharacteristic();
3540
BoschParser::setSensorDataCharacteristic(_sensorDataCharacteristic);
3641
return true;
37-
#endif
42+
#endif
43+
}
44+
45+
void Arduino_BHY2::pingI2C()
46+
{
47+
#if defined(ARDUINO_NICLA)
48+
char response = 0xFF;
49+
int currTime = millis();
50+
if ((currTime - _pingTime) > 30000)
51+
{
52+
_pingTime = currTime;
53+
// Read status reg
54+
nicla::readLDOreg();
55+
}
56+
#endif
3857
}
3958

4059
void Arduino_BHY2::update()
4160
{
61+
#if defined(ARDUINO_NICLA)
62+
pingI2C();
4263
sensortec.update();
4364
bleHandler.update();
65+
66+
// Flash new firmware
67+
if (dfuManager.isPending())
68+
{
69+
while (dfuManager.isPending())
70+
{
71+
if (bleHandler.bleActive)
72+
{
73+
bleHandler.update();
74+
}
75+
pingI2C();
76+
}
77+
// Wait some time for acknowledgment retrieval
78+
if (dfuManager.dfuSource() == bleDFU)
79+
{
80+
auto timeRef = millis();
81+
while (millis() - timeRef < 1000)
82+
{
83+
bleHandler.update();
84+
}
85+
}
86+
// Reboot after fw update
87+
NVIC_SystemReset();
88+
}
89+
#endif
4490
}
4591

4692
// Update and then sleep
@@ -52,19 +98,23 @@ void Arduino_BHY2::update(unsigned long ms)
5298

5399
void Arduino_BHY2::delay(unsigned long ms)
54100
{
101+
#if defined(ARDUINO_NICLA)
55102
unsigned long start = millis();
56103
unsigned long elapsed = 0;
57104
while (elapsed < ms)
58105
{
59106
bleHandler.poll(ms - elapsed);
60107
elapsed = millis() - start;
61108
}
109+
#endif
62110
}
63111

64112
void Arduino_BHY2::debug(Stream &stream)
65113
{
114+
#if defined(ARDUINO_NICLA)
66115
_debug = &stream;
67116
BLEHandler::debug(stream);
68117
sensortec.debug(stream);
69118
BoschParser::debug(stream);
119+
#endif
70120
}

src/nicla/Arduino_BHY2.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ class Arduino_BHY2 {
1010
Arduino_BHY2();
1111
virtual ~Arduino_BHY2();
1212

13+
14+
1315
// Necessary API. Update function should be continuously polled
16+
void pingI2C();
17+
int _pingTime;
18+
1419
bool begin();
1520
void update(); // remove this to enforce a sleep
1621
void update(unsigned long ms); // Update and then sleep

src/nicla/BLEHandler.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(ARDUINO_NICLA)
2+
13
#include "BLEHandler.h"
24
#include "DeviceInfo.h"
35

@@ -10,6 +12,13 @@ auto sensorConfigUuid = "34c2e3bd-34aa-11eb-adc1-0242ac120002";
1012
BLECharacteristic sensorDataCharacteristic(sensorDataUuid, (BLERead | BLENotify), sizeof(SensorDataPacket));
1113
BLECharacteristic sensorConfigCharacteristic(sensorConfigUuid, BLEWrite, sizeof(SensorConfigurationPacket));
1214

15+
// DFU channels
16+
BLEService dfuService("34c2e3b8-34aa-11eb-adc1-0242ac120002");
17+
auto dfuInternalUuid = "34c2e3b9-34aa-11eb-adc1-0242ac120002";
18+
auto dfuExternalUuid = "34c2e3ba-34aa-11eb-adc1-0242ac120002";
19+
BLECharacteristic dfuInternalCharacteristic(dfuInternalUuid, BLEWrite, sizeof(DFUPacket), true);
20+
BLECharacteristic dfuExternalCharacteristic(dfuExternalUuid, BLEWrite, sizeof(DFUPacket), true);
21+
1322
// Device information channel
1423
BLEService deviceInfoService("45622510-6468-465a-b141-0b9b0f96b468");
1524
auto deviceIdentifierUuid = "45622511-6468-465a-b141-0b9b0f96b468";
@@ -67,11 +76,50 @@ bool BLEHandler::begin()
6776
deviceIdentifierCharacteristic.writeValue(deviceIdentifier);
6877
deviceGenerationCharacteristic.writeValue(deviceGeneration);
6978

70-
//
79+
// DFU channel
80+
BLE.setAdvertisedService(dfuService);
81+
dfuService.addCharacteristic(dfuInternalCharacteristic);
82+
dfuService.addCharacteristic(dfuExternalCharacteristic);
83+
BLE.addService(dfuService);
84+
dfuInternalCharacteristic.setEventHandler(BLEWritten, receivedInternalDFU);
85+
dfuExternalCharacteristic.setEventHandler(BLEWritten, receivedExternalDFU);
86+
87+
7188
BLE.advertise();
7289
return true;
7390
}
7491

92+
// DFU channel
93+
void BLEHandler::processDFUPacket(DFUType dfuType, BLECharacteristic characteristic)
94+
{
95+
uint8_t data[sizeof(DFUPacket)];
96+
characteristic.readValue(data, sizeof(data));
97+
if (_debug) {
98+
_debug->print("Size of data: ");
99+
_debug->println(sizeof(data));
100+
}
101+
dfuManager.processPacket(bleDFU, dfuType, data);
102+
103+
if (data[0]) {
104+
//Last packet
105+
_lastDfuPack = true;
106+
dfuManager.closeDfu();
107+
}
108+
}
109+
110+
void BLEHandler::receivedInternalDFU(BLEDevice central, BLECharacteristic characteristic)
111+
{
112+
if (_debug) {
113+
_debug->println("receivedInternalDFU");
114+
}
115+
bleHandler.processDFUPacket(DFU_INTERNAL, characteristic);
116+
}
117+
118+
void BLEHandler::receivedExternalDFU(BLEDevice central, BLECharacteristic characteristic)
119+
{
120+
bleHandler.processDFUPacket(DFU_EXTERNAL, characteristic);
121+
}
122+
75123
BLECharacteristic *BLEHandler::getSensorDataCharacteristic()
76124
{
77125
return &sensorDataCharacteristic;
@@ -103,3 +151,5 @@ void BLEHandler::debug(Stream &stream)
103151
}
104152

105153
BLEHandler bleHandler;
154+
155+
#endif

src/nicla/BLEHandler.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
#if defined(ARDUINO_NICLA)
2+
13
#ifndef BLE_HANDLER_H_
24
#define BLE_HANDLER_H_
35

46
#include "Arduino.h"
57
#include "ArduinoBLE.h"
68

9+
#include "DFUManager.h"
10+
711
class BLEHandler {
812
public:
913
BLEHandler();
@@ -22,9 +26,15 @@ class BLEHandler {
2226
static Stream *_debug;
2327

2428
bool _lastDfuPack;
29+
30+
void processDFUPacket(DFUType dfuType, BLECharacteristic characteristic);
31+
static void receivedInternalDFU(BLEDevice central, BLECharacteristic characteristic);
32+
static void receivedExternalDFU(BLEDevice central, BLECharacteristic characteristic);
33+
2534
static void receivedSensorConfig(BLEDevice central, BLECharacteristic characteristic);
2635
};
2736

2837
extern BLEHandler bleHandler;
2938

3039
#endif
40+
#endif

src/nicla/BoschParser.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(ARDUINO_NICLA)
2+
13
#include "BoschParser.h"
24
#include "BoschSensortec.h"
35

@@ -50,4 +52,6 @@ void BoschParser::parseData(const struct bhy2_fifo_parse_data_info *fifoData, vo
5052
}
5153

5254
_sensorDataCharacteristic->writeValue(&sensorData, sizeof(SensorDataPacket));
53-
}
55+
}
56+
57+
#endif

src/nicla/BoschParser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(ARDUINO_NICLA)
2+
13
#ifndef BOSCH_PARSER_H_
24
#define BOSCH_PARSER_H_
35

@@ -30,3 +32,5 @@ class BoschParser {
3032
};
3133

3234
#endif
35+
36+
#endif

src/nicla/BoschSensortec.cpp

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(ARDUINO_NICLA)
2+
13
#include "BoschSensortec.h"
24
#include "BoschParser.h"
35

@@ -92,23 +94,6 @@ void BoschSensortec::configureSensor(SensorConfigurationPacket &config)
9294
}
9395
}
9496

95-
/*
96-
uint8_t BoschSensortec::availableSensorData()
97-
{
98-
return _sensorQueue.size();
99-
}
100-
101-
bool BoschSensortec::readSensorData(SensorDataPacket &data)
102-
{
103-
return _sensorQueue.pop(data);
104-
}
105-
106-
void BoschSensortec::addSensorData(SensorDataPacket &sensorData)
107-
{
108-
// Overwrites oldest data when fifo is full
109-
_sensorQueue.push(sensorData);
110-
}*/
111-
11297
// acknowledgment flag is reset when read
11398
uint8_t BoschSensortec::acknowledgment()
11499
{
@@ -120,31 +105,12 @@ uint8_t BoschSensortec::acknowledgment()
120105

121106
void BoschSensortec::update()
122107
{
123-
// Setup timing
124-
/*
125-
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
126-
DWT->CYCCNT = 0;
127-
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
128-
if (ARM_CM_DWT_CTRL != 0)
129-
{
130-
ARM_CM_DEMCR |= 1 << 24; // Set bit 24
131-
ARM_CM_DWT_CYCCNT = 0;
132-
ARM_CM_DWT_CTRL |= 1 << 0; // Set bit 0
133-
}
134-
*/
135-
136-
// Measure time
137-
//uint32_t startTime = ARM_CM_DWT_CYCCNT;
138-
139108
if (get_interrupt_status())
140109
{
141110
auto ret = bhy2_get_and_process_fifo(_workBuffer, WORK_BUFFER_SIZE, &_bhy2);
142111
if (_debug)
143112
_debug->println(get_api_error(ret));
144113
}
145-
146-
//uint32_t endTime = ARM_CM_DWT_CYCCNT;
147-
//Serial.println(endTime - startTime);
148114
}
149115

150116
void BoschSensortec::debug(Stream &stream)
@@ -179,3 +145,5 @@ extern "C"
179145
#endif /*__cplusplus */
180146

181147
BoschSensortec sensortec;
148+
149+
#endif

src/nicla/BoschSensortec.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if defined(ARDUINO_NICLA)
2+
13
#ifndef BOSCH_SENSORTEC_H_
24
#define BOSCH_SENSORTEC_H_
35

@@ -21,7 +23,6 @@ extern "C"
2123
}
2224
#endif
2325

24-
//#define SENSOR_QUEUE_SIZE 10
2526
#define WORK_BUFFER_SIZE 1024
2627

2728
#define MAX_READ_WRITE_LEN 256
@@ -67,3 +68,5 @@ class BoschSensortec {
6768
extern BoschSensortec sensortec;
6869

6970
#endif
71+
72+
#endif

0 commit comments

Comments
 (0)