Monday, February 27, 2012

Hidden Variables: Arduino and AVR Microcontrollers

(Update 2013-01-23: this article is about the Arduino Uno and similar boards that use the eight-bit Atmel AVR ATmega microcontrollers. If you're looking for the Arduino Due that uses the thirty-two-bit ARM Cortex-M3 microcontroller, see Hidden Variables: Arduino Due and the Cortex-M3.)

In Hidden Variables I showed what command line options you could use to see what preprocessor symbols your GNU C and C++ compilers define by default. There are a lot of them. If you write code intended to run on different AVR microcontrollers, as I am doing for my Amigo project, this is worth knowing. For example, the Ardunino Uno board uses an Atmel ATmega328P microcontroller, while the Ardunino Mega 2560 board uses an Atmel ATmega2560 microcontroller. There are significant differences, some of which the compiler knows about.

Here are all the preprocessor symbols for the ATmega328P that are generated by the command

avr-g++ -dM -E -mmcu=atmega328p - < /dev/null

when using the GCC 4.3.2 AVR tool chain that comes with the Arduino 1.0 software development kit. It's a long list.

#define __DBL_MIN_EXP__ (-125)
#define __FLT_MIN__ 1.17549435e-38F
#define __DEC64_DEN__ 0.000000000000001E-383DD
#define __CHAR_BIT__ 8
#define __WCHAR_MAX__ 32767
#define __DBL_DENORM_MIN__ 1.40129846e-45
#define __FLT_EVAL_METHOD__ 0
#define __DBL_MIN_10_EXP__ (-37)
#define __FINITE_MATH_ONLY__ 0
#define __GNUC_PATCHLEVEL__ 2
#define __AVR_MEGA__ 1
#define __DEC64_MAX_EXP__ 384
#define __SHRT_MAX__ 32767
#define __LDBL_MAX__ 3.40282347e+38L
#define __UINTMAX_TYPE__ long long unsigned int
#define __DEC32_EPSILON__ 1E-6DF
#define __LDBL_MAX_EXP__ 128
#define __SCHAR_MAX__ 127
#define __DBL_DIG__ 6
#define __SIZEOF_INT__ 2
#define __SIZEOF_POINTER__ 2
#define __USER_LABEL_PREFIX__
#define __STDC_HOSTED__ 1
#define __LDBL_HAS_INFINITY__ 1
#define __FLT_EPSILON__ 1.19209290e-7F
#define __LDBL_MIN__ 1.17549435e-38L
#define __DEC32_MAX__ 9.999999E96DF
#define __SIZEOF_LONG__ 4
#define __DECIMAL_DIG__ 9
#define __AVR_2_BYTE_PC__ 1
#define __LDBL_HAS_QUIET_NAN__ 1
#define __GNUC__ 4
#define __FLT_HAS_DENORM__ 1
#define __SIZEOF_LONG_DOUBLE__ 4
#define __DBL_MAX__ 3.40282347e+38
#define __DBL_HAS_INFINITY__ 1
#define __DEC32_MIN_EXP__ (-95)
#define __LDBL_HAS_DENORM__ 1
#define __DEC128_MAX__ 9.999999999999999999999999999999999E6144DL
#define __USING_SJLJ_EXCEPTIONS__ 1
#define __DEC32_MIN__ 1E-95DF
#define __DBL_MAX_EXP__ 128
#define __DEC128_EPSILON__ 1E-33DL
#define __AVR_ENHANCED__ 1
#define __LONG_LONG_MAX__ 9223372036854775807LL
#define __SIZEOF_SIZE_T__ 2
#define __SIZEOF_WINT_T__ 2
#define __GXX_ABI_VERSION 1002
#define __FLT_MIN_EXP__ (-125)
#define __DBL_MIN__ 1.17549435e-38
#define __DEC128_MIN__ 1E-6143DL
#define __REGISTER_PREFIX__
#define __DBL_HAS_DENORM__ 1
#define __AVR_ARCH__ 5
#define __NO_INLINE__ 1
#define __FLT_MANT_DIG__ 24
#define __VERSION__ "4.3.2"
#define __AVR_HAVE_MUL__ 1
#define __DEC64_EPSILON__ 1E-15DD
#define __DEC128_MIN_EXP__ (-6143)
#define __SIZE_TYPE__ unsigned int
#define __DEC32_DEN__ 0.000001E-95DF
#define __FLT_RADIX__ 2
#define __LDBL_EPSILON__ 1.19209290e-7L
#define __SIZEOF_PTRDIFF_T__ 2
#define __AVR 1
#define __FLT_HAS_QUIET_NAN__ 1
#define __FLT_MAX_10_EXP__ 38
#define __LONG_MAX__ 2147483647L
#define __FLT_HAS_INFINITY__ 1
#define __DEC64_MAX__ 9.999999999999999E384DD
#define __AVR_HAVE_LPMX__ 1
#define __DEC64_MANT_DIG__ 16
#define __DEC32_MAX_EXP__ 96
#define __DEC128_DEN__ 0.000000000000000000000000000000001E-6143DL
#define __LDBL_MANT_DIG__ 24
#define __DBL_HAS_QUIET_NAN__ 1
#define AVR 1
#define __WCHAR_TYPE__ int
#define __SIZEOF_FLOAT__ 4
#define __AVR__ 1
#define __AVR_HAVE_JMP_CALL__ 1
#define __DEC64_MIN_EXP__ (-383)
#define __FLT_DIG__ 6
#define __INT_MAX__ 32767
#define __FLT_MAX_EXP__ 128
#define __DBL_MANT_DIG__ 24
#define __AVR_HAVE_MOVW__ 1
#define __DEC64_MIN__ 1E-383DD
#define __WINT_TYPE__ unsigned int
#define __SIZEOF_SHORT__ 2
#define __LDBL_MIN_EXP__ (-125)
#define __LDBL_MAX_10_EXP__ 38
#define __DBL_EPSILON__ 1.19209290e-7
#define __SIZEOF_WCHAR_T__ 2
#define __DEC_EVAL_METHOD__ 2
#define __INTMAX_MAX__ 9223372036854775807LL
#define __FLT_DENORM_MIN__ 1.40129846e-45F
#define __FLT_MAX__ 3.40282347e+38F
#define __AVR_ATmega328P__ 1
#define __SIZEOF_DOUBLE__ 4
#define __FLT_MIN_10_EXP__ (-37)
#define __INTMAX_TYPE__ long long int
#define __DEC128_MAX_EXP__ 6144
#define __GNUC_MINOR__ 3
#define __DEC32_MANT_DIG__ 7
#define __DBL_MAX_10_EXP__ 38
#define __LDBL_DENORM_MIN__ 1.40129846e-45L
#define __STDC__ 1
#define __PTRDIFF_TYPE__ int
#define __DEC128_MANT_DIG__ 34
#define __LDBL_MIN_10_EXP__ (-37)
#define __SIZEOF_LONG_LONG__ 8
#define __LDBL_DIG__ 6
#define __GNUC_GNU_INLINE__ 1

Here are just the symbols containing the string "AVR" sorted alphabetically. Significantly (to me anyway) there are symbols that identify the size of the Program Counter (PC) register as being two bytes, and the underlying architecture as AVR5, which tells you what machine instructions are available.

#define AVR 1
#define __AVR 1
#define __AVR_2_BYTE_PC__ 1
#define __AVR_ARCH__ 5
#define __AVR_ATmega328P__ 1
#define __AVR_ENHANCED__ 1
#define __AVR_HAVE_JMP_CALL__ 1
#define __AVR_HAVE_LPMX__ 1
#define __AVR_HAVE_MOVW__ 1
#define __AVR_HAVE_MUL__ 1
#define __AVR_MEGA__ 1
#define __AVR__ 1

Here are the preprocessor symbols for the ATmega2560 that are generated by the command

avr-g++ -dM -E -mmcu=atmega2560 - < /dev/null

that differ from the complete list above when using the same tool chain. The PC is now three bytes in length, because the ATmega2560 has more program memory than can be addressed in two, and the instruction set conforms to the AVR6 architecture, so that additional machine instructions are available.

3,5c3,5
< #define __AVR_2_BYTE_PC__ 1
< #define __AVR_ARCH__ 5
< #define __AVR_ATmega328P__ 1
---
> #define __AVR_3_BYTE_PC__ 1
> #define __AVR_ARCH__ 6
> #define __AVR_ATmega2560__ 1
6a7,9
> #define __AVR_HAVE_EIJMP_EICALL__ 1
> #define __AVR_HAVE_ELPMX__ 1
> #define __AVR_HAVE_ELPM__ 1
10a14
> #define __AVR_HAVE_RAMPZ__ 1

When you are developing close to bare metal it pays to know these things.

No comments: