Skip to content

Instantly share code, notes, and snippets.

@willdurand
Last active March 26, 2022 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willdurand/614ad3ad1cac0189691f67c0ac71b9e6 to your computer and use it in GitHub Desktop.
Save willdurand/614ad3ad1cac0189691f67c0ac71b9e6 to your computer and use it in GitHub Desktop.
Minimal bare metal project for Raspberry Pi 2 - https://williamdurand.fr/2021/01/23/bare-metal-raspberry-pi-2-programming/
.section ".text.boot"
.globl _start
_start:
# Setup the stack.
mov sp, #0x8000
bl kernel_main
halt:
wfe
b halt
.globl do_nothing
do_nothing:
bx lr
#include <stdint.h>
#define GPFSEL4 0x3F200010
#define GPSET1 0x3F200020
#define GPCLR1 0x3F20002C
extern void do_nothing();
void kernel_main() {
*(volatile uint32_t *)GPFSEL4 &= ~(7 << 21);
*(volatile uint32_t *)GPFSEL4 |= 1 << 21;
while (1) {
*(volatile uint32_t *)GPSET1 = 1 << (47 - 32);
for (uint32_t i = 0; i < 0x100000; i++) {
do_nothing();
}
*(volatile uint32_t *)GPCLR1 = 1 << (47 - 32);
for (uint32_t i = 0; i < 0x100000; i++) {
do_nothing();
}
}
}
ENTRY(_start)
SECTIONS
{
. = 0x8000;
.text :
{
KEEP(*(.text.boot))
*(.text*)
}
.rodata :
{
*(.rodata*)
. = ALIGN(4K);
}
/DISCARD/ :
{
*(.comment)
}
}
default: all
CFLAGS = -mcpu=cortex-a7 -ffreestanding -nostdlib -fno-builtin -Wall -Wextra
all: kernel7.img
kernel7.img: kernel.elf
arm-none-eabi-objcopy $< -O binary $@
kernel.elf: boot.o kernel.o
arm-none-eabi-gcc $(CFLAGS) -T linker.ld -o $@ $^
boot.o: boot.S
arm-none-eabi-gcc $(CFLAGS) -c $< -o $@
kernel.o: kernel.c
arm-none-eabi-gcc $(CFLAGS) -c $< -o $@
clean:
rm -f *.o *.img *.elf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment