I tried Bare Metal Programming!

I tried Bare Metal Programming!

First of all, let's not get carried away here, but only a few years ago I could just read of concepts and do nothing about them. This was due to lack of resources & also maybe due to a little naivety. But a lot has changed since!

In this article, I show you the easiest of easy in terms of Bare metal & later on I'd could try more of complex projects. I can remember my first project when learning with Arduino being the blinking of LEDs ,both external & the ones on the board, well ;this might sound cringe but yeah, we'll be blinking still, but this time in a more sophisticated way, in a bare metal way, so -Let's Go!

#Basic concepts

Bare metal Programming, as the name suggests, has to do with manipulating the microcontroller (hardware) directly without using an Operating System; i.e no abstraction whatsoever.

With the Arduino, this means we compile code and upload it directly to the board without the IDE.

For the requirements ,You'll only need your Arduino board alongside Usb cable, and maybe LEDs depending on how many you plan to blink.

#atmega schematic

Looking closer into the ATmega 328p(the brain of the Arduino) micro-controller; It has different 8-bit registers e.g-DDR(B/D/C), PORT(B/D/C), PIN(B/D/C) which control how a port/pin works, either as an input or an output.

From the reference design below, we can see the IOH (digital) block of pins are classified into PORTB register,so setting a 1 on our 5th bit sets the pin to HIGH.

We then need to use the DDR(Data Direction Register B) to classify it as an OUTPUT(for inputs use PIN)

Arduino Reference Design

#setting&clearing bits

Now, for our simple project, we'll be blinking the inbuilt LED on the Arduino-which we can execute by simply setting and clearing bits of the data register(we'll see this later on).

From C & Digital electronics; for us to manipulate bits, we have to perform bitwise and shifting operations, enabled by operators such as AND(&), OR(|) ,Left shift(<<),Right shift(>>) for example ;

In our case we'll use left shift to set 1 and an OR operator as it outputs always outputs a 1. i.e;

And to clear bit(turn off LED), we'll do the same but instead use an AND operator as it always gives a 0 output .

We'll therefore apply the above into our project.

#main file

A short explanation of the code below;

1.We first define the input-output libraries as well as delay .

2.We initialize LED 1 and 2 to correspond to digital pin 5 & 4 in the ATmega328P chip configuration

3. As mentioned earlier, for OUTPUTS, we use the DDR register hence;

4. We use while loop for indefinite blinking ; then set 1 to the PORTB register which turns LED 1 high and LED2 low, we use '|=' to set the bit and '&=~' operator to clear the bit.

For blinking to be a little dramatic , we then do the exact opposite ;

After all is said and done, it's time to upload our code, so connect your board!

But first we need to understand how the IDE used by Arduino works;

  • The code is compiled by AVR-gcc compiler to obtain a Binary file

  • AVR objcopy tool is used to convert the binary to an uploadable HEX file

  • An AVRDUDE program is then used to upload the HEX file to your board.

*for the compilation, I used ubuntu to run the commands above. check more here on installation of tools

Check out the short demo video of the blinking below:

https://youtu.be/6aZ_lJaCKCs

CONCLUSION

I really enjoyed doing this simple project as I got a chance to go back to my digital electronics ,C & Arduino knowhow.

There's so much behind the IDE curtain and only the few get to discover it. And while I did this purely for fun & learning , you can be sure to ditch the IDE when you need complete control over your hardware ,all while saving time and reducing dependencies.

So, the next step would be to try even more intriguing and challenging projects in the near future.

Till next time!

To view or add a comment, sign in

Explore topics