1
1
#include " EslovHandler.h"
2
2
3
- #define ESLOV_DELAY (10 )
3
+ #define ESLOV_DELAY (1 )
4
4
5
5
EslovHandler::EslovHandler () :
6
6
_rxIndex(0 ),
@@ -20,9 +20,7 @@ EslovHandler::~EslovHandler()
20
20
21
21
bool EslovHandler::begin (bool passthrough)
22
22
{
23
- pinMode (_eslovIntPin, OUTPUT);
24
- digitalWrite (_eslovIntPin, LOW);
25
-
23
+ pinMode (_eslovIntPin, INPUT);
26
24
Wire.begin ();
27
25
Wire.setClock (400000 );
28
26
if (passthrough) {
@@ -40,16 +38,12 @@ void EslovHandler::update()
40
38
if (_rxBuffer[0 ] == HOST_DFU_EXTERNAL_OPCODE || _rxBuffer[0 ] == HOST_DFU_INTERNAL_OPCODE) {
41
39
if (_rxIndex == sizeof (DFUPacket) + 1 ) {
42
40
43
- toggleEslovIntPin ();
44
-
45
41
if (!_dfuLedOn) {
46
42
pinMode (LED_BUILTIN, OUTPUT);
47
43
digitalWrite (LED_BUILTIN, HIGH);
48
44
flushWire ();
49
45
}
50
46
51
- pinMode (_eslovIntPin, INPUT);
52
-
53
47
// Wait for Nicla to set _eslovIntPin HIGH, meaning that is ready to receive
54
48
while (!digitalRead (_eslovIntPin)) {
55
49
if (_debug) _debug->println (" Waiting for Eslov Int pin to be released" );
@@ -76,8 +70,6 @@ void EslovHandler::update()
76
70
dump ();
77
71
78
72
_rxIndex = 0 ;
79
-
80
- delay (ESLOV_DELAY);
81
73
82
74
Serial.write (ack);
83
75
}
@@ -92,9 +84,15 @@ void EslovHandler::update()
92
84
93
85
SensorDataPacket sensorData;
94
86
while (availableData) {
95
- // delay(ESLOV_DELAY);
96
87
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 );
98
96
Serial.write ((uint8_t *)&sensorData, sizeof (SensorDataPacket));
99
97
availableData--;
100
98
}
@@ -113,7 +111,7 @@ void EslovHandler::update()
113
111
while (availableData) {
114
112
// delay(ESLOV_DELAY);
115
113
requestSensorLongData (sensorData);
116
- delay (ESLOV_DELAY );
114
+ delay (10 );
117
115
Serial.write ((uint8_t *)&sensorData, sizeof (SensorLongDataPacket));
118
116
availableData--;
119
117
}
@@ -123,7 +121,6 @@ void EslovHandler::update()
123
121
} else if (_rxBuffer[0 ] == HOST_CONFIG_SENSOR_OPCODE) {
124
122
if (_rxIndex == sizeof (SensorConfigurationPacket) + 1 ) {
125
123
126
- toggleEslovIntPin ();
127
124
SensorConfigurationPacket* config = (SensorConfigurationPacket*)&_rxBuffer[1 ];
128
125
if (_debug) {
129
126
_debug->print (" received config: " );
@@ -187,31 +184,28 @@ void EslovHandler::writeDfuPacket(uint8_t *data, uint8_t length)
187
184
188
185
void EslovHandler::writeStateChange (EslovState state)
189
186
{
190
- delay (ESLOV_DELAY);
191
187
while (!digitalRead (_eslovIntPin)) {}
192
188
uint8_t packet[2 ] = {ESLOV_SENSOR_STATE_OPCODE, state};
193
189
Wire.beginTransmission (ESLOV_DEFAULT_ADDRESS);
194
190
Wire.write ((uint8_t *)packet, sizeof (packet));
195
191
Wire.endTransmission ();
196
- delay (ESLOV_DELAY);
197
192
_eslovState = state;
198
193
}
199
194
200
195
void EslovHandler::writeConfigPacket (SensorConfigurationPacket& config)
201
196
{
202
- delay (ESLOV_DELAY);
197
+ while (! digitalRead (_eslovIntPin)) {}
203
198
uint8_t packet[sizeof (SensorConfigurationPacket) + 1 ];
204
199
packet[0 ] = ESLOV_SENSOR_CONFIG_OPCODE;
205
200
memcpy (&packet[1 ], &config, sizeof (SensorConfigurationPacket));
206
201
Wire.beginTransmission (ESLOV_DEFAULT_ADDRESS);
207
202
Wire.write (packet, sizeof (SensorConfigurationPacket) + 1 );
208
203
Wire.endTransmission ();
209
- delay (ESLOV_DELAY);
210
204
}
211
205
212
206
uint8_t EslovHandler::requestPacketAck ()
213
- {
214
- delay (ESLOV_DELAY);
207
+ {
208
+ while (! digitalRead (_eslovIntPin)) {}
215
209
uint8_t ret = 0 ;
216
210
while (!ret) {
217
211
ret = Wire.requestFrom (ESLOV_DEFAULT_ADDRESS, 1 );
@@ -230,7 +224,6 @@ uint8_t EslovHandler::requestAvailableData()
230
224
uint8_t ret = Wire.requestFrom (ESLOV_DEFAULT_ADDRESS, 1 );
231
225
if (!ret) return 0 ;
232
226
return Wire.read ();
233
- delay (ESLOV_DELAY);
234
227
}
235
228
236
229
uint8_t EslovHandler::requestAvailableLongData ()
@@ -247,9 +240,8 @@ bool EslovHandler::requestSensorData(SensorDataPacket &sData)
247
240
{
248
241
if (_eslovState != ESLOV_READ_SENSOR_STATE) {
249
242
writeStateChange (ESLOV_READ_SENSOR_STATE);
250
- while (!digitalRead (_eslovIntPin)) {}
251
243
}
252
- // uint8_t ret = Wire.requestFrom(ESLOV_DEFAULT_ADDRESS, sizeof(SensorDataPacket));
244
+ while (! digitalRead (_eslovIntPin)) {}
253
245
uint8_t ret = Wire.requestFrom (ESLOV_DEFAULT_ADDRESS, sizeof (SensorDataPacket));
254
246
if (!ret) return false ;
255
247
@@ -264,8 +256,8 @@ bool EslovHandler::requestSensorLongData(SensorLongDataPacket &sData)
264
256
{
265
257
if (_eslovState != ESLOV_READ_LONG_SENSOR_STATE) {
266
258
writeStateChange (ESLOV_READ_LONG_SENSOR_STATE);
267
- while (!digitalRead (_eslovIntPin)) {}
268
259
}
260
+ while (!digitalRead (_eslovIntPin)) {}
269
261
uint8_t ret = Wire.requestFrom (ESLOV_DEFAULT_ADDRESS, sizeof (SensorLongDataPacket));
270
262
if (!ret) return false ;
271
263
@@ -276,28 +268,6 @@ bool EslovHandler::requestSensorLongData(SensorLongDataPacket &sData)
276
268
return true ;
277
269
}
278
270
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
-
301
271
void EslovHandler::niclaAsShield ()
302
272
{
303
273
_eslovIntPin = I2C_INT_PIN;
0 commit comments