Skip to content

Instantly share code, notes, and snippets.

@Kaapiii
Last active July 13, 2022 20:33
Show Gist options
  • Save Kaapiii/cb2011bc1ddd28c46945f0191af50079 to your computer and use it in GitHub Desktop.
Save Kaapiii/cb2011bc1ddd28c46945f0191af50079 to your computer and use it in GitHub Desktop.
Blinking LED - Assembly - Raspberry Pi4
//.section .init
//=====================================
// LED Blink -frist try
// Markus Liechti
//
// For Raspberry Pi 4
// BCM2711 (Cortex-A72 / ARM-V8)
//=====================================
//=====================================
//
// Documentation P4 (BCM2711): https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2711/rpi_DATA_2711_1p0.pdf
// Documentation P3 (BCM2837B0) https://www.raspberrypi.org/documentation/hardware/computemodule/datasheets/rpi_DATA_CM3plus_1p0.pdf
// Documentation ARM v8: https://documentation-service.arm.com/static/5f10674f0daa596235e7ecbf?token=
// -------------------------------------------------------
// Situation
//
// Use GPIO21 (PIN40) and GND (PIN39)
//
// Each GPIO (PIN) is controlled by three registers (a SELECT,a SET,a CLEAR register)
// To interact we need the physical addresses of this registers. These can be created by adding the offset and the BASE address
//
// Responsible for GPIO21 are the following registers:
// - GPFSEL2 [Offset: 0x08] responsible for GPIO Pins 20 to 29 -> our Field Name for GPIO21 is = FSEL21
// - GPCLR0 [Offset: 0x28] responsible for GPIO Pins (0 to 31) -> our Field Name for GPIO21 is = CLR21
// - GPSET0 [Offest: 0x1C] responsible for GPIO Pins (0 to 31) -> our Field Name for GPIO21 is = SET21
.global _start
//---------------------
//Setup values
.equ BASE, 0x3f200000 // Base address of peripherials
.equ GPFSEL2,0x08 // Hex 8 is in binary -> 1000
.equ GPCLR0,0x28
.equ GPSET0,0x1c
.equ SET_GPFSEL2_OFFSET,0b1000 // in Hex would be 0x08
.equ SET_GPCLR0_OFFSET,0x200000 // offset for GPIO21 -> Offset for GPIO22 is 0x400000
_start:
//------------------------------------------------------------------
// Define BASE
//------------------------------------------------------------------
ldr r0,=BASE // load BASE physical address
//------------------------------------------------------------------
// Set the GPIO21 PIN as OUTPUT
//------------------------------------------------------------------
ldr r1,=SET_GPFSEL2_OFFSET // load Offset for GPIO21 SELECT address
str r1,[r0,#GPFSEL2] // store value in r1 to memory at address r0 with the offset of #GPFSEL2
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Turn on voltag on the GPIO21 PIN
//-----------------------------------------------------------------
ldr r1,=SET_GPCLR0_OFFSET @ load Offset for PIN 21 into r1
str r1,[r0,#GPFSET0] @ store value in r1 to memory at address r0 with the offset of #GPCLR0
CC = arm-none-eabi-gcc # specify the compiler executable
CCLD = arm-none-eabi-ld # arm gcc load
CODE = ledblinking.s # store filename in var
OBJ = ledblinking.o # store object filename in var
EXC = ledblinking #
# Specify target ARM processor with "-mcpu" option
# Raspberry Pi 4 => "cortex-a72"
# Raspberry Pi 3B => "cortex-a53"
# Raspberry Pi 2 => "cortex-a7"
TRG_ARCH = cortex-a72
# Wrap cc flags in variable
CFLAGS = -mcpu=$(TRG_ARCH)
all: $(EXC)
$(EXC) : $(OBJ)
$(CCLOAD) $^ -o $@
$(OBJ): $(CODE)
# only compile -> "-c"
# See: https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Overall-Options.html#Overall-Options
# Next line is the came as: $(CC) $(CFLAGS) $(CODE) -o $(OBJ)
# $^ = source file; $@ traget file
$(CC) $(CFLAGS) $^ -o $@
@dogukan-bicer
Copy link

While compiling the blinking_led_pi4 file, I get the following error: ledblinking.s: Assembler messages: ledblinking.s:59: Error: internal_relocation (type: OFFSET_IMM) not fixed up make: *** [makefile:26: ledblinking.o] Error 1
Can you help me?

@mathis-m
Copy link

Anyone who might need a full version that can blink any GPIO PIN have a look at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment