4 * \brief Commonly used includes, types and macros.
6 * Copyright (C) 2012-2016 Atmel Corporation. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
20 * 3. The name of Atmel may not be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * 4. This software may only be redistributed and used in connection with an
24 * Atmel microcontroller product.
26 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
29 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
42 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45 #ifndef UTILS_COMPILER_H_INCLUDED
46 #define UTILS_COMPILER_H_INCLUDED
49 * \defgroup group_sam0_utils Compiler abstraction layer and code utilities
51 * Compiler abstraction layer and code utilities for Cortex-M0+ based Atmel SAM devices.
52 * This module provides various abstraction layers and utilities to make code compatible between different compilers.
57 #if (defined __ICCARM__)
58 # include <intrinsics.h>
63 //#include <status_codes.h>
64 //#include <preprocessor.h>
76 * \brief Marking \a v as a unused parameter or value.
78 #define UNUSED(v) (void)(v)
82 * \brief Memory barrier
85 # define barrier() asm volatile("" ::: "memory")
87 # define barrier() asm ("")
91 * \brief Emit the compiler pragma \a arg.
93 * \param[in] arg The pragma directive as it would appear after \e \#pragma
94 * (i.e. not stringified).
96 #define COMPILER_PRAGMA(arg) _Pragma(#arg)
99 * \def COMPILER_PACK_SET(alignment)
100 * \brief Set maximum alignment for subsequent struct and union definitions to \a alignment.
102 #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
105 * \def COMPILER_PACK_RESET()
106 * \brief Set default alignment for subsequent struct and union definitions.
108 #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
112 * \brief Set aligned boundary.
114 #if (defined __GNUC__) || (defined __CC_ARM)
115 # define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
116 #elif (defined __ICCARM__)
117 # define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
121 * \brief Set word-aligned boundary.
123 #if (defined __GNUC__) || defined(__CC_ARM)
124 #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4)))
125 #elif (defined __ICCARM__)
126 #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4)
130 * \def __always_inline
131 * \brief The function should always be inlined.
133 * This annotation instructs the compiler to ignore its inlining
134 * heuristics and inline the function no matter how big it thinks it
137 #if !defined(__always_inline)
138 #if defined(__CC_ARM)
139 # define __always_inline __forceinline
140 #elif (defined __GNUC__)
141 # define __always_inline __attribute__((__always_inline__))
142 #elif (defined __ICCARM__)
143 # define __always_inline _Pragma("inline=forced")
149 * \brief The function should never be inlined
151 * This annotation instructs the compiler to ignore its inlining
152 * heuristics and not inline the function no matter how small it thinks it
155 #if defined(__CC_ARM)
156 # define __no_inline __attribute__((noinline))
157 #elif (defined __GNUC__)
158 # define __no_inline __attribute__((noinline))
159 #elif (defined __ICCARM__)
160 # define __no_inline _Pragma("inline=never")
164 /** \brief This macro is used to test fatal errors.
166 * The macro tests if the expression is false. If it is, a fatal error is
167 * detected and the application hangs up. If \c TEST_SUITE_DEFINE_ASSERT_MACRO
168 * is defined, a unit test version of the macro is used, to allow execution
169 * of further tests after a false expression.
171 * \param[in] expr Expression to evaluate and supposed to be nonzero.
173 #if defined(_ASSERT_ENABLE_)
174 # if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
175 # include "unit_test/suite.h"
177 # undef TEST_SUITE_DEFINE_ASSERT_MACRO
178 # define Assert(expr) \
180 if (!(expr)) asm("BKPT #0");\
184 # define Assert(expr) ((void) 0)
187 /* Define WEAK attribute */
188 #if defined ( __CC_ARM )
189 # define WEAK __attribute__ ((weak))
190 #elif defined ( __ICCARM__ )
192 #elif defined ( __GNUC__ )
193 # define WEAK __attribute__ ((weak))
196 /* Define NO_INIT attribute */
197 #if defined ( __CC_ARM )
198 # define NO_INIT __attribute__((zero_init))
199 #elif defined ( __ICCARM__ )
200 # define NO_INIT __no_init
201 #elif defined ( __GNUC__ )
202 # define NO_INIT __attribute__((section(".no_init")))
205 //#include "interrupt.h"
207 /** \name Usual Types
210 # if !defined(__bool_true_false_are_defined)
211 typedef unsigned char bool;
214 typedef uint16_t le16_t;
215 typedef uint16_t be16_t;
216 typedef uint32_t le32_t;
217 typedef uint32_t be32_t;
218 typedef uint32_t iram_size_t;
221 /** \name Aliasing Aggregate Types
257 /** Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
270 /** Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
273 volatile int64_t *s64ptr;
274 volatile uint64_t *u64ptr;
275 volatile int32_t *s32ptr;
276 volatile uint32_t *u32ptr;
277 volatile int16_t *s16ptr;
278 volatile uint16_t *u16ptr;
279 volatile int8_t *s8ptr;
280 volatile uint8_t *u8ptr;
283 /** Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
286 const int64_t *s64ptr;
287 const uint64_t *u64ptr;
288 const int32_t *s32ptr;
289 const uint32_t *u32ptr;
290 const int16_t *s16ptr;
291 const uint16_t *u16ptr;
293 const uint8_t *u8ptr;
296 /** Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
299 const volatile int64_t *s64ptr;
300 const volatile uint64_t *u64ptr;
301 const volatile int32_t *s32ptr;
302 const volatile uint32_t *u32ptr;
303 const volatile int16_t *s16ptr;
304 const volatile uint16_t *u16ptr;
305 const volatile int8_t *s8ptr;
306 const volatile uint8_t *u8ptr;
309 /** Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
322 /** Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
325 volatile int64_t *s64ptr;
326 volatile uint64_t *u64ptr;
327 volatile int32_t *s32ptr;
328 volatile uint32_t *u32ptr;
329 volatile int16_t *s16ptr;
330 volatile uint16_t *u16ptr;
331 volatile int8_t *s8ptr;
332 volatile uint8_t *u8ptr;
335 /** Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
338 const int64_t *s64ptr;
339 const uint64_t *u64ptr;
340 const int32_t *s32ptr;
341 const uint32_t *u32ptr;
342 const int16_t *s16ptr;
343 const uint16_t *u16ptr;
345 const uint8_t *u8ptr;
348 /** Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
351 const volatile int64_t *s64ptr;
352 const volatile uint64_t *u64ptr;
353 const volatile int32_t *s32ptr;
354 const volatile uint32_t *u32ptr;
355 const volatile int16_t *s16ptr;
356 const volatile uint16_t *u16ptr;
357 const volatile int8_t *s8ptr;
358 const volatile uint8_t *u8ptr;
363 #endif /* #ifndef __ASSEMBLY__ */
365 /** \name Usual Constants
367 //kmod #define DISABLE 0
368 //kmod #define ENABLE 1
371 # if !defined(__bool_true_false_are_defined)
380 /** \name Optimization Control
385 * \brief The expression \a exp is likely to be true
387 #if !defined(likely) || defined(__DOXYGEN__)
388 # define likely(exp) (exp)
393 * \brief The expression \a exp is unlikely to be true
395 #if !defined(unlikely) || defined(__DOXYGEN__)
396 # define unlikely(exp) (exp)
400 * \def is_constant(exp)
401 * \brief Determine if an expression evaluates to a constant value.
403 * \param[in] exp Any expression
405 * \return true if \a exp is constant, false otherwise.
407 #if (defined __GNUC__) || (defined __CC_ARM)
408 # define is_constant(exp) __builtin_constant_p(exp)
410 # define is_constant(exp) (0)
415 /** \name Bit-Field Handling
418 /** \brief Reads the bits of a value specified by a given bit-mask.
420 * \param[in] value Value to read bits from.
421 * \param[in] mask Bit-mask indicating bits to read.
425 #define Rd_bits( value, mask) ((value) & (mask))
427 /** \brief Writes the bits of a C lvalue specified by a given bit-mask.
429 * \param[in] lvalue C lvalue to write bits to.
430 * \param[in] mask Bit-mask indicating bits to write.
431 * \param[in] bits Bits to write.
433 * \return Resulting value with written bits.
435 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
438 /** \brief Tests the bits of a value specified by a given bit-mask.
440 * \param[in] value Value of which to test bits.
441 * \param[in] mask Bit-mask indicating bits to test.
443 * \return \c 1 if at least one of the tested bits is set, else \c 0.
445 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
447 /** \brief Clears the bits of a C lvalue specified by a given bit-mask.
449 * \param[in] lvalue C lvalue of which to clear bits.
450 * \param[in] mask Bit-mask indicating bits to clear.
452 * \return Resulting value with cleared bits.
454 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
456 /** \brief Sets the bits of a C lvalue specified by a given bit-mask.
458 * \param[in] lvalue C lvalue of which to set bits.
459 * \param[in] mask Bit-mask indicating bits to set.
461 * \return Resulting value with set bits.
463 #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
465 /** \brief Toggles the bits of a C lvalue specified by a given bit-mask.
467 * \param[in] lvalue C lvalue of which to toggle bits.
468 * \param[in] mask Bit-mask indicating bits to toggle.
470 * \return Resulting value with toggled bits.
472 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
474 /** \brief Reads the bit-field of a value specified by a given bit-mask.
476 * \param[in] value Value to read a bit-field from.
477 * \param[in] mask Bit-mask indicating the bit-field to read.
479 * \return Read bit-field.
481 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
483 /** \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
485 * \param[in] lvalue C lvalue to write a bit-field to.
486 * \param[in] mask Bit-mask indicating the bit-field to write.
487 * \param[in] bitfield Bit-field to write.
489 * \return Resulting value with written bit-field.
491 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask)))
496 /** \name Zero-Bit Counting
498 * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
499 * applied to constant expressions (values known at compile time), so they are
500 * more optimized than the use of the corresponding assembly instructions and
501 * they can be used as constant expressions e.g. to initialize objects having
502 * static storage duration, and like the corresponding assembly instructions
503 * when applied to non-constant expressions (values unknown at compile time), so
504 * they are more optimized than an assembly periphrasis. Hence, clz and ctz
505 * ensure a possible and optimized behavior for both constant and non-constant
510 /** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
512 * \param[in] u Value of which to count the leading zero bits.
514 * \return The count of leading zero bits in \a u.
516 #if (defined __GNUC__) || (defined __CC_ARM)
517 # define clz(u) ((u) ? __builtin_clz(u) : 32)
519 # define clz(u) (((u) == 0) ? 32 : \
520 ((u) & (1ul << 31)) ? 0 : \
521 ((u) & (1ul << 30)) ? 1 : \
522 ((u) & (1ul << 29)) ? 2 : \
523 ((u) & (1ul << 28)) ? 3 : \
524 ((u) & (1ul << 27)) ? 4 : \
525 ((u) & (1ul << 26)) ? 5 : \
526 ((u) & (1ul << 25)) ? 6 : \
527 ((u) & (1ul << 24)) ? 7 : \
528 ((u) & (1ul << 23)) ? 8 : \
529 ((u) & (1ul << 22)) ? 9 : \
530 ((u) & (1ul << 21)) ? 10 : \
531 ((u) & (1ul << 20)) ? 11 : \
532 ((u) & (1ul << 19)) ? 12 : \
533 ((u) & (1ul << 18)) ? 13 : \
534 ((u) & (1ul << 17)) ? 14 : \
535 ((u) & (1ul << 16)) ? 15 : \
536 ((u) & (1ul << 15)) ? 16 : \
537 ((u) & (1ul << 14)) ? 17 : \
538 ((u) & (1ul << 13)) ? 18 : \
539 ((u) & (1ul << 12)) ? 19 : \
540 ((u) & (1ul << 11)) ? 20 : \
541 ((u) & (1ul << 10)) ? 21 : \
542 ((u) & (1ul << 9)) ? 22 : \
543 ((u) & (1ul << 8)) ? 23 : \
544 ((u) & (1ul << 7)) ? 24 : \
545 ((u) & (1ul << 6)) ? 25 : \
546 ((u) & (1ul << 5)) ? 26 : \
547 ((u) & (1ul << 4)) ? 27 : \
548 ((u) & (1ul << 3)) ? 28 : \
549 ((u) & (1ul << 2)) ? 29 : \
550 ((u) & (1ul << 1)) ? 30 : \
554 /** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
556 * \param[in] u Value of which to count the trailing zero bits.
558 * \return The count of trailing zero bits in \a u.
560 #if (defined __GNUC__) || (defined __CC_ARM)
561 # define ctz(u) ((u) ? __builtin_ctz(u) : 32)
563 # define ctz(u) ((u) & (1ul << 0) ? 0 : \
564 (u) & (1ul << 1) ? 1 : \
565 (u) & (1ul << 2) ? 2 : \
566 (u) & (1ul << 3) ? 3 : \
567 (u) & (1ul << 4) ? 4 : \
568 (u) & (1ul << 5) ? 5 : \
569 (u) & (1ul << 6) ? 6 : \
570 (u) & (1ul << 7) ? 7 : \
571 (u) & (1ul << 8) ? 8 : \
572 (u) & (1ul << 9) ? 9 : \
573 (u) & (1ul << 10) ? 10 : \
574 (u) & (1ul << 11) ? 11 : \
575 (u) & (1ul << 12) ? 12 : \
576 (u) & (1ul << 13) ? 13 : \
577 (u) & (1ul << 14) ? 14 : \
578 (u) & (1ul << 15) ? 15 : \
579 (u) & (1ul << 16) ? 16 : \
580 (u) & (1ul << 17) ? 17 : \
581 (u) & (1ul << 18) ? 18 : \
582 (u) & (1ul << 19) ? 19 : \
583 (u) & (1ul << 20) ? 20 : \
584 (u) & (1ul << 21) ? 21 : \
585 (u) & (1ul << 22) ? 22 : \
586 (u) & (1ul << 23) ? 23 : \
587 (u) & (1ul << 24) ? 24 : \
588 (u) & (1ul << 25) ? 25 : \
589 (u) & (1ul << 26) ? 26 : \
590 (u) & (1ul << 27) ? 27 : \
591 (u) & (1ul << 28) ? 28 : \
592 (u) & (1ul << 29) ? 29 : \
593 (u) & (1ul << 30) ? 30 : \
594 (u) & (1ul << 31) ? 31 : \
601 /** \name Bit Reversing
604 /** \brief Reverses the bits of \a u8.
606 * \param[in] u8 U8 of which to reverse the bits.
608 * \return Value resulting from \a u8 with reversed bits.
610 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
612 /** \brief Reverses the bits of \a u16.
614 * \param[in] u16 U16 of which to reverse the bits.
616 * \return Value resulting from \a u16 with reversed bits.
618 #define bit_reverse16(u16) ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16))
620 /** \brief Reverses the bits of \a u32.
622 * \param[in] u32 U32 of which to reverse the bits.
624 * \return Value resulting from \a u32 with reversed bits.
626 #define bit_reverse32(u32) __RBIT(u32)
628 /** \brief Reverses the bits of \a u64.
630 * \param[in] u64 U64 of which to reverse the bits.
632 * \return Value resulting from \a u64 with reversed bits.
634 #define bit_reverse64(u64) ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) |\
635 ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32)))
643 /** \brief Tests alignment of the number \a val with the \a n boundary.
645 * \param[in] val Input value.
646 * \param[in] n Boundary.
648 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
650 #define Test_align(val, n) (!Tst_bits( val, (n) - 1 ) )
652 /** \brief Gets alignment of the number \a val with respect to the \a n boundary.
654 * \param[in] val Input value.
655 * \param[in] n Boundary.
657 * \return Alignment of the number \a val with respect to the \a n boundary.
659 #define Get_align(val, n) ( Rd_bits( val, (n) - 1 ) )
661 /** \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
663 * \param[in] lval Input/output lvalue.
664 * \param[in] n Boundary.
665 * \param[in] alg Alignment.
667 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
669 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
671 /** \brief Aligns the number \a val with the upper \a n boundary.
673 * \param[in] val Input value.
674 * \param[in] n Boundary.
676 * \return Value resulting from the number \a val aligned with the upper \a n boundary.
678 #define Align_up( val, n) (((val) + ((n) - 1)) & ~((n) - 1))
680 /** \brief Aligns the number \a val with the lower \a n boundary.
682 * \param[in] val Input value.
683 * \param[in] n Boundary.
685 * \return Value resulting from the number \a val aligned with the lower \a n boundary.
687 #define Align_down(val, n) ( (val) & ~((n) - 1))
692 /** \name Mathematics
694 * The same considerations as for clz and ctz apply here but GCC does not
695 * provide built-in functions to access the assembly instructions abs, min and
696 * max and it does not produce them by itself in most cases, so two sets of
697 * macros are defined here:
698 * - Abs, Min and Max to apply to constant expressions (values known at
700 * - abs, min and max to apply to non-constant expressions (values unknown at
701 * compile time), abs is found in stdlib.h.
705 /** \brief Takes the absolute value of \a a.
707 * \param[in] a Input value.
709 * \return Absolute value of \a a.
711 * \note More optimized if only used with values known at compile time.
713 #define Abs(a) (((a) < 0 ) ? -(a) : (a))
716 /** \brief Takes the minimal value of \a a and \a b.
718 * \param[in] a Input value.
719 * \param[in] b Input value.
721 * \return Minimal value of \a a and \a b.
723 * \note More optimized if only used with values known at compile time.
725 #define Min(a, b) (((a) < (b)) ? (a) : (b))
727 /** \brief Takes the maximal value of \a a and \a b.
729 * \param[in] a Input value.
730 * \param[in] b Input value.
732 * \return Maximal value of \a a and \a b.
734 * \note More optimized if only used with values known at compile time.
736 #define Max(a, b) (((a) > (b)) ? (a) : (b))
738 /** \brief Takes the minimal value of \a a and \a b.
740 * \param[in] a Input value.
741 * \param[in] b Input value.
743 * \return Minimal value of \a a and \a b.
745 * \note More optimized if only used with values unknown at compile time.
747 #define min(a, b) Min(a, b)
749 /** \brief Takes the maximal value of \a a and \a b.
751 * \param[in] a Input value.
752 * \param[in] b Input value.
754 * \return Maximal value of \a a and \a b.
756 * \note More optimized if only used with values unknown at compile time.
758 #define max(a, b) Max(a, b)
764 /** \brief Calls the routine at address \a addr.
766 * It generates a long call opcode.
768 * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
769 * it is invoked from the CPU supervisor mode.
771 * \param[in] addr Address of the routine to call.
773 * \note It may be used as a long jump opcode in some special cases.
775 #define Long_call(addr) ((*(void (*)(void))(addr))())
778 /** \name MCU Endianism Handling
779 * ARM is MCU little endian.
782 #define BE16(x) swap16(x)
785 #define le16_to_cpu(x) (x)
786 #define cpu_to_le16(x) (x)
787 #define LE16_TO_CPU(x) (x)
788 #define CPU_TO_LE16(x) (x)
790 #define be16_to_cpu(x) swap16(x)
791 #define cpu_to_be16(x) swap16(x)
792 #define BE16_TO_CPU(x) swap16(x)
793 #define CPU_TO_BE16(x) swap16(x)
795 #define le32_to_cpu(x) (x)
796 #define cpu_to_le32(x) (x)
797 #define LE32_TO_CPU(x) (x)
798 #define CPU_TO_LE32(x) (x)
800 #define be32_to_cpu(x) swap32(x)
801 #define cpu_to_be32(x) swap32(x)
802 #define BE32_TO_CPU(x) swap32(x)
803 #define CPU_TO_BE32(x) swap32(x)
807 /** \name Endianism Conversion
809 * The same considerations as for clz and ctz apply here but GCC's
810 * __builtin_bswap_32 and __builtin_bswap_64 do not behave like macros when
811 * applied to constant expressions, so two sets of macros are defined here:
812 * - Swap16, Swap32 and Swap64 to apply to constant expressions (values known
814 * - swap16, swap32 and swap64 to apply to non-constant expressions (values
815 * unknown at compile time).
819 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
821 * \param[in] u16 U16 of which to toggle the endianism.
823 * \return Value resulting from \a u16 with toggled endianism.
825 * \note More optimized if only used with values known at compile time.
827 #define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) |\
828 ((uint16_t)(u16) << 8)))
830 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
832 * \param[in] u32 U32 of which to toggle the endianism.
834 * \return Value resulting from \a u32 with toggled endianism.
836 * \note More optimized if only used with values known at compile time.
838 #define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) |\
839 ((uint32_t)Swap16((uint32_t)(u32)) << 16)))
841 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
843 * \param[in] u64 U64 of which to toggle the endianism.
845 * \return Value resulting from \a u64 with toggled endianism.
847 * \note More optimized if only used with values known at compile time.
849 #define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) |\
850 ((uint64_t)Swap32((uint64_t)(u64)) << 32)))
852 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
854 * \param[in] u16 U16 of which to toggle the endianism.
856 * \return Value resulting from \a u16 with toggled endianism.
858 * \note More optimized if only used with values unknown at compile time.
860 #define swap16(u16) Swap16(u16)
862 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
864 * \param[in] u32 U32 of which to toggle the endianism.
866 * \return Value resulting from \a u32 with toggled endianism.
868 * \note More optimized if only used with values unknown at compile time.
870 #if (defined __GNUC__)
871 # define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32)))
873 # define swap32(u32) Swap32(u32)
876 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
878 * \param[in] u64 U64 of which to toggle the endianism.
880 * \return Value resulting from \a u64 with toggled endianism.
882 * \note More optimized if only used with values unknown at compile time.
884 #if (defined __GNUC__)
885 # define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64)))
887 # define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) |\
888 ((uint64_t)swap32((uint64_t)(u64)) << 32)))
894 /** \name Target Abstraction
898 #define _GLOBEXT_ extern /**< extern storage-class specifier. */
899 #define _CONST_TYPE_ const /**< const type qualifier. */
900 #define _MEM_TYPE_SLOW_ /**< Slow memory type. */
901 #define _MEM_TYPE_MEDFAST_ /**< Fairly fast memory type. */
902 #define _MEM_TYPE_FAST_ /**< Fast memory type. */
904 #define memcmp_ram2ram memcmp /**< Target-specific memcmp of RAM to RAM. */
905 #define memcmp_code2ram memcmp /**< Target-specific memcmp of RAM to NVRAM. */
906 #define memcpy_ram2ram memcpy /**< Target-specific memcpy from RAM to RAM. */
907 #define memcpy_code2ram memcpy /**< Target-specific memcpy from NVRAM to RAM. */
912 * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
913 * integer arithmetic.
915 * \param[in] a An integer
916 * \param[in] b Another integer
918 * \return (\a a / \a b) rounded up to the nearest integer.
920 #define div_ceil(a, b) (((a) + (b) - 1) / (b))
922 #endif /* #ifndef __ASSEMBLY__ */
924 /** \name Compiler Keywords
926 * Port of some keywords from GCC to IAR Embedded Workbench.
931 #define __inline__ inline
938 #define FUNC_PTR void *
941 * \brief Marking \a v as a unused parameter or value.
943 #define unused(v) do { (void)(v); } while(0)
945 /* Define RAMFUNC attribute */
946 #if defined ( __CC_ARM ) /* Keil uVision 4 */
947 # define RAMFUNC __attribute__ ((section(".ramfunc")))
948 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
949 # define RAMFUNC __ramfunc
950 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
951 # define RAMFUNC __attribute__ ((section(".ramfunc")))
954 /* Define OPTIMIZE_HIGH attribute */
955 #if defined ( __CC_ARM ) /* Keil uVision 4 */
956 # define OPTIMIZE_HIGH _Pragma("O3")
957 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
958 # define OPTIMIZE_HIGH _Pragma("optimize=high")
959 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
960 # define OPTIMIZE_HIGH __attribute__((optimize("s")))
962 //kmod #define PASS 0
963 //kmod #define FAIL 1
965 //kmod #define HIGH 1
967 typedef int8_t S8 ; //!< 8-bit signed integer.
968 typedef uint8_t U8 ; //!< 8-bit unsigned integer.
969 typedef int16_t S16; //!< 16-bit signed integer.
970 typedef uint16_t U16; //!< 16-bit unsigned integer.
971 typedef int32_t S32; //!< 32-bit signed integer.
972 typedef uint32_t U32; //!< 32-bit unsigned integer.
973 typedef int64_t S64; //!< 64-bit signed integer.
974 typedef uint64_t U64; //!< 64-bit unsigned integer.
975 typedef float F32; //!< 32-bit floating-point number.
976 typedef double F64; //!< 64-bit floating-point number.
978 #define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16.
979 #define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16.
981 #define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32.
982 #define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32.
983 #define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32.
984 #define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32.
985 #define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32.
986 #define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32.
987 #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32.
988 #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32.
989 #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32.
990 #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32.
992 #define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64.
993 #define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64.
994 #define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64.
995 #define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64.
996 #define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64.
997 #define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64.
998 #define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64.
999 #define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64.
1000 #define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64.
1001 #define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64.
1002 #define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64.
1003 #define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64.
1004 #define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64.
1005 #define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64.
1006 #define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64.
1007 #define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64.
1008 #define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64.
1009 #define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64.
1010 #define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64.
1011 #define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64.
1012 #define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64.
1013 #define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64.
1014 #define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64.
1015 #define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64.
1016 #define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64.
1017 #define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64.
1019 #define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32.
1020 #define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32.
1021 #define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32.
1022 #define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32.
1023 #define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32.
1024 #define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32.
1025 #define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32.
1026 #define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32.
1028 #if defined(__ICCARM__)
1029 #define SHORTENUM __packed
1030 #elif defined(__GNUC__)
1031 #define SHORTENUM __attribute__((packed))
1035 #if defined(__ICCARM__)
1036 #define nop() __no_operation()
1037 #elif defined(__GNUC__)
1038 #define nop() (__NOP())
1041 #define FLASH_DECLARE(x) const x
1042 #define FLASH_EXTERN(x) extern const x
1043 #define PGM_READ_BYTE(x) *(x)
1044 #define PGM_READ_WORD(x) *(x)
1045 #define MEMCPY_ENDIAN memcpy
1046 #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len))
1048 /*Defines the Flash Storage for the request and response of MAC*/
1049 #define CMD_ID_OCTET (0)
1051 /* Converting of values from CPU endian to little endian. */
1052 #define CPU_ENDIAN_TO_LE16(x) (x)
1053 #define CPU_ENDIAN_TO_LE32(x) (x)
1054 #define CPU_ENDIAN_TO_LE64(x) (x)
1056 /* Converting of values from little endian to CPU endian. */
1057 #define LE16_TO_CPU_ENDIAN(x) (x)
1058 #define LE32_TO_CPU_ENDIAN(x) (x)
1059 #define LE64_TO_CPU_ENDIAN(x) (x)
1061 /* Converting of constants from little endian to CPU endian. */
1062 #define CLE16_TO_CPU_ENDIAN(x) (x)
1063 #define CLE32_TO_CPU_ENDIAN(x) (x)
1064 #define CLE64_TO_CPU_ENDIAN(x) (x)
1066 /* Converting of constants from CPU endian to little endian. */
1067 #define CCPU_ENDIAN_TO_LE16(x) (x)
1068 #define CCPU_ENDIAN_TO_LE32(x) (x)
1069 #define CCPU_ENDIAN_TO_LE64(x) (x)
1071 #define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src))
1072 #define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src))
1075 * @brief Converts a 64-Bit value into a 8 Byte array
1077 * @param[in] value 64-Bit value
1078 * @param[out] data Pointer to the 8 Byte array to be updated with 64-Bit value
1079 * @ingroup apiPalApi
1081 static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data)
1087 data[index++] = value & 0xFF;
1093 * @brief Converts a 16-Bit value into a 2 Byte array
1095 * @param[in] value 16-Bit value
1096 * @param[out] data Pointer to the 2 Byte array to be updated with 16-Bit value
1097 * @ingroup apiPalApi
1099 static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1101 data[0] = value & 0xFF;
1102 data[1] = (value >> 8) & 0xFF;
1105 /* Converts a 16-Bit value into a 2 Byte array */
1106 static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1108 data[0] = value & 0xFF;
1109 data[1] = (value >> 8) & 0xFF;
1112 /* Converts a 16-Bit value into a 2 Byte array */
1113 static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data)
1115 data[0] = value & 0xFF;
1116 data[1] = (value >> 8) & 0xFF;
1120 * @brief Converts a 2 Byte array into a 16-Bit value
1122 * @param data Specifies the pointer to the 2 Byte array
1124 * @return 16-Bit value
1125 * @ingroup apiPalApi
1127 static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data)
1129 return (data[0] | ((uint16_t)data[1] << 8));
1132 /* Converts a 4 Byte array into a 32-Bit value */
1133 static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data)
1143 for (index = 0; index < 4; index++)
1145 long_addr.u8[index] = *data++;
1148 return long_addr.u32;
1152 * @brief Converts a 8 Byte array into a 64-Bit value
1154 * @param data Specifies the pointer to the 8 Byte array
1156 * @return 64-Bit value
1157 * @ingroup apiPalApi
1159 static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data)
1169 for (index = 0; index < 8; index++)
1171 long_addr.u8[index] = *data++;
1174 return long_addr.u64;
1179 #endif /* UTILS_COMPILER_H_INCLUDED */