APinto
Posts: 11
Joined: Sun Jun 24, 2012 9:05 am

Adventures in bare metal

Sun Jul 08, 2012 12:00 am

Hello!

As someone pointed out in another thread, a lot of the activity around the Raspberry Pi seems to focus on Linux. Although it's wise the stand in the shoulders of giants, I usually find myself off the beaten path. Comfort is usually something I do not seek in computing!

That said, I would like to do some bare metal development. But I don't have a clue where to start from. I have almost 2 decades of high-level programming experience, and a few years of low-level programming experience long time ago. I also have extensive knowledge in electronics.

I know that the RPi starts by loading the firmware (start.elf) that then looks for a kernel file (kernel.img). From that point on, I need some help :). I presume I will at least need the following:
  • A compiler that targets ARM code;
  • Datasheets or some documentation for the hardware;
  • A video and network stack.
Can any of you point me in the right directions? Thank you very much!

AP

User avatar
jbeale
Posts: 3367
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Adventures in bare metal

Sun Jul 08, 2012 12:17 am

Have you seen these threads?

Got bare bones graphics working post by DexOS » 30 May 2012 18:49
"Good news, we (as in Dex and Dave_G_2) have got a bare-bone OS (start of DexOS port) to boot and run our GPIO code and setup the screen res, get buffer address and display a image on the Raspberry PI board." - http://www.raspberrypi.org/phpBB3/viewtopic.php?t=7004

also this: http://www.raspberrypi.org/phpBB3/viewt ... 29&t=10167

bobc
Posts: 86
Joined: Fri Apr 06, 2012 8:01 am

Re: Adventures in bare metal

Sun Jul 08, 2012 12:51 am

The problem here is that to provide useful advice it needs to be specific, and there are plenty of details involved. So any guides I can point you to are for specific hardware/development environments, which will give you an idea what to look for but not specifically for the Pi.

For example, James Lynch's guide http://www2.amontec.com/sdk4arm/ext/jly ... 061124.pdf is a comprehensive step by step for specific set of tools, and is now somewhat out of date.

-- A compiler that targets ARM code;

The Codesourcery Lite toolchain (now Mentor graphics) is widely used. You will need the arm-none-eabi variant.

-- Datasheets or some documentation for the hardware;

Hmm tricky.. I believe there are some minimal docs in the wiki.

-- A video and network stack.

More tricky.. those are in Linux ;) There are several open source networks stack around, you will need to do some porting at least. I don't think the network interface can be accessed easily, and graphics will be limited.

The thing about bare metal, is you don't get the support software you normally take for granted. Expect to be writing your own!

The traditional place to start is with the "blinky" project. Flashing an LED might sound trivial, but there is more to it than meets the eye.

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Adventures in bare metal

Sun Jul 08, 2012 2:39 am

APinto wrote:Hello!

As someone pointed out in another thread, a lot of the activity around the Raspberry Pi seems to focus on Linux. Although it's wise the stand in the shoulders of giants, I usually find myself off the beaten path. Comfort is usually something I do not seek in computing!

That said, I would like to do some bare metal development. But I don't have a clue where to start from. I have almost 2 decades of high-level programming experience, and a few years of low-level programming experience long time ago. I also have extensive knowledge in electronics.

I know that the RPi starts by loading the firmware (start.elf) that then looks for a kernel file (kernel.img). From that point on, I need some help :). I presume I will at least need the following:
  • A compiler that targets ARM code;
  • Datasheets or some documentation for the hardware;
  • A video and network stack.
Can any of you point me in the right directions? Thank you very much!

AP
You will need these:
http://www.raspberrypi.org/wp-content/u ... herals.pdf
http://infocenter.arm.com/help/topic/co ... p7_trm.pdf

Info to get screen buffer working
* ************************************************************************ *
* **** BCM2835 Specific Stuff **** *
* ************************************************************************ *
* This is where things get interesting (and specific to the BCM2835).
* Most of this was worked out by reading the Linux source code (mostly
* drivers/video/bcm2708_fb.c and arch/arm/mach-bcm2708/) and experimentation.
*
*
* **** Basic procedure to get stuff on screen ****
* The basic procedure to get a frame buffer is:
* 1) Set up a structure with the frame buffer specification (resolution, etc)
* 2) Tell the GPU about this structure by writing to the mailbox
* 3) Wait by reading from the mailbox for the GPU to modify this structure
* 4) Write to the frame buffer at the pointer we got in stage 3
* Only step 4 is required for subsequent writes to the frame buffer. Currently,
* I do not know how to enable the HDMI output, so this will always operate the
* composite, and not the HDMI.
*
*
* **** Mailbox operations ****
* Read/write operatitons on the mailbox consist of transfering data via a
* 32 bit register. 28 bits of this 32 bit register are the data to be sent
* to the receiver, while the lower 4 bits specify the channel (channel 1 is
* the frame buffer, but there are others).
*
* To send data via the mailbox:
* 1) Wait for space in the mailbox
* 2) Write ((data << 4) || channel) to the write register TODO: Make implementation match
*
* To receive data via the mailbox:
* 1) Wait for the mailbox to be non-empty
* 2) Execute a memory barrier
* 3) Read from the read register
* 4) Check the lowest 4 bits of the read value for the correct channel
* 5) If the channel is not the one we wish to read from (i.e: 1), go to step 1
* 6) Return the read value >> 4 TODO: Make implementation match
* Note: This will not work if we're interested in reading from more than one
* channel as it does not handle the reception of other channels' data
*
*
* **** Memory mapped registers ****
* The bus address for the mailbox memory mapped registers is 0x7E00B880.
* This corresponds to an ARM physical address of 0x2000B880 (the address we
* use from the ARM processor, and hence here). We use three registers from
* the mail box:
* - The read register for mailbox 0 at offset 0x00
* - The status register for mailbox 0 at offset 0x1C
* - The write register for mailbox 0 at offset 0x20 (this is actually the read
* register for mailbox 1).
*
*
* **** Notes ****
* - The address of the frame buffer must be at least a multiple of 16 (in
* order to be accurately transmitted in the 28 bits available in the
* mailbox)
* - The 32 bit value we actually send over the mailbox (including the channel)
* is (ADDRESS | 1) where ADDRESS is the address of the structure. This is
* equivalent to sending as the data (ADDRESS >> 4) (remember we do data << 4)
* - This works if we set vwidth = width, vheight = height, x = 0, y = 0.
* - I haven't managaged to make anything but 24 bit depth work, however the
* Linux source seems to use 16 bit?!
* - Sometimes the procedure described to get stuff on the screen doesn't
* work first time. I've hacked around this by repeating until it does work.
* - The two conditions for successfully acquiring a frame buffer are:
* - The data read from the mailbox (with the 4 least significant bits set
* to zero) is 0 (or 1 including the channel)
* - The pointer in the structure is non-zero after the mailbox read
* - Once we have the frame buffer, we can just write to it. The pixels (in
* 24 bit mode) are RGB ordered by y then x coordinate. The address of a
* subpixel is given by: y * pitch + x * 3 + rgb_channel, where rgb_channel
* is 0 for red, 1 for green, and 2 for blue.
For a assembler i use FasmArm
http://arm.flatassembler.net/

You will find lots of bare metal demos doted around the forum.
I have avoided dumping code, because i want to make a useful kiss OS for the R-PI, i want the OS to be useful and not the code.
Once my OS is useful i will release the code as a whole.
The problem with hobby OS is every one reinvents the wheel, over and over again and little progress is made.
This is fine if you understand, that bare metal coding is you with limited doc and lots of testing.
So when bare metal OS's make progress and start to be useful maybe others will help out, but its up to you to get it to that point.
So good luck and any ? just ask.
Batteries not included, Some assembly required.

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Adventures in bare metal

Sun Jul 08, 2012 7:41 am

I would personally strongly recommend building yourself (or otherwise acquiring) an arm-targetting bare metal set of the gnu tools. It's what everybody apart from dex uses, gets you more than just an assembler, and can eventually be hosted on the pi.

A lot of the information you need has been posted in this thread already, but there's a lot dotted about on the forum that isn't directly referenced anywhere. And, of course, some of it is now wrong.

Initially at least, you will need a ttl 3v3 serial adaptor. It's the easiest way to talk to your pi. Don't even think about trying to write a usb stack yet :)

Probably the best starting point is Dave's stuff on github (https://github.com/dwelch67) which breaks it all down into tiny pieces. Also check my .sig, but it's less easy to digest.

Simon

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Adventures in bare metal

Sun Jul 08, 2012 2:14 pm

tufty wrote: Initially at least, you will need a ttl 3v3 serial adaptor. It's the easiest way to talk to your pi. Don't even think about trying to write a usb stack yet :)
I agree with tufty, its the single most important bit of hardware, after the PI for bare metal coders.

Also me using FasmArm has the big advantage for me, in that i can developer on my own OS, has my x86 version of my OS, has a fasmArm port.
Batteries not included, Some assembly required.

APinto
Posts: 11
Joined: Sun Jun 24, 2012 9:05 am

Re: Adventures in bare metal

Sun Jul 08, 2012 10:53 pm

Thank you very much guys! :) I think this thread has more than enough information for me to start my adventure!

Regards,

AP

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

Re: Adventures in bare metal

Wed Jul 11, 2012 3:01 am

http://github.com/dwelch67/raspberrypi

the buildgcc directory has a script for building the gnu tools (binutils (assembler, linker) and gcc (c compiler). then starting with the blinker examples a number of mostly C with asm startup and support code walking through some of the peripherals in baby steps demonstrating what they do. Or just get the LITE version from mentor graphics, http://codesourcery.com and you will be redirected. (LITE version is a free download windows or linux).

Ultimately you will need a serial/uart solution, I have links to some/many (the jim.sh/ftx is pretty cool, but you need a kernel new enough on your host if running linux to use it, otherwise the $14 solutions at sparkfun.com or other places work just fine). And if you can solder you can try the jtag approach as well (I have solutions linked as well). I have a serial/uart based bootloader so you dont have to wear out your sd card slot and a jtag bootloader that enables the arm jtag stuff so you can use that approach along with an openocd config script to get you started with the raspberry pi.

Once you can "see" something either blinking an led or spitting some items out the uart, it is off to the races, have some fun programming at this level, no operating system means no rules, however and whatever you want. I should have links to the docs there as well....hmm.. dont have the arm arm and arm trm listed/linked, see the answer in a prior post about infocenter.arm.com or go to one of my other repos (mbed_examples) to find the general info on how to get the right docs from arm...

arturo777
Posts: 28
Joined: Sat Mar 24, 2012 10:02 am

Re: Adventures in bare metal

Wed Jul 11, 2012 8:21 am

Talking about boot loaders etc, I have a question:

Is it possible to boot a bare metal program using "kexec"? In theory this now exists for ARM. That would be an interesting way of developing on the Pi itself. Couple that with a little routine to reset the system after running the program, one could actually develop on the Pi without changing the SD card or pulling the power all the time :)

For anyone looking for reset code, the method used in the function bcm2708_power_off(void) in /arch/arm/mach-bcm2708/bcm2708.c should do it, coupled with void arch_reset(char mode, const char *cmd) in /arch/arm/mach-bcm2708/include/mach/system.h

Searge
Posts: 4
Joined: Sun Jul 15, 2012 8:11 am

Re: Adventures in bare metal

Sun Jul 15, 2012 8:21 am

I also found a lot of useful information for bare metal coding in the RiscOS sources for rpi. Maybe you can get all this from looking at the linux sources, but I like the fact that most of the information I need can be found in one place.

I downloaded the source code from https://www.riscosopen.org/content/down ... s-tarballs - the BCM2835Dev archive is the right one.

All the information I needed to get started could be found in the directory mixed/RiscOS/Sources/HAL/BCM2835

It's assembly code, so you should have a basic understanding to use the information in those files, but everything needed to get started should be there.

Searge
Posts: 4
Joined: Sun Jul 15, 2012 8:11 am

Re: Adventures in bare metal

Sun Jul 15, 2012 8:35 pm

One other thing I just noticed was the wiki at https://github.com/raspberrypi/firmware/wiki/_pages

Not sure if it was mentioned before, but for getting something on the screen instead of just blinking some LED, this looks most useful: https://github.com/raspberrypi/firmware ... -interface

User avatar
Cycl0ne
Posts: 102
Joined: Mon Jun 25, 2012 8:03 am

Re: Adventures in bare metal

Sun Jul 15, 2012 8:55 pm

it will come better.. at the moment they are looking forward to release the complete mailbox messages to public. with them you can do everything .. from 2d, 3d, sound, video,....

Searge
Posts: 4
Joined: Sun Jul 15, 2012 8:11 am

Re: Adventures in bare metal

Sun Jul 15, 2012 9:35 pm

Cycl0ne, that'd be awesome. If that happens I'll probably never run any Linux flavors on my RPI again. I've been coding close to the hardware on C64 and Amiga in the 80's and PC in the 90's, so this will be my next low-level toy.

I'll never have to look back to the good old 3dfx/GUS times ;) Can't wait to port my libraries, games and demos to the RPI.

I wonder if the VC is capable of doing rasterline synced effects, but if they release the whole docs for the messages, I'll soon know.

User avatar
Cycl0ne
Posts: 102
Joined: Mon Jun 25, 2012 8:03 am

Re: Adventures in bare metal

Sun Jul 15, 2012 10:40 pm

Have a look here:

http://elinux.org/RPi_VideoCore_APIs

you can disassemble the lib and look for yourself Function -> what it places into the VCHIQ Service. Then you see whats all possible with the videcore.

and to be honest.. i really like this way BCM does this. and first i thought: binary_blob and closed source driver-- URKS.. but this mailbox thing is great ;-) they can hide all from us and we have an api to make all things..

Searge
Posts: 4
Joined: Sun Jul 15, 2012 8:11 am

Re: Adventures in bare metal

Sun Jul 15, 2012 11:02 pm

Yeah, it's a very good solution. Close enough to the hardware to do a lot of "nasty" things, but still a more reliable way to access the hardware than messing with registers directly.

Reminds me a lot of the 3dfx API, very low level, but better than just a bunch of hardware registers.

Thanks for the link, I already started disassembling some of the libraries, but for the moment I plan to try things on my own and only look at the libraries code when I get stuck.

The mailbox documentation we have in the firmware wiki should be enough to get started, I'll already be happy if I'm able to put some text on the screen.

After that's working I have to take care of input. Either get USB or LAN working, so I can attach a keyboard or use telnet. Depends on the documentation I can find.

User avatar
Cycl0ne
Posts: 102
Joined: Mon Jun 25, 2012 8:03 am

Re: Adventures in bare metal

Mon Jul 16, 2012 12:42 am

look into the USB thread. http://www.raspberrypi.org/phpBB3/viewt ... 72&t=10485

USB is "relatively" hard. Got an USB Stack working with Ethernet and Mass Storage and Mice, but no Keyboard :-/ Send a PM with your email and I send you the "usb stack" to have a look at. i havent put this into my repo yet, because of the limited functionality.

The "mailbox api" page you gave was implemented for the ppl from Riscos and is very incomplete. you cant switch from CVBS/HDMI with this mailbox mechanism.

Return to “Bare metal, Assembly language”