zapta
Posts: 73
Joined: Thu May 06, 2021 4:23 pm

How to use flash as an EEPROM replacement?

Tue May 11, 2021 6:13 pm

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

My questions

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.

cleverca22
Posts: 3937
Joined: Sat Aug 18, 2012 2:33 pm

Re: How to use flash as an EEPROM replacement?

Tue May 11, 2021 6:24 pm

zapta wrote:
Tue May 11, 2021 6:13 pm
1. How to select a 4096 bytes block in flash at a stable address that does not interfere with anything else?
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
zapta wrote:
Tue May 11, 2021 6:13 pm
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?
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

hippy
Posts: 10237
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to use flash as an EEPROM replacement?

Tue May 11, 2021 6:36 pm

zapta wrote:
Tue May 11, 2021 6:13 pm
1. How to select a 4096 bytes block in flash at a stable address that does not interfere with anything else?
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
zapta wrote:
Tue May 11, 2021 6:13 pm
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?
Reading is fine but the restrictions will still apply when writing.
zapta wrote:
Tue May 11, 2021 6:13 pm
3. Is there a similar example for writing to a small, stable and independent flash block? An EEPROM emulation if you may.
This worked for me - viewtopic.php?p=1854403#p1854403

zapta
Posts: 73
Joined: Thu May 06, 2021 4:23 pm

Re: How to use flash as an EEPROM replacement?

Wed May 12, 2021 5:26 am

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?

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 724
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: How to use flash as an EEPROM replacement?

Wed May 12, 2021 12:55 pm

DMA is fine; unless you have an IRQ, in which case you either need to disable interrupts or make sure the IRQ handler runs from flash

pic0
Posts: 53
Joined: Tue Jan 26, 2021 11:04 am

Re: How to use flash as an EEPROM replacement?

Wed May 12, 2021 12:58 pm

kilograham wrote:
Wed May 12, 2021 12:55 pm
... or make sure the IRQ handler runs from flash
Seems to be a typo: Should be "runs from RAM" in my opinion.

Return to “SDK”