Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

sfr_defs.h

00001 /* Copyright (c) 2002, Marek Michalkiewicz <marekm@amelek.gda.pl>
00002    All rights reserved.
00003 
00004    Redistribution and use in source and binary forms, with or without
00005    modification, are permitted provided that the following conditions are met:
00006 
00007    * Redistributions of source code must retain the above copyright
00008      notice, this list of conditions and the following disclaimer.
00009    * Redistributions in binary form must reproduce the above copyright
00010      notice, this list of conditions and the following disclaimer in
00011      the documentation and/or other materials provided with the
00012      distribution.
00013 
00014    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00015    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00017    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00018    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00019    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00020    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00021    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00022    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00023    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00024    POSSIBILITY OF SUCH DAMAGE.  */
00025 
00026 /* avr/sfr_defs.h - macros for accessing AVR special function registers */
00027 
00028 /* $Id: sfr_defs.h,v 1.1.1.1 2002/12/07 07:16:53 marwi Exp $ */
00029 
00030 #ifndef _AVR_SFR_DEFS_H_
00031 #define _AVR_SFR_DEFS_H_ 1
00032 
00115 #ifdef __ASSEMBLER__
00116 #define _SFR_ASM_COMPAT 1
00117 #endif
00118 
00119 #ifndef __ASSEMBLER__
00120 /* These only work in C programs.  */
00121 #define _MMIO_BYTE(mem_addr) (*(volatile unsigned char *)(mem_addr))
00122 #define _MMIO_WORD(mem_addr) (*(volatile unsigned int *)(mem_addr))
00123 #endif
00124 
00125 #if _SFR_ASM_COMPAT
00126 
00127 #ifndef __SFR_OFFSET
00128 /* Define as 0 before including this file for compatibility with old asm
00129    sources that don't subtract __SFR_OFFSET from symbolic I/O addresses.  */
00130 #define __SFR_OFFSET 0x20
00131 #endif
00132 
00133 #if (__SFR_OFFSET != 0) && (__SFR_OFFSET != 0x20)
00134 #error "__SFR_OFFSET must be 0 or 0x20"
00135 #endif
00136 
00137 #define _SFR_MEM8(mem_addr) (mem_addr)
00138 #define _SFR_MEM16(mem_addr) (mem_addr)
00139 #define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET)
00140 #define _SFR_IO16(io_addr) ((io_addr) + __SFR_OFFSET)
00141 
00142 #define _SFR_IO_ADDR(sfr) ((sfr) - __SFR_OFFSET)
00143 #define _SFR_MEM_ADDR(sfr) (sfr)
00144 #define _SFR_IO_REG_P(sfr) ((sfr) < 0x40 + __SFR_OFFSET)
00145 
00146 #if (__SFR_OFFSET == 0x20)
00147 /* No need to use ?: operator, so works in assembler too.  */
00148 #define _SFR_ADDR(sfr) _SFR_MEM_ADDR(sfr)
00149 #elif !defined(__ASSEMBLER__)
00150 #define _SFR_ADDR(sfr) (_SFR_IO_REG_P(sfr) ? (_SFR_IO_ADDR(sfr) + 0x20) : _SFR_MEM_ADDR(sfr))
00151 #endif
00152 
00153 #else  /* !_SFR_ASM_COMPAT */
00154 
00155 #define _SFR_MEM8(mem_addr) _MMIO_BYTE(mem_addr)
00156 #define _SFR_MEM16(mem_addr) _MMIO_WORD(mem_addr)
00157 #define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + 0x20)
00158 #define _SFR_IO16(io_addr) _MMIO_WORD((io_addr) + 0x20)
00159 
00160 #define _SFR_MEM_ADDR(sfr) ((unsigned int) &(sfr))
00161 #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - 0x20)
00162 #define _SFR_IO_REG_P(sfr) (_SFR_MEM_ADDR(sfr) < 0x60)
00163 
00164 #define _SFR_ADDR(sfr) _SFR_MEM_ADDR(sfr)
00165 
00166 #endif /* !_SFR_ASM_COMPAT */
00167 
00182 #define _BV(bit) (1 << (bit))
00183 
00186 #ifndef _VECTOR
00187 #define _VECTOR(N) __vector_ ## N
00188 #endif
00189 
00190 #ifndef __ASSEMBLER__
00191 
00192 #define _SFR_BYTE(sfr) _MMIO_BYTE(_SFR_ADDR(sfr))
00193 #define _SFR_WORD(sfr) _MMIO_WORD(_SFR_ADDR(sfr))
00194 
00195 /* The outb/outw macros now have the correct order of arguments.  */
00196 
00200 
00208 #define inb(sfr) _SFR_BYTE(sfr)
00209 
00217 #define inw(sfr) _SFR_WORD(sfr)
00218 
00229 #define outb(sfr, val) (_SFR_BYTE(sfr) = (val))
00230 
00245 #define outw(sfr, val) (_SFR_WORD(sfr) = (val))
00246 
00252 
00260 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
00261 
00269 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
00270 
00271 
00279 #define bit_is_set(sfr, bit) (inb(sfr) & _BV(bit))
00280 
00288 #define bit_is_clear(sfr, bit) (~inb(sfr) & _BV(bit))
00289 
00297 #define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit))
00298 
00306 #define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit))
00307 
00310 #endif /* !__ASSEMBLER__ */
00311 
00312 /* Backwards compatibility, do not use in new programs.  */
00313 
00316 
00325 #define outp(val, sfr) outb(sfr, val)
00326 
00335 #define inp(sfr) inb(sfr)
00336 
00345 #define BV(bit) _BV(bit)
00346 
00349 #endif  /* _SFR_DEFS_H_ */

Generated on Thu Jan 30 22:30:52 2003 for EduNet by doxygen1.2.18