3 Programming ATMEGA components with the Raspberry - PolytechAngersMecatroniqueClub/Tutorials GitHub Wiki

Programming ATMEGA components with the Raspberry

Installing avrdude

We want to write a program on the Raspberry, compile it and load it in the ATMEGA micro controller. Doing that is called cross-compilation: we want to compile a program that will not be executed in the host (computer where we do the compilation) but on a target device. To be able to cross compile for ATMEGA micro controller, we need to install the avrdude tool. Do it with the command

$ sudo apt-get update
$ sudo apt-get install avrdude avr-libc

This should install avrdude and the tools that will be used to compile and load the program.

Using a makefile

It is possible to do the compilation, loading steps by hand each time you want to update a program on the micro controller, writing commands like

$ avrdude -c avrispmkii -p m32m1 -P usb -U flash::w:output
$ avr-g++ -c -W -Wall -Werror -mmcu=atmega32m1 -Os -I src/ -DF_CPU=16000000UL -std=c++1 src/pin.cpp -o src/pin.o
...

… But it is quite painful, and it is easier (and possible) to automate most of it. To do that, we will do a makefile. A makefile is a file named makefile that contains instruction for the Terminal and can be executed with the command make.

My first makefile

Lets make a really simple makefile just to illustrate. Create a new folder named test1 on the Desktop, and inside this folder create a new file named makefile. Edit this file using gedit and write the following:

all:clean
	ifconfig > ifconfig.txt
clean:
	rm -f ifconfig.txt

Warning: before ifconfig and rm you need to have tabulations, not spaces!!!

Lets try this makefile by opening a Terminal in the test1 folder (F4) and doing the command make :

$ make
rm -f ifconfig.txt
ifconfig > ifconfig.txt

If everything went well you should now have a ifconfig.txt file next to the makefile file with the network configuration written inside. What happened??

By doing make, we said that we wanted to “execute” the “all” instructions. But before doing the “all” instruction, we need to do the “clean” instruction (rm… ). This command line remove the ifconfig.txt file if it exists. Once the “clean” instruction is done, we can do the “all” instruction (ifconfig > ifconfig.txt) that write the result of the ifconfig command into a text file named ifconfig.txt. It creates the file if it does not exist.

A generic makefile for the ATMEGA programming

The makefile we want to use here will be more complicated than the previous as the instructions to automate are more complicated.

Organizing the files

For the following makefile to do the job the program files must be organized as follow:

PROJECT_FOLDER/                 (folder)
code/                           (folder)
    main.cpp                    (file)
    include/                    (folder)
    *.h                         (file)
    *.cpp                       (file)
    Makefile                    (file)
documentation/                  (folder)
    doxygen_configuration.txt   (file)
    logo.png                    (file)
    documentation.html          (file)

Basically, all the source code should be in a “code” named folder, and the Makefile file should also be in that folder. The main file of the project must be named main.cpp or main.c and the rest of the source code should be in a “include” folder next to it.

The documentation folder is needed for the documentation generation using oxygen software, detailed later.

The Makefile file

The Makefile source code to compile the code can be find in some repositories of the Polytech Angers Mecatronique Club organization gitHub https://github.com/PolytechAngersMecatroniqueClub . Note that this is for programming an ATMEGA32M1 micro controller using a AVR-ISP-MK2 programmer. If you do not have the same configuration you may have to change few things. Note also that for a Makefile, tabulations must be tabulations and not spaces!

# Parameters for compiling

TARGET = output

FOLDER_NAME = include

F_CPU = 16000000UL

SRC = $(wildcard $(FOLDER_NAME)/*.cpp *.cpp)
INC = -I $(FOLDER_NAME)/
OBJ = $(SRC:.cpp=.o)

# Default compiler and programmer
CC = avr-g++
AVRDUDE = avrdude
OBJCOPY=avr-objcopy
# Device name (compiler and programmer)
MCU = atmega32m1
AVRDUDE_MCU = m32m1
AVRDUDE_PROG = avrispmkii

# for the documentation
DOCDIR = ../documentation
DOCFILE = doxygen_configuration.txt

AVRDUDEFLAGS = -P usb
# Default compiler and linker flags
CFLAGS = -c -W -Wall -Werror -mmcu=$(MCU) -Os $(INC) -DF_CPU=$(F_CPU) -std=c++11
LDFLAGS =

all: hex upload clean

documentation: $(DOCDIR)/$(DOCFILE)
	rm -rf $(DOCDIR)/html
	doxygen $(DOCDIR)/doxygen_configuration.txt

hex: $(TARGET).hex

# Create object files
%.o : %.cpp
	$(CC) $(CFLAGS) $^ -o $@

$(TARGET).hex: $(OBJ)
	$(CC) $(LDFLAGS) -mmcu=$(MCU) $^ -o $@

clean:
	rm $(OBJ)
	rm $(TARGET).hex

# Upload hex file in the target
upload:
	$(AVRDUDE) -c $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex

flash:
	$(AVRDUDE) -c $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) $(AVRDUDEFLAGS) -U lfuse:w:0xEE:m

# .PHONY => force the update
.PHONY: clean all upload documentation

Using the makefile

For the following, you should have downloaded a valid makefile in the gitHub repository. By using the command :

$ make

you do three things: you compile the program and generate an output.hex file for the micro controller, you load the output.hex file into the micro controller, and you remove all the files generated during the compilation, including the output.hex file.

If do not want to load the file but just generate it, you can do:

$ make hex

If the ATMEGA32M1 was never programmed before, you may need to flash it. This can be done with the command

$ make flash

Finally, if doxygen is installed you can use it to generate a documentation of your program with the commands

$ make documentation

All those commands do the job if the Makefile file is the one detailed above and if the files are well organized as presented before!

Not working with IstiaBot boards

If the programming method does not work well with the IstiaBot boards. First check if the compilation is OK with “make hex”. If not, your program needs to be checked. If the output.hex file is well generated but can not be loaded:

  • Try to flash the fuse with “make flash” and do the loading step again
  • Check the wiring of the programmer. Be careful where is the ground located on the board! The board must be powered while being programmed (with the CAN connector for instance)

Using doxygen to generate documentation

Doxygen is a software that can generate an html documentation based on the comments in your source codes. To install doxygen, use the commands

$ sudo apt-get update
$ sudo apt-get install doxygen

and also install graphviz to generate graph for your documentation

$ sudo apt-get install graphviz

To use doxygen with the previous Makefile, you need

  • to have the doxygen_configuration.txt file detailed in the appendix. In this file, you can change the PROJECT_NAME entry to match your project name
  • to have a logo.png image

Note that this will generate all the html document inside a html/ file. To ease the use of the documentation, you can create a documentation.html file with only the following content:

<meta http-equiv="REFRESH" content="0;URL=html/index.html">

To access the document you thus just have to open the documentation.html file with your favorite web explorer.

Be careful to follow the file organization presented above.