summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2019-11-18 08:22:41 -0600
committerJeff Epler <jepler@gmail.com>2019-11-18 11:01:23 -0600
commit7f744a2369a5036cadfa05575da1704f709c98bb (patch)
tree62b0c1d21d75910076a6c4b8d11a0c55861267aa /ports
parent45d1b290ee13ee4080e9718909c33053583314b8 (diff)
Supervisor: move most of systick to the supervisor
This code is shared by most parts, except where not all the #ifdefs inside the tick function were present in all ports. This mostly would have broken gamepad tick support on non-samd ports. The "ms32" and "ms64" variants of the tick functions are introduced because there is no 64-bit atomic read. Disabling interrupts avoids a low probability bug where milliseconds could be off by ~49.5 days once every ~49.5 days (2^32 ms). Avoiding disabling interrupts when only the low 32 bits are needed is a minor optimization. Testing performed: on metro m4 express, USB still works and time.monotonic_ns() still counts up
Diffstat (limited to 'ports')
-rw-r--r--ports/atmel-samd/background.c5
-rw-r--r--ports/atmel-samd/common-hal/busio/UART.c13
-rw-r--r--ports/atmel-samd/common-hal/time/__init__.c4
-rw-r--r--ports/atmel-samd/mphalport.c6
-rw-r--r--ports/atmel-samd/mphalport.h6
-rw-r--r--ports/atmel-samd/tick.c36
-rw-r--r--ports/atmel-samd/tick.h2
-rw-r--r--ports/cxd56/common-hal/time/__init__.c4
-rw-r--r--ports/cxd56/mphalport.c6
-rw-r--r--ports/cxd56/mphalport.h2
-rw-r--r--ports/cxd56/tick.c15
-rw-r--r--ports/cxd56/tick.h2
-rw-r--r--ports/nrf/common-hal/_bleio/Adapter.c3
-rw-r--r--ports/nrf/common-hal/_bleio/CharacteristicBuffer.c5
-rw-r--r--ports/nrf/common-hal/busio/UART.c12
-rw-r--r--ports/nrf/common-hal/time/__init__.c2
-rw-r--r--ports/nrf/mphalport.c5
-rw-r--r--ports/nrf/mphalport.h5
-rw-r--r--ports/nrf/tick.c33
-rw-r--r--ports/nrf/tick.h2
-rw-r--r--ports/stm32f4/common-hal/time/__init__.c2
-rw-r--r--ports/stm32f4/mphalport.c6
-rw-r--r--ports/stm32f4/mphalport.h4
-rw-r--r--ports/stm32f4/tick.c32
-rw-r--r--ports/stm32f4/tick.h2
25 files changed, 71 insertions, 143 deletions
diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c
index 386ba0715..3b698768b 100644
--- a/ports/atmel-samd/background.c
+++ b/ports/atmel-samd/background.c
@@ -28,6 +28,7 @@
#include "audio_dma.h"
#include "tick.h"
#include "supervisor/filesystem.h"
+#include "supervisor/shared/tick.h"
#include "supervisor/usb.h"
#include "py/runtime.h"
@@ -71,9 +72,9 @@ void run_background_tasks(void) {
running_background_tasks = false;
assert_heap_ok();
- last_finished_tick = ticks_ms;
+ last_finished_tick = supervisor_ticks_ms64();
}
bool background_tasks_ok(void) {
- return ticks_ms - last_finished_tick < 1000;
+ return supervisor_ticks_ms64() - last_finished_tick < 1000;
}
diff --git a/ports/atmel-samd/common-hal/busio/UART.c b/ports/atmel-samd/common-hal/busio/UART.c
index 2505e894a..42493a6b6 100644
--- a/ports/atmel-samd/common-hal/busio/UART.c
+++ b/ports/atmel-samd/common-hal/busio/UART.c
@@ -34,8 +34,7 @@
#include "py/runtime.h"
#include "py/stream.h"
#include "supervisor/shared/translate.h"
-
-#include "tick.h"
+#include "supervisor/shared/tick.h"
#include "hpl_sercom_config.h"
#include "peripheral_clk_config.h"
@@ -272,10 +271,10 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
usart_async_get_io_descriptor(usart_desc_p, &io);
size_t total_read = 0;
- uint64_t start_ticks = ticks_ms;
+ uint64_t start_ticks = supervisor_ticks_ms64();
// Busy-wait until timeout or until we've read enough chars.
- while (ticks_ms - start_ticks <= self->timeout_ms) {
+ while (supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) {
// Read as many chars as we can right now, up to len.
size_t num_read = io_read(io, data, len);
@@ -289,7 +288,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
}
if (num_read > 0) {
// Reset the timeout on every character read.
- start_ticks = ticks_ms;
+ start_ticks = supervisor_ticks_ms64();
}
RUN_BACKGROUND_TASKS;
// Allow user to break out of a timeout with a KeyboardInterrupt.
@@ -330,9 +329,9 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
// Wait until write is complete or timeout.
bool done = false;
- uint64_t start_ticks = ticks_ms;
+ uint64_t start_ticks = supervisor_ticks_ms64();
// Busy-wait for timeout.
- while (ticks_ms - start_ticks < self->timeout_ms) {
+ while (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) {
if (usart_async_is_tx_empty(usart_desc_p)) {
done = true;
break;
diff --git a/ports/atmel-samd/common-hal/time/__init__.c b/ports/atmel-samd/common-hal/time/__init__.c
index 0d60adef2..2d82b3d1a 100644
--- a/ports/atmel-samd/common-hal/time/__init__.c
+++ b/ports/atmel-samd/common-hal/time/__init__.c
@@ -28,10 +28,10 @@
#include "shared-bindings/time/__init__.h"
-#include "tick.h"
+#include "supervisor/shared/tick.h"
inline uint64_t common_hal_time_monotonic() {
- return ticks_ms;
+ return supervisor_ticks_ms64();
}
void common_hal_time_delay_ms(uint32_t delay) {
diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c
index 957bf6073..96433d729 100644
--- a/ports/atmel-samd/mphalport.c
+++ b/ports/atmel-samd/mphalport.c
@@ -45,12 +45,12 @@
#include "mpconfigboard.h"
#include "mphalport.h"
#include "reset.h"
-#include "tick.h"
+#include "supervisor/shared/tick.h"
extern uint32_t common_hal_mcu_processor_get_frequency(void);
void mp_hal_delay_ms(mp_uint_t delay) {
- uint64_t start_tick = ticks_ms;
+ uint64_t start_tick = supervisor_ticks_ms64();
uint64_t duration = 0;
while (duration < delay) {
RUN_BACKGROUND_TASKS;
@@ -59,7 +59,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
break;
}
- duration = (ticks_ms - start_tick);
+ duration = (supervisor_ticks_ms64() - start_tick);
// TODO(tannewt): Go to sleep for a little while while we wait.
}
}
diff --git a/ports/atmel-samd/mphalport.h b/ports/atmel-samd/mphalport.h
index 64269b201..8a762e258 100644
--- a/ports/atmel-samd/mphalport.h
+++ b/ports/atmel-samd/mphalport.h
@@ -31,11 +31,11 @@
#include "lib/oofatfs/ff.h"
-// Global millisecond tick count (driven by SysTick interrupt).
-extern volatile uint64_t ticks_ms;
+#include "supervisor/shared/tick.h"
+// Global millisecond tick count (driven by SysTick interrupt).
static inline mp_uint_t mp_hal_ticks_ms(void) {
- return ticks_ms;
+ return supervisor_ticks_ms32();
}
// Number of bytes in receive buffer
volatile uint8_t usb_rx_count;
diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c
index 4d7bb9dca..f996440ae 100644
--- a/ports/atmel-samd/tick.c
+++ b/ports/atmel-samd/tick.c
@@ -28,47 +28,21 @@
#include "peripheral_clk_config.h"
-#include "supervisor/shared/autoreload.h"
-#include "supervisor/filesystem.h"
+#include "supervisor/shared/tick.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Processor.h"
-#if CIRCUITPY_GAMEPAD
-#include "shared-module/gamepad/__init__.h"
-#endif
-
-#if CIRCUITPY_GAMEPADSHIFT
-#include "shared-module/gamepadshift/__init__.h"
-#endif
-// Global millisecond tick count
-volatile uint64_t ticks_ms = 0;
-
void SysTick_Handler(void) {
// SysTick interrupt handler called when the SysTick timer reaches zero
// (every millisecond).
common_hal_mcu_disable_interrupts();
- ticks_ms += 1;
// Read the control register to reset the COUNTFLAG.
(void) SysTick->CTRL;
common_hal_mcu_enable_interrupts();
-#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
- filesystem_tick();
-#endif
-#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
- autoreload_tick();
-#endif
-#ifdef CIRCUITPY_GAMEPAD_TICKS
- if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
- #if CIRCUITPY_GAMEPAD
- gamepad_tick();
- #endif
- #if CIRCUITPY_GAMEPADSHIFT
- gamepadshift_tick();
- #endif
- }
-#endif
+ // Do things common to all ports when the tick occurs
+ supervisor_tick();
}
void tick_init() {
@@ -115,7 +89,7 @@ void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
uint32_t tick_status = SysTick->CTRL;
uint32_t current_us = SysTick->VAL;
uint32_t tick_status2 = SysTick->CTRL;
- uint64_t current_ms = ticks_ms;
+ uint64_t current_ms = supervisor_ticks_ms64();
// The second clause ensures our value actually rolled over. Its possible it hit zero between
// the VAL read and CTRL read.
if ((tick_status & SysTick_CTRL_COUNTFLAG_Msk) != 0 ||
@@ -129,5 +103,5 @@ void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
void wait_until(uint64_t ms, uint32_t us_until_ms) {
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
- while (ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
+ while (supervisor_ticks_ms64() <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
}
diff --git a/ports/atmel-samd/tick.h b/ports/atmel-samd/tick.h
index c8c8d739a..334352df2 100644
--- a/ports/atmel-samd/tick.h
+++ b/ports/atmel-samd/tick.h
@@ -28,8 +28,6 @@
#include "py/mpconfig.h"
-extern volatile uint64_t ticks_ms;
-
extern struct timer_descriptor ms_timer;
void tick_init(void);
diff --git a/ports/cxd56/common-hal/time/__init__.c b/ports/cxd56/common-hal/time/__init__.c
index 8f7326b62..6f5eedd41 100644
--- a/ports/cxd56/common-hal/time/__init__.c
+++ b/ports/cxd56/common-hal/time/__init__.c
@@ -26,10 +26,10 @@
#include "py/mphal.h"
-#include "tick.h"
+#include "supervisor/shared/tick.h"
uint64_t common_hal_time_monotonic(void) {
- return ticks_ms;
+ return supervisor_ticks_ms64();
}
void common_hal_time_delay_ms(uint32_t delay) {
diff --git a/ports/cxd56/mphalport.c b/ports/cxd56/mphalport.c
index 79d93f975..1305706ca 100644
--- a/ports/cxd56/mphalport.c
+++ b/ports/cxd56/mphalport.c
@@ -31,7 +31,7 @@
#include "py/mpstate.h"
-#include "tick.h"
+#include "supervisor/shared/tick.h"
#define DELAY_CORRECTION (700)
#define DELAY_INTERVAL (50)
@@ -57,7 +57,7 @@ mp_uint_t mp_hal_ticks_cpu(void) {
}
void mp_hal_delay_ms(mp_uint_t delay) {
- uint64_t start_tick = ticks_ms;
+ uint64_t start_tick = supervisor_ticks_ms64();
uint64_t duration = 0;
while (duration < delay) {
#ifdef MICROPY_VM_HOOK_LOOP
@@ -68,7 +68,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
break;
}
- duration = (ticks_ms - start_tick);
+ duration = (supervisor_ticks_ms64() - start_tick);
// TODO(tannewt): Go to sleep for a little while while we wait.
}
}
diff --git a/ports/cxd56/mphalport.h b/ports/cxd56/mphalport.h
index 25bca97ad..a2be10b8d 100644
--- a/ports/cxd56/mphalport.h
+++ b/ports/cxd56/mphalport.h
@@ -31,6 +31,4 @@
#include "lib/utils/interrupt_char.h"
-extern volatile uint64_t ticks_ms;
-
#endif // MICROPY_INCLUDED_CXD56_MPHALPORT_H
diff --git a/ports/cxd56/tick.c b/ports/cxd56/tick.c
index 6529db790..671b82b74 100644
--- a/ports/cxd56/tick.c
+++ b/ports/cxd56/tick.c
@@ -27,19 +27,10 @@
#include "tick.h"
#include "supervisor/shared/autoreload.h"
-#include "supervisor/filesystem.h"
-
-// Global millisecond tick count
-volatile uint64_t ticks_ms = 0;
+#include "supervisor/shared/tick.h"
void board_timerhook(void)
{
- ticks_ms += 1;
-
-#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
- filesystem_tick();
-#endif
-#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
- autoreload_tick();
-#endif
+ // Do things common to all ports when the tick occurs
+ supervisor_tick();
}
diff --git a/ports/cxd56/tick.h b/ports/cxd56/tick.h
index a0d9ee526..d641d9cd4 100644
--- a/ports/cxd56/tick.h
+++ b/ports/cxd56/tick.h
@@ -29,6 +29,4 @@
#include "py/mpconfig.h"
-extern volatile uint64_t ticks_ms;
-
#endif // MICROPY_INCLUDED_CXD56_TICK_H
diff --git a/ports/nrf/common-hal/_bleio/Adapter.c b/ports/nrf/common-hal/_bleio/Adapter.c
index 295a42d63..c3b8dcebe 100644
--- a/ports/nrf/common-hal/_bleio/Adapter.c
+++ b/ports/nrf/common-hal/_bleio/Adapter.c
@@ -40,6 +40,7 @@
#include "py/objstr.h"
#include "py/runtime.h"
#include "supervisor/shared/safe_mode.h"
+#include "supervisor/shared/tick.h"
#include "supervisor/usb.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Adapter.h"
@@ -353,7 +354,7 @@ STATIC bool scan_on_ble_evt(ble_evt_t *ble_evt, void *scan_results_in) {
ble_gap_evt_adv_report_t *report = &ble_evt->evt.gap_evt.params.adv_report;
shared_module_bleio_scanresults_append(scan_results,
- ticks_ms,
+ supervisor_ticks_ms64(),
report->type.connectable,
report->type.scan_response,
report->rssi,
diff --git a/ports/nrf/common-hal/_bleio/CharacteristicBuffer.c b/ports/nrf/common-hal/_bleio/CharacteristicBuffer.c
index 5f280e121..9f9b453de 100644
--- a/ports/nrf/common-hal/_bleio/CharacteristicBuffer.c
+++ b/ports/nrf/common-hal/_bleio/CharacteristicBuffer.c
@@ -39,6 +39,7 @@
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Connection.h"
+#include "supervisor/shared/tick.h"
#include "common-hal/_bleio/CharacteristicBuffer.h"
STATIC void write_to_ringbuf(bleio_characteristic_buffer_obj_t *self, uint8_t *data, uint16_t len) {
@@ -100,10 +101,10 @@ void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffe
}
int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) {
- uint64_t start_ticks = ticks_ms;
+ uint64_t start_ticks = supervisor_ticks_ms64();
// Wait for all bytes received or timeout
- while ( (ringbuf_count(&self->ringbuf) < len) && (ticks_ms - start_ticks < self->timeout_ms) ) {
+ while ( (ringbuf_count(&self->ringbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
RUN_BACKGROUND_TASKS;
// Allow user to break out of a timeout with a KeyboardInterrupt.
if ( mp_hal_is_interrupted() ) {
diff --git a/ports/nrf/common-hal/busio/UART.c b/ports/nrf/common-hal/busio/UART.c
index 54a66ddbe..982b8efa9 100644
--- a/ports/nrf/common-hal/busio/UART.c
+++ b/ports/nrf/common-hal/busio/UART.c
@@ -231,10 +231,10 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
}
size_t rx_bytes = 0;
- uint64_t start_ticks = ticks_ms;
+ uint64_t start_ticks = supervisor_ticks_ms64();
// Wait for all bytes received or timeout
- while ( (ringbuf_count(&self->rbuf) < len) && (ticks_ms - start_ticks < self->timeout_ms) ) {
+ while ( (ringbuf_count(&self->rbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
RUN_BACKGROUND_TASKS;
// Allow user to break out of a timeout with a KeyboardInterrupt.
if ( mp_hal_is_interrupted() ) {
@@ -265,15 +265,15 @@ size_t common_hal_busio_uart_write (busio_uart_obj_t *self, const uint8_t *data,
if ( len == 0 ) return 0;
- uint64_t start_ticks = ticks_ms;
+ uint64_t start_ticks = supervisor_ticks_ms64();
// Wait for on-going transfer to complete
- while ( nrfx_uarte_tx_in_progress(self->uarte) && (ticks_ms - start_ticks < self->timeout_ms) ) {
+ while ( nrfx_uarte_tx_in_progress(self->uarte) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
RUN_BACKGROUND_TASKS;
}
// Time up
- if ( !(ticks_ms - start_ticks < self->timeout_ms) ) {
+ if ( !(supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
*errcode = MP_EAGAIN;
return MP_STREAM_ERROR;
}
@@ -290,7 +290,7 @@ size_t common_hal_busio_uart_write (busio_uart_obj_t *self, const uint8_t *data,
_VERIFY_ERR(*errcode);
(*errcode) = 0;
- while ( nrfx_uarte_tx_in_progress(self->uarte) && (ticks_ms - start_ticks < self->timeout_ms) ) {
+ while ( nrfx_uarte_tx_in_progress(self->uarte) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
RUN_BACKGROUND_TASKS;
}
diff --git a/ports/nrf/common-hal/time/__init__.c b/ports/nrf/common-hal/time/__init__.c
index e3cb481ef..976f519db 100644
--- a/ports/nrf/common-hal/time/__init__.c
+++ b/ports/nrf/common-hal/time/__init__.c
@@ -29,7 +29,7 @@
#include "tick.h"
uint64_t common_hal_time_monotonic(void) {
- return ticks_ms;
+ return supervisor_ticks_ms64();
}
void common_hal_time_delay_ms(uint32_t delay) {
diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c
index bcd9fb114..3885d5a82 100644
--- a/ports/nrf/mphalport.c
+++ b/ports/nrf/mphalport.c
@@ -31,12 +31,13 @@
#include "py/mphal.h"
#include "py/mpstate.h"
#include "py/gc.h"
+#include "supervisor/shared/tick.h"
/*------------------------------------------------------------------*/
/* delay
*------------------------------------------------------------------*/
void mp_hal_delay_ms(mp_uint_t delay) {
- uint64_t start_tick = ticks_ms;
+ uint64_t start_tick = supervisor_ticks_ms64();
uint64_t duration = 0;
while (duration < delay) {
RUN_BACKGROUND_TASKS;
@@ -45,7 +46,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
break;
}
- duration = (ticks_ms - start_tick);
+ duration = (supervisor_ticks_ms64() - start_tick);
// TODO(tannewt): Go to sleep for a little while while we wait.
}
}
diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h
index a1929a4ac..8bb351401 100644
--- a/ports/nrf/mphalport.h
+++ b/ports/nrf/mphalport.h
@@ -33,12 +33,11 @@
#include "lib/utils/interrupt_char.h"
#include "nrfx_uarte.h"
#include "py/mpconfig.h"
+#include "supervisor/shared/tick.h"
extern nrfx_uarte_t serial_instance;
-extern volatile uint64_t ticks_ms;
-
-#define mp_hal_ticks_ms() ((mp_uint_t) ticks_ms)
+#define mp_hal_ticks_ms() ((mp_uint_t) supervisor_ticks_ms32())
#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))
bool mp_hal_stdin_any(void);
diff --git a/ports/nrf/tick.c b/ports/nrf/tick.c
index 6d8fd13e0..ac825a7f1 100644
--- a/ports/nrf/tick.c
+++ b/ports/nrf/tick.c
@@ -26,31 +26,14 @@
#include "tick.h"
-#include "supervisor/shared/autoreload.h"
-#include "supervisor/filesystem.h"
+#include "supervisor/shared/tick.h"
#include "shared-module/gamepad/__init__.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "nrf.h"
-// Global millisecond tick count
-volatile uint64_t ticks_ms = 0;
-
void SysTick_Handler(void) {
- // SysTick interrupt handler called when the SysTick timer reaches zero
- // (every millisecond).
- ticks_ms += 1;
-
-#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
- filesystem_tick();
-#endif
-#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
- autoreload_tick();
-#endif
-#ifdef CIRCUITPY_GAMEPAD_TICKS
- if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
- gamepad_tick();
- }
-#endif
+ // Do things common to all ports when the tick occurs
+ supervisor_tick();
}
void tick_init() {
@@ -61,11 +44,11 @@ void tick_init() {
void tick_delay(uint32_t us) {
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
uint32_t us_between_ticks = SysTick->VAL / ticks_per_us;
- uint64_t start_ms = ticks_ms;
+ uint64_t start_ms = supervisor_ticks_ms64();
while (us > 1000) {
- while (ticks_ms == start_ms) {}
+ while (supervisor_ticks_ms64() == start_ms) {}
us -= us_between_ticks;
- start_ms = ticks_ms;
+ start_ms = supervisor_ticks_ms64();
us_between_ticks = 1000;
}
while (SysTick->VAL > ((us_between_ticks - us) * ticks_per_us)) {}
@@ -74,11 +57,11 @@ void tick_delay(uint32_t us) {
// us counts down!
void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
- *ms = ticks_ms;
+ *ms = supervisor_ticks_ms64();
*us_until_ms = SysTick->VAL / ticks_per_us;
}
void wait_until(uint64_t ms, uint32_t us_until_ms) {
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
- while(ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
+ while(supervisor_ticks_ms64() <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
}
diff --git a/ports/nrf/tick.h b/ports/nrf/tick.h
index 838e9fbea..d638ad025 100644
--- a/ports/nrf/tick.h
+++ b/ports/nrf/tick.h
@@ -30,8 +30,6 @@
#include <stdint.h>
-extern volatile uint64_t ticks_ms;
-
extern struct timer_descriptor ms_timer;
void tick_init(void);
diff --git a/ports/stm32f4/common-hal/time/__init__.c b/ports/stm32f4/common-hal/time/__init__.c
index e3cb481ef..976f519db 100644
--- a/ports/stm32f4/common-hal/time/__init__.c
+++ b/ports/stm32f4/common-hal/time/__init__.c
@@ -29,7 +29,7 @@
#include "tick.h"
uint64_t common_hal_time_monotonic(void) {
- return ticks_ms;
+ return supervisor_ticks_ms64();
}
void common_hal_time_delay_ms(uint32_t delay) {
diff --git a/ports/stm32f4/mphalport.c b/ports/stm32f4/mphalport.c
index ea864e7ce..f78e9c750 100644
--- a/ports/stm32f4/mphalport.c
+++ b/ports/stm32f4/mphalport.c
@@ -31,11 +31,13 @@
#include "py/mpstate.h"
#include "py/gc.h"
+#include "supervisor/shared/tick.h"
+
/*------------------------------------------------------------------*/
/* delay
*------------------------------------------------------------------*/
void mp_hal_delay_ms(mp_uint_t delay) {
- uint64_t start_tick = ticks_ms;
+ uint64_t start_tick = supervisor_ticks_ms64();
uint64_t duration = 0;
while (duration < delay) {
#ifdef MICROPY_VM_HOOK_LOOP
@@ -46,7 +48,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
break;
}
- duration = (ticks_ms - start_tick);
+ duration = (supervisor_ticks_ms64() - start_tick);
// TODO(tannewt): Go to sleep for a little while while we wait.
}
}
diff --git a/ports/stm32f4/mphalport.h b/ports/stm32f4/mphalport.h
index d184138f7..df2f0ca65 100644
--- a/ports/stm32f4/mphalport.h
+++ b/ports/stm32f4/mphalport.h
@@ -32,10 +32,10 @@
#include "lib/utils/interrupt_char.h"
#include "py/mpconfig.h"
+#include "supervisor/shared/tick.h"
-extern volatile uint64_t ticks_ms;
-#define mp_hal_ticks_ms() ((mp_uint_t) ticks_ms)
+#define mp_hal_ticks_ms() ((mp_uint_t) supervisor_ticks_ms32())
//#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))
bool mp_hal_stdin_any(void);
diff --git a/ports/stm32f4/tick.c b/ports/stm32f4/tick.c
index 688f71dbd..f4adf183a 100644
--- a/ports/stm32f4/tick.c
+++ b/ports/stm32f4/tick.c
@@ -26,37 +26,23 @@
#include "tick.h"
-#include "supervisor/shared/autoreload.h"
#include "supervisor/filesystem.h"
-#include "shared-module/gamepad/__init__.h"
+#include "supervisor/shared/tick.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "stm32f4xx.h"
-// Global millisecond tick count
-volatile uint64_t ticks_ms = 0;
-
void SysTick_Handler(void) {
// SysTick interrupt handler called when the SysTick timer reaches zero
// (every millisecond).
- ticks_ms += 1;
-#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
- filesystem_tick();
-#endif
-#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
- autoreload_tick();
-#endif
-#ifdef CIRCUITPY_GAMEPAD_TICKS
- if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
- gamepad_tick();
- }
-#endif
+ // Do things common to all ports when the tick occurs
+ supervisor_tick();
}
uint32_t HAL_GetTick(void) //override ST HAL
{
- return (uint32_t)ticks_ms;
+ return (uint32_t)supervisor_ticks_ms32();
}
void tick_init() {
@@ -72,11 +58,11 @@ void tick_init() {
void tick_delay(uint32_t us) {
uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000;
uint32_t us_between_ticks = SysTick->VAL / ticks_per_us;
- uint64_t start_ms = ticks_ms;
+ uint64_t start_ms = supervisor_ticks_ms64();
while (us > 1000) {
- while (ticks_ms == start_ms) {}
+ while (supervisor_ticks_ms64() == start_ms) {}
us -= us_between_ticks;
- start_ms = ticks_ms;
+ start_ms = supervisor_ticks_ms64();
us_between_ticks = 1000;
}
while (SysTick->VAL > ((us_between_ticks - us) * ticks_per_us)) {}
@@ -85,11 +71,11 @@ void tick_delay(uint32_t us) {
// us counts down!
void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000;
- *ms = ticks_ms;
+ *ms = supervisor_ticks_ms32();
*us_until_ms = SysTick->VAL / ticks_per_us;
}
void wait_until(uint64_t ms, uint32_t us_until_ms) {
uint32_t ticks_per_us = SystemCoreClock / 1000 / 1000;
- while(ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
+ while(supervisor_ticks_ms64() <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
}
diff --git a/ports/stm32f4/tick.h b/ports/stm32f4/tick.h
index e4772fa2c..999acc7a3 100644
--- a/ports/stm32f4/tick.h
+++ b/ports/stm32f4/tick.h
@@ -30,8 +30,6 @@
#include <stdint.h>
-extern volatile uint64_t ticks_ms;
-
extern struct timer_descriptor ms_timer;
void tick_init(void);