# boot.s - Startup assembly (32-bit ARM)
# Entry point at 0x80000 (as loaded by Raspberry Pi GPU firmware)
.section ".text.boot"
.globl _start
_start:
// Set up stack pointer (256 KiB stack space after code/data)
ldr sp, =stack_top
// Clear .bss section
ldr r0, =__bss_start__
ldr r1, =__bss_end__
mov r2, #0
bss_clear_loop:
cmp r0, r1
beq bss_clear_done
str r2, [r0], #4
b bss_clear_loop
bss_clear_done:
// Jump to C main()
bl main
// Halt loop if main() returns
halt:
wfi
b halt
// Weak alias for main() if not defined
.section ".text"
.weak main
c
// blink.c - Main application logic
// Blinks the onboard ACT LED (GPIO 47 on Raspberry Pi 3B+/4B)
// GPIO47 is controlled directly in bare-metal mode (no OS interference)
// Peripherals base address (compatible view provided by Pi firmware for Pi3/4)
#define PERIPHERAL_BASE 0x3F000000
#define GPIO_BASE (PERIPHERAL_BASE + 0x200000)
// GPIO registers (offsets in bytes)
#define GPFSEL4 ((volatile unsigned int *)(GPIO_BASE + 0x10)) // GPIO40-49 function select
#define GPSET1 ((volatile unsigned int *)(GPIO_BASE + 0x20)) // Set bits 32-53
#define GPCLR1 ((volatile unsigned int *)(GPIO_BASE + 0x2C)) // Clear bits 32-53
// Simple busy-wait delay (tune COUNT for desired blink rate)
void delay(unsigned int count) {
while (count--) {
asm volatile("nop"); // Prevent optimization
}
}
void main(void) {
// Configure GPIO47 as output
// GPIO47: in GPFSEL4, bits [23:21] = 001 (output)
// Bit position: GPIO47 % 10 = 7, 7*3 = 21, so bits 23:21
*GPFSEL4 &= ~(7 << 21); // Clear bits 23:21
*GPFSEL4 |= (1 << 21); // Set to 001 (output)
while (1) {
*GPSET1 = (1 << 15); // Set GPIO47 high (47-32=15)
delay(10000000); // Delay ~0.5s (depends on CPU clock)
*GPCLR1 = (1 << 15); // Set GPIO47 low
delay(10000000); // Delay ~0.5s
}
}
ARM GNU Toolchain: sudo apt update && sudo apt install gcc-arm-none-eabi
Raspberry Pi 3B+/4B (tested on Pi 4 Model B Rev 1.4)
Build:
code
make clean
make
Produces kernel.img (~1-2 KiB).
Deploy to Raspberry Pi:
Mount the boot partition of your SD card (FAT32).
Copy kernel.img to the boot partition root.
Edit config.txt (in boot partition):
code
# Disable default kernels
kernel=kernel.img
# Optional: Enable UART for debug (blinking doesn't need it)
# enable_uart=1
# dtoverlay=disable-bt
Safely unmount and insert SD card into Pi.
Power on: The onboard green ACT LED (next to Ethernet/USB) will blink ~once per second.
Verification:
LED on GPIO47 blinks forever.
No UART output needed (pure bare-metal).
If no blink: Check wiring (onboard LED), try GPIO16 instead (adjust code: GPFSEL1 bits[20:18], SET0/CLR0 bit16).
Customization:
Different GPIO (e.g., GPIO16 external LED): Change to GPFSEL1, bits[20:18], GPSET0/GPCLR0 (1<<16).
Faster blink: Reduce delay(10000000) count.
Pi Zero/1/2: Same code (GPIO16 onboard LED).
AArch64: Use aarch64-none-elf-gcc, adjust flags (-march=armv8-a), linker/entry for EL1/EL0, but requires armstub or Pi4-specific firmware.
This is a complete, minimal, self-contained bare-metal app for ARM Cortex-A53/A72 (RPi 3/4). No MMU, caches untouched (direct GPIO access works). Total ~500 bytes image.