diff --git a/main.c b/main.c index 201022f1af..ce95de95f0 100755 --- a/main.c +++ b/main.c @@ -101,8 +101,6 @@ void start_mp(supervisor_allocation* heap) { reset_status_led(); autoreload_stop(); - background_tasks_reset(); - // Stack limit should be less than real stack size, so we have a chance // to recover from limit hit. (Limit is measured in bytes.) mp_stack_ctrl_init(); diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c index 5c499ab3a8..62c233a3f8 100644 --- a/ports/atmel-samd/background.c +++ b/ports/atmel-samd/background.c @@ -39,59 +39,21 @@ #include "shared-module/displayio/__init__.h" #endif -volatile uint64_t last_finished_tick = 0; - -bool stack_ok_so_far = true; - -static bool running_background_tasks = false; - #ifdef MONITOR_BACKGROUND_TASKS // PB03 is physical pin "SCL" on the Metro M4 express // so you can't use this code AND an i2c peripheral // at the same time unless you change this -STATIC void start_background_task(void) { +void port_start_background_task(void) { REG_PORT_DIRSET1 = (1<<3); REG_PORT_OUTSET1 = (1<<3); } -STATIC void finish_background_task(void) { +void port_finish_background_task(void) { REG_PORT_OUTCLR1 = (1<<3); } #else -STATIC void start_background_task(void) {} -STATIC void finish_background_task(void) {} +void port_start_background_task(void) {} +void port_finish_background_task(void) {} #endif -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void run_background_tasks(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } - - start_background_task(); - - assert_heap_ok(); - running_background_tasks = true; - - #if CIRCUITPY_DISPLAYIO - displayio_background(); - #endif - - #if CIRCUITPY_NETWORK - network_module_background(); - #endif - filesystem_background(); - running_background_tasks = false; - assert_heap_ok(); - - last_finished_tick = port_get_raw_ticks(NULL); - finish_background_task(); -} - -bool background_tasks_ok(void) { - return port_get_raw_ticks(NULL) - last_finished_tick < 1024; -} +void port_background_task(void) {} diff --git a/ports/atmel-samd/background.h b/ports/atmel-samd/background.h index d9866a6abc..2a89c3b1b8 100644 --- a/ports/atmel-samd/background.h +++ b/ports/atmel-samd/background.h @@ -29,9 +29,4 @@ #include -void background_tasks_reset(void); -void run_background_tasks(void); -void run_background_vm_tasks(void); -bool background_tasks_ok(void); - #endif // MICROPY_INCLUDED_ATMEL_SAMD_BACKGROUND_H diff --git a/ports/atmel-samd/common-hal/frequencyio/FrequencyIn.c b/ports/atmel-samd/common-hal/frequencyio/FrequencyIn.c index 1952df5637..02d0482dca 100644 --- a/ports/atmel-samd/common-hal/frequencyio/FrequencyIn.c +++ b/ports/atmel-samd/common-hal/frequencyio/FrequencyIn.c @@ -46,6 +46,7 @@ #include "hpl_gclk_config.h" #include "shared-bindings/time/__init__.h" +#include "supervisor/shared/tick.h" #include "supervisor/shared/translate.h" #ifdef SAMD21 @@ -132,7 +133,7 @@ void frequencyin_interrupt_handler(uint8_t index) { } // Check if we've reached the upper limit of detection - if (!background_tasks_ok() || self->errored_too_fast) { + if (!supervisor_background_tasks_ok() || self->errored_too_fast) { self->errored_too_fast = true; frequencyin_emergency_cancel_capture(i); } diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.c b/ports/atmel-samd/common-hal/pulseio/PulseIn.c index b825579dbe..ae58b089de 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.c @@ -42,6 +42,7 @@ #include "samd/timers.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/pulseio/PulseIn.h" +#include "supervisor/shared/tick.h" #include "supervisor/shared/translate.h" // This timer is shared amongst all PulseIn objects as a higher resolution clock. @@ -87,7 +88,7 @@ void pulsein_interrupt_handler(uint8_t channel) { uint32_t current_count = tc->COUNT16.COUNT.reg; pulseio_pulsein_obj_t* self = get_eic_channel_data(channel); - if (!background_tasks_ok() || self->errored_too_fast) { + if (!supervisor_background_tasks_ok() || self->errored_too_fast) { self->errored_too_fast = true; common_hal_pulseio_pulsein_pause(self); return; diff --git a/ports/cxd56/background.c b/ports/cxd56/background.c index ade257dd24..6de6d7275b 100644 --- a/ports/cxd56/background.c +++ b/ports/cxd56/background.c @@ -30,24 +30,6 @@ #include "supervisor/filesystem.h" #include "supervisor/shared/stack.h" -static bool running_background_tasks = false; - -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void run_background_tasks(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } - - assert_heap_ok(); - running_background_tasks = true; - - usb_background(); - filesystem_background(); - - running_background_tasks = false; - assert_heap_ok(); -} +void port_background_task(void) {} +void port_start_background_task(void) {} +void port_finish_background_task(void) {} diff --git a/ports/cxd56/background.h b/ports/cxd56/background.h index a38e3faed4..5f76e64429 100644 --- a/ports/cxd56/background.h +++ b/ports/cxd56/background.h @@ -27,7 +27,4 @@ #ifndef MICROPY_INCLUDED_CXD56_BACKGROUND_H #define MICROPY_INCLUDED_CXD56_BACKGROUND_H -void background_tasks_reset(void); -void run_background_tasks(void); - #endif // MICROPY_INCLUDED_CXD56_BACKGROUND_H diff --git a/ports/esp32s2/background.c b/ports/esp32s2/background.c index a90fa7d0aa..40ce9ecfdf 100644 --- a/ports/esp32s2/background.c +++ b/ports/esp32s2/background.c @@ -35,27 +35,12 @@ #include "shared-module/displayio/__init__.h" #endif -static bool running_background_tasks = false; - -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void run_background_tasks(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } +void port_background_task(void) { // Zero delay in case FreeRTOS wants to switch to something else. vTaskDelay(0); - running_background_tasks = true; - filesystem_background(); - - #if CIRCUITPY_DISPLAYIO - displayio_background(); - #endif - running_background_tasks = false; - - assert_heap_ok(); } + +void port_start_background_task(void) {} + +void port_finish_background_task(void) {} diff --git a/ports/esp32s2/background.h b/ports/esp32s2/background.h index 0e1fb7a568..cb850d4e5a 100644 --- a/ports/esp32s2/background.h +++ b/ports/esp32s2/background.h @@ -29,7 +29,4 @@ #include -void background_tasks_reset(void); -void run_background_tasks(void); - #endif // MICROPY_INCLUDED_ESP32S2_BACKGROUND_H diff --git a/ports/litex/background.c b/ports/litex/background.c index 8c18970434..174d9588ac 100644 --- a/ports/litex/background.c +++ b/ports/litex/background.c @@ -29,32 +29,6 @@ #include "supervisor/usb.h" #include "supervisor/shared/stack.h" -#if CIRCUITPY_DISPLAYIO -#include "shared-module/displayio/__init__.h" -#endif - -static bool running_background_tasks = false; - -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void run_background_tasks(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } - running_background_tasks = true; - filesystem_background(); - - #if USB_AVAILABLE - usb_background(); - #endif - - #if CIRCUITPY_DISPLAYIO - displayio_background(); - #endif - running_background_tasks = false; - - assert_heap_ok(); -} +void port_background_task(void) {} +void port_start_background_task(void) {} +void port_finish_background_task(void) {} diff --git a/ports/litex/background.h b/ports/litex/background.h index 09551c7fbb..c80fbbe5cb 100644 --- a/ports/litex/background.h +++ b/ports/litex/background.h @@ -27,9 +27,4 @@ #ifndef MICROPY_INCLUDED_LITEX_BACKGROUND_H #define MICROPY_INCLUDED_LITEX_BACKGROUND_H -#include - -void background_tasks_reset(void); -void run_background_tasks(void); - #endif // MICROPY_INCLUDED_LITEX_BACKGROUND_H diff --git a/ports/mimxrt10xx/background.c b/ports/mimxrt10xx/background.c index ff53ea44f4..a8a613d41a 100644 --- a/ports/mimxrt10xx/background.c +++ b/ports/mimxrt10xx/background.c @@ -24,58 +24,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include "background.h" -//#include "audio_dma.h" -#include "supervisor/filesystem.h" -#include "supervisor/shared/tick.h" -#include "supervisor/usb.h" - -#include "py/runtime.h" -#include "shared-module/network/__init__.h" -#include "supervisor/linker.h" -#include "supervisor/shared/stack.h" - -#ifdef CIRCUITPY_DISPLAYIO -#include "shared-module/displayio/__init__.h" -#endif - -volatile uint64_t last_finished_tick = 0; - -bool stack_ok_so_far = true; - -static bool running_background_tasks = false; - -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void PLACE_IN_ITCM(run_background_tasks)(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } - assert_heap_ok(); - running_background_tasks = true; +#include "supervisor/port.h" +void port_background_task(void) { #if CIRCUITPY_AUDIOIO || CIRCUITPY_AUDIOBUSIO audio_dma_background(); #endif - #if CIRCUITPY_DISPLAYIO - displayio_background(); - #endif - - #if CIRCUITPY_NETWORK - network_module_background(); - #endif - filesystem_background(); - usb_background(); - running_background_tasks = false; - assert_heap_ok(); - - last_finished_tick = supervisor_ticks_ms64(); -} - -bool background_tasks_ok(void) { - return supervisor_ticks_ms64() - last_finished_tick < 1000; } +void port_start_background_task(void) {} +void port_finish_background_task(void) {} diff --git a/ports/mimxrt10xx/background.h b/ports/mimxrt10xx/background.h index 52789d0389..a3fe102acc 100644 --- a/ports/mimxrt10xx/background.h +++ b/ports/mimxrt10xx/background.h @@ -28,11 +28,4 @@ #ifndef MICROPY_INCLUDED_MIMXRT10XX_BACKGROUND_H #define MICROPY_INCLUDED_MIMXRT10XX_BACKGROUND_H -#include - -void background_tasks_reset(void); -void run_background_tasks(void); -void run_background_vm_tasks(void); -bool background_tasks_ok(void); - #endif // MICROPY_INCLUDED_MIMXRT10XX_BACKGROUND_H diff --git a/ports/mimxrt10xx/common-hal/pulseio/PulseIn.c b/ports/mimxrt10xx/common-hal/pulseio/PulseIn.c index d8bf2017ea..ec02908612 100644 --- a/ports/mimxrt10xx/common-hal/pulseio/PulseIn.c +++ b/ports/mimxrt10xx/common-hal/pulseio/PulseIn.c @@ -64,7 +64,7 @@ // // last ms. // current_us = 1000 - current_us; // pulseio_pulsein_obj_t* self = get_eic_channel_data(channel); -// if (!background_tasks_ok() || self->errored_too_fast) { +// if (!supervisor_background_tasks_ok() || self->errored_too_fast) { // self->errored_too_fast = true; // common_hal_pulseio_pulsein_pause(self); // return; diff --git a/ports/nrf/background.c b/ports/nrf/background.c index bc7c0d7d32..10543ddb21 100644 --- a/ports/nrf/background.c +++ b/ports/nrf/background.c @@ -46,35 +46,14 @@ #include "common-hal/_bleio/bonding.h" #endif -static bool running_background_tasks = false; +void port_start_background_task(void) {} +void port_finish_background_task(void) {} -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void run_background_tasks(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } - running_background_tasks = true; - filesystem_background(); +void port_background_task(void) { #if CIRCUITPY_AUDIOPWMIO audiopwmout_background(); #endif #if CIRCUITPY_AUDIOBUSIO i2s_background(); #endif - -#if CIRCUITPY_BLEIO - supervisor_bluetooth_background(); - bonding_background(); -#endif - - #if CIRCUITPY_DISPLAYIO - displayio_background(); - #endif - running_background_tasks = false; - - assert_heap_ok(); } diff --git a/ports/nrf/background.h b/ports/nrf/background.h index d53681c0fd..64a768cf9b 100644 --- a/ports/nrf/background.h +++ b/ports/nrf/background.h @@ -27,9 +27,4 @@ #ifndef MICROPY_INCLUDED_NRF_BACKGROUND_H #define MICROPY_INCLUDED_NRF_BACKGROUND_H -#include - -void background_tasks_reset(void); -void run_background_tasks(void); - #endif // MICROPY_INCLUDED_NRF_BACKGROUND_H diff --git a/ports/stm/background.c b/ports/stm/background.c index 8c18970434..d83a0ccec7 100644 --- a/ports/stm/background.c +++ b/ports/stm/background.c @@ -33,28 +33,6 @@ #include "shared-module/displayio/__init__.h" #endif -static bool running_background_tasks = false; - -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void run_background_tasks(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } - running_background_tasks = true; - filesystem_background(); - - #if USB_AVAILABLE - usb_background(); - #endif - - #if CIRCUITPY_DISPLAYIO - displayio_background(); - #endif - running_background_tasks = false; - - assert_heap_ok(); -} +void port_background_task(void) {} +void port_start_background_task(void) {} +void port_finish_background_task(void) {} diff --git a/ports/stm/background.h b/ports/stm/background.h index 6225429f89..e57aa40dd7 100644 --- a/ports/stm/background.h +++ b/ports/stm/background.h @@ -27,9 +27,4 @@ #ifndef MICROPY_INCLUDED_STM32_BACKGROUND_H #define MICROPY_INCLUDED_STM32_BACKGROUND_H -#include - -void background_tasks_reset(void); -void run_background_tasks(void); - #endif // MICROPY_INCLUDED_STM32_BACKGROUND_H diff --git a/supervisor/port.h b/supervisor/port.h index 8a12d34c8a..ad5b3cf32a 100644 --- a/supervisor/port.h +++ b/supervisor/port.h @@ -91,4 +91,12 @@ void port_interrupt_after_ticks(uint32_t ticks); // Sleep the CPU until an interrupt is received. void port_sleep_until_interrupt(void); +// Execute port specific actions during background tasks. +void port_background_task(void); + +// Take port specific actions at the beginning and end of background tasks. +// This is used e.g., to set a monitoring pin for debug purposes. "Actual +// work" should be done in port_background_task() instead. +void port_start_background_task(void); +void port_finish_background_task(void); #endif // MICROPY_INCLUDED_SUPERVISOR_PORT_H diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c index 46172d0f30..bc270030f3 100644 --- a/supervisor/shared/tick.c +++ b/supervisor/shared/tick.c @@ -32,8 +32,16 @@ #include "supervisor/background_callback.h" #include "supervisor/port.h" #include "supervisor/shared/autoreload.h" +#include "supervisor/shared/stack.h" -static volatile uint64_t PLACE_IN_DTCM_BSS(background_ticks); +#if CIRCUITPY_BLEIO +#include "supervisor/shared/bluetooth.h" +#include "common-hal/_bleio/bonding.h" +#endif + +#if CIRCUITPY_DISPLAYIO +#include "shared-module/displayio/__init__.h" +#endif #if CIRCUITPY_GAMEPAD #include "shared-module/gamepad/__init__.h" @@ -43,6 +51,10 @@ static volatile uint64_t PLACE_IN_DTCM_BSS(background_ticks); #include "shared-module/gamepadshift/__init__.h" #endif +#if CIRCUITPY_NETWORK +#include "shared-module/network/__init__.h" +#endif + #include "shared-bindings/microcontroller/__init__.h" #if CIRCUITPY_WATCHDOG @@ -52,12 +64,42 @@ static volatile uint64_t PLACE_IN_DTCM_BSS(background_ticks); #define WATCHDOG_EXCEPTION_CHECK() 0 #endif +static volatile uint64_t PLACE_IN_DTCM_BSS(background_ticks); + static background_callback_t callback; -extern void run_background_tasks(void); +volatile uint64_t last_finished_tick = 0; -void background_task_tick(void *unused) { - run_background_tasks(); +void supervisor_background_tasks(void *unused) { + port_start_background_task(); + + assert_heap_ok(); + + #if CIRCUITPY_DISPLAYIO + displayio_background(); + #endif + + #if CIRCUITPY_NETWORK + network_module_background(); + #endif + filesystem_background(); + + #if CIRCUITPY_BLEIO + supervisor_bluetooth_background(); + bonding_background(); + #endif + + port_background_task(); + + assert_heap_ok(); + + last_finished_tick = port_get_raw_ticks(NULL); + + port_finish_background_task(); +} + +bool supervisor_background_tasks_ok(void) { + return port_get_raw_ticks(NULL) - last_finished_tick < 1024; } void supervisor_tick(void) { @@ -77,7 +119,7 @@ void supervisor_tick(void) { #endif } #endif - background_callback_add(&callback, background_task_tick, NULL); + background_callback_add(&callback, supervisor_background_tasks, NULL); } uint64_t supervisor_ticks_ms64() { diff --git a/supervisor/shared/tick.h b/supervisor/shared/tick.h index e7e8080581..3a01bd6222 100644 --- a/supervisor/shared/tick.h +++ b/supervisor/shared/tick.h @@ -28,6 +28,7 @@ #define __INCLUDED_SUPERVISOR_TICK_H #include +#include /** @brief To be called once every ms * @@ -36,13 +37,6 @@ * interrupt context. */ extern void supervisor_tick(void); -/** @brief Cause background tasks to be called soon - * - * Normally, background tasks are only run once per tick. For other cases where - * an event noticed from an interrupt context needs to be completed by a background - * task activity, the interrupt can call supervisor_fake_tick. - */ -extern void supervisor_fake_tick(void); /** @brief Get the lower 32 bits of the time in milliseconds * * This can be more efficient than supervisor_ticks_ms64, for sites where a wraparound @@ -67,4 +61,12 @@ extern void supervisor_run_background_if_tick(void); extern void supervisor_enable_tick(void); extern void supervisor_disable_tick(void); +/** + * @brief Return true if tick-based background tasks ran within the last 1s + * + * Note that when ticks are not enabled, this function can return false; this is + * intended. + */ +extern bool supervisor_background_tasks_ok(void); + #endif