My application needs a very small persisted storage to occasionally save user enter configuration.
Looking at the documentation, the flash and these functions seem to be a good fit:
void flash_range_erase // 4096 bytes block granularity
flash_range_program // 256 bytes block granularity
1. How to select a 4096 bytes block in flash at a stable address that does not interfere with anything else?
2. If I just read/write to that block, which is not used for anything else, do the restrictions such as no interrupts or no multicore access still apply?
3. Is there a similar example for writing to a small, stable and independent flash block? An EEPROM emulation if you may.
6 posts • Page 1 of 1
there is a __flash_binary_end symbol being generated by the linker scripts, you can put an int at it with "extern uint32_t __flash_binary_end;", then get the addr with &__flash_binary_end
then round up to the next 4096 byte erase block
doing any write/erase activity requires turning the flash off entirely, so nothing can read from it
the erase/program functions you mentioned already do that, but you have to stop any attempts to read
if the 2nd core is running, you must pick which core is doing the flashing and which is the "victim"
the victim must then run multicore_lockout_victim_init()
the core doing the flashing must then run multicore_lockout_start_blocking() and multicore_lockout_end_blocking() around any erase/write code, to stop the victim core
any dma reading from flash must be paused manually
You can put it anywhere after the end of your program. Ideally high enough above that growth in your program won't corrupt it, cause problems. Top of Flash would probably be ideal but some things like MicroPython and CircuitPython put their own file systems up at the top.
Testing with MicroPython running, there is a third of a MB - 1,408 pages of 256 bytes - available between end of program and start of file system which should be plenty for most cases -
Code: Select all
usr : 10048000 .. 1009FFFF = 360,448, 352 KB, 0.34 MB brd : 10000000 .. 101FFFFF = 2,097,152, 2,048 KB, 2 MB rp2 : 10000000 .. 10FFFFFF = 16,777,216, 16,384 KB, 16 MB
Reading is fine but the restrictions will still apply when writing.
This worked for me - viewtopic.php?p=1854403#p1854403
Thanks everybody, I will give it a try. I currently use only one core so I presume I don't need to worry about locking it. As for DMA, the board has a continues alternating DMA channels transferring from ADC to RAM. Do I need to turn off the DMA or is it OK because it just access peripherals and RAM rather than flash?