AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Reference FAQ Example Projects

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 <inttypes.h> 127127 128128#define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr)) 129129#define _MMIO_WORD(mem_addr) (*(volatile uint16_t *)(mem_addr)) 130130#define _MMIO_DWORD(mem_addr) (*(volatile uint32_t *)(mem_addr)) 131131#endif 132132 133133#if _SFR_ASM_COMPAT 134134 135135#ifndef __SFR_OFFSET 136136/* Define as 0 before including this file for compatibility with old asm 137137sources that don't subtract __SFR_OFFSET from symbolic I/O addresses. */ 138138# if __AVR_ARCH__ >> = 100 139139# define __SFR_OFFSET 0x00 140140# else 141141# define __SFR_OFFSET 0x20 142142# endif 143143#endif 144144 145145#if (__SFR_OFFSET != 0) && (__SFR_OFFSET != 0x20) 146146#error "__SFR_OFFSET must be 0 or 0x20" 147147#endif 148148 149149#define _SFR_MEM8(mem_addr) (mem_addr) 150150#define _SFR_MEM16(mem_addr) (mem_addr) 151151#define _SFR_MEM32(mem_addr) (mem_addr) 152152#define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET) 153153#define _SFR_IO16(io_addr) ((io_addr) + __SFR_OFFSET) 154154 155155#define _SFR_IO_ADDR(sfr) ((sfr) - __SFR_OFFSET) 156156#define _SFR_MEM_ADDR(sfr) (sfr) 157157#define _SFR_IO_REG_P(sfr) ((sfr) < 0x40 + __SFR_OFFSET) 158158 159159#if (__SFR_OFFSET == 0x20) 160160/* No need to use ?: operator, so works in assembler too. */ 161161#define _SFR_ADDR(sfr) _SFR_MEM_ADDR(sfr) 162162#elif !defined(__ASSEMBLER__) 163163#define _SFR_ADDR(sfr) (_SFR_IO_REG_P(sfr) ? (_SFR_IO_ADDR(sfr) + 0x20) : _SFR_MEM_ADDR(sfr)) 164164#endif 165165 166166#else /* !_SFR_ASM_COMPAT */ 167167 168168#ifndef __SFR_OFFSET 169169# if __AVR_ARCH__ >> = 100 170170# define __SFR_OFFSET 0x00 171171# else 172172# define __SFR_OFFSET 0x20 173173# endif 174174#endif 175175 176176#define _SFR_MEM8(mem_addr) _MMIO_BYTE(mem_addr) 177177#define _SFR_MEM16(mem_addr) _MMIO_WORD(mem_addr) 178178#define _SFR_MEM32(mem_addr) _MMIO_DWORD(mem_addr) 179179#define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + __SFR_OFFSET) 180180#define _SFR_IO16(io_addr) _MMIO_WORD((io_addr) + __SFR_OFFSET) 181181 182182#define _SFR_MEM_ADDR(sfr) ((uint16_t) &(sfr)) 183183#define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET) 184184#define _SFR_IO_REG_P(sfr) (_SFR_MEM_ADDR(sfr) < 0x40 + __SFR_OFFSET) 185185 186186#define _SFR_ADDR(sfr) _SFR_MEM_ADDR(sfr) 187187 188188#endif /* !_SFR_ASM_COMPAT */ 189189 190190#define _SFR_BYTE(sfr) _MMIO_BYTE(_SFR_ADDR(sfr)) 191191#define _SFR_WORD(sfr) _MMIO_WORD(_SFR_ADDR(sfr)) 192192#define _SFR_DWORD(sfr) _MMIO_DWORD(_SFR_ADDR(sfr)) 193193 194194/** \name Bit manipulation */ 195195 196196/*@{*/ 197197/** \def _BV 198198\ingroup avr_sfr 199199 200200\code #include <avr/io.h>\endcode 201201 202202Converts a bit number into a byte value. 203203 204204\note The bit shift is performed by the compiler which then inserts the 205205result into the code. Thus, there is no run-time overhead when using 206206_BV(). */ 207207208 208#define _BV(bit) (1 << (bit)) 209209 210210/*@}*/ 211211 212212#ifndef _VECTOR 213213#define _VECTOR(N) __vector_ ## N 214214#endif 215215 216216#ifndef __ASSEMBLER__ 217217 218218 219219/** \name IO register bit manipulation */ 220220 221221/*@{*/ 222222 223223 224224 225225/** \def bit_is_set 226226\ingroup avr_sfr 227227 228228\code #include <avr/io.h>\endcode 229229 230230Test whether bit \c bit in IO register \c sfr is set. 231231This will return a 0 if the bit is clear, and non-zero 232232if the bit is set. */ 233233 234 234#define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit)) 235235 236236/** \def bit_is_clear 237237\ingroup avr_sfr 238238 239239\code #include <avr/io.h>\endcode 240240 241241Test whether bit \c bit in IO register \c sfr is clear. 242242This will return non-zero if the bit is clear, and a 0 243243if the bit is set. */ 244244 245 245#define bit_is_clear(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit))) 246246 247247/** \def loop_until_bit_is_set 248248\ingroup avr_sfr 249249 250250\code #include <avr/io.h>\endcode 251251 252252Wait until bit \c bit in IO register \c sfr is set. */ 253253 254 254#define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit)) 255255 256256/** \def loop_until_bit_is_clear 257257\ingroup avr_sfr 258258 259259\code #include <avr/io.h>\endcode 260260 261261Wait until bit \c bit in IO register \c sfr is clear. */ 262262 263 263#define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit)) 264264 265265/*@}*/ 266266 267267#endif /* !__ASSEMBLER__ */ 268268 269269#endif /* _SFR_DEFS_H_ */

inttypes.h