Application Developer
Application Developer

Reputation: 149

Avr-GCC with Arduino

How can I program my Arduino in C on Ubuntu. I've heard of avr-gcc but all online tutorials seem extremely tedious and don't have options for an AVR chip with the Arduino bootloader. Can anyone help me with an easier way to install avr-gcc on Ubuntu and get started programming in C for the Arduino?

Upvotes: 8

Views: 6921

Answers (2)

easyGoing
easyGoing

Reputation: 106

If you just want to use C code with an Arduino that already has a boot loader installed. You can literally write the code in C in the Arduino IDE and compile it as usual. Sketch is effectively a bunch of header files and Macros.

Here's the blink sketch written in C:

#include <avr/io.h> //defines pins, ports etc
#include<util/delay.h> //functions for wasting time

int main (void) {
//init
DDRB |= (1<<PB5); //Data Direction Register B:
//writing a 1 to the Pin B5 bit enables output
//Event loop
  while (1) {
    PORTB = 0b00100000; //turn on 5th LED bit/pin in PORT B (Pin13 in Arduino)
    _delay_ms (1000); //wait

    PORTB = 0b00000000; //turn off all bits/pins on PB    
    _delay_ms (1000); //wait
  } //end loop
  return(0); //end program. This never happens.
}

Paste this into the IDE and try it for yourself.

If you want to move away from the Arduino to programming AVR's without a bootloader, may I recommend an excellent webcast by Elliot Williams as an introduction. - https://www.youtube.com/watch?v=ERY7d7W-6nA

Good luck and have fun :)

Upvotes: 6

I recommend the following set of command line options for compiling:

avr-gcc -c
        -std=gnu99
        -Os
        -Wall
        -ffunction-sections -fdata-sections
        -mmcu=m328p
        -DF_CPU=16000000

And for linking:

avr-gcc -Os
        -mmcu=m328p
        -ffunction-sections -fdata-sections
        -Wl,--gc-sections

Where…

  • -c means "compile to object file only, do not link"
  • -std=gnu99 means "My code conforms to C99 and I use GNU extensions"
  • -Os means "optimize for executable size rather than code speed"
  • -Wall means "turn on (almost) all warnings"
  • -ffunction-sections -fdata-sections is necessary for the -Wl,--gc-sections optimization
  • -mmcu=m328p means "the MCU part number is ATmega 328P"
  • -DF_CPU=16000000 means "the clock frequency is 16 MHz" (adjust for your actual clock frequency)
  • -Wl,--gc-sections means "tell the linker to drop unused function and data sections" (this helps reduce code size).

In order to actually compile your code, you would first issue the avr-gcc command with the "compile only flags", like this:

avr-gcc -c -std=gnu99 <etc.> MyProgram.c -o MyProgram.o

Then you would repeat this for all of your source files. Finally, you would link the resulting object files together by invoking AVR-GCC in link mode:

avr-gcc -Os <etc.> MyProgram.o SomeUtility.o -o TheExecutable.elf

This generates an ELF file, which isn't directly executable by your MCU. Thus, you'll need to extract the useful part (the raw machine code) from it in the Intel Hex format:

avr-objcopy -O ihex -R .eeprom TheExecutable.elf TheExecutable.ihex

Finally, you will need AVRdude to upload the contents of the hex file to the MCU:

avrdude -C /path/to/avrdude.conf
        -p m328p
        -c PROGRAMMER_NAME
        -b 19600
        -P PORT_NAME
        -U flash:w:TheExecutable.ihex:i

Where…

  • -C /path/to/avrdude.conf means "use this file as the configuration file"
  • -c PROGRAMMER_NAME means "I am using a programmer of type PROGRAMMER_NAME" (you will need to fill this in yourself depending on what kind of programmer you use).
  • -b 19600 is the baud rate (you may need to adjust this depending on the baud rate you set or have pre-programmed into the bootloader)
  • -P PORT_NAME means "the programmer is connected to port PORT_NAME". On Linux, it will most often be something like /dev/ttyusbN, where N is some number.
  • -U flash:w:TheExecutable.ihex:i means "write to the Flash memory the contents of TheExecutable.ihex which is in Intel Hex format".

Upvotes: 18

Related Questions