From c315895cd96b5cfb185efd8ce71207575583b70a Mon Sep 17 00:00:00 2001 From: Neil McKechnie Date: Fri, 10 Feb 2023 19:52:24 +0000 Subject: [PATCH] Update I2CManager_NonBlocking.h Rework ATOMIC_BLOCK to further simplify and clarify. --- I2CManager_NonBlocking.h | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/I2CManager_NonBlocking.h b/I2CManager_NonBlocking.h index fb39fba..2511a8c 100644 --- a/I2CManager_NonBlocking.h +++ b/I2CManager_NonBlocking.h @@ -33,28 +33,35 @@ // This has the advantage over simple noInterrupts/Interrupts that the // original interrupt state is restored when the block finishes. // -#if defined(__AVR__) +// (This should really be defined in an include file somewhere more global, so +// it can replace use of noInterrupts/interrupts in other parts of DCC-EX. +// static inline uint8_t _deferInterrupts(void) { noInterrupts(); return 1; } -#define ATOMIC_BLOCK(x) \ -for (uint8_t _int_saved=SREG,_ToDo=_deferInterrupts(); \ - _ToDo; _ToDo=0, SREG=_int_saved) -#elif defined(__arm__) -static inline uint8_t _deferInterrupts(void) { - __set_PRIMASK(1); - return 1; +static inline void _enableInterruptsIf(uint8_t wasEnabled) { + if (wasEnabled) interrupts(); } -#define ATOMIC_BLOCK(x) \ -for (uint8_t _int_saved=__get_PRIMASK(),_ToDo=_deferInterrupts(); \ - _ToDo; _ToDo=0, __set_PRIMASK(_int_saved)) +#if defined(__AVR__) // Nano, Uno, Mega2580, NanoEvery, etc. + #define ATOMIC_BLOCK(x) \ + for (bool _int_saved=(SREG & (1<