Skip to content

Commit 12ea354

Browse files
authored
Merge pull request #45 from arduino/fw_fixes_and_refactor
Nicla fw optimizations
2 parents 8c91fa9 + 1c3bb5c commit 12ea354

File tree

6 files changed

+20
-61
lines changed

6 files changed

+20
-61
lines changed

examples/Nicla_IoT_Bridge/Nicla_IoT_Bridge.ino

+1-8
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,4 @@ void loop() {
5959
}
6060

6161
ArduinoCloud.update();
62-
}
63-
64-
void onTempChange() {
65-
}
66-
67-
68-
void onSecondsChange() {
69-
}
62+
}

examples/Nicla_IoT_Bridge/thingProperties.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ const char THING_ID[] = "";
88
const char SSID[] = SECRET_SSID; // Network SSID (name)
99
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)
1010

11-
void onTempChange();
12-
void onSecondsChange();
13-
1411
float temp;
1512
int seconds;
1613

1714
void initProperties(){
1815

1916
ArduinoCloud.setThingId(THING_ID);
20-
ArduinoCloud.addProperty(temp, READWRITE, ON_CHANGE, onTempChange);
21-
ArduinoCloud.addProperty(seconds, READWRITE, ON_CHANGE, onSecondsChange);
17+
ArduinoCloud.addProperty(temp, READ, 1 * SECONDS, NULL);
18+
ArduinoCloud.addProperty(seconds, READ, 1 * SECONDS, NULL);
2219

2320
}
2421

src/Arduino_BHY2Host.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void Arduino_BHY2Host::update()
4848
eslovHandler.update();
4949
} else {
5050
uint8_t available = availableSensorData();
51+
delay(1);
5152
for (int i = 0; i < available; i++) {
5253
SensorDataPacket data;
5354
readSensorData(data);

src/EslovHandler.cpp

+16-46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "EslovHandler.h"
22

3-
#define ESLOV_DELAY (10)
3+
#define ESLOV_DELAY (1)
44

55
EslovHandler::EslovHandler() :
66
_rxIndex(0),
@@ -20,9 +20,7 @@ EslovHandler::~EslovHandler()
2020

2121
bool EslovHandler::begin(bool passthrough)
2222
{
23-
pinMode(_eslovIntPin, OUTPUT);
24-
digitalWrite(_eslovIntPin, LOW);
25-
23+
pinMode(_eslovIntPin, INPUT);
2624
Wire.begin();
2725
Wire.setClock(400000);
2826
if (passthrough) {
@@ -40,16 +38,12 @@ void EslovHandler::update()
4038
if (_rxBuffer[0] == HOST_DFU_EXTERNAL_OPCODE || _rxBuffer[0] == HOST_DFU_INTERNAL_OPCODE) {
4139
if (_rxIndex == sizeof(DFUPacket) + 1) {
4240

43-
toggleEslovIntPin();
44-
4541
if (!_dfuLedOn) {
4642
pinMode(LED_BUILTIN, OUTPUT);
4743
digitalWrite(LED_BUILTIN, HIGH);
4844
flushWire();
4945
}
5046

51-
pinMode(_eslovIntPin, INPUT);
52-
5347
//Wait for Nicla to set _eslovIntPin HIGH, meaning that is ready to receive
5448
while(!digitalRead(_eslovIntPin)) {
5549
if (_debug) _debug->println("Waiting for Eslov Int pin to be released");
@@ -76,8 +70,6 @@ void EslovHandler::update()
7670
dump();
7771

7872
_rxIndex = 0;
79-
80-
delay(ESLOV_DELAY);
8173

8274
Serial.write(ack);
8375
}
@@ -92,9 +84,15 @@ void EslovHandler::update()
9284

9385
SensorDataPacket sensorData;
9486
while (availableData) {
95-
//delay(ESLOV_DELAY);
9687
requestSensorData(sensorData);
97-
delay(ESLOV_DELAY);
88+
/*
89+
This delay is needed because the synchronization mechanism over the Eslov Int Pin
90+
may not apply for the requests from the host board to Nicla.
91+
It may happen that the onRequest callback on Nicla side is serviced after a certain delay.
92+
We need to add this delay of 10ms to avoid that a second request is issued before
93+
the first one is handled.
94+
*/
95+
delay(10);
9896
Serial.write((uint8_t*)&sensorData, sizeof(SensorDataPacket));
9997
availableData--;
10098
}
@@ -113,7 +111,7 @@ void EslovHandler::update()
113111
while (availableData) {
114112
//delay(ESLOV_DELAY);
115113
requestSensorLongData(sensorData);
116-
delay(ESLOV_DELAY);
114+
delay(10);
117115
Serial.write((uint8_t*)&sensorData, sizeof(SensorLongDataPacket));
118116
availableData--;
119117
}
@@ -123,7 +121,6 @@ void EslovHandler::update()
123121
} else if (_rxBuffer[0] == HOST_CONFIG_SENSOR_OPCODE) {
124122
if (_rxIndex == sizeof(SensorConfigurationPacket) + 1) {
125123

126-
toggleEslovIntPin();
127124
SensorConfigurationPacket* config = (SensorConfigurationPacket*)&_rxBuffer[1];
128125
if (_debug) {
129126
_debug->print("received config: ");
@@ -187,31 +184,28 @@ void EslovHandler::writeDfuPacket(uint8_t *data, uint8_t length)
187184

188185
void EslovHandler::writeStateChange(EslovState state)
189186
{
190-
delay(ESLOV_DELAY);
191187
while(!digitalRead(_eslovIntPin)) {}
192188
uint8_t packet[2] = {ESLOV_SENSOR_STATE_OPCODE, state};
193189
Wire.beginTransmission(ESLOV_DEFAULT_ADDRESS);
194190
Wire.write((uint8_t*)packet, sizeof(packet));
195191
Wire.endTransmission();
196-
delay(ESLOV_DELAY);
197192
_eslovState = state;
198193
}
199194

200195
void EslovHandler::writeConfigPacket(SensorConfigurationPacket& config)
201196
{
202-
delay(ESLOV_DELAY);
197+
while(!digitalRead(_eslovIntPin)) {}
203198
uint8_t packet[sizeof(SensorConfigurationPacket) + 1];
204199
packet[0] = ESLOV_SENSOR_CONFIG_OPCODE;
205200
memcpy(&packet[1], &config, sizeof(SensorConfigurationPacket));
206201
Wire.beginTransmission(ESLOV_DEFAULT_ADDRESS);
207202
Wire.write(packet, sizeof(SensorConfigurationPacket) + 1);
208203
Wire.endTransmission();
209-
delay(ESLOV_DELAY);
210204
}
211205

212206
uint8_t EslovHandler::requestPacketAck()
213-
{
214-
delay(ESLOV_DELAY);
207+
{
208+
while(!digitalRead(_eslovIntPin)) {}
215209
uint8_t ret = 0;
216210
while(!ret) {
217211
ret = Wire.requestFrom(ESLOV_DEFAULT_ADDRESS, 1);
@@ -230,7 +224,6 @@ uint8_t EslovHandler::requestAvailableData()
230224
uint8_t ret = Wire.requestFrom(ESLOV_DEFAULT_ADDRESS, 1);
231225
if (!ret) return 0;
232226
return Wire.read();
233-
delay(ESLOV_DELAY);
234227
}
235228

236229
uint8_t EslovHandler::requestAvailableLongData()
@@ -247,9 +240,8 @@ bool EslovHandler::requestSensorData(SensorDataPacket &sData)
247240
{
248241
if (_eslovState != ESLOV_READ_SENSOR_STATE) {
249242
writeStateChange(ESLOV_READ_SENSOR_STATE);
250-
while(!digitalRead(_eslovIntPin)) {}
251243
}
252-
//uint8_t ret = Wire.requestFrom(ESLOV_DEFAULT_ADDRESS, sizeof(SensorDataPacket));
244+
while(!digitalRead(_eslovIntPin)) {}
253245
uint8_t ret = Wire.requestFrom(ESLOV_DEFAULT_ADDRESS, sizeof(SensorDataPacket));
254246
if (!ret) return false;
255247

@@ -264,8 +256,8 @@ bool EslovHandler::requestSensorLongData(SensorLongDataPacket &sData)
264256
{
265257
if (_eslovState != ESLOV_READ_LONG_SENSOR_STATE) {
266258
writeStateChange(ESLOV_READ_LONG_SENSOR_STATE);
267-
while(!digitalRead(_eslovIntPin)) {}
268259
}
260+
while(!digitalRead(_eslovIntPin)) {}
269261
uint8_t ret = Wire.requestFrom(ESLOV_DEFAULT_ADDRESS, sizeof(SensorLongDataPacket));
270262
if (!ret) return false;
271263

@@ -276,28 +268,6 @@ bool EslovHandler::requestSensorLongData(SensorLongDataPacket &sData)
276268
return true;
277269
}
278270

279-
void EslovHandler::toggleEslovIntPin()
280-
{
281-
if (!_intPinAsserted) {
282-
// Indicates eslov presence
283-
pinMode(_eslovIntPin, OUTPUT);
284-
digitalWrite(_eslovIntPin, LOW);
285-
_intPinAsserted = true;
286-
if (_debug) {
287-
_debug->println("Eslov int LOW");
288-
}
289-
//Use 1 sec delay to let Nicla see the LOW pin and enable Eslov
290-
delay(500);
291-
292-
digitalWrite(_eslovIntPin, HIGH);
293-
_intPinCleared = true;
294-
if (_debug) {
295-
_debug->println("Eslov int pin cleared");
296-
}
297-
delay(500);
298-
}
299-
}
300-
301271
void EslovHandler::niclaAsShield()
302272
{
303273
_eslovIntPin = I2C_INT_PIN;

src/EslovHandler.h

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class EslovHandler {
5454
uint8_t requestAvailableLongData();
5555
bool requestSensorData(SensorDataPacket &sData);
5656
bool requestSensorLongData(SensorLongDataPacket &sData);
57-
void toggleEslovIntPin();
5857

5958
protected:
6059
void niclaAsShield();

src/sensors/SensorClass.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ void SensorClass::configure(float rate, uint32_t latency)
3838
if (BHY2Host.getNiclaConnection() == NICLA_VIA_BLE) {
3939
BHY2Host.configureSensor(config);
4040
} else {
41-
eslovHandler.toggleEslovIntPin();
4241
uint8_t ack = 0;
4342
while(ack != 15) {
4443
BHY2Host.configureSensor(config);

0 commit comments

Comments
 (0)