1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-22 23:56:13 +01:00

Update I2CManager_NonBlocking.h

Reduce platform-specific part of ATOMIC_BLOCK definition to one inline function _getInterruptState()
This commit is contained in:
Neil McKechnie 2023-02-10 22:57:15 +00:00
parent e498915b28
commit a9971968c0

View File

@ -40,22 +40,23 @@ static inline uint8_t _deferInterrupts(void) {
noInterrupts(); noInterrupts();
return 1; return 1;
} }
static inline void _enableInterruptsIf(uint8_t wasEnabled) { static inline void _conditionalEnableInterrupts(bool *wasEnabled) {
if (wasEnabled) interrupts(); if (*wasEnabled) interrupts();
} }
#define ATOMIC_BLOCK(x) \
for (bool _int_saved __attribute__((__cleanup__(_conditionalEnableInterrupts))) \
=_getInterruptState(),_ToDo=_deferInterrupts(); _ToDo; _ToDo=0)
#if defined(__AVR__) // Nano, Uno, Mega2580, NanoEvery, etc. #if defined(__AVR__) // Nano, Uno, Mega2580, NanoEvery, etc.
#define ATOMIC_BLOCK(x) \ static inline bool _getInterruptState(void) {
for (bool _int_saved=(SREG & (1<<SREG_I)),_ToDo=_deferInterrupts(); \ return bitRead(SREG, SREG_I); // true if enabled, false if disabled
_ToDo; _ToDo=0, _enableInterruptsIf(_int_saved)) }
#elif defined(__arm__) // STM32, SAMD, Teensy #elif defined(__arm__) // STM32, SAMD, Teensy
static __inline__ bool _getInterruptState( void ) { static inline bool _getInterruptState( void ) {
uint32_t reg; uint32_t reg;
__asm__ __volatile__ ("MRS %0, primask" : "=r" (reg) ); __asm__ __volatile__ ("MRS %0, primask" : "=r" (reg) );
return !(reg & 1); // true if interrupts enabled, false otherwise return !(reg & 1); // true if interrupts enabled, false otherwise
} }
#define ATOMIC_BLOCK(x) \
for (bool _int_saved=_getInterruptState(),_ToDo=_deferInterrupts(); \
_ToDo; _ToDo=0, _enableInterruptsIf(_int_saved))
#else #else
#warning "ATOMIC_BLOCK() not defined for this target type, I2C interrupts disabled" #warning "ATOMIC_BLOCK() not defined for this target type, I2C interrupts disabled"
#define ATOMIC_BLOCK(x) // expand to nothing. #define ATOMIC_BLOCK(x) // expand to nothing.