Skip to content

Commit 25b4c35

Browse files
committed
Increase I2C priority if LED Matrix is used
The distance sensor doesn't like to be interrupted in the middle of a transaction. Changing the priority of the IIC peripheral to be higher than the timer one will prevent it from getting stuck
1 parent 4f15175 commit 25b4c35

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Modulino.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,19 @@ ModulinoColor RED(255, 0, 0);
1111
ModulinoColor BLUE(0, 0, 255);
1212
ModulinoColor GREEN(0, 255, 0);
1313
ModulinoColor VIOLET(255, 0, 255);
14-
ModulinoColor WHITE(255, 255, 255);
14+
ModulinoColor WHITE(255, 255, 255);
15+
16+
#if __has_include("Arduino_LED_Matrix.h")
17+
void __increaseI2CPriority() {
18+
for (int i = 0; i < 96; i++) {
19+
if (R_ICU->IELSR[i] == BSP_PRV_IELS_ENUM(EVENT_IIC0_TXI)) {
20+
NVIC_SetPriority(IRQn_Type(i), 6);
21+
NVIC_SetPriority(IRQn_Type(i+1), 6);
22+
NVIC_SetPriority(IRQn_Type(i+2), 6);
23+
NVIC_SetPriority(IRQn_Type(i+3), 6);
24+
}
25+
}
26+
}
27+
#else
28+
void __increaseI2CPriority() {}
29+
#endif

src/Modulino.h

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define HardwareI2C TwoWire
1515
#endif
1616

17+
void __increaseI2CPriority();
18+
1719
class ModulinoClass {
1820
public:
1921
#ifdef ARDUINO_UNOR4_WIFI
@@ -33,6 +35,7 @@ class ModulinoClass {
3335
_wire = &wire;
3436
_wire->begin();
3537
_wire->setClock(100000);
38+
__increaseI2CPriority();
3639
}
3740
friend class Module;
3841
protected:
@@ -298,6 +301,7 @@ class ModulinoMovement : public Module {
298301
_imu = new LSM6DSOXClass(*((TwoWire*)getWire()), 0x6A);
299302
}
300303
initialized = _imu->begin();
304+
__increaseI2CPriority();
301305
return initialized != 0;
302306
}
303307
operator bool() {
@@ -331,6 +335,7 @@ class ModulinoThermo: public Module {
331335
_sensor = new HS300xClass(*((TwoWire*)getWire()));
332336
}
333337
initialized = _sensor->begin();
338+
__increaseI2CPriority();
334339
return initialized;
335340
}
336341
operator bool() {
@@ -364,6 +369,7 @@ class ModulinoPressure : public Module {
364369
// unfortunately LPS22HBClass calles Wire.end() on failure, restart it
365370
getWire()->begin();
366371
}
372+
__increaseI2CPriority();
367373
return initialized != 0;
368374
}
369375
operator bool() {
@@ -400,6 +406,7 @@ class ModulinoDistance : public Module {
400406
}
401407
tof_sensor = new VL53L4CD((TwoWire*)getWire(), -1);
402408
auto ret = tof_sensor->InitSensor();
409+
__increaseI2CPriority();
403410
if (ret == VL53L4CD_ERROR_NONE) {
404411
tof_sensor->VL53L4CD_SetRangeTiming(20, 0);
405412
tof_sensor->VL53L4CD_StartRanging();

0 commit comments

Comments
 (0)