diff --git a/flash/nuke/CMakeLists.txt b/flash/nuke/CMakeLists.txt index c8ae47a94..d9a7a8c72 100644 --- a/flash/nuke/CMakeLists.txt +++ b/flash/nuke/CMakeLists.txt @@ -7,6 +7,14 @@ target_link_libraries(flash_nuke hardware_flash ) +# We use flash command 0x9f to dynamically get the flash size +# If that fails, assume 16Mb +math(EXPR FLASH_SIZE_OVERRIDE "16 * 1024 * 1024" OUTPUT_FORMAT DECIMAL) +target_compile_definitions(flash_nuke PRIVATE + PICO_INCLUDE_FLASH_UTILS=1 + PICO_FLASH_SIZE_BYTES=${FLASH_SIZE_OVERRIDE} + ) + # It doesn't make sense to run this program from flash. Always build a # RAM-only binary. pico_set_binary_type(flash_nuke no_flash) diff --git a/flash/nuke/nuke.c b/flash/nuke/nuke.c index 521f5d74f..e44efbfcf 100644 --- a/flash/nuke/nuke.c +++ b/flash/nuke/nuke.c @@ -25,14 +25,18 @@ #include "hardware/flash.h" #include "pico/bootrom.h" +#define MIN_FLASH_SIZE (1024 * 1024 * 2) + int main() { - uint flash_size_bytes; -#ifndef PICO_FLASH_SIZE_BYTES -#warning PICO_FLASH_SIZE_BYTES not set, assuming 16M - flash_size_bytes = 16 * 1024 * 1024; -#else - flash_size_bytes = PICO_FLASH_SIZE_BYTES; -#endif + uint flash_size_bytes = 0; + uint8_t txbuf[4]; + uint8_t rxbuf[4]; + txbuf[0] = 0x9f; + flash_do_cmd(txbuf, rxbuf, 4); + flash_size_bytes = 1u << rxbuf[3]; + if (flash_size_bytes < MIN_FLASH_SIZE || flash_size_bytes > PICO_FLASH_SIZE_BYTES) { + flash_size_bytes = PICO_FLASH_SIZE_BYTES; + } flash_range_erase(0, flash_size_bytes); // Leave an eyecatcher pattern in the first page of flash so picotool can // more easily check the size: