I am using STM8 and since vector table is in flash memory I need to write to flash memory in order to create and use vector table. Since addresses of vector tables are fixed my code writes same values to same memory address of the flash on every startup. But there is no erase in the code so I thought after the first write process it won't do anything anymore. My question is would it reduce the life of flash if I write to same flash memory address on every startup/reset without erasing?
CodePudding user response:
My question is would it reduce the life of flash if I write to same flash memory address on every startup/reset without erasing?
In general, writing exactly the same value would NOT reduce the lifespan.
IRL it depends on the implementation of the write routine, i.e. on your "operating system". It might determine that the write is unnecessary and won't perform it or it might always erase/write.
A rule of thumb value for FLASH write/erases is 50 k.
On the manufacturer's page I did not find any mentioning of integrated wear-leveling, neither for FLASH memory nor for EEPROM, see https://www.st.com/en/microcontrollers-microprocessors/stm8-8-bit-mcus.html
- Program memory: 8-Kbyte Flash memory; data retention 20 years at 55 °C after 100 cycles
- RAM: 1 Kbyte
- Data memory: 128-byte true data EEPROM; endurance up to 100 k write/erase cycles
The following page stresses that you should use wear-leveling techniques: https://embedded-lab.com/blog/continuing-stm8-microcontroller-expedition/11/
Though flash memories are primarily intended for storing application codes, it is still possible to use them just like EEPROMs using In-Application Programming (IAP). However, it is important to check that by mistake, we don’t write in those areas where application code reside. IAP can also be used for upgrading application firmware Over-The-Air (OTA).
Avoid frequent writes/erases to increase memory life cycles. If needed to write/erase data frequently use wear-leveling technique.
CodePudding user response:
This Q&A is probably better-suited to https://electronics.stackexchange.com, where more experts on embedded electronics and how flash memory works are likely to be found.
The physics and electronics of how flash memory really works
Quick answers:
Since addresses of vector tables are fixed my code writes same values to same memory address of the flash on every startup.
Study what I wrote below. No new damage occurs.
My question is would it reduce the life of flash if I write to same flash memory address on every startup/reset without erasing?
If you're writing the same exact thing to the same exact flash memory every time, with no erase cycles in between, no new damage occurs after the first write. Only the first write would actually discharge any bits (capacitors), thereby causing damage just that one time.
Details:
My question is would it reduce the life of flash if I write to same flash memory address on every startup/reset without erasing?
Let me add some insight here.
Here is my understanding of how flash memory really works:
- Erase: Flash memory is memory stored in microscopic capacitors. An "erase" writes all cells to HIGH voltage, which is considered a zero (
0
). An "erase" cycle damages the atomic structure of the capacitor cell wall as electrons blast through it in the charging process. Erase cycles are done on entire flash memory "pages", or large chunks of memory, all at once. At erase is an expensive operation, both in terms of energy used and time, since a bunch of charge pumps must pump up the tiny mcu voltage to a high voltage which can charge all cells of an entire flash memory page at once in one large flood of energy to charge the capacitor bank. - Write: A "write" can be done on a granular word (ex: 4 bytes) or byte level (granularity depends on the mcu), rather than on a large flash memory page level which is usually kilobytes. It is fast since all it has to do is short a single bit or several bits to ground to discharge the capacitors to change a HIGH voltage (
0
) to a LOW voltage (1
). "Writing" a1
to a bit simply discharges that bit from HIGH (0
) to LOW (1
). It only changes anything if the bit was "erased" previously to charge it to a HIGH (0
) in the first place. Discharging a bit from HIGH to LOW is also a destructive process since it blasts electrons through the capacitor wall as they discharge since you shorted that cell to ground, removing matter at an atomic level as the electrons blast through. "Writing" a bit to a0
(HIGH voltage) does absolutely nothing. It is the erase cycle that sets that bit to a0
(HIGH). It's like this:- Writing a flash
0
(HIGH) -->1
(LOW) = a charged bit is discharged by shorting it to ground; this is a molecularly destrutive operation - Writing a flash
0
(HIGH) -->0
(HIGH) = nothing happens (writing to HIGH is a no-op); the bit is already charged, and writing cannot charge a bit; only erase can. The bit remains HIGH (0
). - Writing a flash
1
(LOW) -->0
(HIGH) = nothing happens (writing to HIGH is a no-op); writing cannot charge a bit; only erase can. The bit remains LOW (1
). - Writing a flash
1
(LOW) -->1
(LOW) = the bit is shorted to ground to discharge it to LOW (1
), but since it was already discharged, nothing happens. There are no electrons wanting to move, so this is a non-descructive operation, as the "damage" already occurred previously when this bit was discharged from a0
(HIGH) to a1
(LOW).
- Writing a flash
Make sense?
So, if you try to write a binary byte 0b00000000
(all bits at HIGH voltage) to 0b11111111
(all bits at LOW voltage) then it discharges all bits by shorting them to zero, bringing their voltage from HIGH to LOW and you end up with 0b11111111
. This damages the capacitor cells. If you try to write that byte to 0b11111111
again, it shorts all of their cells to ground again, but nothing happens sine the cells were already discharged! No new damage occurs.
But, if you try to write a binary 0b11111111
(all bits at LOW voltage) to 0b00000000
(all bits at HIGH) voltage, nothing happens! Writing can NOT charge the cells, only an erase operation can! You end up with 0b11111111
still. No damage to your cells occurred. You got a no-op.
Therefore, damage occurs when you discharge a bit, and when you charge a bit. Charging a bit is done through an erase cycle, so you can just count how many times you erased as an estimate of the number of "damage cycles" you've imposed upon the flash memory capacitor cells.
- Standard flash memory (erasable in groups of large "pages") is generally rated up to 10,000 "write-to-1 (LOW; via discharge) / erase-to-0 (HIGH, via charge)" cycles. Refer to your datasheet for your exact number.
- EEPROM (erasable at a very granular byte or word level) flash memory is generally more-robust and can handle 100,000 write/erase cycles. Refer to your datasheet for your exact number.
Now, with this knowledge, is your "writing" really doing anything at all without an erase first?
Are you really causing any damage? If you cause it during the first write, will subsequent writes cause more damage?
You should have the tools to know those answers now.