Skip to content

Commit 3119c1b

Browse files
committed
bricks/simhub: Clean up VM_HOOK.
Also rename desktop target to native for clarity.
1 parent 7767995 commit 3119c1b

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

bricks/_common/common.mk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ OPENOCD ?= openocd
153153
OPENOCD_CONFIG ?= openocd_stm32$(PB_MCU_SERIES_LCASE).cfg
154154
TEXT0_ADDR ?= 0x08000000
155155

156-
ifeq ($(PB_MCU_FAMILY),desktop)
156+
ifeq ($(PB_MCU_FAMILY),native)
157157
UNAME_S := $(shell uname -s)
158158
LD = $(CC)
159159
CFLAGS += $(INC) -Wall -Werror -Wdouble-promotion -Wfloat-conversion -std=gnu99 $(COPT) -D_GNU_SOURCE
@@ -163,7 +163,7 @@ else ifeq ($(UNAME_S),Darwin)
163163
LDFLAGS += -Wl,-map,$@.map -Wl,-dead_strip
164164
endif
165165
LIBS =
166-
else # end desktop, begin embedded
166+
else # end native, begin embedded
167167
CROSS_COMPILE ?= arm-none-eabi-
168168
ifeq ($(PB_MCU_FAMILY),STM32)
169169
CFLAGS_MCU_F0 = -mthumb -mtune=cortex-m0 -mcpu=cortex-m0 -msoft-float
@@ -250,7 +250,7 @@ PY_EXTRA_SRC_C = $(addprefix shared/,\
250250
runtime/stdout_helpers.c \
251251
)
252252

253-
ifeq ($(PB_MCU_FAMILY),desktop)
253+
ifeq ($(PB_MCU_FAMILY),native)
254254
PY_EXTRA_SRC_C += $(addprefix shared/,\
255255
runtime/gchelper_generic.c \
256256
)
@@ -286,7 +286,7 @@ PY_EXTRA_SRC_C += $(addprefix bricks/_common/,\
286286
endif
287287

288288
# Not all MCUs support thumb2 instructions.
289-
ifeq ($(PB_MCU_FAMILY),desktop)
289+
ifeq ($(PB_MCU_FAMILY),native)
290290
SRC_S +=
291291
else ifeq ($(PB_MCU_SERIES),$(filter $(PB_MCU_SERIES),AT91SAM7 F0 TIAM1808))
292292
SRC_S += shared/runtime/gchelper_thumb1.s
@@ -587,7 +587,7 @@ CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
587587
MPY_TOOL_FLAGS += -mlongint-impl none
588588
endif
589589

590-
ifneq ($(PB_MCU_FAMILY),desktop)
590+
ifneq ($(PB_MCU_FAMILY),native)
591591
# Main firmware build targets
592592
TARGETS := $(BUILD)/firmware.zip
593593
else

bricks/_common/mpconfigport.h

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

152-
#ifndef MICROPY_VM_HOOK_LOOP
152+
// Optional extra code to run before MicroPython drives the event loop.
153+
#ifndef PYBRICKS_VM_HOOK_LOOP_EXTRA
154+
#define PYBRICKS_VM_HOOK_LOOP_EXTRA
155+
#endif
156+
153157
#define MICROPY_VM_HOOK_LOOP \
154158
do { \
159+
PYBRICKS_VM_HOOK_LOOP_EXTRA \
155160
extern bool pbio_os_run_processes_once(void); \
156161
pbio_os_run_processes_once(); \
157162
} while (0);
158-
#endif
159163

160164
#define MICROPY_GC_HOOK_LOOP(i) do { \
161165
if (((i) & 0xf) == 0) { \

bricks/simhub/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (c) 2025 The Pybricks Authors
33

44
PBIO_PLATFORM = sim_hub
5-
PB_MCU_FAMILY = desktop
5+
PB_MCU_FAMILY = native
66
PB_FROZEN_MODULES = 1
77
MICROPY_ROM_TEXT_COMPRESSION = 1
88

bricks/simhub/mpconfigport.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,30 @@
5454
#define PYBRICKS_OPT_CUSTOM_IMPORT (0)
5555
#define PYBRICKS_OPT_NATIVE_MOD (0)
5656

57+
// The Virtual Hub has no hardware interrupt that requests polling every 1ms.
58+
// We solve this by polling manually as appropriate for the simulation, as
59+
// indicated below.
5760
#if PBDRV_CONFIG_CLOCK_TEST
58-
#define MICROPY_VM_HOOK_LOOP \
61+
// In the CI variant ("counting clock"), the clock is advanced on every
62+
// pbio_os_hook_wait_for_interrupt, which is called from mp_hal_delay_ms. But
63+
// the user could be running a tight loop without any waits. We still want to
64+
// advance the clock in those cases, which we mimic here by advancing the clock
65+
// every couple of MicroPython byte codes. This also polls to the event loop.
66+
#define PYBRICKS_VM_HOOK_LOOP_EXTRA \
5967
do { \
6068
static uint32_t count; \
6169
if ((count % 16) == 0) { \
6270
extern void pbio_test_clock_tick(uint32_t ticks); \
6371
pbio_test_clock_tick(1); \
6472
} \
65-
extern bool pbio_os_run_processes_once(void); \
66-
pbio_os_run_processes_once(); \
6773
} while (0);
6874
#else
69-
#define MICROPY_VM_HOOK_LOOP \
75+
// When using the wall clock, time advances automatically but we still need to
76+
// request polling. This is done at the end of pbio_os_hook_wait_for_interrupt.
77+
// As above, we also need something to move it along with blocking user loops.
78+
// Instead of guessing with a number of instructions, here we can just poll
79+
// whenever the wall clock changes.
80+
#define PYBRICKS_VM_HOOK_LOOP_EXTRA \
7081
do { \
7182
static uint32_t clock_last; \
7283
extern uint32_t pbdrv_clock_get_ms(void); \
@@ -76,8 +87,6 @@
7687
pbio_os_request_poll(); \
7788
clock_last = clock_now; \
7889
} \
79-
extern bool pbio_os_run_processes_once(void); \
80-
pbio_os_run_processes_once(); \
8190
} while (0);
8291
#endif
8392

bricks/simhub/pbio_os_hook.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@
1111

1212
#if PBDRV_CONFIG_CLOCK_TEST
1313
#include <pbdrv/../../drv/clock/clock_test.h>
14-
#endif
1514

1615
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
1716
sigset_t sigmask;
18-
19-
#if PBDRV_CONFIG_CLOCK_TEST
2017
sigemptyset(&sigmask);
2118
return sigmask;
22-
#endif
19+
}
20+
21+
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
22+
}
2323

24+
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
25+
// All events have been handled at this time. Advance the clock
26+
// and continue immediately.
27+
pbio_test_clock_tick(1);
28+
}
29+
30+
#else
31+
32+
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
33+
sigset_t sigmask;
2434
sigfillset(&sigmask);
2535
sigset_t origmask;
2636
pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
2737
return origmask;
2838
}
2939

3040
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
31-
#if PBDRV_CONFIG_CLOCK_TEST
32-
return;
33-
#endif
34-
3541
sigset_t origmask = (sigset_t)flags;
3642
pthread_sigmask(SIG_SETMASK, &origmask, NULL);
3743
}
3844

3945
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
40-
#if PBDRV_CONFIG_CLOCK_TEST
41-
// All events have been handled at this time. Advance the clock
42-
// and continue immediately.
43-
pbio_test_clock_tick(1);
44-
return;
45-
#endif
4646

4747
struct timespec timeout = {
4848
.tv_sec = 0,
@@ -57,3 +57,5 @@ void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
5757
// There is a new timer event to handle.
5858
pbio_os_request_poll();
5959
}
60+
61+
#endif

0 commit comments

Comments
 (0)