AliSyed
Posts: 2
Joined: Mon Aug 15, 2016 7:28 am

Bare Metal with Pi Zero

Mon Aug 15, 2016 7:48 am

Good Morning,

I am a student and I am interested in working bare metal. I have worked with RPi 2 and world was a happy place (thanks to David Welch's collection of bare metal code samples). Recently I bought my new RPi Zero. Unfortunately, I am even unable to flash the ACK LED. There has been discussion on this forum regarding RPi Zero GPIO layout to be similar to RPi 2 or RPi B+. For flashing ACK LED, I have tried both GPIO 16 and 47 from RPi 2 and RPi B+ layouts, respectively. But no success. I don't know if there might be other differences in RPi 2 and RPi Zero.

I also looked for available bare metal code examples for RPi Zero but no luck. I know there has been OS ports to RPi Zero like Ultibo and Linux, but frankly PASCAL is not my cup of tea and Linux is too gigantic to see the clear picture for RPi Zero. I was wondering if anybody else tried it. If yes, please share the code. Any help would be appreciated. Thanks in advance.

Best regards,
PS: Sorry, I am a bit short on time so I didn't read all the search results for this topic (but I tried). If this topic has been discussed before, please provide a link.

User avatar
Gavinmc42
Posts: 4678
Joined: Wed Aug 28, 2013 3:31 am

Re: Bare Metal with Pi Zero

Mon Aug 15, 2016 11:16 am

Welcome to bare metal.

You have read everything here first?
viewtopic.php?f=72&t=72260

I use Ultibo because I want to do stuff not RTFM ;)
Pascal is not that bad, you should try Awk or Sed :lol:

Stuff is hidden in the bootloader.bin, start.bin, fixup.dat and devicetree...
The GPU controls access but there are ways to get control access back with Linux.
Not so much documentation to do that under baremetal.

Try with a led on a GPIO pin first.
Zero is a BCM2835 so Pi2/3 code won't work anyway.
Different GPIO addresses 2/3 and even different method on the 3.

You could use Linux example code then disassemble it to look for clues?

You will be looking for clues in the source etc
https://github.com/raspberrypi/firmware ... t/overlays
Examining the reverse engineering links
https://github.com/hermanhermitage/videocoreiv

The Pi's are GPU's with ARMs attached.
The GPU boots first, ACT led flashes? GPU must have control.
How does Linux get it back? Or does it?

Baremetal for Pi's is a lot of forensic reverse hacking.
If you figure it out or google it, put it here.

And if you happen to figure out how to get i2c0 and i2c2 on the Zero working let me know.
Else I will need to RTFM myself:(

Baremetal OpenVG, OpenGL ES, OpenMax, Camera, Touchscreen still need answers.
http://elinux.org/Raspberry_Pi_VideoCore_APIs

Maybe some code to dump register/memory contents?
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

baantonia
Posts: 63
Joined: Fri Feb 06, 2015 2:19 pm

Re: Bare Metal with Pi Zero

Mon Aug 15, 2016 3:28 pm

A little project, very early days and probably not using the correct tools etc..... Doesn't use any of the libraries (although uses the headers) from the arm-none-eabi install. I'm running it on a Compute Module but in essence, apart from the speed difference and pin access, they are essentially the same.
Gets data from a mcp3002 chip on a Gertboard and sends the converted data through UART to putty. Currently building my own object files.
Sources in C and assembler, compiled and linked from a batch file.

It started out in pure assembler but then I thought how I could link object code from C. There was a small issue in that all access to the hardware was via svc or swi calls and this required extra glue for C to call these routines, I still have them but are not included in the zip file. If something is missing please let me know as I have only included those files I think were needed. I expect some of the header file paths could be changed and their corresponding files moved, ie there are header files in the c directory.
Attachments
project.zip
(16.83 KiB) Downloaded 175 times

AliSyed
Posts: 2
Joined: Mon Aug 15, 2016 7:28 am

Re: Bare Metal with Pi Zero

Mon Aug 15, 2016 5:11 pm

Thank you very much Gavinmc42 and baantonia for providing the links and the code sample. Having code samples from you made me have some faith in my code and I, therefore, started looking for problems somewhere else. I found out that my bootcode.bin and start.elf, which I used with RPi 2, were quite old and hence couldn't initialize RPi Zero. By using the latest files from https://github.com/raspberrypi/firmware ... aster/boot, I was able to see the ACK LED (GPIO47) flashing. Seams like, there are some differences in RPi Zero and other RPis and the booting process is one of them.

Again, thank you very much for your support.

dwelch67
Posts: 967
Joined: Sat May 26, 2012 5:32 pm

Re: Bare Metal with Pi Zero

Tue Aug 16, 2016 2:17 am

I have a simple blinker example, not Pascal (not that there is anything wrong with Pascal, because there isnt, very cool that it is being kept alive) for the pi zero

https://github.com/dwelch67/raspberrypi ... /blinker01

User avatar
Gavinmc42
Posts: 4678
Joined: Wed Aug 28, 2013 3:31 am

Re: Bare Metal with Pi Zero

Tue Aug 16, 2016 3:41 am

I found out that my bootcode.bin and start.elf, which I used with RPi 2, were quite old and hence couldn't initialize RPi Zero
Yep rookie mistake ;)
Keep an eye on the boot stuff.
Sometimes things pop up there before RPF announce it on the blog.

Nice things happening there.
USB boot, NetBoot etc mostly on Pi3 but there is USB boot stuff only on the Zero.
Things like using the Zero as a USB gadget and downloading boot/kernel code from a PC.
https://www.raspberrypi.org/documentati ... ootflow.md

All useful stuff for baremetal too.
Bootloader.bin/Startxx.elf do not care if kernel.bin is a Linux OS or a tiny baremetal led flasher or bigger Ultibo program.

Possible that one day bootloader.bin could be the app, it's around 18KB now and there maybe room to load a 128KB app.

So baremetal could be bootloader.bin or start.elf or kernel.bin.
Standard Start.elf is 2.7MB
My current IoT app in Ultibo makes kernel.bin of 1.8MB.

So GPU boots from internal rom, looks for bootloader.bin, grabs and run that.
Bootloader.bin looks for config.txt and loads the relevant Startxx.elf and fixupxx.dat.
Start.elf then looks for kernel.bin which could be in another location than boot SD card.
Kernel.bin then runs, 2secs in Ultibo.
Boot time in Linux depends on which Linux OS you have, piCore is about 10sec on a Pi3, Raspbian takes longer.

Could you baremetal your own bootloader.bin? That would be the fastest boot.
https://github.com/christinaa/rpi-open-firmware
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Return to “Bare metal, Assembly language”