sfr_defs.h
01 1 Copyright (c) 2002, Marek Michalkiewicz <marekm@amelek.gda.pl>
022All rights reserved.
033
044Redistribution and use in source and binary forms, with or without
055modification, are permitted provided that the following conditions are met:
066
077* Redistributions of source code must retain the above copyright
088notice, this list of conditions and the following disclaimer.
099
1010* Redistributions in binary form must reproduce the above copyright
1111notice, this list of conditions and the following disclaimer in
1212the documentation and/or other materials provided with the
1313distribution.
1414
1515* Neither the name of the copyright holders nor the names of
1616contributors may be used to endorse or promote products derived
1717from this software without specific prior written permission.
1818
1919THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2020AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2121IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2222ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2323LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2424CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2525SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2626INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2727CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2828ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2929POSSIBILITY OF SUCH DAMAGE. */
3030
3131/* avr/sfr_defs.h - macros for accessing AVR special function registers */
3232
3333/* $Id: sfr_defs.h 1691 2008-04-28 22:05:42Z arcanum $ */
3434
3535#ifndef _AVR_SFR_DEFS_H_
3636#define _AVR_SFR_DEFS_H_ 1
3737
3838/** \defgroup avr_sfr_notes Additional notes from <avr/sfr_defs.h>
3939\ingroup avr_sfr
4040
4141The \c <avr/sfr_defs.h> file is included by all of the \c <avr/ioXXXX.h>
4242files, which use macros defined here to make the special function register
4343definitions look like C variables or simple constants, depending on the
4444<tt>_SFR_ASM_COMPAT</tt> define. Some examples from \c <avr/iocanxx.h> to
4545show how to define such macros:
4646
4747\code
4848#define PORTA _SFR_IO8(0x02)
4949#define EEAR _SFR_IO16(0x21)
5050#define UDR0 _SFR_MEM8(0xC6)
5151#define TCNT3 _SFR_MEM16(0x94)
5252#define CANIDT _SFR_MEM32(0xF0)
5353\endcode
5454
5555If \c _SFR_ASM_COMPAT is not defined, C programs can use names like
5656<tt>PORTA</tt> directly in C expressions (also on the left side of
5757assignment operators) and GCC will do the right thing (use short I/O
5858instructions if possible). The \c __SFR_OFFSET definition is not used in
5959any way in this case.
6060
6161Define \c _SFR_ASM_COMPAT as 1 to make these names work as simple constants
6262(addresses of the I/O registers). This is necessary when included in
6363preprocessed assembler (*.S) source files, so it is done automatically if
6464\c __ASSEMBLER__ is defined. By default, all addresses are defined as if
6565they were memory addresses (used in \c lds/sts instructions). To use these
6666addresses in \c in/out instructions, you must subtract 0x20 from them.
6767
6868For more backwards compatibility, insert the following at the start of your
6969old assembler source file:
7070
7171\code
7272#define __SFR_OFFSET 0
7373\endcode
7474
7575This automatically subtracts 0x20 from I/O space addresses, but it's a
7676hack, so it is recommended to change your source: wrap such addresses in
7777macros defined here, as shown below. After this is done, the
7878<tt>__SFR_OFFSET</tt> definition is no longer necessary and can be removed.
7979
8080Real example - this code could be used in a boot loader that is portable
8181between devices with \c SPMCR at different addresses.
8282
8383\verbatim
8484<avr/iom163.h>: #define SPMCR _SFR_IO8(0x37)
8585<avr/iom128.h>: #define SPMCR _SFR_MEM8(0x68)
8686\endverbatim
8787
8888\code
8989#if _SFR_IO_REG_P(SPMCR)
9090out _SFR_IO_ADDR(SPMCR), r24
9191#else
9292sts _SFR_MEM_ADDR(SPMCR), r24
9393#endif
9494\endcode
9595
9696You can use the \c in/out/cbi/sbi/sbic/sbis instructions, without the
9797<tt>_SFR_IO_REG_P</tt> test, if you know that the register is in the I/O
9898space (as with \c SREG, for example). If it isn't, the assembler will
9999complain (I/O address out of range 0...0x3f), so this should be fairly
100100safe.
101101
102102If you do not define \c __SFR_OFFSET (so it will be 0x20 by default), all
103103special register addresses are defined as memory addresses (so \c SREG is
1041040x5f), and (if code size and speed are not important, and you don't like
105105the ugly \#if above) you can always use lds/sts to access them. But, this
106106will not work if <tt>__SFR_OFFSET</tt> != 0x20, so use a different macro
107107(defined only if <tt>__SFR_OFFSET</tt> == 0x20) for safety:
108108
109109\code
110110sts _SFR_ADDR(SPMCR), r24
111111\endcode
112112
113113In C programs, all 3 combinations of \c _SFR_ASM_COMPAT and
114114<tt>__SFR_OFFSET</tt> are supported - the \c _SFR_ADDR(SPMCR) macro can be
115115used to get the address of the \c SPMCR register (0x57 or 0x68 depending on
116116device). */
117117
118118#ifdef __ASSEMBLER__
119119#define _SFR_ASM_COMPAT 1
120120#elif !defined(_SFR_ASM_COMPAT)
121121#define _SFR_ASM_COMPAT 0
122122#endif
123123
124124#ifndef __ASSEMBLER__
125125/* These only work in C programs. */
126126#include <