The user wants a bare-metal application for an ARM Cortex-A processor to blink an LED.
1. Scope and Constraints:
- Target Architecture: ARM Cortex-A (32-bit or 64-bit). This is a broad family. I need to pick a specific, popular target to make the code runnable and understandable.
- Bare-metal: No OS (Linux, RTOS). Direct hardware access.
- Functionality: Blink an LED.
- Deliverables: Code (C and Assembly), linker script, build instructions (Makefile).
2. Selecting the Target Platform:
- Option A: ARM Versatile Express (QEMU). Good because it requires no hardware, but specific memory addresses can be obscure.
- Option B: Raspberry Pi (Pi 1, 2, 3, or 4). Extremely popular. Plenty of documentation.
- Pi 1/Zero: ARMv6 (Technically ARM11, not Cortex-A, though often grouped in bare-metal tutorials).
- Pi 2: Cortex-A7 (32-bit, ARMv7-A).
- Pi 3: Cortex-A53 (64-bit, ARMv8-A).
- Pi 4: Cortex-A72 (64-bit, ARMv8-A).
Decision: The Raspberry Pi 3 or 4 is the standard for "Hello World" bare-metal tutorials. However, 64-bit (AArch64) code is slightly cleaner and modern. Let's aim for Raspberry Pi 3 (AArch64) because it's a Cortex-A53. This allows demonstrating the 64-bit instruction set, which is common for "Cortex-A" requests nowadays.
Wait, actually: To keep it accessible, maybe the Raspberry Pi 2 (Cortex-A7) or Pi 3 in 32-bit mode is easier to explain without the complexity of AArch64 exception levels (ELs) right off the bat.
Let's stick to the most common request: Raspberry Pi (specifically targeting the GPIO controller). I will provide a solution that works on Raspberry Pi 3 (Cortex-A53) in AArch64. This feels the most "modern Cortex-A".
3. Key Components Required:
- Boot Code (Assembly): Needed to set up the stack, clear BSS, and jump to C. In AArch64, this also involves checking the Exception Level (EL) and potentially dropping to EL1 if starting at EL3 (though simpler bootloaders often start us at EL2/EL3, QEMU starts at EL1 usually, real hardware varies). For a "simplest possible" example, I'll assume the GPU firmware loads the kernel at
0x80000.
- Main Code (C): Needs to access GPIO registers.
- Base address for GPIO (BCM2837 for Pi 3).
- Set GPIO direction (Output).
- Set/Clear GPIO pin.
- Delay function (busy wait).
- Linker Script: Define memory layout (entry point, text, data, bss).
- Build System:
make and aarch64-none-elf-gcc.
4. Drafting the Code (Mental or Scratchpad):
5. Refining the Code:
- The Busy Wait: Needs to be long enough to see. A
for loop with a few million iterations.
- GPIO addressing: The MMIO addresses must be correct.
- Pi 3 (BCM2837):
0x3F000000 base.
- Pi 4 (BCM2711):
0xFE000000 base.
- Strategy: I will write the code specifically for the Raspberry Pi 3 (most common target for beginner bare-metal). I will add a note about Pi 4 differences.
6. Structuring the Explanation:
- Introduction: Explain that "Cortex-A" usually requires a board definition. I've chosen Raspberry Pi 3 (Cortex-A53, AArch64) because it's accessible.
- Prerequisites: Toolchain (
aarch-none-eabi-gcc).
- File 1:
boot.S (Startup Assembly): Explain setting the stack and jumping to C.
- File 2:
main.c (Driver Logic): Explain MMIO (Memory Mapped I/O) and the delay loop.
- File 3:
linker.ld (Memory Layout): Explain placing code at 0x80000.
- File 4:
Makefile: How to compile.
- How to Run: Formatting the SD card (
kernel8.img).
7. Self-Correction/Review during drafting:
- Issue: Accessing the Activity LED on Pi 3 is tricky because on the Plus models it's GPIO 47, but older models used 16.
- Fix: I will use GPIO 17 (Pin 11 on header) and instruct the user to connect an external LED. This is universal across all Pis and doesn't depend on specific board revisions of the Activity LED logic. It's a "true" hardware interface example.
- Refined C Code: Define
GPFSEL1 (for GPIO 17) and GPSET0 / GPCLR0.
8. Writing the actual assembly (Mental Check):
.section "text.boot"
.global _start
_start:
// Check processor ID (optional, but good for multi-core)
// For simplicity, only core 0 runs.
mrs x0, mpidr_el1
and x0, x0, #3
cbnz x0, halt
// Set up Stack (assume 64MB RAM)
ldr x0, =_start
mov sp, x0
// Clear BSS
ldr x0, =__bss_start
ldr x1, =__bss_end
sub x1, x1, x0
cbz x1, skip_bss
mov x2, #0
bss_loop:
str x2, [x0], #8
sub x1, x1, #8
cbnz x1, bss_loop
skip_bss:
// Jump to C
bl main
halt:
wfe
b halt
Wait, the linker script usually defines _start at 0x80000. The stack usually goes above that, or at the top of RAM. Setting sp to _start grows down. That's fine.
9. Writing the C Code (Mental Check):
Base address 0x3F200000.
GPIO 17 is in FSEL1 (bits 23-21).
Need to set them to 001 (Output).
mmio_write(GPIO_BASE + 0x1C, (1 << 17)) // Turn On (SET)
mmio_write(GPIO_BASE + 0x28, (1 << 17)) // Turn Off (CLR)
10. Final Polish:
Ensure the tone is educational. Emphasize that volatile is crucial for hardware registers. Ensure the instructions for building are clear.
One nuance: Raspberry Pi 3 starts all 4 cores at once. The code must handle this to avoid 4 CPUs trying to blink the LED at once (or crashing). The assembly check mpidr_el1 is essential.
Let's assemble the final output.
Summary of the Solution provided:
- Target: Raspberry Pi 3 (Broadcom BCM2837, Cortex-A53).
- Architecture: AArch64 (64-bit).
- Source:
boot.S: Entry point, stack setup, core selection, BSS clearing.
main.c: GPIO initialization, infinite blink loop.
linker.ld: Memory map.
Makefile: Build commands.
- Instructions: SD card setup.
Since "ARM Cortex-A" covers a wide range of chips (from NXP i.MX to Broadcom Raspberry Pi), the most practical and accessible example is for the Raspberry Pi 3 (Cortex-A53).
This example creates a "bare metal" application (no Linux, no OS) that blinks the Activity LED (the green LED next to the power red LED).
Prerequisites
You will need the AArch64 bare-metal toolchain (GNU Toolchain).
On Ubuntu/Debian:
sudo apt install gcc-aarch64-none-elf
On macOS (via Homebrew):
brew install --cask gcc-aarch64-embedded
The Code Structure
We need four files:
boot.S: Assembly startup code (sets up the stack and jumps to C).
main.c: The C logic to blink the LED.
linker.ld: Instructions on where to place code in memory.
Makefile: To build everything.
1. boot.S (Assembly Entry)
This code runs immediately after the Raspberry Pi firmware hands over control. It sets up the stack pointer (SP) and clears the BSS (uninitialized data) segment.
.section "text.boot"
.global _start
_start:
// 1. Check processor ID. If not Core 0, hang (stop).
mrs x0, mpidr_el1
and x0, x0, #3
cbnz x0, hang
// 2. Set up the Stack Pointer (SP).
// We assume 64MB of RAM. Stack grows downward from the top.
ldr x0, =_start
mov sp, x0
// 3. Clear the BSS section (Zero out uninitialized global variables)
ldr x0, =__bss_start
ldr x1, =__bss_end
mov x2, #0
bss_loop:
cmp x0, x1
b.ge bss_done
str x2, [x0], #8
b bss_loop
bss_done:
// 4. Jump to the main C function
bl main
hang:
// Infinite loop for non-core-0 CPUs or if main returns
wfe
b hang
2. main.c (Hardware Control)
This uses Memory Mapped I/O (MMIO) to talk to the GPIO controller.
Note: The Raspberry Pi 3 uses a mailbox system to communicate with the VideoCore GPU to toggle the Activity LED.
#include <stdint.h>
// Memory Addresses for Raspberry Pi 3
#define MMIO_BASE 0x3F000000
// GPIO Base Address
#define GPIO_BASE (MMIO_BASE + 0x200000)
// Mailbox Addresses (for communicating with GPU)
#define MBOX_BASE (MMIO_BASE + 0xB880)
#define MBOX_READ ((volatile unsigned int*)(MBOX_BASE + 0x00))
#define MBOX_STATUS ((volatile unsigned int*)(MBOX_BASE + 0x18))
#define MBOX_WRITE ((volatile unsigned int*)(MBOX_BASE + 0x20))
// Mailbox Constants
#define MBOX_REQUEST 0
#define MBOX_CH_PROPERTY 8 // Channel 8 is for ARM -> VC
// Tags for the LED
#define SET_GPIO_STATE 0x00038041
#define SET_GPIO_CONFIG 0x00038043
// Delay function (simple busy wait)
static void delay(int32_t count) {
while(count--) {
asm volatile("nop");
}
}
// Mailbox interaction struct
typedef struct {
uint32_t size;
uint32_t code;
uint32_t tag;
uint32_t val_buf_size;
uint32_t req_resp_size;
uint32_t gpio_num;
uint32_t gpio_dir; // 1=OUT, 0=IN
uint32_t end_tag;
} mailbox_msg_t;
volatile mailbox_msg_t msg __attribute__((aligned(16)));
int mailbox_call(uint8_t ch) {
uint32_t r = (((uint32_t)&msg) & ~0xF) | (ch & 0xF);
// Wait until we can write to the mailbox
while (*MBOX_STATUS & 0x80000000) {}
// Write the address of our message to the mailbox with channel
*MBOX_WRITE = r;
// Wait for a response
while (1) {
// Wait for data
while (*MBOX_STATUS & 0x40000000) {}
// Is it a response to our message?
if (r == *MBOX_READ) {
return msg.code == 0x80000000; // Success code
}
}
return 0;
}
void main() {
// 1. Setup the Activity LED (GPIO 47 on Pi 3, usually)
// Note: On RPi 3B+, the Activity LED is GPIO 29 or controlled via mailbox.
// The safest bare-metal way on RPi3 is using the mailbox system
// to ask the GPU to toggle the Activity LED.
// Setup LED GPIO to Output
msg.size = sizeof(mailbox_msg_t);
msg.code = MBOX_REQUEST;
msg.tag = SET_GPIO_CONFIG;
msg.val_buf_size = 8;
msg.req_resp_size = 0;
msg.gpio_num = 47; // Activity LED GPIO (Check your specific board revision)
msg.gpio_dir = 1; // Output
msg.end_tag = 0;
mailbox_call(MBOX_CH_PROPERTY);
while(1) {
// Turn LED ON
msg.tag = SET_GPIO_STATE;
msg.gpio_num = 47;
msg.gpio_dir = 1; // State: High (or Low depending on circuit)
mailbox_call(MBOX_CH_PROPERTY);
delay(0x3F0000);
// Turn LED OFF
msg.tag = SET_GPIO_STATE;
msg.gpio_num = 47;
msg.gpio_dir = 0;
mailbox_call(MBOX_CH_PROPERTY);
delay(0x3F0000);
}
}
3. linker.ld (Memory Layout)
This tells the linker where to put the code in RAM. The Pi loads kernel8.img at address 0x80000.
ENTRY(_start)
SECTIONS
{
. = 0x80000;
.text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) }
.rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) }
.data : { *(.data .data.* .gnu.linkonce.d*) }
.bss (NOLOAD) : {
. = ALIGN(16);
__bss_start = .;
*(.bss .bss.*)
*(COMMON)
__bss_end = .;
}
_end = .;
/DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) }
}
4. Makefile
Automates the compilation process.
CROSS_COMPILE = aarch64-none-elf-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
OBJCOPY = $(CROSS_COMPILE)objcopy
CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles
LDFLAGS = -nostdlib
all: kernel8.img
boot.o: boot.S
$(CC) $(CFLAGS) -c boot.S -o boot.o
main.o: main.c
$(CC) $(CFLAGS) -c main.c -o main.o
kernel8.elf: boot.o main.o linker.ld
$(LD) $(LDFLAGS) -T linker.ld -o kernel8.elf boot.o main.o
kernel8.img: kernel8.elf
$(OBJCOPY) -O binary kernel8.elf kernel8.img
clean:
rm -rf *.o *.elf *.img
How to Run
- Compile: Run
make in the directory containing these files. You will get kernel8.img.
- Prepare SD Card:
- Take a FAT32 formatted SD card.
- Copy the standard Raspberry Pi firmware files onto it:
bootcode.bin, fixup.dat, start.elf (You can get these from the official Raspberry Pi firmware GitHub or an existing Raspbian image).
- Copy your
kernel8.img to the root of the SD card.
- Boot: Insert the SD card into the Pi and power it on. The green activity LED should start blinking.