├── README.md └── RP2040_flash └── RP2040_flash.ino /README.md: -------------------------------------------------------------------------------- 1 | # RP2040_flash_programming 2 | A simple Arduino IDE example for programming the RP2040 onboard flash, including rudimentary wear-leveling logic. This is explained in detail [on my blog](https://www.makermatrix.com/blog/read-and-write-data-with-the-pi-pico-onboard-flash). 3 | 4 | 5 | Enable your RP2040-based board in the board manager (e.g. Pi Pico). Plug your board into your computer, and select the detected commuincation port under Tools->Port. 6 | Compile this sketch and program it to the board. 7 | 8 | The code will block until you open the serial monitor. It will then report some values of the relevant macros, 9 | traverse the last sector of the flash one page at a time, reporting the value of the first four bytes of each page cast as int. 10 | It will then write an int to the first four bytes of the first empty page. It will do this one time, each time it executes, until the sector is full. 11 | It will then erase the sector and start over at the beginning. 12 | -------------------------------------------------------------------------------- /RP2040_flash/RP2040_flash.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Jarrod A. Smith (MakerMatrix) 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | * SOFTWARE.* Testing/examples for reading/writing flash on the RP2040. 21 | * 22 | * Resources I drew from: 23 | * https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__flash.html 24 | * https://kevinboone.me/picoflash.html 25 | * 26 | * You must disable interrupts while programming flash. This requires you to 27 | * include hardware/sync.h and use save_and_disable_interrupts(), then restore_interrupts() 28 | * 29 | * You must include hardware/flash.h for the erase/program functions. 30 | * Interesting macros defined in flash.h: 31 | * FLASH_PAGE_SIZE, FLASH_SECTOR_SIZE, FLASH_BLOCK_SIZE 32 | * functions: 33 | * flash_range_erase() 34 | * flash_range_program() 35 | * 36 | * pico.h gets included from there. Interesting macros with respect to flash there: 37 | * PICO_FLASH_SIZE_BYTES 38 | * 39 | * and from addresses.h: 40 | * XIP_BASE - the ARM address of the end of the onboard 256KB RAM. 41 | * On RP2040 this is 0x10000000 and the first byte of flash is the next address. 42 | * 43 | * Note that the flash_range_erase() and flash_range_program() functions from flash.h 44 | * do not use ARM address space, only offsets into flash (i.e. don't count the 45 | * first 0x10000000 bytes of address space representing the RAM. 46 | * 47 | * But to read, point a pointer directly to a memory-mapped address somewhere in the 48 | * flash (so, use XIP_BASE to get past RAM) and read directly from it. 49 | */ 50 | 51 | extern "C" { 52 | #include 53 | #include 54 | }; 55 | 56 | // Set the target offest to the last sector of flash 57 | #define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE) 58 | 59 | int buf[FLASH_PAGE_SIZE/sizeof(int)]; // One page buffer of ints 60 | int *p, addr; 61 | unsigned int page; // prevent comparison of unsigned and signed int 62 | int first_empty_page = -1; 63 | 64 | void setup() { 65 | Serial.begin(9600); 66 | while(!Serial) ; 67 | Serial.println("FLASH_PAGE_SIZE = " + String(FLASH_PAGE_SIZE, DEC)); 68 | Serial.println("FLASH_SECTOR_SIZE = " + String(FLASH_SECTOR_SIZE,DEC)); 69 | Serial.println("FLASH_BLOCK_SIZE = " + String(FLASH_BLOCK_SIZE, DEC)); 70 | Serial.println("PICO_FLASH_SIZE_BYTES = " + String(PICO_FLASH_SIZE_BYTES, DEC)); 71 | Serial.println("XIP_BASE = 0x" + String(XIP_BASE, HEX)); 72 | 73 | // Read the flash using memory-mapped addresses 74 | // For that we must skip over the XIP_BASE worth of RAM 75 | // int addr = FLASH_TARGET_OFFSET + XIP_BASE; 76 | for(page = 0; page < FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE; page++){ 77 | addr = XIP_BASE + FLASH_TARGET_OFFSET + (page * FLASH_PAGE_SIZE); 78 | p = (int *)addr; 79 | Serial.print("First four bytes of page " + String(page, DEC) ); 80 | Serial.print("( at 0x" + (String(int(p), HEX)) + ") = "); 81 | Serial.println(*p); 82 | if( *p == -1 && first_empty_page < 0){ 83 | first_empty_page = page; 84 | Serial.println("First empty page is #" + String(first_empty_page, DEC)); 85 | } 86 | } 87 | 88 | *buf = 123456; 89 | 90 | if (first_empty_page < 0){ 91 | Serial.println("Full sector, erasing..."); 92 | uint32_t ints = save_and_disable_interrupts(); 93 | flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE); 94 | first_empty_page = 0; 95 | restore_interrupts (ints); 96 | } 97 | Serial.println("Writing to page #" + String(first_empty_page, DEC)); 98 | uint32_t ints = save_and_disable_interrupts(); 99 | flash_range_program(FLASH_TARGET_OFFSET + (first_empty_page*FLASH_PAGE_SIZE), (uint8_t *)buf, FLASH_PAGE_SIZE); 100 | restore_interrupts (ints); 101 | } 102 | 103 | 104 | void loop() { 105 | } 106 | --------------------------------------------------------------------------------