2019-11-18 09:22:41 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Jeff Epler for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "supervisor/shared/tick.h"
|
2020-01-08 23:32:45 -05:00
|
|
|
|
2020-03-05 20:14:54 -05:00
|
|
|
#include "py/mpstate.h"
|
2020-01-08 23:32:45 -05:00
|
|
|
#include "supervisor/linker.h"
|
2019-11-18 09:22:41 -05:00
|
|
|
#include "supervisor/filesystem.h"
|
2020-05-11 09:37:20 -04:00
|
|
|
#include "supervisor/background_callback.h"
|
2020-03-05 20:14:54 -05:00
|
|
|
#include "supervisor/port.h"
|
2019-11-18 09:22:41 -05:00
|
|
|
#include "supervisor/shared/autoreload.h"
|
2020-07-13 11:33:58 -04:00
|
|
|
#include "supervisor/shared/stack.h"
|
2019-11-18 09:22:41 -05:00
|
|
|
|
2020-07-13 11:33:58 -04:00
|
|
|
#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
|
2019-11-18 09:22:41 -05:00
|
|
|
|
|
|
|
#if CIRCUITPY_GAMEPAD
|
|
|
|
#include "shared-module/gamepad/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CIRCUITPY_GAMEPADSHIFT
|
|
|
|
#include "shared-module/gamepadshift/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2020-07-13 11:33:58 -04:00
|
|
|
#if CIRCUITPY_NETWORK
|
|
|
|
#include "shared-module/network/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2019-11-18 09:22:41 -05:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
|
2020-05-22 06:52:21 -04:00
|
|
|
#if CIRCUITPY_WATCHDOG
|
|
|
|
#include "shared-bindings/watchdog/__init__.h"
|
|
|
|
#define WATCHDOG_EXCEPTION_CHECK() (MP_STATE_VM(mp_pending_exception) == &mp_watchdog_timeout_exception)
|
|
|
|
#else
|
|
|
|
#define WATCHDOG_EXCEPTION_CHECK() 0
|
|
|
|
#endif
|
|
|
|
|
2020-07-13 11:33:58 -04:00
|
|
|
static volatile uint64_t PLACE_IN_DTCM_BSS(background_ticks);
|
|
|
|
|
2020-07-20 09:45:31 -04:00
|
|
|
static background_callback_t tick_callback;
|
2020-07-09 10:47:10 -04:00
|
|
|
|
2020-07-13 11:33:58 -04:00
|
|
|
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();
|
|
|
|
}
|
2020-07-09 10:47:10 -04:00
|
|
|
|
2020-07-13 11:33:58 -04:00
|
|
|
bool supervisor_background_tasks_ok(void) {
|
|
|
|
return port_get_raw_ticks(NULL) - last_finished_tick < 1024;
|
2020-07-09 10:47:10 -04:00
|
|
|
}
|
|
|
|
|
2019-11-18 09:22:41 -05:00
|
|
|
void supervisor_tick(void) {
|
|
|
|
#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
|
|
|
|
filesystem_tick();
|
|
|
|
#endif
|
|
|
|
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
|
|
|
|
autoreload_tick();
|
|
|
|
#endif
|
|
|
|
#ifdef CIRCUITPY_GAMEPAD_TICKS
|
2020-03-06 21:15:16 -05:00
|
|
|
if (!(port_get_raw_ticks(NULL) & CIRCUITPY_GAMEPAD_TICKS)) {
|
2019-11-18 09:22:41 -05:00
|
|
|
#if CIRCUITPY_GAMEPAD
|
|
|
|
gamepad_tick();
|
|
|
|
#endif
|
|
|
|
#if CIRCUITPY_GAMEPADSHIFT
|
|
|
|
gamepadshift_tick();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
2020-07-20 09:45:31 -04:00
|
|
|
background_callback_add(&tick_callback, supervisor_background_tasks, NULL);
|
2019-11-18 09:22:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t supervisor_ticks_ms64() {
|
|
|
|
uint64_t result;
|
|
|
|
common_hal_mcu_disable_interrupts();
|
2020-03-06 21:15:16 -05:00
|
|
|
result = port_get_raw_ticks(NULL);
|
2019-11-18 09:22:41 -05:00
|
|
|
common_hal_mcu_enable_interrupts();
|
2020-03-05 20:14:54 -05:00
|
|
|
result = result * 1000 / 1024;
|
2019-11-18 09:22:41 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t supervisor_ticks_ms32() {
|
2020-03-05 20:14:54 -05:00
|
|
|
return supervisor_ticks_ms64();
|
2019-11-18 09:22:41 -05:00
|
|
|
}
|
run_background_tasks: Do nothing unless there has been a tick
This improves performance of running python code by 34%, based
on the "pystone" benchmark on metro m4 express at 5000 passes
(1127.65 -> 1521.6 passes/second).
In addition, by instrumenting the tick function and monitoring on an
oscilloscope, the time actually spent in run_background_tasks() on
the metro m4 decreases from average 43% to 0.5%. (however, there's
some additional overhead that is moved around and not accounted for
in that "0.5%" figure, each time supervisor_run_background_tasks_if_tick
is called but no tick has occurred)
On the CPB, it increases pystone from 633 to 769, a smaller percentage
increase of 21%. I did not measure the time actually spent in
run_background_tasks() on CPB.
Testing performed: on metro m4 and cpb, run pystone adapted from python3.4
(change time.time to time.monotonic for sub-second resolution)
Besides running a 5000 pass test, I also ran a 50-pass test while
scoping how long an output pin was set. Average: 34.59ms or 1445/s on m4,
67.61ms or 739/s on cbp, both matching the other pystone result reasonably
well.
import pystone
import board
import digitalio
import time
d = digitalio.DigitalInOut(board.D13)
d.direction = digitalio.Direction.OUTPUT
while True:
d.value = 0
time.sleep(.01)
d.value = 1
pystone.main(50)
2019-11-18 10:53:12 -05:00
|
|
|
|
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() {
|
2020-05-11 09:37:20 -04:00
|
|
|
background_callback_run_all();
|
2019-11-20 11:15:11 -05:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:14:54 -05:00
|
|
|
void mp_hal_delay_ms(mp_uint_t delay) {
|
2020-03-06 21:15:16 -05:00
|
|
|
uint64_t start_tick = port_get_raw_ticks(NULL);
|
2020-03-05 20:14:54 -05:00
|
|
|
// Adjust the delay to ticks vs ms.
|
|
|
|
delay = delay * 1024 / 1000;
|
2020-04-03 21:07:56 -04:00
|
|
|
uint64_t end_tick = start_tick + delay;
|
|
|
|
int64_t remaining = delay;
|
|
|
|
while (remaining > 0) {
|
2020-03-05 20:14:54 -05:00
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
// Check to see if we've been CTRL-Ced by autoreload or the user.
|
2020-06-21 14:28:26 -04:00
|
|
|
if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)))
|
2020-06-19 20:02:43 -04:00
|
|
|
{
|
2020-07-13 11:32:52 -04:00
|
|
|
// clear exception and generate stacktrace
|
2020-06-19 20:02:43 -04:00
|
|
|
MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
|
2020-06-23 13:15:01 -04:00
|
|
|
nlr_raise(&MP_STATE_VM(mp_kbd_exception));
|
2020-06-21 14:28:26 -04:00
|
|
|
}
|
|
|
|
if( MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception)) ||
|
|
|
|
WATCHDOG_EXCEPTION_CHECK()) {
|
2020-07-13 11:32:52 -04:00
|
|
|
// stop sleeping immediately
|
2020-03-05 20:14:54 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-04-03 21:07:56 -04:00
|
|
|
remaining = end_tick - port_get_raw_ticks(NULL);
|
|
|
|
// We break a bit early so we don't risk setting the alarm before the time when we call
|
|
|
|
// sleep.
|
|
|
|
if (remaining < 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
port_interrupt_after_ticks(remaining);
|
2020-03-05 20:14:54 -05:00
|
|
|
// Sleep until an interrupt happens.
|
|
|
|
port_sleep_until_interrupt();
|
2020-04-03 21:07:56 -04:00
|
|
|
remaining = end_tick - port_get_raw_ticks(NULL);
|
2020-03-06 21:15:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
volatile size_t tick_enable_count = 0;
|
|
|
|
extern void supervisor_enable_tick(void) {
|
2020-03-20 15:58:34 -04:00
|
|
|
common_hal_mcu_disable_interrupts();
|
2020-03-06 21:15:16 -05:00
|
|
|
if (tick_enable_count == 0) {
|
|
|
|
port_enable_tick();
|
|
|
|
}
|
|
|
|
tick_enable_count++;
|
2020-03-20 15:58:34 -04:00
|
|
|
common_hal_mcu_enable_interrupts();
|
2020-03-06 21:15:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern void supervisor_disable_tick(void) {
|
2020-03-20 15:58:34 -04:00
|
|
|
common_hal_mcu_disable_interrupts();
|
2020-03-13 19:13:24 -04:00
|
|
|
if (tick_enable_count > 0) {
|
|
|
|
tick_enable_count--;
|
|
|
|
}
|
2020-03-06 21:15:16 -05:00
|
|
|
if (tick_enable_count == 0) {
|
|
|
|
port_disable_tick();
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|
2020-03-20 15:58:34 -04:00
|
|
|
common_hal_mcu_enable_interrupts();
|
2020-03-05 20:14:54 -05:00
|
|
|
}
|