From f97252b93a8962b4f6acf8fa254f3602a8e274a4 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 26 Oct 2022 16:41:51 +1300 Subject: [PATCH] Implement InterruptLock for RP2040 (#3945) --- esphome/core/helpers.cpp | 7 +++++-- esphome/core/helpers.h | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 48ef406a81..53bb6bfd79 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -392,13 +392,16 @@ void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green // System APIs #if defined(USE_ESP8266) -IRAM_ATTR InterruptLock::InterruptLock() { xt_state_ = xt_rsil(15); } -IRAM_ATTR InterruptLock::~InterruptLock() { xt_wsr_ps(xt_state_); } +IRAM_ATTR InterruptLock::InterruptLock() { state_ = xt_rsil(15); } +IRAM_ATTR InterruptLock::~InterruptLock() { xt_wsr_ps(state_); } #elif defined(USE_ESP32) // only affects the executing core // so should not be used as a mutex lock, only to get accurate timing IRAM_ATTR InterruptLock::InterruptLock() { portDISABLE_INTERRUPTS(); } IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); } +#elif defined(USE_RP2040) +IRAM_ATTR InterruptLock::InterruptLock() { state_ = save_and_disable_interrupts(); } +IRAM_ATTR InterruptLock::~InterruptLock() { restore_interrupts(state_); } #endif uint8_t HighFrequencyLoopRequester::num_requests = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 1459f0ef55..6f3b5805cf 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -542,8 +542,8 @@ class InterruptLock { ~InterruptLock(); protected: -#ifdef USE_ESP8266 - uint32_t xt_state_; +#if defined(USE_ESP8266) || defined(USE_RP2040) + uint32_t state_; #endif };