Skip to content

Commit 7767995

Browse files
committed
pbio/platform/sim_hub: Don't use Linux signal for clock tick.
This interrupts syscalls like reading data from a pipe, especially when debugging. We can instead just request polling when MicroPython drives the poll hook and the wall clock time has changed. For the CI clock variant, this was not implented at all yet. While we are at it, ensure it increments the clock every N instructions, so that even tight loops without any waits will advance. For example, this would be needed to run asynchronous scripts that don't _WFI anywhere.
1 parent 40d6823 commit 7767995

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"label": "build simhub (debug)",
4949
"type": "shell",
50-
"command": "make -C ../../micropython/mpy-cross -j && poetry run make DEBUG=1 BUILD=build-debug COPT=-DPBDRV_CONFIG_CLOCK_LINUX -j",
50+
"command": "make -C ../../micropython/mpy-cross -j && poetry run make DEBUG=1 BUILD=build-debug COPT=-DPBDRV_CONFIG_CLOCK_LOCAL -j",
5151
},
5252
{
5353
"label": "build test-pbio",

bricks/_common/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ typedef long mp_off_t;
149149
#define MICROPY_END_ATOMIC_SECTION(state) (void)(state)
150150
#endif
151151

152+
#ifndef MICROPY_VM_HOOK_LOOP
152153
#define MICROPY_VM_HOOK_LOOP \
153154
do { \
154155
extern bool pbio_os_run_processes_once(void); \
155156
pbio_os_run_processes_once(); \
156157
} while (0);
158+
#endif
157159

158160
#define MICROPY_GC_HOOK_LOOP(i) do { \
159161
if (((i) & 0xf) == 0) { \

bricks/simhub/mpconfigport.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdint.h>
2+
#include <pbdrv/config.h>
23

34
#define MICROPY_HW_BOARD_NAME "Desktop"
45
#define MICROPY_HW_MCU_NAME "Desktop"
@@ -53,4 +54,32 @@
5354
#define PYBRICKS_OPT_CUSTOM_IMPORT (0)
5455
#define PYBRICKS_OPT_NATIVE_MOD (0)
5556

57+
#if PBDRV_CONFIG_CLOCK_TEST
58+
#define MICROPY_VM_HOOK_LOOP \
59+
do { \
60+
static uint32_t count; \
61+
if ((count % 16) == 0) { \
62+
extern void pbio_test_clock_tick(uint32_t ticks); \
63+
pbio_test_clock_tick(1); \
64+
} \
65+
extern bool pbio_os_run_processes_once(void); \
66+
pbio_os_run_processes_once(); \
67+
} while (0);
68+
#else
69+
#define MICROPY_VM_HOOK_LOOP \
70+
do { \
71+
static uint32_t clock_last; \
72+
extern uint32_t pbdrv_clock_get_ms(void); \
73+
uint32_t clock_now = pbdrv_clock_get_ms(); \
74+
if (clock_last != clock_now) { \
75+
extern void pbio_os_request_poll(void); \
76+
pbio_os_request_poll(); \
77+
clock_last = clock_now; \
78+
} \
79+
extern bool pbio_os_run_processes_once(void); \
80+
pbio_os_run_processes_once(); \
81+
} while (0);
82+
#endif
83+
84+
5685
#include "../_common/mpconfigport.h"

bricks/simhub/pbio_os_hook.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
5353
MP_THREAD_GIL_EXIT();
5454
pselect(0, NULL, NULL, NULL, &timeout, &origmask);
5555
MP_THREAD_GIL_ENTER();
56+
57+
// There is a new timer event to handle.
58+
pbio_os_request_poll();
5659
}

lib/pbio/platform/sim_hub/pbdrvconfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#define PBDRV_CONFIG_BUTTON_TEST (1)
1616

1717
#define PBDRV_CONFIG_CLOCK (1)
18-
#ifdef PBDRV_CONFIG_CLOCK_LINUX
19-
#define PBDRV_CONFIG_CLOCK_LINUX_SIGNAL (1)
18+
#ifdef PBDRV_CONFIG_CLOCK_LOCAL
19+
#define PBDRV_CONFIG_CLOCK_LINUX (1)
2020
#else
2121
#define PBDRV_CONFIG_CLOCK_TEST (1)
2222
#endif

0 commit comments

Comments
 (0)