avr-libc
1.8.0
Generated by Doxygen 1.7.6.1
Tue Jan 3 2012 17:01:08
CONTENTS i
Contents
1 AVR Libc 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 General information about this library . . . . . . . . . . . . . . . . . . . 1
1.3 Supported Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 avr-libc License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 Toolchain Overview 11
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 FSF and GNU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3 GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4 GNU Binutils . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.5 avr-libc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.6 Building Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.7 AVRDUDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.8 GDB / Insight / DDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.9 AVaRICE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.10 SimulAVR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.11 Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.12 Toolchain Distributions (Distros) . . . . . . . . . . . . . . . . . . . . . . 16
2.13 Open Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3 Memory Areas and Using malloc() 16
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 Internal vs. external RAM . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.3 Tunables for malloc() . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4 Implementation details . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4 Memory Sections 21
4.1 The .text Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.2 The .data Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.3 The .bss Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Generated on Tue Jan 3 2012 17:00:59 for avr-libc by Doxygen
CONTENTS ii
4.4 The .eeprom Section . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.5 The .noinit Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.6 The .initN Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.7 The .finiN Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.8 Using Sections in Assembler Code . . . . . . . . . . . . . . . . . . . . 25
4.9 Using Sections in C Code . . . . . . . . . . . . . . . . . . . . . . . . . 26
5 Data in Program Space 26
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.2 A Note On const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.3 Storing and Retrieving Data in the Program Space . . . . . . . . . . . . 27
5.4 Storing and Retrieving Strings in the Program Space . . . . . . . . . . . 29
5.5 Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6 avr-libc and assembler programs 31
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.2 Invoking the compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.3 Example program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.4 Pseudo-ops and operators . . . . . . . . . . . . . . . . . . . . . . . . 36
7 Inline Assembler Cookbook 37
7.1 GCC asm Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.2 Assembler Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.3 Input and Output Operands . . . . . . . . . . . . . . . . . . . . . . . . 41
7.4 Clobbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
7.5 Assembler Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.6 C Stub Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.7 C Names Used in Assembler Code . . . . . . . . . . . . . . . . . . . . 49
7.8 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8 How to Build a Library 50
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.2 How the Linker Works . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Generated on Tue Jan 3 2012 17:00:59 for avr-libc by Doxygen
CONTENTS iii
8.3 How to Design a Library . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.4 Creating a Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.5 Using a Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
9 Benchmarks 53
9.1 A few of libc functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
9.2 Math functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
10 Porting From IAR to AVR GCC 55
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
10.2 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
10.3 Interrupt Service Routines (ISRs) . . . . . . . . . . . . . . . . . . . . . 57
10.4 Intrinsic Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
10.5 Flash Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
10.6 Non-Returning main() . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
10.7 Locking Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
11 Frequently Asked Questions 60
11.1 FAQ Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
11.2 My program doesn’t recognize a variable updated within an interrupt
routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
11.3 I get "undefined reference to..." for functions like "sin()" . . . . . . . . . . 62
11.4 How to permanently bind a variable to a register? . . . . . . . . . . . . 62
11.5 How to modify MCUCR or WDTCR early? . . . . . . . . . . . . . . . . 63
11.6 What is all this _BV() stuff about? . . . . . . . . . . . . . . . . . . . . . 64
11.7 Can I use C++ on the AVR? . . . . . . . . . . . . . . . . . . . . . . . . 64
11.8 Shouldn’t I initialize all my variables? . . . . . . . . . . . . . . . . . . . 65
11.9 Why do some 16-bit timer registers sometimes get trashed? . . . . . . . 66
11.10How do I use a #define’d constant in an asm statement? . . . . . . . . . 67
11.11Why does the PC randomly jump around when single-stepping through
my program in avr-gdb? . . . . . . . . . . . . . . . . . . . . . . . . . . 67
11.12How do I trace an assembler file in avr-gdb? . . . . . . . . . . . . . . . 68
11.13How do I pass an IO port as a parameter to a function? . . . . . . . . . 69
Generated on Tue Jan 3 2012 17:00:59 for avr-libc by Doxygen
CONTENTS iv
11.14What registers are used by the C compiler? . . . . . . . . . . . . . . . 71
11.15How do I put an array of strings completely in ROM? . . . . . . . . . . . 73
11.16How to use external RAM? . . . . . . . . . . . . . . . . . . . . . . . . 75
11.17Which -O flag to use? . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
11.18How do I relocate code to a fixed address? . . . . . . . . . . . . . . . . 76
11.19My UART is generating nonsense! My ATmega128 keeps crashing! Port
F is completely broken! . . . . . . . . . . . . . . . . . . . . . . . . . . 77
11.20Why do all my "foo...bar" strings eat up the SRAM? . . . . . . . . . . . 77
11.21Why does the compiler compile an 8-bit operation that uses bitwise op-
erators into a 16-bit operation in assembly? . . . . . . . . . . . . . . . 79
11.22How to detect RAM memory and variable overlap problems? . . . . . . 79
11.23Is it really impossible to program the ATtinyXX in C? . . . . . . . . . . . 80
11.24What is this "clock skew detected" message? . . . . . . . . . . . . . . . 80
11.25Why are (many) interrupt flags cleared by writing a logical 1? . . . . . . 81
11.26Why have "programmed" fuses the bit value 0? . . . . . . . . . . . . . . 81
11.27Which AVR-specific assembler operators are available? . . . . . . . . . 82
11.28Why are interrupts re-enabled in the middle of writing the stack pointer? . 82
11.29Why are there five different linker scripts? . . . . . . . . . . . . . . . . 82
11.30How to add a raw binary image to linker output? . . . . . . . . . . . . . 83
11.31How do I perform a software reset of the AVR? . . . . . . . . . . . . . . 84
11.32I am using floating point math. Why is the compiled code so big? Why
does my code not work? . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.33What pitfalls exist when writing reentrant code? . . . . . . . . . . . . . 85
11.34Why are some addresses of the EEPROM corrupted (usually address
zero)? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
11.35Why is my baud rate wrong? . . . . . . . . . . . . . . . . . . . . . . . 89
11.36On a device with more than 128 KiB of flash, how to make function
pointers work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.37Why is assigning ports in a "chain" a bad idea? . . . . . . . . . . . . . 89
12 Building and Installing the GNU Tool Chain 90
12.1 Building and Installing under Linux, FreeBSD, and Others . . . . . . . . 90
12.2 Required Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Generated on Tue Jan 3 2012 17:00:59 for avr-libc by Doxygen