This is a HOWTO
detailing my experiences with setting up the AVR cross-compiler tools
under Fedora 6 using the cdk4avr
suite, and subsequently under Ubuntu. These tools are available in
source form and in binary form for
the i386 architecture. If you have a 64 bit PC then you can still use
most of the tools by installing the binaries. The cdk4avr suite is
comprised of a large number of packages in a rather jumbled state,
which have to be searched out and downloaded individually and installed
in the correct order.
Installation using
DEB files under Ubuntu
When installing the deb files, the software is placed
in
/opt/cdk4avr. Note that there are no docs or man pages
provided in the basic packages and these must be accessed separately.
Obtain the following i386.deb files through the cdk4avr home page,
which has
a description and status given for each package.
-
cdk-avr-base. When you install this, it
runs a script to set the environment. Logout and back in again to
refresh the environment so that the command path is added to the PATH
variable.
-
cdk-avr-binutils. Various utilities needed
to convert file formats and including the GNU assembler and linker.
Optional
packages include:
-
cdk-avr-avrdude. Downloading and uploading
the
on-chip memories of AVR microcontrollers. It can program the
Flash and EEPROM, and program fuse and lock bits.
-
cdk-avr-tools.
This package has some useful stuff but is not essential.
- cdk-avr-uisp. Downloading and uploading
the
on-chip memories of AVR microcontrollers. It can program the
Flash and EEPROM, and program fuse and lock bits. This package is
marked as obsoleted and so may gradually lag behind avrdude in terms of
supported devices. However it has some small advantages over avrdude in
regard to access to fuse and lock bits.
- cdk-avr-gdb-doc-print.
Contains a pdf manual.
- cdk-avr-simulavr-doc-print.
Contains a pdf manual
-
cdk-avr-binutils-doc-print.
Contains a pdf manual.
-
cdk-avr-gcc-doc-print.
Contains a pdf manual.
-
cdk-avr-libc-doc-pdf. Alternatively from the avr-libc
site get the latest pdf manual
and man pages. Untar the man pages and copy man3 to
/opt/cdk4avr/man.
-
cdk-avr-avrdude-doc-man.
If the
recent avr tools from the repositories are already installed, set a
symlink to the cdk4avr binaries to a directory that has path priority,
e.g.
# ln -s /opt/cdk4avr/bin/*
/usr/local/bin/
Compilation of AVR
Programs
There is some valuable information given in the Guido
Socher article. The simplest way to compile a program is to use the
Weddington and Wunsch Makefile as described below.
Test Compilation
Test the installation with a C program from the
LinuxFocus site
avrm8ledtest.c. For an ATMega8 MCU compile and link with:
$ avr-gcc -mmcu atmega8 avrm8ledtest.c -o
avrm8ledtest.o
This will produce avrm8ledtest.o.
avr-gcc
A simple compile will perform both a compile and
link process. The -c option will perform the compilation but will not
link. This is necessary if there are additional external files to be
linked.
Linking is done at the end of the process by taking
all the object files as
inputs and using avr-gcc again, eg
$ avr-gcc -mmcu=atmega8 -o uart.o uart.c
$ avr-gcc -mmcu=atmega8 -o acquisition.o
acquisition.c
$ avr-gcc -mmcu=atmega8 -o acquisition.out
uart.o acquisition.o
avr-objcopy
In order to
produce a file suitable for uploading
to the MCU.
we need to convert it to a
hex format. This command converts the output object file into such a
format.
$ avr-objcopy -R .eeprom -O ihex avrm8ledtest.o
avrm8ledtest.hex
This removes a section .eeprom and outputs in the
ihex (Intel Hex) format.
Makefile
A sample makefile
template written by Eric B.
Weddington, J. G Wunsch, et al. can be used to compile
multiple files. This just needs to be edited to add the MCU type and
the files to be compiled. Sometimes the optimization level may need to
be changed. Commands are:
$ make
$ make clean
$ make all
$ make program
The latter will download using avrdude. A make
will compile to an object file, and create files for Flash and EPROM
memory, so that the only command needed after a make would be to upload.
In the makefile template change the following lines
for the avr-gcc compiler to suit your system:
-----------------------------------------------------------------------------------------------
# Change to whatever MCU you are using (check the avr-gcc
documentation for the appropriate names)
MCU
= atmega8
#
Output format. (can be srec, ihex, binary)
FORMAT
= ihex
#
Optimization level, can be [0, 1, 2, 3, s]. 0 turns off optimization. s
gives smallest code.
OPT
= s
#
Target file name (without extension).
TARGET
= application
#
List C source files here. (C dependencies are automatically
generated.)
SRC
= $(TARGET).c
-----------------------------------------------------------------------------------------------
Programming the Device
My recommendation for programming the device is to use
one of the many small serial programmers that are available, such as
the programmer described in
these pages and which could be built from a very small number of
readily available parts. Failing that, avrdude is quite good and is
currently being
maintained. In the absence of a commercial programmer, the best
interface to use if you have
access to a parallel port is DAPA which is perfectly
reliable. Avrdude can use some serial protocols, toggling certain
lines to provide the reset and clock signals. These are DASA (appears
to be unreliable) and DASA3 (unknown reliability). The Guido
Socher article gives information about how to make a DAPA cable. I
made one from an old Centronics printer cable that was passing by on
its way to landfill. If
you use a commercial programmer or cable, quite likely avrdude will
support it.
The command line to use for our ATMega8 and DAPA cable
is:
$ sudo avrdude -p m8 -c dapa -e -v -U
flash:w:application.hex:a
The
relevant command line
parameters are:
-
-p is a code
for the microcontroller. m8 is ATMega8. Check the man page.
-
-c is a code
for the interface. This is the parallel DAPA interface.
-
-e erase the
memory before programming.
-
-v verbose
-
-U has the
parameter memory:operation:filename:format, where memory refers to the flash memory, w
refers to write, and a refers to an autodetect format (it is a
default and can be omitted).
Note
that access to the parallel port requires root privileges, hence the sudo command. Many websites
recommend changing the privileges on the /dev/parport0 device file but with udev now generating these device
files on boot, such an operation would need to be repeated each time
the PC is booted (unless you can convince udev to do it for you).
In
the makefile template locate
the program: section and
modify it by adding the following to allow make program to work:
-----------------------------------------------------------------------------------------------
AVRDUDE_PROGRAMMER
= dapa
AVRDUDE_FLAGS
= -p $(MCU) -c $(AVRDUDE_PROGRAMMER) -v -e
AVRDUDE_WRITE_FLASH
= -U flash:w:$(TARGET).hex
program:
$(TARGET).hex $(TARGET).eep
sudo
'$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
$(AVRDUDE_WRITE_EEPROM)'
-----------------------------------------------------------------------------------------------
Fuse Bits
A
commercial or open source programmer can deal more effectively with
fuse bits than the tools described here.
These are
device configuration settings in the microcontroller that are not
affected by flash memory erases and so persist over time. They must be
deliberately set or reset with a special command. They control certain
hardware options for the microcontroller operation. Note that
carelessness in programming these has the potential to render the
device useless.
To display the settings, uisp is the best tool if it supports the
device (avrdude can do it but less transparently). If using the DAPA
programmer and the ATMega8, the following command will show the fuse
bits neatly in hex format. The output for the default settings follows:
$ sudo uisp -dprog=dapa
-dpart=atmega8 --rd_fuses
Fuse Low Byte = 0xe1
Fuse High Byte = 0xd9
Fuse Extended Byte = 0xff
Calibration Byte 0 = 0xb5 (Read Only, for more calibration bytes use
--rd_cal)
Lock Bits = 0xff
To reprogram the fuses, avrdude can be used. To change the ATMega8
clock source from the default internal 1MHz clock to an external 8MHz
crystal, we use the following command. Again, take care and double
check everything before proceeding.
$ sudo avrdude -c dapa -p m8 -U
lfuse:w:0xEF:m
Extended Fuse Problem
Having managed to destroy all my ATMega8 chips I moved on to the
cheaper and more capable ATMega88 with its brothers ATMega48 (4M Flash)
and ATMega168 (16M Flash). Here there arose a
small problem with both avrdude and uisp. The extended fuse byte in
uisp was not accessible to uisp and avrdude barfed over it, returning a
failure indication and an invalid read back after attempts to program
it (quite likely avrdude has fixed this since first writing). The
problem with uisp was that the source code did not include the
extended fuse bit.
To fix this, delete the original
uisp package. Download the uisp
latest source uisp-20050207 (if the date is later than this, then the
problem may have been fixed, but it has not changed at the latest date
of this article). Unpack into a suitable directory and
enter the src subdirectory. Edit the Avr.C file. At the top there is a
data structure for all the AVR devices. Find both the ATMega48 and
ATMega88 entries. At the end of the entries replace the AVR_M163 with
AVR_M128. Then save and return to the top directory. Execute the famous
trio:
$ ./configure
$ make
$ sudo make install
The binary is installed by default in /usr/local/bin;
either change the install location in the configure command or make
sure your path points to it. At that point uisp was able to read and
set the extended
fuse.
It appears that avrdude set the extended fuse correctly but couldn't
read it back. I have been using
cdk-avr-avrdude-5.1cvs-20060624.i586.rpm that was grabbed from some
unknown location. The version available from Sourceforge is slightly
earlier and was not able to deal with the ATMega88 at all, being unable
to read the signature bytes.
Installation using
RPMs under Fedora 6
When installing the rpms, the software is placed in
/opt/cdk4avr. Note that there are no docs or man pages
provided in the basic packages and these must be accessed separately.
Obtain the following i586.rpms through the cdk4avr home page,
which has
a description and status given for each package.
-
cdk-avr-base. When you install this, it
runs a script to set the environment. Logout and back in again to
refresh the environment so that the command path is added to the PATH
variable.
-
cdk-avr-binutils. Various utilities needed
to convert file formats and including the GNU assembler and linker.
- cdk-avr-binutils-doc-print.
Contains a pdf manual.
- cdk-avr-gcc-doc-print.
Contains a pdf manual.
- cdk-avr-libc-doc-pdf. Alternatively from the avr-libc
site get the latest pdf manual
and man pages. Untar the man pages and copy man3 to
/opt/cdk4avr/man.
- cdk-avr-gdb-doc-print.
Contains a pdf manual.
- cdk-avr-simulavr-doc-print.
Contains a pdf manual
- cdk-avr-uisp.Downloading and uploading
the
on-chip memories of AVR microcontrollers. It can program the
Flash and EEPROM, and program fuse and lock bits. This package is
marked as obsoleted and so may gradually lag behind avrdude in terms of
supported devices. However it has some small advantages over avrdude in
regard to access to fuse and lock bits.
- cdk-avr-tools.
This package has some useful stuff but is not essential. There are a
number of package dependencies that need to be tracked down. If using a
64 bit PC you will need to compile a lot of the packages to be able to
use this.
|