Section 6.4

nRF52840

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.

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 1024−48−32=9441024 - 48 - 32 = 944 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 944/2=472944 / 2 = 472 KiB, which is 472/4=118472 / 4 = 118 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 118×85=10,030118 \times 85 = 10{,}030 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 96+24+40+64=22496 + 24 + 40 + 64 = 224 KiB, and 256−224=32256 - 224 = 32 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 16+20=3616 + 20 = 36 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 256×4=1024256 \times 4 = 1024 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: 16×4+6×32=25616 \times 4 + 6 \times 32 = 256 KiB, which is 262,144 bytes, at addresses 0x20000000 to 0x2003FFFF.

A firmware design fits the chip only if three conditions hold.

  1. The regions of flash memory do not overlap, and their sizes add up to at most 1024 KiB.

  2. At every instant, the objects that exist in RAM together take at most 256 KiB.

  3. 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 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].