Definition
The NVMC (Non-Volatile Memory Controller) is the hardware block in Nordic Semiconductor’s nRF52 chips that carries out every change to the on-chip flash memory §6.6: writing 32-bit words, erasing one page, erasing the UICR (User Information Configuration Registers) §16.1 region, and erasing all flash at once. Firmware drives it through registers at fixed memory addresses. It selects read-only, write or erase mode, starts an operation, and reads a ready flag that says when the operation has finished. Ordinary reads of flash need no command.
The idea
Flash memory cannot be changed like RAM. An erase sets every bit of a whole page to 1 and takes milliseconds. A write can only turn 1 bits into 0 bits and takes tens of microseconds per word. Both apply high voltages to the memory cells under careful timing, and while a page is being erased or a word written, the flash cannot deliver data. Yet the processor normally fetches its instructions from that same flash.
The controller handles both problems. It hides the physics of the cells behind a few commands, write a word, erase a page, erase everything, and it makes the processor wait while the flash is busy. A library that closes its stacks while a book is being rebound is a fair picture: a reader who needs a book from the stacks waits at the desk until they reopen, while a reader who already has a photocopy, code that sits in RAM, keeps working. The picture stops at one point. In the chip nobody can jump the wait: every flash access by the processor waits, an interrupt handler §6.12 stored in flash included.
How it works
On the nRF52840 §6.4, a chip of the nRF52 family, these registers matter:
CONFIG: the mode, one of read-only (Ren, value 0), write enabled (Wen, value 1) and erase enabled (Een, value 2). Nordic recommends enabling write or erase mode only while it is in use. Enabling either mode empties the instruction cache, a small fast memory that holds recently fetched instructions, and keeps it empty until read-only mode returns, so code runs more slowly meanwhile.READY: reads 0 (busy) while a write or erase is in progress and 1 (ready) when none is.READYNEXT: reads 1 when the controller can accept another write.ERASEPAGE: writing the address of a page here starts erasing that page of the program area.ERASEPAGEPARTIALandERASEPAGEPARTIALCFG: erase a page in slices;ERASEPAGEPARTIALCFGsets the length of a slice in milliseconds, 10 by default.ERASEUICRandERASEALL: erase all of UICR, or all flash together with UICR.
Writing one word takes these steps.
Set
CONFIGto write enabled.Store a full 32-bit word, at an address divisible by 4, with an ordinary store instruction. The controller accepts only whole words. To leave bits of a word unchanged, write 1 in those bits, because a write turns bits from 1 to 0 and never from 0 to 1.
The controller programs the word, which takes up to 41 microseconds on the nRF52840. Meanwhile
READYreads 0. If the processor needs flash during this time, for example to fetch its next instruction, the processor is halted until the write ends. Code and data in RAM, and peripherals that work on their own, such as timers and the radio, carry on.Wait until
READYreads 1. The word is now programmed.Write further words, or set
CONFIGback to read-only.
An erase follows the same pattern with CONFIG set to erase enabled: write the page address to ERASEPAGE, then wait for READY. Erasing a 4096-byte page takes up to 85 milliseconds (ms), and a processor that runs from flash is halted for all of it. Partial erase exists for programs that cannot stand such a pause, such as radio protocol software that must answer on time. Each start of ERASEPAGEPARTIAL erases for one slice, the processor runs between slices, and the firmware repeats the slices until their total reaches the time that the product specification states for a partial erase. Only then is the page erased.
READYNEXT serves writes in quick succession: the controller can accept the next word while it is still programming the previous one. It does not report completion. Only READY = 1 says that no write or erase is still running.
Worked example
Firmware that runs from flash stores 256 bytes of settings, which is words. Each write can halt the processor for up to 41 microseconds, so the halts add up to at most microseconds, about 2.6 ms.
Next the firmware erases one page: up to 85 ms with the processor halted. Take a Bluetooth Low Energy §7.1 connection with a connection interval §7.7 of 7.5 ms: the two devices exchange packets every 7.5 ms. In 85 ms, , so 11 or 12 exchanges fall due, depending on when the erase starts, and code stored in flash can serve none of them. The device misses them, and if the pause were longer than the connection’s supervision timeout §7.8, the time after which a silent link counts as lost, the connection would drop. Radio protocol software therefore places flash erases in gaps between radio events, uses partial erase, or runs the time-critical code from RAM.
Completion matters as much as timing. A driver function that stores a word and returns at once, without waiting for READY, returns while the word is still being programmed. If power fails 10 microseconds after the return, the word holds unknown content, although the function reported success.
The erase unit also matters. To change 4 bytes in a page of 4096, the firmware must erase the whole page. The erase affects 4096 bytes, of which were not meant to change; they survive only if the firmware saved them first and writes them back.
Stated precisely
The contract of the nRF52840 controller for one operation, with the old content of a word and the value written:
From the store or the erase command until completion,
READY= 0.READY= 1 exactly when no write or erase is in progress.When a write completes, the word holds , where is the bitwise AND: a result bit is 1 only where both and have a 1. When a page erase completes, every bit of the page is 1.
While an operation runs, every processor access to flash stalls until the operation completes; accesses to RAM and to peripherals proceed.
A store to flash has an effect only in write-enabled mode, and an erase command only in erase-enabled mode.
The guarantee assumes that the supply voltage stays within the operating range until completion. If power fails before READY returns to 1, the word or page being changed holds unknown content. Durability §8.2, the survival of stored data across power loss, begins at completion. The chip’s errata, the manufacturer’s list of known deviations from the specification, add conditions: one documented anomaly of the nRF52840 hangs the processor when a write or erase runs while the power-failure comparator, the circuit that watches the supply voltage, warns that the voltage is low.
The ready flag covers one operation. A sequence of operations, such as erasing a page and writing it again, has no completion flag of its own: power can fail between two operations, and READY = 1 after the first says nothing about the second.
Limits and common mistakes
A function returning is not the hardware finishing. Some drivers start the operation and return. Nordic’s Bluetooth protocol software, the SoftDevice, for example accepts a flash request, returns at once and reports completion later as an event. Data counts as stored only after the completion that the hardware reports.
READYNEXT = 1 means that the controller can take another word, not that earlier words are stored.
Halting the processor is not disabling interrupts §6.11. An interrupt that arrives during a write or erase is served only after the operation ends if its handler, or the table of handler addresses, sits in flash.
A watchdog timer §6.16, which resets the chip unless the firmware restarts the timer in time, keeps counting while the processor is halted. An 85 ms erase must fit inside the watchdog’s timeout §9.34, or the watchdog resets the chip in the middle of the erase.
Code must not erase the flash page it is running from. The processor waits for the erase and then fetches erased bytes instead of its next instruction.
Partial erase shortens each pause, not the erase. The page is erased only after the last slice; before that it holds undefined content.
Leaving write or erase mode enabled keeps the instruction cache empty and lets a stray store instruction change flash.
Other chips’ flash controllers have different units, times and rules. The numbers here hold for the nRF52840 only.
Questions answered
When does the controller report ready, and what execution restrictions apply during a write?
The NVMC (Non-Volatile Memory Controller) of a Nordic nRF52 chip reports ready, READY = 1, only when no write or erase is in progress, that is, after the operation has finished in the flash memory. From the moment a word is stored or an erase is started until it finishes, READY reads 0. On the nRF52840 a word write takes up to 41 microseconds and a page erase up to 85 ms. A second flag, READYNEXT, says only that the controller can accept the next word, not that earlier words are stored.
The restrictions during a write or erase are these.
Write or erase mode must first be enabled in
CONFIG, and enabling it empties the instruction cache until read-only mode returns.Only whole 32-bit words at addresses divisible by 4 can be written, and a write turns bits from 1 to 0 only.
Any processor access to flash, including fetching the next instruction or an interrupt handler, halts the processor until the operation ends. Code and data in RAM, and peripherals, keep running.
Power must not fail before
READYreturns to 1, or the word or page holds unknown content.
Software should report data as stored only after it has seen READY = 1 for the last operation.
Related entries: Flash memory (§6.6), Durability (§8.2), Torn write (§8.19), Write ordering (§8.20), Credential-preserving programming (§16.7), UICR (User Information Configuration Registers) (§16.1), Watchdog timer (§6.16). Further reading: [56].