Skip to content

Commit 4fc6cf9

Browse files
committed
rp2: Add support for RP2350 in RISCV mode.
As part of this change, the RV32I native emitter is enabled on RISCV board variants. Signed-off-by: Damien George <[email protected]>
1 parent 34e463d commit 4fc6cf9

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ include(${MICROPY_DIR}/py/usermod.cmake)
9393

9494
add_executable(${MICROPY_TARGET})
9595

96+
# Provide a C-level definitions of PICO_ARM.
97+
# (The pico-sdk already defines PICO_RISCV when it's enabled.)
98+
if(PICO_ARM)
99+
target_compile_definitions(pico_platform_headers INTERFACE
100+
PICO_ARM=1
101+
)
102+
endif()
103+
96104
set(MICROPY_QSTRDEFS_PORT
97105
${MICROPY_PORT_DIR}/qstrdefsport.h
98106
)
@@ -108,7 +116,6 @@ set(MICROPY_SOURCE_LIB
108116
${MICROPY_DIR}/shared/netutils/netutils.c
109117
${MICROPY_DIR}/shared/netutils/trace.c
110118
${MICROPY_DIR}/shared/readline/readline.c
111-
${MICROPY_DIR}/shared/runtime/gchelper_thumb1.s
112119
${MICROPY_DIR}/shared/runtime/gchelper_native.c
113120
${MICROPY_DIR}/shared/runtime/interrupt_char.c
114121
${MICROPY_DIR}/shared/runtime/mpirq.c
@@ -123,6 +130,16 @@ set(MICROPY_SOURCE_LIB
123130
${MICROPY_DIR}/shared/tinyusb/mp_usbd_runtime.c
124131
)
125132

133+
if(PICO_ARM)
134+
list(APPEND MICROPY_SOURCE_LIB
135+
${MICROPY_DIR}/shared/runtime/gchelper_thumb1.s
136+
)
137+
elseif(PICO_RISCV)
138+
list(APPEND MICROPY_SOURCE_LIB
139+
${MICROPY_DIR}/shared/runtime/gchelper_rv32i.s
140+
)
141+
endif()
142+
126143
set(MICROPY_SOURCE_DRIVERS
127144
${MICROPY_DIR}/drivers/bus/softspi.c
128145
${MICROPY_DIR}/drivers/dht/dht.c
@@ -178,7 +195,6 @@ set(MICROPY_SOURCE_QSTR
178195
)
179196

180197
set(PICO_SDK_COMPONENTS
181-
cmsis_core
182198
hardware_adc
183199
hardware_base
184200
hardware_boot_lock
@@ -222,6 +238,17 @@ set(PICO_SDK_COMPONENTS
222238
tinyusb_device
223239
)
224240

241+
if(PICO_ARM)
242+
list(APPEND PICO_SDK_COMPONENTS
243+
cmsis_core
244+
)
245+
elseif(PICO_RISCV)
246+
list(APPEND PICO_SDK_COMPONENTS
247+
hardware_hazard3
248+
hardware_riscv
249+
)
250+
endif()
251+
225252
# Use our custom pico_float_micropython float implementation. This is needed for two reasons:
226253
# - to fix inf handling in pico-sdk's __wrap___aeabi_fadd();
227254
# - so we can use our own libm functions, to fix inaccuracies in the pico-sdk versions.
@@ -243,7 +270,7 @@ if(PICO_RP2040)
243270
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_init_rom_rp2040.c
244271
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_v1_rom_shim_rp2040.S
245272
)
246-
elseif(PICO_RP2350)
273+
elseif(PICO_RP2350 AND PICO_ARM)
247274
target_sources(pico_float_micropython INTERFACE
248275
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_aeabi_dcp.S
249276
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_conv_m33.S
@@ -491,6 +518,12 @@ target_link_options(${MICROPY_TARGET} PRIVATE
491518
-Wl,--wrap=runtime_init_clocks
492519
)
493520

521+
if(PICO_RP2350)
522+
target_link_options(${MICROPY_TARGET} PRIVATE
523+
-Wl,--defsym=__micropy_extra_stack__=4096
524+
)
525+
endif()
526+
494527
# Apply optimisations to performance-critical source code.
495528
set_source_files_properties(
496529
${MICROPY_PY_DIR}/map.c
@@ -563,7 +596,7 @@ endif()
563596

564597
pico_add_extra_outputs(${MICROPY_TARGET})
565598

566-
pico_find_compiler(PICO_COMPILER_SIZE ${PICO_GCC_TRIPLE}-size)
599+
pico_find_compiler_with_triples(PICO_COMPILER_SIZE "${PICO_GCC_TRIPLE}" size)
567600

568601
add_custom_command(TARGET ${MICROPY_TARGET}
569602
POST_BUILD

ports/rp2/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@
6060
#endif
6161
#if PICO_RP2040
6262
#include "RP2040.h" // cmsis, for PendSV_IRQn and SCB/SCB_SCR_SEVONPEND_Msk
63-
#elif PICO_RP2350
63+
#elif PICO_RP2350 && PICO_ARM
6464
#include "RP2350.h" // cmsis, for PendSV_IRQn and SCB/SCB_SCR_SEVONPEND_Msk
65-
#else
66-
#error Unknown processor
6765
#endif
6866
#include "pico/aon_timer.h"
6967
#include "shared/timeutils/timeutils.h"
@@ -82,7 +80,9 @@ bi_decl(bi_program_feature_group_with_flags(BINARY_INFO_TAG_MICROPYTHON,
8280

8381
int main(int argc, char **argv) {
8482
// This is a tickless port, interrupts should always trigger SEV.
83+
#if PICO_ARM
8584
SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
85+
#endif
8686

8787
pendsv_init();
8888
soft_timer_init();

ports/rp2/mpconfigport.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@
7878

7979
// MicroPython emitters
8080
#define MICROPY_PERSISTENT_CODE_LOAD (1)
81+
#if PICO_ARM
8182
#define MICROPY_EMIT_THUMB (1)
82-
#define MICROPY_EMIT_THUMB_ARMV7M (0)
8383
#define MICROPY_EMIT_INLINE_THUMB (1)
84+
#if PICO_RP2040
85+
#define MICROPY_EMIT_THUMB_ARMV7M (0)
8486
#define MICROPY_EMIT_INLINE_THUMB_FLOAT (0)
87+
#endif
88+
#elif PICO_RISCV
89+
#define MICROPY_EMIT_RV32 (1)
90+
#endif
8591

8692
// Optimisations
8793
#define MICROPY_OPT_COMPUTED_GOTO (1)

ports/rp2/pendsv.c

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

3232
#if PICO_RP2040
3333
#include "RP2040.h"
34-
#elif PICO_RP2350
34+
#elif PICO_RP2350 && PICO_ARM
3535
#include "RP2350.h"
36-
#else
37-
#error Unknown chip
36+
#elif PICO_RISCV
37+
#include "pico/aon_timer.h"
3838
#endif
3939

4040
#if MICROPY_PY_NETWORK_CYW43
@@ -43,6 +43,8 @@
4343

4444
static pendsv_dispatch_t pendsv_dispatch_table[PENDSV_DISPATCH_NUM_SLOTS];
4545

46+
void PendSV_Handler(void);
47+
4648
// Using the nowait variant here as softtimer updates PendSV from the loop of mp_wfe_or_timeout(),
4749
// where we don't want the CPU event bit to be set.
4850
static recursive_mutex_nowait_t pendsv_mutex;
@@ -75,10 +77,16 @@ void pendsv_resume(void) {
7577
void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f) {
7678
pendsv_dispatch_table[slot] = f;
7779
if (pendsv_mutex.mutex.enter_count == 0) {
80+
#if PICO_ARM
7881
// There is a race here where other core calls pendsv_suspend() before
7982
// ISR can execute, but dispatch will happen later when other core
8083
// calls pendsv_resume().
8184
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
85+
#elif PICO_RISCV
86+
struct timespec ts;
87+
aon_timer_get_time(&ts);
88+
aon_timer_enable_alarm(&ts, PendSV_Handler, false);
89+
#endif
8290
} else {
8391
#if MICROPY_PY_NETWORK_CYW43
8492
CYW43_STAT_INC(PENDSV_DISABLED_COUNT);

0 commit comments

Comments
 (0)