faiz180490
Posts: 8
Joined: Sun Dec 11, 2016 11:51 am

Bare-Metal Programming on RPi3

Sun Dec 11, 2016 1:02 pm

Hi,

This is my first post in this forum and I am a new user of the Raspberry Pi. Therefore, if I make any mistakes or ask naive questions please forgive me. But do let me know so that I don't repeat them.

I recently bought a Raspberry Pi 3 Model B and wanted to try and learn bare metal programming on it by trying to implement the Baking Pi tutorials:
http://www.cl.cam.ac.uk/projects/raspbe ... index.html
I searched online for any updated version of this tutorial for the RPi3 but didn't find any.

As per my understanding, the datasheet for the BCM2870 SOC on Rpi3 is not available. But I got the brief schematics for it: https://www.raspberrypi.org/documentati ... EDUCED.pdf
From this document I deduce that the ACT LED is connected to GPIO25 which is Pin no. 22. Am I right?
If I am right about the pin number, then can anyone help me understand how I need to enable this particular pin and write to it using assembly code? In general how do I use assembly code to access these pins?

Also what is the physical address I need to start with? The RPi B+ uses 0x20200000. Is it the same for Rpi3 or has the mapping changed?

Another question I have is what are the minimum files required to boot up the pi with the SD card? I guess I need the bootcode.bin, start.elf and fixup.dat files along with the kernel.img file that I get. Is there anything else needed?

Also any resources on running bare metal code on the Rpi3 using assembly would be appreciated.

faiz180490
Posts: 8
Joined: Sun Dec 11, 2016 11:51 am

Re: Bare-Metal Programming on RPi3

Sun Dec 11, 2016 4:05 pm

Okay, so I learnt my first lesson on posting questions on the forum i.e. Check for other similar posts before asking the question. Sorry and I will remember this from next time.

So I read through some of the other posts on the topic like the ones below:
viewtopic.php?f=72&t=159861
viewtopic.php?f=72&t=145329
viewtopic.php?f=72&t=139684

So from these posts, I summarize that the ACT LED is now connected to the GPIO expander which is located somewhere on the I2C and this is not accessible via the normal GPIO ports. Hence we need to use mailbox interface to control the LED, example code is given here:
https://github.com/vanvught/rpidmx512/b ... pio_virt.c

For anyone looking for the docs on BCM2837 (not BCM2879 as in the post, sorry for the typo) read this:
viewtopic.php?f=72&t=155104

The posts above answered most of my questions. However, I would like to know if there are any updates on the docs for the BCM2837 and if anyone has anything else to add or share any resources for getting started with bare metal programming using the RPi3.

AlfredJingle
Posts: 69
Joined: Thu Mar 03, 2016 10:43 pm

Re: Bare-Metal Programming on RPi3

Tue Dec 13, 2016 10:59 pm

hi faiz180490,

I started baremetal programming 18 months ago or so, and learned a lot from the pages of dwelch (just use Google to find them). He is active on this forum and you can learn a lot from him/his pages. I did.
going from a 6502 on an Oric-1 to an ARMv8 is quite a big step...

faiz180490
Posts: 8
Joined: Sun Dec 11, 2016 11:51 am

Re: Bare-Metal Programming on RPi3

Thu Dec 22, 2016 12:00 am

Hi AlfredJingle,

Sorry for the really late reply and thank you for letting me know about the dwelch's repo for all things bare metal on the rpi. I have began the baking pi tutorials today, just to get my confidence up about bare metal programming. I will look at dwelch's repo after maybe few lessons of the baking pi tutorial. There is a lot to learn there. In any way I am really excited to start!!

Makogan
Posts: 71
Joined: Tue May 16, 2017 9:17 pm

Re: Bare-Metal Programming on RPi3

Fri May 19, 2017 5:39 am

Hello, I was just wondering If you could share how you managed to get the ACT LED working. I have seen multiple assembly and C examples and yet I seem to have problems getting the LED to work with my code (it does work with a borrowed assembly code). At this point I am not sure if it is that I am making a compilation mistake or if my logic itself is wrong. It's be really helpful if I could look at some working implementation. Thank you.

bscheffold
Posts: 4
Joined: Sun Nov 29, 2015 8:28 pm

Re: Bare-Metal Programming on RPi3

Mon Oct 23, 2017 9:15 pm

Hello,
i am just learning bare-metal programming the rpi3 using the tutorial of alex chadwick, that i found very helpful. His tutorial is targeting the rpi1 and i found some hints that also work with the rpi2 and some of them are also valid for the rpi3 (e.g. change of gpio-base-adress). All in all i was able to reproduce the lessons up to lesson 5 toggling a gpio pin (not the LEDs... but pins, that are accessible on the gpio header). But now i am struggling with the screen-examples. I changed the base adress of the mailbox to 0x3f00b880 ... but could not get the wanted result on the screen.
can anybody help me with source code, based on chadwicks lessons 6 (screen01)ff. , for the rpi3?
best regards
Bruno

LdB
Posts: 1209
Joined: Wed Dec 07, 2016 2:29 pm

Re: Bare-Metal Programming on RPi3

Tue Oct 24, 2017 2:26 am

His code is correct, your address is correct I suspect what you need to know is the GPU address is different. On the Pi1 the alias offset to turn an ARM address to GPU is 0x40000000, On the Pi3 it is 0xC0000000.

So this line is incorrect on a Pi3
1. Write the address of FrameBufferInfo + 0x40000000 to mailbox 1.
On a Pi3 it is
1. Write the address of FrameBufferInfo + 0xC0000000 to mailbox 1

Then later on
3.) Send the address of the frame buffer + 0x40000000 to the mailbox.
On a Pi3 becomes
3) Send the address of the frame buffer + 0xC0000000 to the mailbox.

I would also strongly suggest you use the logical OR (ORR opcode) not add the values. It removes any chance your can roll the address out to somewhere other than the GPU , so is much much safer :-)

bzt
Posts: 386
Joined: Sat Oct 14, 2017 9:57 pm

Re: Bare-Metal Programming on RPi3

Tue Oct 24, 2017 12:25 pm

bscheffold wrote:
Mon Oct 23, 2017 9:15 pm
can anybody help me with source code, based on chadwicks lessons 6 (screen01)ff. , for the rpi3?
best regards
Bruno
Hi,

I don't know whether this interest you, but I have a source code for RPi3 frame buffer which uses mailbox properties channel instead of FrameBufferInfo. With my implementation you don't need special address for the mailbox, any uint32_t array will do.
My mailbox implementation and my frame buffer code.

Cheers,
bzt

bscheffold
Posts: 4
Joined: Sun Nov 29, 2015 8:28 pm

Re: Bare-Metal Programming on RPi3

Tue Oct 24, 2017 9:13 pm

@bzt: thank you for your code examples... to me they look very "clean" and very powerful ... maybe to me - as a beginner - too difficult to access. but i will keep them in mind... until i will see the things clearer.
@LdB: thank you for your hint concerning the different GPU address of the rpi3. I tried it out but still no sign on the screen.
maybe you / someone else can recommend me some other small bare-metal code that will work on the rpi3 writing pixels to the screen. The code should be "slim" because as a beginner i tend to get confused :shock: . (this is really a benefit of chadwicks tutorial: it raises scope of operation very slowly ... so it is easy to follow)
Best regards - bruno

LdB
Posts: 1209
Joined: Wed Dec 07, 2016 2:29 pm

Re: Bare-Metal Programming on RPi3

Wed Oct 25, 2017 5:16 am

So I downloaded the code for tutorial 6 from that site and I am not sure what is going with that tutorial, perhaps it is really old the structure is strange.

Corrected the tutorial as best I can
https://github.com/LdB-ECM/Exchange/tre ... r/screen01

You should get this on screen the lines walk around slowly
https://github.com/LdB-ECM/Docs_and_Ima ... g?raw=true
Image
Last edited by LdB on Wed Oct 25, 2017 6:32 am, edited 2 times in total.

bzt
Posts: 386
Joined: Sat Oct 14, 2017 9:57 pm

Re: Bare-Metal Programming on RPi3

Wed Oct 25, 2017 11:03 am

bscheffold wrote:
Tue Oct 24, 2017 9:13 pm
@bzt: thank you for your code examples... to me they look very "clean" and very powerful ... maybe to me - as a beginner - too difficult to access. but i will keep them in mind... until i will see the things clearer.
Okay :-) Here's a little explanation:

What we have here, is a bunch of defines (lines 216-235), mailbox write function (lines 238-242) and mailbox read function (lines 243-251). Same with all the other examples. I've added another function, mbox_call (lines 253-257), which writes to the mailbox, reads the response back and checks if response valid (also can be found in all the other examples, just not in a separate function but always inlined).

The frame buffer code is really simple, fills up mbox[] array with values (lines 748-790), then uses the aforementioned mbox_call (line 792) to get the response in the same mbox[] array.
  • The first element in the array is the array's size in bytes, the second is a magic value for request. After that come some command packets (each separated by an empty line in the source), finally a zero terminator element (line 790). I've used commands equivalent to FrameBufferInfo plus pixel order. That's all.
  • Probably unnecessary, but I've added extra checks for the response (line 792), the depth must be 32 bit (mbox[20]) and the frame buffer allocation must have returned a valid 8 bytes response (mbox[27]) with a non-empty address (mbox[28]). If so, it's safe to use the values (width, height, pitch, pointer etc., lines 793-799) just as you would from a FrameBufferInfo. There's a one-by-one relation to FrameBufferInfo fields and certain mbox[] elements, I've added comments for them.
I haven't used defines for tags and mbox index because I use them only once so comments are good for me. Detailed description of the tags and their arguments can be found here.

If you have problems with converting C into Assembly, take a look at Peter Lemon's example. Equivalent of mbox_call is here, and mbox[] array is here. And as I see, LdB also using this property channel mailbox instead of FrameBufferInfo, but his mailbox routines are far better and more bullet-proof than Peter's.

Cheers,
bzt

bscheffold
Posts: 4
Joined: Sun Nov 29, 2015 8:28 pm

Re: Bare-Metal Programming on RPi3

Wed Oct 25, 2017 9:45 pm

@bzt & LdB: thank you very much for your help - did not expect to get a solution that fast. I downloaded the files and it worked :D ... exept that i could not see the coloured lines "walking" - it is just a still image exactly like the one LdB downloaded it. But i will check the code and am confident to find the reason for this. For me this result is a good starting point for the next steps - thanks a lot for that jump start.
Until now, the code structure of chadwicks lecture was fine for me because it picked me up from my experince with assembler programming of atmels 8051 derivates - just for the demonstration of how to adress rpi infrastructure in principle it is a quick approach. But i am well aware, that the huge potential of rpi hardware makes it necessary to use functions like yours, bzt.

thanks again ... the next stumbling block is waiting ;)

deepika
Posts: 1
Joined: Sun Mar 25, 2018 7:42 pm

Re: Bare-Metal Programming on RPi3

Sat Mar 31, 2018 5:38 pm

I followed https://github.com/mauri870/baking-pi
this code to replicate the results using "arm-none-eabi" toolchain
copied the generated kernel.img as kernel7.img to sd card having Raspbian lite preinstalled on Raspberry pi 3 model B ,

I am not able to understand why I am getting "screen of death " somehow pi is not able to read the generated kernel image , but when replaced with Raspbian image everything works fine ... can someone help point somewhere ?

Return to “Bare metal, Assembly language”