From 109bd2886b677d42720d3b3daf03d6d1d38e4ee0 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 21 Jul 2021 11:02:55 -0700 Subject: [PATCH] Fix Arduino RP2040 flash size For RP2040 boards, we now change the default flash size based on the configured flash. We will still try to read the size from the flash first. Fixes #4874 --- ports/raspberrypi/Makefile | 6 ++++++ ports/raspberrypi/flash_info.h.jinja | 5 +++++ ports/raspberrypi/gen_stage2.py | 14 ++++++++++++++ ports/raspberrypi/supervisor/internal_flash.c | 3 ++- 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ports/raspberrypi/flash_info.h.jinja diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 967bd67078..840b723398 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -260,6 +260,12 @@ $(BUILD)/stage2.c: stage2.c.jinja gen_stage2.py | $(BUILD)/ $(STEPECHO) "GEN $<" $(Q)$(PYTHON3) gen_stage2.py $< $@ $(EXTERNAL_FLASH_DEVICES) +$(HEADER_BUILD)/flash_info.h: flash_info.h.jinja gen_stage2.py | $(HEADER_BUILD)/ + $(STEPECHO) "GEN $<" + $(Q)$(PYTHON3) gen_stage2.py $< $@ $(EXTERNAL_FLASH_DEVICES) + +$(BUILD)/supervisor/internal_flash.o: $(HEADER_BUILD)/flash_info.h + $(BUILD)/boot2.elf: $(BUILD)/stage2.c $(STEPECHO) "BOOT $<" $(Q)$(CC) $(CFLAGS) $(BOOT2_S_CFLAGS) -Os -ggdb3 -I. -fPIC --specs=nosys.specs -nostartfiles -Wl,-T,boot_stage2.ld -Wl,-Map=$@.map -o $@ $< diff --git a/ports/raspberrypi/flash_info.h.jinja b/ports/raspberrypi/flash_info.h.jinja new file mode 100644 index 0000000000..cfeb4840b9 --- /dev/null +++ b/ports/raspberrypi/flash_info.h.jinja @@ -0,0 +1,5 @@ +// This file is auto-generated using gen_stage2.py + +#pragma once + +#define FLASH_DEFAULT_POWER_OF_TWO {{ default_power_of_two }} diff --git a/ports/raspberrypi/gen_stage2.py b/ports/raspberrypi/gen_stage2.py index 4a67905183..759384e17f 100644 --- a/ports/raspberrypi/gen_stage2.py +++ b/ports/raspberrypi/gen_stage2.py @@ -1,3 +1,4 @@ +import math import sys import cascadetoml import pathlib @@ -46,6 +47,18 @@ def main(input_template: pathlib.Path, output_path: pathlib.Path, skus: str = ty max_clock_speed_mhz = min((x.get("max_clock_speed_mhz", 1000) for x in flashes["nvm"])) + default_power_of_two = None + for nvm in flashes["nvm"]: + capacity = nvm.get("capacity", 0) + if not 21 <= capacity < 30: + power_of_two = int(math.log2(nvm["total_size"])) + if not default_power_of_two: + default_power_of_two = power_of_two + else: + default_power_of_two = min(power_of_two, default_power_of_two) + if not default_power_of_two: + default_power_of_two = 21 + # Check that we have a consistent way to set quad enable. if continuous_status_write is None and split_status_write is None: print("quad not ok", continuous_status_write, split_status_write) @@ -71,6 +84,7 @@ def main(input_template: pathlib.Path, output_path: pathlib.Path, skus: str = ty "clock_divider": clock_divider, "read_command": read_command, "wait_cycles": wait_cycles, + "default_power_of_two": default_power_of_two, } template = Template(input_template.read_text()) diff --git a/ports/raspberrypi/supervisor/internal_flash.c b/ports/raspberrypi/supervisor/internal_flash.c index 51068e184a..fa18f24fe0 100644 --- a/ports/raspberrypi/supervisor/internal_flash.c +++ b/ports/raspberrypi/supervisor/internal_flash.c @@ -32,6 +32,7 @@ #include "extmod/vfs.h" #include "extmod/vfs_fat.h" +#include "genhdr/flash_info.h" #include "py/mphal.h" #include "py/obj.h" #include "py/runtime.h" @@ -68,7 +69,7 @@ void supervisor_flash_init(void) { uint8_t cmd[] = {0x9f, 0, 0, 0}; uint8_t data[4]; flash_do_cmd(cmd, data, 4); - uint8_t power_of_two = 21; + uint8_t power_of_two = FLASH_DEFAULT_POWER_OF_TWO; // Flash must be at least 2MB (1 << 21) because we use the first 1MB for the // CircuitPython core. We validate the range because Adesto Tech flash chips // don't return the correct value. So, we default to 2MB which will work for