A freshly installed Linux will have the GCC toolchain to compile C and C++ programs for its own processor. It won't have the suite of software tools to compile programs for the Atmel AVR processors until you install them.
Open a command terminal and try: $ which gcc
Then try $ which avr-gcc
The screen should show the location of the gcc toolchain program files. Expect nothing if avr-gcc has not been installed. If this computer has the Arduino IDE installed or MPLab X IDE from microchip then a version of avr-gcc will be found.
To install all of the needed programs open a terminal and type the following:
$ sudo apt-get install make binutils gcc-avr avr-libc uisp avrdude flex byacc bison
Packages already installed and up to date will just be skipped. When finished you can compile programs for your Arduino with gcc-avr. Standard libraries are installed with avr-libc. Avrdude will upload and download from the flash of your Arduino board and the makefile utility will run.
Now, let's create a folder called code and put two text files into it, main.c and Makefile.
Use a text editor to enter main.c
#include <avr/io.h>
int main(void){ DDRB = 0xFF;
while(1){PORTB++;
int i; for (i=0; i < 0x7FFF; i++){;}}}
Save and use the text editor to enter the Makefile
MCU=atmega328p
F_CPU=1200000
CC=avr-gcc
OBJCOPY=avr-objcopy
CFLAGS=-std=c99 -Wall -g -Os -mmcu=${MCU} -DF_CPU=${F_CPU} -I.
TARGET=main
SRCS=main.c
all:
${CC} ${CFLAGS} -o ${TARGET}.bin ${SRCS}
${OBJCOPY} -j .text -j .data -O ihex ${TARGET}.bin ${TARGET}.hex
flash:
avrdude -p ${MCU} -c arduino -U flash:w:${TARGET}.hex:i -F -P /dev/ttyACM0
clean:
rm -f *.bin *.hex
Return to the terminal and make sure you are in the code directory. Connect the Arduino Uno to a USB port and type $ ls /dev/tty* to make sure the device is on port /dev/ttyACM0. If it is on another port you will have to modify the makefile.
Type $ make you generate two executable binary files: main.bin then main.hex.
Main.hex needs to be uploaded into the development board with the avrdude program to run. $ make clean will remove the two files.
Type $ make all will do the same thing as $ make.
Type $ make flash and you will see progress as the hex file is uploaded to the Uno board. Tx and Rr LEDs will blink on the board as the file uploads.
The program in main.c will make the built in LED on the Uno board at Arduino pin 13 (Port B, 5th bit) about once a second. The Makefile program in this posting is very simple and works to generate a main.hex file from the main.c program.
This exercise can be done on a windows computer, as well. You will have to install the avr-gcc toolchain and the makefile utility. The Port used by avrdude will need to be changed to the windows port, COM4 for example.
Comments