Skip to content

Commit 46dcc6f

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 PSRAM 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 46dcc6f

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ set(MICROPY_SOURCE_PORT
165165
pendsv.c
166166
rp2_flash.c
167167
rp2_pio.c
168+
rp2_psram.c
168169
rp2_dma.c
169170
uart.c
170171
usbd.c

ports/rp2/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "mpnetworkport.h"
4747
#include "genhdr/mpversion.h"
4848
#include "mp_usbd.h"
49+
#include "rp2_psram.h"
4950

5051
#include "pico/stdlib.h"
5152
#include "pico/binary_info.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_BASE, (void *)(PSRAM_BASE + psram_size));
134+
#else
135+
gc_init((void *)PSRAM_BASE, (void *)(PSRAM_BASE + 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: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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 "py/mphal.h"
30+
31+
#if MICROPY_HW_ENABLE_PSRAM
32+
33+
#include "hardware/structs/ioqspi.h"
34+
#include "hardware/structs/qmi.h"
35+
#include "hardware/structs/xip_ctrl.h"
36+
#include "hardware/clocks.h"
37+
#include "hardware/sync.h"
38+
#include "rp2_psram.h"
39+
40+
size_t __no_inline_not_in_flash_func(psram_detect)() {
41+
int psram_size = 0;
42+
43+
// Try and read the PSRAM ID via direct_csr.
44+
qmi_hw->direct_csr = 30 << QMI_DIRECT_CSR_CLKDIV_LSB | QMI_DIRECT_CSR_EN_BITS;
45+
46+
// Need to poll for the cooldown on the last XIP transfer to expire
47+
// (via direct-mode BUSY flag) before it is safe to perform the first
48+
// direct-mode operation
49+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
50+
}
51+
52+
// Exit out of QMI in case we've inited already
53+
qmi_hw->direct_csr |= QMI_DIRECT_CSR_ASSERT_CS1N_BITS;
54+
55+
// Transmit as quad.
56+
qmi_hw->direct_tx = QMI_DIRECT_TX_OE_BITS | QMI_DIRECT_TX_IWIDTH_VALUE_Q << QMI_DIRECT_TX_IWIDTH_LSB | 0xf5;
57+
58+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
59+
}
60+
61+
(void)qmi_hw->direct_rx;
62+
63+
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS);
64+
65+
// Read the id
66+
qmi_hw->direct_csr |= QMI_DIRECT_CSR_ASSERT_CS1N_BITS;
67+
uint8_t kgd = 0;
68+
uint8_t eid = 0;
69+
70+
for (size_t i = 0; i < 7; i++)
71+
{
72+
if (i == 0) {
73+
qmi_hw->direct_tx = 0x9f;
74+
} else {
75+
qmi_hw->direct_tx = 0xff;
76+
}
77+
78+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_TXEMPTY_BITS) == 0) {
79+
}
80+
81+
while ((qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) != 0) {
82+
}
83+
84+
if (i == 5) {
85+
kgd = qmi_hw->direct_rx;
86+
} else if (i == 6) {
87+
eid = qmi_hw->direct_rx;
88+
} else {
89+
(void)qmi_hw->direct_rx;
90+
}
91+
}
92+
93+
// Disable direct csr.
94+
qmi_hw->direct_csr &= ~(QMI_DIRECT_CSR_ASSERT_CS1N_BITS | QMI_DIRECT_CSR_EN_BITS);
95+
96+
if (kgd == 0x5D) {
97+
psram_size = 1024 * 1024; // 1 MiB
98+
uint8_t size_id = eid >> 5;
99+
if (eid == 0x26 || size_id == 2) {
100+
psram_size *= 8; // 8 MiB
101+
} else if (size_id == 0) {
102+
psram_size *= 2; // 2 MiB
103+
} else if (size_id == 1) {
104+
psram_size *= 4; // 4 MiB
105+
}
106+
}
107+
108+
return psram_size;
109+
}
110+
111+
size_t __no_inline_not_in_flash_func(psram_init)(uint cs_pin) {
112+
gpio_set_function(cs_pin, GPIO_FUNC_XIP_CS1);
113+
114+
uint32_t intr_stash = save_and_disable_interrupts();
115+
116+
size_t psram_size = psram_detect();
117+
118+
if (!psram_size) {
119+
return 0;
120+
}
121+
122+
// Enable direct mode, PSRAM CS, clkdiv of 10.
123+
qmi_hw->direct_csr = 10 << QMI_DIRECT_CSR_CLKDIV_LSB | \
124+
QMI_DIRECT_CSR_EN_BITS | \
125+
QMI_DIRECT_CSR_AUTO_CS1N_BITS;
126+
while (qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) {
127+
;
128+
}
129+
130+
// Enable QPI mode on the PSRAM
131+
const uint CMD_QPI_EN = 0x35;
132+
qmi_hw->direct_tx = QMI_DIRECT_TX_NOPUSH_BITS | CMD_QPI_EN;
133+
134+
while (qmi_hw->direct_csr & QMI_DIRECT_CSR_BUSY_BITS) {
135+
;
136+
}
137+
138+
// Set PSRAM timing for APS6404
139+
//
140+
// Using an rxdelay equal to the divisor isn't enough when running the APS6404 close to 133MHz.
141+
// So: don't allow running at divisor 1 above 100MHz (because delay of 2 would be too late),
142+
// and add an extra 1 to the rxdelay if the divided clock is > 100MHz (i.e. sys clock > 200MHz).
143+
const int max_psram_freq = 133000000;
144+
const int clock_hz = clock_get_hz(clk_sys);
145+
int divisor = (clock_hz + max_psram_freq - 1) / max_psram_freq;
146+
if (divisor == 1 && clock_hz > 100000000) {
147+
divisor = 2;
148+
}
149+
int rxdelay = divisor;
150+
if (clock_hz / divisor > 100000000) {
151+
rxdelay += 1;
152+
}
153+
154+
// - Max select must be <= 8us. The value is given in multiples of 64 system clocks.
155+
// - Min deselect must be >= 18ns. The value is given in system clock cycles - ceil(divisor / 2).
156+
const int clock_period_fs = 1000000000000000ll / clock_hz;
157+
const int max_select = (125 * 1000000) / clock_period_fs; // 125 = 8000ns / 64
158+
const int min_deselect = (18 * 1000000 + (clock_period_fs - 1)) / clock_period_fs - (divisor + 1) / 2;
159+
160+
qmi_hw->m[1].timing = 1 << QMI_M1_TIMING_COOLDOWN_LSB |
161+
QMI_M1_TIMING_PAGEBREAK_VALUE_1024 << QMI_M1_TIMING_PAGEBREAK_LSB |
162+
max_select << QMI_M1_TIMING_MAX_SELECT_LSB |
163+
min_deselect << QMI_M1_TIMING_MIN_DESELECT_LSB |
164+
rxdelay << QMI_M1_TIMING_RXDELAY_LSB |
165+
divisor << QMI_M1_TIMING_CLKDIV_LSB;
166+
167+
// Set PSRAM commands and formats
168+
qmi_hw->m[1].rfmt =
169+
QMI_M0_RFMT_PREFIX_WIDTH_VALUE_Q << QMI_M0_RFMT_PREFIX_WIDTH_LSB | \
170+
QMI_M0_RFMT_ADDR_WIDTH_VALUE_Q << QMI_M0_RFMT_ADDR_WIDTH_LSB | \
171+
QMI_M0_RFMT_SUFFIX_WIDTH_VALUE_Q << QMI_M0_RFMT_SUFFIX_WIDTH_LSB | \
172+
QMI_M0_RFMT_DUMMY_WIDTH_VALUE_Q << QMI_M0_RFMT_DUMMY_WIDTH_LSB | \
173+
QMI_M0_RFMT_DATA_WIDTH_VALUE_Q << QMI_M0_RFMT_DATA_WIDTH_LSB | \
174+
QMI_M0_RFMT_PREFIX_LEN_VALUE_8 << QMI_M0_RFMT_PREFIX_LEN_LSB | \
175+
6 << QMI_M0_RFMT_DUMMY_LEN_LSB;
176+
177+
qmi_hw->m[1].rcmd = 0xEB;
178+
179+
qmi_hw->m[1].wfmt =
180+
QMI_M0_WFMT_PREFIX_WIDTH_VALUE_Q << QMI_M0_WFMT_PREFIX_WIDTH_LSB | \
181+
QMI_M0_WFMT_ADDR_WIDTH_VALUE_Q << QMI_M0_WFMT_ADDR_WIDTH_LSB | \
182+
QMI_M0_WFMT_SUFFIX_WIDTH_VALUE_Q << QMI_M0_WFMT_SUFFIX_WIDTH_LSB | \
183+
QMI_M0_WFMT_DUMMY_WIDTH_VALUE_Q << QMI_M0_WFMT_DUMMY_WIDTH_LSB | \
184+
QMI_M0_WFMT_DATA_WIDTH_VALUE_Q << QMI_M0_WFMT_DATA_WIDTH_LSB | \
185+
QMI_M0_WFMT_PREFIX_LEN_VALUE_8 << QMI_M0_WFMT_PREFIX_LEN_LSB;
186+
187+
qmi_hw->m[1].wcmd = 0x38;
188+
189+
// Disable direct mode
190+
qmi_hw->direct_csr = 0;
191+
192+
// Enable writes to PSRAM
193+
hw_set_bits(&xip_ctrl_hw->ctrl, XIP_CTRL_WRITABLE_M1_BITS);
194+
195+
restore_interrupts(intr_stash);
196+
197+
return psram_size;
198+
}
199+
200+
#endif

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_RP2_PSRAM_H
32+
#define MICROPY_INCLUDED_RP2_RP2_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+
39+
#define PSRAM_BASE _u(0x11000000)
40+
41+
extern size_t psram_init(uint cs_pin);
42+
#endif
43+
44+
#endif

0 commit comments

Comments
 (0)