diff --git a/src/uClock.cpp b/src/uClock.cpp index 9e6e12f..74d1cc8 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -50,9 +50,22 @@ // #if defined(ARDUINO_ARCH_ESP32) || defined(ESP32) hw_timer_t * _uclockTimer = NULL; + portMUX_TYPE _uclockTimerMux = portMUX_INITIALIZER_UNLOCKED; #define TIMER_ID 0 #endif +// +// multicore archs +// +#if defined(ARDUINO_ARCH_ESP32) || defined(ESP32) + #define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux); +// +// singlecore archs +// +#else + #define ATOMIC(X) noInterrupts(); X; interrupts(); +#endif + #if defined(ARDUINO_ARCH_AVR) void uclockInitTimer() { @@ -73,7 +86,14 @@ void uclockInitTimer() ) } #else -void uclockISR(); + + // forward declaration of ISR + #if defined(ARDUINO_ARCH_ESP32) || defined(ESP32) + void ARDUINO_ISR_ATTR uclockISR(); + #else + void uclockISR(); + #endif + void uclockInitTimer() { // begin at 120bpm (20833us) @@ -501,6 +521,8 @@ volatile uint32_t _timer = 0; // #if defined(ARDUINO_ARCH_AVR) ISR(TIMER1_COMPA_vect) +#elif defined(ARDUINO_ARCH_ESP32) || defined(ESP32) +void ARDUINO_ISR_ATTR uclockISR() #else void uclockISR() #endif diff --git a/src/uClock.h b/src/uClock.h index cee7bd6..09b7a74 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -54,8 +54,6 @@ namespace umodular { namespace clock { #define SECS_PER_HOUR (3600UL) #define SECS_PER_DAY (SECS_PER_HOUR * 24L) -#define ATOMIC(X) noInterrupts(); X; interrupts(); - class uClockClass { private: