Skip to content

Commit d2c85c7

Browse files
peterharperukdpgeorge
authored andcommitted
rp2: Integrate RP2350 and use aon_timer instead of rtc API.
This commit separates various build settings and include files that are specific to RP2040 and RP2350, and uses the aon_timer interface instead of rtc, to work across both MCU variants. Signed-off-by: Damien George <damien@micropython.org> Signed-off-by: Phil Howard <phil@gadgetoid.com>
1 parent 27aeade commit d2c85c7

11 files changed

Lines changed: 142 additions & 90 deletions

File tree

ports/rp2/CMakeLists.txt

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,26 @@ set(PICO_SDK_COMPONENTS
191191
hardware_pll
192192
hardware_pwm
193193
hardware_regs
194-
hardware_rtc
194+
hardware_resets
195195
hardware_spi
196196
hardware_structs
197197
hardware_sync
198+
hardware_sync_spin_lock
198199
hardware_timer
199200
hardware_uart
200201
hardware_watchdog
201202
hardware_xosc
203+
pico_aon_timer
202204
pico_base_headers
203205
pico_binary_info
204206
pico_bootrom
205207
pico_multicore
206208
pico_platform
209+
pico_platform_compiler
210+
pico_platform_panic
211+
pico_platform_sections
212+
pico_runtime
213+
pico_runtime_init
207214
pico_stdio
208215
pico_stdlib
209216
pico_sync
@@ -224,14 +231,24 @@ pico_add_library(pico_float_micropython)
224231

225232
# pico_float_micropython: add pico-sdk float and our libm source files.
226233
target_sources(pico_float_micropython INTERFACE
227-
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_aeabi.S
228-
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_init_rom.c
229-
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_v1_rom_shim.S
230234
${MICROPY_SOURCE_LIB_LIBM}
231235
${MICROPY_SOURCE_LIB_LIBM_SQRT_SW}
232236
${MICROPY_PORT_DIR}/libm_extra.c
233237
)
234238

239+
if(PICO_RP2040)
240+
target_sources(pico_float_micropython INTERFACE
241+
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_aeabi_rp2040.S
242+
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_init_rom_rp2040.c
243+
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_v1_rom_shim_rp2040.S
244+
)
245+
elseif(PICO_RP2350)
246+
target_sources(pico_float_micropython INTERFACE
247+
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_aeabi_dcp.S
248+
${PICO_SDK_PATH}/src/rp2_common/pico_float/float_conv_m33.S
249+
)
250+
endif()
251+
235252
# pico_float_micropython: wrap low-level floating-point ops, to call the pico-sdk versions.
236253
pico_wrap_function(pico_float_micropython __aeabi_fdiv)
237254
pico_wrap_function(pico_float_micropython __aeabi_fmul)
@@ -253,7 +270,9 @@ pico_wrap_function(pico_float_micropython __aeabi_ul2f)
253270
pico_wrap_function(pico_float_micropython __aeabi_f2iz)
254271
pico_wrap_function(pico_float_micropython __aeabi_f2lz)
255272
pico_wrap_function(pico_float_micropython __aeabi_f2uiz)
256-
pico_wrap_function(pico_float_micropython __aeabi_f2ulz)
273+
if(PICO_RP2040)
274+
pico_wrap_function(pico_float_micropython __aeabi_f2ulz)
275+
endif()
257276
pico_wrap_function(pico_float_micropython __aeabi_f2d)
258277

259278
if (MICROPY_PY_LWIP)
@@ -507,9 +526,18 @@ target_compile_definitions(${MICROPY_TARGET} PRIVATE
507526
PICO_NO_PROGRAM_VERSION_STRING=1 # do it ourselves in main.c
508527
MICROPY_BUILD_TYPE="${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} ${CMAKE_BUILD_TYPE}"
509528
PICO_NO_BI_STDIO_UART=1 # we call it UART REPL
510-
PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1
511529
)
512530
531+
if(PICO_RP2040)
532+
target_compile_definitions(${MICROPY_TARGET} PRIVATE
533+
PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1
534+
)
535+
elseif(PICO_RP2350)
536+
target_compile_definitions(${MICROPY_TARGET} PRIVATE
537+
PICO_EMBED_XIP_SETUP=1 # to put flash into continuous read mode
538+
)
539+
endif()
540+
513541
target_link_libraries(${MICROPY_TARGET}
514542
${PICO_SDK_COMPONENTS}
515543
)
@@ -525,7 +553,11 @@ endif()
525553
# a linker script modification) until we explicitly add macro calls around the function
526554
# defs to move them into RAM.
527555
if (PICO_ON_DEVICE AND NOT PICO_NO_FLASH AND NOT PICO_COPY_TO_RAM)
528-
pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp_${PICO_PLATFORM}.ld)
556+
if(PICO_RP2040)
557+
pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp_rp2040.ld)
558+
elseif(PICO_RP2350)
559+
pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp_rp2350.ld)
560+
endif()
529561
endif()
530562
531563
pico_add_extra_outputs(${MICROPY_TARGET})
@@ -544,6 +576,9 @@ foreach(comp ${PICO_SDK_COMPONENTS})
544576
micropy_gather_target_properties(${comp}_headers)
545577
endforeach()
546578
579+
set(MICROPY_CPP_FLAGS_EXTRA ${PICO_COMMON_LANG_FLAGS})
580+
separate_arguments(MICROPY_CPP_FLAGS_EXTRA)
581+
547582
# Include the main MicroPython cmake rules.
548583
include(${MICROPY_DIR}/py/mkrules.cmake)
549584
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# cmake file for Raspberry Pi Pico
22
set(PICO_BOARD "pico")
3+
set(PICO_PLATFORM "rp2040")

ports/rp2/fatfs_port.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
*/
2626

2727
#include "lib/oofatfs/ff.h"
28-
#include "hardware/rtc.h"
28+
#include "pico/aon_timer.h"
29+
#include "shared/timeutils/timeutils.h"
2930

3031
MP_WEAK DWORD get_fattime(void) {
31-
datetime_t t;
32-
rtc_get_datetime(&t);
33-
return ((t.year - 1980) << 25) | ((t.month) << 21) | ((t.day) << 16) | ((t.hour) << 11) | ((t.min) << 5) | (t.sec / 2);
32+
struct timespec ts;
33+
timeutils_struct_time_t tm;
34+
aon_timer_get_time(&ts);
35+
timeutils_seconds_since_epoch_to_struct_time(ts.tv_sec, &tm);
36+
return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) | ((tm.tm_mday) << 16) | ((tm.tm_hour) << 11) | ((tm.tm_min) << 5) | (tm.tm_sec / 2);
3437
}

ports/rp2/machine_rtc.c

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
#include <time.h>
3131
#include <sys/time.h>
3232

33+
#include "pico/aon_timer.h"
34+
3335
#include "py/nlr.h"
3436
#include "py/obj.h"
3537
#include "py/runtime.h"
3638
#include "py/mphal.h"
3739
#include "py/mperrno.h"
3840
#include "extmod/modmachine.h"
3941
#include "shared/timeutils/timeutils.h"
40-
#include "hardware/rtc.h"
41-
#include "pico/util/datetime.h"
4242

4343
typedef struct _machine_rtc_obj_t {
4444
mp_obj_base_t base;
@@ -50,14 +50,12 @@ static const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
5050
static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
5151
// check arguments
5252
mp_arg_check_num(n_args, n_kw, 0, 0, false);
53-
bool r = rtc_running();
53+
bool r = aon_timer_is_running();
5454

5555
if (!r) {
56-
// This shouldn't happen as rtc_init() is already called in main so
57-
// it's here just in case
58-
rtc_init();
59-
datetime_t t = { .month = 1, .day = 1 };
60-
rtc_set_datetime(&t);
56+
// This shouldn't happen. it's here just in case
57+
struct timespec ts = { 0, 0 };
58+
aon_timer_start(&ts);
6159
mp_hal_time_ns_set_from_rtc();
6260
}
6361
// return constant object
@@ -66,47 +64,36 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
6664

6765
static mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
6866
if (n_args == 1) {
69-
bool ret;
70-
datetime_t t;
71-
72-
ret = rtc_get_datetime(&t);
73-
if (!ret) {
74-
mp_raise_OSError(MP_EIO);
75-
}
76-
67+
struct timespec ts;
68+
timeutils_struct_time_t tm;
69+
aon_timer_get_time(&ts);
70+
timeutils_seconds_since_epoch_to_struct_time(ts.tv_sec, &tm);
7771
mp_obj_t tuple[8] = {
78-
mp_obj_new_int(t.year),
79-
mp_obj_new_int(t.month),
80-
mp_obj_new_int(t.day),
81-
mp_obj_new_int(t.dotw),
82-
mp_obj_new_int(t.hour),
83-
mp_obj_new_int(t.min),
84-
mp_obj_new_int(t.sec),
85-
mp_obj_new_int(0)
72+
mp_obj_new_int(tm.tm_year),
73+
mp_obj_new_int(tm.tm_mon),
74+
mp_obj_new_int(tm.tm_mday),
75+
mp_obj_new_int(tm.tm_wday),
76+
mp_obj_new_int(tm.tm_hour),
77+
mp_obj_new_int(tm.tm_min),
78+
mp_obj_new_int(tm.tm_sec),
79+
mp_obj_new_int(0),
8680
};
87-
8881
return mp_obj_new_tuple(8, tuple);
8982
} else {
9083
mp_obj_t *items;
91-
9284
mp_obj_get_array_fixed_n(args[1], 8, &items);
93-
94-
datetime_t t = {
95-
.year = mp_obj_get_int(items[0]),
96-
.month = mp_obj_get_int(items[1]),
97-
.day = mp_obj_get_int(items[2]),
98-
.hour = mp_obj_get_int(items[4]),
99-
.min = mp_obj_get_int(items[5]),
100-
.sec = mp_obj_get_int(items[6]),
85+
timeutils_struct_time_t tm = {
86+
.tm_year = mp_obj_get_int(items[0]),
87+
.tm_mon = mp_obj_get_int(items[1]),
88+
.tm_mday = mp_obj_get_int(items[2]),
89+
.tm_hour = mp_obj_get_int(items[4]),
90+
.tm_min = mp_obj_get_int(items[5]),
91+
.tm_sec = mp_obj_get_int(items[6]),
10192
};
102-
// Deliberately ignore the weekday argument and compute the proper value
103-
t.dotw = timeutils_calc_weekday(t.year, t.month, t.day);
104-
105-
if (!rtc_set_datetime(&t)) {
106-
mp_raise_OSError(MP_EINVAL);
107-
}
93+
struct timespec ts = { 0, 0 };
94+
ts.tv_sec = timeutils_seconds_since_epoch(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
95+
aon_timer_set_time(&ts);
10896
mp_hal_time_ns_set_from_rtc();
109-
11097
}
11198
return mp_const_none;
11299
}

ports/rp2/main.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@
4747
#include "genhdr/mpversion.h"
4848
#include "mp_usbd.h"
4949

50-
#include "RP2040.h" // cmsis, for PendSV_IRQn and SCB/SCB_SCR_SEVONPEND_Msk
5150
#include "pico/stdlib.h"
5251
#include "pico/binary_info.h"
5352
#include "pico/unique_id.h"
54-
#include "hardware/rtc.h"
5553
#include "hardware/structs/rosc.h"
5654
#if MICROPY_PY_LWIP
5755
#include "lwip/init.h"
@@ -60,6 +58,15 @@
6058
#if MICROPY_PY_NETWORK_CYW43
6159
#include "lib/cyw43-driver/src/cyw43.h"
6260
#endif
61+
#if PICO_RP2040
62+
#include "RP2040.h" // cmsis, for PendSV_IRQn and SCB/SCB_SCR_SEVONPEND_Msk
63+
#elif PICO_RP2350
64+
#include "RP2350.h" // cmsis, for PendSV_IRQn and SCB/SCB_SCR_SEVONPEND_Msk
65+
#else
66+
#error Unknown processor
67+
#endif
68+
#include "pico/aon_timer.h"
69+
#include "shared/timeutils/timeutils.h"
6370

6471
extern uint8_t __StackTop, __StackBottom;
6572
extern uint8_t __GcHeapStart, __GcHeapEnd;
@@ -106,17 +113,9 @@ int main(int argc, char **argv) {
106113
#endif
107114

108115
// Start and initialise the RTC
109-
datetime_t t = {
110-
.year = 2021,
111-
.month = 1,
112-
.day = 1,
113-
.dotw = 4, // 0 is Monday, so 4 is Friday
114-
.hour = 0,
115-
.min = 0,
116-
.sec = 0,
117-
};
118-
rtc_init();
119-
rtc_set_datetime(&t);
116+
struct timespec ts = { 0, 0 };
117+
ts.tv_sec = timeutils_seconds_since_epoch(2021, 1, 1, 0, 0, 0);
118+
aon_timer_start(&ts);
120119
mp_hal_time_ns_set_from_rtc();
121120

122121
// Initialise stack extents and GC heap.

ports/rp2/mbedtls/mbedtls_port.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
#include "mbedtls_config_port.h"
3131

32-
#include "hardware/rtc.h"
3332
#include "shared/timeutils/timeutils.h"
3433
#include "mbedtls/platform_time.h"
34+
#include "pico/aon_timer.h"
3535

3636
extern uint8_t rosc_random_u8(size_t cycles);
3737

@@ -44,9 +44,9 @@ int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t
4444
}
4545

4646
time_t rp2_rtctime_seconds(time_t *timer) {
47-
datetime_t t;
48-
rtc_get_datetime(&t);
49-
return timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.min, t.sec);
47+
struct timespec ts;
48+
aon_timer_get_time(&ts);
49+
return ts.tv_sec;
5050
}
5151

5252
mbedtls_ms_time_t mbedtls_ms_time(void) {

ports/rp2/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "pico/bootrom.h"
4242
#include "pico/stdlib.h"
4343
#include "pico/unique_id.h"
44+
#include "pico/runtime_init.h"
4445
#if MICROPY_PY_NETWORK_CYW43
4546
#include "lib/cyw43-driver/src/cyw43.h"
4647
#endif

ports/rp2/modtime.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,30 @@
2626

2727
#include "py/obj.h"
2828
#include "shared/timeutils/timeutils.h"
29-
#include "hardware/rtc.h"
29+
#include "pico/aon_timer.h"
3030

3131
// Return the localtime as an 8-tuple.
3232
static mp_obj_t mp_time_localtime_get(void) {
33-
datetime_t t;
34-
rtc_get_datetime(&t);
33+
struct timespec ts;
34+
aon_timer_get_time(&ts);
35+
timeutils_struct_time_t tm;
36+
timeutils_seconds_since_epoch_to_struct_time(ts.tv_sec, &tm);
3537
mp_obj_t tuple[8] = {
36-
mp_obj_new_int(t.year),
37-
mp_obj_new_int(t.month),
38-
mp_obj_new_int(t.day),
39-
mp_obj_new_int(t.hour),
40-
mp_obj_new_int(t.min),
41-
mp_obj_new_int(t.sec),
42-
mp_obj_new_int(t.dotw),
43-
mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)),
38+
mp_obj_new_int(tm.tm_year),
39+
mp_obj_new_int(tm.tm_mon),
40+
mp_obj_new_int(tm.tm_mday),
41+
mp_obj_new_int(tm.tm_hour),
42+
mp_obj_new_int(tm.tm_min),
43+
mp_obj_new_int(tm.tm_sec),
44+
mp_obj_new_int(tm.tm_wday),
45+
mp_obj_new_int(tm.tm_yday),
4446
};
4547
return mp_obj_new_tuple(8, tuple);
4648
}
4749

4850
// Return the number of seconds since the Epoch.
4951
static mp_obj_t mp_time_time_get(void) {
50-
datetime_t t;
51-
rtc_get_datetime(&t);
52-
return mp_obj_new_int_from_ull(timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.min, t.sec));
52+
struct timespec ts;
53+
aon_timer_get_time(&ts);
54+
return mp_obj_new_int_from_ull(ts.tv_sec);
5355
}

0 commit comments

Comments
 (0)