Definition
The Arm Cortex-M4 is a 32-bit processor core for microcontrollers §6.1 (small computers on one chip), designed by the company Arm. A processor core is the circuit that executes instructions. The Cortex-M4 is a design, not a chip: chip makers license it and build it into chips, beside memory and other circuits. The Cortex-M4 follows the Armv7E-M architecture, the specification of its instructions and of how programs see the processor. Cortex-M4 is a product name, not an abbreviation; M marks Arm’s microcontroller cores. With optional floating-point hardware it is written Cortex-M4F.
The idea
Two chips from two makers both name the Cortex-M4 on their datasheets, the documents that describe them. One has 256 KiB of working memory, a radio and hardware for floating-point arithmetic, which is arithmetic on numbers with a fractional part. The other has less memory, no radio and no floating-point hardware. A programmer needs to know which facts follow from the name alone.
The answer has three layers. Arm fixes the first layer for every Cortex-M4: the instruction set and the registers and modes that programs see. The chip maker decides the second layer when building the core into a chip: which optional parts of the core are included, and everything around the core. The firmware, the chip’s program, decides the third layer when it runs: which included parts are switched on. An engine model built into different cars is a fair picture: the model fixes how the engine works, not the size of the car’s tank, and a fitted option can still be switched off. The picture stops in one respect: a core is not a bolted-in part but a design that the chip maker copies into its own chip.
How it works
Fixed by Arm for every Cortex-M4.
The instruction set, which Arm calls Thumb: a mix of 16-bit and 32-bit instructions defined by the Armv7-M architecture, with hardware integer division. The E in Armv7E-M marks an added group of instructions for signal processing.
Registers of 32 bits, which are the processor’s own stores for values, and one flat address space of bytes: every memory location has one fixed 32-bit address.
Two modes: Thread mode for ordinary code and Handler mode for exception handlers §6.15. An exception is an event that makes the processor suspend the running code and run a handler routine, for example an interrupt §6.11, which is a signal from hardware, or a fault, which is an error that the processor detects.
Two privilege levels §6.10. Privileged code can use every instruction and reach every resource. Unprivileged code cannot reach the processor’s control registers. Handler mode is always privileged; Thread mode can be either.
An interrupt controller inside the core, the Nested Vectored Interrupt Controller §6.13 (NVIC), and a system timer.
Chosen by the chip maker inside the core.
A floating-point unit (FPU) for single-precision arithmetic: included or not.
A memory protection unit §6.8 (MPU) with eight regions, which can restrict the memory that code may access: included or not.
The number of interrupt inputs, from 1 to 240, and the number of priority bits, from 3 to 8, which gives to priority levels.
Debug and trace units, a wake-up controller, and the byte order §4.21 of data (endianness).
Chosen by the chip maker around the core. The sizes of flash memory §6.6 and RAM, the clock frequency, the peripherals such as timers and serial ports, any radio, and the pins.
Chosen by the firmware. Whether the MPU is enabled and with which regions, whether floating-point instructions are permitted, which code runs unprivileged, and the priority of each interrupt.
Worked example
Firmware can read the layers from registers at fixed addresses. The values below are the ones Arm’s manuals give for a Cortex-M4 of revision r0p1 that includes the MPU and the FPU.
Which core is this? The register
CPUIDat address0xE000ED00reads0x410FC241. Its top 8 bits are0x41, the code for Arm. Bits 15 to 4 (bit 0 is the lowest) are0xC24, the code for the Cortex-M4.Is an MPU included? The register
MPU_TYPEat address0xE000ED90reads0x00000800. Bits 15 to 8 hold the number of regions:0x08, which is 8. On a chip built without the MPU this register reads 0.Is the MPU enabled? The register
MPU_CTRLat address0xE000ED94reads 0 after reset. Its lowest bit is the enable bit. The MPU is therefore included but off: it checks nothing until the firmware sets that bit.May code use the FPU? The register
CPACRat address0xE000ED88reads 0 after reset, which denies access: a floating-point instruction then causes a fault. To permit access, the firmware sets bits 20 to 23 of the register. As a number, these four bits are0x00F00000.
The name Cortex-M4 answered only the first question, and it does not give the speed either. A sequence of instructions that takes 128 clock cycles lasts microseconds at a clock of 64 megahertz, which is 64 cycles per microsecond, and microseconds at 16 megahertz.
Stated precisely
A program may rely on a feature of the processor only if three conditions hold. First, Arm’s architecture manual or core manual defines the feature. Second, the chip includes it. Third, the firmware has enabled it for the code that uses it. Arm’s manual for the Cortex-M4 has a fixed word for each of the last two conditions: a feature is included when the chip maker selected it, and enabled when software has also switched it on.
A mandatory feature is present in every Cortex-M4. An optional feature is defined by Arm but present only where the chip maker included it; the chip’s datasheet says so, and for the MPU and the FPU a register reports it. A chip-specific property lies outside the core, and only the chip maker’s documents define it.
Limits and common mistakes
The name of the core gives no memory size, no clock frequency and no list of peripherals.
An included MPU that the firmware leaves off checks nothing. Regions that are programmed while all code runs privileged can catch accidents but confine nothing, because privileged code can reprogram the MPU or switch it off.
The FPU handles single-precision numbers only, which are 32 bits wide. Arithmetic on 64-bit floating-point numbers §4.49 is done by library code, with or without the FPU.
Code with floating-point instructions faults at the first such instruction on a Cortex-M4 that lacks the FPU, or whose firmware has not enabled it.
Cycle counts in Arm’s manual are not run times: the clock frequency and the time spent waiting for memory belong to the chip. The nRF52840 §6.4 chip from Nordic Semiconductor adds waiting cycles when code runs from its flash memory.
Questions answered
Which features are mandatory, optional, or chip-specific?
Mandatory features, present in every Arm Cortex-M4, are the instruction set of the Armv7E-M architecture, which Arm calls Thumb, including integer division and the signal-processing instructions; 32-bit registers; Thread mode for ordinary code and Handler mode for handlers of interrupts and faults; privileged and unprivileged execution; the built-in interrupt controller (NVIC, Nested Vectored Interrupt Controller); and the system timer.
Optional features, defined by Arm but included only if the chip maker chose them, are the floating-point unit (FPU), the memory protection unit (MPU), which can restrict the memory that code may access, the debug and trace units and a wake-up controller. The chip maker also sets the number of interrupt inputs (1 to 240), the number of priority levels (8 to 256) and the byte order.
Chip-specific properties, outside the core, are the sizes of flash memory and RAM, the clock frequency, the built-in devices (peripherals), any radio, and the pins.
An optional feature that is included must still be enabled by the firmware (the chip’s program) before code can rely on it. For any feature, check three things in order: Arm’s manual defines it, the chip’s datasheet includes it, the firmware enables it.
Related entries: Instruction set architecture (ISA) (§4.38), Microarchitecture (§4.39), nRF52840 (§6.4), Memory protection unit (MPU) (§6.8), Privilege level (§6.10), NVIC (§6.13), Arm Thumb and Thumb-2 (§4.40). Further reading: [5, 81, 174].