Avr Libc User Manual
User Manual:
Open the PDF directly: View PDF .
Page Count: 371
Download | ![]() |
Open PDF In Browser | View PDF |
avr-libc Reference Manual 1.4.6 Generated by Doxygen 1.4.7 Tue May 15 14:56:11 2007 CONTENTS i Contents 1 2 3 4 5 6 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 avr-libc Module Index 7 2.1 7 avr-libc Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . avr-libc Data Structure Index 8 3.1 8 avr-libc Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . avr-libc File Index 9 4.1 9 avr-libc File List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . avr-libc Page Index 12 5.1 12 avr-libc Related Pages . . . . . . . . . . . . . . . . . . . . . . . . . . avr-libc Module Documentation 6.1 6.2 6.3 6.4 13: Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . 13 6.1.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 13 6.1.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 13 : Character Operations . . . . . . . . . . . . . . . . . . . 14 6.2.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 14 6.2.2 Function Documentation . . . . . . . . . . . . . . . . . . . . 15 : System Errors . . . . . . . . . . . . . . . . . . . . . . . 17 6.3.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 17 6.3.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 17 : Integer Type conversions . . . . . . . . . . . . . . . . 17 6.4.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 17 6.4.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 20 6.4.3 Typedef Documentation . . . . . . . . . . . . . . . . . . . . 29 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS ii : Mathematics . . . . . . . . . . . . . . . . . . . . . . . . 29 6.5.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 29 6.5.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 30 6.5.3 Function Documentation . . . . . . . . . . . . . . . . . . . . 31 : Non-local goto . . . . . . . . . . . . . . . . . . . . . . 34 6.6.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 34 6.6.2 Function Documentation . . . . . . . . . . . . . . . . . . . . 35 : Standard Integer Types . . . . . . . . . . . . . . . . . . 36 6.7.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 36 6.7.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 39 6.7.3 Typedef Documentation . . . . . . . . . . . . . . . . . . . . 45 : Standard IO facilities . . . . . . . . . . . . . . . . . . . . 48 6.8.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 48 6.8.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 52 6.8.3 Function Documentation . . . . . . . . . . . . . . . . . . . . 55 : General utilities . . . . . . . . . . . . . . . . . . . . . . 66 6.9.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 66 6.9.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 68 6.9.3 Typedef Documentation . . . . . . . . . . . . . . . . . . . . 68 6.9.4 Function Documentation . . . . . . . . . . . . . . . . . . . . 68 6.9.5 Variable Documentation . . . . . . . . . . . . . . . . . . . . 77 6.10 : Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 6.10.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 77 6.10.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 78 6.10.3 Function Documentation . . . . . . . . . . . . . . . . . . . . 79 6.11 : Bootloader Support Utilities . . . . . . . . . . . . . . 88 6.11.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 88 6.11.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 90 6.12 : EEPROM handling . . . . . . . . . . . . . . . . . 94 6.12.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 94 6.12.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 95 6.5 6.6 6.7 6.8 6.9 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS iii 6.12.3 Function Documentation . . . . . . . . . . . . . . . . . . . . 96 6.13 : Interrupts . . . . . . . . . . . . . . . . . . . . . . 97 6.13.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 97 6.13.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 114 6.14 : Program Space Utilities . . . . . . . . . . . . . 116 6.14.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 116 6.14.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 118 6.14.3 Typedef Documentation . . . . . . . . . . . . . . . . . . . . 120 6.14.4 Function Documentation . . . . . . . . . . . . . . . . . . . . 122 6.15 : Power Reduction Management . . . . . . . . . . . . 128 6.16 Additional notes from . . . . . . . . . . . . . . . . 131 6.17 : Special function registers . . . . . . . . . . . . . . 133 6.17.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 133 6.17.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 134 6.18 : Power Management and Sleep Modes . . . . . . . . . 136 6.18.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 136 6.18.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 137 6.18.3 Function Documentation . . . . . . . . . . . . . . . . . . . . 138 6.19 : avr-libc version macros . . . . . . . . . . . . . . . 138 6.19.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 138 6.19.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 139 6.20 : Watchdog timer handling . . . . . . . . . . . . . . . . 140 6.20.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 140 6.20.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 141 6.21 : CRC Computations . . . . . . . . . . . . . . . . . . 143 6.21.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 143 6.21.2 Function Documentation . . . . . . . . . . . . . . . . . . . . 144 6.22 : Convenience functions for busy-wait delay loops . . . 147 6.22.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 147 6.22.2 Function Documentation . . . . . . . . . . . . . . . . . . . . 147 6.23 : Basic busy-wait delay loops . . . . . . . . . . 148 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS iv 6.23.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 148 6.23.2 Function Documentation . . . . . . . . . . . . . . . . . . . . 148 6.24 : Parity bit generation . . . . . . . . . . . . . . . . . . 149 6.24.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 149 6.24.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 149 6.25 : TWI bit mask definitions . . . . . . . . . . . . . . . . 150 6.25.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 150 6.25.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 151 6.26 : Deprecated items . . . . . . . . . . . . . . 154 6.26.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 154 6.26.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 155 6.26.3 Function Documentation . . . . . . . . . . . . . . . . . . . . 157 6.27 : Compatibility with IAR EWB 3.x . . . . . . . . 157 6.28 Demo projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 6.28.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 158 6.29 Combining C and assembly source files . . . . . . . . . . . . . . . . 159 6.29.1 Hardware setup . . . . . . . . . . . . . . . . . . . . . . . . . 160 6.29.2 A code walkthrough . . . . . . . . . . . . . . . . . . . . . . 160 6.29.3 The source code . . . . . . . . . . . . . . . . . . . . . . . . 162 6.30 A simple project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 6.30.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 6.30.2 The Source Code . . . . . . . . . . . . . . . . . . . . . . . . 165 6.30.3 Compiling and Linking . . . . . . . . . . . . . . . . . . . . . 166 6.30.4 Examining the Object File . . . . . . . . . . . . . . . . . . . 167 6.30.5 Linker Map Files . . . . . . . . . . . . . . . . . . . . . . . . 171 6.30.6 Generating Intel Hex Files . . . . . . . . . . . . . . . . . . . 173 6.30.7 Letting Make Build the Project . . . . . . . . . . . . . . . . . 174 6.30.8 Reference to the source code . . . . . . . . . . . . . . . . . . 176 6.31 A more sophisticated project . . . . . . . . . . . . . . . . . . . . . . 177 6.31.1 Hardware setup . . . . . . . . . . . . . . . . . . . . . . . . . 177 6.31.2 Functional overview . . . . . . . . . . . . . . . . . . . . . . 180 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS v 6.31.3 A code walkthrough . . . . . . . . . . . . . . . . . . . . . . 181 6.31.4 The source code . . . . . . . . . . . . . . . . . . . . . . . . 184 6.32 Using the standard IO facilities . . . . . . . . . . . . . . . . . . . . . 184 6.32.1 Hardware setup . . . . . . . . . . . . . . . . . . . . . . . . . 184 6.32.2 Functional overview . . . . . . . . . . . . . . . . . . . . . . 186 6.32.3 A code walkthrough . . . . . . . . . . . . . . . . . . . . . . 186 6.32.4 The source code . . . . . . . . . . . . . . . . . . . . . . . . 191 6.33 Example using the two-wire interface (TWI) . . . . . . . . . . . . . . 192 7 192 6.33.2 The TWI example project . . . . . . . . . . . . . . . . . . . 192 6.33.3 The Source Code . . . . . . . . . . . . . . . . . . . . . . . . 193 avr-libc Data Structure Documentation 197 7.1 div_t Struct Reference . . . . . . . . . . . . . . . . . . . . . . . . . 197 7.1.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 197 7.1.2 Field Documentation . . . . . . . . . . . . . . . . . . . . . . 197 ldiv_t Struct Reference . . . . . . . . . . . . . . . . . . . . . . . . . 197 7.2.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 197 7.2.2 Field Documentation . . . . . . . . . . . . . . . . . . . . . . 197 7.2 8 6.33.1 Introduction into TWI . . . . . . . . . . . . . . . . . . . . . avr-libc File Documentation 198 8.1 assert.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 198 8.1.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 198 atoi.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 198 8.2.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 198 atol.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 198 8.3.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 198 boot.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 199 8.4.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 199 8.4.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 200 crc16.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 205 8.5.1 205 8.2 8.3 8.4 8.5 Detailed Description . . . . . . . . . . . . . . . . . . . . . . Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS 8.6 vi ctype.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 205 8.6.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 205 delay.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 206 8.7.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 206 delay_basic.h File Reference . . . . . . . . . . . . . . . . . . . . . . 206 8.8.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 206 eeprom.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . 207 8.9.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 207 8.10 errno.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 208 8.10.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 208 8.11 fdevopen.c File Reference . . . . . . . . . . . . . . . . . . . . . . . 208 8.11.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 208 8.12 ffs.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 8.12.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 209 8.13 ffsl.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 209 8.13.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 209 8.14 ffsll.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 209 8.14.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 209 8.15 interrupt.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . 209 8.15.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 209 8.16 inttypes.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . 209 8.16.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 209 8.17 math.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 212 8.17.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 212 8.18 memccpy.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 213 8.18.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 213 8.19 memchr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 213 8.19.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 213 8.20 memchr_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 214 8.20.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 214 8.21 memcmp.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 214 8.7 8.8 8.9 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS vii 8.21.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 214 8.22 memcmp_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . 214 8.22.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 214 8.23 memcpy.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 214 8.23.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 214 8.24 memcpy_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . 214 8.24.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 214 8.25 memmem.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 215 8.25.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 215 8.26 memmove.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 215 8.26.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 215 8.27 memrchr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 215 8.27.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 215 8.28 memrchr_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . 215 8.28.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 215 8.29 memset.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 215 8.29.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 215 8.30 parity.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 216 8.30.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 216 8.31 pgmspace.h File Reference . . . . . . . . . . . . . . . . . . . . . . . 216 8.31.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 216 8.31.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 218 8.32 power.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 223 8.32.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 223 8.32.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 223 8.33 setjmp.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 223 8.33.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 223 8.34 sleep.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 224 8.34.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 224 8.35 stdint.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 225 8.35.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 225 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS viii 8.36 stdio.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 228 8.36.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 228 8.37 stdlib.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 230 8.37.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 230 8.38 strcasecmp.S File Reference . . . . . . . . . . . . . . . . . . . . . . 232 8.38.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 232 8.39 strcasecmp_P.S File Reference . . . . . . . . . . . . . . . . . . . . . 232 8.39.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 232 8.40 strcasestr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 232 8.40.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 232 8.41 strcat.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 232 8.41.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 232 8.42 strcat_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 233 8.42.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.43 strchr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 233 8.43.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.44 strchr_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 233 8.44.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.45 strchrnul.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 233 8.45.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.46 strchrnul_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . 233 8.46.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.47 strcmp.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 233 8.47.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.48 strcmp_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 233 8.48.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.49 strcpy.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 233 8.49.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 233 8.50 strcpy_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 234 8.50.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 234 8.51 strcspn.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 234 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS ix 8.51.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 234 8.52 strcspn_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 234 8.52.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 234 8.53 string.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 234 8.53.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 234 8.54 strlcat.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 236 8.54.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 236 8.55 strlcat_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 236 8.55.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 236 8.56 strlcpy.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 236 8.56.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 236 8.57 strlcpy_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 236 8.57.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 236 8.58 strlen.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 236 8.58.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 236 8.59 strlen_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 236 8.59.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 236 8.60 strlwr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 237 8.60.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 237 8.61 strncasecmp.S File Reference . . . . . . . . . . . . . . . . . . . . . . 237 8.61.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 237 8.62 strncasecmp_P.S File Reference . . . . . . . . . . . . . . . . . . . . 237 8.62.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 237 8.63 strncat.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 237 8.63.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 237 8.64 strncat_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 237 8.64.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 237 8.65 strncmp.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 238 8.65.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 238 8.66 strncmp_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 238 8.66.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 238 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS x 8.67 strncpy.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 238 8.67.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 238 8.68 strncpy_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 239 8.68.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 239 8.69 strnlen.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 239 8.69.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 239 8.70 strnlen_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 239 8.70.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 239 8.71 strpbrk.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 240 8.71.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 240 8.72 strpbrk_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 240 8.72.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 240 8.73 strrchr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 240 8.73.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 240 8.74 strrchr_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . 242 8.74.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.75 strrev.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 242 8.75.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.76 strsep.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 242 8.76.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.77 strsep_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 242 8.77.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.78 strspn.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 242 8.78.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.79 strspn_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 242 8.79.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.80 strstr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 242 8.80.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.81 strstr_P.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 242 8.81.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.82 strtok_r.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . 242 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS 9 xi 8.82.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.83 strupr.S File Reference . . . . . . . . . . . . . . . . . . . . . . . . . 242 8.83.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.84 twi.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 8.84.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 242 8.85 wdt.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 244 8.85.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . 244 8.85.2 Define Documentation . . . . . . . . . . . . . . . . . . . . . 244 avr-libc Page Documentation 245 9.1 Toolchain Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 9.1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 9.1.2 FSF and GNU . . . . . . . . . . . . . . . . . . . . . . . . . 245 9.1.3 GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 9.1.4 GNU Binutils . . . . . . . . . . . . . . . . . . . . . . . . . . 246 9.1.5 avr-libc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 9.1.6 Building Software . . . . . . . . . . . . . . . . . . . . . . . 248 9.1.7 AVRDUDE . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 9.1.8 GDB / Insight / DDD . . . . . . . . . . . . . . . . . . . . . . 248 9.1.9 AVaRICE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 9.1.10 SimulAVR . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 9.1.11 Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 9.1.12 Toolchain Distributions (Distros) . . . . . . . . . . . . . . . . 249 9.1.13 Open Source . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Memory Areas and Using malloc() . . . . . . . . . . . . . . . . . . . 250 9.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 9.2.2 Internal vs. external RAM . . . . . . . . . . . . . . . . . . . 251 9.2.3 Tunables for malloc() . . . . . . . . . . . . . . . . . . . . . . 252 9.2.4 Implementation details . . . . . . . . . . . . . . . . . . . . . 253 Memory Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 9.3.1 255 9.2 9.3 The .text Section . . . . . . . . . . . . . . . . . . . . . . . . Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS 9.4 9.5 9.6 9.7 xii 9.3.2 The .data Section . . . . . . . . . . . . . . . . . . . . . . . . 255 9.3.3 The .bss Section . . . . . . . . . . . . . . . . . . . . . . . . 256 9.3.4 The .eeprom Section . . . . . . . . . . . . . . . . . . . . . . 256 9.3.5 The .noinit Section . . . . . . . . . . . . . . . . . . . . . . . 256 9.3.6 The .initN Sections . . . . . . . . . . . . . . . . . . . . . . . 256 9.3.7 The .finiN Sections . . . . . . . . . . . . . . . . . . . . . . . 258 9.3.8 Using Sections in Assembler Code . . . . . . . . . . . . . . . 258 9.3.9 Using Sections in C Code . . . . . . . . . . . . . . . . . . . 259 Data in Program Space . . . . . . . . . . . . . . . . . . . . . . . . . 259 9.4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 9.4.2 A Note On const . . . . . . . . . . . . . . . . . . . . . . . . 260 9.4.3 Storing and Retrieving Data in the Program Space . . . . . . 260 9.4.4 Storing and Retrieving Strings in the Program Space . . . . . 262 9.4.5 Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 avr-libc and assembler programs . . . . . . . . . . . . . . . . . . . . 264 9.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 9.5.2 Invoking the compiler . . . . . . . . . . . . . . . . . . . . . 265 9.5.3 Example program . . . . . . . . . . . . . . . . . . . . . . . . 265 9.5.4 Pseudo-ops and operators . . . . . . . . . . . . . . . . . . . 269 Inline Assembler Cookbook . . . . . . . . . . . . . . . . . . . . . . 270 9.6.1 GCC asm Statement . . . . . . . . . . . . . . . . . . . . . . 271 9.6.2 Assembler Code . . . . . . . . . . . . . . . . . . . . . . . . 273 9.6.3 Input and Output Operands . . . . . . . . . . . . . . . . . . . 273 9.6.4 Clobbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 9.6.5 Assembler Macros . . . . . . . . . . . . . . . . . . . . . . . 279 9.6.6 C Stub Functions . . . . . . . . . . . . . . . . . . . . . . . . 280 9.6.7 C Names Used in Assembler Code . . . . . . . . . . . . . . . 281 9.6.8 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 How to Build a Library . . . . . . . . . . . . . . . . . . . . . . . . . 282 9.7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 9.7.2 How the Linker Works . . . . . . . . . . . . . . . . . . . . . 282 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS 9.8 9.9 xiii 9.7.3 How to Design a Library . . . . . . . . . . . . . . . . . . . . 283 9.7.4 Creating a Library . . . . . . . . . . . . . . . . . . . . . . . 283 9.7.5 Using a Library . . . . . . . . . . . . . . . . . . . . . . . . . 284 Porting From IAR to AVR GCC . . . . . . . . . . . . . . . . . . . . 285 9.8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 9.8.2 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 9.8.3 Interrupt Service Routines (ISRs) . . . . . . . . . . . . . . . 286 9.8.4 Intrinsic Routines . . . . . . . . . . . . . . . . . . . . . . . . 287 9.8.5 Flash Variables . . . . . . . . . . . . . . . . . . . . . . . . . 287 9.8.6 Non-Returning main() . . . . . . . . . . . . . . . . . . . . . 288 9.8.7 Locking Registers . . . . . . . . . . . . . . . . . . . . . . . 289 Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . 289 9.9.1 FAQ Index . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 9.9.2 My program doesn’t recognize a variable updated within an interrupt routine . . . . . . . . . . . . . . . . . . . . . . . . 291 9.9.3 I get "undefined reference to..." for functions like "sin()" . . . 291 9.9.4 How to permanently bind a variable to a register? . . . . . . . 291 9.9.5 How to modify MCUCR or WDTCR early? . . . . . . . . . . 292 9.9.6 What is all this _BV() stuff about? . . . . . . . . . . . . . . . 292 9.9.7 Can I use C++ on the AVR? . . . . . . . . . . . . . . . . . . 293 9.9.8 Shouldn’t I initialize all my variables? . . . . . . . . . . . . . 294 9.9.9 Why do some 16-bit timer registers sometimes get trashed? . 295 9.9.10 How do I use a #define’d constant in an asm statement? . . . . 296 9.9.11 Why does the PC randomly jump around when single-stepping through my program in avr-gdb? . . . . . . . . . . . . . . . . 296 9.9.12 How do I trace an assembler file in avr-gdb? . . . . . . . . . . 297 9.9.13 How do I pass an IO port as a parameter to a function? . . . . 298 9.9.14 What registers are used by the C compiler? . . . . . . . . . . 300 9.9.15 How do I put an array of strings completely in ROM? . . . . . 302 9.9.16 How to use external RAM? . . . . . . . . . . . . . . . . . . . 304 9.9.17 Which -O flag to use? . . . . . . . . . . . . . . . . . . . . . 304 9.9.18 How do I relocate code to a fixed address? . . . . . . . . . . . 305 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen CONTENTS xiv 9.9.19 My UART is generating nonsense! My ATmega128 keeps crashing! Port F is completely broken! . . . . . . . . . . . . . 306 9.9.20 Why do all my "foo...bar" strings eat up the SRAM? . . . . . 306 9.9.21 Why does the compiler compile an 8-bit operation that uses bitwise operators into a 16-bit operation in assembly? . . . . . 307 9.9.22 How to detect RAM memory and variable overlap problems? . 308 9.9.23 Is it really impossible to program the ATtinyXX in C? . . . . 308 9.9.24 What is this "clock skew detected" messsage? . . . . . . . . . 309 9.9.25 Why are (many) interrupt flags cleared by writing a logical 1? 309 9.9.26 Why have "programmed" fuses the bit value 0? . . . . . . . . 310 9.9.27 Which AVR-specific assembler operators are available? . . . . 310 9.9.28 Why are interrupts re-enabled in the middle of writing the stack pointer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 9.9.29 Why are there five different linker scripts? . . . . . . . . . . . 311 9.9.30 How to add a raw binary image to linker output? . . . . . . . 311 9.10 Installing the GNU Tool Chain . . . . . . . . . . . . . . . . . . . . . 312 9.10.1 Required Tools . . . . . . . . . . . . . . . . . . . . . . . . . 313 9.10.2 Optional Tools . . . . . . . . . . . . . . . . . . . . . . . . . 313 9.10.3 GNU Binutils for the AVR target . . . . . . . . . . . . . . . . 314 9.10.4 GCC for the AVR target . . . . . . . . . . . . . . . . . . . . 315 9.10.5 AVR Libc . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 9.10.6 Avrdude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 9.10.7 GDB for the AVR target . . . . . . . . . . . . . . . . . . . . 317 9.10.8 Simulavr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 9.10.9 AVaRice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 9.11 Using the GNU tools . . . . . . . . . . . . . . . . . . . . . . . . . . 318 9.11.1 Options for the C compiler avr-gcc . . . . . . . . . . . . . . . 318 9.11.2 Options for the assembler avr-as . . . . . . . . . . . . . . . . 324 9.11.3 Controlling the linker avr-ld . . . . . . . . . . . . . . . . . . 326 9.12 Using the avrdude program . . . . . . . . . . . . . . . . . . . . . . . 328 9.13 Release Numbering and Methodology . . . . . . . . . . . . . . . . . 330 9.13.1 Release Version Numbering Scheme . . . . . . . . . . . . . . 330 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 1 AVR Libc 1 1 9.13.2 Releasing AVR Libc . . . . . . . . . . . . . . . . . . . . . . 331 9.14 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 9.15 Todo List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 9.16 Deprecated List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 AVR Libc 1.1 Introduction The latest version of this document http://savannah.nongnu.org/projects/avr-libc/ is always available from The AVR Libc package provides a subset of the standard C library for Atmel AVR 8-bit RISC microcontrollers. In addition, the library provides the basic startup code needed by most applications. There is a wealth of information in this document which goes beyond simply describing the interfaces and routines provided by the library. We hope that this document provides enough information to get a new AVR developer up to speed quickly using the freely available development tools: binutils, gcc avr-libc and many others. If you find yourself stuck on a problem which this document doesn’t quite address, you may wish to post a message to the avr-gcc mailing list. Most of the developers of the AVR binutils and gcc ports in addition to the devleopers of avr-libc subscribe to the list, so you will usually be able to get your problem resolved. You can subscribe to the list at http://lists.nongnu.org/mailman/listinfo/avr-gcc-list . Before posting to the list, you might want to try reading the Frequently Asked Questions chapter of this document. Note: If you think you’ve found a bug, or have a suggestion for an improvement, either in this documentation or in the library itself, please use the bug tracker at https://savannah.nongnu.org/bugs/?group=avr-libc to ensure the issue won’t be forgotten. 1.2 General information about this library In general, it has been the goal to stick as best as possible to established standards while implementing this library. Commonly, this refers to the C library as described by the ANSI X3.159-1989 and ISO/IEC 9899:1990 ("ANSI-C") standard, as well as parts of their successor ISO/IEC 9899:1999 ("C99"). Some additions have been inspired by other standards like IEEE Std 1003.1-1988 ("POSIX.1"), while other extensions are purely AVR-specific (like the entire program-space string interface). Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 1.3 Supported Devices 2 Unless otherwise noted, functions of this library are not guarenteed to be reentrant. In particular, any functions that store local state are known to be non-reentrant, as well as functions that manipulate IO registers like the EEPROM access routines. If these functions are used within both, standard and interrupt context, undefined behaviour will result. 1.3 Supported Devices The following is a list of AVR devices currently supported by the library. Note that actual support for some newer devices depends on the ability of the compiler/assembler to support these devices at library compile-time. AT90S Type Devices: • at90s1200 [1] • at90s2313 • at90s2323 • at90s2333 • at90s2343 • at90s4414 • at90s4433 • at90s4434 • at90s8515 • at90c8534 • at90s8535 ATmega Type Devices: • atmega8 • atmega103 • atmega128 • atmega1280 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 1.3 Supported Devices • atmega1281 • atmega16 • atmega161 • atmega162 • atmega163 • atmega164p • atmega165 • atmega165p • atmega168 • atmega169 • atmega169p • atmega2560 • atmega2561 • atmega32 • atmega323 • atmega324p • atmega325 • atmega325p • atmega3250 • atmega3250p • atmega329 • atmega329p • atmega3290 • atmega3290p • atmega48 • atmega64 • atmega640 • atmega644 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 3 1.3 Supported Devices • atmega644p • atmega645 • atmega6450 • atmega649 • atmega6490 • atmega8515 • atmega8535 • atmega88 ATtiny Type Devices: • attiny11 [1] • attiny12 [1] • attiny13 • attiny15 [1] • attiny22 • attiny24 • attiny25 • attiny26 • attiny261 • attiny28 [1] • attiny2313 • attiny44 • attiny45 • attiny461 • attiny84 • attiny85 • attiny861 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 4 1.3 Supported Devices 5 Misc Devices: • at94K [2] • at76c711 [3] • at43usb320 • at43usb355 • at86rf401 • at90can32 • at90can64 • at90can128 • at90pwm1 • at90pwm2 • at90pwm3 • at90usb82 • at90usb162 • at90usb646 • at90usb647 • at90usb1286 • at90usb1287 • atmega8hva • atmega16hva • atmega406 Note: [1] Assembly only. There is no direct support for these devices to be programmed in C since they do not have a RAM based stack. Still, it could be possible to program them in C, see the FAQ for an option. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 1.4 avr-libc License 6 Note: [2] The at94K devices are a combination of FPGA and AVR microcontroller. [TRoth-2002/11/12: Not sure of the level of support for these. More information would be welcomed.] Note: [3] The at76c711 is a USB to fast serial interface bridge chip using an AVR core. 1.4 avr-libc License avr-libc can be freely used and redistributed, provided the following license conditions are met. Portions of avr-libc are Copyright (c) 1999-2007 Keith Gudger, Bjoern Haase, Steinar Haugen, Peter Jansen, Reinhard Jessich, Magnus Johansson, Artur Lipowski, Marek Michalkiewicz, Colin O’Flynn, Bob Paddock, Reiner Patommel, Michael Rickman, Theodore A. Roth, Juergen Schilling, Philip Soeberg, Anatoly Sokolov, Nils Kristian Strom, Michael Stumpf, Stefan Swanepoel, Eric B. Weddington, Joerg Wunsch, Dmitry Xmelkov, The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 2 avr-libc Module Index 7 contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2 avr-libc Module Index 2.1 avr-libc Modules Here is a list of all modules: : Diagnostics 13 : Character Operations 14 : System Errors 17 : Integer Type conversions 17 : Mathematics 29 : Non-local goto 34 : Standard Integer Types 36 : Standard IO facilities 48 : General utilities 66 : Strings 77 : Bootloader Support Utilities 88 : EEPROM handling 94 : Interrupts 97 : Program Space Utilities Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 116 3 avr-libc Data Structure Index 8 : Power Reduction Management 128 : Special function registers 133 Additional notes from 131 : Power Management and Sleep Modes 136 : avr-libc version macros 138 : Watchdog timer handling 140 : CRC Computations 143 : Convenience functions for busy-wait delay loops 147 : Basic busy-wait delay loops 148 : Parity bit generation 149 : TWI bit mask definitions 150 : Deprecated items 154 : Compatibility with IAR EWB 3.x 157 Demo projects 158 3 Combining C and assembly source files 159 A simple project 162 A more sophisticated project 177 Using the standard IO facilities 184 Example using the two-wire interface (TWI) 192 avr-libc Data Structure Index 3.1 avr-libc Data Structures Here are the data structures with brief descriptions: div_t 197 ldiv_t 197 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 4 avr-libc File Index 4 9 avr-libc File Index 4.1 avr-libc File List Here is a list of all documented files with brief descriptions: assert.h 198 atoi.S 198 atol.S 198 boot.h 199 crc16.h 205 ctype.h 205 delay.h 206 delay_basic.h 206 eeprom.h 207 errno.h 208 fdevopen.c 208 ffs.S 209 ffsl.S 209 ffsll.S 209 interrupt.h 209 inttypes.h 209 math.h 212 memccpy.S 213 memchr.S 213 memchr_P.S 214 memcmp.S 214 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 4.1 avr-libc File List 10 memcmp_P.S 214 memcpy.S 214 memcpy_P.S 214 memmem.S 215 memmove.S 215 memrchr.S 215 memrchr_P.S 215 memset.S 215 parity.h 216 pgmspace.h 216 power.h 223 setjmp.h 223 sleep.h 224 stdint.h 225 stdio.h 228 stdlib.h 230 strcasecmp.S 232 strcasecmp_P.S 232 strcasestr.S 232 strcat.S 232 strcat_P.S 233 strchr.S 233 strchr_P.S 233 strchrnul.S 233 strchrnul_P.S 233 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 4.1 avr-libc File List 11 strcmp.S 233 strcmp_P.S 233 strcpy.S 233 strcpy_P.S 234 strcspn.S 234 strcspn_P.S 234 string.h 234 strlcat.S 236 strlcat_P.S 236 strlcpy.S 236 strlcpy_P.S 236 strlen.S 236 strlen_P.S 236 strlwr.S 237 strncasecmp.S 237 strncasecmp_P.S 237 strncat.S 237 strncat_P.S 237 strncmp.S 238 strncmp_P.S 238 strncpy.S 238 strncpy_P.S 239 strnlen.S 239 strnlen_P.S 239 strpbrk.S 240 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 5 avr-libc Page Index 12 strpbrk_P.S 240 strrchr.S 240 strrchr_P.S 242 strrev.S 242 strsep.S 242 strsep_P.S 242 strspn.S 242 strspn_P.S 242 strstr.S 242 strstr_P.S 242 strtok_r.S 242 strupr.S 242 util/twi.h 242 wdt.h 244 5 avr-libc Page Index 5.1 avr-libc Related Pages Here is a list of all related documentation pages: Toolchain Overview 245 Memory Areas and Using malloc() 250 Memory Sections 255 Data in Program Space 259 avr-libc and assembler programs 264 Inline Assembler Cookbook 270 How to Build a Library 282 Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6 avr-libc Module Documentation 13 Porting From IAR to AVR GCC 285 Frequently Asked Questions 289 Installing the GNU Tool Chain 312 Using the GNU tools 318 Using the avrdude program 328 Release Numbering and Methodology 330 Acknowledgments 333 Todo List 334 Deprecated List 335 6 avr-libc Module Documentation : Diagnostics 6.1 6.1.1 Detailed Description #include This header file defines a debugging aid. As there is no standard error output stream available for many applications using this library, the generation of a printable error message is not enabled by default. These messages will only be generated if the application defines the macro __ASSERT_USE_STDERR before including the header file. By default, only abort() will be called to halt the application. Defines • #define assert(expression) 6.1.2 6.1.2.1 Define Documentation #define assert(expression) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.2 : Character Operations 14 Parameters: expression Expression to test for. The assert() macro tests the given expression and if it is false, the calling process is terminated. A diagnostic message is written to stderr and the function abort() is called, effectively terminating the program. If expression is true, the assert() macro does nothing. The assert() macro may be removed at compile time by defining NDEBUG as a macro (e.g., by using the compiler option -DNDEBUG). : Character Operations 6.2 6.2.1 Detailed Description These functions perform various operations on characters. #include Character classification routines These functions perform character classification. They return true or false status depending whether the character passed to the function falls into the function’s classification (i.e. isdigit() returns true if its argument is any value ’0’ though ’9’, inclusive.) • • • • • • • • • • • • • int isalnum (int __c) __ATTR_CONST__ int isalpha (int __c) __ATTR_CONST__ int isascii (int __c) __ATTR_CONST__ int isblank (int __c) __ATTR_CONST__ int iscntrl (int __c) __ATTR_CONST__ int isdigit (int __c) __ATTR_CONST__ int isgraph (int __c) __ATTR_CONST__ int islower (int __c) __ATTR_CONST__ int isprint (int __c) __ATTR_CONST__ int ispunct (int __c) __ATTR_CONST__ int isspace (int __c) __ATTR_CONST__ int isupper (int __c) __ATTR_CONST__ int isxdigit (int __c) __ATTR_CONST__ Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.2 : Character Operations 15 Character convertion routines If c is not an unsigned char value, or EOF, the behaviour of these functions is undefined. • int toascii (int __c) __ATTR_CONST__ • int tolower (int __c) __ATTR_CONST__ • int toupper (int __c) __ATTR_CONST__ 6.2.2 6.2.2.1 Function Documentation int isalnum (int __c) Checks for an alphanumeric character. isdigit(c)). 6.2.2.2 int isalpha (int __c) Checks for an alphabetic character. islower(c)). 6.2.2.3 It is equivalent to (isalpha(c) || It is equivalent to (isupper(c) || int isascii (int __c) Checks whether c is a 7-bit unsigned char value that fits into the ASCII character set. 6.2.2.4 int isblank (int __c) Checks for a blank character, that is, a space or a tab. 6.2.2.5 int iscntrl (int __c) Checks for a control character. 6.2.2.6 int isdigit (int __c) Checks for a digit (0 through 9). 6.2.2.7 int isgraph (int __c) Checks for any printable character except space. 6.2.2.8 int islower (int __c) Checks for a lower-case character. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.2 : Character Operations 6.2.2.9 16 int isprint (int __c) Checks for any printable character including space. 6.2.2.10 int ispunct (int __c) Checks for any printable character which is not a space or an alphanumeric character. 6.2.2.11 int isspace (int __c) Checks for white-space characters. For the avr-libc library, these are: space, formfeed (’\f’), newline (’\n’), carriage return (’\r’), horizontal tab (’\t’), and vertical tab (’\v’). 6.2.2.12 int isupper (int __c) Checks for an uppercase letter. 6.2.2.13 int isxdigit (int __c) Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F. 6.2.2.14 int toascii (int __c) Converts c to a 7-bit unsigned char value that fits into the ASCII character set, by clearing the high-order bits. Warning: Many people will be unhappy if you use this function. This function will convert accented letters into random characters. 6.2.2.15 int tolower (int __c) Converts the letter c to lower case, if possible. 6.2.2.16 int toupper (int __c) Converts the letter c to upper case, if possible. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.3 : System Errors 17 : System Errors 6.3 6.3.1 Detailed Description #include Some functions in the library set the global variable errno when an error occurs. The file, , provides symbolic names for various error codes. Warning: The errno global variable is not safe to use in a threaded or multi-task system. A race condition can occur if a task is interrupted between the call which sets error and when the task examines errno. If another task changes errno during this time, the result will be incorrect for the interrupted task. Defines • #define EDOM 33 • #define ERANGE 34 6.3.2 Define Documentation 6.3.2.1 #define EDOM 33 Domain error. 6.3.2.2 #define ERANGE 34 Range error. 6.4 6.4.1 : Integer Type conversions Detailed Description #include This header file includes the exact-width integer definitions from , and extends them with additional facilities provided by the implementation. Currently, the extensions include two additional integer types that could hold a "far" pointer (i.e. a code pointer that can address more than 64 KB), as well as standard names for all printf and scanf formatting options that are supported by the : Standard IO facilities. As the library does not support the full range of conversion Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.4 : Integer Type conversions 18 specifiers from ISO 9899:1999, only those conversions that are actually implemented will be listed here. The idea behind these conversion macros is that, for each of the types defined by , a macro will be supplied that portably allows formatting an object of that type in printf() or scanf() operations. Example: #include uint8_t smallval; int32_t longval; ... printf("The hexadecimal value of smallval is " PRIx8 ", the decimal value of longval is " PRId32 ".\n", smallval, longval); macros for printf and scanf format specifiers For C++, these are only included if __STDC_LIMIT_MACROS is defined before including . • • • • • • • • • • • • • • • • • • • • • • • • #define PRId8 "d" #define PRIdLEAST8 "d" #define PRIdFAST8 "d" #define PRIi8 "i" #define PRIiLEAST8 "i" #define PRIiFAST8 "i" #define PRId16 "d" #define PRIdLEAST16 "d" #define PRIdFAST16 "d" #define PRIi16 "i" #define PRIiLEAST16 "i" #define PRIiFAST16 "i" #define PRId32 "ld" #define PRIdLEAST32 "ld" #define PRIdFAST32 "ld" #define PRIi32 "li" #define PRIiLEAST32 "li" #define PRIiFAST32 "li" #define PRIdPTR PRId16 #define PRIiPTR PRIi16 #define PRIo8 "o" #define PRIoLEAST8 "o" #define PRIoFAST8 "o" #define PRIu8 "u" Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.4 : Integer Type conversions • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • #define PRIuLEAST8 "u" #define PRIuFAST8 "u" #define PRIx8 "x" #define PRIxLEAST8 "x" #define PRIxFAST8 "x" #define PRIX8 "X" #define PRIXLEAST8 "X" #define PRIXFAST8 "X" #define PRIo16 "o" #define PRIoLEAST16 "o" #define PRIoFAST16 "o" #define PRIu16 "u" #define PRIuLEAST16 "u" #define PRIuFAST16 "u" #define PRIx16 "x" #define PRIxLEAST16 "x" #define PRIxFAST16 "x" #define PRIX16 "X" #define PRIXLEAST16 "X" #define PRIXFAST16 "X" #define PRIo32 "lo" #define PRIoLEAST32 "lo" #define PRIoFAST32 "lo" #define PRIu32 "lu" #define PRIuLEAST32 "lu" #define PRIuFAST32 "lu" #define PRIx32 "lx" #define PRIxLEAST32 "lx" #define PRIxFAST32 "lx" #define PRIX32 "lX" #define PRIXLEAST32 "lX" #define PRIXFAST32 "lX" #define PRIoPTR PRIo16 #define PRIuPTR PRIu16 #define PRIxPTR PRIx16 #define PRIXPTR PRIX16 #define SCNd16 "d" #define SCNdLEAST16 "d" #define SCNdFAST16 "d" #define SCNi16 "i" #define SCNiLEAST16 "i" #define SCNiFAST16 "i" #define SCNd32 "ld" Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 19 6.4 : Integer Type conversions • • • • • • • • • • • • • • • • • • • • • • • • • • • • #define SCNdLEAST32 "ld" #define SCNdFAST32 "ld" #define SCNi32 "li" #define SCNiLEAST32 "li" #define SCNiFAST32 "li" #define SCNdPTR SCNd16 #define SCNiPTR SCNi16 #define SCNo16 "o" #define SCNoLEAST16 "o" #define SCNoFAST16 "o" #define SCNu16 "u" #define SCNuLEAST16 "u" #define SCNuFAST16 "u" #define SCNx16 "x" #define SCNxLEAST16 "x" #define SCNxFAST16 "x" #define SCNo32 "lo" #define SCNoLEAST32 "lo" #define SCNoFAST32 "lo" #define SCNu32 "lu" #define SCNuLEAST32 "lu" #define SCNuFAST32 "lu" #define SCNx32 "lx" #define SCNxLEAST32 "lx" #define SCNxFAST32 "lx" #define SCNoPTR SCNo16 #define SCNuPTR SCNu16 #define SCNxPTR SCNx16 Far pointers for memory access >64K • typedef int32_t int_farptr_t • typedef uint32_t uint_farptr_t 6.4.2 6.4.2.1 Define Documentation #define PRId16 "d" decimal printf format for int16_t 6.4.2.2 #define PRId32 "ld" decimal printf format for int32_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 20 6.4 : Integer Type conversions 6.4.2.3 #define PRId8 "d" decimal printf format for int8_t 6.4.2.4 #define PRIdFAST16 "d" decimal printf format for int_fast16_t 6.4.2.5 #define PRIdFAST32 "ld" decimal printf format for int_fast32_t 6.4.2.6 #define PRIdFAST8 "d" decimal printf format for int_fast8_t 6.4.2.7 #define PRIdLEAST16 "d" decimal printf format for int_least16_t 6.4.2.8 #define PRIdLEAST32 "ld" decimal printf format for int_least32_t 6.4.2.9 #define PRIdLEAST8 "d" decimal printf format for int_least8_t 6.4.2.10 #define PRIdPTR PRId16 decimal printf format for intptr_t 6.4.2.11 #define PRIi16 "i" integer printf format for int16_t 6.4.2.12 #define PRIi32 "li" integer printf format for int32_t 6.4.2.13 #define PRIi8 "i" integer printf format for int8_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 21 6.4 : Integer Type conversions 6.4.2.14 #define PRIiFAST16 "i" integer printf format for int_fast16_t 6.4.2.15 #define PRIiFAST32 "li" integer printf format for int_fast32_t 6.4.2.16 #define PRIiFAST8 "i" integer printf format for int_fast8_t 6.4.2.17 #define PRIiLEAST16 "i" integer printf format for int_least16_t 6.4.2.18 #define PRIiLEAST32 "li" integer printf format for int_least32_t 6.4.2.19 #define PRIiLEAST8 "i" integer printf format for int_least8_t 6.4.2.20 #define PRIiPTR PRIi16 integer printf format for intptr_t 6.4.2.21 #define PRIo16 "o" octal printf format for uint16_t 6.4.2.22 #define PRIo32 "lo" octal printf format for uint32_t 6.4.2.23 #define PRIo8 "o" octal printf format for uint8_t 6.4.2.24 #define PRIoFAST16 "o" octal printf format for uint_fast16_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 22 6.4 : Integer Type conversions 6.4.2.25 #define PRIoFAST32 "lo" octal printf format for uint_fast32_t 6.4.2.26 #define PRIoFAST8 "o" octal printf format for uint_fast8_t 6.4.2.27 #define PRIoLEAST16 "o" octal printf format for uint_least16_t 6.4.2.28 #define PRIoLEAST32 "lo" octal printf format for uint_least32_t 6.4.2.29 #define PRIoLEAST8 "o" octal printf format for uint_least8_t 6.4.2.30 #define PRIoPTR PRIo16 octal printf format for uintptr_t 6.4.2.31 #define PRIu16 "u" decimal printf format for uint16_t 6.4.2.32 #define PRIu32 "lu" decimal printf format for uint32_t 6.4.2.33 #define PRIu8 "u" decimal printf format for uint8_t 6.4.2.34 #define PRIuFAST16 "u" decimal printf format for uint_fast16_t 6.4.2.35 #define PRIuFAST32 "lu" decimal printf format for uint_fast32_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 23 6.4 : Integer Type conversions 6.4.2.36 #define PRIuFAST8 "u" decimal printf format for uint_fast8_t 6.4.2.37 #define PRIuLEAST16 "u" decimal printf format for uint_least16_t 6.4.2.38 #define PRIuLEAST32 "lu" decimal printf format for uint_least32_t 6.4.2.39 #define PRIuLEAST8 "u" decimal printf format for uint_least8_t 6.4.2.40 #define PRIuPTR PRIu16 decimal printf format for uintptr_t 6.4.2.41 #define PRIX16 "X" uppercase hexadecimal printf format for uint16_t 6.4.2.42 #define PRIx16 "x" hexadecimal printf format for uint16_t 6.4.2.43 #define PRIX32 "lX" uppercase hexadecimal printf format for uint32_t 6.4.2.44 #define PRIx32 "lx" hexadecimal printf format for uint32_t 6.4.2.45 #define PRIX8 "X" uppercase hexadecimal printf format for uint8_t 6.4.2.46 #define PRIx8 "x" hexadecimal printf format for uint8_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 24 6.4 : Integer Type conversions 6.4.2.47 #define PRIXFAST16 "X" uppercase hexadecimal printf format for uint_fast16_t 6.4.2.48 #define PRIxFAST16 "x" hexadecimal printf format for uint_fast16_t 6.4.2.49 #define PRIXFAST32 "lX" uppercase hexadecimal printf format for uint_fast32_t 6.4.2.50 #define PRIxFAST32 "lx" hexadecimal printf format for uint_fast32_t 6.4.2.51 #define PRIXFAST8 "X" uppercase hexadecimal printf format for uint_fast8_t 6.4.2.52 #define PRIxFAST8 "x" hexadecimal printf format for uint_fast8_t 6.4.2.53 #define PRIXLEAST16 "X" uppercase hexadecimal printf format for uint_least16_t 6.4.2.54 #define PRIxLEAST16 "x" hexadecimal printf format for uint_least16_t 6.4.2.55 #define PRIXLEAST32 "lX" uppercase hexadecimal printf format for uint_least32_t 6.4.2.56 #define PRIxLEAST32 "lx" hexadecimal printf format for uint_least32_t 6.4.2.57 #define PRIXLEAST8 "X" uppercase hexadecimal printf format for uint_least8_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 25 6.4 : Integer Type conversions 6.4.2.58 #define PRIxLEAST8 "x" hexadecimal printf format for uint_least8_t 6.4.2.59 #define PRIXPTR PRIX16 uppercase hexadecimal printf format for uintptr_t 6.4.2.60 #define PRIxPTR PRIx16 hexadecimal printf format for uintptr_t 6.4.2.61 #define SCNd16 "d" decimal scanf format for int16_t 6.4.2.62 #define SCNd32 "ld" decimal scanf format for int32_t 6.4.2.63 #define SCNdFAST16 "d" decimal scanf format for int_fast16_t 6.4.2.64 #define SCNdFAST32 "ld" decimal scanf format for int_fast32_t 6.4.2.65 #define SCNdLEAST16 "d" decimal scanf format for int_least16_t 6.4.2.66 #define SCNdLEAST32 "ld" decimal scanf format for int_least32_t 6.4.2.67 #define SCNdPTR SCNd16 decimal scanf format for intptr_t 6.4.2.68 #define SCNi16 "i" generic-integer scanf format for int16_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 26 6.4 : Integer Type conversions 6.4.2.69 #define SCNi32 "li" generic-integer scanf format for int32_t 6.4.2.70 #define SCNiFAST16 "i" generic-integer scanf format for int_fast16_t 6.4.2.71 #define SCNiFAST32 "li" generic-integer scanf format for int_fast32_t 6.4.2.72 #define SCNiLEAST16 "i" generic-integer scanf format for int_least16_t 6.4.2.73 #define SCNiLEAST32 "li" generic-integer scanf format for int_least32_t 6.4.2.74 #define SCNiPTR SCNi16 generic-integer scanf format for intptr_t 6.4.2.75 #define SCNo16 "o" octal scanf format for uint16_t 6.4.2.76 #define SCNo32 "lo" octal scanf format for uint32_t 6.4.2.77 #define SCNoFAST16 "o" octal scanf format for uint_fast16_t 6.4.2.78 #define SCNoFAST32 "lo" octal scanf format for uint_fast32_t 6.4.2.79 #define SCNoLEAST16 "o" octal scanf format for uint_least16_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 27 6.4 : Integer Type conversions 6.4.2.80 #define SCNoLEAST32 "lo" octal scanf format for uint_least32_t 6.4.2.81 #define SCNoPTR SCNo16 octal scanf format for uintptr_t 6.4.2.82 #define SCNu16 "u" decimal scanf format for uint16_t 6.4.2.83 #define SCNu32 "lu" decimal scanf format for uint32_t 6.4.2.84 #define SCNuFAST16 "u" decimal scanf format for uint_fast16_t 6.4.2.85 #define SCNuFAST32 "lu" decimal scanf format for uint_fast32_t 6.4.2.86 #define SCNuLEAST16 "u" decimal scanf format for uint_least16_t 6.4.2.87 #define SCNuLEAST32 "lu" decimal scanf format for uint_least32_t 6.4.2.88 #define SCNuPTR SCNu16 decimal scanf format for uintptr_t 6.4.2.89 #define SCNx16 "x" hexadecimal scanf format for uint16_t 6.4.2.90 #define SCNx32 "lx" hexadecimal scanf format for uint32_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 28 6.5 : Mathematics 6.4.2.91 29 #define SCNxFAST16 "x" hexadecimal scanf format for uint_fast16_t 6.4.2.92 #define SCNxFAST32 "lx" hexadecimal scanf format for uint_fast32_t 6.4.2.93 #define SCNxLEAST16 "x" hexadecimal scanf format for uint_least16_t 6.4.2.94 #define SCNxLEAST32 "lx" hexadecimal scanf format for uint_least32_t 6.4.2.95 #define SCNxPTR SCNx16 hexadecimal scanf format for uintptr_t 6.4.3 Typedef Documentation 6.4.3.1 typedef int32_t int_farptr_t signed integer type that can hold a pointer > 64 KB 6.4.3.2 typedef uint32_t uint_farptr_t unsigned integer type that can hold a pointer > 64 KB : Mathematics 6.5 6.5.1 Detailed Description #include This header file declares basic mathematics constants and functions. Note: In order to access the functions delcared herein, it is usually also required to additionally link against the library libm.a. See also the related FAQ entry. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.5 : Mathematics Defines • #define M_PI 3.141592653589793238462643 • #define M_SQRT2 1.4142135623730950488016887 Functions • • • • • • • • • • • • • • • • • • • • • • • • • double cos (double __x) __ATTR_CONST__ double fabs (double __x) __ATTR_CONST__ double fmod (double __x, double __y) __ATTR_CONST__ double modf (double __value, double ∗__iptr) double sin (double __x) __ATTR_CONST__ double sqrt (double __x) __ATTR_CONST__ double tan (double __x) __ATTR_CONST__ double floor (double __x) __ATTR_CONST__ double ceil (double __x) __ATTR_CONST__ double frexp (double __value, int ∗__exp) double ldexp (double __x, int __exp) __ATTR_CONST__ double exp (double __x) __ATTR_CONST__ double cosh (double __x) __ATTR_CONST__ double sinh (double __x) __ATTR_CONST__ double tanh (double __x) __ATTR_CONST__ double acos (double __x) __ATTR_CONST__ double asin (double __x) __ATTR_CONST__ double atan (double __x) __ATTR_CONST__ double atan2 (double __y, double __x) __ATTR_CONST__ double log (double __x) __ATTR_CONST__ double log10 (double __x) __ATTR_CONST__ double pow (double __x, double __y) __ATTR_CONST__ int isnan (double __x) __ATTR_CONST__ int isinf (double __x) __ATTR_CONST__ double square (double __x) __ATTR_CONST__ 6.5.2 6.5.2.1 Define Documentation #define M_PI 3.141592653589793238462643 The constant pi. 6.5.2.2 #define M_SQRT2 1.4142135623730950488016887 The square root of 2. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 30 6.5 : Mathematics 6.5.3 6.5.3.1 31 Function Documentation double acos (double __x) The acos() function computes the principal value of the arc cosine of x. The returned value is in the range [0, pi] radians. A domain error occurs for arguments not in the range [-1, +1]. 6.5.3.2 double asin (double __x) The asin() function computes the principal value of the arc sine of x. The returned value is in the range [0, pi] radians. A domain error occurs for arguments not in the range [-1, +1]. 6.5.3.3 double atan (double __x) The atan() function computes the principal value of the arc tangent of x. The returned value is in the range [0, pi] radians. A domain error occurs for arguments not in the range [-1, +1]. 6.5.3.4 double atan2 (double __y, double __x) The atan2() function computes the principal value of the arc tangent of y / x, using the signs of both arguments to determine the quadrant of the return value. The returned value is in the range [-pi, +pi] radians. If both x and y are zero, the global variable errno is set to EDOM. 6.5.3.5 double ceil (double __x) The ceil() function returns the smallest integral value greater than or equal to x, expressed as a floating-point number. 6.5.3.6 double cos (double __x) The cos() function returns the cosine of x, measured in radians. 6.5.3.7 double cosh (double __x) The cosh() function returns the hyperbolic cosine of x. 6.5.3.8 double exp (double __x) The exp() function returns the exponential value of x. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.5 : Mathematics 6.5.3.9 32 double fabs (double __x) The fabs() function computes the absolute value of a floating-point number x. 6.5.3.10 double floor (double __x) The floor() function returns the largest integral value less than or equal to x, expressed as a floating-point number. 6.5.3.11 double fmod (double __x, double __y) The function fmod() returns the floating-point remainder of x / y. 6.5.3.12 double frexp (double __value, int ∗ __exp) The frexp() function breaks a floating-point number into a normalized fraction and an integral power of 2. It stores the integer in the int object pointed to by exp. The frexp() function returns the value x, such that x is a double with magnitude in the interval [1/2, 1) or zero, and value equals x times 2 raised to the power ∗exp. If value is zero, both parts of the result are zero. 6.5.3.13 int isinf (double __x) The function isinf() returns 1 if the argument x is either positive or negative infinity, otherwise 0. 6.5.3.14 int isnan (double __x) The function isnan() returns 1 if the argument x represents a "not-a-number" (NaN) object, otherwise 0. 6.5.3.15 double ldexp (double __x, int __exp) The ldexp() function multiplies a floating-point number by an integral power of 2. The ldexp() function returns the value of x times 2 raised to the power exp. If the resultant value would cause an overflow, the global variable errno is set to ERANGE, and the value NaN is returned. 6.5.3.16 double log (double __x) The log() function returns the natural logarithm of argument x. If the argument is less than or equal 0, a domain error will occur. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.5 : Mathematics 6.5.3.17 33 double log10 (double __x) The log10() function returns the logarithm of argument x to base 10. If the argument is less than or equal 0, a domain error will occur. 6.5.3.18 double modf (double __value, double ∗ __iptr) The modf() function breaks the argument value into integral and fractional parts, each of which has the same sign as the argument. It stores the integral part as a double in the object pointed to by iptr. The modf() function returns the signed fractional part of value. 6.5.3.19 double pow (double __x, double __y) The function pow() returns the value of x to the exponent y. 6.5.3.20 double sin (double __x) The sin() function returns the sine of x, measured in radians. 6.5.3.21 double sinh (double __x) The sinh() function returns the hyperbolic sine of x. 6.5.3.22 double sqrt (double __x) The sqrt() function returns the non-negative square root of x. 6.5.3.23 double square (double __x) The function square() returns x ∗ x. Note: This function does not belong to the C standard definition. 6.5.3.24 double tan (double __x) The tan() function returns the tangent of x, measured in radians. 6.5.3.25 double tanh (double __x) The tanh() function returns the hyperbolic tangent of x. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.6 : Non-local goto 34 : Non-local goto 6.6 6.6.1 Detailed Description While the C language has the dreaded goto statement, it can only be used to jump to a label in the same (local) function. In order to jump directly to another (non-local) function, the C library provides the setjmp() and longjmp() functions. setjmp() and longjmp() are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. Note: setjmp() and longjmp() make programs hard to understand and maintain. If possible, an alternative should be used. longjmp() can destroy changes made to global register variables (see How to permanently bind a variable to a register?). For a very detailed discussion of setjmp()/longjmp(), see Chapter 7 of Advanced Programming in the UNIX Environment, by W. Richard Stevens. Example: #include jmp_buf env; int main (void) { if (setjmp (env)) { ... handle error ... } while (1) { ... main processing loop which calls foo() some where ... } } ... void foo (void) { ... blah, blah, blah ... if (err) { longjmp (env, 1); } } Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.6 : Non-local goto 35 Functions • int setjmp (jmp_buf __jmpb) • void longjmp (jmp_buf __jmpb, int __ret) __ATTR_NORETURN__ 6.6.2 Function Documentation 6.6.2.1 void longjmp (jmp_buf __jmpb, int __ret) Non-local jump to a saved stack context. #include longjmp() restores the environment saved by the last call of setjmp() with the corresponding __jmpb argument. After longjmp() is completed, program execution continues as if the corresponding call of setjmp() had just returned the value __ret. Note: longjmp() cannot cause 0 to be returned. If longjmp() is invoked with a second argument of 0, 1 will be returned instead. Parameters: __jmpb Information saved by a previous call to setjmp(). __ret Value to return to the caller of setjmp(). Returns: This function never returns. 6.6.2.2 int setjmp (jmp_buf __jmpb) Save stack context for non-local goto. #include setjmp() saves the stack context/environment in __jmpb for later use by longjmp(). The stack context will be invalidated if the function which called setjmp() returns. Parameters: __jmpb Variable of type jmp_buf which holds the stack information such that the environment can be restored. Returns: setjmp() returns 0 if returning directly, and non-zero when returning from longjmp() using the saved context. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.7 : Standard Integer Types 36 : Standard Integer Types 6.7 6.7.1 Detailed Description #include Use [u]intN_t if you need exactly N bits. Since these typedefs are mandated by the C99 standard, they are preferred over rolling your own typedefs. Limits of specified-width integer types C++ implementations should define these macros only when __STDC_LIMIT_MACROS is defined before is included • • • • • • • • • • • • #define INT8_MAX 0x7f #define INT8_MIN (-INT8_MAX - 1) #define UINT8_MAX (__CONCAT(INT8_MAX, U) ∗ 2U + 1U) #define INT16_MAX 0x7fff #define INT16_MIN (-INT16_MAX - 1) #define UINT16_MAX (__CONCAT(INT16_MAX, U) ∗ 2U + 1U) #define INT32_MAX 0x7fffffffL #define INT32_MIN (-INT32_MAX - 1L) #define UINT32_MAX (__CONCAT(INT32_MAX, U) ∗ 2UL + 1UL) #define INT64_MAX 0x7fffffffffffffffLL #define INT64_MIN (-INT64_MAX - 1LL) #define UINT64_MAX (__CONCAT(INT64_MAX, U) ∗ 2ULL + 1ULL) Limits of minimum-width integer types • • • • • • • • • • • • #define INT_LEAST8_MAX INT8_MAX #define INT_LEAST8_MIN INT8_MIN #define UINT_LEAST8_MAX UINT8_MAX #define INT_LEAST16_MAX INT16_MAX #define INT_LEAST16_MIN INT16_MIN #define UINT_LEAST16_MAX UINT16_MAX #define INT_LEAST32_MAX INT32_MAX #define INT_LEAST32_MIN INT32_MIN #define UINT_LEAST32_MAX UINT32_MAX #define INT_LEAST64_MAX INT64_MAX #define INT_LEAST64_MIN INT64_MIN #define UINT_LEAST64_MAX UINT64_MAX Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.7 : Standard Integer Types 37 Limits of fastest minimum-width integer types • • • • • • • • • • • • #define INT_FAST8_MAX INT8_MAX #define INT_FAST8_MIN INT8_MIN #define UINT_FAST8_MAX UINT8_MAX #define INT_FAST16_MAX INT16_MAX #define INT_FAST16_MIN INT16_MIN #define UINT_FAST16_MAX UINT16_MAX #define INT_FAST32_MAX INT32_MAX #define INT_FAST32_MIN INT32_MIN #define UINT_FAST32_MAX UINT32_MAX #define INT_FAST64_MAX INT64_MAX #define INT_FAST64_MIN INT64_MIN #define UINT_FAST64_MAX UINT64_MAX Limits of integer types capable of holding object pointers • #define INTPTR_MAX INT16_MAX • #define INTPTR_MIN INT16_MIN • #define UINTPTR_MAX UINT16_MAX Limits of greatest-width integer types • #define INTMAX_MAX INT64_MAX • #define INTMAX_MIN INT64_MIN • #define UINTMAX_MAX UINT64_MAX Limits of other integer types C++ implementations should define these macros only when __STDC_LIMIT_MACROS is defined before is included • • • • • #define PTRDIFF_MAX INT16_MAX #define PTRDIFF_MIN INT16_MIN #define SIG_ATOMIC_MAX INT8_MAX #define SIG_ATOMIC_MIN INT8_MIN #define SIZE_MAX (__CONCAT(INT16_MAX, U)) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.7 : Standard Integer Types 38 Macros for integer constants C++ implementations should define these macros only when __STDC_CONSTANT_MACROS is defined before is included. These definitions are valid for integer constants without suffix and for macros defined as integer constant without suffix • • • • • • • • • • #define INT8_C(value) ((int8_t) value) #define UINT8_C(value) ((uint8_t) __CONCAT(value, U)) #define INT16_C(value) value #define UINT16_C(value) __CONCAT(value, U) #define INT32_C(value) __CONCAT(value, L) #define UINT32_C(value) __CONCAT(value, UL) #define INT64_C(value) __CONCAT(value, LL) #define UINT64_C(value) __CONCAT(value, ULL) #define INTMAX_C(value) __CONCAT(value, LL) #define UINTMAX_C(value) __CONCAT(value, ULL) Exact-width integer types Integer types having exactly the specified width • • • • • • • • typedef signed char int8_t typedef unsigned char uint8_t typedef signed int int16_t typedef unsigned int uint16_t typedef signed long int int32_t typedef unsigned long int uint32_t typedef signed long long int int64_t typedef unsigned long long int uint64_t Integer types capable of holding object pointers These allow you to declare variables of the same size as a pointer. • typedef int16_t intptr_t • typedef uint16_t uintptr_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.7 : Standard Integer Types 39 Minimum-width integer types Integer types having at least the specified width • • • • • • • • typedef int8_t int_least8_t typedef uint8_t uint_least8_t typedef int16_t int_least16_t typedef uint16_t uint_least16_t typedef int32_t int_least32_t typedef uint32_t uint_least32_t typedef int64_t int_least64_t typedef uint64_t uint_least64_t Fastest minimum-width integer types Integer types being usually fastest having at least the specified width • • • • • • • • typedef int8_t int_fast8_t typedef uint8_t uint_fast8_t typedef int16_t int_fast16_t typedef uint16_t uint_fast16_t typedef int32_t int_fast32_t typedef uint32_t uint_fast32_t typedef int64_t int_fast64_t typedef uint64_t uint_fast64_t Greatest-width integer types Types designating integer data capable of representing any value of any integer type in the corresponding signed or unsigned category • typedef int64_t intmax_t • typedef uint64_t uintmax_t 6.7.2 6.7.2.1 Define Documentation #define INT16_C(value) value define a constant of type int16_t 6.7.2.2 #define INT16_MAX 0x7fff largest positive value an int16_t can hold. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.7 : Standard Integer Types 6.7.2.3 #define INT16_MIN (-INT16_MAX - 1) smallest negative value an int16_t can hold. 6.7.2.4 #define INT32_C(value) __CONCAT(value, L) define a constant of type int32_t 6.7.2.5 #define INT32_MAX 0x7fffffffL largest positive value an int32_t can hold. 6.7.2.6 #define INT32_MIN (-INT32_MAX - 1L) smallest negative value an int32_t can hold. 6.7.2.7 #define INT64_C(value) __CONCAT(value, LL) define a constant of type int64_t 6.7.2.8 #define INT64_MAX 0x7fffffffffffffffLL largest positive value an int64_t can hold. 6.7.2.9 #define INT64_MIN (-INT64_MAX - 1LL) smallest negative value an int64_t can hold. 6.7.2.10 #define INT8_C(value) ((int8_t) value) define a constant of type int8_t 6.7.2.11 #define INT8_MAX 0x7f largest positive value an int8_t can hold. 6.7.2.12 #define INT8_MIN (-INT8_MAX - 1) smallest negative value an int8_t can hold. 6.7.2.13 #define INT_FAST16_MAX INT16_MAX largest positive value an int_fast16_t can hold. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 40 6.7 : Standard Integer Types 6.7.2.14 #define INT_FAST16_MIN INT16_MIN smallest negative value an int_fast16_t can hold. 6.7.2.15 #define INT_FAST32_MAX INT32_MAX largest positive value an int_fast32_t can hold. 6.7.2.16 #define INT_FAST32_MIN INT32_MIN smallest negative value an int_fast32_t can hold. 6.7.2.17 #define INT_FAST64_MAX INT64_MAX largest positive value an int_fast64_t can hold. 6.7.2.18 #define INT_FAST64_MIN INT64_MIN smallest negative value an int_fast64_t can hold. 6.7.2.19 #define INT_FAST8_MAX INT8_MAX largest positive value an int_fast8_t can hold. 6.7.2.20 #define INT_FAST8_MIN INT8_MIN smallest negative value an int_fast8_t can hold. 6.7.2.21 #define INT_LEAST16_MAX INT16_MAX largest positive value an int_least16_t can hold. 6.7.2.22 #define INT_LEAST16_MIN INT16_MIN smallest negative value an int_least16_t can hold. 6.7.2.23 #define INT_LEAST32_MAX INT32_MAX largest positive value an int_least32_t can hold. 6.7.2.24 #define INT_LEAST32_MIN INT32_MIN smallest negative value an int_least32_t can hold. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 41 6.7 : Standard Integer Types 6.7.2.25 #define INT_LEAST64_MAX INT64_MAX largest positive value an int_least64_t can hold. 6.7.2.26 #define INT_LEAST64_MIN INT64_MIN smallest negative value an int_least64_t can hold. 6.7.2.27 #define INT_LEAST8_MAX INT8_MAX largest positive value an int_least8_t can hold. 6.7.2.28 #define INT_LEAST8_MIN INT8_MIN smallest negative value an int_least8_t can hold. 6.7.2.29 #define INTMAX_C(value) __CONCAT(value, LL) define a constant of type intmax_t 6.7.2.30 #define INTMAX_MAX INT64_MAX largest positive value an intmax_t can hold. 6.7.2.31 #define INTMAX_MIN INT64_MIN smallest negative value an intmax_t can hold. 6.7.2.32 #define INTPTR_MAX INT16_MAX largest positive value an intptr_t can hold. 6.7.2.33 #define INTPTR_MIN INT16_MIN smallest negative value an intptr_t can hold. 6.7.2.34 #define PTRDIFF_MAX INT16_MAX largest positive value a ptrdiff_t can hold. 6.7.2.35 #define PTRDIFF_MIN INT16_MIN smallest negative value a ptrdiff_t can hold. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 42 6.7 : Standard Integer Types 6.7.2.36 43 #define SIG_ATOMIC_MAX INT8_MAX largest positive value a sig_atomic_t can hold. 6.7.2.37 #define SIG_ATOMIC_MIN INT8_MIN smallest negative value a sig_atomic_t can hold. 6.7.2.38 #define SIZE_MAX (__CONCAT(INT16_MAX, U)) largest value a size_t can hold. 6.7.2.39 #define UINT16_C(value) __CONCAT(value, U) define a constant of type uint16_t 6.7.2.40 #define UINT16_MAX (__CONCAT(INT16_MAX, U) ∗ 2U + 1U) largest value an uint16_t can hold. 6.7.2.41 #define UINT32_C(value) __CONCAT(value, UL) define a constant of type uint32_t 6.7.2.42 #define UINT32_MAX (__CONCAT(INT32_MAX, U) ∗ 2UL + 1UL) largest value an uint32_t can hold. 6.7.2.43 #define UINT64_C(value) __CONCAT(value, ULL) define a constant of type uint64_t 6.7.2.44 #define UINT64_MAX (__CONCAT(INT64_MAX, U) ∗ 2ULL + 1ULL) largest value an uint64_t can hold. 6.7.2.45 #define UINT8_C(value) ((uint8_t) __CONCAT(value, U)) define a constant of type uint8_t 6.7.2.46 #define UINT8_MAX (__CONCAT(INT8_MAX, U) ∗ 2U + 1U) largest value an uint8_t can hold. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.7 : Standard Integer Types 6.7.2.47 #define UINT_FAST16_MAX UINT16_MAX largest value an uint_fast16_t can hold. 6.7.2.48 #define UINT_FAST32_MAX UINT32_MAX largest value an uint_fast32_t can hold. 6.7.2.49 #define UINT_FAST64_MAX UINT64_MAX largest value an uint_fast64_t can hold. 6.7.2.50 #define UINT_FAST8_MAX UINT8_MAX largest value an uint_fast8_t can hold. 6.7.2.51 #define UINT_LEAST16_MAX UINT16_MAX largest value an uint_least16_t can hold. 6.7.2.52 #define UINT_LEAST32_MAX UINT32_MAX largest value an uint_least32_t can hold. 6.7.2.53 #define UINT_LEAST64_MAX UINT64_MAX largest value an uint_least64_t can hold. 6.7.2.54 #define UINT_LEAST8_MAX UINT8_MAX largest value an uint_least8_t can hold. 6.7.2.55 #define UINTMAX_C(value) __CONCAT(value, ULL) define a constant of type uintmax_t 6.7.2.56 #define UINTMAX_MAX UINT64_MAX largest value an uintmax_t can hold. 6.7.2.57 #define UINTPTR_MAX UINT16_MAX largest value an uintptr_t can hold. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 44 6.7 : Standard Integer Types 6.7.3 6.7.3.1 Typedef Documentation typedef signed int int16_t 16-bit signed type. 6.7.3.2 typedef signed long int int32_t 32-bit signed type. 6.7.3.3 typedef signed long long int int64_t 64-bit signed type. Note: This type is not available when the compiler option -mint8 is in effect. 6.7.3.4 typedef signed char int8_t 8-bit signed type. 6.7.3.5 typedef int16_t int_fast16_t fastest signed int with at least 16 bits. 6.7.3.6 typedef int32_t int_fast32_t fastest signed int with at least 32 bits. 6.7.3.7 typedef int64_t int_fast64_t fastest signed int with at least 64 bits. Note: This type is not available when the compiler option -mint8 is in effect. 6.7.3.8 typedef int8_t int_fast8_t fastest signed int with at least 8 bits. 6.7.3.9 typedef int16_t int_least16_t signed int with at least 16 bits. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 45 6.7 : Standard Integer Types 6.7.3.10 typedef int32_t int_least32_t signed int with at least 32 bits. 6.7.3.11 typedef int64_t int_least64_t signed int with at least 64 bits. Note: This type is not available when the compiler option -mint8 is in effect. 6.7.3.12 typedef int8_t int_least8_t signed int with at least 8 bits. 6.7.3.13 typedef int64_t intmax_t largest signed int available. 6.7.3.14 typedef int16_t intptr_t Signed pointer compatible type. 6.7.3.15 typedef unsigned int uint16_t 16-bit unsigned type. 6.7.3.16 typedef unsigned long int uint32_t 32-bit unsigned type. 6.7.3.17 typedef unsigned long long int uint64_t 64-bit unsigned type. Note: This type is not available when the compiler option -mint8 is in effect. 6.7.3.18 typedef unsigned char uint8_t 8-bit unsigned type. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 46 6.7 : Standard Integer Types 6.7.3.19 typedef uint16_t uint_fast16_t fastest unsigned int with at least 16 bits. 6.7.3.20 typedef uint32_t uint_fast32_t fastest unsigned int with at least 32 bits. 6.7.3.21 typedef uint64_t uint_fast64_t fastest unsigned int with at least 64 bits. Note: This type is not available when the compiler option -mint8 is in effect. 6.7.3.22 typedef uint8_t uint_fast8_t fastest unsigned int with at least 8 bits. 6.7.3.23 typedef uint16_t uint_least16_t unsigned int with at least 16 bits. 6.7.3.24 typedef uint32_t uint_least32_t unsigned int with at least 32 bits. 6.7.3.25 typedef uint64_t uint_least64_t unsigned int with at least 64 bits. Note: This type is not available when the compiler option -mint8 is in effect. 6.7.3.26 typedef uint8_t uint_least8_t unsigned int with at least 8 bits. 6.7.3.27 typedef uint64_t uintmax_t largest unsigned int available. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 47 6.8 : Standard IO facilities 6.7.3.28 48 typedef uint16_t uintptr_t Unsigned pointer compatible type. 6.8 6.8.1 : Standard IO facilities Detailed Description #include Introduction to the Standard IO facilities This file declares the standard IO facilities that are implemented in avr-libc. Due to the nature of the underlying hardware, only a limited subset of standard IO is implemented. There is no actual file implementation available, so only device IO can be performed. Since there’s no operating system, the application needs to provide enough details about their devices in order to make them usable by the standard IO facilities. Due to space constraints, some functionality has not been implemented at all (like some of the printf conversions that have been left out). Nevertheless, potential users of this implementation should be warned: the printf and scanf families of functions, although usually associated with presumably simple things like the famous "Hello, world!" program, are actually fairly complex which causes their inclusion to eat up a fair amount of code space. Also, they are not fast due to the nature of interpreting the format string at run-time. Whenever possible, resorting to the (sometimes nonstandard) predetermined conversion facilities that are offered by avr-libc will usually cost much less in terms of speed and code size. Tunable options for code size vs. feature set In order to allow programmers a code size vs. functionality tradeoff, the function vfprintf() which is the heart of the printf family can be selected in different flavours using linker options. See the documentation of vfprintf() for a detailed description. The same applies to vfscanf() and the scanf family of functions. Outline of the chosen API The standard streams stdin, stdout, and stderr are provided, but contrary to the C standard, since avr-libc has no knowledge about applicable devices, these streams are not already pre-initialized at application startup. Also, since there is no notion of "file" whatsoever to avr-libc, there is no function fopen() that could be used to associate a stream to some device. (See note 1.) Instead, the function fdevopen() is provided to associate a stream to a device, where the device needs to provide a function to send a character, to receive a character, or both. There is no differentiation between "text" and "binary" streams inside avr-libc. Character \n is sent literally down to the device’s put() function. If the device requires a carriage return (\r) character to be sent before the linefeed, its put() routine must implement this (see note 2). Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 49 As an alternative method to fdevopen(), the macro fdev_setup_stream() might be used to setup a user-supplied FILE structure. It should be noted that the automatic conversion of a newline character into a carriage return - newline sequence breaks binary transfers. If binary transfers are desired, no automatic conversion should be performed, but instead any string that aims to issue a CR-LF sequence must use "\r\n" explicitly. For convenience, the first call to fdevopen() that opens a stream for reading will cause the resulting stream to be aliased to stdin. Likewise, the first call to fdevopen() that opens a stream for writing will cause the resulting stream to be aliased to both, stdout, and stderr. Thus, if the open was done with both, read and write intent, all three standard streams will be identical. Note that these aliases are indistinguishable from each other, thus calling fclose() on such a stream will also effectively close all of its aliases (note 3). It is possible to tie additional user data to a stream, using fdev_set_udata(). The backend put and get functions can then extract this user data using fdev_get_udata(), and act appropriately. For example, a single put function could be used to talk to two different UARTs that way, or the put and get functions could keep internal state between calls there. Format strings in flash ROM All the printf and scanf family functions come in two flavours: the standard name, where the format string is expected to be in SRAM, as well as a version with the suffix "_P" where the format string is expected to reside in the flash ROM. The macro PSTR (explained in : Program Space Utilities) becomes very handy for declaring these format strings. Running stdio without malloc() By default, fdevopen() requires malloc(). As this is often not desired in the limited environment of a microcontroller, an alternative option is provided to run completely without malloc(). The macro fdev_setup_stream() is provided to prepare a user-supplied FILE buffer for operation with stdio. Example #include static int uart_putchar(char c, FILE *stream); static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); static int uart_putchar(char c, FILE *stream) { Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 50 if (c == ’\n’) uart_putchar(’\r’, stream); loop_until_bit_is_set(UCSRA, UDRE); UDR = c; return 0; } int main(void) { init_uart(); stdout = &mystdout; printf("Hello, world!\n"); return 0; } This example uses the initializer form FDEV_SETUP_STREAM() rather than the function-like fdev_setup_stream(), so all data initialization happens during C start-up. If streams initialized that way are no longer needed, they can be destroyed by first calling the macro fdev_close(), and then destroying the object itself. No call to fclose() should be issued for these streams. While calling fclose() itself is harmless, it will cause an undefined reference to free() and thus cause the linker to link the malloc module into the application. Notes Note 1: It might have been possible to implement a device abstraction that is compatible with fopen() but since this would have required to parse a string, and to take all the information needed either out of this string, or out of an additional table that would need to be provided by the application, this approach was not taken. Note 2: This basically follows the Unix approach: if a device such as a terminal needs special handling, it is in the domain of the terminal device driver to provide this functionality. Thus, a simple function suitable as put() for fdevopen() that talks to a UART interface might look like this: int uart_putchar(char c, FILE *stream) { if (c == ’\n’) uart_putchar(’\r’); loop_until_bit_is_set(UCSRA, UDRE); UDR = c; return 0; } Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 51 Note 3: This implementation has been chosen because the cost of maintaining an alias is considerably smaller than the cost of maintaining full copies of each stream. Yet, providing an implementation that offers the complete set of standard streams was deemed to be useful. Not only that writing printf() instead of fprintf(mystream, ...) saves typing work, but since avr-gcc needs to resort to pass all arguments of variadic functions on the stack (as opposed to passing them in registers for functions that take a fixed number of parameters), the ability to pass one parameter less by implying stdin will also save some execution time. Defines • • • • • • • • • • • • • • • • • • • #define FILE struct __file #define stdin (__iob[0]) #define stdout (__iob[1]) #define stderr (__iob[2]) #define EOF (-1) #define fdev_set_udata(stream, u) do { (stream) → udata = u; } while(0) #define fdev_get_udata(stream) ((stream) → udata) #define fdev_setup_stream(stream, put, get, rwflag) #define _FDEV_SETUP_READ __SRD #define _FDEV_SETUP_WRITE __SWR #define _FDEV_SETUP_RW (__SRD|__SWR) #define _FDEV_ERR (-1) #define _FDEV_EOF (-2) #define FDEV_SETUP_STREAM(put, get, rwflag) #define fdev_close() #define putc(__c, __stream) fputc(__c, __stream) #define putchar(__c) fputc(__c, stdout) #define getc(__stream) fgetc(__stream) #define getchar() fgetc(stdin) Functions • • • • • • • • int fclose (FILE ∗__stream) int vfprintf (FILE ∗__stream, const char ∗__fmt, va_list __ap) int vfprintf_P (FILE ∗__stream, const char ∗__fmt, va_list __ap) int fputc (int __c, FILE ∗__stream) int printf (const char ∗__fmt,...) int printf_P (const char ∗__fmt,...) int vprintf (const char ∗__fmt, va_list __ap) int sprintf (char ∗__s, const char ∗__fmt,...) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 52 int sprintf_P (char ∗__s, const char ∗__fmt,...) int snprintf (char ∗__s, size_t __n, const char ∗__fmt,...) int snprintf_P (char ∗__s, size_t __n, const char ∗__fmt,...) int vsprintf (char ∗__s, const char ∗__fmt, va_list ap) int vsprintf_P (char ∗__s, const char ∗__fmt, va_list ap) int vsnprintf (char ∗__s, size_t __n, const char ∗__fmt, va_list ap) int vsnprintf_P (char ∗__s, size_t __n, const char ∗__fmt, va_list ap) int fprintf (FILE ∗__stream, const char ∗__fmt,...) int fprintf_P (FILE ∗__stream, const char ∗__fmt,...) int fputs (const char ∗__str, FILE ∗__stream) int fputs_P (const char ∗__str, FILE ∗__stream) int puts (const char ∗__str) int puts_P (const char ∗__str) size_t fwrite (const void ∗__ptr, size_t __size, size_t __nmemb, FILE ∗__stream) int fgetc (FILE ∗__stream) int ungetc (int __c, FILE ∗__stream) char ∗ fgets (char ∗__str, int __size, FILE ∗__stream) char ∗ gets (char ∗__str) size_t fread (void ∗__ptr, size_t __size, size_t __nmemb, FILE ∗__stream) void clearerr (FILE ∗__stream) int feof (FILE ∗__stream) int ferror (FILE ∗__stream) int vfscanf (FILE ∗__stream, const char ∗__fmt, va_list __ap) int vfscanf_P (FILE ∗__stream, const char ∗__fmt, va_list __ap) int fscanf (FILE ∗__stream, const char ∗__fmt,...) int fscanf_P (FILE ∗__stream, const char ∗__fmt,...) int scanf (const char ∗__fmt,...) int scanf_P (const char ∗__fmt,...) int vscanf (const char ∗__fmt, va_list __ap) int sscanf (const char ∗__buf, const char ∗__fmt,...) int sscanf_P (const char ∗__buf, const char ∗__fmt,...) int fflush (FILE ∗stream) FILE ∗ fdevopen (int(∗put)(char, FILE ∗), int(∗get)(FILE ∗)) 6.8.2 6.8.2.1 Define Documentation #define _FDEV_EOF (-2) Return code for an end-of-file condition during device read. To be used in the get function of fdevopen(). Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 6.8.2.2 53 #define _FDEV_ERR (-1) Return code for an error condition during device read. To be used in the get function of fdevopen(). 6.8.2.3 #define _FDEV_SETUP_READ __SRD fdev_setup_stream() with read intent 6.8.2.4 #define _FDEV_SETUP_RW (__SRD|__SWR) fdev_setup_stream() with read/write intent 6.8.2.5 #define _FDEV_SETUP_WRITE __SWR fdev_setup_stream() with write intent 6.8.2.6 #define EOF (-1) EOF declares the value that is returned by various standard IO functions in case of an error. Since the AVR platform (currently) doesn’t contain an abstraction for actual files, its origin as "end of file" is somewhat meaningless here. 6.8.2.7 #define fdev_close() This macro frees up any library resources that might be associated with stream. It should be called if stream is no longer needed, right before the application is going to destroy the stream object itself. (Currently, this macro evaluates to nothing, but this might change in future versions of the library.) 6.8.2.8 #define fdev_get_udata(stream) ((stream) → udata) This macro retrieves a pointer to user defined data from a FILE stream object. 6.8.2.9 #define fdev_set_udata(stream, u) do { (stream) → udata = u; } while(0) This macro inserts a pointer to user defined data into a FILE stream object. The user data can be useful for tracking state in the put and get functions supplied to the fdevopen() function. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 6.8.2.10 54 #define FDEV_SETUP_STREAM(put, get, rwflag) Initializer for a user-supplied stdio stream. This macro acts similar to fdev_setup_stream(), but it is to be used as the initializer of a variable of type FILE. The remaining arguments are to be used as explained in fdev_setup_stream(). 6.8.2.11 #define fdev_setup_stream(stream, put, get, rwflag) Setup a user-supplied buffer as an stdio stream. This macro takes a user-supplied buffer stream, and sets it up as a stream that is valid for stdio operations, similar to one that has been obtained dynamically from fdevopen(). The buffer to setup must be of type FILE. The arguments put and get are identical to those that need to be passed to fdevopen(). The rwflag argument can take one of the values _FDEV_SETUP_READ, _FDEV_SETUP_WRITE, or _FDEV_SETUP_RW, for read, write, or read/write intent, respectively. Note: No assignments to the standard streams will be performed by fdev_setup_stream(). If standard streams are to be used, these need to be assigned by the user. See also under Running stdio without malloc(). 6.8.2.12 #define FILE struct __file FILE is the opaque structure that is passed around between the various standard IO functions. 6.8.2.13 #define getc(__stream) fgetc(__stream) The macro getc used to be a "fast" macro implementation with a functionality identical to fgetc(). For space constraints, in avr-libc, it is just an alias for fgetc. 6.8.2.14 #define getchar(void) fgetc(stdin) The macro getchar reads a character from stdin. Return values and error handling is identical to fgetc(). 6.8.2.15 #define putc(__c, __stream) fputc(__c, __stream) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 55 The macro putc used to be a "fast" macro implementation with a functionality identical to fputc(). For space constraints, in avr-libc, it is just an alias for fputc. 6.8.2.16 #define putchar(__c) fputc(__c, stdout) The macro putchar sends character c to stdout. 6.8.2.17 #define stderr (__iob[2]) Stream destined for error output. Unless specifically assigned, identical to stdout. If stderr should point to another stream, the result of another fdevopen() must be explicitly assigned to it without closing the previous stderr (since this would also close stdout). 6.8.2.18 #define stdin (__iob[0]) Stream that will be used as an input stream by the simplified functions that don’t take a stream argument. The first stream opened with read intent using fdevopen() will be assigned to stdin. 6.8.2.19 #define stdout (__iob[1]) Stream that will be used as an output stream by the simplified functions that don’t take a stream argument. The first stream opened with write intent using fdevopen() will be assigned to both, stdin, and stderr. 6.8.3 6.8.3.1 Function Documentation void clearerr (FILE ∗ __stream) Clear the error and end-of-file flags of stream. 6.8.3.2 int fclose (FILE ∗ __stream) This function closes stream, and disallows and further IO to and from it. When using fdevopen() to setup the stream, a call to fclose() is needed in order to free the internal resources allocated. If the stream has been set up using fdev_setup_stream() or FDEV_SETUP_STREAM(), use fdev_close() instead. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 56 It currently always returns 0 (for success). 6.8.3.3 FILE∗ fdevopen (int(∗)(char, FILE ∗) put, int(∗)(FILE ∗) get) This function is a replacement for fopen(). It opens a stream for a device where the actual device implementation needs to be provided by the application. If successful, a pointer to the structure for the opened stream is returned. Reasons for a possible failure currently include that neither the put nor the get argument have been provided, thus attempting to open a stream with no IO intent at all, or that insufficient dynamic memory is available to establish a new stream. If the put function pointer is provided, the stream is opened with write intent. The function passed as put shall take two arguments, the first a character to write to the device, and the second a pointer to FILE, and shall return 0 if the output was successful, and a nonzero value if the character could not be sent to the device. If the get function pointer is provided, the stream is opened with read intent. The function passed as get shall take a pointer to FILE as its single argument, and return one character from the device, passed as an int type. If an error occurs when trying to read from the device, it shall return _FDEV_ERR. If an end-of-file condition was reached while reading from the device, _FDEV_EOF shall be returned. If both functions are provided, the stream is opened with read and write intent. The first stream opened with read intent is assigned to stdin, and the first one opened with write intent is assigned to both, stdout and stderr. fdevopen() uses calloc() (und thus malloc()) in order to allocate the storage for the new stream. Note: If the macro __STDIO_FDEVOPEN_COMPAT_12 is declared before including , a function prototype for fdevopen() will be chosen that is backwards compatible with avr-libc version 1.2 and before. This is solely intented for providing a simple migration path without the need to immediately change all source code. Do not use for new code. 6.8.3.4 int feof (FILE ∗ __stream) Test the end-of-file flag of stream. This flag can only be cleared by a call to clearerr(). 6.8.3.5 int ferror (FILE ∗ __stream) Test the error flag of stream. This flag can only be cleared by a call to clearerr(). Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 6.8.3.6 57 int fflush (FILE ∗ stream) Flush stream. This is a null operation provided for source-code compatibility only, as the standard IO implementation currently does not perform any buffering. 6.8.3.7 int fgetc (FILE ∗ __stream) The function fgetc reads a character from stream. It returns the character, or EOF in case end-of-file was encountered or an error occurred. The routines feof() or ferror() must be used to distinguish between both situations. 6.8.3.8 char∗ fgets (char ∗ __str, int __size, FILE ∗ __stream) Read at most size - 1 bytes from stream, until a newline character was encountered, and store the characters in the buffer pointed to by str. Unless an error was encountered while reading, the string will then be terminated with a NUL character. If an error was encountered, the function returns NULL and sets the error flag of stream, which can be tested using ferror(). Otherwise, a pointer to the string will be returned. 6.8.3.9 int fprintf (FILE ∗ __stream, const char ∗ __fmt, ...) The function fprintf performs formatted output to stream. See vfprintf() for details. 6.8.3.10 int fprintf_P (FILE ∗ __stream, const char ∗ __fmt, ...) Variant of fprintf() that uses a fmt string that resides in program memory. 6.8.3.11 int fputc (int __c, FILE ∗ __stream) The function fputc sends the character c (though given as type int) to stream. It returns the character, or EOF in case an error occurred. 6.8.3.12 int fputs (const char ∗ __str, FILE ∗ __stream) Write the string pointed to by str to stream stream. Returns 0 on success and EOF on error. 6.8.3.13 int fputs_P (const char ∗ __str, FILE ∗ __stream) Variant of fputs() where str resides in program memory. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 6.8.3.14 stream) 58 size_t fread (void ∗ __ptr, size_t __size, size_t __nmemb, FILE ∗ __- Read nmemb objects, size bytes each, from stream, to the buffer pointed to by ptr. Returns the number of objects successfully read, i. e. nmemb unless an input error occured or end-of-file was encountered. feof() and ferror() must be used to distinguish between these two conditions. 6.8.3.15 int fscanf (FILE ∗ __stream, const char ∗ __fmt, ...) The function fscanf performs formatted input, reading the input data from stream. See vfscanf() for details. 6.8.3.16 int fscanf_P (FILE ∗ __stream, const char ∗ __fmt, ...) Variant of fscanf() using a fmt string in program memory. 6.8.3.17 size_t fwrite (const void ∗ __ptr, size_t __size, size_t __nmemb, FILE ∗ __stream) Write nmemb objects, size bytes each, to stream. The first byte of the first object is referenced by ptr. Returns the number of objects successfully written, i. e. nmemb unless an output error occured. 6.8.3.18 char∗ gets (char ∗ __str) Similar to fgets() except that it will operate on stream stdin, and the trailing newline (if any) will not be stored in the string. It is the caller’s responsibility to provide enough storage to hold the characters read. 6.8.3.19 int printf (const char ∗ __fmt, ...) The function printf performs formatted output to stream stderr. vfprintf() for details. 6.8.3.20 int printf_P (const char ∗ __fmt, ...) Variant of printf() that uses a fmt string that resides in program memory. 6.8.3.21 int puts (const char ∗ __str) Write the string pointed to by str, and a trailing newline character, to stdout. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen See 6.8 : Standard IO facilities 6.8.3.22 59 int puts_P (const char ∗ __str) Variant of puts() where str resides in program memory. 6.8.3.23 int scanf (const char ∗ __fmt, ...) The function scanf performs formatted input from stream stdin. See vfscanf() for details. 6.8.3.24 int scanf_P (const char ∗ __fmt, ...) Variant of scanf() where fmt resides in program memory. 6.8.3.25 int snprintf (char ∗ __s, size_t __n, const char ∗ __fmt, ...) Like sprintf(), but instead of assuming s to be of infinite size, no more than n characters (including the trailing NUL character) will be converted to s. Returns the number of characters that would have been written to s if there were enough space. 6.8.3.26 int snprintf_P (char ∗ __s, size_t __n, const char ∗ __fmt, ...) Variant of snprintf() that uses a fmt string that resides in program memory. 6.8.3.27 int sprintf (char ∗ __s, const char ∗ __fmt, ...) Variant of printf() that sends the formatted characters to string s. 6.8.3.28 int sprintf_P (char ∗ __s, const char ∗ __fmt, ...) Variant of sprintf() that uses a fmt string that resides in program memory. 6.8.3.29 int sscanf (const char ∗ __buf, const char ∗ __fmt, ...) The function sscanf performs formatted input, reading the input data from the buffer pointed to by buf. See vfscanf() for details. 6.8.3.30 int sscanf_P (const char ∗ __buf, const char ∗ __fmt, ...) Variant of sscanf() using a fmt string in program memory. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 6.8.3.31 60 int ungetc (int __c, FILE ∗ __stream) The ungetc() function pushes the character c (converted to an unsigned char) back onto the input stream pointed to by stream. The pushed-back character will be returned by a subsequent read on the stream. Currently, only a single character can be pushed back onto the stream. The ungetc() function returns the character pushed back after the conversion, or EOF if the operation fails. If the value of the argument c character equals EOF, the operation will fail and the stream will remain unchanged. 6.8.3.32 int vfprintf (FILE ∗ __stream, const char ∗ __fmt, va_list __ap) vfprintf is the central facility of the printf family of functions. It outputs values to stream under control of a format string passed in fmt. The actual values to print are passed as a variable argument list ap. vfprintf returns the number of characters written to stream, or EOF in case of an error. Currently, this will only happen if stream has not been opened with write intent. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the % character. The arguments must properly correspond (after type promotion) with the conversion specifier. After the %, the following appear in sequence: • Zero or more of the following flags: – # The value should be converted to an "alternate form". For c, d, i, s, and u conversions, this option has no effect. For o conversions, the precision of the number is increased to force the first character of the output string to a zero (except if a zero value is printed with an explicit precision of zero). For x and X conversions, a non-zero result has the string ‘0x’ (or ‘0X’ for X conversions) prepended to it. – 0 (zero) Zero padding. For all conversions, the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored. – - A negative field width flag; the converted value is to be left adjusted on the field boundary. The converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A - overrides a 0 if both are given. – ’ ’ (space) A blank should be left before a positive number produced by a signed conversion (d, or i). – + A sign must always be placed before a number produced by a signed conversion. A + overrides a space if both are used. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 61 • An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjust ment flag has been given) to fill out the field width. • An optional precision, in the form of a period . followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, or the maximum number of characters to be printed from a string for s conversions. • An optional l length modifier, that specifies that the argument for the d, i, o, u, x, or X conversion is a "long int" rather than int. • A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are: • diouxX The int (or appropriate variant) argument is converted to signed decimal (d and i), unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. The letters "abcdef" are used for x conversions; the letters "ABCDEF" are used for X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. • p The void ∗ argument is taken as an unsigned integer, and converted similarly as a %#x command would do. • c The int argument is converted to an "unsigned char", and the resulting character is written. • s The "char ∗" argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating NUL character; if a precision is specified, no more than the number specified are written. If a precision is given, no null character need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating NUL character. • % A % is written. No argument is converted. The complete conversion specification is "%%". • eE The double argument is rounded and converted in the format "[-]d.dddeśdd" where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter ’E’ (rather than ’e’) to introduce the exponent. The exponent always contains two digits; if the value is zero, the exponent is 00. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 62 • fF The double argument is rounded and converted to decimal notation in the format "[-]ddd.ddd", where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it. • gG The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit. • S Similar to the s format, except the pointer is expected to point to a programmemory (ROM) string instead of a RAM string. In no case does a non-existent or small field width cause truncation of a numeric field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result. Since the full implementation of all the mentioned features becomes fairly large, three different flavours of vfprintf() can be selected using linker options. The default vfprintf() implements all the mentioned functionality except floating point conversions. A minimized version of vfprintf() is available that only implements the very basic integer and string conversion facilities, but none of the additional options that can be specified using conversion flags (these flags are parsed correctly from the format specification, but then simply ignored). This version can be requested using the following compiler options: -Wl,-u,vfprintf -lprintf_min If the full functionality including the floating point conversions is required, the following options should be used: -Wl,-u,vfprintf -lprintf_flt -lm Limitations: • The specified width and precision can be at most 127. • For floating-point conversions, trailing digits will be lost if a number close to DBL_MAX is converted with a precision > 0. 6.8.3.33 int vfprintf_P (FILE ∗ __stream, const char ∗ __fmt, va_list __ap) Variant of vfprintf() that uses a fmt string that resides in program memory. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 6.8.3.34 63 int vfscanf (FILE ∗ __stream, const char ∗ __fmt, va_list __ap) Formatted input. This function is the heart of the scanf family of functions. Characters are read from stream and processed in a way described by fmt. Conversion results will be assigned to the parameters passed via ap. The format string fmt is scanned for conversion specifications. Anything that doesn’t comprise a conversion specification is taken as text that is matched literally against the input. White space in the format string will match any white space in the data (including none), all other characters match only itself. Processing is aborted as soon as the data and format string no longer match, or there is an error or end-of-file condition on stream. Most conversions skip leading white space before starting the actual conversion. Conversions are introduced with the character %. Possible options can follow the %: • a ∗ indicating that the conversion should be performed but the conversion result is to be discarded; no parameters will be processed from ap, • the character h indicating that the argument is a pointer to short int (rather than int), • the character l indicating that the argument is a pointer to long int (rather than int, for integer type conversions), or a pointer to double (for floating point conversions). In addition, a maximal field width may be specified as a nonzero positive decimal integer, which will restrict the conversion to at most this many characters from the input stream. This field width is limited to at most 127 characters which is also the default value (except for the c conversion that defaults to 1). The following conversion flags are supported: • % Matches a literal % character. This is not a conversion. • d Matches an optionally signed decimal integer; the next pointer must be a pointer to int. • i Matches an optionally signed integer; the next pointer must be a pointer to int. The integer is read in base 16 if it begins with 0x or 0X, in base 8 if it begins with 0, and in base 10 otherwise. Only characters that correspond to the base are used. • o Matches an octal integer; the next pointer must be a pointer to unsigned int. • u Matches an optionally signed decimal integer; the next pointer must be a pointer to unsigned int. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 64 • x Matches an optionally signed hexadecimal integer; the next pointer must be a pointer to unsigned int. • f Matches an optionally signed floating-point number; the next pointer must be a pointer to float. • e, g, E, G Equivalent to f. • s Matches a sequence of non-white-space characters; the next pointer must be a pointer to char, and the array must be large enough to accept all the sequence and the terminating NUL character. The input string stops at white space or at the maximum field width, whichever occurs first. • c Matches a sequence of width count characters (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating NUL is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format. • [ Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex ∧ . To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, [∧ ]0-9-] means the set of everything except close bracket, zero through nine, and hyphen. The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out. • p Matches a pointer value (as printed by p in printf()); the next pointer must be a pointer to void. • n Nothing is expected; instead, the number of characters consumed thus far from the input is stored through the next pointer, which must be a pointer to int. This is not a conversion, although it can be suppressed with the ∗ flag. These functions return the number of input items assigned, which can be fewer than provided for, or even zero, in the event of a matching failure. Zero indicates that, while there was input available, no conversions were assigned; typically this is due to an invalid input character, such as an alphabetic character for a d conversion. The value EOF is returned if an input failure occurs before any conversion such as an end-of-file occurs. If an error or end-of-file occurs after conversion has begun, the number of conversions which were successfully completed is returned. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.8 : Standard IO facilities 65 By default, all the conversions described above are available except the floating-point conversions, and the %[ conversion. These conversions will be available in the extended version provided by the library libscanf_flt.a. Note that these conversions require a 40-byte conversion buffer, so the extended version requires more stack space than the basic version irrespective of whether the actual call in progress actually uses this buffer or not. To link a program against the extended version, use the following compiler flags in the link stage: -Wl,-u,vfscanf -lscanf_flt -lm A third version is available for environments that are tight on space. This version is provided in the library libscanf_min.a, and can be requested using the following options in the link stage: -Wl,-u,vfscanf -lscanf_min -lm In addition to the restrictions of the standard version, this version implements no field width specification, no conversion assignment suppression flag (∗), no n specification, and no general format character matching at all. All characters in fmt that do not comprise a conversion specification will simply be ignored, including white space (that is normally used to consume any amount of white space in the input stream). However, the usual skip of initial white space in the formats that support it is implemented. 6.8.3.35 int vfscanf_P (FILE ∗ __stream, const char ∗ __fmt, va_list __ap) Variant of vfscanf() using a fmt string in program memory. 6.8.3.36 int vprintf (const char ∗ __fmt, va_list __ap) The function vprintf performs formatted output to stream stdout, taking a variable argument list as in vfprintf(). See vfprintf() for details. 6.8.3.37 int vscanf (const char ∗ __fmt, va_list __ap) The function vscanf performs formatted input from stream stdin, taking a variable argument list as in vfscanf(). See vfscanf() for details. 6.8.3.38 int vsnprintf (char ∗ __s, size_t __n, const char ∗ __fmt, va_list ap) Like vsprintf(), but instead of assuming s to be of infinite size, no more than n characters (including the trailing NUL character) will be converted to s. Returns the number of characters that would have been written to s if there were enough space. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 6.8.3.39 66 int vsnprintf_P (char ∗ __s, size_t __n, const char ∗ __fmt, va_list ap) Variant of vsnprintf() that uses a fmt string that resides in program memory. 6.8.3.40 int vsprintf (char ∗ __s, const char ∗ __fmt, va_list ap) Like sprintf() but takes a variable argument list for the arguments. 6.8.3.41 int vsprintf_P (char ∗ __s, const char ∗ __fmt, va_list ap) Variant of vsprintf() that uses a fmt string that resides in program memory. : General utilities 6.9 6.9.1 Detailed Description #include This file declares some basic C macros and functions as defined by the ISO standard, plus some AVR-specific extensions. Data Structures • struct div_t • struct ldiv_t Non-standard (i.e. non-ISO C) functions. • • • • • • • • #define RANDOM_MAX 0x7FFFFFFF char ∗ ltoa (long int __val, char ∗__s, int __radix) char ∗ utoa (unsigned int __val, char ∗__s, int __radix) char ∗ ultoa (unsigned long int __val, char ∗__s, int __radix) long random (void) void srandom (unsigned long __seed) long random_r (unsigned long ∗ctx) char ∗ itoa (int __val, char ∗__s, int __radix) Conversion functions for double arguments. Note that these functions are not located in the default library, libc.a, but in the mathematical library, libm.a. So when linking the application, the -lm option needs to be specified. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 67 • • • • #define DTOSTR_ALWAYS_SIGN 0x01 #define DTOSTR_PLUS_SIGN 0x02 #define DTOSTR_UPPERCASE 0x04 char ∗ dtostre (double __val, char ∗__s, unsigned char __prec, unsigned char __flags) • char ∗ dtostrf (double __val, char __width, char __prec, char ∗__s) Defines • #define RAND_MAX 0x7FFF Typedefs • typedef int(∗) __compar_fn_t (const void ∗, const void ∗) Functions • • • • • • • • • • • • • • • • • • • • • __inline__ void abort (void) __ATTR_NORETURN__ int abs (int __i) __ATTR_CONST__ long labs (long __i) __ATTR_CONST__ void ∗ bsearch (const void ∗__key, const void ∗__base, size_t __nmemb, size_t __size, int(∗__compar)(const void ∗, const void ∗)) div_t div (int __num, int __denom) __asm__("__divmodhi4") __ATTR_CONST__ ldiv_t ldiv (long __num, long __denom) __asm__("__divmodsi4") __ATTR_CONST__ void qsort (void ∗__base, size_t __nmemb, size_t __size, __compar_fn_t __compar) long strtol (const char ∗__nptr, char ∗∗__endptr, int __base) unsigned long strtoul (const char ∗__nptr, char ∗∗__endptr, int __base) long atol (const char ∗s) __ATTR_PURE__ int atoi (const char ∗s) __ATTR_PURE__ void exit (int __status) __ATTR_NORETURN__ void ∗ malloc (size_t __size) __ATTR_MALLOC__ void free (void ∗__ptr) void ∗ calloc (size_t __nele, size_t __size) __ATTR_MALLOC__ void ∗ realloc (void ∗__ptr, size_t __size) __ATTR_MALLOC__ double strtod (const char ∗__nptr, char ∗∗__endptr) double atof (const char ∗__nptr) int rand (void) void srand (unsigned int __seed) int rand_r (unsigned long ∗ctx) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 68 Variables • size_t __malloc_margin • char ∗ __malloc_heap_start • char ∗ __malloc_heap_end 6.9.2 6.9.2.1 Define Documentation #define DTOSTR_ALWAYS_SIGN 0x01 Bit value that can be passed in flags to dtostre(). 6.9.2.2 #define DTOSTR_PLUS_SIGN 0x02 Bit value that can be passed in flags to dtostre(). 6.9.2.3 #define DTOSTR_UPPERCASE 0x04 Bit value that can be passed in flags to dtostre(). 6.9.2.4 #define RAND_MAX 0x7FFF Highest number that can be generated by rand(). 6.9.2.5 #define RANDOM_MAX 0x7FFFFFFF Highest number that can be generated by random(). 6.9.3 6.9.3.1 Typedef Documentation typedef int(∗) __compar_fn_t(const void ∗, const void ∗) Comparision function type for qsort(), just for convenience. 6.9.4 6.9.4.1 Function Documentation __inline__ void abort (void) The abort() function causes abnormal program termination to occur. In the limited AVR environment, execution is effectively halted by entering an infinite loop. 6.9.4.2 int abs (int __i) The abs() function computes the absolute value of the integer i. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 69 Note: The abs() and labs() functions are builtins of gcc. 6.9.4.3 double atof (const char ∗ __nptr) The atof() function converts the initial portion of the string pointed to by nptr to double representation. It is equivalent to calling strtod(nptr, (char **)NULL); 6.9.4.4 int atoi (const char ∗ s) Convert a string to an integer. The atoi() function converts the initial portion of the string pointed to by s to integer representation. In contrast to (int)strtol(s, (char **)NULL, 10); this function does not detect overflow (errno is not changed and the result value is not predictable), uses smaller memory (flash and stack) and works more quickly. 6.9.4.5 long atol (const char ∗ s) Convert a string to a long integer. The atol() function converts the initial portion of the string pointed to by s to long integer representation. In contrast to strtol(s, (char **)NULL, 10); this function does not detect overflow (errno is not changed and the result value is not predictable), uses smaller memory (flash and stack) and works more quickly. 6.9.4.6 void∗ bsearch (const void ∗ __key, const void ∗ __base, size_t __nmemb, size_t __size, int(∗)(const void ∗, const void ∗) __compar) The bsearch() function searches an array of nmemb objects, the initial member of which is pointed to by base, for a member that matches the object pointed to by key. The size of each member of the array is specified by size. The contents of the array should be in ascending sorted order according to the comparison function referenced by compar. The compar routine is expected to have two Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 70 arguments which point to the key object and to an array member, in that order, and should return an integer less than, equal to, or greater than zero if the key object is found, respectively, to be less than, to match, or be greater than the array member. The bsearch() function returns a pointer to a matching member of the array, or a null pointer if no match is found. If two members compare as equal, which member is matched is unspecified. 6.9.4.7 void∗ calloc (size_t __nele, size_t __size) Allocate nele elements of size each. Identical to calling malloc() using nele ∗ size as argument, except the allocated memory will be cleared to zero. 6.9.4.8 div_t div (int __num, int __denom) The div() function computes the value num/denom and returns the quotient and remainder in a structure named div_t that contains two int members named quot and rem. 6.9.4.9 char∗ dtostre (double __val, char ∗ __s, unsigned char __prec, unsigned char __flags) The dtostre() function converts the double value passed in val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s. Conversion is done in the format "[-]d.dddeśdd" where there is one digit before the decimal-point character and the number of digits after it is equal to the precision prec; if the precision is zero, no decimal-point character appears. If flags has the DTOSTRE_UPPERCASE bit set, the letter ’E’ (rather than ’e’ ) will be used to introduce the exponent. The exponent always contains two digits; if the value is zero, the exponent is "00". If flags has the DTOSTRE_ALWAYS_SIGN bit set, a space character will be placed into the leading position for positive numbers. If flags has the DTOSTRE_PLUS_SIGN bit set, a plus sign will be used instead of a space character in this case. The dtostre() function returns the pointer to the converted string s. 6.9.4.10 char∗ dtostrf (double __val, char __width, char __prec, char ∗ __s) The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s. Conversion is done in the format "[-]d.ddd". The minimum field width of the Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 71 output string (including the ’.’ and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. The dtostrf() function returns the pointer to the converted string s. 6.9.4.11 void exit (int __status) The exit() function terminates the application. Since there is no environment to return to, status is ignored, and code execution will eventually reach an infinite loop, thereby effectively halting all code processing. In a C++ context, global destructors will be called before halting execution. 6.9.4.12 void free (void ∗ __ptr) The free() function causes the allocated memory referenced by ptr to be made available for future allocations. If ptr is NULL, no action occurs. 6.9.4.13 char∗ itoa (int __val, char ∗ __s, int __radix) Convert an integer to a string. The function itoa() converts the integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s. Note: The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 ∗ sizeof (int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size. Warning: If the buffer is too small, you risk a buffer overflow. Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after ’9’ will be the letter ’a’. If radix is 10 and val is negative, a minus sign will be prepended. The itoa() function returns the pointer passed as s. 6.9.4.14 long labs (long __i) The labs() function computes the absolute value of the long integer i. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 72 Note: The abs() and labs() functions are builtins of gcc. 6.9.4.15 ldiv_t ldiv (long __num, long __denom) The ldiv() function computes the value num/denom and returns the quotient and remainder in a structure named ldiv_t that contains two long integer members named quot and rem. 6.9.4.16 char∗ ltoa (long int __val, char ∗ __s, int __radix) Convert a long integer to a string. The function ltoa() converts the long integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s. Note: The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 ∗ sizeof (long int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size. Warning: If the buffer is too small, you risk a buffer overflow. Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after ’9’ will be the letter ’a’. If radix is 10 and val is negative, a minus sign will be prepended. The ltoa() function returns the pointer passed as s. 6.9.4.17 void∗ malloc (size_t __size) The malloc() function allocates size bytes of memory. If malloc() fails, a NULL pointer is returned. Note that malloc() does not initialize the returned memory to zero bytes. See the chapter about malloc() usage for implementation details. 6.9.4.18 void qsort (void ∗ __base, size_t __nmemb, size_t __size, __compar_fn_t __compar) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 73 The qsort() function is a modified partition-exchange sort, or quicksort. The qsort() function sorts an array of nmemb objects, the initial member of which is pointed to by base. The size of each object is specified by size. The contents of the array base are sorted in ascending order according to a comparison function pointed to by compar, which requires two arguments pointing to the objects being compared. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. 6.9.4.19 int rand (void) The rand() function computes a sequence of pseudo-random integers in the range of 0 to RAND_MAX (as defined by the header file ). The srand() function sets its argument seed as the seed for a new sequence of pseudorandom numbers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If no seed value is provided, the functions are automatically seeded with a value of 1. In compliance with the C standard, these functions operate on int arguments. Since the underlying algorithm already uses 32-bit calculations, this causes a loss of precision. See random() for an alternate set of functions that retains full 32-bit precision. 6.9.4.20 int rand_r (unsigned long ∗ ctx) Variant of rand() that stores the context in the user-supplied variable located at ctx instead of a static library variable so the function becomes re-entrant. 6.9.4.21 long random (void) The random() function computes a sequence of pseudo-random integers in the range of 0 to RANDOM_MAX (as defined by the header file ). The srandom() function sets its argument seed as the seed for a new sequence of pseudo-random numbers to be returned by rand(). These sequences are repeatable by calling srandom() with the same seed value. If no seed value is provided, the functions are automatically seeded with a value of 1. 6.9.4.22 long random_r (unsigned long ∗ ctx) Variant of random() that stores the context in the user-supplied variable located at ctx instead of a static library variable so the function becomes re-entrant. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 6.9.4.23 74 void∗ realloc (void ∗ __ptr, size_t __size) The realloc() function tries to change the size of the region allocated at ptr to the new size value. It returns a pointer to the new region. The returned pointer might be the same as the old pointer, or a pointer to a completely different region. The contents of the returned region up to either the old or the new size value (whatever is less) will be identical to the contents of the old region, even in case a new region had to be allocated. It is acceptable to pass ptr as NULL, in which case realloc() will behave identical to malloc(). If the new memory cannot be allocated, realloc() returns NULL, and the region at ptr will not be changed. 6.9.4.24 void srand (unsigned int __seed) Pseudo-random number generator seeding; see rand(). 6.9.4.25 void srandom (unsigned long __seed) Pseudo-random number generator seeding; see random(). 6.9.4.26 double strtod (const char ∗ __nptr, char ∗∗ __endptr) The strtod() function converts the initial portion of the string pointed to by nptr to double representation. The expected form of the string is an optional plus ( ’+’ ) or minus sign ( ’-’ ) followed by a sequence of digits optionally containing a decimal-point character, optionally followed by an exponent. An exponent consists of an ’E’ or ’e’, followed by an optional plus or minus sign, followed by a sequence of digits. Leading white-space characters in the string are skipped. The strtod() function returns the converted value, if any. If endptr is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr. If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr. If the correct value would cause overflow, plus or minus HUGE_VAL is returned (according to the sign of the value), and ERANGE is stored in errno. If the correct value would cause underflow, zero is returned and ERANGE is stored in errno. FIXME: HUGE_VAL needs to be defined somewhere. The bit pattern is 0x7fffffff, but what number would this be? Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 6.9.4.27 75 long strtol (const char ∗ __nptr, char ∗∗ __endptr, int __base) The strtol() function converts the string in nptr to a long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0. The string may begin with an arbitrary amount of white space (as determined by isspace()) followed by a single optional ’+’ or ’-’ sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is ’0’, in which case it is taken as 8 (octal). The remainder of the string is converted to a long value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter ’A’ in either upper or lower case represents 10, ’B’ represents 11, and so forth, with ’Z’ representing 35.) If endptr is not NULL, strtol() stores the address of the first invalid character in ∗endptr. If there were no digits at all, however, strtol() stores the original value of nptr in endptr. (Thus, if ∗nptr is not ’\0’ but ∗∗endptr is ’\0’ on return, the entire string was valid.) The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If no conversion could be performed, 0 is returned. If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped to LONG_MIN or LONG_MAX, respectively. 6.9.4.28 unsigned long strtoul (const char ∗ __nptr, char ∗∗ __endptr, int __base) The strtoul() function converts the string in nptr to an unsigned long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0. The string may begin with an arbitrary amount of white space (as determined by isspace()) followed by a single optional ’+’ or ’-’ sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is ’0’, in which case it is taken as 8 (octal). The remainder of the string is converted to an unsigned long value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter ’A’ in either upper or lower case represents 10, ’B’ represents 11, and so forth, with ’Z’ representing 35.) If endptr is not NULL, strtoul() stores the address of the first invalid character in ∗endptr. If there were no digits at all, however, strtoul() stores the original value of nptr in endptr. (Thus, if ∗nptr is not ’\0’ but ∗∗endptr is ’\0’ on return, the entire string was valid.) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.9 : General utilities 76 The strtoul() function return either the result of the conversion or, if there was a leading minus sign, the negation of the result of the conversion, unless the original (nonnegated) value would overflow; in the latter case, strtoul() returns ULONG_MAX, and errno is set to ERANGE. If no conversion could be performed, 0 is returned. 6.9.4.29 char∗ ultoa (unsigned long int __val, char ∗ __s, int __radix) Convert an unsigned long integer to a string. The function ultoa() converts the unsigned long integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s. Note: The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 ∗ sizeof (unsigned long int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size. Warning: If the buffer is too small, you risk a buffer overflow. Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after ’9’ will be the letter ’a’. The ultoa() function returns the pointer passed as s. 6.9.4.30 char∗ utoa (unsigned int __val, char ∗ __s, int __radix) Convert an unsigned integer to a string. The function utoa() converts the unsigned integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s. Note: The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 ∗ sizeof (unsigned int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size. Warning: If the buffer is too small, you risk a buffer overflow. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 77 Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after ’9’ will be the letter ’a’. The utoa() function returns the pointer passed as s. 6.9.5 Variable Documentation 6.9.5.1 char∗ __malloc_heap_end malloc() tunable. 6.9.5.2 char∗ __malloc_heap_start malloc() tunable. 6.9.5.3 size_t __malloc_margin malloc() tunable. 6.10 : Strings 6.10.1 Detailed Description #include The string functions perform string operations on NULL terminated strings. Note: If the strings you are working on resident in program space (flash), you will need to use the string functions described in : Program Space Utilities. Defines • #define _FFS(x) Functions • • • • • int ffs (int) __attribute__((const )) int ffsl (long) __attribute__((const )) int ffsll (long long) __attribute__((const )) void ∗ memccpy (void ∗, const void ∗, int, size_t) void ∗ memchr (const void ∗, int, size_t) __ATTR_PURE__ Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 78 int memcmp (const void ∗, const void ∗, size_t) __ATTR_PURE__ void ∗ memcpy (void ∗, const void ∗, size_t) void ∗ memmem (const void ∗, size_t, const void ∗, size_t) __ATTR_PURE__ void ∗ memmove (void ∗, const void ∗, size_t) void ∗ memrchr (const void ∗, int, size_t) __ATTR_PURE__ void ∗ memset (void ∗, int, size_t) int strcasecmp (const char ∗, const char ∗) __ATTR_PURE__ char ∗ strcasestr (const char ∗, const char ∗) __ATTR_PURE__ char ∗ strcat (char ∗, const char ∗) char ∗ strchr (const char ∗, int) __ATTR_PURE__ char ∗ strchrnul (const char ∗, int) __ATTR_PURE__ int strcmp (const char ∗, const char ∗) __ATTR_PURE__ char ∗ strcpy (char ∗, const char ∗) size_t strcspn (const char ∗s, const char ∗reject) __ATTR_PURE__ size_t strlcat (char ∗, const char ∗, size_t) size_t strlcpy (char ∗, const char ∗, size_t) size_t strlen (const char ∗) __ATTR_PURE__ char ∗ strlwr (char ∗) int strncasecmp (const char ∗, const char ∗, size_t) __ATTR_PURE__ char ∗ strncat (char ∗, const char ∗, size_t) int strncmp (const char ∗, const char ∗, size_t) __ATTR_PURE__ char ∗ strncpy (char ∗, const char ∗, size_t) size_t strnlen (const char ∗, size_t) __ATTR_PURE__ char ∗ strpbrk (const char ∗s, const char ∗accept) __ATTR_PURE__ char ∗ strrchr (const char ∗, int) __ATTR_PURE__ char ∗ strrev (char ∗) char ∗ strsep (char ∗∗, const char ∗) size_t strspn (const char ∗s, const char ∗accept) __ATTR_PURE__ char ∗ strstr (const char ∗, const char ∗) __ATTR_PURE__ char ∗ strtok_r (char ∗, const char ∗, char ∗∗) char ∗ strupr (char ∗) 6.10.2 6.10.2.1 Define Documentation #define _FFS(x) This macro finds the first (least significant) bit set in the input value. This macro is very similar to the function ffs() except that it evaluates its argument at compile-time, so it should only be applied to compile-time constant expressions where it will reduce to a constant itself. Application of this macro to expressions that are not constant at compile-time is not recommended, and might result in a huge amount of code generated. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 79 Returns: The _FFS() macro returns the position of the first (least significant) bit set in the word val, or 0 if no bits are set. The least significant bit is position 1. 6.10.3 Function Documentation 6.10.3.1 int ffs (int val) const This function finds the first (least significant) bit set in the input value. Returns: The ffs() function returns the position of the first (least significant) bit set in the word val, or 0 if no bits are set. The least significant bit is position 1. Note: For expressions that are constant at compile time, consider using the _FFS macro instead. 6.10.3.2 int ffsl (long) const Same as ffs(), for an argument of type long. 6.10.3.3 int ffsll (long long) const Same as ffs(), for an argument of type long long. 6.10.3.4 void ∗ memccpy (void ∗ dest, const void ∗ src, int val, size_t len) Copy memory area. The memccpy() function copies no more than len bytes from memory area src to memory area dest, stopping when the character val is found. Returns: The memccpy() function returns a pointer to the next character in dest after val, or NULL if val was not found in the first len characters of src. 6.10.3.5 void ∗ memchr (const void ∗ src, int val, size_t len) Scan memory for a character. The memchr() function scans the first len bytes of the memory area pointed to by src for the character val. The first byte to match val (interpreted as an unsigned character) stops the operation. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 80 Returns: The memchr() function returns a pointer to the matching byte or NULL if the character does not occur in the given memory area. 6.10.3.6 int memcmp (const void ∗ s1, const void ∗ s2, size_t len) Compare memory areas. The memcmp() function compares the first len bytes of the memory areas s1 and s2. The comparision is performed using unsigned char operations. Returns: The memcmp() function returns an integer less than, equal to, or greater than zero if the first len bytes of s1 is found, respectively, to be less than, to match, or be greater than the first len bytes of s2. Note: Be sure to store the result in a 16 bit variable since you may get incorrect results if you use an unsigned char or char due to truncation. Warning: This function is not -mint8 compatible, although if you only care about testing for equality, this function should be safe to use. 6.10.3.7 void ∗ memcpy (void ∗ dest, const void ∗ src, size_t len) Copy a memory area. The memcpy() function copies len bytes from memory area src to memory area dest. The memory areas may not overlap. Use memmove() if the memory areas do overlap. Returns: The memcpy() function returns a pointer to dest. 6.10.3.8 void ∗ memmem (const void ∗ s1, size_t len1, const void ∗ s2, size_t len2) The memmem() function finds the start of the first occurrence of the substring s2 of length len2 in the memory area s1 of length len1. Returns: The memmem() function returns a pointer to the beginning of the substring, or NULL if the substring is not found. If len2 is zero, the function returns s1. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 6.10.3.9 81 void ∗ memmove (void ∗ dest, const void ∗ src, size_t len) Copy memory area. The memmove() function copies len bytes from memory area src to memory area dest. The memory areas may overlap. Returns: The memmove() function returns a pointer to dest. 6.10.3.10 void ∗ memrchr (const void ∗ src, int val, size_t len) The memrchr() function is like the memchr() function, except that it searches backwards from the end of the len bytes pointed to by src instead of forwards from the front. (Glibc, GNU extension.) Returns: The memrchr() function returns a pointer to the matching byte or NULL if the character does not occur in the given memory area. 6.10.3.11 void ∗ memset (void ∗ dest, int val, size_t len) Fill memory with a constant byte. The memset() function fills the first len bytes of the memory area pointed to by dest with the constant byte val. Returns: The memset() function returns a pointer to the memory area dest. 6.10.3.12 int strcasecmp (const char ∗ s1, const char ∗ s2) Compare two strings ignoring case. The strcasecmp() function compares the two strings s1 and s2, ignoring the case of the characters. Returns: The strcasecmp() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. A consequence of the ordering used by strcasecmp() is that if s1 is an initial substring of s2, then s1 is considered to be "less than" s2. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 6.10.3.13 82 char ∗ strcasestr (const char ∗ s1, const char ∗ s2) The strcasestr() function finds the first occurrence of the substring s2 in the string s1. This is like strstr(), except that it ignores case of alphabetic symbols in searching for the substring. (Glibc, GNU extension.) Returns: The strcasestr() function returns a pointer to the beginning of the substring, or NULL if the substring is not found. If s2 points to a string of zero length, the function returns s1. 6.10.3.14 char ∗ strcat (char ∗ dest, const char ∗ src) Concatenate two strings. The strcat() function appends the src string to the dest string overwriting the ’\0’ character at the end of dest, and then adds a terminating ’\0’ character. The strings may not overlap, and the dest string must have enough space for the result. Returns: The strcat() function returns a pointer to the resulting string dest. 6.10.3.15 char ∗ strchr (const char ∗ src, int val) Locate character in string. The strchr() function returns a pointer to the first occurrence of the character val in the string src. Here "character" means "byte" - these functions do not work with wide or multi-byte characters. Returns: The strchr() function returns a pointer to the matched character or NULL if the character is not found. 6.10.3.16 char ∗ strchrnul (const char ∗ s, int c) The strchrnul() function is like strchr() except that if c is not found in s, then it returns a pointer to the null byte at the end of s, rather than NULL. (Glibc, GNU extension.) Returns: The strchrnul() function returns a pointer to the matched character, or a pointer to the null byte at the end of s (i.e., s+strlen(s)) if the character is not found. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 6.10.3.17 83 int strcmp (const char ∗ s1, const char ∗ s2) Compare two strings. The strcmp() function compares the two strings s1 and s2. Returns: The strcmp() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. A consequence of the ordering used by strcmp() is that if s1 is an initial substring of s2, then s1 is considered to be "less than" s2. 6.10.3.18 char ∗ strcpy (char ∗ dest, const char ∗ src) Copy a string. The strcpy() function copies the string pointed to by src (including the terminating ’\0’ character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. Returns: The strcpy() function returns a pointer to the destination string dest. Note: If the destination string of a strcpy() is not large enough (that is, if the programmer was stupid/lazy, and failed to check the size before copying) then anything might happen. Overflowing fixed length strings is a favourite cracker technique. 6.10.3.19 size_t strcspn (const char ∗ s, const char ∗ reject) The strcspn() function calculates the length of the initial segment of s which consists entirely of characters not in reject. Returns: The strcspn() function returns the number of characters in the initial segment of s which are not in the string reject. The terminating zero is not considered as a part of string. 6.10.3.20 size_t strlcat (char ∗ dst, const char ∗ src, size_t siz) Concatenate two strings. Appends src to string dst of size siz (unlike strncat(), siz is the full size of dst, not space left). At most siz-1 characters will be copied. Always NULL terminates (unless siz <= strlen(dst)). Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 84 Returns: The strlcat() function returns strlen(src) + MIN(siz, strlen(initial dst)). If retval >= siz, truncation occurred. 6.10.3.21 size_t strlcpy (char ∗ dst, const char ∗ src, size_t siz) Copy a string. Copy src to string dst of size siz. At most siz-1 characters will be copied. Always NULL terminates (unless siz == 0). Returns: The strlcpy() function returns strlen(src). If retval >= siz, truncation occurred. 6.10.3.22 size_t strlen (const char ∗ src) Calculate the length of a string. The strlen() function calculates the length of the string src, not including the terminating ’\0’ character. Returns: The strlen() function returns the number of characters in src. 6.10.3.23 char ∗ strlwr (char ∗ s) Convert a string to lower case. The strlwr() function will convert a string to lower case. Only the upper case alphabetic characters [A .. Z] are converted. Non-alphabetic characters will not be changed. Returns: The strlwr() function returns a pointer to the converted string. 6.10.3.24 int strncasecmp (const char ∗ s1, const char ∗ s2, size_t len) Compare two strings ignoring case. The strncasecmp() function is similar to strcasecmp(), except it only compares the first len characters of s1. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 85 Returns: The strncasecmp() function returns an integer less than, equal to, or greater than zero if s1 (or the first len bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. A consequence of the ordering used by strncasecmp() is that if s1 is an initial substring of s2, then s1 is considered to be "less than" s2. 6.10.3.25 char ∗ strncat (char ∗ dest, const char ∗ src, size_t len) Concatenate two strings. The strncat() function is similar to strcat(), except that only the first n characters of src are appended to dest. Returns: The strncat() function returns a pointer to the resulting string dest. 6.10.3.26 int strncmp (const char ∗ s1, const char ∗ s2, size_t len) Compare two strings. The strncmp() function is similar to strcmp(), except it only compares the first (at most) n characters of s1 and s2. Returns: The strncmp() function returns an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. 6.10.3.27 char ∗ strncpy (char ∗ dest, const char ∗ src, size_t len) Copy a string. The strncpy() function is similar to strcpy(), except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated. In the case where the length of src is less than that of n, the remainder of dest will be padded with nulls. Returns: The strncpy() function returns a pointer to the destination string dest. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 6.10.3.28 86 size_t strnlen (const char ∗ src, size_t len) Determine the length of a fixed-size string. The strnlen function returns the number of characters in the string pointed to by src, not including the terminating ’\0’ character, but at most len. In doing this, strnlen looks only at the first len characters at src and never beyond src+len. Returns: The strnlen function returns strlen(src), if that is less than len, or len if there is no ’\0’ character among the first len characters pointed to by src. 6.10.3.29 char ∗ strpbrk (const char ∗ s, const char ∗ accept) The strpbrk() function locates the first occurrence in the string s of any of the characters in the string accept. Returns: The strpbrk() function returns a pointer to the character in s that matches one of the characters in accept, or NULL if no such character is found. The terminating zero is not considered as a part of string: if one or both args are empty, the result will NULL. 6.10.3.30 char ∗ strrchr (const char ∗ src, int val) Locate character in string. The strrchr() function returns a pointer to the last occurrence of the character val in the string src. Here "character" means "byte" - these functions do not work with wide or multi-byte characters. Returns: The strrchr() function returns a pointer to the matched character or NULL if the character is not found. 6.10.3.31 char ∗ strrev (char ∗ s) Reverse a string. The strrev() function reverses the order of the string. Returns: The strrev() function returns a pointer to the beginning of the reversed string. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.10 : Strings 6.10.3.32 87 char ∗ strsep (char ∗∗ sp, const char ∗ delim) Parse a string into tokens. The strsep() function locates, in the string referenced by ∗sp, the first occurrence of any character in the string delim (or the terminating ’\0’ character) and replaces it with a ’\0’. The location of the next character after the delimiter character (or NULL, if the end of the string was reached) is stored in ∗sp. An “empty” field, i.e. one caused by two adjacent delimiter characters, can be detected by comparing the location referenced by the pointer returned in ∗sp to ’\0’. Returns: The strsep() function returns a pointer to the original value of ∗sp. If ∗sp is initially NULL, strsep() returns NULL. 6.10.3.33 size_t strspn (const char ∗ s, const char ∗ accept) The strspn() function calculates the length of the initial segment of s which consists entirely of characters in accept. Returns: The strspn() function returns the number of characters in the initial segment of s which consist only of characters from accept. The terminating zero is not considered as a part of string. 6.10.3.34 char ∗ strstr (const char ∗ s1, const char ∗ s2) Locate a substring. The strstr() function finds the first occurrence of the substring s2 in the string s1. The terminating ’\0’ characters are not compared. Returns: The strstr() function returns a pointer to the beginning of the substring, or NULL if the substring is not found. If s2 points to a string of zero length, the function returns s1. 6.10.3.35 char ∗ strtok_r (char ∗ string, const char ∗ delim, char ∗∗ last) Parses the string s into tokens. strtok_r parses the string s into tokens. The first call to strtok_r should have string as its first argument. Subsequent calls should have the first argument set to NULL. If a Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.11 : Bootloader Support Utilities 88 token ends with a delimiter, this delimiting character is overwritten with a ’\0’ and a pointer to the next character is saved for the next call to strtok_r. The delimiter string delim may be different for each call. last is a user allocated char∗ pointer. It must be the same while parsing the same string. strtok_r is a reentrant version of strtok(). Returns: The strtok_r() function returns a pointer to the next token or NULL when no more tokens are found. 6.10.3.36 char ∗ strupr (char ∗ s) Convert a string to upper case. The strupr() function will convert a string to upper case. Only the lower case alphabetic characters [a .. z] are converted. Non-alphabetic characters will not be changed. Returns: The strupr() function returns a pointer to the converted string. The pointer is the same as that passed in since the operation is perform in place. 6.11 : Bootloader Support Utilities 6.11.1 Detailed Description #include #include The macros in this module provide a C language interface to the bootloader support functionality of certain AVR processors. These macros are designed to work with all sizes of flash memory. Global interrupts are not automatically disabled for these macros. It is left up to the programmer to do this. See the code example below. Also see the processor datasheet for caveats on having global interrupts enabled during writing of the Flash. Note: Not all AVR processors provide bootloader support. See your processor datasheet to see if it provides bootloader support. Todo From email with Marek: On smaller devices (all except ATmega64/128), __SPM_REG is in the I/O space, accessible with the shorter "in" and "out" instructions since the boot loader has a limited size, this could be an important optimization. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.11 : Bootloader Support Utilities 89 API Usage Example The following code shows typical usage of the boot API. #include #include #include void boot_program_page (uint32_t page, uint8_t *buf) { uint16_t i; uint8_t sreg; // Disable interrupts. sreg = SREG; cli(); eeprom_busy_wait (); boot_page_erase (page); boot_spm_busy_wait (); // Wait until the memory is erased. for (i=0; i : Bootloader Support Utilities • • • • • • • • • • • • • • • • • 90 #define boot_spm_busy() (__SPM_REG & (uint8_t)_BV(SPMEN)) #define boot_spm_busy_wait() do{}while(boot_spm_busy()) #define GET_LOW_FUSE_BITS (0x0000) #define GET_LOCK_BITS (0x0001) #define GET_EXTENDED_FUSE_BITS (0x0002) #define GET_HIGH_FUSE_BITS (0x0003) #define boot_lock_fuse_bits_get(address) #define boot_page_fill(address, data) __boot_page_fill_normal(address, data) #define boot_page_erase(address) __boot_page_erase_normal(address) #define boot_page_write(address) __boot_page_write_normal(address) #define boot_rww_enable() __boot_rww_enable() #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set(lock_bits) #define boot_page_fill_safe(address, data) #define boot_page_erase_safe(address) #define boot_page_write_safe(address) #define boot_rww_enable_safe() #define boot_lock_bits_set_safe(lock_bits) 6.11.2 Define Documentation 6.11.2.1 #define BV(SPMIE)) boot_is_spm_interrupt() (__SPM_REG & (uint8_t)_- Check if the SPM interrupt is enabled. 6.11.2.2 #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set(lock_bits) Set the bootloader lock bits. Parameters: lock_bits A mask of which Boot Loader Lock Bits to set. Note: In this context, a ’set bit’ will be written to a zero value. Note also that only BLBxx bits can be programmed by this command. For example, to disallow the SPM instruction from writing to the Boot Loader memory section of flash, you would use this macro as such: boot_lock_bits_set (_BV (BLB11)); Note: Like any lock bits, the Boot Loader Lock Bits, once set, cannot be cleared again except by a chip erase which will in turn also erase the boot loader itself. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.11 : Bootloader Support Utilities 6.11.2.3 91 #define boot_lock_bits_set_safe(lock_bits) Value: do { \ boot_spm_busy_wait(); eeprom_busy_wait(); boot_lock_bits_set (lock_bits); } while (0) \ \ \ Same as boot_lock_bits_set() except waits for eeprom and spm operations to complete before setting the lock bits. 6.11.2.4 #define boot_lock_fuse_bits_get(address) Value: (__extension__({ uint8_t __result; __asm__ __volatile__ ( "ldi r30, %3\n\t" "ldi r31, 0\n\t" "sts %1, %2\n\t" "lpm %0, Z\n\t" : "=r" (__result) : "i" (_SFR_MEM_ADDR(__SPM_REG)), "r" ((uint8_t)__BOOT_LOCK_BITS_SET), "M" (address) : "r0", "r30", "r31" ); __result; })) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Read the lock or fuse bits at address. Parameter address can be any of GET_LOW_FUSE_BITS, GET_LOCK_BITS, GET_EXTENDED_FUSE_BITS, or GET_HIGH_FUSE_BITS. Note: The lock and fuse bits returned are the physical values, i.e. a bit returned as 0 means the corresponding fuse or lock bit is programmed. 6.11.2.5 #define boot_page_erase(address) __boot_page_erase_normal(address) Erase the flash page that contains address. Note: address is a byte address in flash, not a word address. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.11 : Bootloader Support Utilities 6.11.2.6 92 #define boot_page_erase_safe(address) Value: do { \ boot_spm_busy_wait(); eeprom_busy_wait(); boot_page_erase (address); } while (0) \ \ \ Same as boot_page_erase() except it waits for eeprom and spm operations to complete before erasing the page. 6.11.2.7 data) #define boot_page_fill(address, data) __boot_page_fill_normal(address, Fill the bootloader temporary page buffer for flash address with data word. Note: The address is a byte address. The data is a word. The AVR writes data to the buffer a word at a time, but addresses the buffer per byte! So, increment your address by 2 between calls, and send 2 data bytes in a word format! The LSB of the data is written to the lower address; the MSB of the data is written to the higher address. 6.11.2.8 #define boot_page_fill_safe(address, data) Value: do { \ boot_spm_busy_wait(); eeprom_busy_wait(); boot_page_fill(address, data); } while (0) \ \ \ Same as boot_page_fill() except it waits for eeprom and spm operations to complete before filling the page. 6.11.2.9 #define boot_page_write(address) __boot_page_write_normal(address) Write the bootloader temporary page buffer to flash page that contains address. Note: address is a byte address in flash, not a word address. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.11 : Bootloader Support Utilities 6.11.2.10 93 #define boot_page_write_safe(address) Value: do { \ boot_spm_busy_wait(); eeprom_busy_wait(); boot_page_write (address); } while (0) \ \ \ Same as boot_page_write() except it waits for eeprom and spm operations to complete before writing the page. 6.11.2.11 #define COMMON_ASB)) boot_rww_busy() (__SPM_REG & (uint8_t)_BV(__- Check if the RWW section is busy. 6.11.2.12 #define boot_rww_enable() __boot_rww_enable() Enable the Read-While-Write memory section. 6.11.2.13 #define boot_rww_enable_safe() Value: do { \ boot_spm_busy_wait(); eeprom_busy_wait(); boot_rww_enable(); } while (0) \ \ \ Same as boot_rww_enable() except waits for eeprom and spm operations to complete before enabling the RWW mameory. 6.11.2.14 #define boot_spm_busy() (__SPM_REG & (uint8_t)_BV(SPMEN)) Check if the SPM instruction is busy. 6.11.2.15 #define boot_spm_busy_wait() do{}while(boot_spm_busy()) Wait while the SPM instruction is busy. 6.11.2.16 #define boot_spm_interrupt_disable() (__SPM_REG &= (uint8_t)∼_BV(SPMIE)) Disable the SPM interrupt. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.12 : EEPROM handling 94 6.11.2.17 #define boot_spm_interrupt_enable() (__SPM_REG |= (uint8_t)_BV(SPMIE)) Enable the SPM interrupt. 6.11.2.18 #define BOOTLOADER_SECTION __attribute__ ((section (".bootloader"))) Used to declare a function or variable to be placed into a new section called .bootloader. This section and its contents can then be relocated to any address (such as the bootloader NRWW area) at link-time. 6.11.2.19 #define GET_EXTENDED_FUSE_BITS (0x0002) address to read the extended fuse bits, using boot_lock_fuse_bits_get 6.11.2.20 #define GET_HIGH_FUSE_BITS (0x0003) address to read the high fuse bits, using boot_lock_fuse_bits_get 6.11.2.21 #define GET_LOCK_BITS (0x0001) address to read the lock bits, using boot_lock_fuse_bits_get 6.11.2.22 #define GET_LOW_FUSE_BITS (0x0000) address to read the low fuse bits, using boot_lock_fuse_bits_get 6.12 : EEPROM handling 6.12.1 Detailed Description #include This header file declares the interface to some simple library routines suitable for handling the data EEPROM contained in the AVR microcontrollers. The implementation uses a simple polled mode interface. Applications that require interrupt-controlled EEPROM access to ensure that no time will be wasted in spinloops will have to deploy their own implementation. Note: All of the read/write functions first make sure the EEPROM is ready to be accessed. Since this may cause long delays if a write operation is still pending, timecritical applications should first poll the EEPROM e. g. using eeprom_is_ready() before attempting any actual I/O. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.12 : EEPROM handling 95 This header file declares inline functions that call the assembler subroutines directly. This prevents that the compiler generates push/pops for the call-clobbered registers. This way also a specific calling convention could be used for the eeprom routines e.g. by passing values in __tmp_reg__, eeprom addresses in X and memory addresses in Z registers. Method is optimized for code size. Presently supported are two locations of the EEPROM register set: 0x1F,0x20,0x21 and 0x1C,0x1D,0x1E (see __EEPROM_REG_LOCATIONS__). As these functions modify IO registers, they are known to be non-reentrant. If any of these functions are used from both, standard and interrupt context, the applications must ensure proper protection (e.g. by disabling interrupts before accessing them). avr-libc declarations • • • • • • #define EEMEM __attribute__((section(".eeprom"))) #define eeprom_is_ready() #define eeprom_busy_wait() do {} while (!eeprom_is_ready()) uint8_t eeprom_read_byte (const uint8_t ∗addr) uint16_t eeprom_read_word (const uint16_t ∗addr) void eeprom_read_block (void ∗pointer_ram, const void ∗pointer_eeprom, size_t n) • void eeprom_write_byte (uint8_t ∗addr, uint8_t value) • void eeprom_write_word (uint16_t ∗addr, uint16_t value) • void eeprom_write_block (const void ∗pointer_ram, void ∗pointer_eeprom, size_t n) IAR C compatibility defines • #define _EEPUT(addr, val) eeprom_write_byte ((uint8_t ∗)(addr), (uint8_t)(val)) • #define _EEGET(var, addr) (var) = eeprom_read_byte ((uint8_t ∗)(addr)) Defines • #define __EEPROM_REG_LOCATIONS__ 1C1D1E 6.12.2 6.12.2.1 Define Documentation #define __EEPROM_REG_LOCATIONS__ 1C1D1E In order to be able to work without a requiring a multilib approach for dealing with controllers having the EEPROM registers at different positions in memory space, the eeprom functions evaluate __EEPROM_REG_LOCATIONS__: It is assumed to be Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.12 : EEPROM handling 96 defined by the device io header and contains 6 uppercase hex digits encoding the addresses of EECR,EEDR and EEAR. First two letters: EECR address. Second two letters: EEDR address. Last two letters: EEAR address. The default 1C1D1E corresponds to the register location that is valid for most controllers. The value of this define symbol is used for appending it to the base name of the assembler functions. 6.12.2.2 #define _EEGET(var, addr) (var) = eeprom_read_byte ((uint8_t ∗)(addr)) Read a byte from EEPROM. Compatibility define for IAR C. 6.12.2.3 #define _EEPUT(addr, val) eeprom_write_byte ((uint8_t ∗)(addr), (uint8_t)(val)) Write a byte to EEPROM. Compatibility define for IAR C. 6.12.2.4 #define EEMEM __attribute__((section(".eeprom"))) Attribute expression causing a variable to be allocated within the .eeprom section. 6.12.2.5 #define eeprom_busy_wait() do {} while (!eeprom_is_ready()) Loops until the eeprom is no longer busy. Returns: Nothing. 6.12.2.6 #define eeprom_is_ready() Returns: 1 if EEPROM is ready for a new read/write operation, 0 if not. 6.12.3 Function Documentation 6.12.3.1 void eeprom_read_block (void ∗ pointer_ram, const void ∗ pointer_eeprom, size_t n) Read a block of n bytes from EEPROM address pointer_eeprom to pointer_ram. For constant n <= 256 bytes a library function is used. For block sizes unknown at compile time or block sizes > 256 an inline loop is expanded. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts 97 6.12.3.2 uint8_t eeprom_read_byte (const uint8_t ∗ addr) Read one byte from EEPROM address addr. 6.12.3.3 uint16_t eeprom_read_word (const uint16_t ∗ addr) Read one 16-bit word (little endian) from EEPROM address addr. 6.12.3.4 void eeprom_write_block (const void ∗ pointer_ram, void ∗ pointer_eeprom, size_t n) Write a block of n bytes to EEPROM address pointer_eeprom from pointer_ram. 6.12.3.5 void eeprom_write_byte (uint8_t ∗ addr, uint8_t value) Write a byte value to EEPROM address addr. 6.12.3.6 void eeprom_write_word (uint16_t ∗ addr, uint16_t value) Write a word value to EEPROM address addr. 6.13 : Interrupts 6.13.1 Detailed Description Note: This discussion of interrupts was originally taken from Rich Neswold’s document. See Acknowledgments. Introduction to avr-libc’s interrupt handling It’s nearly impossible to find compilers that agree on how to handle interrupt code. Since the C language tries to stay away from machine dependent details, each compiler writer is forced to design their method of support. In the AVR-GCC environment, the vector table is predefined to point to interrupt routines with predetermined names. By using the appropriate name, your routine will be called when the corresponding interrupt occurs. The device library provides a set of default interrupt routines, which will get used if you don’t define your own. Patching into the vector table is only one part of the problem. The compiler uses, by convention, a set of registers when it’s normally executing compiler-generated code. It’s important that these registers, as well as the status register, get saved and restored. The extra code needed to do this is enabled by tagging the interrupt function with __attribute__((signal)). Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts 98 These details seem to make interrupt routines a little messy, but all these details are handled by the Interrupt API. An interrupt routine is defined with ISR(). This macro register and mark the routine as an interrupt handler for the specified peripheral. The following is an example definition of a handler for the ADC interrupt. #include ISR(ADC_vect) { // user code here } Refer to the chapter explaining assembler programming for an explanation about interrupt routines written solely in assembler language. Catch-all interrupt vector If an unexpected interrupt occurs (interrupt is enabled and no handler is installed, which usually indicates a bug), then the default action is to reset the device by jumping to the reset vector. You can override this by supplying a function named __vector_default which should be defined with ISR() as such. #include ISR(__vector_default) { // user code here } Nested interrupts The AVR hardware clears the global interrupt flag in SREG before entering an interrupt vector. Thus, normally interrupts will remain disabled inside the handler until the handler exits, where the RETI instruction (that is emitted by the compiler as part of the normal function epilogue for an interrupt handler) will eventually re-enable further interrupts. For that reason, interrupt handlers normally do not nest. For most interrupt handlers, this is the desired behaviour, for some it is even required in order to prevent infinitely recursive interrupts (like UART interrupts, or level-triggered external interrupts). In rare circumstances though it might be desired to re-enable the global interrupt flag as early as possible in the interrupt handler, in order to not defer any other interrupt more than absolutely needed. This could be done using an sei() instruction right at the beginning of the interrupt handler, but this still leaves few instructions inside the compiler-generated function prologue to run with global interrupts disabled. The compiler can be instructed to insert an SEI instruction right at the beginning of an interrupt handler by declaring the handler the following way: void XXX_vect(void) __attribute__((interrupt)); void XXX_vect(void) { ... } Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts 99 where XXX_vect is the name of a valid interrupt vector for the MCU type in question, as explained below. Choosing the vector: Interrupt vector names The interrupt is chosen by supplying one of the symbols in following table. There are currently two different styles present for naming the vectors. One form uses names starting with SIG_, followed by a relatively verbose but arbitrarily chosen name describing the interrupt vector. This has been the only available style in avr-libc up to version 1.2.x. Starting with avr-libc version 1.4.0, a second style of interrupt vector names has been added, where a short phrase for the vector description is followed by _vect. The short phrase matches the vector name as described in the datasheet of the respective device (and in Atmel’s XML files), with spaces replaced by an underscore and other non-alphanumeric characters dropped. Using the suffix _vect is intented to improve portability to other C compilers available for the AVR that use a similar naming convention. The historical naming style might become deprecated in a future release, so it is not recommended for new projects. Note: The ISR() macro cannot really spell-check the argument passed to them. Thus, by misspelling one of the names below in a call to ISR(), a function will be created that, while possibly being usable as an interrupt function, is not actually wired into the interrupt vector table. The compiler will generate a warning if it detects a suspiciously looking name of a ISR() function (i.e. one that after macro replacement does not start with "__vector_"). Vector name Old name vector Description Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen Applicable for device 6.13 : Interrupts Vector name 100 Description Applicable for device ADC_vect Old vector name SIG_ADC ADC Conversion Complete ANALOG_COMP_0_vect ANALOG_COMP_1_vect ANALOG_COMP_2_vect ANALOG_COMP_vect SIG_COMPARATOR0 SIG_COMPARATOR1 SIG_COMPARATOR2 SIG_COMPARATOR Analog parator 0 Analog parator 1 Analog parator 2 Analog parator Com- AT90S2333, AT90S4433, AT90S4434, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny13, ATtiny15, ATtiny26, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90PWM3, AT90PWM2, AT90PWM1 Com- AT90PWM3, AT90PWM2, AT90PWM1 Com- AT90PWM3, AT90PWM2, AT90PWM1 Com- ANA_COMP_vect SIG_Analog COMPARATOR parator CANIT_vect SIG_CAN_INTERRUPT1 AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90S1200, AT90S2313, AT90S2333, AT90S4414, AT90S4433, AT90S4434, AT90S8515, AT90S8535, ATmega16, ATmega161, ATmega162, ATmega163, ATmega32, ATmega323, ATmega8, ATmega8515, ATmega8535, ATtiny11, ATtiny12, ATtiny13, ATtiny15, ATtiny2313, ATtiny26, ATtiny28, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861 AT90CAN128, AT90CAN32, AT90CAN64 Com- CAN Transfer Complete or Error Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts Vector name Old vector name SIG_EEPROM_READY, SIG_EE_READY SIG_EEPROM_READY Description EE_READY_vect SIG_EEPROM_READY EEPROM Ready EXT_INT0_vect INT0_vect SIG_INTERRUPT0 SIG_INTERRUPT0 External Interrupt Request 0 External Interrupt 0 EEPROM_READY_vect EE_RDY_vect 101 Applicable for device ATtiny2313 EEPROM Ready Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen AT90S2333, AT90S4433, AT90S4434, AT90S8535, ATmega16, ATmega161, ATmega162, ATmega163, ATmega32, ATmega323, ATmega8, ATmega8515, ATmega8535, ATtiny12, ATtiny13, ATtiny15, ATtiny26, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861 AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega406, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATtiny24, ATtiny44, ATtiny84 AT90S1200, AT90S2313, AT90S2323, AT90S2333, AT90S2343, AT90S4414, AT90S4433, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega406, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny11, ATtiny12, ATtiny13, ATtiny15, ATtiny22, ATtiny2313, ATtiny26, ATtiny28, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 6.13 : Interrupts Vector name 102 Old vector name SIG_INTERRUPT1 Description Applicable for device External Interrupt Request 1 INT2_vect SIG_INTERRUPT2 External Interrupt Request 2 INT3_vect SIG_INTERRUPT3 External Interrupt Request 3 INT4_vect SIG_INTERRUPT4 External Interrupt Request 4 INT5_vect SIG_INTERRUPT5 External Interrupt Request 5 AT90S2313, AT90S2333, AT90S4414, AT90S4433, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega32, ATmega323, ATmega406, ATmega64, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny2313, ATtiny28, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega32, ATmega323, ATmega406, ATmega64, ATmega8515, ATmega8535, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega406, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 INT1_vect Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts Vector name 103 Old vector name SIG_INTERRUPT6 Description Applicable for device External Interrupt Request 6 INT7_vect SIG_INTERRUPT7 External Interrupt Request 7 IO_PINS_vect SIG_PIN, SIG_PIN_CHANGE SIG_LCD External Interrupt Request 0 AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATtiny11, ATtiny12, ATtiny15, ATtiny26 SIG_PIN SIG_CAN_OVERFLOW1 SIG_PIN_CHANGE0 Low-level Input on Port B CAN Timer Overrun Pin Change Interrupt Request 0 PCINT1_vect SIG_PIN_CHANGE1 Pin Change Interrupt Request 1 PCINT2_vect SIG_PIN_CHANGE2 Pin Change Interrupt Request 2 INT6_vect LCD_vect LOWLEVEL_IO_PINS_vect OVRIT_vect PCINT0_vect LCD Start Frame of Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen ATmega169, ATmega169P, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega649, ATmega6490 ATtiny28 AT90CAN128, AT90CAN32, AT90CAN64 ATmega162, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega406, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny13, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATmega162, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega406, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny24, ATtiny44, ATtiny84, AT90USB162, AT90USB82 ATmega3250, ATmega3290, ATmega3290P, ATmega6450, ATmega6490, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644 6.13 : Interrupts Vector name PCINT3_vect PCINT_vect Old vector name SIG_PIN_CHANGE3 PSC0_CAPT_vect PSC0_EC_vect PSC1_CAPT_vect PSC1_EC_vect PSC2_CAPT_vect PSC2_EC_vect SPI_STC_vect SIG_PIN_CHANGE, SIG_PCINT SIG_PSC0_CAPTURE SIG_PSC0_END_CYCLE SIG_PSC1_CAPTURE SIG_PSC1_END_CYCLE SIG_PSC2_CAPTURE SIG_PSC2_END_CYCLE SIG_SPI SPM_RDY_vect SIG_SPM_READY 104 Description Applicable for device Pin Change Interrupt Request 3 ATmega3250, ATmega3290, ATmega3290P, ATmega6450, ATmega6490, ATmega324P, ATmega164P, ATmega644P, ATmega644 ATtiny2313, ATtiny261, ATtiny461, ATtiny861 PSC0 Capture Event PSC0 End Cycle AT90PWM3, AT90PWM2, AT90PWM1 PSC1 Capture Event PSC1 End Cycle AT90PWM3, AT90PWM2, AT90PWM1 PSC2 Capture Event PSC2 End Cycle AT90PWM3, AT90PWM2, AT90PWM1 Serial Transfer Complete AT90S2333, AT90S4414, AT90S4433, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATmega16, ATmega162, ATmega32, ATmega323, ATmega8, ATmega8515, ATmega8535 Store Program Memory Ready Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen AT90PWM3, AT90PWM2, AT90PWM1 AT90PWM3, AT90PWM2, AT90PWM1 AT90PWM3, AT90PWM2, AT90PWM1 6.13 : Interrupts Vector name SPM_READY_vect TIM0_COMPA_vect TIM0_COMPB_vect TIM0_OVF_vect TIM1_CAPT_vect TIM1_COMPA_vect TIM1_COMPB_vect TIM1_OVF_vect TIMER0_CAPT_vect TIMER0_COMPA_vect TIMER0_COMPB_vect 105 Old vector name SIG_SPM_READY Description Applicable for device Store Program Memory Read SIG_OUTPUT_COMPARE0A SIG_OUTPUT_COMPARE0B SIG_OVERFLOW0 SIG_INPUT_CAPTURE1 SIG_OUTPUT_COMPARE1A SIG_OUTPUT_COMPARE1B SIG_OVERFLOW1 SIG_INPUT_CAPTURE0 SIG_OUTPUT_COMPARE0A Timer/Counter Compare Match A Timer/Counter Compare Match B Timer/Counter0 Overflow Timer/Counter1 Capture Event Timer/Counter1 Compare Match A Timer/Counter1 Compare Match B Timer/Counter1 Overflow ADC Conversion Complete TimerCounter0 Compare Match A AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega406, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATtiny13, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85 SIG_OUTPUT_COMPARE0B, SIG_OUTPUT_COMPARE0_B Timer Counter 0 Compare Match B Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen ATtiny13, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85 ATtiny13, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85 ATtiny24, ATtiny44, ATtiny84 ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85 ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85 ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85 ATtiny261, ATtiny461, ATtiny861 ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny2313, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90PWM3, AT90PWM2, AT90PWM1, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny2313, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 6.13 : Interrupts Vector name 106 Old vector name SIG_OUTPUT_COMPARE0A, SIG_OUTPUT_COMPARE0_A SIG_OUTPUT_COMPARE0 Description Applicable for device Timer/Counter0 Compare Match A AT90PWM3, AT90PWM2, AT90PWM1 Timer/Counter0 Compare Match TIMER0_OVF0_vect TIMER0_OVF_vect SIG_OVERFLOW0 SIG_OVERFLOW0 Timer/Counter0 Overflow Timer/Counter0 Overflow TIMER1_CAPT1_vect SIG_INPUT_CAPTURE1 Timer/Counter1 Capture Event AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8515, ATmega8535 AT90S2313, AT90S2323, AT90S2343, ATtiny22, ATtiny26 AT90S1200, AT90S2333, AT90S4414, AT90S4433, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny11, ATtiny12, ATtiny15, ATtiny2313, ATtiny28, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90S2313 TIMER0_COMP_A_vect TIMER0_COMP_vect Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts Vector name TIMER1_CAPT_vect TIMER1_CMPA_vect TIMER1_CMPB_vect TIMER1_COMP1_vect TIMER1_COMPA_vect 107 Old vector name SIG_INPUT_CAPTURE1 Description Applicable for device Timer/Counter Capture Event SIG_OUTPUT_COMPARE1A SIG_OUTPUT_COMPARE1B SIG_OUTPUT_COMPARE1A SIG_OUTPUT_COMPARE1A Timer/Counter1 Compare Match 1A Timer/Counter1 Compare Match 1B Timer/Counter1 Compare Match AT90S2333, AT90S4414, AT90S4433, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny2313, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATtiny26 Timer/Counter1 Compare Match A Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen ATtiny26 AT90S2313 AT90S4414, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny2313, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 6.13 : Interrupts Vector name 108 Old vector name SIG_OUTPUT_COMPARE1B Description Applicable for device Timer/Counter1 Compare MatchB TIMER1_COMPC_vect SIG_OUTPUT_COMPARE1C Timer/Counter1 Compare Match C TIMER1_COMPD_vect SIG_OUTPUT_COMPARE0D SIG_OUTPUT_COMPARE1A SIG_OVERFLOW1 SIG_OVERFLOW1 Timer/Counter1 Compare Match D Timer/Counter1 Compare Match AT90S4414, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny2313, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATtiny261, ATtiny461, ATtiny861 TIMER1_COMPB_vect TIMER1_COMP_vect TIMER1_OVF1_vect TIMER1_OVF_vect Timer/Counter1 Overflow Timer/Counter1 Overflow Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen AT90S2333, AT90S4433, ATtiny15 AT90S2313, ATtiny26 AT90S2333, AT90S4414, AT90S4433, AT90S4434, AT90S8515, AT90S8535, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8515, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny15, ATtiny2313, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 6.13 : Interrupts Vector name 109 Old vector name SIG_OUTPUT_COMPARE2A Description Applicable for device Timer/Counter2 Compare Match A TIMER2_COMPB_vect SIG_OUTPUT_COMPARE2B Timer/Counter2 Compare Match A TIMER2_COMP_vect SIG_OUTPUT_COMPARE2 Timer/Counter2 Compare Match TIMER2_OVF_vect SIG_OVERFLOW2 Timer/Counter2 Overflow TIMER3_CAPT_vect SIG_INPUT_CAPTURE3 Timer/Counter3 Capture Event TIMER3_COMPA_vect SIG_OUTPUT_COMPARE3A Timer/Counter3 Compare Match A ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90S4434, AT90S8535, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8535 AT90S4434, AT90S8535, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega16, ATmega161, ATmega162, ATmega163, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega162, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega162, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 TIMER2_COMPA_vect Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts Vector name 110 Old vector name SIG_OUTPUT_COMPARE3B Description Applicable for device Timer/Counter3 Compare Match B TIMER3_COMPC_vect SIG_OUTPUT_COMPARE3C Timer/Counter3 Compare Match C TIMER3_OVF_vect SIG_OVERFLOW3 Timer/Counter3 Overflow TIMER4_CAPT_vect TIMER4_COMPA_vect SIG_INPUT_CAPTURE4 SIG_OUTPUT_COMPARE4A SIG_OUTPUT_COMPARE4B SIG_OUTPUT_COMPARE4C SIG_OVERFLOW4 SIG_INPUT_CAPTURE5 SIG_OUTPUT_COMPARE5A SIG_OUTPUT_COMPARE5B SIG_OUTPUT_COMPARE5C SIG_OVERFLOW5 SIG_2WIRE_SERIAL Timer/Counter4 Capture Event Timer/Counter4 Compare Match A Timer/Counter4 Compare Match B Timer/Counter4 Compare Match C Timer/Counter4 Overflow Timer/Counter5 Capture Event Timer/Counter5 Compare Match A Timer/Counter5 Compare Match B Timer/Counter5 Compare Match C Timer/Counter5 Overflow 2-wire Serial Interface AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega162, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega162, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 TIMER3_COMPB_vect TIMER4_COMPB_vect TIMER4_COMPC_vect TIMER4_OVF_vect TIMER5_CAPT_vect TIMER5_COMPA_vect TIMER5_COMPB_vect TIMER5_COMPC_vect TIMER5_OVF_vect TWI_vect Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega16, ATmega163, ATmega32, ATmega323, ATmega406, ATmega64, ATmega8, ATmega8535, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 6.13 : Interrupts Vector name TXDONE_vect Old vector name SIG_TXDONE 111 Description Applicable for device Transmission Done, Bit Timer Flag 2 Interrupt Transmit Buffer Empty, Bit Itmer Flag 0 Interrupt UART0, Rx Complete AT86RF401 UART0, Complete Tx ATmega161 UART0 Data Register Empty ATmega161 UART1, Complete Rx ATmega161 UART1, Complete Tx ATmega161 UART1 Data Register Empty ATmega161 UART, Rx Complete AT90S2313, AT90S2333, AT90S4433, AT90S4434, AT90S8535, ATmega103, ATmega8515 AT90S2313, AT90S2333, AT90S4433, AT90S4434, AT90S8535, ATmega103, ATmega8515 AT90S2313, AT90S2333, AT90S4433, AT90S4434, AT90S8535, ATmega103, ATmega8515 ATmega162 TXEMPTY_vect SIG_TXBE UART0_RX_vect UART_RX_vect SIG_UART0_RECV SIG_UART0_TRANS SIG_UART0_DATA SIG_UART1_RECV SIG_UART1_TRANS SIG_UART1_DATA SIG_UART_RECV UART_TX_vect SIG_UART_TRANS UART, Tx Complete UART_UDRE_vect SIG_UART_DATA UART Data Register Empty USART0_RXC_vect SIG_USART0_RECV SIG_UART0_RECV USART0, Complete Rx USART0, Complete Rx SIG_USART0_TRANS USART0, Complete Tx UART0_TX_vect UART0_UDRE_vect UART1_RX_vect UART1_TX_vect UART1_UDRE_vect USART0_RX_vect USART0_TXC_vect Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen AT86RF401 ATmega161 AT90S4414, AT90S8515, ATmega163, AT90S4414, AT90S8515, ATmega163, AT90S4414, AT90S8515, ATmega163, AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega329, ATmega329P, ATmega64, ATmega645, ATmega649, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644 ATmega162 6.13 : Interrupts Vector name 112 Old vector name SIG_UART0_TRANS Description USART0_UDRE_vect SIG_UART0_DATA USART0 Data Register Empty USART1_RXC_vect SIG_USART1_RECV SIG_UART1_RECV USART1, Complete Rx USART1, Complete Rx SIG_USART1_TRANS SIG_UART1_TRANS USART1, Complete Tx USART1, Complete Tx USART1_UDRE_vect SIG_UART1_DATA USART1, Data Register Empty USART2_RX_vect SIG_USART2_RECV SIG_USART2_TRANS USART2, Complete Rx USART2, Complete Tx USART0_TX_vect USART1_RX_vect USART1_TXC_vect USART1_TX_vect USART2_TX_vect USART0, Complete Applicable for device Tx Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega162, ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega329, ATmega329P, ATmega64, ATmega645, ATmega649, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644 ATmega162 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATmega162 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 AT90CAN128, AT90CAN32, AT90CAN64, ATmega128, ATmega162, ATmega64, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 6.13 : Interrupts Vector name 113 Old vector name SIG_USART2_DATA SIG_USART3_RECV SIG_USART3_TRANS SIG_USART3_DATA SIG_USART_RECV, SIG_UART_RECV SIG_USART_RECV, SIG_UART_RECV Description Applicable for device USART2 Data register Empty ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 USART3, Complete Rx ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 USART3, Complete Tx ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 USART3 Data register Empty ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561 USART, Complete Rx ATmega16, ATmega32, ATmega323, ATmega8 USART, Complete Rx SIG_USART_TRANS, SIG_UART_TRANS SIG_USART_TRANS, SIG_UART_TRANS SIG_USART_DATA, SIG_UART_DATA USART, Complete Tx AT90PWM3, AT90PWM2, AT90PWM1, ATmega3250, ATmega3290, ATmega3290P, ATmega6450, ATmega6490, ATmega8535, ATmega168, ATmega48, ATmega88, ATtiny2313 ATmega16, ATmega32, ATmega323, ATmega8 USART, Complete Tx AT90PWM3, AT90PWM2, AT90PWM1, ATmega8535, ATmega168, ATmega48, ATmega88, ATtiny2313 USART Data Register Empty USI_OVERFLOW_vect SIG_USI_OVERFLOW USI Overflow USI_OVF_vect SIG_USI_OVERFLOW USI Overflow USI_START_vect SIG_USI_START USI Start Condition USI_STRT_vect SIG_USI_START USI Start AT90PWM3, AT90PWM2, AT90PWM1, ATmega16, ATmega32, ATmega323, ATmega3250, ATmega3290, ATmega3290P, ATmega6450, ATmega6490, ATmega8, ATmega8535, ATmega168, ATmega48, ATmega88, ATtiny2313 ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega645, ATmega6450, ATmega649, ATmega6490, ATtiny2313 ATtiny26, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861 ATmega165, ATmega165P, ATmega169, ATmega169P, ATmega325, ATmega3250, ATmega329, ATmega329P, ATmega3290, ATmega3290P, ATmega645, ATmega6450, ATmega649, ATmega6490, ATtiny2313, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861 ATtiny26 USART2_UDRE_vect USART3_RX_vect USART3_TX_vect USART3_UDRE_vect USART_RXC_vect USART_RX_vect USART_TXC_vect USART_TX_vect USART_UDRE_vect Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts Vector name USI_STR_vect WATCHDOG_vect WDT_OVERFLOW_vect WDT_vect Old vector name SIG_USI_START SIG_WATCHDOG_TIMEOUT SIG_WATCHDOG_TIMEOUT, SIG_WDT_OVERFLOW SIG_WDT, SIG_WATCHDOG_TIMEOUT 114 Description Applicable for device USI START ATtiny24, ATtiny44, ATtiny84 Watchdog Timeout ATtiny24, ATtiny44, ATtiny84 Watchdog Timer Overflow ATtiny2313 Watchdog Timeout Interrupt AT90PWM3, AT90PWM2, AT90PWM1, ATmega406, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny13, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861, AT90USB162, AT90USB82, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646 Global manipulation of the interrupt flag The global interrupt flag is maintained in the I bit of the status register (SREG). • #define sei() • #define cli() Macros for writing interrupt handler functions • • • • #define ISR(vector) #define SIGNAL(vector) #define EMPTY_INTERRUPT(vector) #define ISR_ALIAS(vector, target_vector) 6.13.2 6.13.2.1 Define Documentation #define cli() #include Disables all interrupts by clearing the global interrupt mask. This function actually compiles into a single line of assembly, so there is no function call overhead. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.13 : Interrupts 6.13.2.2 115 #define EMPTY_INTERRUPT(vector) #include Defines an empty interrupt handler function. This will not generate any prolog or epilog code and will only return from the ISR. Do not define a function body as this will define it for you. Example: EMPTY_INTERRUPT(ADC_vect); 6.13.2.3 #define ISR(vector) #include Introduces an interrupt handler function (interrupt service routine) that runs with global interrupts initially disabled. vector must be one of the interrupt vector names that are valid for the particular MCU type. 6.13.2.4 #define ISR_ALIAS(vector, target_vector) #include Defines vector to point to the same interrupt vector as target_vector. That way, a single interrupt vector implementation can be used to serve several interrupt sources. Do not define a function body. Note: This requires a recent version of AVR-GCC to work (GCC 4.2 or patched GCC 4.1.x). Example: ISR(INT0_vect) { PORTB = 42; } ISR_ALIAS(INT1_vect, INT0_vect); Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.13.2.5 116 #define sei() #include Enables interrupts by setting the global interrupt mask. This function actually compiles into a single line of assembly, so there is no function call overhead. 6.13.2.6 #define SIGNAL(vector) #include Introduces an interrupt handler function that runs with global interrupts initially disabled. This is the same as the ISR macro. Deprecated Do not use SIGNAL() in new code. Use ISR() instead. 6.14 : Program Space Utilities 6.14.1 Detailed Description #include #include The functions in this module provide interfaces for a program to access data stored in program space (flash memory) of the device. In order to use these functions, the target device must support either the LPM or ELPM instructions. Note: These functions are an attempt to provide some compatibility with header files that come with IAR C, to make porting applications between different compilers easier. This is not 100% compatibility though (GCC does not have full support for multiple address spaces yet). If you are working with strings which are completely based in ram, use the standard string functions described in : Strings. If possible, put your constant tables in the lower 64 KB and use pgm_read_byte_near() or pgm_read_word_near() instead of pgm_read_byte_far() or pgm_read_word_far() since it is more efficient that way, and you can still use the upper 64K for executable code. All functions that are suffixed with a _P require their arguments to be in the lower 64 KB of the flash ROM, as they do not use ELPM instructions. This is normally not a big concern as the linker setup arranges any Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 117 program space constants declared using the macros from this header file so they are placed right after the interrupt vectors, and in front of any executable code. However, it can become a problem if there are too many of these constants, or for bootloaders on devices with more than 64 KB of ROM. All these functions will not work in that situation. Defines • • • • • • • • • • • • • #define PROGMEM __ATTR_PROGMEM__ #define PSTR(s) ((const PROGMEM char ∗)(s)) #define pgm_read_byte_near(address_short) __LPM((uint16_t)(address_short)) #define pgm_read_word_near(address_short) __LPM_word((uint16_t)(address_short)) #define pgm_read_dword_near(address_short) __LPM_dword((uint16_t)(address_short)) #define pgm_read_byte_far(address_long) __ELPM((uint32_t)(address_long)) #define pgm_read_word_far(address_long) __ELPM_word((uint32_t)(address_long)) #define pgm_read_dword_far(address_long) __ELPM_dword((uint32_t)(address_long)) #define pgm_read_byte(address_short) pgm_read_byte_near(address_short) #define pgm_read_word(address_short) pgm_read_word_near(address_short) #define pgm_read_dword(address_short) pgm_read_dword_near(address_short) #define PGM_P const prog_char ∗ #define PGM_VOID_P const prog_void ∗ Typedefs • • • • • • • • • • • typedef void PROGMEM prog_void typedef char PROGMEM prog_char typedef unsigned char PROGMEM prog_uchar typedef int8_t PROGMEM prog_int8_t typedef uint8_t PROGMEM prog_uint8_t typedef int16_t PROGMEM prog_int16_t typedef uint16_t PROGMEM prog_uint16_t typedef int32_t PROGMEM prog_int32_t typedef uint32_t PROGMEM prog_uint32_t typedef int64_t PROGMEM prog_int64_t typedef uint64_t PROGMEM prog_uint64_t Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 118 Functions • PGM_VOID_P memchr_P (PGM_VOID_P s, int val, size_t len) __ATTR_CONST__ • int memcmp_P (const void ∗, PGM_VOID_P, size_t) __ATTR_PURE__ • void ∗ memcpy_P (void ∗, PGM_VOID_P, size_t) • PGM_VOID_P memrchr_P (PGM_VOID_P s, int val, size_t len) __ATTR_CONST__ • int strcasecmp_P (const char ∗, PGM_P) __ATTR_PURE__ • char ∗ strcat_P (char ∗, PGM_P) • PGM_P strchr_P (PGM_P s, int val) __ATTR_CONST__ • PGM_P strchrnul_P (PGM_P s, int val) __ATTR_CONST__ • int strcmp_P (const char ∗, PGM_P) __ATTR_PURE__ • char ∗ strcpy_P (char ∗, PGM_P) • size_t strcspn_P (const char ∗s, PGM_P reject) __ATTR_PURE__ • size_t strlcat_P (char ∗, PGM_P, size_t) • size_t strlcpy_P (char ∗, PGM_P, size_t) • size_t strlen_P (PGM_P) __ATTR_CONST__ • int strncasecmp_P (const char ∗, PGM_P, size_t) __ATTR_PURE__ • char ∗ strncat_P (char ∗, PGM_P, size_t) • int strncmp_P (const char ∗, PGM_P, size_t) __ATTR_PURE__ • char ∗ strncpy_P (char ∗, PGM_P, size_t) • size_t strnlen_P (PGM_P, size_t) __ATTR_CONST__ • char ∗ strpbrk_P (const char ∗s, PGM_P accept) __ATTR_PURE__ • PGM_P strrchr_P (PGM_P s, int val) __ATTR_CONST__ • char ∗ strsep_P (char ∗∗sp, PGM_P delim) • size_t strspn_P (const char ∗s, PGM_P accept) __ATTR_PURE__ • char ∗ strstr_P (const char ∗, PGM_P) __ATTR_PURE__ • void ∗ memmem_P (const void ∗, size_t, PGM_VOID_P, size_t) __ATTR_PURE__ • char ∗ strcasestr_P (const char ∗, PGM_P) __ATTR_PURE__ 6.14.2 6.14.2.1 Define Documentation #define PGM_P const prog_char ∗ Used to declare a variable that is a pointer to a string in program space. 6.14.2.2 short) #define pgm_read_byte(address_short) pgm_read_byte_near(address_- Read a byte from the program space with a 16-bit (near) address. Note: The address is a byte address. The address is in the program space. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.14.2.3 #define t)(address_long)) 119 pgm_read_byte_far(address_long) __ELPM((uint32_- Read a byte from the program space with a 32-bit (far) address. Note: The address is a byte address. The address is in the program space. 6.14.2.4 #define t)(address_short)) pgm_read_byte_near(address_short) __LPM((uint16_- Read a byte from the program space with a 16-bit (near) address. Note: The address is a byte address. The address is in the program space. 6.14.2.5 #define near(address_short) pgm_read_dword(address_short) pgm_read_dword_- Read a double word from the program space with a 16-bit (near) address. Note: The address is a byte address. The address is in the program space. 6.14.2.6 #define pgm_read_dword_far(address_long) dword((uint32_t)(address_long)) __ELPM_- Read a double word from the program space with a 32-bit (far) address. Note: The address is a byte address. The address is in the program space. 6.14.2.7 #define pgm_read_dword_near(address_short) dword((uint16_t)(address_short)) Read a double word from the program space with a 16-bit (near) address. Note: The address is a byte address. The address is in the program space. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen __LPM_- 6.14 : Program Space Utilities 6.14.2.8 #define near(address_short) pgm_read_word(address_short) 120 pgm_read_word_- Read a word from the program space with a 16-bit (near) address. Note: The address is a byte address. The address is in the program space. 6.14.2.9 #define pgm_read_word_far(address_long) __ELPM_word((uint32_t)(address_long)) Read a word from the program space with a 32-bit (far) address. Note: The address is a byte address. The address is in the program space. 6.14.2.10 #define pgm_read_word_near(address_short) __LPM_word((uint16_t)(address_short)) Read a word from the program space with a 16-bit (near) address. Note: The address is a byte address. The address is in the program space. 6.14.2.11 #define PGM_VOID_P const prog_void ∗ Used to declare a generic pointer to an object in program space. 6.14.2.12 #define PROGMEM __ATTR_PROGMEM__ Attribute to use in order to declare an object being located in flash ROM. 6.14.2.13 #define PSTR(s) ((const PROGMEM char ∗)(s)) Used to declare a static pointer to a string in program space. 6.14.3 Typedef Documentation 6.14.3.1 prog_char Type of a "char" object located in flash ROM. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.14.3.2 prog_int16_t Type of an "int16_t" object located in flash ROM. 6.14.3.3 prog_int32_t Type of an "int32_t" object located in flash ROM. 6.14.3.4 prog_int64_t Type of an "int64_t" object located in flash ROM. Note: This type is not available when the compiler option -mint8 is in effect. 6.14.3.5 prog_int8_t Type of an "int8_t" object located in flash ROM. 6.14.3.6 prog_uchar Type of an "unsigned char" object located in flash ROM. 6.14.3.7 prog_uint16_t Type of an "uint16_t" object located in flash ROM. 6.14.3.8 prog_uint32_t Type of an "uint32_t" object located in flash ROM. 6.14.3.9 prog_uint64_t Type of an "uint64_t" object located in flash ROM. Note: This type is not available when the compiler option -mint8 is in effect. 6.14.3.10 prog_uint8_t Type of an "uint8_t" object located in flash ROM. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 121 6.14 : Program Space Utilities 122 6.14.3.11 prog_void Type of a "void" object located in flash ROM. Does not make much sense by itself, but can be used to declare a "void ∗" object in flash ROM. 6.14.4 Function Documentation 6.14.4.1 PGM_VOID_P memchr_P (PGM_VOID_P s, int val, size_t len) Scan flash memory for a character. The memchr_P() function scans the first len bytes of the flash memory area pointed to by s for the character val. The first byte to match val (interpreted as an unsigned character) stops the operation. Returns: The memchr_P() function returns a pointer to the matching byte or NULL if the character does not occur in the given memory area. 6.14.4.2 int memcmp_P (const void ∗ s1, PGM_VOID_P s2, size_t len) Compare memory areas. The memcmp_P() function compares the first len bytes of the memory areas s1 and flash s2. The comparision is performed using unsigned char operations. Returns: The memcmp_P() function returns an integer less than, equal to, or greater than zero if the first len bytes of s1 is found, respectively, to be less than, to match, or be greater than the first len bytes of s2. 6.14.4.3 void ∗ memcpy_P (void ∗ dest, PGM_VOID_P src, size_t n) The memcpy_P() function is similar to memcpy(), except the src string resides in program space. Returns: The memcpy_P() function returns a pointer to dest. 6.14.4.4 void ∗ memmem_P (const void ∗ s1, size_t len1, PGM_VOID_P s2, size_t len2) The memmem_P() function is similar to memmem() except that s2 is pointer to a string in program space. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.14.4.5 123 PGM_VOID_P memrchr_P (PGM_VOID_P src, int val, size_t len) The memrchr_P() function is like the memchr_P() function, except that it searches backwards from the end of the len bytes pointed to by src instead of forwards from the front. (Glibc, GNU extension.) Returns: The memrchr_P() function returns a pointer to the matching byte or NULL if the character does not occur in the given memory area. 6.14.4.6 int strcasecmp_P (const char ∗ s1, PGM_P s2) Compare two strings ignoring case. The strcasecmp_P() function compares the two strings s1 and s2, ignoring the case of the characters. Parameters: s1 A pointer to a string in the devices SRAM. s2 A pointer to a string in the devices Flash. Returns: The strcasecmp_P() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. A consequence of the ordering used by strcasecmp_P() is that if s1 is an initial substring of s2, then s1 is considered to be "less than" s2. 6.14.4.7 char ∗ strcasestr_P (const char ∗ s1, PGM_P s2) This funtion is similar to strcasestr() except that s2 is pointer to a string in program space. 6.14.4.8 char ∗ strcat_P (char ∗ dest, PGM_P src) The strcat_P() function is similar to strcat() except that the src string must be located in program space (flash). Returns: The strcat() function returns a pointer to the resulting string dest. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.14.4.9 124 PGM_P strchr_P (PGM_P s, int val) Locate character in program space string. The strchr_P() function locates the first occurrence of val (converted to a char) in the string pointed to by s in program space. The terminating null character is considered to be part of the string. The strchr_P() function is similar to strchr() except that s is pointer to a string in program space. Returns: The strchr_P() function returns a pointer to the matched character or NULL if the character is not found. 6.14.4.10 PGM_P strchrnul_P (PGM_P s, int c) The strchrnul_P() function is like strchr_P() except that if c is not found in s, then it returns a pointer to the null byte at the end of s, rather than NULL. (Glibc, GNU extension.) Returns: The strchrnul_P() function returns a pointer to the matched character, or a pointer to the null byte at the end of s (i.e., s+strlen(s)) if the character is not found. 6.14.4.11 int strcmp_P (const char ∗ s1, PGM_P s2) The strcmp_P() function is similar to strcmp() except that s2 is pointer to a string in program space. Returns: The strcmp_P() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. A consequence of the ordering used by strcmp_P() is that if s1 is an initial substring of s2, then s1 is considered to be "less than" s2. 6.14.4.12 char ∗ strcpy_P (char ∗ dest, PGM_P src) The strcpy_P() function is similar to strcpy() except that src is a pointer to a string in program space. Returns: The strcpy_P() function returns a pointer to the destination string dest. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.14.4.13 125 size_t strcspn_P (const char ∗ s, PGM_P reject) The strcspn_P() function calculates the length of the initial segment of s which consists entirely of characters not in reject. This function is similar to strcspn() except that reject is a pointer to a string in program space. Returns: The strcspn_P() function returns the number of characters in the initial segment of s which are not in the string reject. The terminating zero is not considered as a part of string. 6.14.4.14 size_t strlcat_P (char ∗ dst, PGM_P, size_t siz) Concatenate two strings. The strlcat_P() function is similar to strlcat(), except that the src string must be located in program space (flash). Appends src to string dst of size siz (unlike strncat(), siz is the full size of dst, not space left). At most siz-1 characters will be copied. Always NULL terminates (unless siz <= strlen(dst)). Returns: The strlcat_P() function returns strlen(src) + MIN(siz, strlen(initial dst)). If retval >= siz, truncation occurred. 6.14.4.15 size_t strlcpy_P (char ∗ dst, PGM_P, size_t siz) Copy a string from progmem to RAM. Copy src to string dst of size siz. At most siz-1 characters will be copied. Always NULL terminates (unless siz == 0). Returns: The strlcpy_P() function returns strlen(src). If retval >= siz, truncation occurred. 6.14.4.16 size_t strlen_P (PGM_P src) The strlen_P() function is similar to strlen(), except that src is a pointer to a string in program space. Returns: The strlen() function returns the number of characters in src. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.14.4.17 126 int strncasecmp_P (const char ∗ s1, PGM_P s2, size_t n) Compare two strings ignoring case. The strncasecmp_P() function is similar to strcasecmp_P(), except it only compares the first n characters of s1. Parameters: s1 A pointer to a string in the devices SRAM. s2 A pointer to a string in the devices Flash. n The maximum number of bytes to compare. Returns: The strncasecmp_P() function returns an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. A consequence of the ordering used by strncasecmp_P() is that if s1 is an initial substring of s2, then s1 is considered to be "less than" s2. 6.14.4.18 char ∗ strncat_P (char ∗ dest, PGM_P src, size_t len) Concatenate two strings. The strncat_P() function is similar to strncat(), except that the src string must be located in program space (flash). Returns: The strncat_P() function returns a pointer to the resulting string dest. 6.14.4.19 int strncmp_P (const char ∗ s1, PGM_P s2, size_t n) The strncmp_P() function is similar to strcmp_P() except it only compares the first (at most) n characters of s1 and s2. Returns: The strncmp_P() function returns an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.14 : Program Space Utilities 6.14.4.20 127 char ∗ strncpy_P (char ∗ dest, PGM_P src, size_t n) The strncpy_P() function is similar to strcpy_P() except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated. In the case where the length of src is less than that of n, the remainder of dest will be padded with nulls. Returns: The strncpy_P() function returns a pointer to the destination string dest. 6.14.4.21 size_t strnlen_P (PGM_P src, size_t len) Determine the length of a fixed-size string. The strnlen_P() function is similar to strnlen(), except that src is a pointer to a string in program space. Returns: The strnlen_P function returns strlen_P(src), if that is less than len, or len if there is no ’\0’ character among the first len characters pointed to by src. 6.14.4.22 char ∗ strpbrk_P (const char ∗ s, PGM_P accept) The strpbrk_P() function locates the first occurrence in the string s of any of the characters in the flash string accept. This function is similar to strpbrk() except that accept is a pointer to a string in program space. Returns: The strpbrk_P() function returns a pointer to the character in s that matches one of the characters in accept, or NULL if no such character is found. The terminating zero is not considered as a part of string: if one or both args are empty, the result will NULL. 6.14.4.23 PGM_P strrchr_P (PGM_P s, int val) Locate character in string. The strrchr_P() function returns a pointer to the last occurrence of the character val in the flash string s. Returns: The strrchr_P() function returns a pointer to the matched character or NULL if the character is not found. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.15 : Power Reduction Management 6.14.4.24 128 char ∗ strsep_P (char ∗∗ sp, PGM_P delim) Parse a string into tokens. The strsep_P() function locates, in the string referenced by ∗sp, the first occurrence of any character in the string delim (or the terminating ’\0’ character) and replaces it with a ’\0’. The location of the next character after the delimiter character (or NULL, if the end of the string was reached) is stored in ∗sp. An “empty” field, i.e. one caused by two adjacent delimiter characters, can be detected by comparing the location referenced by the pointer returned in ∗sp to ’\0’. This function is similar to strsep() except that delim is a pointer to a string in program space. Returns: The strsep_P() function returns a pointer to the original value of ∗sp. If ∗sp is initially NULL, strsep_P() returns NULL. 6.14.4.25 size_t strspn_P (const char ∗ s, PGM_P accept) The strspn_P() function calculates the length of the initial segment of s which consists entirely of characters in accept. This function is similar to strspn() except that accept is a pointer to a string in program space. Returns: The strspn_P() function returns the number of characters in the initial segment of s which consist only of characters from accept. The terminating zero is not considered as a part of string. 6.14.4.26 char ∗ strstr_P (const char ∗ s1, PGM_P s2) Locate a substring. The strstr_P() function finds the first occurrence of the substring s2 in the string s1. The terminating ’\0’ characters are not compared. The strstr_P() function is similar to strstr() except that s2 is pointer to a string in program space. Returns: The strstr_P() function returns a pointer to the beginning of the substring, or NULL if the substring is not found. If s2 points to a string of zero length, the function returns s1. 6.15 : Power Reduction Management #include Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.15 : Power Reduction Management 129 Many AVRs contain a Power Reduction Register (PRR) or Registers (PRRx) that allow you to reduce power consumption by disabling or enabling various on-board peripherals as needed. There are many macros in this header file that provide an easy interface to enable or disable on-board peripherals to reduce power. See the table below. Note: Not all AVR devices have a Power Reduction Register (for example the ATmega128). On those devices without a Power Reduction Register, these macros are not available. Not all AVR devices contain the same peripherals (for example, the LCD interface), or they will be named differently (for example, USART and USART0). Please consult your device’s datasheet, or the header file, to find out which macros are applicable to your device. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.15 : Power Reduction Management 130 Power Macro Description Applicable for device power_adc_enable() Enable the Analog to Digital Converter module. ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, AT90PWM1, AT90PWM2, AT90PWM3, ATmega165, ATmega165P, ATmega325, ATmega3250, ATmega645, ATmega6450, ATmega169, ATmega169P, ATmega329, ATmega3290, ATmega649, ATmega6490, ATmega164P, ATmega324P, ATmega644, ATmega48, ATmega88, ATmega168, ATtiny24, ATtiny44, ATtiny84, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861 power_adc_disable() Disable the Analog to Digital Converter module. ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, AT90PWM1, AT90PWM2, AT90PWM3, ATmega165, ATmega165P, ATmega325, ATmega3250, ATmega645, ATmega6450, ATmega169, ATmega169P, ATmega329, ATmega3290, ATmega649, ATmega6490, ATmega164P, ATmega324P, ATmega644, ATmega48, ATmega88, ATmega168, ATtiny24, ATtiny44, ATtiny84, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861 power_lcd_enable() Enable the LCD module. ATmega169, ATmega169P, ATmega329, ATmega3290, ATmega649, ATmega6490 power_lcd_disable(). Disable the LCD module. ATmega169, ATmega169P, ATmega329, ATmega3290, ATmega649, ATmega6490 power_psc0_enable() Enable the Power Stage Controller 0 module. AT90PWM1, AT90PWM2, AT90PWM3 power_psc0_disable() Disable the Power Stage Controller 0 module. AT90PWM1, AT90PWM2, AT90PWM3 power_psc1_enable() Enable the Power Stage Controller 1 module. AT90PWM1, AT90PWM2, AT90PWM3 power_psc1_disable() Disable the Power Stage AT90PWM1, AT90PWM2, AT90PWM3 Generated on Tue May 15 14:56:11 2007 Controller for avr-libc 1bymodule. Doxygen power_psc2_enable() Enable the Power Stage Controller 2 module. AT90PWM1, AT90PWM2, AT90PWM3 power_psc2_disable() Disable the Power Stage Controller 2 module. AT90PWM1, AT90PWM2, AT90PWM3 power_spi_enable() Enable the Serial Peripheral Interface module. ATmega640, ATmega1280, ATmega1281, ATmega2560, Additional notes from 6.16 131 Some of the newer AVRs contain a System Clock Prescale Register (CLKPR) that allows you to decrease the system clock frequency and the power consumption when the need for processing power is low. Below are two macros and an enumerated type that can be used to interface to the Clock Prescale Register. Note: Not all AVR devices have a Clock Prescale Register. On those devices without a Clock Prescale Register, these macros are not available. typedef enum { clock_div_1 = 0, clock_div_2 = 1, clock_div_4 = 2, clock_div_8 = 3, clock_div_16 = 4, clock_div_32 = 5, clock_div_64 = 6, clock_div_128 = 7, clock_div_256 = 8 } clock_div_t; Clock prescaler setting enumerations. clock_prescale_set(x) Set the clock prescaler register select bits, selecting a system clock division setting. They type of x is clock_div_t. clock_prescale_get() Gets and returns the clock prescaler register setting. The return type is clock_div_t. 6.16 Additional notes from The file is included by all of the files, which use macros defined here to make the special function register definitions look like C variables or simple constants, depending on the _SFR_ASM_COMPAT define. Some examples from to show how to define such macros: #define #define #define #define #define PORTA EEAR UDR0 TCNT3 CANIDT _SFR_IO8(0x02) _SFR_IO16(0x21) _SFR_MEM8(0xC6) _SFR_MEM16(0x94) _SFR_MEM32(0xF0) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.16 Additional notes from 132 If _SFR_ASM_COMPAT is not defined, C programs can use names like PORTA directly in C expressions (also on the left side of assignment operators) and GCC will do the right thing (use short I/O instructions if possible). The __SFR_OFFSET definition is not used in any way in this case. Define _SFR_ASM_COMPAT as 1 to make these names work as simple constants (addresses of the I/O registers). This is necessary when included in preprocessed assembler (∗.S) source files, so it is done automatically if __ASSEMBLER__ is defined. By default, all addresses are defined as if they were memory addresses (used in lds/sts instructions). To use these addresses in in/out instructions, you must subtract 0x20 from them. For more backwards compatibility, insert the following at the start of your old assembler source file: #define __SFR_OFFSET 0 This automatically subtracts 0x20 from I/O space addresses, but it’s a hack, so it is recommended to change your source: wrap such addresses in macros defined here, as shown below. After this is done, the __SFR_OFFSET definition is no longer necessary and can be removed. Real example - this code could be used in a boot loader that is portable between devices with SPMCR at different addresses. : #define SPMCR _SFR_IO8(0x37) : #define SPMCR _SFR_MEM8(0x68) #if _SFR_IO_REG_P(SPMCR) out _SFR_IO_ADDR(SPMCR), r24 #else sts _SFR_MEM_ADDR(SPMCR), r24 #endif You can use the in/out/cbi/sbi/sbic/sbis instructions, without the _SFR_IO_REG_P test, if you know that the register is in the I/O space (as with SREG, for example). If it isn’t, the assembler will complain (I/O address out of range 0...0x3f), so this should be fairly safe. If you do not define __SFR_OFFSET (so it will be 0x20 by default), all special register addresses are defined as memory addresses (so SREG is 0x5f), and (if code size and speed are not important, and you don’t like the ugly #if above) you can always use lds/sts to access them. But, this will not work if __SFR_OFFSET != 0x20, so use a different macro (defined only if __SFR_OFFSET == 0x20) for safety: sts _SFR_ADDR(SPMCR), r24 In C programs, all 3 combinations of _SFR_ASM_COMPAT and __SFR_OFFSET are supported - the _SFR_ADDR(SPMCR) macro can be used to get the address of the SPMCR register (0x57 or 0x68 depending on device). Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.17 : Special function registers 6.17 : Special function registers 6.17.1 Detailed Description 133 When working with microcontrollers, many of the tasks usually consist of controlling the peripherals that are connected to the device, respectively programming the subsystems that are contained in the controller (which by itself communicate with the circuitry connected to the controller). The AVR series of microcontrollers offers two different paradigms to perform this task. There’s a separate IO address space available (as it is known from some high-level CISC CPUs) that can be addressed with specific IO instructions that are applicable to some or all of the IO address space (in, out, sbi etc.). The entire IO address space is also made available as memory-mapped IO, i. e. it can be accessed using all the MCU instructions that are applicable to normal data memory. The IO register space is mapped into the data memory address space with an offset of 0x20 since the bottom of this space is reserved for direct access to the MCU registers. (Actual SRAM is available only behind the IO register area, starting at either address 0x60, or 0x100 depending on the device.) AVR Libc supports both these paradigms. While by default, the implementation uses memory-mapped IO access, this is hidden from the programmer. So the programmer can access IO registers either with a special function like outb(): #include outb(PORTA, 0x33); or they can assign a value directly to the symbolic address: PORTA = 0x33; The compiler’s choice of which method to use when actually accessing the IO port is completely independent of the way the programmer chooses to write the code. So even if the programmer uses the memory-mapped paradigm and writes PORTA |= 0x40; the compiler can optimize this into the use of an sbi instruction (of course, provided the target address is within the allowable range for this instruction, and the right-hand side of the expression is a constant value known at compile-time). The advantage of using the memory-mapped paradigm in C programs is that it makes the programs more portable to other C compilers for the AVR platform. Some people might also feel that this is more readable. For example, the following two statements would be equivalent: Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.17 : Special function registers 134 outb(DDRD, inb(DDRD) & ~LCDBITS); DDRD &= ~LCDBITS; The generated code is identical for both. Without optimization, the compiler strictly generates code following the memory-mapped paradigm, while with optimization turned on, code is generated using the (faster and smaller) in/out MCU instructions. Note that special care must be taken when accessing some of the 16-bit timer IO registers where access from both the main program and within an interrupt context can happen. See Why do some 16-bit timer registers sometimes get trashed?. Porting programs that use sbi/cbi As described above, access to the AVR single bit set and clear instructions are provided via the standard C bit manipulation commands. The sbi and cbi commands are no longer directly supported. sbi (sfr,bit) can be replaced by sfr |= _BV(bit) . ie: sbi(PORTB, PB1); is now PORTB |= _BV(PB1); This actually is more flexible than having sbi directly, as the optimizer will use a hardware sbi if appropriate, or a read/or/write if not. You do not need to keep track of which registers sbi/cbi will operate on. Likewise, cbi (sfr,bit) is now sfr &= ∼(_BV(bit)); Modules • Additional notes from Bit manipulation • #define _BV(bit) (1 << (bit)) IO register bit manipulation • • • • #define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit)) #define bit_is_clear(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit))) #define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit)) #define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit)) 6.17.2 6.17.2.1 Define Documentation #define _BV(bit) (1 << (bit)) Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.17 : Special function registers 135 #include Converts a bit number into a byte value. Note: The bit shift is performed by the compiler which then inserts the result into the code. Thus, there is no run-time overhead when using _BV(). 6.17.2.2 #define bit_is_clear(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit))) #include Test whether bit bit in IO register sfr is clear. This will return non-zero if the bit is clear, and a 0 if the bit is set. 6.17.2.3 #define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit)) #include Test whether bit bit in IO register sfr is set. This will return a 0 if the bit is clear, and non-zero if the bit is set. 6.17.2.4 #define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit)) #include Wait until bit bit in IO register sfr is clear. 6.17.2.5 #define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit)) #include Wait until bit bit in IO register sfr is set. Generated on Tue May 15 14:56:11 2007 for avr-libc by Doxygen 6.18