4 * \brief Commonly used includes, types and macros.
\r
6 * Copyright (c) 2010-2013 Atmel Corporation. All rights reserved.
\r
12 * Redistribution and use in source and binary forms, with or without
\r
13 * modification, are permitted provided that the following conditions are met:
\r
15 * 1. Redistributions of source code must retain the above copyright notice,
\r
16 * this list of conditions and the following disclaimer.
\r
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
19 * this list of conditions and the following disclaimer in the documentation
\r
20 * and/or other materials provided with the distribution.
\r
22 * 3. The name of Atmel may not be used to endorse or promote products derived
\r
23 * from this software without specific prior written permission.
\r
25 * 4. This software may only be redistributed and used in connection with an
\r
26 * Atmel microcontroller product.
\r
28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
\r
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
\r
32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
38 * POSSIBILITY OF SUCH DAMAGE.
\r
44 #ifndef UTILS_COMPILER_H
\r
45 #define UTILS_COMPILER_H
\r
48 * \defgroup group_sam_utils Compiler abstraction layer and code utilities
\r
50 * Compiler abstraction layer and code utilities for AT91SAM.
\r
51 * This module provides various abstraction layers and utilities to make code compatible between different compilers.
\r
57 #if (defined __ICCARM__)
\r
58 # include <intrinsics.h>
\r
62 #include "preprocessor.h"
\r
66 //_____ D E C L A R A T I O N S ____________________________________________
\r
68 #ifndef __ASSEMBLY__ // Not defined for assembling.
\r
71 #include <stdbool.h>
\r
76 /*! \name Compiler Keywords
\r
78 * Port of some keywords from GCC to IAR Embedded Workbench.
\r
82 #define __inline__ inline
\r
83 #define __volatile__
\r
88 #define FUNC_PTR void *
\r
91 * \brief Marking \a v as a unused parameter or value.
\r
93 #define UNUSED(v) (void)(v)
\r
97 * \brief Marking \a v as a unused parameter or value.
\r
99 #define unused(v) do { (void)(v); } while(0)
\r
103 * \brief Memory barrier
\r
105 #define barrier() __DMB()
\r
108 * \brief Emit the compiler pragma \a arg.
\r
110 * \param arg The pragma directive as it would appear after \e \#pragma
\r
111 * (i.e. not stringified).
\r
113 #define COMPILER_PRAGMA(arg) _Pragma(#arg)
\r
116 * \def COMPILER_PACK_SET(alignment)
\r
117 * \brief Set maximum alignment for subsequent struct and union
\r
118 * definitions to \a alignment.
\r
120 #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
\r
123 * \def COMPILER_PACK_RESET()
\r
124 * \brief Set default alignment for subsequent struct and union
\r
127 #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
\r
131 * \brief Set aligned boundary.
\r
133 #if (defined __GNUC__) || (defined __CC_ARM)
\r
134 # define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
\r
135 #elif (defined __ICCARM__)
\r
136 # define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
\r
140 * \brief Set word-aligned boundary.
\r
142 #if (defined __GNUC__) || defined(__CC_ARM)
\r
143 #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4)))
\r
144 #elif (defined __ICCARM__)
\r
145 #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4)
\r
149 * \def __always_inline
\r
150 * \brief The function should always be inlined.
\r
152 * This annotation instructs the compiler to ignore its inlining
\r
153 * heuristics and inline the function no matter how big it thinks it
\r
156 #if defined(__CC_ARM)
\r
157 # define __always_inline __forceinline
\r
158 #elif (defined __GNUC__)
\r
159 # define __always_inline inline __attribute__((__always_inline__))
\r
160 #elif (defined __ICCARM__)
\r
161 # define __always_inline _Pragma("inline=forced")
\r
164 /*! \brief This macro is used to test fatal errors.
\r
166 * The macro tests if the expression is false. If it is, a fatal error is
\r
167 * detected and the application hangs up. If TEST_SUITE_DEFINE_ASSERT_MACRO
\r
168 * is defined, a unit test version of the macro is used, to allow execution
\r
169 * of further tests after a false expression.
\r
171 * \param expr Expression to evaluate and supposed to be nonzero.
\r
173 #if defined(_ASSERT_ENABLE_)
\r
174 # if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
\r
175 // Assert() is defined in unit_test/suite.h
\r
176 # include "unit_test/suite.h"
\r
178 #undef TEST_SUITE_DEFINE_ASSERT_MACRO
\r
179 # define Assert(expr) \
\r
181 if (!(expr)) while (true);\
\r
185 # define Assert(expr) ((void) 0)
\r
188 /* Define WEAK attribute */
\r
189 #if defined ( __CC_ARM ) /* Keil µVision 4 */
\r
190 # define WEAK __attribute__ ((weak))
\r
191 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
\r
192 # define WEAK __weak
\r
193 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
\r
194 # define WEAK __attribute__ ((weak))
\r
197 /* Define NO_INIT attribute */
\r
198 #if defined ( __CC_ARM )
\r
199 # define NO_INIT __attribute__((zero_init))
\r
200 #elif defined ( __ICCARM__ )
\r
201 # define NO_INIT __no_init
\r
202 #elif defined ( __GNUC__ )
\r
203 # define NO_INIT __attribute__((section(".no_init")))
\r
206 /* Define RAMFUNC attribute */
\r
207 #if defined ( __CC_ARM ) /* Keil µVision 4 */
\r
208 # define RAMFUNC __attribute__ ((section(".ramfunc")))
\r
209 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
\r
210 # define RAMFUNC __ramfunc
\r
211 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
\r
212 # define RAMFUNC __attribute__ ((section(".ramfunc")))
\r
215 /* Define OPTIMIZE_HIGH attribute */
\r
216 #if defined ( __CC_ARM ) /* Keil µVision 4 */
\r
217 # define OPTIMIZE_HIGH _Pragma("O3")
\r
218 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
\r
219 # define OPTIMIZE_HIGH _Pragma("optimize=high")
\r
220 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
\r
221 # define OPTIMIZE_HIGH __attribute__((optimize(s)))
\r
224 #include "interrupt.h"
\r
226 /*! \name Usual Types
\r
229 typedef unsigned char Bool; //!< Boolean.
\r
230 #ifndef __cplusplus
\r
231 #if !defined(__bool_true_false_are_defined)
\r
232 typedef unsigned char bool; //!< Boolean.
\r
235 typedef int8_t S8 ; //!< 8-bit signed integer.
\r
236 typedef uint8_t U8 ; //!< 8-bit unsigned integer.
\r
237 typedef int16_t S16; //!< 16-bit signed integer.
\r
238 typedef uint16_t U16; //!< 16-bit unsigned integer.
\r
239 typedef uint16_t le16_t;
\r
240 typedef uint16_t be16_t;
\r
241 typedef int32_t S32; //!< 32-bit signed integer.
\r
242 typedef uint32_t U32; //!< 32-bit unsigned integer.
\r
243 typedef uint32_t le32_t;
\r
244 typedef uint32_t be32_t;
\r
245 typedef int64_t S64; //!< 64-bit signed integer.
\r
246 typedef uint64_t U64; //!< 64-bit unsigned integer.
\r
247 typedef float F32; //!< 32-bit floating-point number.
\r
248 typedef double F64; //!< 64-bit floating-point number.
\r
249 typedef uint32_t iram_size_t;
\r
253 /*! \name Status Types
\r
256 typedef bool Status_bool_t; //!< Boolean status.
\r
257 typedef U8 Status_t; //!< 8-bit-coded status.
\r
261 /*! \name Aliasing Aggregate Types
\r
298 //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
\r
311 //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
\r
314 volatile S64 *s64ptr;
\r
315 volatile U64 *u64ptr;
\r
316 volatile S32 *s32ptr;
\r
317 volatile U32 *u32ptr;
\r
318 volatile S16 *s16ptr;
\r
319 volatile U16 *u16ptr;
\r
320 volatile S8 *s8ptr ;
\r
321 volatile U8 *u8ptr ;
\r
324 //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
\r
337 //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
\r
340 const volatile S64 *s64ptr;
\r
341 const volatile U64 *u64ptr;
\r
342 const volatile S32 *s32ptr;
\r
343 const volatile U32 *u32ptr;
\r
344 const volatile S16 *s16ptr;
\r
345 const volatile U16 *u16ptr;
\r
346 const volatile S8 *s8ptr ;
\r
347 const volatile U8 *u8ptr ;
\r
350 //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
\r
363 //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
\r
366 volatile S64 *s64ptr;
\r
367 volatile U64 *u64ptr;
\r
368 volatile S32 *s32ptr;
\r
369 volatile U32 *u32ptr;
\r
370 volatile S16 *s16ptr;
\r
371 volatile U16 *u16ptr;
\r
372 volatile S8 *s8ptr ;
\r
373 volatile U8 *u8ptr ;
\r
376 //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
\r
389 //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
\r
392 const volatile S64 *s64ptr;
\r
393 const volatile U64 *u64ptr;
\r
394 const volatile S32 *s32ptr;
\r
395 const volatile U32 *u32ptr;
\r
396 const volatile S16 *s16ptr;
\r
397 const volatile U16 *u16ptr;
\r
398 const volatile S8 *s8ptr ;
\r
399 const volatile U8 *u8ptr ;
\r
404 #endif // #ifndef __ASSEMBLY__
\r
406 /*! \name Usual Constants
\r
411 #ifndef __cplusplus
\r
412 #if !defined(__bool_true_false_are_defined)
\r
424 #ifndef __ASSEMBLY__ // not for assembling.
\r
426 //! \name Optimization Control
\r
431 * \brief The expression \a exp is likely to be true
\r
434 # define likely(exp) (exp)
\r
438 * \def unlikely(exp)
\r
439 * \brief The expression \a exp is unlikely to be true
\r
442 # define unlikely(exp) (exp)
\r
446 * \def is_constant(exp)
\r
447 * \brief Determine if an expression evaluates to a constant value.
\r
449 * \param exp Any expression
\r
451 * \return true if \a exp is constant, false otherwise.
\r
453 #if (defined __GNUC__) || (defined __CC_ARM)
\r
454 # define is_constant(exp) __builtin_constant_p(exp)
\r
456 # define is_constant(exp) (0)
\r
461 /*! \name Bit-Field Handling
\r
465 /*! \brief Reads the bits of a value specified by a given bit-mask.
\r
467 * \param value Value to read bits from.
\r
468 * \param mask Bit-mask indicating bits to read.
\r
470 * \return Read bits.
\r
472 #define Rd_bits( value, mask) ((value) & (mask))
\r
474 /*! \brief Writes the bits of a C lvalue specified by a given bit-mask.
\r
476 * \param lvalue C lvalue to write bits to.
\r
477 * \param mask Bit-mask indicating bits to write.
\r
478 * \param bits Bits to write.
\r
480 * \return Resulting value with written bits.
\r
482 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
\r
483 ((bits ) & (mask)))
\r
485 /*! \brief Tests the bits of a value specified by a given bit-mask.
\r
487 * \param value Value of which to test bits.
\r
488 * \param mask Bit-mask indicating bits to test.
\r
490 * \return \c 1 if at least one of the tested bits is set, else \c 0.
\r
492 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
\r
494 /*! \brief Clears the bits of a C lvalue specified by a given bit-mask.
\r
496 * \param lvalue C lvalue of which to clear bits.
\r
497 * \param mask Bit-mask indicating bits to clear.
\r
499 * \return Resulting value with cleared bits.
\r
501 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
\r
503 /*! \brief Sets the bits of a C lvalue specified by a given bit-mask.
\r
505 * \param lvalue C lvalue of which to set bits.
\r
506 * \param mask Bit-mask indicating bits to set.
\r
508 * \return Resulting value with set bits.
\r
510 #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
\r
512 /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.
\r
514 * \param lvalue C lvalue of which to toggle bits.
\r
515 * \param mask Bit-mask indicating bits to toggle.
\r
517 * \return Resulting value with toggled bits.
\r
519 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
\r
521 /*! \brief Reads the bit-field of a value specified by a given bit-mask.
\r
523 * \param value Value to read a bit-field from.
\r
524 * \param mask Bit-mask indicating the bit-field to read.
\r
526 * \return Read bit-field.
\r
528 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
\r
530 /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
\r
532 * \param lvalue C lvalue to write a bit-field to.
\r
533 * \param mask Bit-mask indicating the bit-field to write.
\r
534 * \param bitfield Bit-field to write.
\r
536 * \return Resulting value with written bit-field.
\r
538 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))
\r
543 /*! \name Zero-Bit Counting
\r
545 * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
\r
546 * applied to constant expressions (values known at compile time), so they are
\r
547 * more optimized than the use of the corresponding assembly instructions and
\r
548 * they can be used as constant expressions e.g. to initialize objects having
\r
549 * static storage duration, and like the corresponding assembly instructions
\r
550 * when applied to non-constant expressions (values unknown at compile time), so
\r
551 * they are more optimized than an assembly periphrasis. Hence, clz and ctz
\r
552 * ensure a possible and optimized behavior for both constant and non-constant
\r
557 /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
\r
559 * \param u Value of which to count the leading zero bits.
\r
561 * \return The count of leading zero bits in \a u.
\r
563 #if (defined __GNUC__) || (defined __CC_ARM)
\r
564 # define clz(u) __builtin_clz(u)
\r
565 #elif (defined __ICCARM__)
\r
566 # define clz(u) __CLZ(u)
\r
568 # define clz(u) (((u) == 0) ? 32 : \
\r
569 ((u) & (1ul << 31)) ? 0 : \
\r
570 ((u) & (1ul << 30)) ? 1 : \
\r
571 ((u) & (1ul << 29)) ? 2 : \
\r
572 ((u) & (1ul << 28)) ? 3 : \
\r
573 ((u) & (1ul << 27)) ? 4 : \
\r
574 ((u) & (1ul << 26)) ? 5 : \
\r
575 ((u) & (1ul << 25)) ? 6 : \
\r
576 ((u) & (1ul << 24)) ? 7 : \
\r
577 ((u) & (1ul << 23)) ? 8 : \
\r
578 ((u) & (1ul << 22)) ? 9 : \
\r
579 ((u) & (1ul << 21)) ? 10 : \
\r
580 ((u) & (1ul << 20)) ? 11 : \
\r
581 ((u) & (1ul << 19)) ? 12 : \
\r
582 ((u) & (1ul << 18)) ? 13 : \
\r
583 ((u) & (1ul << 17)) ? 14 : \
\r
584 ((u) & (1ul << 16)) ? 15 : \
\r
585 ((u) & (1ul << 15)) ? 16 : \
\r
586 ((u) & (1ul << 14)) ? 17 : \
\r
587 ((u) & (1ul << 13)) ? 18 : \
\r
588 ((u) & (1ul << 12)) ? 19 : \
\r
589 ((u) & (1ul << 11)) ? 20 : \
\r
590 ((u) & (1ul << 10)) ? 21 : \
\r
591 ((u) & (1ul << 9)) ? 22 : \
\r
592 ((u) & (1ul << 8)) ? 23 : \
\r
593 ((u) & (1ul << 7)) ? 24 : \
\r
594 ((u) & (1ul << 6)) ? 25 : \
\r
595 ((u) & (1ul << 5)) ? 26 : \
\r
596 ((u) & (1ul << 4)) ? 27 : \
\r
597 ((u) & (1ul << 3)) ? 28 : \
\r
598 ((u) & (1ul << 2)) ? 29 : \
\r
599 ((u) & (1ul << 1)) ? 30 : \
\r
603 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
\r
605 * \param u Value of which to count the trailing zero bits.
\r
607 * \return The count of trailing zero bits in \a u.
\r
609 #if (defined __GNUC__) || (defined __CC_ARM)
\r
610 # define ctz(u) __builtin_ctz(u)
\r
612 # define ctz(u) ((u) & (1ul << 0) ? 0 : \
\r
613 (u) & (1ul << 1) ? 1 : \
\r
614 (u) & (1ul << 2) ? 2 : \
\r
615 (u) & (1ul << 3) ? 3 : \
\r
616 (u) & (1ul << 4) ? 4 : \
\r
617 (u) & (1ul << 5) ? 5 : \
\r
618 (u) & (1ul << 6) ? 6 : \
\r
619 (u) & (1ul << 7) ? 7 : \
\r
620 (u) & (1ul << 8) ? 8 : \
\r
621 (u) & (1ul << 9) ? 9 : \
\r
622 (u) & (1ul << 10) ? 10 : \
\r
623 (u) & (1ul << 11) ? 11 : \
\r
624 (u) & (1ul << 12) ? 12 : \
\r
625 (u) & (1ul << 13) ? 13 : \
\r
626 (u) & (1ul << 14) ? 14 : \
\r
627 (u) & (1ul << 15) ? 15 : \
\r
628 (u) & (1ul << 16) ? 16 : \
\r
629 (u) & (1ul << 17) ? 17 : \
\r
630 (u) & (1ul << 18) ? 18 : \
\r
631 (u) & (1ul << 19) ? 19 : \
\r
632 (u) & (1ul << 20) ? 20 : \
\r
633 (u) & (1ul << 21) ? 21 : \
\r
634 (u) & (1ul << 22) ? 22 : \
\r
635 (u) & (1ul << 23) ? 23 : \
\r
636 (u) & (1ul << 24) ? 24 : \
\r
637 (u) & (1ul << 25) ? 25 : \
\r
638 (u) & (1ul << 26) ? 26 : \
\r
639 (u) & (1ul << 27) ? 27 : \
\r
640 (u) & (1ul << 28) ? 28 : \
\r
641 (u) & (1ul << 29) ? 29 : \
\r
642 (u) & (1ul << 30) ? 30 : \
\r
643 (u) & (1ul << 31) ? 31 : \
\r
650 /*! \name Bit Reversing
\r
654 /*! \brief Reverses the bits of \a u8.
\r
656 * \param u8 U8 of which to reverse the bits.
\r
658 * \return Value resulting from \a u8 with reversed bits.
\r
660 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
\r
662 /*! \brief Reverses the bits of \a u16.
\r
664 * \param u16 U16 of which to reverse the bits.
\r
666 * \return Value resulting from \a u16 with reversed bits.
\r
668 #define bit_reverse16(u16) ((U16)(bit_reverse32((U16)(u16)) >> 16))
\r
670 /*! \brief Reverses the bits of \a u32.
\r
672 * \param u32 U32 of which to reverse the bits.
\r
674 * \return Value resulting from \a u32 with reversed bits.
\r
676 #define bit_reverse32(u32) __RBIT(u32)
\r
678 /*! \brief Reverses the bits of \a u64.
\r
680 * \param u64 U64 of which to reverse the bits.
\r
682 * \return Value resulting from \a u64 with reversed bits.
\r
684 #define bit_reverse64(u64) ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\
\r
685 ((U64)bit_reverse32((U64)(u64)) << 32)))
\r
690 /*! \name Alignment
\r
694 /*! \brief Tests alignment of the number \a val with the \a n boundary.
\r
696 * \param val Input value.
\r
697 * \param n Boundary.
\r
699 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
\r
701 #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) )
\r
703 /*! \brief Gets alignment of the number \a val with respect to the \a n boundary.
\r
705 * \param val Input value.
\r
706 * \param n Boundary.
\r
708 * \return Alignment of the number \a val with respect to the \a n boundary.
\r
710 #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) )
\r
712 /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
\r
714 * \param lval Input/output lvalue.
\r
715 * \param n Boundary.
\r
716 * \param alg Alignment.
\r
718 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
\r
720 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
\r
722 /*! \brief Aligns the number \a val with the upper \a n boundary.
\r
724 * \param val Input value.
\r
725 * \param n Boundary.
\r
727 * \return Value resulting from the number \a val aligned with the upper \a n boundary.
\r
729 #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1))
\r
731 /*! \brief Aligns the number \a val with the lower \a n boundary.
\r
733 * \param val Input value.
\r
734 * \param n Boundary.
\r
736 * \return Value resulting from the number \a val aligned with the lower \a n boundary.
\r
738 #define Align_down(val, n ) ( (val) & ~((n) - 1))
\r
743 /*! \name Mathematics
\r
745 * The same considerations as for clz and ctz apply here but GCC does not
\r
746 * provide built-in functions to access the assembly instructions abs, min and
\r
747 * max and it does not produce them by itself in most cases, so two sets of
\r
748 * macros are defined here:
\r
749 * - Abs, Min and Max to apply to constant expressions (values known at
\r
751 * - abs, min and max to apply to non-constant expressions (values unknown at
\r
752 * compile time), abs is found in stdlib.h.
\r
756 /*! \brief Takes the absolute value of \a a.
\r
758 * \param a Input value.
\r
760 * \return Absolute value of \a a.
\r
762 * \note More optimized if only used with values known at compile time.
\r
764 #define Abs(a) (((a) < 0 ) ? -(a) : (a))
\r
766 /*! \brief Takes the minimal value of \a a and \a b.
\r
768 * \param a Input value.
\r
769 * \param b Input value.
\r
771 * \return Minimal value of \a a and \a b.
\r
773 * \note More optimized if only used with values known at compile time.
\r
775 #define Min(a, b) (((a) < (b)) ? (a) : (b))
\r
777 /*! \brief Takes the maximal value of \a a and \a b.
\r
779 * \param a Input value.
\r
780 * \param b Input value.
\r
782 * \return Maximal value of \a a and \a b.
\r
784 * \note More optimized if only used with values known at compile time.
\r
786 #define Max(a, b) (((a) > (b)) ? (a) : (b))
\r
788 // abs() is already defined by stdlib.h
\r
790 /*! \brief Takes the minimal value of \a a and \a b.
\r
792 * \param a Input value.
\r
793 * \param b Input value.
\r
795 * \return Minimal value of \a a and \a b.
\r
797 * \note More optimized if only used with values unknown at compile time.
\r
799 #define min(a, b) Min(a, b)
\r
801 /*! \brief Takes the maximal value of \a a and \a b.
\r
803 * \param a Input value.
\r
804 * \param b Input value.
\r
806 * \return Maximal value of \a a and \a b.
\r
808 * \note More optimized if only used with values unknown at compile time.
\r
810 #define max(a, b) Max(a, b)
\r
815 /*! \brief Calls the routine at address \a addr.
\r
817 * It generates a long call opcode.
\r
819 * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
\r
820 * it is invoked from the CPU supervisor mode.
\r
822 * \param addr Address of the routine to call.
\r
824 * \note It may be used as a long jump opcode in some special cases.
\r
826 #define Long_call(addr) ((*(void (*)(void))(addr))())
\r
829 /*! \name MCU Endianism Handling
\r
830 * ARM is MCU little endianism.
\r
833 #define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16.
\r
834 #define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16.
\r
836 #define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32.
\r
837 #define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32.
\r
838 #define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32.
\r
839 #define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32.
\r
840 #define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32.
\r
841 #define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32.
\r
842 #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32.
\r
843 #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32.
\r
844 #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32.
\r
845 #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32.
\r
847 #define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64.
\r
848 #define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64.
\r
849 #define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64.
\r
850 #define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64.
\r
851 #define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64.
\r
852 #define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64.
\r
853 #define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64.
\r
854 #define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64.
\r
855 #define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64.
\r
856 #define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64.
\r
857 #define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64.
\r
858 #define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64.
\r
859 #define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64.
\r
860 #define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64.
\r
861 #define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64.
\r
862 #define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64.
\r
863 #define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64.
\r
864 #define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64.
\r
865 #define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64.
\r
866 #define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64.
\r
867 #define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64.
\r
868 #define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64.
\r
869 #define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64.
\r
870 #define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64.
\r
871 #define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64.
\r
872 #define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64.
\r
874 #define BE16(x) Swap16(x)
\r
875 #define LE16(x) (x)
\r
877 #define le16_to_cpu(x) (x)
\r
878 #define cpu_to_le16(x) (x)
\r
879 #define LE16_TO_CPU(x) (x)
\r
880 #define CPU_TO_LE16(x) (x)
\r
882 #define be16_to_cpu(x) Swap16(x)
\r
883 #define cpu_to_be16(x) Swap16(x)
\r
884 #define BE16_TO_CPU(x) Swap16(x)
\r
885 #define CPU_TO_BE16(x) Swap16(x)
\r
887 #define le32_to_cpu(x) (x)
\r
888 #define cpu_to_le32(x) (x)
\r
889 #define LE32_TO_CPU(x) (x)
\r
890 #define CPU_TO_LE32(x) (x)
\r
892 #define be32_to_cpu(x) swap32(x)
\r
893 #define cpu_to_be32(x) swap32(x)
\r
894 #define BE32_TO_CPU(x) swap32(x)
\r
895 #define CPU_TO_BE32(x) swap32(x)
\r
899 /*! \name Endianism Conversion
\r
901 * The same considerations as for clz and ctz apply here but GCC's
\r
902 * __builtin_bswap_32 and __builtin_bswap_64 do not behave like macros when
\r
903 * applied to constant expressions, so two sets of macros are defined here:
\r
904 * - Swap16, Swap32 and Swap64 to apply to constant expressions (values known
\r
905 * at compile time);
\r
906 * - swap16, swap32 and swap64 to apply to non-constant expressions (values
\r
907 * unknown at compile time).
\r
911 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
\r
913 * \param u16 U16 of which to toggle the endianism.
\r
915 * \return Value resulting from \a u16 with toggled endianism.
\r
917 * \note More optimized if only used with values known at compile time.
\r
919 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\
\r
920 ((U16)(u16) << 8)))
\r
922 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
\r
924 * \param u32 U32 of which to toggle the endianism.
\r
926 * \return Value resulting from \a u32 with toggled endianism.
\r
928 * \note More optimized if only used with values known at compile time.
\r
930 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\
\r
931 ((U32)Swap16((U32)(u32)) << 16)))
\r
933 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
\r
935 * \param u64 U64 of which to toggle the endianism.
\r
937 * \return Value resulting from \a u64 with toggled endianism.
\r
939 * \note More optimized if only used with values known at compile time.
\r
941 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\
\r
942 ((U64)Swap32((U64)(u64)) << 32)))
\r
944 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
\r
946 * \param u16 U16 of which to toggle the endianism.
\r
948 * \return Value resulting from \a u16 with toggled endianism.
\r
950 * \note More optimized if only used with values unknown at compile time.
\r
952 #define swap16(u16) Swap16(u16)
\r
954 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
\r
956 * \param u32 U32 of which to toggle the endianism.
\r
958 * \return Value resulting from \a u32 with toggled endianism.
\r
960 * \note More optimized if only used with values unknown at compile time.
\r
962 #if (defined __GNUC__)
\r
963 # define swap32(u32) ((U32)__builtin_bswap32((U32)(u32)))
\r
965 # define swap32(u32) Swap32(u32)
\r
968 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
\r
970 * \param u64 U64 of which to toggle the endianism.
\r
972 * \return Value resulting from \a u64 with toggled endianism.
\r
974 * \note More optimized if only used with values unknown at compile time.
\r
976 #if (defined __GNUC__)
\r
977 # define swap64(u64) ((U64)__builtin_bswap64((U64)(u64)))
\r
979 # define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\
\r
980 ((U64)swap32((U64)(u64)) << 32)))
\r
986 /*! \name Target Abstraction
\r
990 #define _GLOBEXT_ extern //!< extern storage-class specifier.
\r
991 #define _CONST_TYPE_ const //!< const type qualifier.
\r
992 #define _MEM_TYPE_SLOW_ //!< Slow memory type.
\r
993 #define _MEM_TYPE_MEDFAST_ //!< Fairly fast memory type.
\r
994 #define _MEM_TYPE_FAST_ //!< Fast memory type.
\r
996 typedef U8 Byte; //!< 8-bit unsigned integer.
\r
998 #define memcmp_ram2ram memcmp //!< Target-specific memcmp of RAM to RAM.
\r
999 #define memcmp_code2ram memcmp //!< Target-specific memcmp of RAM to NVRAM.
\r
1000 #define memcpy_ram2ram memcpy //!< Target-specific memcpy from RAM to RAM.
\r
1001 #define memcpy_code2ram memcpy //!< Target-specific memcpy from NVRAM to RAM.
\r
1003 #define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32.
\r
1004 #define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32.
\r
1005 #define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32.
\r
1006 #define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32.
\r
1007 #define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32.
\r
1008 #define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32.
\r
1009 #define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32.
\r
1010 #define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32.
\r
1015 * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
\r
1016 * integer arithmetic.
\r
1018 * \param a An integer
\r
1019 * \param b Another integer
\r
1021 * \return (\a a / \a b) rounded up to the nearest integer.
\r
1023 #define div_ceil(a, b) (((a) + (b) - 1) / (b))
\r
1025 #endif // #ifndef __ASSEMBLY__
\r
1028 #if defined(__ICCARM__)
\r
1029 #define SHORTENUM __packed
\r
1030 #elif defined(__GNUC__)
\r
1031 #define SHORTENUM __attribute__((packed))
\r
1034 #define FLASH_DECLARE(x) const x
\r
1035 #define FLASH_EXTERN(x) extern const x
\r
1036 #define PGM_READ_BYTE(x) *(x)
\r
1037 #define PGM_READ_WORD(x) *(x)
\r
1038 #define MEMCPY_ENDIAN memcpy
\r
1039 #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len))
\r
1041 /* Converting of values from CPU endian to little endian. */
\r
1042 #define CPU_ENDIAN_TO_LE16(x) (x)
\r
1043 #define CPU_ENDIAN_TO_LE32(x) (x)
\r
1044 #define CPU_ENDIAN_TO_LE64(x) (x)
\r
1046 /* Converting of values from little endian to CPU endian. */
\r
1047 #define LE16_TO_CPU_ENDIAN(x) (x)
\r
1048 #define LE32_TO_CPU_ENDIAN(x) (x)
\r
1049 #define LE64_TO_CPU_ENDIAN(x) (x)
\r
1051 /* Converting of constants from CPU endian to little endian. */
\r
1052 #define CCPU_ENDIAN_TO_LE16(x) (x)
\r
1053 #define CCPU_ENDIAN_TO_LE32(x) (x)
\r
1054 #define CCPU_ENDIAN_TO_LE64(x) (x)
\r
1056 #define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src))
\r
1057 #define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src))
\r
1063 #endif /* UTILS_COMPILER_H */
\r