Thursday, July 25, 2013

Bare-Metal Programming - Sanguino!

For people who might not be knowing what is Bare-Metal programming is, I just want to brief about it. It is low level programming!. By default when we install the SDK, IDE and when we add libraries (downloaded from web) for our project, usually for all drivers including the pin configuration(GPIO), USRAT/UART, SPI or I2C etc will have the APIs to configure, writing or reading the data. While writing code for our project, as a developer we will try to use those APIs and write programs. Bare-Metal programming involves either creating our own APIs (for future use) or using the direct registers while writing program for our project.

People who bought Sanguino / Arduino development boards for practice or for hobby will get the Arduino software, FTDI driver and IDE in a CD. Programming using that IDE is not the usual C programming as we know of, it is a derived from C, and language is called Processing programming language. Some developers would like to use the open source IDEs with normal C coding. For such developers I would like to share my thoughts and steps involved in doing so.

Note: I have used the CodeBlock as the IDE and checked with Sanguino board(ATmega644p controller). This should be the same procedure for any Arduino and AVR controller boards (I have not tested for all Arduino or AVR controller boards, tested only on ATmega644p Sanguino board).

Step 1: Make sure that you have open source IDE or IDE which you are familiar with and you can change the compiler settings for build (in my case I used CodeBlock).

Step 2: Download the AVR GCC Compiler (basically these copilers will have the registers, SFR pages etc.. required for AVR controllers) and install the AVR GCC compiler.

Step 3: Now we have to setup the IDE for AVR controllers. For CodeBlock IDE, in settings, click on the compiler, in that under select compiler select "GNU AVR GCC Compilers", under compiler settings tag, for compiler flags, check the ATmega644P. Under Toolchain executables, Give your directory path where you have installed the AVR GCC Compiler, for C compiler mention the name of the exe file like below:
You can also select the optimization level, and many other settings which suits your requirement!

Step 4: Create the project, and make sure that for that project both Debug and Release builds are having the same Compiler settings. And now just give build with empty main.c file! If build goes through without any error, then compiler settings are fine. You are ready to implement your project!
Click here, for the example project! which will toggle LED connected to PortB, pin 0.

Note: In the latest version of the code::blocks and AVR compiler toolchain, the linker will be pointing to wrong libc.a static library. Which might not be needed for your project to build and execute. If linking libc.a is not required, please make sure to delete the <path to AVR compiler>\avr\lib in Settings -> Compiler -> Search directory Tab -> Linker Tab

Step 5: Now, once the code is ready with binaries (.hex files) we need to flash (download) that to our controller board. that can be done with avrdude or some app which will help in programming our controlled board. One such app is XLoader. With that you have to give the path for the .hex file, com part number and baud-rate at which your controller needs to be flashed. For ATmega644P the baud-rate for flashing serially is 38400. (Make sure that when your controller board is connected with the help of USB cable com port connection is established. If needed install FTDI drivers)

Once controller board is flashed, your program should be working. If it is simple LED blinking program attached above, you can verify that by LED toggling. With this example you have successfully written bare-metal programming! Now you can write code for your project in C with direct register access and with IDE of your own choice! :)

1 comment:

  1. Dude, Nice article ...
    In past 4 yrs back I used ICCAVR for AVR controllers compiling and flashing. But now I use AVR Studio, its good and built on Visual Studion ....

    ReplyDelete