Skip to content

Commit 8048ab9

Browse files
tyethpeterharperuk
andauthored
Update nuke.c to auto-detect flash size (#653)
* Update nuke.c to auto-detect flash size Fixes #642 Thanks to @Gadgetoid for the work "Making A Thing" - Support [PiMoRoNi by buying stuff](https://shop.pimoroni.com/)! * Add some validation of flash size If flash_do_cmd returns a bad size for the flash or fails, use a default flash size of 16MB --------- Co-authored-by: Peter Harper <peter.harper@raspberrypi.com>
1 parent 30c18c4 commit 8048ab9

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

flash/nuke/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ target_link_libraries(flash_nuke
77
hardware_flash
88
)
99

10+
# We use flash command 0x9f to dynamically get the flash size
11+
# If that fails, assume 16Mb
12+
math(EXPR FLASH_SIZE_OVERRIDE "16 * 1024 * 1024" OUTPUT_FORMAT DECIMAL)
13+
target_compile_definitions(flash_nuke PRIVATE
14+
PICO_FLASH_SIZE_BYTES=${FLASH_SIZE_OVERRIDE}
15+
)
16+
1017
# It doesn't make sense to run this program from flash. Always build a
1118
# RAM-only binary.
1219
pico_set_binary_type(flash_nuke no_flash)

flash/nuke/nuke.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@
2525
#include "hardware/flash.h"
2626
#include "pico/bootrom.h"
2727

28+
#define MIN_FLASH_SIZE (1024 * 1024 * 2)
29+
2830
int main() {
29-
uint flash_size_bytes;
30-
#ifndef PICO_FLASH_SIZE_BYTES
31-
#warning PICO_FLASH_SIZE_BYTES not set, assuming 16M
32-
flash_size_bytes = 16 * 1024 * 1024;
33-
#else
34-
flash_size_bytes = PICO_FLASH_SIZE_BYTES;
35-
#endif
31+
uint flash_size_bytes = 0;
32+
uint8_t txbuf[4];
33+
uint8_t rxbuf[4];
34+
txbuf[0] = 0x9f;
35+
flash_do_cmd(txbuf, rxbuf, 4);
36+
flash_size_bytes = 1u << rxbuf[3];
37+
if (flash_size_bytes < MIN_FLASH_SIZE || flash_size_bytes > PICO_FLASH_SIZE_BYTES) {
38+
flash_size_bytes = PICO_FLASH_SIZE_BYTES;
39+
}
3640
flash_range_erase(0, flash_size_bytes);
3741
// Leave an eyecatcher pattern in the first page of flash so picotool can
3842
// more easily check the size:

0 commit comments

Comments
 (0)