I want to save some values in external flash memory to a microcontroller. These values are typically going to be incremented every time I run the program. I would like to initialize them to 0. Before I can write to this memory I need to make sure it is initialized. I want to do that by selecting a fixed location in memory that indicates whether or not it is initialized. In the beginning of the program, check whether it was previously initialized, if not, do the initialization and then proceed with reads and writes.
Is there a better way?
work in progress. I will try this, looking for a better approach if possible
Initializing flash to 0 (and frequently modifying flash contents) is generally problematic if you want to modify it later because usually the flash controller in micros can only write from a 1 -> 0, and the "erased" state is all 1's.
You can read-erase-write to the flash every time, but this is also somewhat problematic because usually the flash controller can only erase an entire page at a time. So you'll need to read and write back everything you care about on the entire page... If that's acceptable it should work though. Just make sure you've offset your application code correctly so you don't accidentally erase it as well.
Some flash controllers on't support modifying flash while running from flash, and require you to execute out of RAM when modifying the flash contents. That could be another complication to consider.
I can't think of any great alternatives for this unless you're willing to go off-chip to an external EEPROM.