5252#include "pico/bootrom.h"
5353#include "hardware/watchdog.h"
5454
55+ #ifdef PICO_RP2350
56+ #include "src/rp2_common/cmsis/stub/CMSIS/Device/RP2350/Include/RP2350.h"
57+ #endif
58+
5559#include "supervisor/shared/serial.h"
5660
5761#include "tusb.h"
5862#include <cmsis_compiler.h>
63+ #include "lib/tlsf/tlsf.h"
5964
6065critical_section_t background_queue_lock ;
6166
@@ -83,20 +88,18 @@ extern uint32_t _ld_itcm_destination;
8388extern uint32_t _ld_itcm_size ;
8489extern uint32_t _ld_itcm_flash_copy ;
8590
86- #ifdef CIRCUITPY_PSRAM_CHIP_SELECT
91+ static tlsf_t _heap = NULL ;
92+ static pool_t _ram_pool = NULL ;
93+ static pool_t _psram_pool = NULL ;
94+ static size_t _psram_size = 0 ;
8795
88- #include "lib/tlsf/tlsf.h"
96+ #ifdef CIRCUITPY_PSRAM_CHIP_SELECT
8997
9098#include "src/rp2350/hardware_regs/include/hardware/regs/qmi.h"
9199#include "src/rp2350/hardware_regs/include/hardware/regs/xip.h"
92100#include "src/rp2350/hardware_structs/include/hardware/structs/qmi.h"
93101#include "src/rp2350/hardware_structs/include/hardware/structs/xip_ctrl.h"
94102
95- static tlsf_t _heap = NULL ;
96- static pool_t _ram_pool = NULL ;
97- static pool_t _psram_pool = NULL ;
98- static size_t _psram_size = 0 ;
99-
100103static void __no_inline_not_in_flash_func (setup_psram )(void ) {
101104 gpio_set_function (CIRCUITPY_PSRAM_CHIP_SELECT -> number , GPIO_FUNC_XIP_CS1 );
102105 _psram_size = 0 ;
@@ -236,8 +239,9 @@ static void __no_inline_not_in_flash_func(setup_psram)(void) {
236239 return ;
237240 }
238241}
242+ #endif
239243
240- void port_heap_init (void ) {
244+ static void _port_heap_init (void ) {
241245 uint32_t * heap_bottom = port_heap_get_bottom ();
242246 uint32_t * heap_top = port_heap_get_top ();
243247 size_t size = (heap_top - heap_bottom ) * sizeof (uint32_t );
@@ -248,6 +252,10 @@ void port_heap_init(void) {
248252 }
249253}
250254
255+ void port_heap_init (void ) {
256+ // We call _port_heap_init from port_init to initialize the heap early.
257+ }
258+
251259void * port_malloc (size_t size , bool dma_capable ) {
252260 void * block = tlsf_malloc (_heap , size );
253261 return block ;
@@ -278,7 +286,6 @@ size_t port_heap_get_largest_free_size(void) {
278286 // IDF does this. Not sure why.
279287 return tlsf_fit_size (_heap , max_size );
280288}
281- #endif
282289
283290safe_mode_t port_init (void ) {
284291 _binary_info ();
@@ -344,6 +351,9 @@ safe_mode_t port_init(void) {
344351 setup_psram ();
345352 #endif
346353
354+ // Initialize heap early to allow for early allocation.
355+ _port_heap_init ();
356+
347357 // Check brownout.
348358
349359 #if CIRCUITPY_CYW43
@@ -352,6 +362,7 @@ safe_mode_t port_init(void) {
352362 // are intended to meet the power on timing requirements, but apparently
353363 // are inadequate. We'll back off this long delay based on future testing.
354364 mp_hal_delay_ms (1000 );
365+
355366 // Change this as a placeholder as to how to init with country code.
356367 // Default country code is CYW43_COUNTRY_WORLDWIDE)
357368 if (cyw43_arch_init_with_country (PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE )) {
@@ -497,6 +508,7 @@ void port_interrupt_after_ticks(uint32_t ticks) {
497508}
498509
499510void port_idle_until_interrupt (void ) {
511+ #ifdef PICO_RP2040
500512 common_hal_mcu_disable_interrupts ();
501513 #if CIRCUITPY_USB_HOST
502514 if (!background_callback_pending () && !tud_task_event_ready () && !tuh_task_event_ready () && !_woken_up ) {
@@ -507,6 +519,31 @@ void port_idle_until_interrupt(void) {
507519 __WFI ();
508520 }
509521 common_hal_mcu_enable_interrupts ();
522+ #else
523+ // because we use interrupt priority, don't use
524+ // common_hal_mcu_disable_interrupts (because an interrupt masked by
525+ // BASEPRI will not occur)
526+ uint32_t state = save_and_disable_interrupts ();
527+
528+ // Ensure BASEPRI is at 0...
529+ uint32_t oldBasePri = __get_BASEPRI ();
530+ __set_BASEPRI (0 );
531+ __isb ();
532+ #if CIRCUITPY_USB_HOST
533+ if (!background_callback_pending () && !tud_task_event_ready () && !tuh_task_event_ready () && !_woken_up ) {
534+ #else
535+ if (!background_callback_pending () && !tud_task_event_ready () && !_woken_up ) {
536+ #endif
537+ __DSB ();
538+ __WFI ();
539+ }
540+
541+ // and restore basepri before reenabling interrupts
542+ __set_BASEPRI (oldBasePri );
543+ __isb ();
544+
545+ restore_interrupts (state );
546+ #endif
510547}
511548
512549/**
0 commit comments