10000 Merge remote-tracking branch 'upstream/main' into main · WaveyD/circuitpython@ee6bbec · GitHub
[go: up one dir, main page]

Skip to content

Commit ee6bbec

Browse files
Merge remote-tracking branch 'upstream/main' into main
2 parents 00a831f + ed1a44d commit ee6bbec

File tree

8 files changed

+177
-15
lines changed

8 files changed

+177
-15
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@
312312
[submodule "ports/raspberrypi/lib/lwip"]
313313
path = ports/raspberrypi/lib/lwip
314314
url = https://github.com/adafruit/lwip.git
315-
branch = circuitpython8
315+
branch = circuitpython9
316316
[submodule "lib/mbedtls"]
317317
path = lib/mbedtls
318318
url = https://github.com/ARMmbed/mbedtls.git

ports/raspberrypi/common-hal/microcontroller/__init__.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
#include "src/rp2_common/hardware_sync/include/hardware/sync.h"
2424

2525
#include "hardware/watchdog.h"
26+
#include "hardware/irq.h"
2627

2728
void common_hal_mcu_delay_us(uint32_t delay) {
2829
mp_hal_delay_us(delay);
2930
}
3031

3132
volatile uint32_t nesting_count = 0;
33+
#ifdef PICO_RP2040
3234
void common_hal_mcu_disable_interrupts(void) {
3335
// We don't use save_and_disable_interrupts() from the sdk because we don't want to worry about PRIMASK.
3436
// This is what we do on the SAMD21 via CMSIS.
@@ -48,6 +50,38 @@ void common_hal_mcu_enable_interrupts(void) {
4850
__dmb();
4951
asm volatile ("cpsie i" : : : "memory");
5052
}
53+
#else
54+
#include "src/rp2_common/cmsis/stub/CMSIS/Device/RP2350/Include/RP2350.h"
55+
#define PICO_ELEVATED_IRQ_PRIORITY (0x60) // between PICO_DEFAULT and PIOCO_HIGHEST_IRQ_PRIORITY
56+
static uint32_t oldBasePri = 0; // 0 (default) masks nothing, other values mask equal-or-larger priority values
57+
void common_hal_mcu_disable_interrupts(void) {
58+
if (nesting_count == 0) {
59+
// We must keep DMA_IRQ_1 (reserved for pico dvi) enabled at all times,
60+
// including during flash writes. Do this by setting the priority mask (BASEPRI
61+
// register).
62+
// grab old base priority
63+
oldBasePri = __get_BASEPRI();
64+
// and set the new one
65+
__set_BASEPRI_MAX(PICO_ELEVATED_IRQ_PRIORITY);
66+
__isb(); // Instruction synchronization barrier
67+
}
68+
nesting_count++;
69+
}
70+
71+
void common_hal_mcu_enable_interrupts(void) {
72+
uint32_t my_interrupts = save_and_disable_interrupts();
73+
if (nesting_count == 0) {
74+
reset_into_safe_mode(SAFE_MODE_INTERRUPT_ERROR);
75+
}
76+
nesting_count--;
77+
if (nesting_count == 0) {
78+
// return to the old priority setting
79+
__set_BASEPRI(oldBasePri);
80+
__isb(); // Instruction synchronization barrier
81+
}
82+
restore_interrupts(my_interrupts);
83+
}
84+
#endif
5185

5286
static bool next_reset_to_bootloader = false;
5387

ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self,
430430
dma_hw->inte1 = (1u << self->dma_pixel_channel);
431431
irq_set_exclusive_handler(DMA_IRQ_1, dma_irq_handler);
432432
irq_set_enabled(DMA_IRQ_1, true);
433+
irq_set_priority(DMA_IRQ_1, PICO_HIGHEST_IRQ_PRIORITY);
433434

434435
bus_ctrl_hw->priority = BUSCTRL_BUS_PRIORITY_DMA_W_BITS | BUSCTRL_BUS_PRIORITY_DMA_R_BITS;
435436

ports/raspberrypi/lib/lwip

Submodule lwip updated 151 files

ports/raspberrypi/lwip_inc/lwip_mem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2025 Bob Abeles
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
#include <stddef.h>
10+
11+
void *lwip_heap_malloc(size_t size);
12+
void lwip_heap_free(void *ptr);
13+
void *lwip_heap_calloc(size_t num, size_t size);

ports/raspberrypi/lwip_inc/lwipopts.h

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Common settings used in most of the pico_w examples
66
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details)
77

8+
#include "lwip_mem.h"
9+
810
// allow override in some examples
911
#ifndef NO_SYS
1012
#define NO_SYS 1
@@ -20,14 +22,62 @@
2022
#define MEM_LIBC_MALLOC 0
2123
#endif
2224
#define MEM_ALIGNMENT 4
23-
#define MEM_SIZE 4000
24-
#define MEMP_NUM_TCP_SEG 32
25-
#define MEMP_NUM_ARP_QUEUE 10
26-
#define PBUF_POOL_SIZE 24
25+
// MEM_USE_POOLS: mem_malloc uses pools of fixed size memory blocks. Default is 0.
26+
#define MEM_USE_POOLS 0
27+
// MEM_USE_POOLS_TRY_BIGGER_POOL: if one pool is empty, try the next bigger pool. Default is 0.
28+
#define MEM_USE_POOLS_TRY_BIGGER_POOL 0
29+
// MEMP_USE_CUSTOM_POOLS: Use custom pools defined in lwippools.h. Default is 0.
30+
#define MEMP_USE_CUSTOM_POOLS 0
31+
// MEMP_MEM_MALLOC: Use mem_malloc() for pool memory. Default is 0.
32+
#define MEMP_MEM_MALLOC 1
33+
#define MEM_CUSTOM_ALLOCATOR 1
34+
#define MEM_CUSTOM_FREE lwip_heap_free
35+
#define MEM_CUSTOM_MALLOC lwip_heap_malloc
36+
#define MEM_CUSTOM_CALLOC lwip_heap_calloc
37+
38+
// MEM_SIZE: The LWIP heap size. Memory for mem_malloc and mem_calloc are allocated from
39+
// this heap. If MEMP_MEM_MALLOC is set to 1, memory for memp_malloc is also allocated from
40+
// this heap; if it is 0, memory is statically pre-allocated for each pool.
41+
// Default is 1600.
42+
#define MEM_SIZE 1600
43+
// MEMP_NUM_PBUF: memp pbufs used when sending from static memory. Default is 16.
44+
#define MEMP_NUM_PBUF 16
45+
// MEMP_NUM_RAW_PCB: Number of raw connection PCBs. Default is 4.
46+
#define MEMP_NUM_RAW_PCB 4
47+
// MEMP_NUM_UDP_PCB: Number of UDP PCBs. Default is 4.
48+
#define MEMP_NUM_UDP_PCB 4
49+
// MEMP_NUM_TCP_PCB: Number of simultaneously active TCP connections. Default is 5.
50+
#define MEMP_NUM_TCP_PCB 5
51+
// MEMP_NUM_TCP_PCB_LISTEN: Number of listening TCP PCBs. Default is 8.
52+
#define MEMP_NUM_TCP_PCB_LISTEN 8
53+
// MEMP_NUM_TCP_SEG: Number of simultaneously queued TCP segments. Default is 16.
54+
#define MEMP_NUM_TCP_SEG 16
55+
// MEMP_NUM_ALTCP_PCB: Number of simultaneously active altcp connections. Default is 5.
56+
#define MEMP_NUM_ALTCP_PCB 5
57+
// MEMP_NUM_REASSDATA: Number of simultaneously IP packets queued for reassembly. Default is 5.
58+
#define MEMP_NUM_REASSDATA 5
59+
// MEMP_NUM_FRAG_PBUF: Number of simultaneously IP fragments. Default is 15.
60+
#define MEMP_NUM_FRAG_PBUF 15
61+
// MEMP_NUM_ARP_QUEUE: Number of simultaneously queued ARP packets. Default is 30.
62+
#define MEMP_NUM_ARP_QUEUE 30
63+
// MEMP_NUM_IGMP_GROUP: Number of simultaneously active IGMP groups. Default is 8.
64+
#define MEMP_NUM_IGMP_GROUP 8
65+
// MEMP_NUM_SYS_TIMEOUT: Number of simultaneously active timeouts.
66+
// Use calculated default based on enabled modules.
67+
68+
// PBUF_POOL_SIZE: Number of pbufs in the pbuf pool. Default is 16.
69+
#define PBUF_POOL_SIZE 16
70+
71+
// LWIP's default 250 ms periodic timer interval is too long, resulting in network
72+
// performance issues. We reduce it to 25 ms giving a slow-timer of 50 ms and a
73+
// fast-timer of 25 ms.
74+
#define TCP_TMR_INTERVAL 25
75+
2776
#define LWIP_ARP 1
2877
#define LWIP_ETHERNET 1
2978
#define LWIP_ICMP 1
3079
#define LWIP_RAW 1
80+
3181
#define TCP_WND (8 * TCP_MSS)
3282
#define TCP_MSS 1460
3383
#define TCP_SND_BUF (8 * TCP_MSS)

ports/raspberrypi/lwip_src/lwip_mem.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2025 Bob Abeles
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include <stdint.h>
8+
#include <string.h> F438
9+
#include "lib/tlsf/tlsf.h"
10+
#include "lwip_mem.h"
11+
#include "supervisor/port_heap.h"
12+
13+
void *lwip_heap_malloc(size_t size) {
14+
return port_malloc(size, true);
15+
}
16+
17+
void lwip_heap_free(void *ptr) {
18+
port_free(ptr);
19+
}
20+
21+
void *lwip_heap_calloc(size_t num, size_t size) {
22+
void *ptr = lwip_heap_malloc(num * size);
23+
if (ptr != NULL) {
24+
memset(ptr, 0, num * size);
25+
}
26+
return ptr;
27+
}

ports/raspberrypi/supervisor/port.c

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@
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

6065
critical_section_t background_queue_lock;
6166

@@ -83,20 +88,18 @@ extern uint32_t _ld_itcm_destination;
8388
extern uint32_t _ld_itcm_size;
8489
extern 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-
100103
static 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+
251259
void *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

283290
safe_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

499510
void 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

Comments
 (0)
0