diff --git a/ports/cc3200/hal/cc3200_hal.h b/ports/cc3200/hal/cc3200_hal.h index 9e57d39e2e..d07a9a1703 100644 --- a/ports/cc3200/hal/cc3200_hal.h +++ b/ports/cc3200/hal/cc3200_hal.h @@ -30,6 +30,10 @@ #include "hal/utils.h" #include "hal/systick.h" +// assembly functions to handle critical sections, interrupt +// disabling/enabling and sleep mode enter/exit +#include "cc3200_asm.h" + /****************************************************************************** DEFINE CONSTANTS ******************************************************************************/ @@ -55,6 +59,9 @@ " isb \n"); \ } +#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() +#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) + /****************************************************************************** DECLARE PUBLIC FUNCTIONS ******************************************************************************/ diff --git a/ports/cc3200/mods/pybrtc.c b/ports/cc3200/mods/pybrtc.c index e79fb29d00..340e86e158 100644 --- a/ports/cc3200/mods/pybrtc.c +++ b/ports/cc3200/mods/pybrtc.c @@ -25,7 +25,7 @@ * THE SOFTWARE. */ -#include "py/mpconfig.h" +#include "py/mphal.h" #include "py/obj.h" #include "py/runtime.h" #include "py/mperrno.h" diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index d9087efdd5..fe579c0874 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -161,14 +161,8 @@ typedef int32_t mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size typedef long mp_off_t; -#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() -#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) #define MICROPY_EVENT_POLL_HOOK __WFI(); -// assembly functions to handle critical sections, interrupt -// disabling/enabling and sleep mode enter/exit -#include "cc3200_asm.h" - // We need to provide a declaration/definition of alloca() #include diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 35943c47b6..ff6f6ab8d0 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -187,13 +187,6 @@ void *esp_native_code_commit(void *, size_t, void *); #define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc) #define MP_SSIZE_MAX (0x7fffffff) -// Note: these "critical nested" macros do not ensure cross-CPU exclusion, -// the only disable interrupts on the current CPU. To full manage exclusion -// one should use portENTER_CRITICAL/portEXIT_CRITICAL instead. -#include "freertos/FreeRTOS.h" -#define MICROPY_BEGIN_ATOMIC_SECTION() portSET_INTERRUPT_MASK_FROM_ISR() -#define MICROPY_END_ATOMIC_SECTION(state) portCLEAR_INTERRUPT_MASK_FROM_ISR(state) - #if MICROPY_PY_SOCKET_EVENTS #define MICROPY_PY_SOCKET_EVENTS_HANDLER extern void socket_events_handler(void); socket_events_handler(); #else diff --git a/ports/esp32/mphalport.h b/ports/esp32/mphalport.h index d77e2dd0df..9a7c39487d 100644 --- a/ports/esp32/mphalport.h +++ b/ports/esp32/mphalport.h @@ -63,6 +63,13 @@ void check_esp_err_(esp_err_t code); void check_esp_err_(esp_err_t code, const char *func, const int line, const char *file); #endif +// Note: these "critical nested" macros do not ensure cross-CPU exclusion, +// the only disable interrupts on the current CPU. To full manage exclusion +// one should use portENTER_CRITICAL/portEXIT_CRITICAL instead. +#include "freertos/FreeRTOS.h" +#define MICROPY_BEGIN_ATOMIC_SECTION() portSET_INTERRUPT_MASK_FROM_ISR() +#define MICROPY_END_ATOMIC_SECTION(state) portCLEAR_INTERRUPT_MASK_FROM_ISR(state) + uint32_t mp_hal_ticks_us(void); __attribute__((always_inline)) static inline uint32_t mp_hal_ticks_cpu(void) { uint32_t ccount; diff --git a/ports/esp8266/esp_mphal.h b/ports/esp8266/esp_mphal.h index b0351877e8..73a36a4b65 100644 --- a/ports/esp8266/esp_mphal.h +++ b/ports/esp8266/esp_mphal.h @@ -29,6 +29,9 @@ #include "shared/runtime/interrupt_char.h" #include "xtirq.h" +#define MICROPY_BEGIN_ATOMIC_SECTION() esp_disable_irq() +#define MICROPY_END_ATOMIC_SECTION(state) esp_enable_irq(state) + void mp_sched_keyboard_interrupt(void); struct _mp_print_t; @@ -66,6 +69,9 @@ uint32_t mp_hal_get_cpu_freq(void); void uart_task_init(); void dupterm_task_init(); +uint32_t esp_disable_irq(void); +void esp_enable_irq(uint32_t state); + void ets_event_poll(void); #define ETS_POLL_WHILE(cond) { while (cond) ets_event_poll(); } diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index ad9fefe0cd..07586a0ea5 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -127,9 +127,6 @@ #define MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_POLL #define MICROPY_VM_HOOK_RETURN MICROPY_VM_HOOK_POLL -#define MICROPY_BEGIN_ATOMIC_SECTION() esp_disable_irq() -#define MICROPY_END_ATOMIC_SECTION(state) esp_enable_irq(state) - // type definitions for the specific machine #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p))) @@ -173,6 +170,3 @@ extern const struct _mp_print_t mp_debug_print; #define WDEV_HWRNG ((volatile uint32_t *)0x3ff20e44) #define _assert(expr) ((expr) ? (void)0 : __assert_func(__FILE__, __LINE__, __func__, #expr)) - -uint32_t esp_disable_irq(void); -void esp_enable_irq(uint32_t state); diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 51f057ca82..5feb5b62b8 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -151,30 +151,8 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-mimxrt" #endif -// For regular code that wants to prevent "background tasks" from running. -// These background tasks (LWIP, Bluetooth) run in PENDSV context. -// TODO: Check for the settings of the STM32 port in irq.h -#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) -#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 15, 0) -#define MICROPY_PY_PENDSV_ENTER uint32_t atomic_state = raise_irq_pri(IRQ_PRI_PENDSV); -#define MICROPY_PY_PENDSV_REENTER atomic_state = raise_irq_pri(IRQ_PRI_PENDSV); -#define MICROPY_PY_PENDSV_EXIT restore_irq_pri(atomic_state); - // Hooks to add builtins -__attribute__((always_inline)) static inline void enable_irq(uint32_t state) { - __set_PRIMASK(state); -} - -__attribute__((always_inline)) static inline uint32_t disable_irq(void) { - uint32_t state = __get_PRIMASK(); - __disable_irq(); - return state; -} - -#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() -#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) - #if defined(IOMUX_TABLE_ENET) extern const struct _mp_obj_type_t network_lan_type; #define MICROPY_HW_NIC_ETH { MP_ROM_QSTR(MP_QSTR_LAN), MP_ROM_PTR(&network_lan_type) }, diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index 36eb387798..c4e5b052c9 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -36,6 +36,18 @@ #define MICROPY_HAL_VERSION "2.8.0" +#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq() +#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) + +// For regular code that wants to prevent "background tasks" from running. +// These background tasks (LWIP, Bluetooth) run in PENDSV context. +// TODO: Check for the settings of the STM32 port in irq.h +#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) +#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 15, 0) +#define MICROPY_PY_PENDSV_ENTER uint32_t at