A GNU Development Environment for
the AVR Microcontroller
Rich Neswold
rneswold@earthlink.net
A GNU Development Environment for the AVR Microcontroller
by Rich Neswold
Published $Date$
Copyright © 1999, 2000, 2001, 2002 by Richard M. Neswold, Jr.
This document attempts to cover the details of the GNU Tools that are specific to the AVR family of processors.
Acknowledgements
This document tries to tie together the labors of a large group of people. Without these individuals’ efforts, we
wouldn’t have a terrific, free set of tools to develop AVR projects. We all owe thanks to:
• The GCC Team, which produced a very capable set of development tools for an amazing number of platforms and
processors.
• Denis Chertykov <denisc@overta.ru> for making the AVR-specific changes to the GNU tools.
• Denis Chertykov and Marek Michalkiewicz <marekm@linux.org.pl> for developing the standard libraries and
startup code for AVR-GCC.
• Uros Platise for developing the AVR programmer tool, uisp.
• Joerg Wunsch <joerg@FreeBSD.ORG> for adding all the AVR development tools to the FreeBSD
(http://www.freebsd.org) ports tree and for providing the demo project in Chapter 2.
• Brian Dean <bsd@bsdhome.com> for developing avrprog (an alternate to uisp) and for contributing Section
1.4.1 which describes how to use it.
Table of Contents
1. Installing the GNU Tools..........................................................................................................................................1
1.1. GNU Binutils..................................................................................................................................................1
1.2. AVR-GCC ......................................................................................................................................................2
1.2.1. Downloading the Source....................................................................................................................2
1.2.2. Building the Project...........................................................................................................................2
1.2.3. Installing the Tools.............................................................................................................................2
1.3. AVR-LIBC......................................................................................................................................................3
1.3.1. Downloading the Source....................................................................................................................3
1.3.2. Building the Libraries........................................................................................................................3
1.3.3. Installing the Libraries and Header Files...........................................................................................3
1.4. Programming Your Processor.........................................................................................................................3
1.4.1. Using avrprog...................................................................................................................................3
1.4.2. Using uisp..........................................................................................................................................5
2. Using the GNU Tools.................................................................................................................................................6
2.1. The Project .....................................................................................................................................................6
2.2. Compiling and Linking ..................................................................................................................................8
2.3. “Map” Files ..................................................................................................................................................13
2.4. Generating .hex Files..................................................................................................................................15
2.5. Letting Make Build the Project ...................................................................................................................15
3. Application Start-up...............................................................................................................................................17
4. Standard C Library................................................................................................................................................19
4.1. Function Reference ......................................................................................................................................19
4.1.1. abort...............................................................................................................................................19
4.1.2. abs...................................................................................................................................................19
4.1.3. atoi.................................................................................................................................................19
4.1.4. atol.................................................................................................................................................20
4.1.5. bsearch ..........................................................................................................................................20
4.1.6. cos...................................................................................................................................................20
4.1.7. cosh.................................................................................................................................................21
4.1.8. div...................................................................................................................................................21
4.1.9. exit.................................................................................................................................................21
4.1.10. fabs...............................................................................................................................................21
4.1.11. isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, islower, isprint,
ispunct, isspace, isupper, isxdigit .....................................................................................22
4.1.12. labs...............................................................................................................................................22
4.1.13. ldiv...............................................................................................................................................23
4.1.14. longjmp ........................................................................................................................................23
4.1.15. qsort.............................................................................................................................................23
4.1.16. setjmp ..........................................................................................................................................24
4.1.17. sin.................................................................................................................................................24
4.1.18. sinh...............................................................................................................................................24
4.1.19. sqrt...............................................................................................................................................25
4.1.20. tan.................................................................................................................................................25
4.1.21. toascii ........................................................................................................................................25
iv
4.1.22. tolower, toupper .......................................................................................................................25
5. Memory APIs ..........................................................................................................................................................27
5.1. Program Memory .........................................................................................................................................27
5.2. Function Reference ......................................................................................................................................27
5.2.1. __ATTR_CONST__, __ATTR_PROGMEM__, __ATTR_PURE__ ........................................................28
5.2.2. __elpm_inline .............................................................................................................................28
5.2.3. __lpm_inline ...............................................................................................................................28
5.2.4. memcpy_P ........................................................................................................................................29
5.2.5. PRG_RDB ..........................................................................................................................................29
5.2.6. PSTR.................................................................................................................................................29
5.2.7. strcat_P ........................................................................................................................................29
5.2.8. strcmp_P ........................................................................................................................................30
5.2.9. strcpy_P ........................................................................................................................................30
5.2.10. strcasecmp_P .............................................................................................................................30
5.2.11. strlen_P ......................................................................................................................................31
5.2.12. strncasecmp_P ...........................................................................................................................31
5.2.13. strncmp_P....................................................................................................................................31
5.2.14. strncpy_P....................................................................................................................................31
5.3. EEPROM......................................................................................................................................................32
5.4. Function Reference ......................................................................................................................................32
5.4.1. eeprom_is_ready.........................................................................................................................32
5.4.2. eeprom_rb......................................................................................................................................33
5.4.3. eeprom_read_block.....................................................................................................................33
5.4.4. eeprom_rw......................................................................................................................................33
5.4.5. eeprom_wb......................................................................................................................................33
6. Interrupt API ..........................................................................................................................................................35
6.1. Function Reference ......................................................................................................................................36
6.1.1. cli...................................................................................................................................................36
6.1.2. enable_external_int ................................................................................................................36
6.1.3. INTERRUPT......................................................................................................................................36
6.1.4. sei...................................................................................................................................................37
6.1.5. SIGNAL ............................................................................................................................................37
6.1.6. timer_enable_int.......................................................................................................................38
7. I/O API.....................................................................................................................................................................39
7.1. I/O Port APIs................................................................................................................................................39
7.2. Function Reference ......................................................................................................................................39
7.2.1. BV.....................................................................................................................................................39
7.2.2. bit_is_clear ...............................................................................................................................39
7.2.3. bit_is_set....................................................................................................................................39
7.2.4. cbi...................................................................................................................................................40
7.2.5. inp...................................................................................................................................................40
7.2.6. __inw...............................................................................................................................................40
7.2.7. __inw_atomic ...............................................................................................................................41
7.2.8. loop_until_bit_is_clear........................................................................................................41
7.2.9. loop_until_bit_is_set............................................................................................................41
7.2.10. outp...............................................................................................................................................42
7.2.11. __outw ..........................................................................................................................................42
v