Programming Arduino in pure C

In this article we will see how to stepping away from the Arduino IDE, and Programming Arduino in pure C Language.


Using C to programming Arduino means we are able to Create smaller programs. All the tools we going to use, Fortunately Arduino IDE use them but under the hood. So Arduino IDE use arv-gcc compiler and arvdude to upload.
The Arduino IDE preferences contains verbosity options, so we can see all the commands are been used.

From C code to Arduino board with avr-gcc
avr-gcc toolchain doesn’t know the Arduino Uno board layout, it only knows the microcontroller that is mounted on it, which is the Atmega328p, so we need to read/write directly the hardware I/O registers of that particular chip, and look at its pinout to understand the pin names and where they go: everything can be found in the Atmega328p datasheet. Instead from the Arduino Uno schematics we can find out where the pins are connected, for example we can find that the LED on Pin 13 of the arduino uno is connected to the PB5 pin of the Atmega328p chip.

The AVR compiler is complemented with a C library: avr-libc, which contains useful functions and headers to access the functionalities and I/Os of AVR chips. It also makes it easy to write complete C programs without using assembly language. Inside the Atmega328p datasheet there are many examples on how to toggle IOs, and with them I prepared the following code in a file called "led.c".


On Ubuntu Please Install the following tools :
$ sudo apt-get install gcc-avr
$ sudo apt-get install avrdude
$ sudo apt-get install avr-libc
The compiler avr-gcc is able to create an ELF executable program that contain machine code and other information such as program section memory layout and debug information.
The tool to convert the ELF into IHEX in our case is avr-objcopy. Now it’s time to run the command lines that build and upload the LED blink program using avrdude.
Most of the parameters and options of the avr-gcc and the avrdude tools for the Uno board can be found in the "hardware/arduino/avr/boards.txt" file from inside the Arduino IDE installation directory, and some other information is present in the avrdude manual.

$ avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o led.o led.c
$ avr-gcc -mmcu=atmega328p led.o -o led

$ avr-objcopy -O ihex -R .eeprom led led.hex
$ avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyUSB0 -b 115200 -U flash:w:led.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "led.hex"
avrdude: input file led.hex auto detected as Intel Hex
avrdude: writing flash (176 bytes):

Writing | ################################################## | 100% 0.04s

avrdude: 176 bytes of flash written

avrdude: safemode: Fuses OK (H:00, E:00, L:00)

avrdude done.  Thank you.

1) The first command avr-gcc takes the C source file and compiles it into an object file. The options tell the compilerto optimize for code size,what is the clock frequency (it’s useful for delay functions for example) and which is the processor for which to compile code.

2) The second commands avr-gcc links the object file together with system libraries (that are linked implicitly as needed) into an ELF program.

3) The third command avr-objcopy converts the ELF program into an IHEX file.

4) The fourth command avrdude uploads the IHEX data in to the Atmega chip embedded flash, and the options tells avrdude program to communicate using the Arduino serial protocol, through a particular serial port which is the Linux device "/dev/ttyUSB0", and to use 115200bps as the data rate.

After the commands are done the code is uploaded and the led starts blinking. That went well at the first try, mostly due to the fact that the tools have good support for the Arduino.

It is possible to list the ELF program in terms of assembly instructions (disassemble the program) by using this command:
 $ avr-objdump -d led >led.lst
 It is also possible to do a backup of the Atmega328p Flash content by using this command:
avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyUSB0 -b 115200 -U flash:w:led.hex
This Article has been take from Freedom Embedded.

SLIDER

Author

OUSSAMA AMRI

Oussama Amri 21 years, security professional from Tunisia. Lover of computer science especially programming and secuirty. Follow me On :

Popular Posts

iThePro.com. Powered by Blogger.