]> git.sur5r.net Git - freertos/blob - Demo/AVR32_UC3A_GCC/Atmel_SW_Framework/UTILS/compiler.h
Temporarily revert the AVR32 port back to the V6.0.5 files. Work will continue on...
[freertos] / Demo / AVR32_UC3A_GCC / Atmel_SW_Framework / UTILS / compiler.h
1 /* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */\r
2
3 /*This file is prepared for Doxygen automatic documentation generation.*/\r
4 /*! \file *********************************************************************\r
5  *\r
6  * \brief Compiler file for AVR32.\r
7  *\r
8  * This file defines commonly used types and macros.\r
9  *\r
10  * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32\r
11  * - Supported devices:  All AVR32 devices can be used.\r
12  * - AppNote:\r
13  *\r
14  * \author               Atmel Corporation: http://www.atmel.com \n\r
15  *                       Support and FAQ: http://support.atmel.no/\r
16  *\r
17  ******************************************************************************/\r
18 \r
19 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.\r
20  *\r
21  * Redistribution and use in source and binary forms, with or without\r
22  * modification, are permitted provided that the following conditions are met:\r
23  *\r
24  * 1. Redistributions of source code must retain the above copyright notice, this\r
25  * list of conditions and the following disclaimer.\r
26  *\r
27  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
28  * this list of conditions and the following disclaimer in the documentation\r
29  * and/or other materials provided with the distribution.\r
30  *\r
31  * 3. The name of Atmel may not be used to endorse or promote products derived\r
32  * from this software without specific prior written permission.\r
33  *\r
34  * 4. This software may only be redistributed and used in connection with an Atmel\r
35  * AVR product.\r
36  *\r
37  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
39  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
40  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
41  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
42  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
45  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
46  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\r
47  *\r
48  */\r
49 \r
50 #ifndef _COMPILER_H_\r
51 #define _COMPILER_H_\r
52 \r
53 #if ((defined __GNUC__) && (defined __AVR32__)) || (defined __ICCAVR32__ || defined __AAVR32__)\r
54 #  include <avr32/io.h>\r
55 #endif\r
56 #if (defined __ICCAVR32__)\r
57 #  include <intrinsics.h>\r
58 #endif\r
59 #include "preprocessor.h"\r
60 \r
61 #include "parts.h"\r
62 \r
63 \r
64 //_____ D E C L A R A T I O N S ____________________________________________\r
65 \r
66 #ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.\r
67 \r
68 #include <stddef.h>\r
69 #include <stdlib.h>\r
70 \r
71 \r
72 #if (defined __ICCAVR32__)\r
73 \r
74 /*! \name Compiler Keywords\r
75  *\r
76  * Port of some keywords from GNU GCC for AVR32 to IAR Embedded Workbench for Atmel AVR32.\r
77  */\r
78 //! @{\r
79 #define __asm__             asm\r
80 #define __inline__          inline\r
81 #define __volatile__\r
82 //! @}\r
83 \r
84 #endif\r
85 \r
86 \r
87 /*! \name Usual Types\r
88  */\r
89 //! @{\r
90 typedef unsigned char           Bool; //!< Boolean.\r
91 #ifndef __cplusplus\r
92 #if !defined(__bool_true_false_are_defined)\r
93 typedef unsigned char           bool; //!< Boolean.\r
94 #endif\r
95 #endif\r
96 typedef signed char             S8 ;  //!< 8-bit signed integer.\r
97 typedef unsigned char           U8 ;  //!< 8-bit unsigned integer.\r
98 typedef signed short int        S16;  //!< 16-bit signed integer.\r
99 typedef unsigned short int      U16;  //!< 16-bit unsigned integer.\r
100 typedef signed long int         S32;  //!< 32-bit signed integer.\r
101 typedef unsigned long int       U32;  //!< 32-bit unsigned integer.\r
102 typedef signed long long int    S64;  //!< 64-bit signed integer.\r
103 typedef unsigned long long int  U64;  //!< 64-bit unsigned integer.\r
104 typedef float                   F32;  //!< 32-bit floating-point number.\r
105 typedef double                  F64;  //!< 64-bit floating-point number.\r
106 //! @}\r
107 \r
108 \r
109 /*! \name Status Types\r
110  */\r
111 //! @{\r
112 typedef Bool                Status_bool_t;  //!< Boolean status.\r
113 typedef U8                  Status_t;       //!< 8-bit-coded status.\r
114 //! @}\r
115 \r
116 \r
117 /*! \name Aliasing Aggregate Types\r
118  */\r
119 //! @{\r
120 \r
121 //! 16-bit union.\r
122 typedef union\r
123 {\r
124   S16 s16   ;\r
125   U16 u16   ;\r
126   S8  s8 [2];\r
127   U8  u8 [2];\r
128 } Union16;\r
129 \r
130 //! 32-bit union.\r
131 typedef union\r
132 {\r
133   S32 s32   ;\r
134   U32 u32   ;\r
135   S16 s16[2];\r
136   U16 u16[2];\r
137   S8  s8 [4];\r
138   U8  u8 [4];\r
139 } Union32;\r
140 \r
141 //! 64-bit union.\r
142 typedef union\r
143 {\r
144   S64 s64   ;\r
145   U64 u64   ;\r
146   S32 s32[2];\r
147   U32 u32[2];\r
148   S16 s16[4];\r
149   U16 u16[4];\r
150   S8  s8 [8];\r
151   U8  u8 [8];\r
152 } Union64;\r
153 \r
154 //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.\r
155 typedef union\r
156 {\r
157   S64 *s64ptr;\r
158   U64 *u64ptr;\r
159   S32 *s32ptr;\r
160   U32 *u32ptr;\r
161   S16 *s16ptr;\r
162   U16 *u16ptr;\r
163   S8  *s8ptr ;\r
164   U8  *u8ptr ;\r
165 } UnionPtr;\r
166 \r
167 //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
168 typedef union\r
169 {\r
170   volatile S64 *s64ptr;\r
171   volatile U64 *u64ptr;\r
172   volatile S32 *s32ptr;\r
173   volatile U32 *u32ptr;\r
174   volatile S16 *s16ptr;\r
175   volatile U16 *u16ptr;\r
176   volatile S8  *s8ptr ;\r
177   volatile U8  *u8ptr ;\r
178 } UnionVPtr;\r
179 \r
180 //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.\r
181 typedef union\r
182 {\r
183   const S64 *s64ptr;\r
184   const U64 *u64ptr;\r
185   const S32 *s32ptr;\r
186   const U32 *u32ptr;\r
187   const S16 *s16ptr;\r
188   const U16 *u16ptr;\r
189   const S8  *s8ptr ;\r
190   const U8  *u8ptr ;\r
191 } UnionCPtr;\r
192 \r
193 //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
194 typedef union\r
195 {\r
196   const volatile S64 *s64ptr;\r
197   const volatile U64 *u64ptr;\r
198   const volatile S32 *s32ptr;\r
199   const volatile U32 *u32ptr;\r
200   const volatile S16 *s16ptr;\r
201   const volatile U16 *u16ptr;\r
202   const volatile S8  *s8ptr ;\r
203   const volatile U8  *u8ptr ;\r
204 } UnionCVPtr;\r
205 \r
206 //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.\r
207 typedef struct\r
208 {\r
209   S64 *s64ptr;\r
210   U64 *u64ptr;\r
211   S32 *s32ptr;\r
212   U32 *u32ptr;\r
213   S16 *s16ptr;\r
214   U16 *u16ptr;\r
215   S8  *s8ptr ;\r
216   U8  *u8ptr ;\r
217 } StructPtr;\r
218 \r
219 //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
220 typedef struct\r
221 {\r
222   volatile S64 *s64ptr;\r
223   volatile U64 *u64ptr;\r
224   volatile S32 *s32ptr;\r
225   volatile U32 *u32ptr;\r
226   volatile S16 *s16ptr;\r
227   volatile U16 *u16ptr;\r
228   volatile S8  *s8ptr ;\r
229   volatile U8  *u8ptr ;\r
230 } StructVPtr;\r
231 \r
232 //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.\r
233 typedef struct\r
234 {\r
235   const S64 *s64ptr;\r
236   const U64 *u64ptr;\r
237   const S32 *s32ptr;\r
238   const U32 *u32ptr;\r
239   const S16 *s16ptr;\r
240   const U16 *u16ptr;\r
241   const S8  *s8ptr ;\r
242   const U8  *u8ptr ;\r
243 } StructCPtr;\r
244 \r
245 //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
246 typedef struct\r
247 {\r
248   const volatile S64 *s64ptr;\r
249   const volatile U64 *u64ptr;\r
250   const volatile S32 *s32ptr;\r
251   const volatile U32 *u32ptr;\r
252   const volatile S16 *s16ptr;\r
253   const volatile U16 *u16ptr;\r
254   const volatile S8  *s8ptr ;\r
255   const volatile U8  *u8ptr ;\r
256 } StructCVPtr;\r
257 \r
258 //! @}\r
259 \r
260 #endif  // __AVR32_ABI_COMPILER__\r
261 \r
262 \r
263 //_____ M A C R O S ________________________________________________________\r
264 \r
265 /*! \name Usual Constants\r
266  */\r
267 //! @{\r
268 #define DISABLE   0\r
269 #define ENABLE    1\r
270 #define DISABLED  0\r
271 #define ENABLED   1\r
272 #define OFF       0\r
273 #define ON        1\r
274 #define FALSE     0\r
275 #define TRUE      1\r
276 #ifndef __cplusplus\r
277 #if !defined(__bool_true_false_are_defined)\r
278 #define false     FALSE\r
279 #define true      TRUE\r
280 #endif\r
281 #endif\r
282 #define KO        0\r
283 #define OK        1\r
284 #define PASS      0\r
285 #define FAIL      1\r
286 #define LOW       0\r
287 #define HIGH      1\r
288 #define CLR       0\r
289 #define SET       1\r
290 //! @}\r
291 \r
292 \r
293 #ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.\r
294 \r
295 /*! \name Bit-Field Handling\r
296  */\r
297 //! @{\r
298 \r
299 /*! \brief Reads the bits of a value specified by a given bit-mask.\r
300  *\r
301  * \param value Value to read bits from.\r
302  * \param mask  Bit-mask indicating bits to read.\r
303  *\r
304  * \return Read bits.\r
305  */\r
306 #define Rd_bits( value, mask)        ((value) & (mask))\r
307 \r
308 /*! \brief Writes the bits of a C lvalue specified by a given bit-mask.\r
309  *\r
310  * \param lvalue  C lvalue to write bits to.\r
311  * \param mask    Bit-mask indicating bits to write.\r
312  * \param bits    Bits to write.\r
313  *\r
314  * \return Resulting value with written bits.\r
315  */\r
316 #define Wr_bits(lvalue, mask, bits)  ((lvalue) = ((lvalue) & ~(mask)) |\\r
317                                                  ((bits  ) &  (mask)))\r
318 \r
319 /*! \brief Tests the bits of a value specified by a given bit-mask.\r
320  *\r
321  * \param value Value of which to test bits.\r
322  * \param mask  Bit-mask indicating bits to test.\r
323  *\r
324  * \return \c 1 if at least one of the tested bits is set, else \c 0.\r
325  */\r
326 #define Tst_bits( value, mask)  (Rd_bits(value, mask) != 0)\r
327 \r
328 /*! \brief Clears the bits of a C lvalue specified by a given bit-mask.\r
329  *\r
330  * \param lvalue  C lvalue of which to clear bits.\r
331  * \param mask    Bit-mask indicating bits to clear.\r
332  *\r
333  * \return Resulting value with cleared bits.\r
334  */\r
335 #define Clr_bits(lvalue, mask)  ((lvalue) &= ~(mask))\r
336 \r
337 /*! \brief Sets the bits of a C lvalue specified by a given bit-mask.\r
338  *\r
339  * \param lvalue  C lvalue of which to set bits.\r
340  * \param mask    Bit-mask indicating bits to set.\r
341  *\r
342  * \return Resulting value with set bits.\r
343  */\r
344 #define Set_bits(lvalue, mask)  ((lvalue) |=  (mask))\r
345 \r
346 /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.\r
347  *\r
348  * \param lvalue  C lvalue of which to toggle bits.\r
349  * \param mask    Bit-mask indicating bits to toggle.\r
350  *\r
351  * \return Resulting value with toggled bits.\r
352  */\r
353 #define Tgl_bits(lvalue, mask)  ((lvalue) ^=  (mask))\r
354 \r
355 /*! \brief Reads the bit-field of a value specified by a given bit-mask.\r
356  *\r
357  * \param value Value to read a bit-field from.\r
358  * \param mask  Bit-mask indicating the bit-field to read.\r
359  *\r
360  * \return Read bit-field.\r
361  */\r
362 #define Rd_bitfield( value, mask)           (Rd_bits( value, mask) >> ctz(mask))\r
363 \r
364 /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.\r
365  *\r
366  * \param lvalue    C lvalue to write a bit-field to.\r
367  * \param mask      Bit-mask indicating the bit-field to write.\r
368  * \param bitfield  Bit-field to write.\r
369  *\r
370  * \return Resulting value with written bit-field.\r
371  */\r
372 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))\r
373 \r
374 //! @}\r
375 \r
376 \r
377 /*! \brief This macro is used to test fatal errors.\r
378  *\r
379  * The macro tests if the expression is FALSE. If it is, a fatal error is\r
380  * detected and the application hangs up.\r
381  *\r
382  * \param expr  Expression to evaluate and supposed to be nonzero.\r
383  */\r
384 #ifdef _ASSERT_ENABLE_\r
385   #define Assert(expr) \\r
386   {\\r
387     if (!(expr)) while (TRUE);\\r
388   }\r
389 #else\r
390   #define Assert(expr)\r
391 #endif\r
392 \r
393 \r
394 /*! \name Zero-Bit Counting\r
395  *\r
396  * Under AVR32-GCC, __builtin_clz and __builtin_ctz behave like macros when\r
397  * applied to constant expressions (values known at compile time), so they are\r
398  * more optimized than the use of the corresponding assembly instructions and\r
399  * they can be used as constant expressions e.g. to initialize objects having\r
400  * static storage duration, and like the corresponding assembly instructions\r
401  * when applied to non-constant expressions (values unknown at compile time), so\r
402  * they are more optimized than an assembly periphrasis. Hence, clz and ctz\r
403  * ensure a possible and optimized behavior for both constant and non-constant\r
404  * expressions.\r
405  */\r
406 //! @{\r
407 \r
408 /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.\r
409  *\r
410  * \param u Value of which to count the leading zero bits.\r
411  *\r
412  * \return The count of leading zero bits in \a u.\r
413  */\r
414 #if (defined __GNUC__)\r
415   #define clz(u)              __builtin_clz(u)\r
416 #elif (defined __ICCAVR32__)\r
417   #define clz(u)              __count_leading_zeros(u)\r
418 #endif\r
419 \r
420 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.\r
421  *\r
422  * \param u Value of which to count the trailing zero bits.\r
423  *\r
424  * \return The count of trailing zero bits in \a u.\r
425  */\r
426 #if (defined __GNUC__)\r
427   #define ctz(u)              __builtin_ctz(u)\r
428 #elif (defined __ICCAVR32__)\r
429   #define ctz(u)              __count_trailing_zeros(u)\r
430 #endif\r
431 \r
432 //! @}\r
433 \r
434 \r
435 /*! \name Bit Reversing\r
436  */\r
437 //! @{\r
438 \r
439 /*! \brief Reverses the bits of \a u8.\r
440  *\r
441  * \param u8  U8 of which to reverse the bits.\r
442  *\r
443  * \return Value resulting from \a u8 with reversed bits.\r
444  */\r
445 #define bit_reverse8(u8)    ((U8)(bit_reverse32((U8)(u8)) >> 24))\r
446 \r
447 /*! \brief Reverses the bits of \a u16.\r
448  *\r
449  * \param u16 U16 of which to reverse the bits.\r
450  *\r
451  * \return Value resulting from \a u16 with reversed bits.\r
452  */\r
453 #define bit_reverse16(u16)  ((U16)(bit_reverse32((U16)(u16)) >> 16))\r
454 \r
455 /*! \brief Reverses the bits of \a u32.\r
456  *\r
457  * \param u32 U32 of which to reverse the bits.\r
458  *\r
459  * \return Value resulting from \a u32 with reversed bits.\r
460  */\r
461 #if (defined __GNUC__)\r
462   #define bit_reverse32(u32) \\r
463   (\\r
464     {\\r
465       unsigned int __value = (U32)(u32);\\r
466       __asm__ ("brev\t%0" : "+r" (__value) :  : "cc");\\r
467       (U32)__value;\\r
468     }\\r
469   )\r
470 #elif (defined __ICCAVR32__)\r
471   #define bit_reverse32(u32)  ((U32)__bit_reverse((U32)(u32)))\r
472 #endif\r
473 \r
474 /*! \brief Reverses the bits of \a u64.\r
475  *\r
476  * \param u64 U64 of which to reverse the bits.\r
477  *\r
478  * \return Value resulting from \a u64 with reversed bits.\r
479  */\r
480 #define bit_reverse64(u64)  ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\\r
481                                    ((U64)bit_reverse32((U64)(u64)) << 32)))\r
482 \r
483 //! @}\r
484 \r
485 \r
486 /*! \name Alignment\r
487  */\r
488 //! @{\r
489 \r
490 /*! \brief Tests alignment of the number \a val with the \a n boundary.\r
491  *\r
492  * \param val Input value.\r
493  * \param n   Boundary.\r
494  *\r
495  * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.\r
496  */\r
497 #define Test_align(val, n     ) (!Tst_bits( val, (n) - 1     )   )\r
498 \r
499 /*! \brief Gets alignment of the number \a val with respect to the \a n boundary.\r
500  *\r
501  * \param val Input value.\r
502  * \param n   Boundary.\r
503  *\r
504  * \return Alignment of the number \a val with respect to the \a n boundary.\r
505  */\r
506 #define Get_align( val, n     ) (  Rd_bits( val, (n) - 1     )   )\r
507 \r
508 /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.\r
509  *\r
510  * \param lval  Input/output lvalue.\r
511  * \param n     Boundary.\r
512  * \param alg   Alignment.\r
513  *\r
514  * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.\r
515  */\r
516 #define Set_align(lval, n, alg) (  Wr_bits(lval, (n) - 1, alg)   )\r
517 \r
518 /*! \brief Aligns the number \a val with the upper \a n boundary.\r
519  *\r
520  * \param val Input value.\r
521  * \param n   Boundary.\r
522  *\r
523  * \return Value resulting from the number \a val aligned with the upper \a n boundary.\r
524  */\r
525 #define Align_up(  val, n     ) (((val) + ((n) - 1)) & ~((n) - 1))\r
526 \r
527 /*! \brief Aligns the number \a val with the lower \a n boundary.\r
528  *\r
529  * \param val Input value.\r
530  * \param n   Boundary.\r
531  *\r
532  * \return Value resulting from the number \a val aligned with the lower \a n boundary.\r
533  */\r
534 #define Align_down(val, n     ) ( (val)              & ~((n) - 1))\r
535 \r
536 //! @}\r
537 \r
538 \r
539 /*! \name Mathematics\r
540  *\r
541  * The same considerations as for clz and ctz apply here but AVR32-GCC does not\r
542  * provide built-in functions to access the assembly instructions abs, min and\r
543  * max and it does not produce them by itself in most cases, so two sets of\r
544  * macros are defined here:\r
545  *   - Abs, Min and Max to apply to constant expressions (values known at\r
546  *     compile time);\r
547  *   - abs, min and max to apply to non-constant expressions (values unknown at\r
548  *     compile time).\r
549  */\r
550 //! @{\r
551 \r
552 /*! \brief Takes the absolute value of \a a.\r
553  *\r
554  * \param a Input value.\r
555  *\r
556  * \return Absolute value of \a a.\r
557  *\r
558  * \note More optimized if only used with values known at compile time.\r
559  */\r
560 #define Abs(a)              (((a) <  0 ) ? -(a) : (a))\r
561 \r
562 /*! \brief Takes the minimal value of \a a and \a b.\r
563  *\r
564  * \param a Input value.\r
565  * \param b Input value.\r
566  *\r
567  * \return Minimal value of \a a and \a b.\r
568  *\r
569  * \note More optimized if only used with values known at compile time.\r
570  */\r
571 #define Min(a, b)           (((a) < (b)) ?  (a) : (b))\r
572 \r
573 /*! \brief Takes the maximal value of \a a and \a b.\r
574  *\r
575  * \param a Input value.\r
576  * \param b Input value.\r
577  *\r
578  * \return Maximal value of \a a and \a b.\r
579  *\r
580  * \note More optimized if only used with values known at compile time.\r
581  */\r
582 #define Max(a, b)           (((a) > (b)) ?  (a) : (b))\r
583 \r
584 /*! \brief Takes the absolute value of \a a.\r
585  *\r
586  * \param a Input value.\r
587  *\r
588  * \return Absolute value of \a a.\r
589  *\r
590  * \note More optimized if only used with values unknown at compile time.\r
591  */\r
592 #if (defined __GNUC__)\r
593   #define abs(a) \\r
594   (\\r
595     {\\r
596       int __value = (a);\\r
597       __asm__ ("abs\t%0" : "+r" (__value) :  : "cc");\\r
598       __value;\\r
599     }\\r
600   )\r
601 #elif (defined __ICCAVR32__)\r
602   #define abs(a)      Abs(a)\r
603 #endif\r
604 \r
605 /*! \brief Takes the minimal value of \a a and \a b.\r
606  *\r
607  * \param a Input value.\r
608  * \param b Input value.\r
609  *\r
610  * \return Minimal value of \a a and \a b.\r
611  *\r
612  * \note More optimized if only used with values unknown at compile time.\r
613  */\r
614 #if (defined __GNUC__)\r
615   #define min(a, b) \\r
616   (\\r
617     {\\r
618       int __value, __arg_a = (a), __arg_b = (b);\\r
619       __asm__ ("min\t%0, %1, %2" : "=r" (__value) : "r" (__arg_a), "r" (__arg_b));\\r
620       __value;\\r
621     }\\r
622   )\r
623 #elif (defined __ICCAVR32__)\r
624   #define min(a, b)   __min(a, b)\r
625 #endif\r
626 \r
627 /*! \brief Takes the maximal value of \a a and \a b.\r
628  *\r
629  * \param a Input value.\r
630  * \param b Input value.\r
631  *\r
632  * \return Maximal value of \a a and \a b.\r
633  *\r
634  * \note More optimized if only used with values unknown at compile time.\r
635  */\r
636 #if (defined __GNUC__)\r
637   #define max(a, b) \\r
638   (\\r
639     {\\r
640       int __value, __arg_a = (a), __arg_b = (b);\\r
641       __asm__ ("max\t%0, %1, %2" : "=r" (__value) : "r" (__arg_a), "r" (__arg_b));\\r
642       __value;\\r
643     }\\r
644   )\r
645 #elif (defined __ICCAVR32__)\r
646   #define max(a, b)   __max(a, b)\r
647 #endif\r
648 \r
649 //! @}\r
650 \r
651 \r
652 /*! \brief Calls the routine at address \a addr.\r
653  *\r
654  * It generates a long call opcode.\r
655  *\r
656  * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if\r
657  * it is invoked from the CPU supervisor mode.\r
658  *\r
659  * \param addr  Address of the routine to call.\r
660  *\r
661  * \note It may be used as a long jump opcode in some special cases.\r
662  */\r
663 #define Long_call(addr)                   ((*(void (*)(void))(addr))())\r
664 \r
665 /*! \brief Resets the CPU by software.\r
666  *\r
667  * \warning It shall not be called from the CPU application mode.\r
668  */\r
669 #if (defined __GNUC__)\r
670   #define Reset_CPU() \\r
671   (\\r
672     {\\r
673       __asm__ __volatile__ (\\r
674         "lddpc   r9, 3f\n\t"\\r
675         "mfsr    r8, %[SR]\n\t"\\r
676         "bfextu  r8, r8, %[SR_M_OFFSET], %[SR_M_SIZE]\n\t"\\r
677         "cp.w    r8, 0b001\n\t"\\r
678         "breq    0f\n\t"\\r
679         "sub     r8, pc, $ - 1f\n\t"\\r
680         "pushm   r8-r9\n\t"\\r
681         "rete\n"\\r
682         "0:\n\t"\\r
683         "mtsr    %[SR], r9\n"\\r
684         "1:\n\t"\\r
685         "mov     r0, 0\n\t"\\r
686         "mov     r1, 0\n\t"\\r
687         "mov     r2, 0\n\t"\\r
688         "mov     r3, 0\n\t"\\r
689         "mov     r4, 0\n\t"\\r
690         "mov     r5, 0\n\t"\\r
691         "mov     r6, 0\n\t"\\r
692         "mov     r7, 0\n\t"\\r
693         "mov     r8, 0\n\t"\\r
694         "mov     r9, 0\n\t"\\r
695         "mov     r10, 0\n\t"\\r
696         "mov     r11, 0\n\t"\\r
697         "mov     r12, 0\n\t"\\r
698         "mov     sp, 0\n\t"\\r
699         "stdsp   sp[0], sp\n\t"\\r
700         "ldmts   sp, sp\n\t"\\r
701         "mov     lr, 0\n\t"\\r
702         "lddpc   pc, 2f\n\t"\\r
703         ".balign 4\n"\\r
704         "2:\n\t"\\r
705         ".word   _start\n"\\r
706         "3:\n\t"\\r
707         ".word   %[RESET_SR]"\\r
708         :\\r
709         : [SR] "i" (AVR32_SR),\\r
710           [SR_M_OFFSET] "i" (AVR32_SR_M_OFFSET),\\r
711           [SR_M_SIZE] "i" (AVR32_SR_M_SIZE),\\r
712           [RESET_SR] "i" (AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | (AVR32_SR_M_SUP << AVR32_SR_M_OFFSET))\\r
713       );\\r
714     }\\r
715   )\r
716 #elif (defined __ICCAVR32__)\r
717   #define Reset_CPU() \\r
718   {\\r
719     extern void *volatile __program_start;\\r
720     __asm__ __volatile__ (\\r
721       "mov     r7, LWRD(__program_start)\n\t"\\r
722       "orh     r7, HWRD(__program_start)\n\t"\\r
723       "mov     r9, LWRD("ASTRINGZ(AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | (AVR32_SR_M_SUP << AVR32_SR_M_OFFSET))")\n\t"\\r
724       "orh     r9, HWRD("ASTRINGZ(AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | (AVR32_SR_M_SUP << AVR32_SR_M_OFFSET))")\n\t"\\r
725       "mfsr    r8, "ASTRINGZ(AVR32_SR)"\n\t"\\r
726       "bfextu  r8, r8, "ASTRINGZ(AVR32_SR_M_OFFSET)", "ASTRINGZ(AVR32_SR_M_SIZE)"\n\t"\\r
727       "cp.w    r8, 001b\n\t"\\r
728       "breq    $ + 10\n\t"\\r
729       "sub     r8, pc, -12\n\t"\\r
730       "pushm   r8-r9\n\t"\\r
731       "rete\n\t"\\r
732       "mtsr    "ASTRINGZ(AVR32_SR)", r9\n\t"\\r
733       "mov     r0, 0\n\t"\\r
734       "mov     r1, 0\n\t"\\r
735       "mov     r2, 0\n\t"\\r
736       "mov     r3, 0\n\t"\\r
737       "mov     r4, 0\n\t"\\r
738       "mov     r5, 0\n\t"\\r
739       "mov     r6, 0\n\t"\\r
740       "st.w    r0[4], r7\n\t"\\r
741       "mov     r7, 0\n\t"\\r
742       "mov     r8, 0\n\t"\\r
743       "mov     r9, 0\n\t"\\r
744       "mov     r10, 0\n\t"\\r
745       "mov     r11, 0\n\t"\\r
746       "mov     r12, 0\n\t"\\r
747       "mov     sp, 0\n\t"\\r
748       "stdsp   sp[0], sp\n\t"\\r
749       "ldmts   sp, sp\n\t"\\r
750       "mov     lr, 0\n\t"\\r
751       "ld.w    pc, lr[4]"\\r
752     );\\r
753     __program_start;\\r
754   }\r
755 #endif\r
756 \r
757 \r
758 /*! \name System Register Access\r
759  */\r
760 //! @{\r
761 \r
762 /*! \brief Gets the value of the \a sysreg system register.\r
763  *\r
764  * \param sysreg  Address of the system register of which to get the value.\r
765  *\r
766  * \return Value of the \a sysreg system register.\r
767  */\r
768 #if (defined __GNUC__)\r
769   #define Get_system_register(sysreg)         __builtin_mfsr(sysreg)\r
770 #elif (defined __ICCAVR32__)\r
771   #define Get_system_register(sysreg)         __get_system_register(sysreg)\r
772 #endif\r
773 \r
774 /*! \brief Sets the value of the \a sysreg system register to \a value.\r
775  *\r
776  * \param sysreg  Address of the system register of which to set the value.\r
777  * \param value   Value to set the \a sysreg system register to.\r
778  */\r
779 #if (defined __GNUC__)\r
780   #define Set_system_register(sysreg, value)  __builtin_mtsr(sysreg, value)\r
781 #elif (defined __ICCAVR32__)\r
782   #define Set_system_register(sysreg, value)  __set_system_register(sysreg, value)\r
783 #endif\r
784 \r
785 //! @}\r
786 \r
787 \r
788 /*! \name CPU Status Register Access\r
789  */\r
790 //! @{\r
791 \r
792 /*! \brief Tells whether exceptions are globally enabled.\r
793  *\r
794  * \return \c 1 if exceptions are globally enabled, else \c 0.\r
795  */\r
796 #define Is_global_exception_enabled()         (!Tst_bits(Get_system_register(AVR32_SR), AVR32_SR_EM_MASK))\r
797 \r
798 /*! \brief Disables exceptions globally.\r
799  */\r
800 #if (defined __GNUC__)\r
801   #define Disable_global_exception()          ({__asm__ __volatile__ ("ssrf\t%0" :  : "i" (AVR32_SR_EM_OFFSET));})\r
802 #elif (defined __ICCAVR32__)\r
803   #define Disable_global_exception()          (__set_status_flag(AVR32_SR_EM_OFFSET))\r
804 #endif\r
805 \r
806 /*! \brief Enables exceptions globally.\r
807  */\r
808 #if (defined __GNUC__)\r
809   #define Enable_global_exception()           ({__asm__ __volatile__ ("csrf\t%0" :  : "i" (AVR32_SR_EM_OFFSET));})\r
810 #elif (defined __ICCAVR32__)\r
811   #define Enable_global_exception()           (__clear_status_flag(AVR32_SR_EM_OFFSET))\r
812 #endif\r
813 \r
814 /*! \brief Tells whether interrupts are globally enabled.\r
815  *\r
816  * \return \c 1 if interrupts are globally enabled, else \c 0.\r
817  */\r
818 #define Is_global_interrupt_enabled()         (!Tst_bits(Get_system_register(AVR32_SR), AVR32_SR_GM_MASK))\r
819 \r
820 /*! \brief Disables interrupts globally.\r
821  */\r
822 #if (defined __GNUC__)\r
823   #define Disable_global_interrupt()          ({__asm__ __volatile__ ("ssrf\t%0" :  : "i" (AVR32_SR_GM_OFFSET));})\r
824 #elif (defined __ICCAVR32__)\r
825   #define Disable_global_interrupt()          (__disable_interrupt())\r
826 #endif\r
827 \r
828 /*! \brief Enables interrupts globally.\r
829  */\r
830 #if (defined __GNUC__)\r
831   #define Enable_global_interrupt()           ({__asm__ __volatile__ ("csrf\t%0" :  : "i" (AVR32_SR_GM_OFFSET));})\r
832 #elif (defined __ICCAVR32__)\r
833   #define Enable_global_interrupt()           (__enable_interrupt())\r
834 #endif\r
835 \r
836 /*! \brief Tells whether interrupt level \a int_level is enabled.\r
837  *\r
838  * \param int_level Interrupt level (0 to 3).\r
839  *\r
840  * \return \c 1 if interrupt level \a int_level is enabled, else \c 0.\r
841  */\r
842 #define Is_interrupt_level_enabled(int_level) (!Tst_bits(Get_system_register(AVR32_SR), TPASTE3(AVR32_SR_I, int_level, M_MASK)))\r
843 \r
844 /*! \brief Disables interrupt level \a int_level.\r
845  *\r
846  * \param int_level Interrupt level to disable (0 to 3).\r
847  */\r
848 #if (defined __GNUC__)\r
849   #define Disable_interrupt_level(int_level)  ({__asm__ __volatile__ ("ssrf\t%0" :  : "i" (TPASTE3(AVR32_SR_I, int_level, M_OFFSET)));})\r
850 #elif (defined __ICCAVR32__)\r
851   #define Disable_interrupt_level(int_level)  (__set_status_flag(TPASTE3(AVR32_SR_I, int_level, M_OFFSET)))\r
852 #endif\r
853 \r
854 /*! \brief Enables interrupt level \a int_level.\r
855  *\r
856  * \param int_level Interrupt level to enable (0 to 3).\r
857  */\r
858 #if (defined __GNUC__)\r
859   #define Enable_interrupt_level(int_level)   ({__asm__ __volatile__ ("csrf\t%0" :  : "i" (TPASTE3(AVR32_SR_I, int_level, M_OFFSET)));})\r
860 #elif (defined __ICCAVR32__)\r
861   #define Enable_interrupt_level(int_level)   (__clear_status_flag(TPASTE3(AVR32_SR_I, int_level, M_OFFSET)))\r
862 #endif\r
863 \r
864 /*! \brief Protects subsequent code from interrupts.\r
865  */\r
866 #define AVR32_ENTER_CRITICAL_REGION( ) \\r
867   { \\r
868   Bool global_interrupt_enabled = Is_global_interrupt_enabled(); \\r
869   Disable_global_interrupt(); // Disable the appropriate interrupts.\r
870 \r
871 /*! \brief This macro must always be used in conjunction with AVR32_ENTER_CRITICAL_REGION\r
872  *         so that interrupts are enabled again.\r
873  */\r
874 #define AVR32_LEAVE_CRITICAL_REGION( ) \\r
875   if (global_interrupt_enabled) Enable_global_interrupt(); \\r
876   }\r
877 \r
878 //! @}\r
879 \r
880 \r
881 /*! \name Debug Register Access\r
882  */\r
883 //! @{\r
884 \r
885 /*! \brief Gets the value of the \a dbgreg debug register.\r
886  *\r
887  * \param dbgreg  Address of the debug register of which to get the value.\r
888  *\r
889  * \return Value of the \a dbgreg debug register.\r
890  */\r
891 #if (defined __GNUC__)\r
892   #define Get_debug_register(dbgreg)          __builtin_mfdr(dbgreg)\r
893 #elif (defined __ICCAVR32__)\r
894   #define Get_debug_register(dbgreg)          __get_debug_register(dbgreg)\r
895 #endif\r
896 \r
897 /*! \brief Sets the value of the \a dbgreg debug register to \a value.\r
898  *\r
899  * \param dbgreg  Address of the debug register of which to set the value.\r
900  * \param value   Value to set the \a dbgreg debug register to.\r
901  */\r
902 #if (defined __GNUC__)\r
903   #define Set_debug_register(dbgreg, value)   __builtin_mtdr(dbgreg, value)\r
904 #elif (defined __ICCAVR32__)\r
905   #define Set_debug_register(dbgreg, value)   __set_debug_register(dbgreg, value)\r
906 #endif\r
907 \r
908 //! @}\r
909 \r
910 #endif  // __AVR32_ABI_COMPILER__\r
911 \r
912 \r
913 //! Boolean evaluating MCU little endianism.\r
914 #if ((defined __GNUC__) && (defined __AVR32__)) || ((defined __ICCAVR32__) || (defined __AAVR32__))\r
915   #define LITTLE_ENDIAN_MCU     FALSE\r
916 #else\r
917   #error If you are here, you should check what is exactly the processor you are using...\r
918   #define LITTLE_ENDIAN_MCU     FALSE\r
919 #endif\r
920 \r
921 // Check that MCU endianism is correctly defined.\r
922 #ifndef LITTLE_ENDIAN_MCU\r
923   #error YOU MUST define the MCU endianism with LITTLE_ENDIAN_MCU: either FALSE or TRUE\r
924 #endif\r
925 \r
926 //! Boolean evaluating MCU big endianism.\r
927 #define BIG_ENDIAN_MCU        (!LITTLE_ENDIAN_MCU)\r
928 \r
929 \r
930 #ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.\r
931 \r
932 /*! \name MCU Endianism Handling\r
933  */\r
934 //! @{\r
935 \r
936 #if (LITTLE_ENDIAN_MCU==TRUE)\r
937   #define LSB(u16)        (((U8  *)&(u16))[0])  //!< Least significant byte of \a u16.\r
938   #define MSB(u16)        (((U8  *)&(u16))[1])  //!< Most significant byte of \a u16.\r
939 \r
940   #define LSH(u32)        (((U16 *)&(u32))[0])  //!< Least significant half-word of \a u32.\r
941   #define MSH(u32)        (((U16 *)&(u32))[1])  //!< Most significant half-word of \a u32.\r
942   #define LSB0W(u32)      (((U8  *)&(u32))[0])  //!< Least significant byte of 1st rank of \a u32.\r
943   #define LSB1W(u32)      (((U8  *)&(u32))[1])  //!< Least significant byte of 2nd rank of \a u32.\r
944   #define LSB2W(u32)      (((U8  *)&(u32))[2])  //!< Least significant byte of 3rd rank of \a u32.\r
945   #define LSB3W(u32)      (((U8  *)&(u32))[3])  //!< Least significant byte of 4th rank of \a u32.\r
946   #define MSB3W(u32)      LSB0W(u32)            //!< Most significant byte of 4th rank of \a u32.\r
947   #define MSB2W(u32)      LSB1W(u32)            //!< Most significant byte of 3rd rank of \a u32.\r
948   #define MSB1W(u32)      LSB2W(u32)            //!< Most significant byte of 2nd rank of \a u32.\r
949   #define MSB0W(u32)      LSB3W(u32)            //!< Most significant byte of 1st rank of \a u32.\r
950 \r
951   #define LSW(u64)        (((U32 *)&(u64))[0])  //!< Least significant word of \a u64.\r
952   #define MSW(u64)        (((U32 *)&(u64))[1])  //!< Most significant word of \a u64.\r
953   #define LSH0(u64)       (((U16 *)&(u64))[0])  //!< Least significant half-word of 1st rank of \a u64.\r
954   #define LSH1(u64)       (((U16 *)&(u64))[1])  //!< Least significant half-word of 2nd rank of \a u64.\r
955   #define LSH2(u64)       (((U16 *)&(u64))[2])  //!< Least significant half-word of 3rd rank of \a u64.\r
956   #define LSH3(u64)       (((U16 *)&(u64))[3])  //!< Least significant half-word of 4th rank of \a u64.\r
957   #define MSH3(u64)       LSH0(u64)             //!< Most significant half-word of 4th rank of \a u64.\r
958   #define MSH2(u64)       LSH1(u64)             //!< Most significant half-word of 3rd rank of \a u64.\r
959   #define MSH1(u64)       LSH2(u64)             //!< Most significant half-word of 2nd rank of \a u64.\r
960   #define MSH0(u64)       LSH3(u64)             //!< Most significant half-word of 1st rank of \a u64.\r
961   #define LSB0D(u64)      (((U8  *)&(u64))[0])  //!< Least significant byte of 1st rank of \a u64.\r
962   #define LSB1D(u64)      (((U8  *)&(u64))[1])  //!< Least significant byte of 2nd rank of \a u64.\r
963   #define LSB2D(u64)      (((U8  *)&(u64))[2])  //!< Least significant byte of 3rd rank of \a u64.\r
964   #define LSB3D(u64)      (((U8  *)&(u64))[3])  //!< Least significant byte of 4th rank of \a u64.\r
965   #define LSB4D(u64)      (((U8  *)&(u64))[4])  //!< Least significant byte of 5th rank of \a u64.\r
966   #define LSB5D(u64)      (((U8  *)&(u64))[5])  //!< Least significant byte of 6th rank of \a u64.\r
967   #define LSB6D(u64)      (((U8  *)&(u64))[6])  //!< Least significant byte of 7th rank of \a u64.\r
968   #define LSB7D(u64)      (((U8  *)&(u64))[7])  //!< Least significant byte of 8th rank of \a u64.\r
969   #define MSB7D(u64)      LSB0D(u64)            //!< Most significant byte of 8th rank of \a u64.\r
970   #define MSB6D(u64)      LSB1D(u64)            //!< Most significant byte of 7th rank of \a u64.\r
971   #define MSB5D(u64)      LSB2D(u64)            //!< Most significant byte of 6th rank of \a u64.\r
972   #define MSB4D(u64)      LSB3D(u64)            //!< Most significant byte of 5th rank of \a u64.\r
973   #define MSB3D(u64)      LSB4D(u64)            //!< Most significant byte of 4th rank of \a u64.\r
974   #define MSB2D(u64)      LSB5D(u64)            //!< Most significant byte of 3rd rank of \a u64.\r
975   #define MSB1D(u64)      LSB6D(u64)            //!< Most significant byte of 2nd rank of \a u64.\r
976   #define MSB0D(u64)      LSB7D(u64)            //!< Most significant byte of 1st rank of \a u64.\r
977 \r
978 #elif (BIG_ENDIAN_MCU==TRUE) \r
979   #define MSB(u16)        (((U8  *)&(u16))[0])  //!< Most significant byte of \a u16.\r
980   #define LSB(u16)        (((U8  *)&(u16))[1])  //!< Least significant byte of \a u16.\r
981 \r
982   #define MSH(u32)        (((U16 *)&(u32))[0])  //!< Most significant half-word of \a u32.\r
983   #define LSH(u32)        (((U16 *)&(u32))[1])  //!< Least significant half-word of \a u32.\r
984   #define MSB0W(u32)      (((U8  *)&(u32))[0])  //!< Most significant byte of 1st rank of \a u32.\r
985   #define MSB1W(u32)      (((U8  *)&(u32))[1])  //!< Most significant byte of 2nd rank of \a u32.\r
986   #define MSB2W(u32)      (((U8  *)&(u32))[2])  //!< Most significant byte of 3rd rank of \a u32.\r
987   #define MSB3W(u32)      (((U8  *)&(u32))[3])  //!< Most significant byte of 4th rank of \a u32.\r
988   #define LSB3W(u32)      MSB0W(u32)            //!< Least significant byte of 4th rank of \a u32.\r
989   #define LSB2W(u32)      MSB1W(u32)            //!< Least significant byte of 3rd rank of \a u32.\r
990   #define LSB1W(u32)      MSB2W(u32)            //!< Least significant byte of 2nd rank of \a u32.\r
991   #define LSB0W(u32)      MSB3W(u32)            //!< Least significant byte of 1st rank of \a u32.\r
992 \r
993   #define MSW(u64)        (((U32 *)&(u64))[0])  //!< Most significant word of \a u64.\r
994   #define LSW(u64)        (((U32 *)&(u64))[1])  //!< Least significant word of \a u64.\r
995   #define MSH0(u64)       (((U16 *)&(u64))[0])  //!< Most significant half-word of 1st rank of \a u64.\r
996   #define MSH1(u64)       (((U16 *)&(u64))[1])  //!< Most significant half-word of 2nd rank of \a u64.\r
997   #define MSH2(u64)       (((U16 *)&(u64))[2])  //!< Most significant half-word of 3rd rank of \a u64.\r
998   #define MSH3(u64)       (((U16 *)&(u64))[3])  //!< Most significant half-word of 4th rank of \a u64.\r
999   #define LSH3(u64)       MSH0(u64)             //!< Least significant half-word of 4th rank of \a u64.\r
1000   #define LSH2(u64)       MSH1(u64)             //!< Least significant half-word of 3rd rank of \a u64.\r
1001   #define LSH1(u64)       MSH2(u64)             //!< Least significant half-word of 2nd rank of \a u64.\r
1002   #define LSH0(u64)       MSH3(u64)             //!< Least significant half-word of 1st rank of \a u64.\r
1003   #define MSB0D(u64)      (((U8  *)&(u64))[0])  //!< Most significant byte of 1st rank of \a u64.\r
1004   #define MSB1D(u64)      (((U8  *)&(u64))[1])  //!< Most significant byte of 2nd rank of \a u64.\r
1005   #define MSB2D(u64)      (((U8  *)&(u64))[2])  //!< Most significant byte of 3rd rank of \a u64.\r
1006   #define MSB3D(u64)      (((U8  *)&(u64))[3])  //!< Most significant byte of 4th rank of \a u64.\r
1007   #define MSB4D(u64)      (((U8  *)&(u64))[4])  //!< Most significant byte of 5th rank of \a u64.\r
1008   #define MSB5D(u64)      (((U8  *)&(u64))[5])  //!< Most significant byte of 6th rank of \a u64.\r
1009   #define MSB6D(u64)      (((U8  *)&(u64))[6])  //!< Most significant byte of 7th rank of \a u64.\r
1010   #define MSB7D(u64)      (((U8  *)&(u64))[7])  //!< Most significant byte of 8th rank of \a u64.\r
1011   #define LSB7D(u64)      MSB0D(u64)            //!< Least significant byte of 8th rank of \a u64.\r
1012   #define LSB6D(u64)      MSB1D(u64)            //!< Least significant byte of 7th rank of \a u64.\r
1013   #define LSB5D(u64)      MSB2D(u64)            //!< Least significant byte of 6th rank of \a u64.\r
1014   #define LSB4D(u64)      MSB3D(u64)            //!< Least significant byte of 5th rank of \a u64.\r
1015   #define LSB3D(u64)      MSB4D(u64)            //!< Least significant byte of 4th rank of \a u64.\r
1016   #define LSB2D(u64)      MSB5D(u64)            //!< Least significant byte of 3rd rank of \a u64.\r
1017   #define LSB1D(u64)      MSB6D(u64)            //!< Least significant byte of 2nd rank of \a u64.\r
1018   #define LSB0D(u64)      MSB7D(u64)            //!< Least significant byte of 1st rank of \a u64.\r
1019 \r
1020 #else\r
1021   #error  Unknown endianism.\r
1022 #endif\r
1023 \r
1024 //! @}\r
1025 \r
1026 \r
1027 /*! \name Endianism Conversion\r
1028  *\r
1029  * The same considerations as for clz and ctz apply here but AVR32-GCC's\r
1030  * __builtin_bswap_16 and __builtin_bswap_32 do not behave like macros when\r
1031  * applied to constant expressions, so two sets of macros are defined here:\r
1032  *   - Swap16, Swap32 and Swap64 to apply to constant expressions (values known\r
1033  *     at compile time);\r
1034  *   - swap16, swap32 and swap64 to apply to non-constant expressions (values\r
1035  *     unknown at compile time).\r
1036  */\r
1037 //! @{\r
1038 \r
1039 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).\r
1040  *\r
1041  * \param u16 U16 of which to toggle the endianism.\r
1042  *\r
1043  * \return Value resulting from \a u16 with toggled endianism.\r
1044  *\r
1045  * \note More optimized if only used with values known at compile time.\r
1046  */\r
1047 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\\r
1048                            ((U16)(u16) << 8)))\r
1049 \r
1050 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).\r
1051  *\r
1052  * \param u32 U32 of which to toggle the endianism.\r
1053  *\r
1054  * \return Value resulting from \a u32 with toggled endianism.\r
1055  *\r
1056  * \note More optimized if only used with values known at compile time.\r
1057  */\r
1058 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\\r
1059                            ((U32)Swap16((U32)(u32)) << 16)))\r
1060 \r
1061 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).\r
1062  *\r
1063  * \param u64 U64 of which to toggle the endianism.\r
1064  *\r
1065  * \return Value resulting from \a u64 with toggled endianism.\r
1066  *\r
1067  * \note More optimized if only used with values known at compile time.\r
1068  */\r
1069 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\\r
1070                            ((U64)Swap32((U64)(u64)) << 32)))\r
1071 \r
1072 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).\r
1073  *\r
1074  * \param u16 U16 of which to toggle the endianism.\r
1075  *\r
1076  * \return Value resulting from \a u16 with toggled endianism.\r
1077  *\r
1078  * \note More optimized if only used with values unknown at compile time.\r
1079  */\r
1080 #if (defined __GNUC__)\r
1081   #define swap16(u16) ((U16)__builtin_bswap_16((U16)(u16)))\r
1082 #elif (defined __ICCAVR32__)\r
1083   #define swap16(u16) ((U16)__swap_bytes_in_halfwords((U16)(u16)))\r
1084 #endif\r
1085 \r
1086 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).\r
1087  *\r
1088  * \param u32 U32 of which to toggle the endianism.\r
1089  *\r
1090  * \return Value resulting from \a u32 with toggled endianism.\r
1091  *\r
1092  * \note More optimized if only used with values unknown at compile time.\r
1093  */\r
1094 #if (defined __GNUC__)\r
1095   #define swap32(u32) ((U32)__builtin_bswap_32((U32)(u32)))\r
1096 #elif (defined __ICCAVR32__)\r
1097   #define swap32(u32) ((U32)__swap_bytes((U32)(u32)))\r
1098 #endif\r
1099 \r
1100 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).\r
1101  *\r
1102  * \param u64 U64 of which to toggle the endianism.\r
1103  *\r
1104  * \return Value resulting from \a u64 with toggled endianism.\r
1105  *\r
1106  * \note More optimized if only used with values unknown at compile time.\r
1107  */\r
1108 #define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\\r
1109                            ((U64)swap32((U64)(u64)) << 32)))\r
1110 \r
1111 //! @}\r
1112 \r
1113 \r
1114 /*! \name Target Abstraction\r
1115  */\r
1116 //! @{\r
1117 \r
1118 #define _GLOBEXT_           extern      //!< extern storage-class specifier.\r
1119 #define _CONST_TYPE_        const       //!< const type qualifier.\r
1120 #define _MEM_TYPE_SLOW_                 //!< Slow memory type.\r
1121 #define _MEM_TYPE_MEDFAST_              //!< Fairly fast memory type.\r
1122 #define _MEM_TYPE_FAST_                 //!< Fast memory type.\r
1123 \r
1124 typedef U8                  Byte;       //!< 8-bit unsigned integer.\r
1125 \r
1126 #define memcmp_ram2ram      memcmp      //!< Target-specific memcmp of RAM to RAM.\r
1127 #define memcmp_code2ram     memcmp      //!< Target-specific memcmp of RAM to NVRAM.\r
1128 #define memcpy_ram2ram      memcpy      //!< Target-specific memcpy from RAM to RAM.\r
1129 #define memcpy_code2ram     memcpy      //!< Target-specific memcpy from NVRAM to RAM.\r
1130 \r
1131 #define LSB0(u32)           LSB0W(u32)  //!< Least significant byte of 1st rank of \a u32.\r
1132 #define LSB1(u32)           LSB1W(u32)  //!< Least significant byte of 2nd rank of \a u32.\r
1133 #define LSB2(u32)           LSB2W(u32)  //!< Least significant byte of 3rd rank of \a u32.\r
1134 #define LSB3(u32)           LSB3W(u32)  //!< Least significant byte of 4th rank of \a u32.\r
1135 #define MSB3(u32)           MSB3W(u32)  //!< Most significant byte of 4th rank of \a u32.\r
1136 #define MSB2(u32)           MSB2W(u32)  //!< Most significant byte of 3rd rank of \a u32.\r
1137 #define MSB1(u32)           MSB1W(u32)  //!< Most significant byte of 2nd rank of \a u32.\r
1138 #define MSB0(u32)           MSB0W(u32)  //!< Most significant byte of 1st rank of \a u32.\r
1139 \r
1140 //! @}\r
1141 \r
1142 #endif  // __AVR32_ABI_COMPILER__\r
1143 \r
1144 \r
1145 #endif  // _COMPILER_H_\r