Skip to content

Commit d6413c2

Browse files
committed
fix handling of events from low accuracy watches
1 parent e0a9fe2 commit d6413c2

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

targets/nrf5x/jshardware.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,16 @@ volatile bool flashIsBusy = false;
278278
volatile bool hadEvent = false; // set if we've had an event we need to deal with
279279
unsigned int ticksSinceStart = 0;
280280

281+
#if GPIO_COUNT>1
282+
#define NRF_GPIO_PIN_MAX (P0_PIN_NUM+P1_PIN_NUM)
283+
#else
284+
#define NRF_GPIO_PIN_MAX P0_PIN_NUM
285+
#endif
286+
287+
/// Current state of each pin
281288
JshPinFunction pinStates[JSH_PIN_COUNT];
289+
/// for each EXTI, which nordic pin (0..31 / 0..47) is used (PIN_UNDEFINED if unused)
290+
uint8_t extiToPin[EXTI_COUNT];
282291

283292
#ifdef NRF52_SERIES
284293
/// This is used to handle the case where an analog read happens in an IRQ interrupts one being done outside
@@ -728,6 +737,7 @@ void jshInit() {
728737
lastSystemTimeInv = ~lastSystemTime;
729738

730739
memset(pinStates, 0, sizeof(pinStates));
740+
memset(extiToPin, PIN_UNDEFINED, sizeof(extiToPin));
731741

732742
jshInitDevices();
733743
jshResetPeripherals();
@@ -1528,11 +1538,8 @@ void jshPinPulse(Pin pin, bool pulsePolarity, JsVarFloat pulseTime) {
15281538

15291539

15301540
static IOEventFlags jshGetEventFlagsForWatchedPin(nrf_drv_gpiote_pin_t pin) {
1531-
uint32_t addr = nrf_drv_gpiote_in_event_addr_get(pin);
1532-
// sigh. all because the right stuff isn't exported. All we wanted was channel_port_get
1533-
int i;
1534-
for (i=0;i<GPIOTE_CH_NUM;i++)
1535-
if (addr == nrf_gpiote_event_addr_get((nrf_gpiote_events_t)((uint32_t)NRF_GPIOTE_EVENTS_IN_0+(sizeof(uint32_t)*i))))
1541+
for (int i=0;i<EXTI_COUNT;i++)
1542+
if (pin == extiToPin[i])
15361543
return EV_EXTI0+i;
15371544
return EV_NONE;
15381545
}
@@ -1567,8 +1574,19 @@ IOEventFlags jshPinWatch(Pin pin, bool shouldWatch) {
15671574
cls_1_config.is_watcher = true; // stop this resetting the input state
15681575
nrf_drv_gpiote_in_init(p, &cls_1_config, jsvPinWatchHandler);
15691576
nrf_drv_gpiote_in_event_enable(p, true);
1570-
return jshGetEventFlagsForWatchedPin(p);
1577+
// allocate an 'EXTI'
1578+
for (int i=0;i<EXTI_COUNT;i++) {
1579+
if (extiToPin[i] == PIN_UNDEFINED) {
1580+
extiToPin[i] = p;
1581+
return EV_EXTI0+i;
1582+
}
1583+
}
1584+
jsWarn("No free EXTI for watch");
1585+
return EV_NONE;
15711586
} else {
1587+
for (int i=0;i<EXTI_COUNT;i++)
1588+
if (extiToPin[i] == p)
1589+
extiToPin[i] = PIN_UNDEFINED;
15721590
nrf_drv_gpiote_in_event_disable(p);
15731591
return EV_NONE;
15741592
}

0 commit comments

Comments
 (0)