Skip to content

Commit 097af80

Browse files
committed
Fix ticks
In #7497 port_background_task was renamed to port_background_tick but the actual call site wasn't changed. This meant that it was no longer called! Rename more functions from task to tick to make it clearer which is which.
1 parent 76f9c18 commit 097af80

File tree

14 files changed

+44
-48
lines changed

14 files changed

+44
-48
lines changed

ports/atmel-samd/background.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@
4242
// PB03 is physical pin "SCL" on the Metro M4 express
4343
// so you can't use this code AND an i2c peripheral
4444
// at the same time unless you change this
45-
void port_start_background_task(void) {
45+
void port_start_background_tick(void) {
4646
REG_PORT_DIRSET1 = (1 << 3);
4747
REG_PORT_OUTSET1 = (1 << 3);
4848
}
4949

50-
void port_finish_background_task(void) {
50+
void port_finish_background_tick(void) {
5151
REG_PORT_OUTCLR1 = (1 << 3);
5252
}
5353
#else
54-
void port_start_background_task(void) {
54+
void port_start_background_tick(void) {
5555
}
56-
void port_finish_background_task(void) {
56+
void port_finish_background_tick(void) {
5757
}
5858
#endif
5959

ports/broadcom/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#include "py/runtime.h"
2929
#include "supervisor/port.h"
3030

31-
void port_start_background_task(void) {
31+
void port_start_background_tick(void) {
3232
}
33-
void port_finish_background_task(void) {
33+
void port_finish_background_tick(void) {
3434
}
3535

3636
void port_background_tick(void) {

ports/cxd56/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void port_background_tick(void) {
3434
}
3535
void port_background_task(void) {
3636
}
37-
void port_start_background_task(void) {
37+
void port_start_background_tick(void) {
3838
}
39-
void port_finish_background_task(void) {
39+
void port_finish_background_tick(void) {
4040
}

ports/espressif/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ void port_background_tick(void) {
5151
void port_background_task(void) {
5252
}
5353

54-
void port_start_background_task(void) {
54+
void port_start_background_tick(void) {
5555
}
5656

57-
void port_finish_background_task(void) {
57+
void port_finish_background_tick(void) {
5858
}

ports/litex/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void port_background_task(void) {
3434
}
3535
void port_background_tick(void) {
3636
}
37-
void port_start_background_task(void) {
37+
void port_start_background_tick(void) {
3838
}
39-
void port_finish_background_task(void) {
39+
void port_finish_background_tick(void) {
4040
}

ports/mimxrt10xx/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ void PLACE_IN_ITCM(port_background_task)(void) {
3737
void port_background_tick(void) {
3838
}
3939

40-
void port_start_background_task(void) {
40+
void port_start_background_tick(void) {
4141
}
4242

43-
void port_finish_background_task(void) {
43+
void port_finish_background_tick(void) {
4444
}

ports/nrf/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
#include "common-hal/audiopwmio/PWMAudioOut.h"
4545
#endif
4646

47-
void port_start_background_task(void) {
47+
void port_start_background_tick(void) {
4848
}
4949

50-
void port_finish_background_task(void) {
50+
void port_finish_background_tick(void) {
5151
}
5252

5353
void port_background_tick(void) {

ports/raspberrypi/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
#include "py/runtime.h"
2929
#include "supervisor/port.h"
3030

31-
void port_start_background_task(void) {
31+
void port_start_background_tick(void) {
3232
}
3333

34-
void port_finish_background_task(void) {
34+
void port_finish_background_tick(void) {
3535
}
3636

3737
void port_background_tick(void) {

ports/stm/background.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void port_background_task(void) {
3737
}
3838
void port_background_tick(void) {
3939
}
40-
void port_start_background_task(void) {
40+
void port_start_background_tick(void) {
4141
}
42-
void port_finish_background_task(void) {
42+
void port_finish_background_tick(void) {
4343
}

py/circuitpy_mpconfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ struct _supervisor_allocation_node;
439439
const char *readline_hist[8]; \
440440
struct _supervisor_allocation_node *first_embedded_allocation; \
441441

442-
void supervisor_run_background_tasks_if_tick(void);
443-
#define RUN_BACKGROUND_TASKS (supervisor_run_background_tasks_if_tick())
442+
void background_callback_run_all(void);
443+
#define RUN_BACKGROUND_TASKS (background_callback_run_all())
444444

445445
#define MICROPY_VM_HOOK_LOOP RUN_BACKGROUND_TASKS;
446446
#define MICROPY_VM_HOOK_RETURN RUN_BACKGROUND_TASKS;

supervisor/background_callback.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@
3737
* To schedule the work, use background_callback_add, with fun as the
3838
* function to call and data pointing to the object itself.
3939
*
40-
* Next time run_background_tasks_if_tick is called, the callback will
41-
* be run and removed from the linked list.
40+
* Next time background_callback_run_all() is called, the callback will
41+
* be run and removed from the linked list. Use `RUN_BACKGROUND_TASKS;` instead
42+
* of calling background_callback_run_all() directly.
4243
*
4344
* Queueing a task that is already queued does nothing. Unconditionally
4445
* re-queueing it from its own background task will cause it to run during the
4546
* very next background-tasks invocation, leading to a CircuitPython freeze, so
4647
* don't do that.
4748
*
4849
* background_callback_add can be called from interrupt context.
50+
*
51+
* If your work isn't triggered by an event, then it may be better implemented
52+
* using ticks, which runs tasks every millisecond or so. Ticks are enabled with
53+
* supervisor_enable_tick() and disabled with supervisor_disable_tick(). When
54+
* enabled, a timer will schedule a callback to supervisor_background_tick(),
55+
* which includes port_background_tick(), every millisecond.
4956
*/
5057
typedef void (*background_callback_fun)(void *data);
5158
typedef struct background_callback {

supervisor/port.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ void port_idle_until_interrupt(void);
9494
void port_background_tick(void);
9595

9696
// Execute port specific actions during background tasks. This is before the
97-
// background callback system.
97+
// background callback system and happens *very* often. Use
98+
// port_background_tick() when possible.
9899
void port_background_task(void);
99100

100-
// Take port specific actions at the beginning and end of background tasks.
101+
// Take port specific actions at the beginning and end of background ticks.
101102
// This is used e.g., to set a monitoring pin for debug purposes. "Actual
102-
// work" should be done in port_background_task() instead.
103-
void port_start_background_task(void);
104-
void port_finish_background_task(void);
103+
// work" should be done in port_background_tick() instead.
104+
void port_start_background_tick(void);
105+
void port_finish_background_tick(void);
105106

106107
// Some ports need special handling to wake the main task from another task. The
107108
// port must implement the necessary code in this function. A default weak

supervisor/shared/tick.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ static volatile uint64_t last_finished_tick = 0;
6565

6666
static volatile size_t tick_enable_count = 0;
6767

68-
static void supervisor_background_tasks(void *unused) {
69-
port_start_background_task();
68+
static void supervisor_background_tick(void *unused) {
69+
port_start_background_tick();
7070

7171
assert_heap_ok();
7272

@@ -80,16 +80,16 @@ static void supervisor_background_tasks(void *unused) {
8080

8181
filesystem_background();
8282

83-
port_background_task();
83+
port_background_tick();
8484

8585
assert_heap_ok();
8686

8787
last_finished_tick = port_get_raw_ticks(NULL);
8888

89-
port_finish_background_task();
89+
port_finish_background_tick();
9090
}
9191

92-
bool supervisor_background_tasks_ok(void) {
92+
bool supervisor_background_ticks_ok(void) {
9393
return port_get_raw_ticks(NULL) - last_finished_tick < 1024;
9494
}
9595

@@ -103,7 +103,7 @@ void supervisor_tick(void) {
103103
keypad_tick();
104104
#endif
105105

106-
background_callback_add(&tick_callback, supervisor_background_tasks, NULL);
106+
background_callback_add(&tick_callback, supervisor_background_tick, NULL);
107107
}
108108

109109
uint64_t supervisor_ticks_ms64() {
@@ -117,11 +117,6 @@ uint32_t supervisor_ticks_ms32() {
117117
return supervisor_ticks_ms64();
118118
}
119119

120-
121-
void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() {
122-
background_callback_run_all();
123-
}
124-
125120
void mp_hal_delay_ms(mp_uint_t delay_ms) {
126121
uint64_t start_tick = port_get_raw_ticks(NULL);
127122
// Adjust the delay to ticks vs ms.

supervisor/shared/tick.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ extern uint32_t supervisor_ticks_ms32(void);
5454
*/
5555
extern uint64_t supervisor_ticks_ms64(void);
5656

57-
/** @brief Run background ticks, but only about every millisecond.
58-
*
59-
* Normally, this is not called directly. Instead use the RUN_BACKGROUND_TASKS
60-
* macro.
61-
*/
62-
extern void supervisor_run_background_if_tick(void);
63-
6457
extern void supervisor_enable_tick(void);
6558
extern void supervisor_disable_tick(void);
6659

@@ -70,6 +63,6 @@ extern void supervisor_disable_tick(void);
7063
* Note that when ticks are not enabled, this function can return false; this is
7164
* intended.
7265
*/
73-
extern bool supervisor_background_tasks_ok(void);
66+
extern bool supervisor_background_ticks_ok(void);
7467

7568
#endif

0 commit comments

Comments
 (0)