Skip to content

Commit ee177a1

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 ee177a1

File tree

6 files changed

+270
-0
lines changed

6 files changed

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