g7tnz
Posts: 9
Joined: Thu Dec 01, 2011 5:33 pm
Contact: Website

Re: [Guide] Beginning Bare Metal on Raspi

Sun Aug 05, 2012 5:42 pm

Thanks again. I am looking at it now. It certainly looks like it is better than what I had, and probably is what I am needing.

romell
Posts: 25
Joined: Mon Jul 23, 2012 6:57 pm

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 12:13 am

Wow, I just found a really strange compiler behavior that I thought might be good to share with anyone starting out in bare metal...

I have a global variable that I initialize to 0, e.g. uint32_t count = 0;
The thing is, when I tested it, the value was always random. So i thought there might be something wrong with initialized data in general, but all other variables worked just fine, and any other value worked fine for the "count" variable. Strange right?

After some examination of the object file, it turned out that the compiler was being "smart" and placed the the variable in the .bss section (uninitialized data) instead of the .data section. This small optimization works on most systems since there is some standard that dictates that everything in the .bss section should be set to zero when the binary is loaded, which is not the case on this system.

So kids, do not initialize your variables to 0 :)

Btw, does anyone know how to turn off this behavior in gcc? I already run with -O0, so there's nothing more to do there...

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1359
Joined: Sat Sep 10, 2011 11:43 am

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 5:58 am

Cycl0ne wrote:
Problem is only: Ethernet works, Mass Storage works, Mouse might work, Keyboard hangs ;-) So someone has to look into it.

From my quick look through the code it seems that the driver doesn't do split transactions so it won't work across a hub, at least that is assuming that you are running the usb at high speed, if you limit it to low speed then it might be possible to do it but you also need to implement periodic transfers as well...

I'd guess the problem is it is limited to bulk access only...

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

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

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 6:32 am

romell wrote:After some examination of the object file, it turned out that the compiler was being "smart" and placed the the variable in the .bss section (uninitialized data) instead of the .data section. This small optimization works on most systems since there is some standard that dictates that everything in the .bss section should be set to zero when the binary is loaded, which is not the case on this system.
The compiler's doing what it should. Initialising BSS to 0 is one of the first things you should be doing on boot.

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

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 7:36 am

gsh wrote:
Cycl0ne wrote:
Problem is only: Ethernet works, Mass Storage works, Mouse might work, Keyboard hangs ;-) So someone has to look into it.

From my quick look through the code it seems that the driver doesn't do split transactions so it won't work across a hub, at least that is assuming that you are running the usb at high speed, if you limit it to low speed then it might be possible to do it but you also need to implement periodic transfers as well...

I'd guess the problem is it is limited to bulk access only...

Gordon
But the mass storage works and goes also through the hub.

romell
Posts: 25
Joined: Mon Jul 23, 2012 6:57 pm

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 9:22 am

tufty wrote: The compiler's doing what it should. Initialising BSS to 0 is one of the first things you should be doing on boot.
Yes, I know. I'm not saying the compiler is wrong, just that when explicitly initializing it in the code, I don't expect it to end up in BSS in the first place...

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

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 10:33 am

romell wrote:
tufty wrote: The compiler's doing what it should. Initialising BSS to 0 is one of the first things you should be doing on boot.
Yes, I know. I'm not saying the compiler is wrong, just that when explicitly initializing it in the code, I don't expect it to end up in BSS in the first place...
I accomplish this by not using global variables. i thing global variables have nothing to do in a "kernel". think of putting your kernel into a flash. there you also cant use global variables, because its read only ;-)

romell
Posts: 25
Joined: Mon Jul 23, 2012 6:57 pm

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 12:59 pm

Cycl0ne wrote:
romell wrote:
tufty wrote:...
...
I accomplish this by not using global variables. i thing global variables have nothing to do in a "kernel". think of putting your kernel into a flash. there you also cant use global variables, because its read only ;-)
I absolutely agree and I normally don't use them. That's probably why I didn't discover this sooner. I just needed this variable for debugging purposes since I broke my memory manager.. :P

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

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 1:17 pm

You should let the compiler do what it wants to do, its the master not you, it knows best :roll: .
Batteries not included, Some assembly required.

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

Re: [Guide] Beginning Bare Metal on Raspi

Mon Aug 06, 2012 8:43 pm

DexOS wrote:You should let the compiler do what it wants to do, its the master not you, it knows best :roll: .
Hey Sheldon, if you do sarcasm, you shouldnt forget your "Bazinga" :D

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

Re: [Guide] Beginning Bare Metal on Raspi

Tue Aug 07, 2012 8:42 pm

Cycl0ne wrote:
DexOS wrote:You should let the compiler do what it wants to do, its the master not you, it knows best :roll: .
Hey Sheldon, if you do sarcasm, you shouldnt forget your "Bazinga" :D
HAHAHAHAHAHA
Batteries not included, Some assembly required.

andyreal
Posts: 5
Joined: Tue Aug 21, 2012 12:44 am
Location: London
Contact: Website

Re: [Guide] Beginning Bare Metal on Raspi

Fri Aug 24, 2012 4:12 pm

Thanks for all the valuable information ...
Some useful references if you haven't come across them already ...
Arm System Developer's Guide by Sloss, Symes and Wright (not the most up to date but
still probably the best ...)
ARM Assembly Language - Fundamentals and Techniques by William Hohl ...
pretty up to date ...
If you want a nice bare metal project (that's also useful) how about porting Ed Sutter's uMon to the RaspberryPi ?
[ The standard reference is Embedded Systems Firmware Demystyfied by Ed Sutter (would you believe ?)

Return to “Bare metal, Assembly language”