Merge contributed patch to extend support of atxmega.
[bertos.git] / bertos / cpu / attr.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007, 2008 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  * -->
32  *
33  * \brief CPU-specific attributes.
34  *
35  * \author Giovanni Bajo <rasky@develer.com>
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  * \author Francesco Sacchi <batt@develer.com>
39  */
40 #ifndef CPU_ATTR_H
41 #define CPU_ATTR_H
42
43 #include "detect.h"
44
45 #include "cfg/cfg_proc.h"      /* CONFIG_KERN_PREEMPT */
46 #include "cfg/cfg_attr.h"      /* CONFIG_FAST_MEM */
47
48
49 /**
50  * \name Macros for determining CPU endianness.
51  * \{
52  */
53 #define CPU_BIG_ENDIAN    0x1234
54 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
55 /*\}*/
56
57 /** Macro to include cpu-specific versions of the headers. */
58 #define CPU_HEADER(module)          PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).h)
59
60 /** Macro to include cpu-specific versions of implementation files. */
61 #define CPU_CSOURCE(module)         PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).c)
62
63
64 #if CPU_I196
65
66         #define NOP                     nop_instruction()
67
68         #define CPU_REG_BITS            16
69         #define CPU_REGS_CNT            16
70         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
71         #define CPU_HARVARD             0
72
73         /// Valid pointers should be >= than this value (used for debug)
74         #define CPU_RAM_START           0x100
75
76 #elif CPU_X86
77
78         #define CPU_REGS_CNT            7
79         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
80         #define CPU_HARVARD             0
81
82         #if CPU_X86_64
83                 #define CPU_REG_BITS    64
84
85                 #ifdef __WIN64__
86                         /* WIN64 is an IL32-P64 weirdo. */
87                         #define SIZEOF_LONG  4
88                 #endif
89         #else
90                 #define CPU_REG_BITS    32
91         #endif
92
93         /// Valid pointers should be >= than this value (used for debug)
94         #define CPU_RAM_START      0x1000
95
96         #ifdef __GNUC__
97                 #define NOP         asm volatile ("nop")
98                 /* This is a good thing to insert into busy-wait loops. */
99                 #define PAUSE       asm volatile ("rep; nop" ::: "memory")
100                 #define BREAKPOINT  asm volatile ("int3" ::)
101         #endif
102
103 #elif CPU_ARM
104
105         #define CPU_REG_BITS           32
106         #define CPU_REGS_CNT           16
107         #define CPU_HARVARD            0
108
109         /// Valid pointers should be >= than this value (used for debug)
110         #if CPU_ARM_AT91
111                 #define CPU_RAM_START           0x00200000
112         #elif CPU_ARM_LPC2
113                 #define CPU_RAM_START           0x40000000
114         #else
115                 #warning Fix CPU_RAM_START address for your ARM, default value set to 0x200
116                 #define CPU_RAM_START           0x200
117         #endif
118
119         #ifdef __IAR_SYSTEMS_ICC__
120                 #warning Check CPU_BYTE_ORDER
121                 #define CPU_BYTE_ORDER (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
122
123                 #define NOP            __no_operation()
124
125         #else /* GCC and compatibles */
126
127                 #if defined(__ARMEB__)
128                         #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
129                 #elif defined(__ARMEL__)
130                         #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
131                 #else
132                         #error Unable to detect ARM endianness!
133                 #endif
134
135                 #define NOP            asm volatile ("mov r0,r0" ::)
136                 #define BREAKPOINT  /* asm("bkpt 0") DOES NOT WORK */
137
138                 #if CONFIG_FAST_MEM
139                         /**
140                          * Function attribute for use with performance critical code.
141                          *
142                          * On the AT91 family, code residing in flash has wait states.
143                          * Moving functions to the data section is a quick & dirty way
144                          * to get them transparently copied to SRAM for zero-wait-state
145                          * operation.
146                          */
147                         #define FAST_FUNC __attribute__((section(".ramfunc")))
148
149                         /**
150                          * Data attribute to move constant data to fast memory storage.
151                          *
152                          * \see FAST_FUNC
153                          */
154                         #define FAST_RODATA __attribute__((section(".data")))
155
156                 #else // !CONFIG_FAST_MEM
157                         #define FAST_RODATA /**/
158                         #define FAST_FUNC /**/
159                 #endif
160
161                 /*
162                  * Function attribute to move it into ram memory.
163                  */
164                 #define RAM_FUNC __attribute__((section(".ramfunc")))
165
166         #endif /* !__IAR_SYSTEMS_ICC_ */
167 #elif CPU_CM3
168
169         #define CPU_REG_BITS           32
170         #define CPU_REGS_CNT           16
171         #define CPU_HARVARD            0
172
173         /// Valid pointers should be >= than this value (used for debug)
174         #if (CPU_CM3_LM3S1968 || CPU_CM3_LM3S8962 || CPU_CM3_STM32 || CPU_CM3_SAM3)
175                 #define CPU_RAM_START 0x20000000
176         #else
177                 #warning Fix CPU_RAM_START address for your Cortex-M3, default value set to 0x20000000
178                 #define CPU_RAM_START 0x20000000
179         #endif
180
181     #if defined( __ICCARM__)
182         #if ((defined __LITTLE_ENDIAN__) && (__LITTLE_ENDIAN__ == 0))
183             #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
184         #elif ((defined __LITTLE_ENDIAN__) && (__LITTLE_ENDIAN__ == 1))
185                     #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
186         #else
187             #error Unable to detect Cortex-M3 endianess!
188         #endif
189
190         #define NOP            __no_operation()
191     #else
192         #if defined(__ARMEB__) // GCC
193             #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
194         #elif defined(__ARMEL__) // GCC
195             #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
196         #else
197             #error Unable to detect Cortex-M3 endianess!
198         #endif
199
200         #define NOP         asm volatile ("nop")
201         #define PAUSE       asm volatile ("wfi" ::: "memory")
202         #define BREAKPOINT  /* asm("bkpt 0") DOES NOT WORK */
203
204         /*
205          * Function attribute to move it into ram memory.
206          */
207         #define RAM_FUNC __attribute__((section(".ramfunc")))
208     #endif
209
210 #elif CPU_PPC
211
212         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
213         #define CPU_REGS_CNT           FIXME
214         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
215         #define CPU_HARVARD            0
216
217         /// Valid pointers should be >= than this value (used for debug)
218         #define CPU_RAM_START          0x1000
219
220         #ifdef __GNUC__
221             #define NOP         asm volatile ("nop" ::)
222                 #define BREAKPOINT  asm volatile ("twge 2,2" ::)
223         #endif
224
225 #elif CPU_DSP56K
226
227         #define CPU_REG_BITS            16
228         #define CPU_REGS_CNT            FIXME
229         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
230         #define CPU_HARVARD             1
231
232         /* Memory is word-addessed in the DSP56K */
233         #define CPU_BITS_PER_CHAR  16
234         #define SIZEOF_SHORT        1
235         #define SIZEOF_INT          1
236         #define SIZEOF_LONG         2
237         #define SIZEOF_PTR          1
238
239         /// Valid pointers should be >= than this value (used for debug)
240         #define CPU_RAM_START       0x200
241
242         #define NOP                     asm(nop)
243         #define BREAKPOINT              asm(debug)
244
245 #elif CPU_AVR
246
247         #define NOP                     asm volatile ("nop" ::)
248
249         #define CPU_REG_BITS            8
250         #define CPU_REGS_CNT           33 /* Includes SREG */
251         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
252         #define CPU_HARVARD             1
253
254         /// Valid pointers should be >= than this value (used for debug)
255         #if CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32 || CPU_AVR_ATMEGA103
256                 #define CPU_RAM_START       0x60
257         #elif CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
258                 #define CPU_RAM_START       0x100
259         #elif CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA1280 || CPU_AVR_ATMEGA2560
260                 #define CPU_RAM_START       0x200
261         #elif CPU_AVR_XMEGA
262                 #define CPU_RAM_START           0x2000
263         #else
264                 #warning Fix CPU_RAM_START address for your AVR, default value set to 0x100
265                 #define CPU_RAM_START       0x100
266         #endif
267
268 #elif CPU_MSP430
269
270         #define CPU_REG_BITS            16
271         #define CPU_REGS_CNT            12
272         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
273         #define CPU_HARVARD                     0
274
275         /// Valid pointers should be >= than this value (used for debug)
276         #define CPU_RAM_START           0x200
277
278         #define NOP                     __asm__ __volatile__ ("nop")
279
280 #else
281         #error No CPU_... defined.
282 #endif
283
284 #ifndef BREAKPOINT
285 #define BREAKPOINT /* nop */
286 #endif
287
288 #ifndef FAST_FUNC
289         /// Function attribute for use with performance critical code.
290         #define FAST_FUNC /* */
291 #endif
292
293 #ifndef FAST_RODATA
294         /// Data attribute to move constant data to fast memory storage.
295         #define FAST_RODATA /* */
296 #endif
297
298 #ifndef PAUSE
299         /// Generic PAUSE implementation.
300         #define PAUSE   do {NOP; MEMORY_BARRIER;} while (0)
301 #endif
302
303 #endif /* CPU_ATTR_H */