nandlog v1.1

nandlog

Append-only, CRC-validated logging on raw SPI NAND, built to keep every committed record across a power cut. No flash translation layer, no filesystem, no heap.

nandlog is a C99 library for devices that collect data for weeks and hand it over in one go, such as a battery-powered sensor, a tag, or a logger. It assumes that writes are small and frequent, reads are rare and bulk, and power can fail at any instant without warning.

The log assembles records into a page in RAM and commits each page to the next position on the part, with one CRC-32 over the page header and another over the payload. Each page is programmed once, read back, and never rewritten. A metadata ring describes each epoch, one generation of the log, and a boot finds where the next page goes, the write head, by a binary search.

The library is four layers with three interfaces (Figure 1). You write the port, thirteen small functions over your SPI peripheral, and use one of the two supplied chip drivers or write one for your part.

If you are deciding whether to use nandlog, read What it provides and What survives a power cut. If you are integrating it, start with Getting Started.

application nandlog.h records in, pages out nandlog.c: the log core framing, CRCs, epochs, the write head, bad-block policy nandlog_chip.h 15 functions: what the log needs from a part chips/nandlog_chip_AS5F…c one self-contained driver chips/nandlog_chip_W25N…c per fitted part nandlog_port.h 13 functions: what the log needs from a platform your nandlog_port.c transfers, init, lock, power, delays, faults: the only file you write SPI peripheral → NAND part
Figure 1. Four layers, three interfaces. The port interface sits at the level of SPI commands rather than pages, so a host simulator can emulate the part behind it and run the production chip driver unmodified.

What it provides

CapabilityDetail
Static allocationAbout 16 KB of RAM on a 4 KB-page part, fixed at compile time. No malloc.
DurabilityOnce the call that commits a page returns, its records survive a power cut at any later instant. See What survives a power cut.
Corruption detectionSeparate CRC-32s over each page header and payload, and every commit read back and compared byte for byte.
Bad-block handlingA block that refuses a program or an erase is retired, and its pages are moved and verified. The bad-block table survives a power cut at any instant (details).
Fast recoveryA warm boot of the 8 Gbit Alliance part takes 82–89 ms with 35 blocks in its bad-block table. The search for the write head stays correct with retired blocks, unreadable pages, and torn writes in the epoch.
Timestamped recordsEvery record carries a 32-bit timestamp you supply, in your own units. The log reads no clock, and reads select pages by time window (details).
Bulk offloadTime-bounded reads, a framed wire format with a CRC on every page, and per-page retransmission.
Multiple generationsEpochs. An earlier epoch stays readable until the write head sweeps over it.
Bounded workEvery search loop has a stated bound, so a failing part cannot stall the calling task indefinitely.
Nothing dropped silentlynandlog_store_record() says why it refused a record, and running counters say how many records were refused or lost.

What survives a power cut

Once the call that commits a page returns, every record in that page survives a power cut at any later instant, and a page that a power cut tore reads back as a gap. Records still in RAM are lost, as at any reset, and the guarantee has stated limits, chiefly a page left erased in every bit and epochs that are sealed when their metadata can no longer be verified. The simulator checks the contract at every program and erase, and an unattended soak on the Alliance part has passed 2,059 RESET-interrupted operations. A soak on the Winbond part found one residual second program in 2,017 cuts, and tests that remove power are still to come. Durability has the full contract, its limits, and the evidence.

What it does not do

A minimal example

#include "nandlog.h"

if (!nandlog_init())
   return;                        /* the port would not open, or no part answered */

nandlog_begin_session();
nandlog_begin_epoch(&details, sizeof(details));
nandlog_end_session();

if (nandlog_store_record(RECORD_SAMPLE, milliseconds, &sample, sizeof(sample)) != NANDLOG_OK)
   ;                              /* not stored, and the status says why */
nandlog_flush(true);

Getting Started has the complete picture, including how to read data back.

Supported parts

DriverPartPageBlockArray
nandlog_chip_AS5F18G04SND.cAlliance Memory, 8 Gbit4096 + 256 B64 pages4096 blocks
nandlog_chip_W25N01GWZEIG.cWinbond, 1 Gbit2048 + 64 B64 pages1024 blocks

Any other part needs a driver, which is one .c file. See Chip Drivers.

Requirements

Project layout

PathContents
nandlog.c, nandlog.hThe log core and its public API.
nandlog_conf.hConfiguration, the one file you edit rather than write.
nandlog_chip.hWhat the log requires of a part.
nandlog_port.h, nandlog_port.cWhat the log requires of a platform, and a template to fill in.
chips/One self-contained driver per supported part.
sim/Host build: the real log and chip drivers over a RAM-backed emulated part.
tests/Applications that run on real hardware.
tools/nandlog_parse.pyReference reader for raw images and offload streams.

License

MIT. See LICENSE.