Definition
The nRF52840 is a system on chip §6.2 (SoC) made by Nordic Semiconductor: a single chip that combines an Arm Cortex-M4 §6.3 processor core with floating-point hardware, 1 MiB of flash memory §6.6 for the program, 256 KiB of RAM, a 2.4 gigahertz radio and other built-in devices, called peripherals. It is made for low-power wireless products; its radio supports Bluetooth Low Energy (BLE) §7.1 among other protocols. The name is a part number, not an abbreviation: nRF52 is the product series, and 840 identifies the chip within the series.
The idea
The feature list at the front of Nordic’s product specification, the document that describes the chip, names 1 megabyte of flash memory, 256 kilobytes of RAM and a 64 megahertz processor. Take a designer who needs a 16 KiB buffer, a region of RAM, for the data of an update of the firmware, the program stored on the chip. The designer may conclude that 256 KiB leave plenty of room. That conclusion skips the question that matters: what else has to exist at the same time.
The chip is one house with several tenants. The application, the real-time operating system (RTOS), the radio protocol software, which is called the protocol stack, the bootloader §6.30 (the small program that starts the firmware), the stored settings and the place where a new firmware version waits during an update all live inside the same walls. The picture stops in one respect. The tenants share time as well as space: there is one processor, one flash controller and one radio, and while one tenant uses them the others wait.
How it works
Every part of the firmware draws on the resources below. The chip has exactly one of each, except that it has several hardware timers.
Flash memory: 1 MiB, divided into 256 pages of 4 KiB. It keeps its content without power, is erased a page at a time and is written in 32-bit words. The designer divides it into regions, for example for the bootloader, the running firmware, a new firmware version being received, and stored settings.
RAM: 256 KiB. It holds the working data of all software on the chip.
Processor time: one Cortex-M4 core, clocked at 64 megahertz, runs everything. The protocol stack runs partly in interrupt handlers §6.12, routines that take the processor away from all other code whenever the radio needs service.
The flash controller, which Nordic calls the non-volatile memory controller §16.3 (NVMC). While it erases a page, for up to 85 milliseconds (ms), or writes a word, for up to 41 microseconds, the processor is halted if it tries to run code from flash memory.
The radio and the hardware timers: there is one radio, and the protocol stack reserves it and some of the timers for itself.
Power: one supply feeds the processor, the radio and the flash controller.
Three checks follow. There must be space in flash memory. There must be space in RAM at the moment of highest use. And no flash operation may stall the processor for longer than the most urgent software can wait. For the third check the chip offers a partial erase, which splits one page erase into shorter steps.
Worked example
The layout in this example is invented. A firmware design reserves 48 KiB of flash memory for a bootloader and 32 KiB for stored settings. That leaves KiB. The design keeps the running firmware in one region and receives a new version into a second region of the same size, so that the old version stays usable until the new one is complete. Each region gets KiB, which is pages. The firmware, including the RTOS and the protocol stack, may therefore be at most 472 KiB: less than half of the 1 MiB that the chip has.
Erasing the second region before an update takes up to ms, about 10 seconds, in 118 page erases that can each stall the processor for up to 85 ms.
In RAM, say that fixed data takes 96 KiB, the stacks, which are the working areas of the running code, take 24 KiB, the buffers of the protocol stack 40 KiB, and the heap, the region for memory that the firmware requests while it runs, 64 KiB. The sum is KiB, and KiB remain. A 16 KiB buffer for update data fits. A second object of 20 KiB that must exist at the same time does not fit, because KiB exceeds the remaining 32 KiB by 4 KiB.
Stated precisely
Nordic writes 1 megabyte and 256 kilobytes and counts in binary units. Flash memory is KiB, which is 1,048,576 bytes, at addresses 0x00000000 to 0x000FFFFF. RAM consists of 16 sections of 4 KiB and 6 sections of 32 KiB: KiB, which is 262,144 bytes, at addresses 0x20000000 to 0x2003FFFF.
A firmware design fits the chip only if three conditions hold.
The regions of flash memory do not overlap, and their sizes add up to at most 1024 KiB.
At every instant, the objects that exist in RAM together take at most 256 KiB.
Every flash operation stalls the processor for no longer than the most urgent software can wait.
The product specification adds two limits. Each page endures at least 10,000 erases. A word can be written at most twice between two erases of its page. The erase and write times given above hold while the chip takes its clock from a quartz crystal; with the chip’s internal clock source they vary with the accuracy of that source.
Limits and common mistakes
The capacity in the product specification is not the budget of the application. Subtract the bootloader, the RTOS, the protocol stack and any second firmware region first.
Do not count an object twice. A buffer that is taken from the heap is already inside the heap’s share.
The product specification says what the chip can do, not what a given firmware does. Whether memory protection is enabled, which radio protocol runs and how flash memory is divided are decisions of the firmware.
The 64 megahertz are the clock rate of the processor. They do not say how long a piece of code takes, because code that runs from flash memory can incur waiting cycles.
The name nRF52840 identifies a chip, not a board. Boards that carry the chip differ in everything outside it; the entry on development kit versus dongle §6.5 gives an example.
Questions answered
Which resources are shared between application, protocol stack, and update staging?
The application, the protocol stack and update staging share every main resource of the nRF52840 chip: flash memory, RAM, the processor, the flash controller, the radio with the hardware timers, and the power supply. The protocol stack is the software that runs the radio protocol, and update staging means storing a new version of the firmware, the chip’s program, until it replaces the old one. The details follow.
The 1 MiB of flash memory. The running firmware, the waiting new version, the bootloader (the small program that starts the firmware) and the stored settings each need a region of it.
The 256 KiB of RAM, which must hold the working data of all three at the moment of highest use.
The single processor core. Interrupt handlers of the protocol stack, routines that hardware events start, take processor time from the application.
The flash controller. Its page erases, of up to 85 milliseconds each, and its word writes halt the processor while it runs code from flash memory, so writing an update delays the protocol stack.
The single radio, and the hardware timers that the protocol stack reserves.
The power supply.
The sizes in the chip’s product specification are therefore upper limits for the sum, not budgets for any one of the three. Check the space in flash memory, the peak use of RAM and the longest stall of the processor separately.
Related entries: Peak versus retained memory (§11.37), Static versus dynamic footprint (§11.38), Firmware build profile (§6.44), Staging versus commit versus activation (§8.22), System on chip (SoC) (§6.2), Arm Cortex-M4 (§6.3), Development kit versus dongle (§6.5), Flash memory (§6.6), SRAM (§6.7), NVMC (Non-Volatile Memory Controller) (§16.3). Further reading: [20, 56].