From 7d7d29dbe29df909616094bbf0ec047401f8ab36 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 10 Nov 2021 13:59:09 +0100 Subject: [PATCH] mimxrt: Fix mp_hal_quiet_timing_enter()/exit() so timer still runs. The initial code disabled IRQs, which caused the us-ticks timer to stop. The change here changes the priotity level, such that the timer still runs. --- ports/mimxrt/mpconfigport.h | 17 +++++++++++++++++ ports/mimxrt/mphalport.h | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 64e991c799..361b8bd089 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -189,6 +189,23 @@ __attribute__((always_inline)) static inline uint32_t disable_irq(void) { return state; } +static inline uint32_t raise_irq_pri(uint32_t pri) { + uint32_t basepri = __get_BASEPRI(); + // If non-zero, the processor does not process any exception with a + // priority value greater than or equal to BASEPRI. + // When writing to BASEPRI_MAX the write goes to BASEPRI only if either: + // - Rn is non-zero and the current BASEPRI value is 0 + // - Rn is non-zero and less than the current BASEPRI value + pri <<= (8 - __NVIC_PRIO_BITS); + __ASM volatile ("msr basepri_max, %0" : : "r" (pri) : "memory"); + return basepri; +} + +// "basepri" should be the value returned from raise_irq_pri +static inline void restore_irq_pri(uint32_t basepri) { + __set_BASEPRI(basepri); +} + #define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() #define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index c02f561a9d..0dc49f01a5 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -53,8 +53,8 @@ extern ringbuf_t stdin_ringbuf; #define mp_hal_pin_od_low(p) mp_hal_pin_low(p) #define mp_hal_pin_od_high(p) mp_hal_pin_high(p) -#define mp_hal_quiet_timing_enter() MICROPY_BEGIN_ATOMIC_SECTION() -#define mp_hal_quiet_timing_exit(irq_state) MICROPY_END_ATOMIC_SECTION(irq_state) +#define mp_hal_quiet_timing_enter() raise_irq_pri(1) +#define mp_hal_quiet_timing_exit(irq_state) restore_irq_pri(irq_state) void mp_hal_set_interrupt_char(int c);