@@ -46,6 +46,62 @@ MICROPY_FLOAT_IMPL ?= float
4646include $(TOP ) /py/py.mk
4747include $(TOP ) /extmod/extmod.mk
4848
49+ # ###############################################################################
50+ # Zephyr Threading Integration (optional, enabled per-board)
51+
52+ ifeq ($(MICROPY_ZEPHYR_THREADING ) ,1)
53+
54+ # Set architecture for Zephyr (only ARM Cortex-M supported currently)
55+ ZEPHYR_ARCH := arm
56+
57+ # Enable idle thread for k_msleep() support in EVENT_POLL_HOOK
58+ MICROPY_ZEPHYR_USE_IDLE_THREAD = 1
59+
60+ # Include Zephyr kernel build
61+ include $(TOP ) /extmod/zephyr_kernel/zephyr_kernel.mk
62+
63+ # Add Zephyr includes and flags
64+ # ZEPHYR_CFLAGS includes -include of the config header, which is CRITICAL
65+ # because any file that includes <zephyr/kernel.h> needs the CONFIG_* defines
66+ CFLAGS += $(ZEPHYR_INC )
67+ CFLAGS += $(ZEPHYR_CFLAGS )
68+ # Define MICROPY_ZEPHYR_THREADING as a preprocessor symbol for C code
69+ CFLAGS += -DMICROPY_ZEPHYR_THREADING=1
70+ CFLAGS += -DMICROPY_ZEPHYR_USE_IDLE_THREAD=1
71+
72+ # Add QEMU-specific Zephyr architecture implementation
73+ SRC_C += zephyr_arch_qemu.c
74+
75+ # Ensure Zephyr objects depend on generated headers
76+ $(addprefix $(BUILD ) /, $(SRC_THIRDPARTY_C :.c=.o)): $(ZEPHYR_GEN_HEADERS )
77+
78+ # Pattern-specific CFLAGS for Zephyr objects
79+ $(BUILD ) /$(TOP ) /lib/zephyr/% .o : CFLAGS += -include $(TOP ) /extmod/zephyr_kernel/zephyr_config_cortex_m.h -Wno-unused-parameter -Wno-sign-compare -Wno-unused-function -Wno-return-type
80+ $(BUILD ) /$(TOP ) /extmod/zephyr_kernel/% .o : CFLAGS += -Wno-unused-parameter -Wno-unused-function
81+
82+ # Build Zephyr assembly files (context switching, PSP initialization)
83+ ZEPHYR_SWAP_HELPER_O = $(BUILD ) /swap_helper.o
84+ ZEPHYR_PSP_SWITCH_O = $(BUILD ) /zephyr_psp_switch.o
85+ OBJ += $(ZEPHYR_SWAP_HELPER_O )
86+ OBJ += $(ZEPHYR_PSP_SWITCH_O )
87+ OBJ += $(BUILD ) /zephyr_offsets.o
88+
89+ # Common assembler flags for Zephyr .S files
90+ ZEPHYR_AS_FLAGS = -x assembler-with-cpp -D_ASMLANGUAGE \
91+ $(filter-out -std=% -Wdouble-promotion -Wfloat-conversion -Werror,$(CFLAGS ) ) \
92+ -include $(TOP ) /extmod/zephyr_kernel/zephyr_config_cortex_m.h \
93+ -Wa,-mimplicit-it=thumb
94+
95+ $(ZEPHYR_SWAP_HELPER_O ) : $(TOP ) /lib/zephyr/arch/arm/core/cortex_m/swap_helper.S $(ZEPHYR_GEN_HEADERS )
96+ $(ECHO ) " CC $<"
97+ $(Q )$(CC ) $(ZEPHYR_AS_FLAGS ) -c -o $@ $<
98+
99+ $(ZEPHYR_PSP_SWITCH_O ) : $(TOP ) /extmod/zephyr_kernel/zephyr_psp_switch.S
100+ $(ECHO ) " CC $<"
101+ $(Q )$(CC ) $(ZEPHYR_AS_FLAGS ) -c -o $@ $<
102+
103+ endif # MICROPY_ZEPHYR_THREADING
104+
49105GIT_SUBMODULES += lib/berkeley-db-1.xx
50106
51107CFLAGS += -DMICROPY_HEAP_SIZE=$(MICROPY_HEAP_SIZE )
@@ -172,6 +228,11 @@ CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -We
172228 -ffunction-sections -fdata-sections
173229CFLAGS += $(CFLAGS_EXTRA )
174230
231+ # Remove -Werror for Zephyr threading builds (Zephyr headers have many macro redefinitions)
232+ ifeq ($(MICROPY_ZEPHYR_THREADING ) ,1)
233+ CFLAGS := $(filter-out -Werror,$(CFLAGS ) )
234+ endif
235+
175236LDFLAGS += -T $(LDSCRIPT ) -Wl,--gc-sections -Wl,-Map=$(@:.elf=.map )
176237
177238# Debugging/Optimization
@@ -235,6 +296,8 @@ OBJ += $(addprefix $(BUILD)/, $(LIBM_SRC_C:.c=.o))
235296
236297# List of sources for qstr extraction
237298SRC_QSTR += $(SRC_C ) $(LIBM_SRC_C )
299+ # Add Zephyr MicroPython sources for root pointer scanning (e.g., mp_thread_list_head)
300+ SRC_QSTR += $(ZEPHYR_MP_SRC_C )
238301
239302# ###############################################################################
240303# Main targets
0 commit comments