1 /*This file is prepared for Doxygen automatic documentation generation.*/
2 /*! \file *********************************************************************
4 * \brief Compiler file for AVR32.
6 * This file defines commonly used types and macros.
8 * - Compiler: IAR EWAVR32 and GNU GCC for AVR32
9 * - Supported devices: All AVR32 devices can be used.
12 * \author Atmel Corporation: http://www.atmel.com \n
13 * Support and FAQ: http://support.atmel.no/
15 ******************************************************************************/
17 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
22 * 1. Redistributions of source code must retain the above copyright notice,
23 * this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
29 * 3. The name of ATMEL may not be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
35 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #if (__GNUC__ && __AVR32__) || (__ICCAVR32__ || __AAVR32__)
49 # include <avr32/io.h>
52 # include <intrinsics.h>
54 #include "preprocessor.h"
57 //_____ D E C L A R A T I O N S ____________________________________________
59 #ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.
67 /*! \name Compiler Keywords
69 * Port of some keywords from GNU GCC for AVR32 to IAR Embedded Workbench for Atmel AVR32.
73 #define __inline__ inline
83 typedef unsigned char Bool; //!< Boolean.
84 typedef unsigned char U8 ; //!< 8-bit unsigned integer.
85 typedef unsigned short int U16; //!< 16-bit unsigned integer.
86 typedef unsigned long int U32; //!< 32-bit unsigned integer.
87 typedef unsigned long long int U64; //!< 64-bit unsigned integer.
88 typedef signed char S8 ; //!< 8-bit signed integer.
89 typedef signed short int S16; //!< 16-bit signed integer.
90 typedef signed long int S32; //!< 32-bit signed integer.
91 typedef signed long long int S64; //!< 64-bit signed integer.
92 typedef float F32; //!< 32-bit floating-point number.
93 typedef double F64; //!< 64-bit floating-point number.
97 /*! \name Status Types
100 typedef Bool Status_bool_t; //!< Boolean status.
101 typedef U8 Status_t; //!< 8-bit-coded status.
105 /*! \name Aliasing Aggregate Types
133 //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
142 //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
145 volatile U64 *u64ptr;
146 volatile U32 *u32ptr;
147 volatile U16 *u16ptr;
151 //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
160 //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
163 const volatile U64 *u64ptr;
164 const volatile U32 *u32ptr;
165 const volatile U16 *u16ptr;
166 const volatile U8 *u8ptr ;
169 //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
178 //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
181 volatile U64 *u64ptr;
182 volatile U32 *u32ptr;
183 volatile U16 *u16ptr;
187 //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
196 //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
199 const volatile U64 *u64ptr;
200 const volatile U32 *u32ptr;
201 const volatile U16 *u16ptr;
202 const volatile U8 *u8ptr ;
207 #endif // __AVR32_ABI_COMPILER__
210 //_____ M A C R O S ________________________________________________________
212 /*! \name Usual Constants
234 #ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.
236 /*! \name Bit-Field Handling
240 /*! \brief Reads the bits of a value specified by a given bit-mask.
242 * \param value Value to read bits from.
243 * \param mask Bit-mask indicating bits to read.
247 #define Rd_bits( value, mask) ((value) & (mask))
249 /*! \brief Writes the bits of a C lvalue specified by a given bit-mask.
251 * \param lvalue C lvalue to write bits to.
252 * \param mask Bit-mask indicating bits to write.
253 * \param bits Bits to write.
255 * \return Resulting value with written bits.
257 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
260 /*! \brief Tests the bits of a value specified by a given bit-mask.
262 * \param value Value of which to test bits.
263 * \param mask Bit-mask indicating bits to test.
265 * \return \c 1 if at least one of the tested bits is set, else \c 0.
267 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
269 /*! \brief Clears the bits of a C lvalue specified by a given bit-mask.
271 * \param lvalue C lvalue of which to clear bits.
272 * \param mask Bit-mask indicating bits to clear.
274 * \return Resulting value with cleared bits.
276 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
278 /*! \brief Sets the bits of a C lvalue specified by a given bit-mask.
280 * \param lvalue C lvalue of which to set bits.
281 * \param mask Bit-mask indicating bits to set.
283 * \return Resulting value with set bits.
285 #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
287 /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.
289 * \param lvalue C lvalue of which to toggle bits.
290 * \param mask Bit-mask indicating bits to toggle.
292 * \return Resulting value with toggled bits.
294 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
296 /*! \brief Reads the bit-field of a value specified by a given bit-mask.
298 * \param value Value to read a bit-field from.
299 * \param mask Bit-mask indicating the bit-field to read.
301 * \return Read bit-field.
303 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
305 /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
307 * \param lvalue C lvalue to write a bit-field to.
308 * \param mask Bit-mask indicating the bit-field to write.
309 * \param bitfield Bit-field to write.
311 * \return Resulting value with written bit-field.
313 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))
318 /*! \brief This macro is used to test fatal errors.
320 * The macro tests if the expression is FALSE. If it is, a fatal error is
321 * detected and the application hangs up.
323 * \param expr Expression to evaluate and supposed to be nonzero.
325 #ifdef _ASSERT_ENABLE_
326 #define Assert(expr) \
328 if (!(expr)) while (TRUE);\
335 /*! \name Zero-Bit Counting
337 * Under AVR32-GCC, __builtin_clz and __builtin_ctz behave like macros when
338 * applied to constant expressions (values known at compile time), so they are
339 * more optimized than the use of the corresponding assembly instructions and
340 * they can be used as constant expressions e.g. to initialize objects having
341 * static storage duration, and like the corresponding assembly instructions
342 * when applied to non-constant expressions (values unknown at compile time), so
343 * they are more optimized than an assembly periphrasis. Hence, clz and ctz
344 * ensure a possible and optimized behavior for both constant and non-constant
349 /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
351 * \param u Value of which to count the leading zero bits.
353 * \return The count of leading zero bits in \a u.
356 #define clz(u) __builtin_clz(u)
358 #define clz(u) __count_leading_zeros(u)
361 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
363 * \param u Value of which to count the trailing zero bits.
365 * \return The count of trailing zero bits in \a u.
368 #define ctz(u) __builtin_ctz(u)
370 #define ctz(u) __count_trailing_zeros(u)
380 /*! \brief Tests alignment of the number \a val with the \a n boundary.
382 * \param val Input value.
385 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
387 #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) )
389 /*! \brief Gets alignment of the number \a val with respect to the \a n boundary.
391 * \param val Input value.
394 * \return Alignment of the number \a val with respect to the \a n boundary.
396 #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) )
398 /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
400 * \param lval Input/output lvalue.
402 * \param alg Alignment.
404 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
406 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
408 /*! \brief Aligns the number \a val with the upper \a n boundary.
410 * \param val Input value.
413 * \return Value resulting from the number \a val aligned with the upper \a n boundary.
415 #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1))
417 /*! \brief Aligns the number \a val with the lower \a n boundary.
419 * \param val Input value.
422 * \return Value resulting from the number \a val aligned with the lower \a n boundary.
424 #define Align_down(val, n ) ( (val) & ~((n) - 1))
429 /*! \name Mathematics
431 * The same considerations as for clz and ctz apply here but AVR32-GCC does not
432 * provide built-in functions to access the assembly instructions abs, min and
433 * max and it does not produce them by itself in most cases, so two sets of
434 * macros are defined here:
435 * - Abs, Min and Max to apply to constant expressions (values known at
437 * - abs, min and max to apply to non-constant expressions (values unknown at
442 /*! \brief Takes the absolute value of \a a.
444 * \param a Input value.
446 * \return Absolute value of \a a.
448 * \note More optimized if only used with values known at compile time.
450 #define Abs(a) (((a) < 0 ) ? -(a) : (a))
452 /*! \brief Takes the minimal value of \a a and \a b.
454 * \param a Input value.
455 * \param b Input value.
457 * \return Minimal value of \a a and \a b.
459 * \note More optimized if only used with values known at compile time.
461 #define Min(a, b) (((a) < (b)) ? (a) : (b))
463 /*! \brief Takes the maximal value of \a a and \a b.
465 * \param a Input value.
466 * \param b Input value.
468 * \return Maximal value of \a a and \a b.
470 * \note More optimized if only used with values known at compile time.
472 #define Max(a, b) (((a) > (b)) ? (a) : (b))
474 /*! \brief Takes the absolute value of \a a.
476 * \param a Input value.
478 * \return Absolute value of \a a.
480 * \note More optimized if only used with values unknown at compile time.
487 __asm__ ("abs\t%0" : "+r" (__value) : : "cc");\
492 #define abs(a) Abs(a)
495 /*! \brief Takes the minimal value of \a a and \a b.
497 * \param a Input value.
498 * \param b Input value.
500 * \return Minimal value of \a a and \a b.
502 * \note More optimized if only used with values unknown at compile time.
508 int __value, __arg_a = (a), __arg_b = (b);\
509 __asm__ ("min\t%0, %1, %2" : "=r" (__value) : "r" (__arg_a), "r" (__arg_b));\
514 #define min(a, b) __min(a, b)
517 /*! \brief Takes the maximal value of \a a and \a b.
519 * \param a Input value.
520 * \param b Input value.
522 * \return Maximal value of \a a and \a b.
524 * \note More optimized if only used with values unknown at compile time.
530 int __value, __arg_a = (a), __arg_b = (b);\
531 __asm__ ("max\t%0, %1, %2" : "=r" (__value) : "r" (__arg_a), "r" (__arg_b));\
536 #define max(a, b) __max(a, b)
542 /*! \brief Calls the routine at address \a addr.
544 * It generates a long call opcode.
546 * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
547 * it is invoked from the CPU supervisor mode.
549 * \param addr Address of the routine to call.
551 * \note It may be used as a long jump opcode in some special cases.
553 #define Long_call(addr) ((*(void (*)(void))(addr))())
555 /*! \brief Resets the CPU by software.
557 * \warning It shall not be called from the CPU application mode.
560 #define Reset_CPU() \
563 __asm__ __volatile__ (\
565 "mfsr r8, %[SR]\n\t"\
566 "bfextu r8, r8, %[SR_MX_OFFSET], %[SR_MX_SIZE]\n\t"\
567 "cp.w r8, 0b001\n\t"\
569 "sub r8, pc, $ - 1f\n\t"\
589 "stdsp sp[0], sp\n\t"\
599 : [SR] "i" (AVR32_SR),\
600 [SR_MX_OFFSET] "i" (AVR32_SR_M0_OFFSET),\
601 [SR_MX_SIZE] "i" (AVR32_SR_M0_SIZE + AVR32_SR_M1_SIZE + AVR32_SR_M2_SIZE),\
602 [RESET_SR] "i" (AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | AVR32_SR_M0_MASK)\
607 #define Reset_CPU() \
609 extern void *volatile __program_start;\
610 __asm__ __volatile__ (\
611 "mov r7, LWRD(__program_start)\n\t"\
612 "orh r7, HWRD(__program_start)\n\t"\
613 "mov r9, LWRD("ASTRINGZ(AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | AVR32_SR_M0_MASK)")\n\t"\
614 "orh r9, HWRD("ASTRINGZ(AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | AVR32_SR_M0_MASK)")\n\t"\
615 "mfsr r8, "ASTRINGZ(AVR32_SR)"\n\t"\
616 "bfextu r8, r8, "ASTRINGZ(AVR32_SR_M0_OFFSET)", "ASTRINGZ(AVR32_SR_M0_SIZE + AVR32_SR_M1_SIZE + AVR32_SR_M2_SIZE)"\n\t"\
619 "sub r8, pc, -12\n\t"\
622 "mtsr "ASTRINGZ(AVR32_SR)", r9\n\t"\
630 "st.w r0[4], r7\n\t"\
638 "stdsp sp[0], sp\n\t"\
648 /*! \name System Register Access
652 /*! \brief Gets the value of the \a sysreg system register.
654 * \param sysreg Address of the system register of which to get the value.
656 * \return Value of the \a sysreg system register.
659 #define Get_system_register(sysreg) __builtin_mfsr(sysreg)
661 #define Get_system_register(sysreg) __get_system_register(sysreg)
664 /*! \brief Sets the value of the \a sysreg system register to \a value.
666 * \param sysreg Address of the system register of which to set the value.
667 * \param value Value to set the \a sysreg system register to.
670 #define Set_system_register(sysreg, value) __builtin_mtsr(sysreg, value)
672 #define Set_system_register(sysreg, value) __set_system_register(sysreg, value)
678 /*! \name CPU Status Register Access
682 /*! \brief Tells whether exceptions are globally enabled.
684 * \return \c 1 if exceptions are globally enabled, else \c 0.
686 #define Is_global_exception_enabled() (!Tst_bits(Get_system_register(AVR32_SR), AVR32_SR_EM_MASK))
688 /*! \brief Disables exceptions globally.
691 #define Disable_global_exception() ({__asm__ __volatile__ ("ssrf\t%0" : : "i" (AVR32_SR_EM_OFFSET));})
693 #define Disable_global_exception() (__set_status_flag(AVR32_SR_EM_OFFSET))
696 /*! \brief Enables exceptions globally.
699 #define Enable_global_exception() ({__asm__ __volatile__ ("csrf\t%0" : : "i" (AVR32_SR_EM_OFFSET));})
701 #define Enable_global_exception() (__clear_status_flag(AVR32_SR_EM_OFFSET))
704 /*! \brief Tells whether interrupts are globally enabled.
706 * \return \c 1 if interrupts are globally enabled, else \c 0.
708 #define Is_global_interrupt_enabled() (!Tst_bits(Get_system_register(AVR32_SR), AVR32_SR_GM_MASK))
710 /*! \brief Disables interrupts globally.
713 #define Disable_global_interrupt() ({__asm__ __volatile__ ("ssrf\t%0\n\tnop\n\tnop" : : "i" (AVR32_SR_GM_OFFSET));})
715 #define Disable_global_interrupt() {__asm__ __volatile__ ("ssrf\t"ASTRINGZ(AVR32_SR_GM_OFFSET)"\n\tnop\n\tnop");}
718 /*! \brief Enables interrupts globally.
721 #define Enable_global_interrupt() ({__asm__ __volatile__ ("csrf\t%0" : : "i" (AVR32_SR_GM_OFFSET));})
723 #define Enable_global_interrupt() (__enable_interrupt())
726 /*! \brief Tells whether interrupt level \a int_lev is enabled.
728 * \param int_lev Interrupt level (0 to 3).
730 * \return \c 1 if interrupt level \a int_lev is enabled, else \c 0.
732 #define Is_interrupt_level_enabled(int_lev) (!Tst_bits(Get_system_register(AVR32_SR), TPASTE3(AVR32_SR_I, int_lev, M_MASK)))
734 /*! \brief Disables interrupt level \a int_lev.
736 * \param int_lev Interrupt level to disable (0 to 3).
739 #define Disable_interrupt_level(int_lev) ({__asm__ __volatile__ ("ssrf\t%0\n\tnop\n\tnop" : : "i" (TPASTE3(AVR32_SR_I, int_lev, M_OFFSET)));})
741 #define Disable_interrupt_level(int_lev) {__asm__ __volatile__ ("ssrf\t"ASTRINGZ(TPASTE3(AVR32_SR_I, int_lev, M_OFFSET))"\n\tnop\n\tnop");}
744 /*! \brief Enables interrupt level \a int_lev.
746 * \param int_lev Interrupt level to enable (0 to 3).
749 #define Enable_interrupt_level(int_lev) ({__asm__ __volatile__ ("csrf\t%0" : : "i" (TPASTE3(AVR32_SR_I, int_lev, M_OFFSET)));})
751 #define Enable_interrupt_level(int_lev) (__clear_status_flag(TPASTE3(AVR32_SR_I, int_lev, M_OFFSET)))
757 /*! \name Debug Register Access
761 /*! \brief Gets the value of the \a dbgreg debug register.
763 * \param dbgreg Address of the debug register of which to get the value.
765 * \return Value of the \a dbgreg debug register.
768 #define Get_debug_register(dbgreg) __builtin_mfdr(dbgreg)
770 #define Get_debug_register(dbgreg) __get_debug_register(dbgreg)
773 /*! \brief Sets the value of the \a dbgreg debug register to \a value.
775 * \param dbgreg Address of the debug register of which to set the value.
776 * \param value Value to set the \a dbgreg debug register to.
779 #define Set_debug_register(dbgreg, value) __builtin_mtdr(dbgreg, value)
781 #define Set_debug_register(dbgreg, value) __set_debug_register(dbgreg, value)
786 #endif // __AVR32_ABI_COMPILER__
789 //! Boolean evaluating MCU little endianism.
790 #if (__GNUC__ && __AVR32__) || (__ICCAVR32__ || __AAVR32__)
791 #define LITTLE_ENDIAN_MCU FALSE
794 // Check that MCU endianism is correctly defined.
795 #ifndef LITTLE_ENDIAN_MCU
796 #error YOU MUST define the MCU endianism with LITTLE_ENDIAN_MCU: either FALSE or TRUE
799 //! Boolean evaluating MCU big endianism.
800 #define BIG_ENDIAN_MCU (!LITTLE_ENDIAN_MCU)
803 #ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.
805 /*! \name MCU Endianism Handling
809 #if LITTLE_ENDIAN_MCU
811 #define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16.
812 #define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16.
814 #define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32.
815 #define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32.
816 #define LSB0W(u32) (((U8 *)&(u32))[0]) //!< Least significant byte of 1st rank of \a u32.
817 #define LSB1W(u32) (((U8 *)&(u32))[1]) //!< Least significant byte of 2nd rank of \a u32.
818 #define LSB2W(u32) (((U8 *)&(u32))[2]) //!< Least significant byte of 3rd rank of \a u32.
819 #define LSB3W(u32) (((U8 *)&(u32))[3]) //!< Least significant byte of 4th rank of \a u32.
820 #define MSB3W(u32) LSB0W(u32) //!< Most significant byte of 4th rank of \a u32.
821 #define MSB2W(u32) LSB1W(u32) //!< Most significant byte of 3rd rank of \a u32.
822 #define MSB1W(u32) LSB2W(u32) //!< Most significant byte of 2nd rank of \a u32.
823 #define MSB0W(u32) LSB3W(u32) //!< Most significant byte of 1st rank of \a u32.
825 #define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64.
826 #define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64.
827 #define LSH0(u64) (((U16 *)&(u64))[0]) //!< Least significant half-word of 1st rank of \a u64.
828 #define LSH1(u64) (((U16 *)&(u64))[1]) //!< Least significant half-word of 2nd rank of \a u64.
829 #define LSH2(u64) (((U16 *)&(u64))[2]) //!< Least significant half-word of 3rd rank of \a u64.
830 #define LSH3(u64) (((U16 *)&(u64))[3]) //!< Least significant half-word of 4th rank of \a u64.
831 #define MSH3(u64) LSH0(u64) //!< Most significant half-word of 4th rank of \a u64.
832 #define MSH2(u64) LSH1(u64) //!< Most significant half-word of 3rd rank of \a u64.
833 #define MSH1(u64) LSH2(u64) //!< Most significant half-word of 2nd rank of \a u64.
834 #define MSH0(u64) LSH3(u64) //!< Most significant half-word of 1st rank of \a u64.
835 #define LSB0D(u64) (((U8 *)&(u64))[0]) //!< Least significant byte of 1st rank of \a u64.
836 #define LSB1D(u64) (((U8 *)&(u64))[1]) //!< Least significant byte of 2nd rank of \a u64.
837 #define LSB2D(u64) (((U8 *)&(u64))[2]) //!< Least significant byte of 3rd rank of \a u64.
838 #define LSB3D(u64) (((U8 *)&(u64))[3]) //!< Least significant byte of 4th rank of \a u64.
839 #define LSB4D(u64) (((U8 *)&(u64))[4]) //!< Least significant byte of 5th rank of \a u64.
840 #define LSB5D(u64) (((U8 *)&(u64))[5]) //!< Least significant byte of 6th rank of \a u64.
841 #define LSB6D(u64) (((U8 *)&(u64))[6]) //!< Least significant byte of 7th rank of \a u64.
842 #define LSB7D(u64) (((U8 *)&(u64))[7]) //!< Least significant byte of 8th rank of \a u64.
843 #define MSB7D(u64) LSB0D(u64) //!< Most significant byte of 8th rank of \a u64.
844 #define MSB6D(u64) LSB1D(u64) //!< Most significant byte of 7th rank of \a u64.
845 #define MSB5D(u64) LSB2D(u64) //!< Most significant byte of 6th rank of \a u64.
846 #define MSB4D(u64) LSB3D(u64) //!< Most significant byte of 5th rank of \a u64.
847 #define MSB3D(u64) LSB4D(u64) //!< Most significant byte of 4th rank of \a u64.
848 #define MSB2D(u64) LSB5D(u64) //!< Most significant byte of 3rd rank of \a u64.
849 #define MSB1D(u64) LSB6D(u64) //!< Most significant byte of 2nd rank of \a u64.
850 #define MSB0D(u64) LSB7D(u64) //!< Most significant byte of 1st rank of \a u64.
852 #else // BIG_ENDIAN_MCU
854 #define MSB(u16) (((U8 *)&(u16))[0]) //!< Most significant byte of \a u16.
855 #define LSB(u16) (((U8 *)&(u16))[1]) //!< Least significant byte of \a u16.
857 #define MSH(u32) (((U16 *)&(u32))[0]) //!< Most significant half-word of \a u32.
858 #define LSH(u32) (((U16 *)&(u32))[1]) //!< Least significant half-word of \a u32.
859 #define MSB0W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 1st rank of \a u32.
860 #define MSB1W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 2nd rank of \a u32.
861 #define MSB2W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 3rd rank of \a u32.
862 #define MSB3W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 4th rank of \a u32.
863 #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32.
864 #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32.
865 #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32.
866 #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32.
868 #define MSW(u64) (((U32 *)&(u64))[0]) //!< Most significant word of \a u64.
869 #define LSW(u64) (((U32 *)&(u64))[1]) //!< Least significant word of \a u64.
870 #define MSH0(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 1st rank of \a u64.
871 #define MSH1(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 2nd rank of \a u64.
872 #define MSH2(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 3rd rank of \a u64.
873 #define MSH3(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 4th rank of \a u64.
874 #define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64.
875 #define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64.
876 #define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64.
877 #define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64.
878 #define MSB0D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 1st rank of \a u64.
879 #define MSB1D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 2nd rank of \a u64.
880 #define MSB2D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 3rd rank of \a u64.
881 #define MSB3D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 4th rank of \a u64.
882 #define MSB4D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 5th rank of \a u64.
883 #define MSB5D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 6th rank of \a u64.
884 #define MSB6D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 7th rank of \a u64.
885 #define MSB7D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 8th rank of \a u64.
886 #define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64.
887 #define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64.
888 #define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64.
889 #define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64.
890 #define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64.
891 #define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64.
892 #define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64.
893 #define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64.
900 /*! \name Endianism Conversion
902 * The same considerations as for clz and ctz apply here but AVR32-GCC's
903 * __builtin_bswap_16 and __builtin_bswap_32 do not behave like macros when
904 * applied to constant expressions, so two sets of macros are defined here:
905 * - Swap16, Swap32 and Swap64 to apply to constant expressions (values known
907 * - swap16, swap32 and swap64 to apply to non-constant expressions (values
908 * unknown at compile time).
912 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
914 * \param u16 U16 of which to toggle the endianism.
916 * \return Value resulting from \a u16 with toggled endianism.
918 * \note More optimized if only used with values known at compile time.
920 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\
923 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
925 * \param u32 U32 of which to toggle the endianism.
927 * \return Value resulting from \a u32 with toggled endianism.
929 * \note More optimized if only used with values known at compile time.
931 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\
932 ((U32)Swap16((U32)(u32)) << 16)))
934 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
936 * \param u64 U64 of which to toggle the endianism.
938 * \return Value resulting from \a u64 with toggled endianism.
940 * \note More optimized if only used with values known at compile time.
942 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\
943 ((U64)Swap32((U64)(u64)) << 32)))
945 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
947 * \param u16 U16 of which to toggle the endianism.
949 * \return Value resulting from \a u16 with toggled endianism.
951 * \note More optimized if only used with values unknown at compile time.
954 #define swap16(u16) ((U16)__builtin_bswap_16((U16)(u16)))
956 #define swap16(u16) ((U16)__swap_bytes_in_halfwords((U16)(u16)))
959 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
961 * \param u32 U32 of which to toggle the endianism.
963 * \return Value resulting from \a u32 with toggled endianism.
965 * \note More optimized if only used with values unknown at compile time.
968 #define swap32(u32) ((U32)__builtin_bswap_32((U32)(u32)))
970 #define swap32(u32) ((U32)__swap_bytes((U32)(u32)))
973 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
975 * \param u64 U64 of which to toggle the endianism.
977 * \return Value resulting from \a u64 with toggled endianism.
979 * \note More optimized if only used with values unknown at compile time.
981 #define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\
982 ((U64)swap32((U64)(u64)) << 32)))
987 /*! \name Target Abstraction
991 #define _GLOBEXT_ extern //!< extern storage-class specifier.
992 #define _CONST_TYPE_ const //!< const type qualifier.
993 #define _MEM_TYPE_SLOW_ //!< Slow memory type.
994 #define _MEM_TYPE_MEDFAST_ //!< Fairly fast memory type.
995 #define _MEM_TYPE_FAST_ //!< Fast memory type.
997 typedef U8 Byte; //!< 8-bit unsigned integer.
999 #define memcmp_ram2ram memcmp //!< Target-specific memcmp of RAM to RAM.
1000 #define memcmp_code2ram memcmp //!< Target-specific memcmp of RAM to NVRAM.
1001 #define memcpy_ram2ram memcpy //!< Target-specific memcpy from RAM to RAM.
1002 #define memcpy_code2ram memcpy //!< Target-specific memcpy from NVRAM to RAM.
1004 #define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32.
1005 #define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32.
1006 #define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32.
1007 #define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32.
1008 #define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32.
1009 #define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32.
1010 #define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32.
1011 #define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32.
1015 #endif // __AVR32_ABI_COMPILER__
1018 #endif // _COMPILER_H_