RTEMS CPU Kit with SuperCore  4.11.3
sfr_defs.h
Go to the documentation of this file.
1 
7 /* Copyright (c) 2002, Marek Michalkiewicz <marekm@amelek.gda.pl>
8  All rights reserved.
9 
10  Redistribution and use in source and binary forms, with or without
11  modification, are permitted provided that the following conditions are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in
18  the documentation and/or other materials provided with the
19  distribution.
20 
21  * Neither the name of the copyright holders nor the names of
22  contributors may be used to endorse or promote products derived
23  from this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE. */
36 
37 /* avr/sfr_defs.h - macros for accessing AVR special function registers */
38 
39 
40 #ifndef _AVR_SFR_DEFS_H_
41 #define _AVR_SFR_DEFS_H_ 1
42 
127 #ifdef __ASSEMBLER__
128 #define _SFR_ASM_COMPAT 1
129 #elif !defined(_SFR_ASM_COMPAT)
130 #define _SFR_ASM_COMPAT 0
131 #endif
132 
133 #ifndef __ASSEMBLER__
134 /* These only work in C programs. */
135 #include <inttypes.h>
136 
137 #define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))
138 #define _MMIO_WORD(mem_addr) (*(volatile uint16_t *)(mem_addr))
139 #define _MMIO_DWORD(mem_addr) (*(volatile uint32_t *)(mem_addr))
140 #endif
141 
142 #if _SFR_ASM_COMPAT
143 
144 #ifndef __SFR_OFFSET
145 /* Define as 0 before including this file for compatibility with old asm
146  sources that don't subtract __SFR_OFFSET from symbolic I/O addresses. */
147 # if __AVR_ARCH__ >= 100
148 # define __SFR_OFFSET 0x00
149 # else
150 # define __SFR_OFFSET 0x20
151 # endif
152 #endif
153 
154 #if (__SFR_OFFSET != 0) && (__SFR_OFFSET != 0x20)
155 #error "__SFR_OFFSET must be 0 or 0x20"
156 #endif
157 
158 #define _SFR_MEM8(mem_addr) (mem_addr)
159 #define _SFR_MEM16(mem_addr) (mem_addr)
160 #define _SFR_MEM32(mem_addr) (mem_addr)
161 #define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET)
162 #define _SFR_IO16(io_addr) ((io_addr) + __SFR_OFFSET)
163 
164 #define _SFR_IO_ADDR(sfr) ((sfr) - __SFR_OFFSET)
165 #define _SFR_MEM_ADDR(sfr) (sfr)
166 #define _SFR_IO_REG_P(sfr) ((sfr) < 0x40 + __SFR_OFFSET)
167 
168 #if (__SFR_OFFSET == 0x20)
169 /* No need to use ?: operator, so works in assembler too. */
170 #define _SFR_ADDR(sfr) _SFR_MEM_ADDR(sfr)
171 #elif !defined(__ASSEMBLER__)
172 #define _SFR_ADDR(sfr) (_SFR_IO_REG_P(sfr) ? (_SFR_IO_ADDR(sfr) + 0x20) : _SFR_MEM_ADDR(sfr))
173 #endif
174 
175 #else /* !_SFR_ASM_COMPAT */
176 
177 #ifndef __SFR_OFFSET
178 # if __AVR_ARCH__ >= 100
179 # define __SFR_OFFSET 0x00
180 # else
181 # define __SFR_OFFSET 0x20
182 # endif
183 #endif
184 
185 #define _SFR_MEM8(mem_addr) _MMIO_BYTE(mem_addr)
186 #define _SFR_MEM16(mem_addr) _MMIO_WORD(mem_addr)
187 #define _SFR_MEM32(mem_addr) _MMIO_DWORD(mem_addr)
188 #define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + __SFR_OFFSET)
189 #define _SFR_IO16(io_addr) _MMIO_WORD((io_addr) + __SFR_OFFSET)
190 
191 #define _SFR_MEM_ADDR(sfr) ((uint16_t) &(sfr))
192 #define _SFR_IO_ADDR(sfr) (_SFR_MEM_ADDR(sfr) - __SFR_OFFSET)
193 #define _SFR_IO_REG_P(sfr) (_SFR_MEM_ADDR(sfr) < 0x40 + __SFR_OFFSET)
194 
195 #define _SFR_ADDR(sfr) _SFR_MEM_ADDR(sfr)
196 
197 #endif /* !_SFR_ASM_COMPAT */
198 
199 #define _SFR_BYTE(sfr) _MMIO_BYTE(_SFR_ADDR(sfr))
200 #define _SFR_WORD(sfr) _MMIO_WORD(_SFR_ADDR(sfr))
201 #define _SFR_DWORD(sfr) _MMIO_DWORD(_SFR_ADDR(sfr))
202 
217 #define _BV(bit) (1 << (bit))
218 
221 #ifndef _VECTOR
222 #define _VECTOR(N) __vector_ ## N
223 #endif
224 
225 #ifndef __ASSEMBLER__
226 
227 
240 #define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit))
241 
250 #define bit_is_clear(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit)))
251 
257 #define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit))
258 
264 #define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit))
265 
270 #endif /* !__ASSEMBLER__ */
271 
272 #endif /* _SFR_DEFS_H_ */