rvanya
Posts: 1
Joined: Thu Sep 05, 2019 2:13 pm

Clean, OS-independent Pi programming using C/C++

Thu Sep 05, 2019 2:35 pm

Hello!

I'd like to program Raspberry Pi 3/4 as a clean hardware unit, independently from any OS. I don't mean cross-platform way of independence, but programming this HW unit similar way as a simple MCU. Is this possible? Did somebody try something like this? Can somebody refer some links, tutorials, docs, etc, how to start? Is the available hardware documentation sufficient to be able to utilize the hardware units of the chipset?

My idea is: to take a C/C++ compiler, eg. CLang on a desktop PC, write the code, compile to ARM binary, upload this binary to a place on an SD card where Pi boots from and get my code executed. It would help me a lot if somebody could help with the firsts steps.

Thank you!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12217
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Clean, OS-independent Pi programming using C/C++

Thu Sep 05, 2019 2:42 pm

You are talking about "bare metal" programming.

moved this post to the bare metal and assembler section of the forum.

pik33
Posts: 182
Joined: Thu Sep 10, 2015 4:26 pm

Re: Clean, OS-independent Pi programming using C/C++

Thu Sep 05, 2019 3:22 pm

Yes, it is, but it is Pascal.

The Ultibo environment is an open source environment for programming RPi without OS and you can find a lot of hardware related information on its forum and its source files.

As it is now Ultibo became a complex environment with a lot of operating system functionality, but what is essential, all of the hardware can be used as you want and there are nothing but kernel7.img file after the compilation.

This complexity has good and bad sides. Good is you have near all hardware elements initialized and working (the exception is a Wifi chip) Bad is you need to have a CPU busy for servicing all these hardware elements, USB, etc. They needs interrupts, and threads to run. The workaround is: you can free one or more cores from this activity and having them for you.

As an example what is possible: I wrote an audio "driver" for it using the RPi "as a microcontroller" - it works by direct accessing DMA and PWM hardware registers. I am also playing with a something like GUI: it has a window system and a player which uses this audio driver to output the sound to the audio jack.

This is the drver code: https://github.com/pik33/SimpleAudio

Ultibo can also use C libraries if compiled to .a static linked file

The environment can be set up on Windows machine as a crosscompiler or on RPi/Raspbian. In this case you can develop and test on the same RPi using several scripts and code fragments (I work with Ultibo this way)

https://ultibo.org/

We are waiting for RPi4 compatible version: because of the big hardware revolution Ultibo can not compile for RPi4 yet.

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

Re: Clean, OS-independent Pi programming using C/C++

Fri Sep 06, 2019 1:08 am

The generic answer without recommendation is read the two stickies at the top of the post lists and then ask anything specific not covered.

It offers background, references and options and links to those items.

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

Re: Clean, OS-independent Pi programming using C/C++

Fri Sep 06, 2019 2:34 am

So many ways to do this now.
Asm, C, C++, Pascal, Rust all have baremetal working.

The hardware has been figured out enough on the Zero's to 3B+.
The Pi4 has a peripheral document in the editing stage?

I prefer Ultibo because I come from a C embedded micro background and like my IDE's and libs.
I think there is/was an Arduino lib for Pi's?
That might be worth chasing down if you prefer C++.

There is one advantage with Ultibo, it is based on Lazarus/Free Pascal which is based on Delphi, based on Turbo Pascal.
That means there is a bucket load of old code which sometimes just runs without issues.

I can install Lazarus/Free Pascal and Ultibo on Buster.
This allows me to test code in Raspbian before porting it to Ultibo.
Pi4 has become my home development box, it's better than Windows for that now :D

I never really grasped C++ decades ago, FPC has been a way to get my head around this Object stuff.
Besides lots of C libs are hardware dependant or dependant on other libs, that drives me nuts.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

MikeDB
Posts: 77
Joined: Sun Oct 12, 2014 8:27 am

Re: Clean, OS-independent Pi programming using C/C++

Fri Sep 13, 2019 10:53 pm

pik33 wrote:
Thu Sep 05, 2019 3:22 pm
We are waiting for RPi4 compatible version: because of the big hardware revolution Ultibo can not compile for RPi4 yet.
Is there any expected timescale for at least a testable version of Ultibo for Pi4 ?

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

Re: Clean, OS-independent Pi programming using C/C++

Sat Sep 14, 2019 2:38 am

Is there any expected timescale for at least a testable version of Ultibo for Pi4 ?
Depend on lots of things, I'm just a Ultibo user and have no inside info.

More hardware was changed on the Pi4 than any previous version.
A72, VC6, USB, Ethernet...
The BCM2835 peripheral manual was available and lots of reverse engineering had been done and documented.

I'm actually more interested in using the Pi4 as the development box, but that just could be my home Mint box died the week my first Pi4 showed up :lol:

Last night I got Nim and Zig working on Gentoo64
Both can talk to Ultibo ;)
Having a choice of languages to make OS's and even mixing them up is now possible and there will be no need to just have a C based OS.
That's freedom in diversity.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Heater
Posts: 13290
Joined: Tue Jul 17, 2012 3:02 pm

Re: Clean, OS-independent Pi programming using C/C++

Sat Sep 14, 2019 5:28 am

rvanya,

You can easily build executable binaries with the GCC C/C++ compiler or Clang/LLVM.

The problem then is getting those binaries onto the Pi and having them run.

The problem then is arranging that your programs can access the Pi peripheral devices: serial port, screen, timers, interrupts, GPIO pins etc, etc,

This is all doable and there are many examples and discussions about it on this forum.

Of course what you are actually doing then is building a lot of operating system functionality into your program, more and more as you want to do more complicated things. So have a look at some example "operating systems" that are designed to show you how to do all this:

Course - Cambridge University, Department of Computer Science and Technology
Baking Pi – Operating Systems Development
https://www.cl.cam.ac.uk/projects/raspb ... orials/os/

Building an Operating System for the Raspberry Pi
https://jsandler18.github.io/


For some reason people have offered a solution to your question in Pascal despite the fact you asked for C/C++. So I will take the liberty of suggesting something a bit more modern:

A Raspi OS in the Rust language:
https://github.com/rust-embedded/rust-r ... -tutorials

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

Re: Clean, OS-independent Pi programming using C/C++

Sat Sep 14, 2019 6:16 am

Thanks Heater, bookmarked.
Andre's Rust version has really improved since I looked at it last.

I do wish they would update that Cambridge OS tut.
Yep, we should have linked to Circle++ as that's what you asked for.
https://github.com/rsta2/circle

Peter's is anther you need to bookmark
https://github.com/PeterLemon/RaspberryPi
Hmm, some interesting ideas in DexOS.

Try them all and see which suits you.

No baremetal yet in D, which has got me interested now :D
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

MikeDB
Posts: 77
Joined: Sun Oct 12, 2014 8:27 am

Re: Clean, OS-independent Pi programming using C/C++

Sat Sep 14, 2019 9:17 am

Heater wrote:
Sat Sep 14, 2019 5:28 am
For some reason people have offered a solution to your question in Pascal despite the fact you asked for C/C++. So I will take the liberty of suggesting something a bit more modern:

A Raspi OS in the Rust language:
https://github.com/rust-embedded/rust-r ... -tutorials
I would point out that the Pascal used nowadays isn't the same as in the 1980s so is a fully up to date and viable method of programming, with the added advantage you can run Turbo Pascal 7 code if you are old enough to still have some lieing around :-)

But yes, Rust is another possibility so I'll look at that as well.

I'm trying to do Linux on two cores, bare metal on the other two of a Raspberry Pi which I thought would be straightforward but appears to be anything but. I think you were one of the people who helped me with the Linux side some time ago and that appears ok now.

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

Re: Clean, OS-independent Pi programming using C/C++

Sat Sep 14, 2019 11:03 am

There are some game libs for Rust now.
http://arewegameyet.com/
May not be suitable for baremetal yet, but fun to make games in something else besides C/C++ or Python.

Just got libkinect to compile, VRui failed, so a Pi4 AR Sandpit is still some work away.
AR Sandpit in Rust?

One of my target gadgets for Pi's is a desktop AR sandbox.
OpenGL works, Kinect may work, now it is just some software :lol:
Eventually it will be a baremetal version, mainly because I hate maintaining those Linux AR Sandboxes we have around the place.
Don't really care what language it is written in as long as it works.
Single use application gadgets like an AR Sandbox don't need Linux.

Rats, Plantex need OpenGL4.0, but at least it compiled.
http://arewegameyet.com/#games
First time I threw something this big at Rust.
Nice Cargo and reasonably fast compile on a PI4.
Just need to config for OpenGL3.0 and try again?

I looked at Rust years ago but the Cargo lib system was a real pain behind the Uni firewall.
Which is why most of my Pi stuff has been self hosted or built around included libraries.

Will Rust talk directly to the VC4 OpenGLES on the older Pi's?
We can forget accelerated VC6 baremetal for some time on Pi4's.

Totally off topic, is there a Rust forum?
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Heater
Posts: 13290
Joined: Tue Jul 17, 2012 3:02 pm

Re: Clean, OS-independent Pi programming using C/C++

Sat Sep 14, 2019 1:45 pm

No, it's the same Pascal that a core component of our company's product is written in. Drives me nuts. Luckily others in the company can deal with it.

There is a Rust language discussion forum here: https://users.rust-lang.org/ People there have been very helpful. I suspect they prefer short, sweet, specific questions rather than random chit chat and brain dumps.

Return to “Bare metal, Assembly language”