diff options
| author | hierophect <hierophect@gmail.com> | 2020-07-22 13:59:39 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-22 13:59:39 -0400 |
| commit | e232ec10ceb9c78df27cb4b5cee79079efd85d84 (patch) | |
| tree | 132c5bfbc1db02d29a82021be595eec1a1122ae8 /supervisor/shared/tick.c | |
| parent | 138189bad130569ac9a9b3051543b804db54ec6f (diff) | |
| parent | 05a10f7905bfb77f3d5dff2525c9d10375801329 (diff) | |
Merge branch 'main' into stm32-timer-allocator
Diffstat (limited to 'supervisor/shared/tick.c')
| -rw-r--r-- | supervisor/shared/tick.c | 65 |
1 files changed, 56 insertions, 9 deletions
diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c index dd7dba8f3..4af59f78e 100644 --- a/supervisor/shared/tick.c +++ b/supervisor/shared/tick.c @@ -29,10 +29,19 @@ #include "py/mpstate.h" #include "supervisor/linker.h" #include "supervisor/filesystem.h" +#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" @@ -42,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 @@ -51,6 +64,44 @@ 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 tick_callback; + +volatile uint64_t last_finished_tick = 0; + +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) { #if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0 filesystem_tick(); @@ -68,6 +119,7 @@ void supervisor_tick(void) { #endif } #endif + background_callback_add(&tick_callback, supervisor_background_tasks, NULL); } uint64_t supervisor_ticks_ms64() { @@ -83,14 +135,9 @@ uint32_t supervisor_ticks_ms32() { return supervisor_ticks_ms64(); } -extern void run_background_tasks(void); void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() { - // TODO: Add a global that can be set by anyone to indicate we should run background tasks. That - // way we can short circuit the background tasks early. We used to do it based on time but it - // breaks cases where we wake up for a short period and then sleep. If we skipped the last - // background task or more before sleeping we may end up starving a task like USB. - run_background_tasks(); + background_callback_run_all(); } void mp_hal_delay_ms(mp_uint_t delay) { @@ -104,13 +151,13 @@ void mp_hal_delay_ms(mp_uint_t delay) { // Check to see if we've been CTRL-Ced by autoreload or the user. if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) { - // clear exception and generate stacktrace + // clear exception and generate stacktrace MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; nlr_raise(&MP_STATE_VM(mp_kbd_exception)); } if( MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception)) || WATCHDOG_EXCEPTION_CHECK()) { - // stop sleeping immediately + // stop sleeping immediately break; } remaining = end_tick - port_get_raw_ticks(NULL); |
