Skip to content

Commit fce81e6

Browse files
authored
Merge pull request #2297 from jepler/tick-refactor
Call background tasks only once per ms
2 parents e1a904f + 95d9c49 commit fce81e6

File tree

39 files changed

+287
-167
lines changed

39 files changed

+287
-167
lines changed

drivers/wiznet5k/internet/dns/dns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
#include <string.h>
5454
#include <stdlib.h>
55-
#include "tick.h"
55+
#include "supervisor/shared/tick.h"
5656

5757
//#include "Ethernet/socket.h"
5858
//#include "Internet/DNS/dns.h"
@@ -125,7 +125,7 @@ uint16_t DNS_MSGID; // DNS message ID
125125

126126

127127
uint32_t HAL_GetTick(void) {
128-
return ticks_ms;
128+
return supervisor_ticks_ms32();
129129
}
130130

131131
uint32_t hal_sys_tick;

ports/atmel-samd/background.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "audio_dma.h"
2929
#include "tick.h"
3030
#include "supervisor/filesystem.h"
31+
#include "supervisor/shared/tick.h"
3132
#include "supervisor/usb.h"
3233

3334
#include "py/runtime.h"
@@ -44,6 +45,23 @@ bool stack_ok_so_far = true;
4445

4546
static bool running_background_tasks = false;
4647

48+
#ifdef MONITOR_BACKGROUND_TASKS
49+
// PB03 is physical pin "SCL" on the Metro M4 express
50+
// so you can't use this code AND an i2c peripheral
51+
// at the same time unless you change this
52+
STATIC void start_background_task(void) {
53+
REG_PORT_DIRSET1 = (1<<3);
54+
REG_PORT_OUTSET1 = (1<<3);
55+
}
56+
57+
STATIC void finish_background_task(void) {
58+
REG_PORT_OUTCLR1 = (1<<3);
59+
}
60+
#else
61+
STATIC void start_background_task(void) {}
62+
STATIC void finish_background_task(void) {}
63+
#endif
64+
4765
void background_tasks_reset(void) {
4866
running_background_tasks = false;
4967
}
@@ -53,6 +71,9 @@ void run_background_tasks(void) {
5371
if (running_background_tasks) {
5472
return;
5573
}
74+
75+
start_background_task();
76+
5677
assert_heap_ok();
5778
running_background_tasks = true;
5879

@@ -71,9 +92,10 @@ void run_background_tasks(void) {
7192
running_background_tasks = false;
7293
assert_heap_ok();
7394

74-
last_finished_tick = ticks_ms;
95+
last_finished_tick = supervisor_ticks_ms64();
96+
finish_background_task();
7597
}
7698

7799
bool background_tasks_ok(void) {
78-
return ticks_ms - last_finished_tick < 1000;
100+
return supervisor_ticks_ms64() - last_finished_tick < 1000;
79101
}

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
#include "py/runtime.h"
3535
#include "py/stream.h"
3636
#include "supervisor/shared/translate.h"
37-
38-
#include "tick.h"
37+
#include "supervisor/shared/tick.h"
3938

4039
#include "hpl_sercom_config.h"
4140
#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
272271
usart_async_get_io_descriptor(usart_desc_p, &io);
273272

274273
size_t total_read = 0;
275-
uint64_t start_ticks = ticks_ms;
274+
uint64_t start_ticks = supervisor_ticks_ms64();
276275

277276
// Busy-wait until timeout or until we've read enough chars.
278-
while (ticks_ms - start_ticks <= self->timeout_ms) {
277+
while (supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) {
279278
// Read as many chars as we can right now, up to len.
280279
size_t num_read = io_read(io, data, len);
281280

@@ -289,7 +288,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
289288
}
290289
if (num_read > 0) {
291290
// Reset the timeout on every character read.
292-
start_ticks = ticks_ms;
291+
start_ticks = supervisor_ticks_ms64();
293292
}
294293
RUN_BACKGROUND_TASKS;
295294
// Allow user to break out of a timeout with a KeyboardInterrupt.

ports/atmel-samd/common-hal/time/__init__.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828

2929
#include "shared-bindings/time/__init__.h"
3030

31-
#include "tick.h"
31+
#include "supervisor/shared/tick.h"
3232

3333
inline uint64_t common_hal_time_monotonic() {
34-
return ticks_ms;
34+
return supervisor_ticks_ms64();
3535
}
3636

3737
void common_hal_time_delay_ms(uint32_t delay) {

ports/atmel-samd/mphalport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
#include "mpconfigboard.h"
4646
#include "mphalport.h"
4747
#include "reset.h"
48-
#include "tick.h"
48+
#include "supervisor/shared/tick.h"
4949

5050
extern uint32_t common_hal_mcu_processor_get_frequency(void);
5151

5252
void mp_hal_delay_ms(mp_uint_t delay) {
53-
uint64_t start_tick = ticks_ms;
53+
uint64_t start_tick = supervisor_ticks_ms64();
5454
uint64_t duration = 0;
5555
while (duration < delay) {
5656
RUN_BACKGROUND_TASKS;
@@ -59,7 +59,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
5959
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
6060
break;
6161
}
62-
duration = (ticks_ms - start_tick);
62+
duration = (supervisor_ticks_ms64() - start_tick);
6363
// TODO(tannewt): Go to sleep for a little while while we wait.
6464
}
6565
}

ports/atmel-samd/mphalport.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131

3232
#include "lib/oofatfs/ff.h"
3333

34-
// Global millisecond tick count (driven by SysTick interrupt).
35-
extern volatile uint64_t ticks_ms;
34+
#include "supervisor/shared/tick.h"
3635

36+
// Global millisecond tick count (driven by SysTick interrupt).
3737
static inline mp_uint_t mp_hal_ticks_ms(void) {
38-
return ticks_ms;
38+
return supervisor_ticks_ms32();
3939
}
4040
// Number of bytes in receive buffer
4141
volatile uint8_t usb_rx_count;

ports/atmel-samd/tick.c

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,21 @@
2828

2929
#include "peripheral_clk_config.h"
3030

31-
#include "supervisor/shared/autoreload.h"
32-
#include "supervisor/filesystem.h"
31+
#include "supervisor/shared/tick.h"
3332
#include "shared-bindings/microcontroller/__init__.h"
3433
#include "shared-bindings/microcontroller/Processor.h"
3534

36-
#if CIRCUITPY_GAMEPAD
37-
#include "shared-module/gamepad/__init__.h"
38-
#endif
39-
40-
#if CIRCUITPY_GAMEPADSHIFT
41-
#include "shared-module/gamepadshift/__init__.h"
42-
#endif
43-
// Global millisecond tick count
44-
volatile uint64_t ticks_ms = 0;
45-
4635
void SysTick_Handler(void) {
4736
// SysTick interrupt handler called when the SysTick timer reaches zero
4837
// (every millisecond).
4938
common_hal_mcu_disable_interrupts();
50-
ticks_ms += 1;
5139

5240
// Read the control register to reset the COUNTFLAG.
5341
(void) SysTick->CTRL;
5442
common_hal_mcu_enable_interrupts();
5543

56-
#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
57-
filesystem_tick();
58-
#endif
59-
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
60-
autoreload_tick();
61-
#endif
62-
#ifdef CIRCUITPY_GAMEPAD_TICKS
63-
if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
64-
#if CIRCUITPY_GAMEPAD
65-
gamepad_tick();
66-
#endif
67-
#if CIRCUITPY_GAMEPADSHIFT
68-
gamepadshift_tick();
69-
#endif
70-
}
71-
#endif
44+
// Do things common to all ports when the tick occurs
45+
supervisor_tick();
7246
}
7347

7448
void tick_init() {
@@ -115,7 +89,7 @@ void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
11589
uint32_t tick_status = SysTick->CTRL;
11690
uint32_t current_us = SysTick->VAL;
11791
uint32_t tick_status2 = SysTick->CTRL;
118-
uint64_t current_ms = ticks_ms;
92+
uint64_t current_ms = supervisor_ticks_ms64();
11993
// The second clause ensures our value actually rolled over. Its possible it hit zero between
12094
// the VAL read and CTRL read.
12195
if ((tick_status & SysTick_CTRL_COUNTFLAG_Msk) != 0 ||
@@ -129,5 +103,5 @@ void current_tick(uint64_t* ms, uint32_t* us_until_ms) {
129103

130104
void wait_until(uint64_t ms, uint32_t us_until_ms) {
131105
uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000;
132-
while (ticks_ms <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
106+
while (supervisor_ticks_ms64() <= ms && SysTick->VAL / ticks_per_us >= us_until_ms) {}
133107
}

ports/atmel-samd/tick.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
#include "py/mpconfig.h"
3030

31-
extern volatile uint64_t ticks_ms;
32-
3331
extern struct timer_descriptor ms_timer;
3432

3533
void tick_init(void);

ports/cxd56/common-hal/time/__init__.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
#include "py/mphal.h"
2828

29-
#include "tick.h"
29+
#include "supervisor/shared/tick.h"
3030

3131
uint64_t common_hal_time_monotonic(void) {
32-
return ticks_ms;
32+
return supervisor_ticks_ms64();
3333
}
3434

3535
void common_hal_time_delay_ms(uint32_t delay) {

ports/cxd56/mphalport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#include "py/mpstate.h"
3333

34-
#include "tick.h"
34+
#include "supervisor/shared/tick.h"
3535

3636
#define DELAY_CORRECTION (700)
3737
#define DELAY_INTERVAL (50)
@@ -57,7 +57,7 @@ mp_uint_t mp_hal_ticks_cpu(void) {
5757
}
5858

5959
void mp_hal_delay_ms(mp_uint_t delay) {
60-
uint64_t start_tick = ticks_ms;
60+
uint64_t start_tick = supervisor_ticks_ms64();
6161
uint64_t duration = 0;
6262
while (duration < delay) {
6363
#ifdef MICROPY_VM_HOOK_LOOP
@@ -68,7 +68,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
6868
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
6969
break;
7070
}
71-
duration = (ticks_ms - start_tick);
71+
duration = (supervisor_ticks_ms64() - start_tick);
7272
// TODO(tannewt): Go to sleep for a little while while we wait.
7373
}
7474
}

ports/cxd56/mphalport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,4 @@
3131

3232
#include "lib/utils/interrupt_char.h"
3333

34-
extern volatile uint64_t ticks_ms;
35-
3634
#endif // MICROPY_INCLUDED_CXD56_MPHALPORT_H

ports/cxd56/tick.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,10 @@
2727
#include "tick.h"
2828

2929
#include "supervisor/shared/autoreload.h"
30-
#include "supervisor/filesystem.h"
31-
32-
// Global millisecond tick count
33-
volatile uint64_t ticks_ms = 0;
30+
#include "supervisor/shared/tick.h"
3431

3532
void board_timerhook(void)
3633
{
37-
ticks_ms += 1;
38-
39-
#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
40-
filesystem_tick();
41-
#endif
42-
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
43-
autoreload_tick();
44-
#endif
34+
// Do things common to all ports when the tick occurs
35+
supervisor_tick();
4536
}

ports/cxd56/tick.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@
2929

3030
#include "py/mpconfig.h"
3131

32-
extern volatile uint64_t ticks_ms;
33-
3432
#endif // MICROPY_INCLUDED_CXD56_TICK_H

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "py/objstr.h"
4141
#include "py/runtime.h"
4242
#include "supervisor/shared/safe_mode.h"
43+
#include "supervisor/shared/tick.h"
4344
#include "supervisor/usb.h"
4445
#include "shared-bindings/_bleio/__init__.h"
4546
#include "shared-bindings/_bleio/Adapter.h"
@@ -343,7 +344,7 @@ STATIC bool scan_on_ble_evt(ble_evt_t *ble_evt, void *scan_results_in) {
343344
ble_gap_evt_adv_report_t *report = &ble_evt->evt.gap_evt.params.adv_report;
344345

345346
shared_module_bleio_scanresults_append(scan_results,
346-
ticks_ms,
347+
supervisor_ticks_ms64(),
347348
report->type.connectable,
348349
report->type.scan_response,
349350
report->rssi,

ports/nrf/common-hal/_bleio/CharacteristicBuffer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include "shared-bindings/_bleio/__init__.h"
4141
#include "shared-bindings/_bleio/Connection.h"
42+
#include "supervisor/shared/tick.h"
4243
#include "common-hal/_bleio/CharacteristicBuffer.h"
4344

4445
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
100101
}
101102

102103
int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) {
103-
uint64_t start_ticks = ticks_ms;
104+
uint64_t start_ticks = supervisor_ticks_ms64();
104105

105106
// Wait for all bytes received or timeout
106-
while ( (ringbuf_count(&self->ringbuf) < len) && (ticks_ms - start_ticks < self->timeout_ms) ) {
107+
while ( (ringbuf_count(&self->ringbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
107108
RUN_BACKGROUND_TASKS;
108109
// Allow user to break out of a timeout with a KeyboardInterrupt.
109110
if ( mp_hal_is_interrupted() ) {

ports/nrf/common-hal/busio/UART.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
231231
}
232232

233233
size_t rx_bytes = 0;
234-
uint64_t start_ticks = ticks_ms;
234+
uint64_t start_ticks = supervisor_ticks_ms64();
235235

236236
// Wait for all bytes received or timeout
237-
while ( (ringbuf_count(&self->rbuf) < len) && (ticks_ms - start_ticks < self->timeout_ms) ) {
237+
while ( (ringbuf_count(&self->rbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) {
238238
RUN_BACKGROUND_TASKS;
239239
// Allow user to break out of a timeout with a KeyboardInterrupt.
240240
if ( mp_hal_is_interrupted() ) {

ports/nrf/common-hal/time/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "tick.h"
3030

3131
uint64_t common_hal_time_monotonic(void) {
32-
return ticks_ms;
32+
return supervisor_ticks_ms64();
3333
}
3434

3535
void common_hal_time_delay_ms(uint32_t delay) {

ports/nrf/mphalport.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
#include "py/mphal.h"
3232
#include "py/mpstate.h"
3333
#include "py/gc.h"
34+
#include "supervisor/shared/tick.h"
3435

3536
/*------------------------------------------------------------------*/
3637
/* delay
3738
*------------------------------------------------------------------*/
3839
void mp_hal_delay_ms(mp_uint_t delay) {
39-
uint64_t start_tick = ticks_ms;
40+
uint64_t start_tick = supervisor_ticks_ms64();
4041
uint64_t duration = 0;
4142
while (duration < delay) {
4243
RUN_BACKGROUND_TASKS;
@@ -45,7 +46,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
4546
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
4647
break;
4748
}
48-
duration = (ticks_ms - start_tick);
49+
duration = (supervisor_ticks_ms64() - start_tick);
4950
// TODO(tannewt): Go to sleep for a little while while we wait.
5051
}
5152
}

0 commit comments

Comments
 (0)