Setting Up AVR-GCC toolchain and avrdude To Program An AVR Development Board In UBUNTU
Contemporarily we have technologies like IoT or the Internet of Things trending everywhere. And the embedded systems field forms the backbone of IoT technologies. IoT means interconnection between various devices presents all over the globe. And it is the embedded system that plays a vital role in IoT, by connecting the physical world through sensors and other hardware devices, with the cloud.
So what are embedded systems?
Ever wondered what kind of system drives devices like printers, coffee machine or washing machine? What is the technology behind exciting products like drones and humanoids? What kind of system will provide home automation and fitness tracking gadgets?It is none other than the embedded systems field that works on all these fascinating and enthralling domains. Embedded systems basically deal with microcontrollers and microprocessors and using them to interface hardware peripherals like sensors, LEDs, LCDs and plethora of other components.
Sounds interesting? Great!
This blog post is a stepping stone to the world of embedded systems. In this blog post, we will be discussing how to start working with an AVR development board in UBUNTU. We will be discussing the required hardware and software as well eventually write programs to interface some basic peripherals.
The hardware requirements:
- AVR development board: In my case, I have used the Explore Embedded’s Explore starter AVR.
Here is a link for your reference. And it is always advisable to beginners to start with a development board so that they can focus more on the programming part.
2. A breadboard.
3. Jumper wires.
4. A bunch of LEDs and resistors.
5. A laptop to program.
So after you have gathered your hardware equipment let us move on to the software tools needed.
The list goes as follows:
- binutils : for getting tools like assembler, linker
- gcc-avr : a GNU C cross-compiler for specifically for AVR
- avr-libc: it is a package for AVR C library
- avrdude: it is a utility that transfers codes from the UBUNTU to the microcontroller. Here we will be using the CLI tool of avrdude.
- Other packages like uisp, flex, bison, and byacc.
- Your favorite IDE. We have already covered some text editors for programming here.
Installing the packages:
The installation of all the packages is really simple and straightforward. Just shoot out your terminal (ctrl + alt + T) and type the following code: sudo apt-get install binutils gcc-avr avr-libc uisp avrdude flex byacc bison Once the installation is done, you are ready with the required avr toolchain.But before we start programming and interfacing hardware peripherals and do cool things we have a couple of things left to be done.
Script to convert the .c code to .hex code
So we humans will be writing the codes for the microcontroller in C language. But the microcontroller can not understand C language. So we need to convert the C language code to hex codes. And this step is also pretty simple.At first, let us write a basic embedded C code that turns on and off a particular LED light attached at a particular port.
/*=====================================================
| TASK: TOGGLING THE LED ATTACHED TO A PARTICULAR PORT
| DIFFICULTY LEVEL: VERY BASIC
| Author: BISWARUP BANERJEE
| Language: EMBEDDED C
| To Compile: RUN THE SCRIPT AS MENTIONED BY THE AUTHOR
|
| Date: June 2017
|
================================================*/
#ifndef F_CPU
#define F_CPU 16000000UL // or whatever may be your frequency
#endif
#include <avr/io.h> // adding header files
#include <util/delay.h> // for _delay_ms()
int main(void)
{
DDRC = 0x01; // setting DDR of PORT C
while(1)
{
// LED on
PORTC = 0b00000001; // PC0 = High = LED attached on PC0 is ON
_delay_ms(500); // wait 500 milliseconds
//LED off
PORTC = 0b00000000; // PC0 = Low = LED attached on PC0 is OFF
_delay_ms(500); // wait 500 milliseconds
}
}
Now create a directory anywhere on your computer and save the code as led.c. After saving we will be having a .C code.
Now comes the toolchain that we installed. The installed toolchain will help us to compile the code as well as convert the C code to hex code.
To simplify the process I will suggest creating a script.
The first step is to come to the dir where the led.c is saved. Then type the following command to create a script: sudo nano compile_script This will open the nano editor and in that type the following script:
avr-gcc -g -Os -mmcu=“microcontroller” -c “filename”.c<br>avr-gcc -g -mmcu=“microcontroller” -o “filename”.elf “filename”.o<br>avr-objcopy -j .text -j .data -O ihex “filename”.elf “filename”.hex<br>avr-size --format=avr --mcu=“microcontroller” “filename”.elf Here the filename that we want to compile is: led.c and the microcontroller is: atmega32So the code becomes- avr-gcc -g -Os -mmcu=atmega32 -c led.c<br>avr-gcc -g -mmcu=atmega32 -o led.elf led.o<br>avr-objcopy -j .text -j .data -O ihex led.elf led.hex<br>avr-size --format=avr --mcu=atmega32 led.elf
Now simply save the script as compile_script.To run the script, give it the necessary permission by sudo chmod 755 compile_script Now you are ready to compile the code and generate .hex files from .C file. So to compile the led.c code that we wrote previously, come to the dir where led.c is saved and in that dir run the compile_script by running the following code: sudo ./compile_script And now you are done! Your terminal window should look like the following screenshot.
Check your directory for led.o, led.elf and led.hex files.
If you find these files in the dir that means your code has been compiled. The screenshot below shows how all the files are present in the same dir.
And here is what your .hex file will look like:
You won’t understand anything of the hex code as it is for the microcontroller.
Using avrdude to transfer the hex codes from the laptop to the microcontroller
Now we have almost come to the end part of the process. We need to now only transfer the hex codes from the laptop to the microcontroller so that the microcontroller can function accordingly. Create another script by the name upload_script and within it type the following command: avrdude -c arduino -p m32 -P /dev/ttyUSB0 -b 19200 -U flash:w:led.hex Please note that the above code snippet may require certain small changes depending on your development board and kind of USB port you are using. So I best suggest referring to the avrdude documentation which can be found on the link: You can head directly to chapter 2 to get to know if at all any changes are required.
Once you have saved the script at upload_script you need to give it permission by: sudo chmod 755 upload_script After that connect the USB coming from the AVR board to the USB port of the laptop and type: sudo ./upload_script Please note that you should be in the same dir in which the led.c program and the respective .hex files are stored.You will get something like the following screenshot if avrdude has worked fine.
So if you have reached successfully till this phase, say cheers to yourself! Now get some components like a breadboard, a few jumper wires, a resistor, and a LED.
Setting up the connections of the LED with the AVR board
This is the final part of the article. The hex program is loaded into the microcontroller and now we need to connect the right hardware parts to get started. So take a jumper wire and connect the PORTC 0 of the AVR with the +ve terminal of a LED (the longer leg generally).
Then take another wire and connect the -ve terminal of the LED to the ground and also connect a resistor in the middle. That is it. Your LED should start blinking!
Conclusion
If you are interested in working further, make sure to download the official ATMEL datasheet from here. Now, this blinking a LED is the hello world program. It is just the beginning and a small peep into the embedded world.
From here it starts and it goes on to RTOS, robotics, IoT and beyond.