Skip to content

Commit b725c26

Browse files
Gadgetoiddpgeorge
authored andcommitted
rp2/rp2_flash: Add MICROPY_HW_FLASH_MAX_FREQ to replace fixed max freq.
Assuming a 133MHz capable flash in 91cff8e caused `rp2_flash_set_timing_internal` to set out of range dividers for some boards (anything with value of 4 and flash that doesn't tolerate higher speeds). This affected (at least) the XIAO RP2350 board, making it non-bootable. Since Pico SDK's `PICO_FLASH_SPI_CLKDIV` is entirely unreliable on a system with a variable system clock (users can change it at runtime) then use it only to work out a default `MICROPY_HW_FLASH_MAX_FREQ`. This value can be overridden in board config. Note that RP2350's default clock is 150MHz, RP2040's is 125MHz and it has been certified at 200MHz so it's quite possible that `PICO_FLASH_SPI_CLKDIV` is unreliable even at standard RP2 clocks. (If flash timings are marginal then this can manifest as instability rather than outright failure.) Fixes issue micropython#17375. Signed-off-by: Phil Howard <[email protected]>
1 parent c1196ca commit b725c26

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

ports/rp2/rp2_flash.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
static_assert(MICROPY_HW_ROMFS_BYTES % 4096 == 0, "ROMFS size must be a multiple of 4K");
5050
static_assert(MICROPY_HW_FLASH_STORAGE_BYTES % 4096 == 0, "Flash storage size must be a multiple of 4K");
5151

52+
#ifndef MICROPY_HW_FLASH_MAX_FREQ
53+
// Emulate Pico SDK's SYS_CLK_HZ / PICO_FLASH_SPI_CLKDIV behaviour by default.
54+
// On RP2040 if PICO_USE_FASTEST_SUPPORTED_CLOCK is set then SYS_CLK_HZ can be
55+
// 200MHz, potentially putting timings derived from PICO_FLASH_SPI_CLKDIV
56+
// out of range.
57+
#define MICROPY_HW_FLASH_MAX_FREQ (SYS_CLK_HZ / PICO_FLASH_SPI_CLKDIV)
58+
#endif
59+
5260
#ifndef MICROPY_HW_FLASH_STORAGE_BASE
5361
#define MICROPY_HW_FLASH_STORAGE_BASE (PICO_FLASH_SIZE_BYTES - MICROPY_HW_FLASH_STORAGE_BYTES)
5462
#endif
@@ -104,9 +112,8 @@ static bool use_multicore_lockout(void) {
104112
// and core1 locked out if relevant.
105113
static void __no_inline_not_in_flash_func(rp2_flash_set_timing_internal)(int clock_hz) {
106114

107-
// Use the minimum divisor assuming a 133MHz flash.
108-
const int max_flash_freq = 133000000;
109-
int divisor = (clock_hz + max_flash_freq - 1) / max_flash_freq;
115+
// Use the minimum divisor based upon our target MICROPY_HW_FLASH_MAX_FREQ
116+
int divisor = (clock_hz + MICROPY_HW_FLASH_MAX_FREQ - 1) / MICROPY_HW_FLASH_MAX_FREQ;
110117

111118
#if PICO_RP2350
112119
// Make sure flash is deselected - QMI doesn't appear to have a busy flag(!)

0 commit comments

Comments
 (0)