From 3ac53c52b4ce8b403b9282ca831b4dda409bf610 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sun, 12 Apr 2020 01:51:54 -0700 Subject: [PATCH 1/9] Fix GPIOTE channel usage First, when looking for a channel, first loop through all mapped channels to see if the GPIO was already mapped. This prevents a single GPIO being configured for more than one channel at a time. Second, do not use a GPIOTE channel if the channel is already in use (e.g., by a library, sketch, or other code outside the Board Support Package). --- cores/nRF5/WInterrupts.c | 53 +++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index a03352f0..9591e096 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -21,6 +21,7 @@ #include "Arduino.h" #include "wiring_private.h" +#include "nrf_gpiote.h" #include @@ -95,24 +96,54 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) return 0; } + // All information for the configuration is known, except the prior values + // of the config register. Pre-compute the mask and new bits for later use. + // CONFIG[n] = (CONFIG[n] & oldRegMask) | newRegBits; + // + // Three fields are configured here: PORT/PIN, POLARITY, MODE + const uint32_t oldRegMask = ~(GPIOTE_CONFIG_PORT_PIN_Msk | GPIOTE_CONFIG_POLARITY_Msk | GPIOTE_CONFIG_MODE_Msk); + const uint32_t newRegBits = + ((pin << GPIOTE_CONFIG_PSEL_Pos ) & GPIOTE_CONFIG_PORT_PIN_Msk) | + ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk) | + ((GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos ) & GPIOTE_CONFIG_MODE_Msk ) ; + + // To avoid unexpected consequences, first loop through the channelMap + // to preferably re-use any channel that was already in use (even if + // earlier channel is no longer in use). for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { - if (channelMap[ch] == -1 || (uint32_t)channelMap[ch] == pin) { - channelMap[ch] = pin; + if ((uint32_t)channelMap[ch] == pin || channelMap[ch] == -1) { + // The pin is already allocated in the channelMap + // update the polarity (when events fire) and callbacks + uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; + tmp &= oldRegMask; + tmp |= newRegBits; + NRF_GPIOTE->CONFIG[ch] = tmp; + NRF_GPIOTE->INTENSET = (1 << ch); // old code did this ... no harm in ensuring this is set callbacksInt[ch] = callback; callbackDeferred[ch] = deferred; - - NRF_GPIOTE->CONFIG[ch] &= ~(GPIOTE_CONFIG_PORT_PIN_Msk | GPIOTE_CONFIG_POLARITY_Msk); - NRF_GPIOTE->CONFIG[ch] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PORT_PIN_Msk) | - ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk); - - NRF_GPIOTE->CONFIG[ch] |= GPIOTE_CONFIG_MODE_Event; - - NRF_GPIOTE->INTENSET = (1 << ch); - return (1 << ch); } } + // When the pin isn't already configured for interrupts, then attempt to + // find an unused GPIOTE channel. This code is identical to the above, + // except for also validating that the MODE bits show the channel is disabled. + for (int ch=0; ch < NUMBER_OF_GPIO_TE; ch++) { + // skip if already used in AttachInterrupt for another pin + if (channelMap[ch] != -1) continue; + // skip if channel is not disabled (e.g., in use by some other component or library) + if (nrf_gpiote_te_is_enabled(ch)) continue; + uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; + tmp &= oldRegMask; + tmp |= newRegBits; + // TODO: make check/set for new channel an atomic operation + NRF_GPIOTE->CONFIG[ch] = tmp; + NRF_GPIOTE->INTENSET = (1 << ch); + callbacksInt[ch] = callback; + callbackDeferred[ch] = deferred; + return (1 << ch); + } + // Else, pin was neither already setup, nor could a GPIOTE be allocated return 0; } From 0cb59145fdda7d3fecc7a640ae8d0b8b09715592 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sun, 12 Apr 2020 01:57:10 -0700 Subject: [PATCH 2/9] Reduce ISR overhead Reduce the number of cycles spent in the GPIOTE ISR, by reading common volatile registers across AHB only once, and using a safer hack to prevent spurious ISR firing when the last channel's event was cleared. --- cores/nRF5/WInterrupts.c | 64 +++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index 9591e096..0f5265f5 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -179,30 +179,58 @@ void GPIOTE_IRQHandler() SEGGER_SYSVIEW_RecordEnterISR(); #endif - uint32_t event = offsetof(NRF_GPIOTE_Type, EVENTS_IN[0]); - + // Read this once (not 8x), as it's a volatile read + // across the AHB, which adds up to 3 cycles. + uint32_t const enabledInterruptMask = NRF_GPIOTE->INTENSET; for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { - if ((*(uint32_t *)((uint32_t)NRF_GPIOTE + event) == 0x1UL) && (NRF_GPIOTE->INTENSET & (1 << ch))) { - if (channelMap[ch] != -1 && callbacksInt[ch]) { - if ( callbackDeferred[ch] ) { - // Adafruit defer callback to non-isr if configured so - ada_callback(NULL, 0, callbacksInt[ch]); - }else{ - callbacksInt[ch](); - } - } + // only process where the interrupt is enabled and the event register is set + // check interrupt enabled mask first, as already read that IOM value, to + // reduce delays from AHB (16MHz) reads. + if ( 0 == (enabledInterruptMask & (1 << ch))) continue; + if ( 0 == NRF_GPIOTE->EVENTS_IN[ch]) continue; - *(uint32_t *)((uint32_t)NRF_GPIOTE + event) = 0; -#if __CORTEX_M == 0x04 - volatile uint32_t dummy = *((volatile uint32_t *)((uint32_t)NRF_GPIOTE + event)); - (void)dummy; -#endif + // If the event was set and interrupts are enabled, + // call the callback function only if it exists, + // but ALWAYS clear the event to prevent an interrupt storm. + if (channelMap[ch] != -1 && callbacksInt[ch]) { + if ( callbackDeferred[ch] ) { + // Adafruit defer callback to non-isr if configured so + ada_callback(NULL, 0, callbacksInt[ch]); + } else { + callbacksInt[ch](); + } } - event = (uint32_t)((uint32_t)event + 4); + // clear the event + NRF_GPIOTE->EVENTS_IN[ch] = 0; } +#if __CORTEX_M == 0x04 + // To quote from nRF52840 product specification: + // -------------------------------------------------------------------------- + // Note: To avoid an interrupt reoccurring before a new event has come, + // the program should perform a read from one of the peripheral + // registers. For example, the event register that has been cleared, + // or the INTENCLR register that has been used to disable the + // interrupt. This will cause a one to three cycle delay and ensure + // the interrupt is cleared before exiting the interrupt handler. + // Care should be taken to ensure the compiler does not remove the read + // operation as an optimization. If the program can guarantee a four-cycle + // delay after event being cleared or interrupt disabled in any other way, + // then a read of a register is not required. + // -------------------------------------------------------------------------- + // + // Unfortunately, simply marking a variable as volatile is not enough to + // prevent GCC from reordering or combining memory accesses, without also + // having an explicit sequence point. + // See https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html + // + // Here, ensure a sequence point by adding a memory barrier + // followed with four nops before exiting the ISR + asm volatile ("" : : : "memory"); + __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); +#endif #if CFG_SYSVIEW - SEGGER_SYSVIEW_RecordExitISR(); + SEGGER_SYSVIEW_RecordExitISR(); #endif } From 9d47073cae3f4ac306897b8ddbcdb39469e66034 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Tue, 21 Apr 2020 19:53:15 -0700 Subject: [PATCH 3/9] fix obvious typo --- cores/nRF5/WInterrupts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index 0f5265f5..cc2ad9c6 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -132,7 +132,7 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) // skip if already used in AttachInterrupt for another pin if (channelMap[ch] != -1) continue; // skip if channel is not disabled (e.g., in use by some other component or library) - if (nrf_gpiote_te_is_enabled(ch)) continue; + if (nrf_gpiote_te_is_enabled(NRF_GPIOTE, ch)) continue; uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; tmp &= oldRegMask; tmp |= newRegBits; From 280ecaf5aa80848e2e3de64c3606e93da395475b Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Thu, 23 Apr 2020 15:01:11 -0700 Subject: [PATCH 4/9] Three bugs fixed * First loop should not be checking pinmap for -1. * Second loop should ensure no pending event when first allocating the GPIOTE channel. * Second loop needs to update pinmap with pin when allocated the GPIOTE channel. --- cores/nRF5/WInterrupts.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index cc2ad9c6..c7bd1f65 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -111,16 +111,18 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) // to preferably re-use any channel that was already in use (even if // earlier channel is no longer in use). for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { - if ((uint32_t)channelMap[ch] == pin || channelMap[ch] == -1) { + if ((uint32_t)channelMap[ch] == pin) { // The pin is already allocated in the channelMap // update the polarity (when events fire) and callbacks + // However, do NOT clear any GPIOTE events uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; + channelMap[ch] = pin; + callbacksInt[ch] = callback; + callbackDeferred[ch] = deferred; tmp &= oldRegMask; tmp |= newRegBits; NRF_GPIOTE->CONFIG[ch] = tmp; NRF_GPIOTE->INTENSET = (1 << ch); // old code did this ... no harm in ensuring this is set - callbacksInt[ch] = callback; - callbackDeferred[ch] = deferred; return (1 << ch); } } @@ -133,14 +135,17 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) if (channelMap[ch] != -1) continue; // skip if channel is not disabled (e.g., in use by some other component or library) if (nrf_gpiote_te_is_enabled(NRF_GPIOTE, ch)) continue; + // clear any old events on this GPIOTE channel + NRF_GPIOTE->EVENTS_IN[ch] = 0; uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; + channelMap[ch] = pin; + callbacksInt[ch] = callback; + callbackDeferred[ch] = deferred; tmp &= oldRegMask; tmp |= newRegBits; // TODO: make check/set for new channel an atomic operation NRF_GPIOTE->CONFIG[ch] = tmp; NRF_GPIOTE->INTENSET = (1 << ch); - callbacksInt[ch] = callback; - callbackDeferred[ch] = deferred; return (1 << ch); } // Else, pin was neither already setup, nor could a GPIOTE be allocated From cbbc57bd0d80be5204c58998a88495c96fb4e083 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Thu, 23 Apr 2020 16:15:09 -0700 Subject: [PATCH 5/9] Ensure CONFIG is fully cleared --- cores/nRF5/WInterrupts.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index c7bd1f65..1ceb8f96 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -165,14 +165,28 @@ void detachInterrupt(uint32_t pin) for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { if ((uint32_t)channelMap[ch] == pin) { + // Unfortunately, simply marking a variable as volatile is not enough to + // prevent GCC from reordering or combining memory accesses, without also + // having an explicit sequence point. + // See https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html + // + // Here, ensure a sequence point by adding a memory barrier + // followed with four nops after having disabled the interrupt, + // to ensure the peripheral registers have been updated. + NRF_GPIOTE->INTENCLR = (1 << ch); + asm volatile ("" : : : "memory"); + __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + + // now cleanup the rest of the use of the channel channelMap[ch] = -1; callbacksInt[ch] = NULL; callbackDeferred[ch] = false; - - NRF_GPIOTE->CONFIG[ch] &= ~GPIOTE_CONFIG_MODE_Event; - - NRF_GPIOTE->INTENCLR = (1 << ch); - + NRF_GPIOTE->EVENTS_IN[ch] = 0; // clear the event + // Finally, clear the CONFIG register only after ensure + // all the other state has been written to the peripheral registers + asm volatile ("" : : : "memory"); + __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + NRF_GPIOTE->CONFIG[ch] = 0; break; } } From 1c90673a64ba4161ca6b89263a2886f5a31eb249 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Thu, 23 Apr 2020 16:33:01 -0700 Subject: [PATCH 6/9] Ensure config is written before enabling the corresponding interrupt enable flag --- cores/nRF5/WInterrupts.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index 1ceb8f96..f7b53ed1 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -111,20 +111,23 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) // to preferably re-use any channel that was already in use (even if // earlier channel is no longer in use). for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { - if ((uint32_t)channelMap[ch] == pin) { - // The pin is already allocated in the channelMap - // update the polarity (when events fire) and callbacks - // However, do NOT clear any GPIOTE events - uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; - channelMap[ch] = pin; - callbacksInt[ch] = callback; - callbackDeferred[ch] = deferred; - tmp &= oldRegMask; - tmp |= newRegBits; - NRF_GPIOTE->CONFIG[ch] = tmp; - NRF_GPIOTE->INTENSET = (1 << ch); // old code did this ... no harm in ensuring this is set - return (1 << ch); - } + // skip if the channel was not already assigned to this pin + if ((uint32_t)channelMap[ch] != pin) continue; + + // The pin is already allocated in the channelMap + // update the polarity (when events fire) and callbacks + // However, do NOT clear any GPIOTE events + uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; + channelMap[ch] = pin; + callbacksInt[ch] = callback; + callbackDeferred[ch] = deferred; + tmp &= oldRegMask; + tmp |= newRegBits; + NRF_GPIOTE->CONFIG[ch] = tmp; + asm volatile ("" : : : "memory"); + __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + NRF_GPIOTE->INTENSET = (1 << ch); // old code did this ... no harm in ensuring this is set + return (1 << ch); } // When the pin isn't already configured for interrupts, then attempt to @@ -135,6 +138,7 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) if (channelMap[ch] != -1) continue; // skip if channel is not disabled (e.g., in use by some other component or library) if (nrf_gpiote_te_is_enabled(NRF_GPIOTE, ch)) continue; + // clear any old events on this GPIOTE channel NRF_GPIOTE->EVENTS_IN[ch] = 0; uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; @@ -145,6 +149,8 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) tmp |= newRegBits; // TODO: make check/set for new channel an atomic operation NRF_GPIOTE->CONFIG[ch] = tmp; + asm volatile ("" : : : "memory"); + __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); NRF_GPIOTE->INTENSET = (1 << ch); return (1 << ch); } From 48fda1899634a000004d221f908c5e494589cfb4 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Fri, 24 Apr 2020 11:59:53 -0700 Subject: [PATCH 7/9] one more edge case covered --- cores/nRF5/WInterrupts.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index f7b53ed1..5ca36600 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -140,7 +140,6 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) if (nrf_gpiote_te_is_enabled(NRF_GPIOTE, ch)) continue; // clear any old events on this GPIOTE channel - NRF_GPIOTE->EVENTS_IN[ch] = 0; uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; channelMap[ch] = pin; callbacksInt[ch] = callback; @@ -151,6 +150,9 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) NRF_GPIOTE->CONFIG[ch] = tmp; asm volatile ("" : : : "memory"); __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + NRF_GPIOTE->EVENTS_IN[ch] = 0; + asm volatile ("" : : : "memory"); + __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); NRF_GPIOTE->INTENSET = (1 << ch); return (1 << ch); } From c3c8ac3c8faf8d79f0b8102a7aa51cfe522b70eb Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sun, 26 Apr 2020 16:20:13 -0700 Subject: [PATCH 8/9] Updated per @hathach requested changes * Changed to use compiler intrinsics * Significantly reduced comments near memory barrier usage, as per request * Updating `attachInterrupt()` loops to more closely match requested style --- cores/nRF5/WInterrupts.c | 133 +++++++++++++++------------------------ 1 file changed, 50 insertions(+), 83 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index 5ca36600..c903a348 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -107,57 +107,52 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk) | ((GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos ) & GPIOTE_CONFIG_MODE_Msk ) ; - // To avoid unexpected consequences, first loop through the channelMap - // to preferably re-use any channel that was already in use (even if - // earlier channel is no longer in use). - for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { - // skip if the channel was not already assigned to this pin - if ((uint32_t)channelMap[ch] != pin) continue; + int ch = -1; + int newChannel = 0; - // The pin is already allocated in the channelMap - // update the polarity (when events fire) and callbacks - // However, do NOT clear any GPIOTE events - uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; - channelMap[ch] = pin; - callbacksInt[ch] = callback; - callbackDeferred[ch] = deferred; - tmp &= oldRegMask; - tmp |= newRegBits; - NRF_GPIOTE->CONFIG[ch] = tmp; - asm volatile ("" : : : "memory"); - __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); - NRF_GPIOTE->INTENSET = (1 << ch); // old code did this ... no harm in ensuring this is set - return (1 << ch); + // Find channel where pin is already assigned, if any + for (int i = 0; i < NUMBER_OF_GPIO_TE; i++) { + if ((uint32_t)channelMap[i] != pin) continue; + ch = i; + break; + } + // else, find one not already mapped and also not in use by others + if (ch == -1) { + for (int i = 0; i < NUMBER_OF_GPIO_TE; i++) { + if (channelMap[i] != -1) continue; + if (nrf_gpiote_te_is_enabled(NRF_GPIOTE, i)) continue; + + ch = i; + newChannel = 1; + break; + } + } + // if no channel found, exit + if (ch == -1) { + return 0; // no channel available } - // When the pin isn't already configured for interrupts, then attempt to - // find an unused GPIOTE channel. This code is identical to the above, - // except for also validating that the MODE bits show the channel is disabled. - for (int ch=0; ch < NUMBER_OF_GPIO_TE; ch++) { - // skip if already used in AttachInterrupt for another pin - if (channelMap[ch] != -1) continue; - // skip if channel is not disabled (e.g., in use by some other component or library) - if (nrf_gpiote_te_is_enabled(NRF_GPIOTE, ch)) continue; + uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; + tmp &= oldRegMask; + tmp |= newRegBits; // for existing channel, effectively updates only the polarity + channelMap[ch] = pin; // harmless for existing channel + callbacksInt[ch] = callback; // caller might be updating this for existing channel + callbackDeferred[ch] = deferred; // caller might be updating this for existing channel + NRF_GPIOTE->CONFIG[ch] = tmp; - // clear any old events on this GPIOTE channel - uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; - channelMap[ch] = pin; - callbacksInt[ch] = callback; - callbackDeferred[ch] = deferred; - tmp &= oldRegMask; - tmp |= newRegBits; - // TODO: make check/set for new channel an atomic operation - NRF_GPIOTE->CONFIG[ch] = tmp; - asm volatile ("" : : : "memory"); - __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + // For a new channel, additionally ensure no old events existed, and enable the interrupt + if (newChannel) { + // Why the memory barrier and NOPs? + // See page 4 at https://web.archive.org/web/20190823222657/http://infocenter.arm.com/help/topic/com.arm.doc.dai0321a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf + // and recall the AHB runs at 16MHz, so may take 4 cycles before peripheral register is actually updated. + __DSB(); __NOP();__NOP();__NOP();__NOP(); NRF_GPIOTE->EVENTS_IN[ch] = 0; - asm volatile ("" : : : "memory"); - __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + __DSB(); __NOP();__NOP();__NOP();__NOP(); NRF_GPIOTE->INTENSET = (1 << ch); - return (1 << ch); } - // Else, pin was neither already setup, nor could a GPIOTE be allocated - return 0; + + // Finally, indicate to caller the allocated / updated channel + return (1 << ch); } /* @@ -173,28 +168,19 @@ void detachInterrupt(uint32_t pin) for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { if ((uint32_t)channelMap[ch] == pin) { - // Unfortunately, simply marking a variable as volatile is not enough to - // prevent GCC from reordering or combining memory accesses, without also - // having an explicit sequence point. - // See https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html - // - // Here, ensure a sequence point by adding a memory barrier - // followed with four nops after having disabled the interrupt, - // to ensure the peripheral registers have been updated. + // Why the memory barrier and NOPs? + // See page 4 at https://web.archive.org/web/20190823222657/http://infocenter.arm.com/help/topic/com.arm.doc.dai0321a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf + // and recall the AHB runs at 16MHz, so may take 4 cycles before peripheral register is actually updated. NRF_GPIOTE->INTENCLR = (1 << ch); - asm volatile ("" : : : "memory"); - __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + __DSB(); __NOP();__NOP();__NOP();__NOP(); + NRF_GPIOTE->CONFIG[ch] = 0; + __DSB(); __NOP();__NOP();__NOP();__NOP(); + NRF_GPIOTE->EVENTS_IN[ch] = 0; // clear any final events // now cleanup the rest of the use of the channel channelMap[ch] = -1; callbacksInt[ch] = NULL; callbackDeferred[ch] = false; - NRF_GPIOTE->EVENTS_IN[ch] = 0; // clear the event - // Finally, clear the CONFIG register only after ensure - // all the other state has been written to the peripheral registers - asm volatile ("" : : : "memory"); - __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); - NRF_GPIOTE->CONFIG[ch] = 0; break; } } @@ -232,29 +218,10 @@ void GPIOTE_IRQHandler() NRF_GPIOTE->EVENTS_IN[ch] = 0; } #if __CORTEX_M == 0x04 - // To quote from nRF52840 product specification: - // -------------------------------------------------------------------------- - // Note: To avoid an interrupt reoccurring before a new event has come, - // the program should perform a read from one of the peripheral - // registers. For example, the event register that has been cleared, - // or the INTENCLR register that has been used to disable the - // interrupt. This will cause a one to three cycle delay and ensure - // the interrupt is cleared before exiting the interrupt handler. - // Care should be taken to ensure the compiler does not remove the read - // operation as an optimization. If the program can guarantee a four-cycle - // delay after event being cleared or interrupt disabled in any other way, - // then a read of a register is not required. - // -------------------------------------------------------------------------- - // - // Unfortunately, simply marking a variable as volatile is not enough to - // prevent GCC from reordering or combining memory accesses, without also - // having an explicit sequence point. - // See https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html - // - // Here, ensure a sequence point by adding a memory barrier - // followed with four nops before exiting the ISR - asm volatile ("" : : : "memory"); - __asm__ __volatile__ ("nop\n\tnop\n\tnop\n\tnop\n"); + // See note at nRF52840_PS_v1.1.pdf section 6.1.8 ("interrupt clearing") + // See also https://gcc.gnu.org/onlinedocs/gcc/Volatiles.html for why + // using memory barrier instead of read of an unrelated volatile + __DSB(); __NOP();__NOP();__NOP();__NOP(); #endif #if CFG_SYSVIEW From 55c1b9f53d016296ac119a195dbf3b23bed11d03 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Mon, 27 Apr 2020 11:25:54 -0700 Subject: [PATCH 9/9] Remove sequence points / barriers ... when programming the GPIOTE peripheral. the memory barrier is still used once in the ISR, to prevent spurious interrupts caused by delay writing over the (16MHz) AHB bus. --- cores/nRF5/WInterrupts.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index c903a348..373a9cf0 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -132,22 +132,18 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) return 0; // no channel available } - uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; - tmp &= oldRegMask; - tmp |= newRegBits; // for existing channel, effectively updates only the polarity channelMap[ch] = pin; // harmless for existing channel callbacksInt[ch] = callback; // caller might be updating this for existing channel callbackDeferred[ch] = deferred; // caller might be updating this for existing channel + + uint32_t tmp = NRF_GPIOTE->CONFIG[ch]; + tmp &= oldRegMask; + tmp |= newRegBits; // for existing channel, effectively updates only the polarity NRF_GPIOTE->CONFIG[ch] = tmp; // For a new channel, additionally ensure no old events existed, and enable the interrupt if (newChannel) { - // Why the memory barrier and NOPs? - // See page 4 at https://web.archive.org/web/20190823222657/http://infocenter.arm.com/help/topic/com.arm.doc.dai0321a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf - // and recall the AHB runs at 16MHz, so may take 4 cycles before peripheral register is actually updated. - __DSB(); __NOP();__NOP();__NOP();__NOP(); NRF_GPIOTE->EVENTS_IN[ch] = 0; - __DSB(); __NOP();__NOP();__NOP();__NOP(); NRF_GPIOTE->INTENSET = (1 << ch); } @@ -168,13 +164,8 @@ void detachInterrupt(uint32_t pin) for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { if ((uint32_t)channelMap[ch] == pin) { - // Why the memory barrier and NOPs? - // See page 4 at https://web.archive.org/web/20190823222657/http://infocenter.arm.com/help/topic/com.arm.doc.dai0321a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf - // and recall the AHB runs at 16MHz, so may take 4 cycles before peripheral register is actually updated. NRF_GPIOTE->INTENCLR = (1 << ch); - __DSB(); __NOP();__NOP();__NOP();__NOP(); NRF_GPIOTE->CONFIG[ch] = 0; - __DSB(); __NOP();__NOP();__NOP();__NOP(); NRF_GPIOTE->EVENTS_IN[ch] = 0; // clear any final events // now cleanup the rest of the use of the channel