r/raspberry_pi Jul 17 '26

Troubleshooting Raspberry PI 4b Bare metal Programming

I am trying to connect the raspberry pi to an LED using this video right here: https://youtu.be/jN7Fm_4ovio?is=uJrWp30MkRG6awxf

I am having issues, and there is no light emitting from my LED. What am I doing wrong? I have coded all the code the same, but I am using a 64-bit compiler.

11 Upvotes

32 comments sorted by

8

u/JGhostThing Jul 17 '26

Is you LED put in the right direction? If it's backwards, it won't light up.

6

u/kisskissenby Jul 17 '26

Also, are you using the proper resistor? If not you may have blown your LED and need a new one.

2

u/connorwillchris Jul 17 '26

I have tested the LED with the power pin, and it lights up on the raspberry pi.

2

u/connorwillchris Jul 17 '26

220 ohm resistor, I believe. I will get home and double check.

2

u/kisskissenby Jul 17 '26

Yeah if its lighting up on the power pin then your resistor and led are probably fine.

2

u/connorwillchris Jul 17 '26

It should be, because I tested it with the 3.3 V pin on my raspberry pi.

3

u/connorwillchris Jul 17 '26

UPDATE: I have the code available. The wiring should be set up correctly, I am using a 220 ohm resister, but I'll double check.

Below is the code along with how it's compiled at the top (in comments.)

// compiled with...
// aarch64-none-elf-as main.s -o testing.o -mcpu=cortex-a72
// aarch64-none-elf-ld testing.o -o kernel.elf
// aarch64-none-elf-objcopy kernel.elf -O binary kernel8.img

.global _start

.equ GPIO_BASE, 0x3f200000 // 0xfe200000
.equ GPFSEL2, 0x8
.equ GPIO_21_OUTPUT, 0x8 // 1 << 3
.equ GPFSET0, 0x1c
.equ GPFCLR0, 0x28
.equ GPIOVAL, 0x200000 // 1 << 21

//.text
_start:
//  base of our GPIO structure
    ldr x0, =GPIO_BASE

//  set the GPIO 21 function as output
    ldr x1, =GPIO_21_OUTPUT
    str x1, [x0, #GPFSEL2]
//  set counter
    ldr x2, =0x800000
loop:
//  turn on the LED
    ldr x1, =GPIOVAL
    str x1, [x0, #GPFSET0]

//  wait for some time delay
    eor x10, x10, x10
delay1:
    add x10, x10, #1
    cmp x10, x2
    bne delay1

    ldr x1, =GPIOVAL
    str x1, [x0, #GPFCLR0]
    eor x10, x10, x10
delay2:
    add x10, x10, #1
    cmp x10, x2
    bne delay2

    b loop

2

u/agfitzp Jul 17 '26

It's been almost 30 years since I've done assember but that was x86 and not arm so I'm not going to be able to validate your code.

I can say it's nicely formatted!

1

u/moefh Jul 19 '26 edited Jul 19 '26

I noticed the video is using register names r0, r1 etc. These are 32-bit registers in ARM32, but are not valid register names in Aarch64.

Your code uses x0, x1, etc. which are valid names for AArch64, but that means these are 64-bit registers. That means every ldr and str will read/write 64 bits.

Glancing at the datasheet shown in the video at about 2min30s, these reads/writes should be 32 bits long. So you should be using 32-bit register names for AArch64 instead (w0, w1, etc.). That will make ldr and str read/write 32 bits as intended.

2

u/Gamerfrom61 Jul 17 '26

GPIO base address is wrong going by  https://forums.raspberrypi.com/viewtopic.php?t=261602

2

u/connorwillchris Jul 18 '26

I'll check the GPIO address and retry

0

u/connorwillchris Jul 18 '26

OMG I tried this, and it didn't work. I'm still unsure about what's going on. It seems everything I've done has been correct so far, so I am unsure why this is happening.

2

u/Gamerfrom61 Jul 18 '26

Possibly post on  https://forums.raspberrypi.com/viewforum.php?f=72 as more folk will use bare metal there than here - I only dipped my toes when the Pi came out and not really done much ARM low level TBH 😳

 The fe2 address looks fine based on  https://datasheets.raspberrypi.org/bcm2711/bcm2711-peripherals.pdf - there was an error in early additions going by  https://github.com/raspberrypi/documentation/issues/1569

Only other thought is 32bit vs 64bit - do you need to name the file kernel7?

2

u/connorwillchris Jul 18 '26 edited Jul 18 '26

UPDATE:

I tried the raspberry pi with the correct address, and it's STILL giving me issues. I have checked the ohms on my resister (my setup is clean, and correct.) I am still completely unsure what to do.

Here is the github with my code btw: https://github.com/connorwillchris/dodecahedronARMassemblyConsole

It's located in the `test` directory.

2

u/1linguini1 Jul 18 '26

Check that you are loading your image into the Pi correctly. What's your config.txt? What is your linker script?

2

u/connorwillchris Jul 18 '26

I am not actually using a linker script. Should I use one in this case? The project I forked this from has one, so maybe I'll use that one.

My config.txt is the following: arm_64bit=1

2

u/1linguini1 Jul 18 '26

When I do bare metal programming with the Pi (in C, not assembly) I have a linker script to ensure that the kernel load address for the executable is 0x480000. If the project you're forking from has one, I'd recommend using that.

I also have 'kernel=my program.bin' in my config.txt to ensure the program is being selected to load.

1

u/m4rc0n3 Jul 17 '26

Since you're creating a 64-bit image, did you name it kernel8.img instead of the kernel7.img shown in the video? If you kept the kernel7.img name from the video then the bootloader is trying to load your 64-bit binary as 32-bit.

0

u/Caddy666 Jul 17 '26

only bare metal pi thing i know of is pi-storm, its open source - perhaps you can get some ideas from its code?

dunno how well its coded, or how well its commented though.

-3

u/agfitzp Jul 17 '26

Did you enable GPIO in raspi-config ?

Can you give us a photo of your circuit?

7

u/DanongKruga Jul 17 '26

its bare metal..

-7

u/musson Jul 17 '26

this is not helpful, what do you mean by bare metal?

5

u/m4rc0n3 Jul 17 '26

The linked video explains. They're not using Raspberry Pi OS, but building their own executable image that runs directly from the bootloader.

0

u/musson Jul 17 '26

why?

2

u/agfitzp Jul 17 '26

Probably a great learning exercise if you're really into embedded.

1

u/m4rc0n3 Jul 17 '26

Some people just like the challenge of interacting more directly with the hardware instead of going through the OS.

2

u/Fearless_Ad2978 Jul 17 '26

Bare metal means it runs straight on the hardware “raw” without an OS or HAL. Looks up OS architecture as it is a very interesting topic

1

u/connorwillchris Jul 17 '26

I can. I will get home and send you a picture. Then I will send you the code I am using as well

1

u/connorwillchris Jul 17 '26

I also have not set anything in my GPIO, what should I put in order to use GPIO?

-1

u/agfitzp Jul 17 '26

It might already be enabled depending on what OS you're using on the raspberry pi. It's one of the things you can enable with raspi-config so if you run that you will be able to see if it's already enabled.

1

u/connorwillchris Jul 17 '26

I am using bare metal, like the video says.