emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Is Bare Metal code for me?

Sat Aug 18, 2012 1:48 am

I am on a highschool robotics team through a program called FIRST (you can take a look at http://www.usfirst.org/ftc). We are currently using a Raspberry Pi with the Raspbian linux as the main brain for our robot. I would like to blow away the Linux, and run the code straight on the Pi, but I am not really sure what I am getting in to. Thus, I have a list of questions I need answered before jumping in and coding anything.

1) What can I run on bare metal? Once I get everything initialized and booted, can I run C or C++ programs which interact with the GPIO pins?

2) If yes, what do I need to do in order to run bare metal code?

I am fairly proficient in C and C++, and am willing to go lower level into assembly and such, but I just haven't done it yet.

Thank you in advance for any and all input and advice.
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 2:39 am

Yes, no problem running C programs. Now that doesnt mean c libraries (printf, scanf, fopen, etc) which take more work. Depending on what you are planning to do bare metal can be much easier than using an operating system.

http://github.com/dwelch67/raspberrypi

At the same time though, you may want to still go with the operating system (ethernet, usb, etc).

The way the raspberry pi boots from a user perspective you can go back and forth between bare metal or full blown linux very easily...

emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 3:10 am

dwelch67 wrote:Yes, no problem running C programs. Now that doesnt mean c libraries (printf, scanf, fopen, etc) which take more work. Depending on what you are planning to do bare metal can be much easier than using an operating system.
Does the same apply to C++? I believe they compile down to the same code, but am not absolutely sure. Not having the C Standard Library is fine; my application should not need any of these functions (short of file IO, but that is optional and can come later). If I really need something, I can re-implement it.
dwelch67 wrote:At the same time though, you may want to still go with the operating system (ethernet, usb, etc).
I should only need a way to access the filesystem. I am currently using SSH / ethernet, but I can do the same thing with a SD card reader no problem.
dwelch67 wrote:The way the raspberry pi boots from a user perspective you can go back and forth between bare metal or full blown linux very easily...
Really? I have been poking around this forum for several days, and haven't seen anything about this. How is it done?

Thanks for the input! :D
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 7:42 am

One way is to manage two kernel.img files on the sd card. One is the file that comes with raspian or whatever linux you are using, the other is the kernel.img file that you are generating with your bare metal programming. Backup/rename the linux one as it is relatively static, when you want to put linux back copy/rename that file back to kernel.img and boot the raspi. This is described in my repo leading up to using a bootloader so that you are not constantly doing the sd card dance.

Easier is to have a second sd card, use one for linux the other for bare metal.

I dont know what additional overhead is needed for C++ as I dont use it, others have. And you may find that code sourcery lite is already ready to go. (sometimes the problem is just building the cross compiler) That would certainly be an educational process you can go through, you wont be making forward progress on your real project, but you will learn about what bootstrap and other behind the scenes items are required for C++ to work (independent of bare metal), which you can carry with you and use for many years to come. hardware is not required for this you can compile and disassemble, the compile and link then disassemble to see how things are put together.

David

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 7:53 am

If asking questions at this level and you have the need to access the filesystem that is a bit advanced. yes there are libraries, etc, out there that will manage the access to the sd card and do the file system work. bare metal ip stacks are available, I think you have to go through usb to get there.

Basically if you feel you have to have a file system, ip stack, etc then bare metal is not for you (until you have a lot more experience). You may find that you dont really need these things (the task of controlling a robot does not need these things) and can then return to bare metal.

If you can swing it I recommend seeing if you cannot get your own personal raspberry pi and sd card, then you can blink leds, play with timers, etc using C then C++.

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 10:29 am

If you're worried about libc (the stock c library), porting newlib to do the basics in terms of memory allocation and so on is relatively trivial. Googling 'newlib arm bare metal' should get you to a tutorial. Once you have that, and a low level sd card access library (reading and writing blocks), adding a fat32 layer is easy, there's loads of them out there.

Using USB (and therefore ethernet) is going to be the most difficult task. If you can live without that, you're golden.

Here, I saved you the googling time : http://balau82.wordpress.com/2010/12/16 ... -programs/

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 1:14 pm

Bare metal coding turns the PI into a supper powerful arduino, the ? is do you want a powerful arduino or do you want a under powered desktop ?.
Bare metal flashing leds can be as easy as

Code: Select all

include 'DexBasic\DexBasic.inc'                    
                                             
pinMode  GPIO16, OUTPUT                           

LetsLoop:                                        
digitalWrite  GPIO16, HIGH                       
delayMicroseconds  1000000                       
digitalWrite  GPIO16, LOW                        
delayMicroseconds  1000000                        
goto  LetsLoop                                    
                                                 
ProgramSize:                                       
ScreenBuffer: 
That's 100% asm in bare metal that hides the asm behind macro to make it the same as the arduino coding.
I mean do you want to wait for linux to boot up when you switch you robot on or do you want it to come on like this:
http://www.youtube.com/watch?v=TL71Nac2 ... el&list=UL
Batteries not included, Some assembly required.

Vanfanel
Posts: 433
Joined: Sat Aug 18, 2012 5:58 pm

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 6:06 pm

Hi there,

I'm new to these forums, but after seeing some videos of DexOS running on the EBox, I couldn't help myself but posting.
I love the DOS-esque looking of DexOS! Direct hardware access without slow hardware abstraction layers, light and fast boot like in the good old Amiga & DOS days...
However, I have some questions about this OS on the Rpi:

Is it possible to run the same commanline / gui on the Rpi port?
Is there usb and sound support on the Rpi? (In the form of well-documented macros for initialisation and interfacing, etc).
How hard would it be to port SDL to DexOS? I'm thinking about DexOS as a light OS for homebrew games programming on the Pi and as a light OS for things like MAME (crazy, but DOS was great for these things!).

regards

emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 7:14 pm

dwelch67 wrote:One way is to manage two kernel.img files on the sd card. One is the file that comes with raspian or whatever linux you are using, the other is the kernel.img file that you are generating with your bare metal programming. Backup/rename the linux one as it is relatively static, when you want to put linux back copy/rename that file back to kernel.img and boot the raspi. This is described in my repo leading up to using a bootloader so that you are not constantly doing the sd card dance.

Easier is to have a second sd card, use one for linux the other for bare metal.
Easy enough. Even I can understand that. This should remove the risk of destroying my existing filesystem, so no problems here.
dwelch67 wrote:If asking questions at this level and you have the need to access the filesystem that is a bit advanced. yes there are libraries, etc, out there that will manage the access to the sd card and do the file system work. bare metal ip stacks are available, I think you have to go through usb to get there.

Basically if you feel you have to have a file system, ip stack, etc then bare metal is not for you (until you have a lot more experience). You may find that you dont really need these things (the task of controlling a robot does not need these things) and can then return to bare metal.
I guess I wasn't very clear. I do NOT need TCP/IP, USB, or a filesystem. I do want to have an error log file, but this is optional. Can a single log file be handled by Bare Metal code?
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 7:16 pm

tufty wrote:If you're worried about libc (the stock c library), porting newlib to do the basics in terms of memory allocation and so on is relatively trivial. Googling 'newlib arm bare metal' should get you to a tutorial. Once you have that, and a low level sd card access library (reading and writing blocks), adding a fat32 layer is easy, there's loads of them out there.

Using USB (and therefore ethernet) is going to be the most difficult task. If you can live without that, you're golden.

Here, I saved you the googling time : http://balau82.wordpress.com/2010/12/16 ... -programs/
Thanks for the link! This should be quite doable. Do I need a full filesystem, or is it possible to just write a single file for error logging?
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 7:24 pm

DexOS wrote:Bare metal coding turns the PI into a supper powerful arduino, the ? is do you want a powerful arduino or do you want a under powered desktop ?.
Bare metal flashing leds can be as easy as

Code: Select all

include 'DexBasic\DexBasic.inc'                    
                                             
pinMode  GPIO16, OUTPUT                           

LetsLoop:                                        
digitalWrite  GPIO16, HIGH                       
delayMicroseconds  1000000                       
digitalWrite  GPIO16, LOW                        
delayMicroseconds  1000000                        
goto  LetsLoop                                    
                                                 
ProgramSize:                                       
ScreenBuffer: 
That's 100% asm in bare metal that hides the asm behind macro to make it the same as the arduino coding.
I mean do you want to wait for linux to boot up when you switch you robot on or do you want it to come on like this:
http://www.youtube.com/watch?v=TL71Nac2 ... el&list=UL
I want the super-strong Arduino. I also want to do the code myself and in C or C++, so I do not think I will end up using your code. It looks great, but is not quite the direction I want to go. I have coded for many years, so I would prefer the raw power of C to the easy-to-understand nature of the code snippet you shared.

Thanks for the video. A fast boot without any extra features is definitely the way to go for our robot.
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 7:33 pm

Vanfanel wrote:Hi there,

I'm new to these forums, but after seeing some videos of DexOS running on the EBox, I couldn't help myself but posting.
I love the DOS-esque looking of DexOS! Direct hardware access without slow hardware abstraction layers, light and fast boot like in the good old Amiga & DOS days...
However, I have some questions about this OS on the Rpi:

Is it possible to run the same commanline / gui on the Rpi port?
Is there usb and sound support on the Rpi? (In the form of well-documented macros for initialisation and interfacing, etc).
How hard would it be to port SDL to DexOS? I'm thinking about DexOS as a light OS for homebrew games programming on the Pi and as a light OS for things like MAME (crazy, but DOS was great for these things!).

regards
Not directly, it needed to be ported to arm, which i did, i had both gui and cli basics working, but i changed my mind about the need for a OS like DexOS on the PI.
As there are a lot of disadvantages when compared to linux, which has lots of drivers.
One of them is as you posted USB drivers, it would be a lot of work to code one for a bare metal OS, not that it could not be done, but more a case of unless there was a number of DexOS pi users it would not be worth the time it took to make it.
So i decide to make a bare metal interface to help coders use bare metal in a basic like language.
http://www.dex-os.com/DexBasic/DexBasic.htm

I may release the bare metal port of DexOS for the pi, once i find a good way to get user input.
I have Uart working and i have usb keyboard working (but users will need one of these http://www.hobbytronics.co.uk/usb-host-board )
I have IR also working.
Theres a bare metal GBA thats making good progress and you could use DexBasic to build a menu sys, and i may add some optimized graphic functions if there was call for it.
Hope this helps.
Batteries not included, Some assembly required.

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 7:39 pm

emcsquirreled wrote:
DexOS wrote:Bare metal coding turns the PI into a supper powerful arduino, the ? is do you want a powerful arduino or do you want a under powered desktop ?.
Bare metal flashing leds can be as easy as

Code: Select all

include 'DexBasic\DexBasic.inc'                    
                                             
pinMode  GPIO16, OUTPUT                           

LetsLoop:                                        
digitalWrite  GPIO16, HIGH                       
delayMicroseconds  1000000                       
digitalWrite  GPIO16, LOW                        
delayMicroseconds  1000000                        
goto  LetsLoop                                    
                                                 
ProgramSize:                                       
ScreenBuffer: 
That's 100% asm in bare metal that hides the asm behind macro to make it the same as the arduino coding.
I mean do you want to wait for linux to boot up when you switch you robot on or do you want it to come on like this:
http://www.youtube.com/watch?v=TL71Nac2 ... el&list=UL
I want the super-strong Arduino. I also want to do the code myself and in C or C++, so I do not think I will end up using your code. It looks great, but is not quite the direction I want to go. I have coded for many years, so I would prefer the raw power of C to the easy-to-understand nature of the code snippet you shared.

Thanks for the video. A fast boot without any extra features is definitely the way to go for our robot.
Your welcome, and i think you will find bare metal coding to your liking, as we are all control freaks.
So "want to do the code myself" is a good sign ;)
Good luck with your robot.
Batteries not included, Some assembly required.

Vanfanel
Posts: 433
Joined: Sat Aug 18, 2012 5:58 pm

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 9:17 pm

So, if I understood correctly (I'm not a native english-speaker and have some advanced comprehension problems), DexOS is lacking the CLI and the graphical interface on the Pi...
I for one believe it would be VERY interesting to have it working as a full OS on the Pi: a lowlevel DOS-style OS makes every platform complete.

And programming/debugging C/Arm ASM on the OS itself would be awesome, too: a simple CLI text editor and a gdb-style debugger would make it a bare-metal retro-programmer heaven on the Pi :)

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

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 9:56 pm

Vanfanel wrote:So, if I understood correctly (I'm not a native english-speaker and have some advanced comprehension problems), DexOS is lacking the CLI and the graphical interface on the Pi...
I for one believe it would be VERY interesting to have it working as a full OS on the Pi: a lowlevel DOS-style OS makes every platform complete.

And programming/debugging C/Arm ASM on the OS itself would be awesome, too: a simple CLI text editor and a gdb-style debugger would make it a bare-metal retro-programmer heaven on the Pi :)
Its most likely me, not explaining properly :) .
I have coded the cli and gui for the pi, but have not released them because there is not a way to get input from users, as i use a addon board to move menu or type stuff in.
Unless they have the addon board, that lets them use a USB keyboard.
They will not be able to use it or test it.
Batteries not included, Some assembly required.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Is Bare Metal code for me?

Sat Aug 18, 2012 11:55 pm

Wouldn't a ps2 keyboard plugged into GPIO be a better choice? It'd be loads cheaper...
note: I may or may not know what I'm talking about...

emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Re: Is Bare Metal code for me?

Sun Aug 19, 2012 1:26 am

DexOS wrote:Your welcome, and i think you will find bare metal coding to your liking, as we are all control freaks.
So "want to do the code myself" is a good sign ;)
Good luck with your robot.
Thanks for all the help. I am definitely going to go bare metal for this project!
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

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

Re: Is Bare Metal code for me?

Sun Aug 19, 2012 12:48 pm

Your welcome emcsquirreled and sorry if these post have high jacked your topic.
toxibunny wrote:Wouldn't a ps2 keyboard plugged into GPIO be a better choice? It'd be loads cheaper...
The problem with bit-banging a ps2 controller is its no small feat, by using a self contained usb chip, you can plug other usb devices in too.
Like hid temperature gauges or electronic devices etc.
The chip just converts keypress to Uart, so its just the same as being connected to to a pc and terminal as far as input is concerned.
but if you have any good examples of working ps2 kegboard on the pi, i may try it out to see how it works.
Also note the chips then selfs are not that expensive, so i was going to have some boards made up and sell them at about £5 each, with some other useful goodies on board.
Batteries not included, Some assembly required.

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

Re: Is Bare Metal code for me?

Sun Aug 19, 2012 4:10 pm

The bitbanging part of ps/2 is easy. Really. Stupidly easy. See http://www.computer-engineering.org/ps2keyboard/ for a pretty much comprehensive guide to doing it.

What makes it hard on the pi is that ps/2 is 5v, and the pi's gpio is 3v3. So you're not going to get away without additional hardware, and at that point you're probably better off using a 3v3 ttl serial/usb converter and accessing your pi via your main pc.

Simon

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

Re: Is Bare Metal code for me?

Sun Aug 19, 2012 4:38 pm

tufty wrote:The bitbanging part of ps/2 is easy. Really. Stupidly easy. See http://www.computer-engineering.org/ps2keyboard/ for a pretty much comprehensive guide to doing it.

What makes it hard on the pi is that ps/2 is 5v, and the pi's gpio is 3v3. So you're not going to get away without additional hardware, and at that point you're probably better off using a 3v3 ttl serial/usb converter and accessing your pi via your main pc.

Simon
Thanks for the info tufty i will look into it, i do use "3v3 ttl serial/usb converter" but theres a lot of people that do not have them.
I do have some sparkfun level converters knocking around
https://www.sparkfun.com/products/8745
So i may give it a go.
Thanks
Batteries not included, Some assembly required.

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

Re: Is Bare Metal code for me?

Sun Aug 19, 2012 8:43 pm

DexOS wrote:i do use "3v3 ttl serial/usb converter" but theres a lot of people that do not have them.
Well, in either case you're going to need extra hardware to do the conversion, at roughly the same cost. Is it worth the hassle of cutting up a keyboard and soldering (maybe, actually, especially if you want to host your Pi *in* the keyboard, but I digress) just to lose the tether to a "mothership" computer? Anyone wanting to do bare metal who doesn't already have a serial-usb converter is probably barking up the wrong tree anyway.

Personally, I have a couple of 3v3 serial-interface keyboards lying about :)

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

Re: Is Bare Metal code for me?

Sun Aug 19, 2012 8:57 pm

tufty wrote:
DexOS wrote:i do use "3v3 ttl serial/usb converter" but theres a lot of people that do not have them.
Well, in either case you're going to need extra hardware to do the conversion, at roughly the same cost. Is it worth the hassle of cutting up a keyboard and soldering (maybe, actually, especially if you want to host your Pi *in* the keyboard, but I digress) just to lose the tether to a "mothership" computer? Anyone wanting to do bare metal who doesn't already have a serial-usb converter is probably barking up the wrong tree anyway.

Personally, I have a couple of 3v3 serial-interface keyboards lying about :)
Your right, but some time the journey is part of the fun ;).
Batteries not included, Some assembly required.

tritonium
Posts: 79
Joined: Tue Jan 03, 2012 7:10 pm

Re: Is Bare Metal code for me?

Wed Aug 22, 2012 3:53 pm

Dexos - This is amazing! :D
I'm still trying to picture the current limitations to being a self controlled development system so we dont have to keep swapping SD cards back and for. :?
Youve solved the output to screen. :)
Youve solved reading writing to GPI/O. ;)
Keyboard input (solved but needs extra hardware at the moment - thats fine as far as I'm concerned - :|
"DexOS wrote: Also note the chips then selfs are not that expensive, so i was going to have some boards made up and sell them at about £5 each, with some other useful goodies on board."
- I have a fiver sitting burning a hole.. let me know when they are available. :lol:
Since I understand a PS2 keyboard is serial, I'd have thought a couple of resistors to level change might be all thats needed (unless the logic is inverted when a transistor may be needed) to connect to uart. :?:
Then reading/writing to files on SD card...
I know your code boots from SD, but after your OS is running are you still able to talk to the SD card? - I'm not sure - I see there is a load command for bit images.
USB not neccessary for me - as long as I can switch the uart (there's only one) between devices - that should be easy using I/O pins to select/switch between streams.
What else?
I think thats it.
Do you have more macros hiding that you havent released yet? as you said you had a CLI working? ;)

mmm I'm getting quite excited.... :P
Dave (yea - too many smilies....) :shock:

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

Re: Is Bare Metal code for me?

Wed Aug 22, 2012 5:45 pm

Thanks tritonium, I will try to answer some of your ?.
The read/writing to sd card is not a problem (not got round to coding it), but theres enough info, the main bit of the work will be writing a fat driver.
I could also write some code, so you could up load new apps via Uart, but i did not need it so did not code it, but a number of people have asked, so maybe i will add that functionality.
Yes i have a lot of functions i have not posted yet, and yes i have coded basic cli and gui that work from input from Uart.
I have also done some work with bare metal controlling the pi with IR and 433MHz wireless, also stuff like step motor control.
I am working on a simple addon pcb board that will add keyboard and mouse, plus more.
So i hope to release the board and the code, as a package for low level developers.
Hope this helps.
Batteries not included, Some assembly required.

tritonium
Posts: 79
Joined: Tue Jan 03, 2012 7:10 pm

Re: Is Bare Metal code for me?

Wed Aug 22, 2012 7:09 pm

Thanks Dexos for your prompt reply.
I await developments with great interest.

Dave

Return to “Bare metal, Assembly language”