Skip to content

Commit ce77d2f

Browse files
GadgetoidgigapodMichaelBell
committed
ports/rp2: PSRAM support.
Add PSRAM support with auto detection. Performs a best-effort attempt to detect attached PSRAM, configure it and *add* it to the MicroPython heap. If PSRAM is not present, should fall back to use internal RAM. Introduce two new port/board defines: * MICROPY_HW_ENABLE_PSRAM to enable PSRAM. * MICROPY_HW_PSRAM_CS_PIN to define the chip-select pin. Changes: ports/rp2/rp2_psram.c/h: Add new PSRAM module. ports/rp2/main.c: Add optional PSRAM support. ports/rp2/CMakeLists.txt: Include rp2_psram.c. ports/rp2/mpconfigport.h: Add MICROPY_HW_ENABLE_PSRAM. ports/rp2/modmachine.c: Reconfigure flash on freq change. Co-authored-by: Kirk Benell <[email protected]> Co-authored-by: Mike Bell <[email protected]> Signed-off-by: Phil Howard <[email protected]>
1 parent 5eee5a6 commit ce77d2f

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ set(MICROPY_SOURCE_PORT
173173
${CMAKE_BINARY_DIR}/pins_${MICROPY_BOARD}.c
174174
)
175175

176+
if(PICO_RP2350)
177+
list(APPEND MICROPY_SOURCE_PORT
178+
rp2_psram.c
179+
)
180+
endif()
181+
176182
set(MICROPY_SOURCE_QSTR
177183
${MICROPY_SOURCE_PY}
178184
${MICROPY_DIR}/shared/readline/readline.c

ports/rp2/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdio.h>
2828

29+
#include "rp2_psram.h"
2930
#include "py/compile.h"
3031
#include "py/cstack.h"
3132
#include "py/runtime.h"
@@ -93,6 +94,10 @@ int main(int argc, char **argv) {
9394
// Hook for setting up anything that needs to be super early in the boot-up process.
9495
MICROPY_BOARD_STARTUP();
9596

97+
#if MICROPY_HW_ENABLE_PSRAM
98+
size_t psram_size = psram_init(MICROPY_HW_PSRAM_CS_PIN);
99+
#endif
100+
96101
#if MICROPY_HW_ENABLE_UART_REPL
97102
bi_decl(bi_program_feature("UART REPL"))
98103
setup_default_uart();
@@ -120,7 +125,21 @@ int main(int argc, char **argv) {
120125

121126
// Initialise stack extents and GC heap.
122127
mp_cstack_init_with_top(&__StackTop, &__StackTop - &__StackBottom);
128+
129+
#if MICROPY_HW_ENABLE_PSRAM
130+
if (psram_size) {
131+
#if MICROPY_GC_SPLIT_HEAP
132+
gc_init(&__GcHeapStart, &__GcHeapEnd);
133+
gc_add((void *)PSRAM_LOCATION, (void *)(PSRAM_LOCATION + psram_size));
134+
#else
135+
gc_init((void *)PSRAM_LOCATION, (void *)(PSRAM_LOCATION + psram_size));
136+
#endif
137+
} else {
138+
gc_init(&__GcHeapStart, &__GcHeapEnd);
139+
}
140+
#else
123141
gc_init(&__GcHeapStart, &__GcHeapEnd);
142+
#endif
124143

125144
#if MICROPY_PY_LWIP
126145
// lwIP doesn't allow to reinitialise itself by subsequent calls to this function

ports/rp2/modmachine.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mp_usbd.h"
3232
#include "modmachine.h"
3333
#include "uart.h"
34+
#include "rp2_psram.h"
3435
#include "clocks_extra.h"
3536
#include "hardware/pll.h"
3637
#include "hardware/structs/rosc.h"
@@ -115,6 +116,9 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
115116
setup_default_uart();
116117
mp_uart_init();
117118
#endif
119+
#if MICROPY_HW_ENABLE_PSRAM
120+
psram_init(MICROPY_HW_PSRAM_CS_PIN);
121+
#endif
118122
}
119123

120124
static void mp_machine_idle(void) {

ports/rp2/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
8282
#endif
8383

84+
#ifndef MICROPY_HW_ENABLE_PSRAM
85+
#define MICROPY_HW_ENABLE_PSRAM (0)
86+
#endif
87+
8488
// Memory allocation policies
8589
#define MICROPY_GC_STACK_ENTRY_TYPE uint16_t
8690
#define MICROPY_ALLOC_PATH_MAX (128)

ports/rp2/rp2_psram.c

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Phil Howard
7+
* Mike Bell
8+
* Kirk D. Benell
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*/
28+
29+
#include "hardware/structs/ioqspi.h"
30+
#include "hardware/structs/qmi.h"
31+
#include "hardware/structs/xip_ctrl.h"
32+
#include "hardware/clocks.h"
33+
#include "hardware/sync.h"
34+
#include "rp2_psram.h"
35+
36+
37+
size_t __no_inline_not_in_flash_func(psram_detect)() {
38+
int psram_size = 0;
39+
40+
// Try and read the PSRAM ID via direct_csr.
41+
qmi_hw->direct_csr = 30 << QMI_DIRECT_CSR_CLKDIV_LSB | QMI_DIRECT_CSR_EN_BITS;
42+
43+
// Need to poll for the cooldown on the last XIP transfer to expire
44+
// (via direct-mode BUSY flag) before it is safe to perform the first
45+
// direct-mode operation
46+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
47+
}
48+
49+
// Exit out of QMI in case we've inited already
50+
qmi_hw->direct_csr |= QMI_DIRECT_CSR_ASSERT_CS1N_BITS;
51+
52+
// Transmit as quad.
53+
qmi_hw->direct_tx = QMI_DIRECT_TX_OE_BITS | QMI_DIRECT_TX_IWIDTH_VALUE_Q << QMI_DIRECT_TX_IWIDTH_LSB | 0xf5;
54+
55+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
56+
}
57+
58+
(void)qmi_hw->direct_rx;
59+
60+
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS);
61+
62+
// Read the id
63+
qmi_hw->direct_csr |= QMI_DIRECT_CSR_ASSERT_CS1N_BITS;
64+
uint8_t kgd = 0;
65+
uint8_t eid = 0;
66+
67+
for (size_t i = 0; i < 7; i++)
68+
{
69+
if (i == 0) {
70+
qmi_hw->direct_tx = 0x9f;
71+
} else {
72+
qmi_hw->direct_tx = 0xff;
73+
}
74+
75+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_TXEMPTY_BITS) == 0) {
76+
}
77+
78+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
79+
}
80+
81+
if (i == 5) {
82+
kgd = qmi_hw->direct_rx;
83+
} else if (i == 6) {
84+
eid = qmi_hw->direct_rx;
85+
} else {
86+
(void)qmi_hw->direct_rx;
87+
}
88+
}
89+
90+
// Disable direct csr.
91+
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS | QMI_DIRECT_CSR_EN_BITS);
92+
93+
if (kgd == 0x5D) {
94+
psram_size = 1024 * 1024; // 1 MiB
95+
uint8_t size_id = eid >> 5;
96+
if (eid == 0x26 || size_id == 2) {
97+
psram_size *= 8; // 8 MiB
98+
} else if (size_id == 0) {
99+
psram_size *= 2; // 2 MiB
100+
} else if (size_id == 1) {
101+
psram_size *= 4; // 4 MiB
102+
}
103+
}
104+
105+
return psram_size;
106+
}
107+
108+
size_t __no_inline_not_in_flash_func(psram_init)(uint cs_pin) {
109+
gpio_set_function(cs_pin, GPIO_FUNC_XIP_CS1);
110+
111+
uint32_t intr_stash = save_and_disable_interrupts();
112+
113+
size_t psram_size = psram_detect();
114+
115+
if (!psram_size) {
116+
return 0;
117+
}
118+
119+
// Enable direct mode, PSRAM CS, clkdiv of 10.
120+
qmi_hw->direct_csr = 10 << QMI_DIRECT_CSR_CLKDIV_LSB | \
121+
QMI_DIRECT_CSR_EN_BITS | \
122+
QMI_DIRECT_CSR_AUTO_CS1N_BITS;
123+
while (qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) {
124+
;
125+
}
126+
127+
// Enable QPI mode on the PSRAM
128+
const uint CMD_QPI_EN = 0x35;
129+
qmi_hw->direct_tx = QMI_DIRECT_TX_NOPUSH_BITS | CMD_QPI_EN;
130+
131+
while (qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) {
132+
;
133+
}
134+
135+
// Set PSRAM timing for APS6404
136+
//
137+
// Using an rxdelay equal to the divisor isn't enough when running the APS6404 close to 133MHz.
138+
// So: don't allow running at divisor 1 above 100MHz (because delay of 2 would be too late),
139+
// and add an extra 1 to the rxdelay if the divided clock is > 100MHz (i.e. sys clock > 200MHz).
140+
const int max_psram_freq = 133000000;
141+
const int clock_hz = clock_get_hz(clk_sys);
142+
int divisor = (clock_hz + max_psram_freq - 1) / max_psram_freq;
143+
if (divisor == 1 && clock_hz > 100000000) {
144+
divisor = 2;
145+
}
146+
int rxdelay = divisor;
147+
if (clock_hz / divisor > 100000000) {
148+
rxdelay += 1;
149+
}
150+
151+
// - Max select must be <= 8us. The value is given in multiples of 64 system clocks.
152+
// - Min deselect must be >= 18ns. The value is given in system clock cycles - ceil(divisor / 2).
153+
const int clock_period_fs = 1000000000000000ll / clock_hz;
154+
const int max_select = (125 * 1000000) / clock_period_fs; // 125 = 8000ns / 64
155+
const int min_deselect = (18 * 1000000 + (clock_period_fs - 1)) / clock_period_fs - (divisor + 1) / 2;
156+
157+
qmi_hw->m[1].timing = 1 << QMI_M1_TIMING_COOLDOWN_LSB |
158+
QMI_M1_TIMING_PAGEBREAK_VALUE_1024 << QMI_M1_TIMING_PAGEBREAK_LSB |
159+
max_select << QMI_M1_TIMING_MAX_SELECT_LSB |
160+
min_deselect << QMI_M1_TIMING_MIN_DESELECT_LSB |
161+
rxdelay << QMI_M1_TIMING_RXDELAY_LSB |
162+
divisor << QMI_M1_TIMING_CLKDIV_LSB;
163+
164+
// Set PSRAM commands and formats
165+
qmi_hw->m[1].rfmt =
166+
QMI_M0_RFMT_PREFIX_WIDTH_VALUE_Q << QMI_M0_RFMT_PREFIX_WIDTH_LSB | \
167+
QMI_M0_RFMT_ADDR_WIDTH_VALUE_Q << QMI_M0_RFMT_ADDR_WIDTH_LSB | \
168+
QMI_M0_RFMT_SUFFIX_WIDTH_VALUE_Q << QMI_M0_RFMT_SUFFIX_WIDTH_LSB | \
169+
QMI_M0_RFMT_DUMMY_WIDTH_VALUE_Q << QMI_M0_RFMT_DUMMY_WIDTH_LSB | \
170+
QMI_M0_RFMT_DATA_WIDTH_VALUE_Q << QMI_M0_RFMT_DATA_WIDTH_LSB | \
171+
QMI_M0_RFMT_PREFIX_LEN_VALUE_8 << QMI_M0_RFMT_PREFIX_LEN_LSB | \
172+
6 << QMI_M0_RFMT_DUMMY_LEN_LSB;
173+
174+
qmi_hw->m[1].rcmd = 0xEB;
175+
176+
qmi_hw->m[1].wfmt =
177+
QMI_M0_WFMT_PREFIX_WIDTH_VALUE_Q << QMI_M0_WFMT_PREFIX_WIDTH_LSB | \
178+
QMI_M0_WFMT_ADDR_WIDTH_VALUE_Q << QMI_M0_WFMT_ADDR_WIDTH_LSB | \
179+
QMI_M0_WFMT_SUFFIX_WIDTH_VALUE_Q << QMI_M0_WFMT_SUFFIX_WIDTH_LSB | \
180+
QMI_M0_WFMT_DUMMY_WIDTH_VALUE_Q << QMI_M0_WFMT_DUMMY_WIDTH_LSB | \
181+
QMI_M0_WFMT_DATA_WIDTH_VALUE_Q << QMI_M0_WFMT_DATA_WIDTH_LSB | \
182+
QMI_M0_WFMT_PREFIX_LEN_VALUE_8 << QMI_M0_WFMT_PREFIX_LEN_LSB;
183+
184+
qmi_hw->m[1].wcmd = 0x38;
185+
186+
// Disable direct mode
187+
qmi_hw->direct_csr = 0;
188+
189+
// Enable writes to PSRAM
190+
hw_set_bits(&xip_ctrl_hw->ctrl, XIP_CTRL_WRITABLE_M1_BITS);
191+
192+
restore_interrupts(intr_stash);
193+
194+
return psram_size;
195+
}

ports/rp2/rp2_psram.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Phil Howard
7+
* Mike Bell
8+
* Kirk D. Benell
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*/
28+
29+
#include "pico/stdlib.h"
30+
31+
#ifndef MICROPY_INCLUDED_RP2_MACHINE_PSRAM_H
32+
#define MICROPY_INCLUDED_RP2_MACHINE_PSRAM_H
33+
34+
#if MICROPY_HW_ENABLE_PSRAM
35+
#ifndef MICROPY_HW_PSRAM_CS_PIN
36+
#error "MICROPY_HW_ENABLE_PSRAM requires MICROPY_HW_PSRAM_CS_PIN"
37+
#endif
38+
#endif
39+
40+
#define PSRAM_LOCATION _u(0x11000000)
41+
42+
extern size_t psram_init(uint cs_pin);
43+
44+
#endif

0 commit comments

Comments
 (0)