C1243 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 43 · Keeping data through power-off

Flash, wear and records that survive a power cut

A thermostat that forgets its schedule at every power cut, or a logger whose last hour of data is garbage, has a storage problem. Microcontrollers keep data in flash, the same memory that holds the program, and flash has rules that RAM doesn't: it erases only in large sectors, a write can only clear bits, an erase takes milliseconds, and each sector survives only a limited number of erases. Firmware that ignores those rules works on the bench and dies in the field, worn out in a week or corrupted by an unlucky power cut. This lesson budgets wear and designs records that survive.

  • The curves are a device's flash lifetime in years against how often it saves a record, for two designs: erasing a sector for every save, and appending records to a log that's erased only when full.
  • Raise the number of sectors the log rotates through: its lifetime grows in proportion. Lower the record size: more records fit before each erase.
  • Note the vertical scale: the naive design wears out in days at one save a minute.

Predict

Flash holds 0xF0 at an address. Without erasing, the program writes 0x3C there. What does it read back?

Name it

How flash behaves

  • Erase sets a whole sector (typically 1 to 4 KB on a microcontroller) to all 1s, 0xFF. It takes milliseconds.
  • Program clears chosen bits to 0, a word or a page at a time, in tens of microseconds. Writing over data that isn't erased gives the AND of the two.
  • Endurance: each sector survives roughly 104 to 105 erase cycles. Retention: data lasts 10 to 20 years, less when hot.
  • While a sector of the same bank is erasing, the processor often can't run code from flash.

Wear

If every save erases a sector, the sector lasts E saves. A log appends fixed-size records to an erased sector and erases only when it's full, so a sector of S bytes with r-byte records takes ⌊S/r⌋ saves per erase, and rotating through n sectors spreads the wear:

saves=En⌊S/r⌋

Surviving a power cut

A record is written in steps, and power can fail between any two. Give each record a sequence number and a CRC, and on startup take the newest record whose CRC is good; a half-written record fails its CRC and is ignored. Erase the oldest sector only after the next one holds a good copy. Firmware updates use the same idea: two image slots (A/B), switching only after the new image checks out.

Hold-up

To finish an operation that's already started when power fails, a capacitor must supply it: from V1 down to the lowest working voltage V2, E=12C(V12−V22).

Worked example

A device saves its settings once a minute to one 4 KB sector rated for 10,000 erases, erasing before each save. How long does the flash last? What if 16-byte records are appended to a log across 8 sectors?

Show the solution
  1. Erasing before each save. The sector lasts 10,000 saves, one a minute: 10000min=6.9days
  2. Records per erase. As a log, the 4 KB sector takes 256 records of 16 bytes between erases.
  3. One sector as a log. 10000×256=2.56×106min=4.9years
  4. Eight sectors. They last 39 years.
  5. Compare. The same flash, written differently, lasts two thousand times longer.

Answer 6.9 days erasing before each save, and 39 years as a log across 8 sectors.

Worked example

A sector erase takes 30 ms at 10 mA from 3.3 V, and the regulator drops out below 2.5 V. What hold-up capacitor lets an erase that has just started finish?

Show the solution
  1. Energy for the erase. E=3.3×0.010×0.030J=0.99mJ
  2. Capacitance to supply it. The capacitor gives up that energy as it falls from 3.3 V to 2.5 V: C=2EV12−V22=1.98×10−310.89−6.25F=427μF so a 470 µF part, more if the rest of the board keeps drawing current.
  3. A cheaper approach. Records with CRCs are cheaper: they make an interrupted write harmless instead of trying to prevent it.

Answer at least 427 µF, so a 470 µF part.

Worked example

Why can a log find its newest record without a separate "latest" pointer?

Show the solution
  1. Number each record. Each record carries a sequence number that only increases.
  2. Scan at startup. The program scans the sectors, ignores erased slots (all 0xFF) and records with bad CRCs, and takes the record with the highest sequence number.
  3. Why not a pointer. A pointer stored somewhere fixed would itself be rewritten on every save, wearing out the one sector the log was meant to protect.

Answer each record carries a sequence number that only increases, so the newest is the valid record with the highest number.

Check

A device erases a flash sector rated for 10000 cycles every time it saves, 1 times a day.

(a)

How long does the sector last?

(b)

In years?

Check

Records of 64 bytes are appended to a log over 8 sectors of 1024 bytes, each rated for 10000 erases; the device saves 1440 times a day.

(a)

How many records fit in a sector?

(b)

How long does the flash last?

Check

Flash holds 0x5A. Without erasing, 0xC9 is programmed over it. What does it read back?

Check

A sector erase takes 50 ms at 5 mA from 3.3 V; the system works down to 2.8 V.

(a)

How much energy does the erase need?

Write units the way you normally would: 1.5 mJ, 1500 µJ and 0.0015 J are the same answer.

(b)

What hold-up capacitance supplies it?

Check

A logger writes 16-byte records 10 times a second into a 1 MB circular log of flash rated for 100000 erases.

(a)

How long does the log take to fill once?

(b)

How long until the flash wears out, each sector erased once per lap?

Check

A 64 KB image is written to flash in 256-byte pages taking 2 ms each, after erasing 16 KB sectors that take 20 ms each.

(a)

How long does the whole update take?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

(b)

What share of it is erasing?

Check

Put the steps of a power-safe settings save in order, writing to a log of records.

  1. Read it back and check the CRC
  2. Build the record with the next sequence number
  3. Compute its CRC
  4. Program it into the next erased slot
  5. If the sector is now full, erase the oldest sector only after the next one holds a good copy

Check

Write program(old, new): flash bytes old programmed with new (lists of equal length) without erasing; return the bytes read back. Then latest(records): each record is a dict with seq, value and crc_ok; return the value of the good record with the highest seq, or None if none is good.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does appending records to a log, rather than erasing and rewriting one fixed location, both extend flash life and protect against power cuts?

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.