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(__CC_ARM)
138 # define __always_inline __forceinline
139 #elif (defined __GNUC__ && __GNUC__ <= 6)
140 # define __always_inline __attribute__((__always_inline__))
141 #elif (defined __ICCARM__)
142 # define __always_inline _Pragma("inline=forced")
147 * \brief The function should never be inlined
149 * This annotation instructs the compiler to ignore its inlining
150 * heuristics and not inline the function no matter how small it thinks it
153 #if defined(__CC_ARM)
154 # define __no_inline __attribute__((noinline))
155 #elif (defined __GNUC__)
156 # define __no_inline __attribute__((noinline))
157 #elif (defined __ICCARM__)
158 # define __no_inline _Pragma("inline=never")
162 /** \brief This macro is used to test fatal errors.
164 * The macro tests if the expression is false. If it is, a fatal error is
165 * detected and the application hangs up. If \c TEST_SUITE_DEFINE_ASSERT_MACRO
166 * is defined, a unit test version of the macro is used, to allow execution
167 * of further tests after a false expression.
169 * \param[in] expr Expression to evaluate and supposed to be nonzero.
171 #if defined(_ASSERT_ENABLE_)
172 # if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
173 # include "unit_test/suite.h"
175 # undef TEST_SUITE_DEFINE_ASSERT_MACRO
176 # define Assert(expr) \
178 if (!(expr)) asm("BKPT #0");\
182 # define Assert(expr) ((void) 0)
185 /* Define WEAK attribute */
186 #if defined ( __CC_ARM )
187 # define WEAK __attribute__ ((weak))
188 #elif defined ( __ICCARM__ )
190 #elif defined ( __GNUC__ )
191 # define WEAK __attribute__ ((weak))
194 /* Define NO_INIT attribute */
195 #if defined ( __CC_ARM )
196 # define NO_INIT __attribute__((zero_init))
197 #elif defined ( __ICCARM__ )
198 # define NO_INIT __no_init
199 #elif defined ( __GNUC__ )
200 # define NO_INIT __attribute__((section(".no_init")))
203 //#include "interrupt.h"
205 /** \name Usual Types
208 # if !defined(__bool_true_false_are_defined)
209 typedef unsigned char bool;
212 typedef uint16_t le16_t;
213 typedef uint16_t be16_t;
214 typedef uint32_t le32_t;
215 typedef uint32_t be32_t;
216 typedef uint32_t iram_size_t;
219 /** \name Aliasing Aggregate Types
255 /** Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
268 /** Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
271 volatile int64_t *s64ptr;
272 volatile uint64_t *u64ptr;
273 volatile int32_t *s32ptr;
274 volatile uint32_t *u32ptr;
275 volatile int16_t *s16ptr;
276 volatile uint16_t *u16ptr;
277 volatile int8_t *s8ptr;
278 volatile uint8_t *u8ptr;
281 /** Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
284 const int64_t *s64ptr;
285 const uint64_t *u64ptr;
286 const int32_t *s32ptr;
287 const uint32_t *u32ptr;
288 const int16_t *s16ptr;
289 const uint16_t *u16ptr;
291 const uint8_t *u8ptr;
294 /** Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
297 const volatile int64_t *s64ptr;
298 const volatile uint64_t *u64ptr;
299 const volatile int32_t *s32ptr;
300 const volatile uint32_t *u32ptr;
301 const volatile int16_t *s16ptr;
302 const volatile uint16_t *u16ptr;
303 const volatile int8_t *s8ptr;
304 const volatile uint8_t *u8ptr;
307 /** Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
320 /** Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
323 volatile int64_t *s64ptr;
324 volatile uint64_t *u64ptr;
325 volatile int32_t *s32ptr;
326 volatile uint32_t *u32ptr;
327 volatile int16_t *s16ptr;
328 volatile uint16_t *u16ptr;
329 volatile int8_t *s8ptr;
330 volatile uint8_t *u8ptr;
333 /** Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
336 const int64_t *s64ptr;
337 const uint64_t *u64ptr;
338 const int32_t *s32ptr;
339 const uint32_t *u32ptr;
340 const int16_t *s16ptr;
341 const uint16_t *u16ptr;
343 const uint8_t *u8ptr;
346 /** Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
349 const volatile int64_t *s64ptr;
350 const volatile uint64_t *u64ptr;
351 const volatile int32_t *s32ptr;
352 const volatile uint32_t *u32ptr;
353 const volatile int16_t *s16ptr;
354 const volatile uint16_t *u16ptr;
355 const volatile int8_t *s8ptr;
356 const volatile uint8_t *u8ptr;
361 #endif /* #ifndef __ASSEMBLY__ */
363 /** \name Usual Constants
365 //kmod #define DISABLE 0
366 //kmod #define ENABLE 1
369 # if !defined(__bool_true_false_are_defined)
378 /** \name Optimization Control
383 * \brief The expression \a exp is likely to be true
385 #if !defined(likely) || defined(__DOXYGEN__)
386 # define likely(exp) (exp)
391 * \brief The expression \a exp is unlikely to be true
393 #if !defined(unlikely) || defined(__DOXYGEN__)
394 # define unlikely(exp) (exp)
398 * \def is_constant(exp)
399 * \brief Determine if an expression evaluates to a constant value.
401 * \param[in] exp Any expression
403 * \return true if \a exp is constant, false otherwise.
405 #if (defined __GNUC__) || (defined __CC_ARM)
406 # define is_constant(exp) __builtin_constant_p(exp)
408 # define is_constant(exp) (0)
413 /** \name Bit-Field Handling
416 /** \brief Reads the bits of a value specified by a given bit-mask.
418 * \param[in] value Value to read bits from.
419 * \param[in] mask Bit-mask indicating bits to read.
423 #define Rd_bits( value, mask) ((value) & (mask))
425 /** \brief Writes the bits of a C lvalue specified by a given bit-mask.
427 * \param[in] lvalue C lvalue to write bits to.
428 * \param[in] mask Bit-mask indicating bits to write.
429 * \param[in] bits Bits to write.
431 * \return Resulting value with written bits.
433 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
436 /** \brief Tests the bits of a value specified by a given bit-mask.
438 * \param[in] value Value of which to test bits.
439 * \param[in] mask Bit-mask indicating bits to test.
441 * \return \c 1 if at least one of the tested bits is set, else \c 0.
443 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
445 /** \brief Clears the bits of a C lvalue specified by a given bit-mask.
447 * \param[in] lvalue C lvalue of which to clear bits.
448 * \param[in] mask Bit-mask indicating bits to clear.
450 * \return Resulting value with cleared bits.
452 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
454 /** \brief Sets the bits of a C lvalue specified by a given bit-mask.
456 * \param[in] lvalue C lvalue of which to set bits.
457 * \param[in] mask Bit-mask indicating bits to set.
459 * \return Resulting value with set bits.
461 #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
463 /** \brief Toggles the bits of a C lvalue specified by a given bit-mask.
465 * \param[in] lvalue C lvalue of which to toggle bits.
466 * \param[in] mask Bit-mask indicating bits to toggle.
468 * \return Resulting value with toggled bits.
470 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
472 /** \brief Reads the bit-field of a value specified by a given bit-mask.
474 * \param[in] value Value to read a bit-field from.
475 * \param[in] mask Bit-mask indicating the bit-field to read.
477 * \return Read bit-field.
479 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
481 /** \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
483 * \param[in] lvalue C lvalue to write a bit-field to.
484 * \param[in] mask Bit-mask indicating the bit-field to write.
485 * \param[in] bitfield Bit-field to write.
487 * \return Resulting value with written bit-field.
489 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask)))
494 /** \name Zero-Bit Counting
496 * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
497 * applied to constant expressions (values known at compile time), so they are
498 * more optimized than the use of the corresponding assembly instructions and
499 * they can be used as constant expressions e.g. to initialize objects having
500 * static storage duration, and like the corresponding assembly instructions
501 * when applied to non-constant expressions (values unknown at compile time), so
502 * they are more optimized than an assembly periphrasis. Hence, clz and ctz
503 * ensure a possible and optimized behavior for both constant and non-constant
508 /** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
510 * \param[in] u Value of which to count the leading zero bits.
512 * \return The count of leading zero bits in \a u.
514 #if (defined __GNUC__) || (defined __CC_ARM)
515 # define clz(u) ((u) ? __builtin_clz(u) : 32)
517 # define clz(u) (((u) == 0) ? 32 : \
518 ((u) & (1ul << 31)) ? 0 : \
519 ((u) & (1ul << 30)) ? 1 : \
520 ((u) & (1ul << 29)) ? 2 : \
521 ((u) & (1ul << 28)) ? 3 : \
522 ((u) & (1ul << 27)) ? 4 : \
523 ((u) & (1ul << 26)) ? 5 : \
524 ((u) & (1ul << 25)) ? 6 : \
525 ((u) & (1ul << 24)) ? 7 : \
526 ((u) & (1ul << 23)) ? 8 : \
527 ((u) & (1ul << 22)) ? 9 : \
528 ((u) & (1ul << 21)) ? 10 : \
529 ((u) & (1ul << 20)) ? 11 : \
530 ((u) & (1ul << 19)) ? 12 : \
531 ((u) & (1ul << 18)) ? 13 : \
532 ((u) & (1ul << 17)) ? 14 : \
533 ((u) & (1ul << 16)) ? 15 : \
534 ((u) & (1ul << 15)) ? 16 : \
535 ((u) & (1ul << 14)) ? 17 : \
536 ((u) & (1ul << 13)) ? 18 : \
537 ((u) & (1ul << 12)) ? 19 : \
538 ((u) & (1ul << 11)) ? 20 : \
539 ((u) & (1ul << 10)) ? 21 : \
540 ((u) & (1ul << 9)) ? 22 : \
541 ((u) & (1ul << 8)) ? 23 : \
542 ((u) & (1ul << 7)) ? 24 : \
543 ((u) & (1ul << 6)) ? 25 : \
544 ((u) & (1ul << 5)) ? 26 : \
545 ((u) & (1ul << 4)) ? 27 : \
546 ((u) & (1ul << 3)) ? 28 : \
547 ((u) & (1ul << 2)) ? 29 : \
548 ((u) & (1ul << 1)) ? 30 : \
552 /** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
554 * \param[in] u Value of which to count the trailing zero bits.
556 * \return The count of trailing zero bits in \a u.
558 #if (defined __GNUC__) || (defined __CC_ARM)
559 # define ctz(u) ((u) ? __builtin_ctz(u) : 32)
561 # define ctz(u) ((u) & (1ul << 0) ? 0 : \
562 (u) & (1ul << 1) ? 1 : \
563 (u) & (1ul << 2) ? 2 : \
564 (u) & (1ul << 3) ? 3 : \
565 (u) & (1ul << 4) ? 4 : \
566 (u) & (1ul << 5) ? 5 : \
567 (u) & (1ul << 6) ? 6 : \
568 (u) & (1ul << 7) ? 7 : \
569 (u) & (1ul << 8) ? 8 : \
570 (u) & (1ul << 9) ? 9 : \
571 (u) & (1ul << 10) ? 10 : \
572 (u) & (1ul << 11) ? 11 : \
573 (u) & (1ul << 12) ? 12 : \
574 (u) & (1ul << 13) ? 13 : \
575 (u) & (1ul << 14) ? 14 : \
576 (u) & (1ul << 15) ? 15 : \
577 (u) & (1ul << 16) ? 16 : \
578 (u) & (1ul << 17) ? 17 : \
579 (u) & (1ul << 18) ? 18 : \
580 (u) & (1ul << 19) ? 19 : \
581 (u) & (1ul << 20) ? 20 : \
582 (u) & (1ul << 21) ? 21 : \
583 (u) & (1ul << 22) ? 22 : \
584 (u) & (1ul << 23) ? 23 : \
585 (u) & (1ul << 24) ? 24 : \
586 (u) & (1ul << 25) ? 25 : \
587 (u) & (1ul << 26) ? 26 : \
588 (u) & (1ul << 27) ? 27 : \
589 (u) & (1ul << 28) ? 28 : \
590 (u) & (1ul << 29) ? 29 : \
591 (u) & (1ul << 30) ? 30 : \
592 (u) & (1ul << 31) ? 31 : \
599 /** \name Bit Reversing
602 /** \brief Reverses the bits of \a u8.
604 * \param[in] u8 U8 of which to reverse the bits.
606 * \return Value resulting from \a u8 with reversed bits.
608 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
610 /** \brief Reverses the bits of \a u16.
612 * \param[in] u16 U16 of which to reverse the bits.
614 * \return Value resulting from \a u16 with reversed bits.
616 #define bit_reverse16(u16) ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16))
618 /** \brief Reverses the bits of \a u32.
620 * \param[in] u32 U32 of which to reverse the bits.
622 * \return Value resulting from \a u32 with reversed bits.
624 #define bit_reverse32(u32) __RBIT(u32)
626 /** \brief Reverses the bits of \a u64.
628 * \param[in] u64 U64 of which to reverse the bits.
630 * \return Value resulting from \a u64 with reversed bits.
632 #define bit_reverse64(u64) ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) |\
633 ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32)))
641 /** \brief Tests alignment of the number \a val with the \a n boundary.
643 * \param[in] val Input value.
644 * \param[in] n Boundary.
646 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
648 #define Test_align(val, n) (!Tst_bits( val, (n) - 1 ) )
650 /** \brief Gets alignment of the number \a val with respect to the \a n boundary.
652 * \param[in] val Input value.
653 * \param[in] n Boundary.
655 * \return Alignment of the number \a val with respect to the \a n boundary.
657 #define Get_align(val, n) ( Rd_bits( val, (n) - 1 ) )
659 /** \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
661 * \param[in] lval Input/output lvalue.
662 * \param[in] n Boundary.
663 * \param[in] alg Alignment.
665 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
667 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
669 /** \brief Aligns the number \a val with the upper \a n boundary.
671 * \param[in] val Input value.
672 * \param[in] n Boundary.
674 * \return Value resulting from the number \a val aligned with the upper \a n boundary.
676 #define Align_up( val, n) (((val) + ((n) - 1)) & ~((n) - 1))
678 /** \brief Aligns the number \a val with the lower \a n boundary.
680 * \param[in] val Input value.
681 * \param[in] n Boundary.
683 * \return Value resulting from the number \a val aligned with the lower \a n boundary.
685 #define Align_down(val, n) ( (val) & ~((n) - 1))
690 /** \name Mathematics
692 * The same considerations as for clz and ctz apply here but GCC does not
693 * provide built-in functions to access the assembly instructions abs, min and
694 * max and it does not produce them by itself in most cases, so two sets of
695 * macros are defined here:
696 * - Abs, Min and Max to apply to constant expressions (values known at
698 * - abs, min and max to apply to non-constant expressions (values unknown at
699 * compile time), abs is found in stdlib.h.
703 /** \brief Takes the absolute value of \a a.
705 * \param[in] a Input value.
707 * \return Absolute value of \a a.
709 * \note More optimized if only used with values known at compile time.
711 #define Abs(a) (((a) < 0 ) ? -(a) : (a))
714 /** \brief Takes the minimal value of \a a and \a b.
716 * \param[in] a Input value.
717 * \param[in] b Input value.
719 * \return Minimal value of \a a and \a b.
721 * \note More optimized if only used with values known at compile time.
723 #define Min(a, b) (((a) < (b)) ? (a) : (b))
725 /** \brief Takes the maximal value of \a a and \a b.
727 * \param[in] a Input value.
728 * \param[in] b Input value.
730 * \return Maximal value of \a a and \a b.
732 * \note More optimized if only used with values known at compile time.
734 #define Max(a, b) (((a) > (b)) ? (a) : (b))
736 /** \brief Takes the minimal value of \a a and \a b.
738 * \param[in] a Input value.
739 * \param[in] b Input value.
741 * \return Minimal value of \a a and \a b.
743 * \note More optimized if only used with values unknown at compile time.
745 #define min(a, b) Min(a, b)
747 /** \brief Takes the maximal value of \a a and \a b.
749 * \param[in] a Input value.
750 * \param[in] b Input value.
752 * \return Maximal value of \a a and \a b.
754 * \note More optimized if only used with values unknown at compile time.
756 #define max(a, b) Max(a, b)
762 /** \brief Calls the routine at address \a addr.
764 * It generates a long call opcode.
766 * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
767 * it is invoked from the CPU supervisor mode.
769 * \param[in] addr Address of the routine to call.
771 * \note It may be used as a long jump opcode in some special cases.
773 #define Long_call(addr) ((*(void (*)(void))(addr))())
776 /** \name MCU Endianism Handling
777 * ARM is MCU little endian.
780 #define BE16(x) swap16(x)
783 #define le16_to_cpu(x) (x)
784 #define cpu_to_le16(x) (x)
785 #define LE16_TO_CPU(x) (x)
786 #define CPU_TO_LE16(x) (x)
788 #define be16_to_cpu(x) swap16(x)
789 #define cpu_to_be16(x) swap16(x)
790 #define BE16_TO_CPU(x) swap16(x)
791 #define CPU_TO_BE16(x) swap16(x)
793 #define le32_to_cpu(x) (x)
794 #define cpu_to_le32(x) (x)
795 #define LE32_TO_CPU(x) (x)
796 #define CPU_TO_LE32(x) (x)
798 #define be32_to_cpu(x) swap32(x)
799 #define cpu_to_be32(x) swap32(x)
800 #define BE32_TO_CPU(x) swap32(x)
801 #define CPU_TO_BE32(x) swap32(x)
805 /** \name Endianism Conversion
807 * The same considerations as for clz and ctz apply here but GCC's
808 * __builtin_bswap_32 and __builtin_bswap_64 do not behave like macros when
809 * applied to constant expressions, so two sets of macros are defined here:
810 * - Swap16, Swap32 and Swap64 to apply to constant expressions (values known
812 * - swap16, swap32 and swap64 to apply to non-constant expressions (values
813 * unknown at compile time).
817 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
819 * \param[in] u16 U16 of which to toggle the endianism.
821 * \return Value resulting from \a u16 with toggled endianism.
823 * \note More optimized if only used with values known at compile time.
825 #define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) |\
826 ((uint16_t)(u16) << 8)))
828 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
830 * \param[in] u32 U32 of which to toggle the endianism.
832 * \return Value resulting from \a u32 with toggled endianism.
834 * \note More optimized if only used with values known at compile time.
836 #define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) |\
837 ((uint32_t)Swap16((uint32_t)(u32)) << 16)))
839 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
841 * \param[in] u64 U64 of which to toggle the endianism.
843 * \return Value resulting from \a u64 with toggled endianism.
845 * \note More optimized if only used with values known at compile time.
847 #define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) |\
848 ((uint64_t)Swap32((uint64_t)(u64)) << 32)))
850 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
852 * \param[in] u16 U16 of which to toggle the endianism.
854 * \return Value resulting from \a u16 with toggled endianism.
856 * \note More optimized if only used with values unknown at compile time.
858 #define swap16(u16) Swap16(u16)
860 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
862 * \param[in] u32 U32 of which to toggle the endianism.
864 * \return Value resulting from \a u32 with toggled endianism.
866 * \note More optimized if only used with values unknown at compile time.
868 #if (defined __GNUC__)
869 # define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32)))
871 # define swap32(u32) Swap32(u32)
874 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
876 * \param[in] u64 U64 of which to toggle the endianism.
878 * \return Value resulting from \a u64 with toggled endianism.
880 * \note More optimized if only used with values unknown at compile time.
882 #if (defined __GNUC__)
883 # define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64)))
885 # define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) |\
886 ((uint64_t)swap32((uint64_t)(u64)) << 32)))
892 /** \name Target Abstraction
896 #define _GLOBEXT_ extern /**< extern storage-class specifier. */
897 #define _CONST_TYPE_ const /**< const type qualifier. */
898 #define _MEM_TYPE_SLOW_ /**< Slow memory type. */
899 #define _MEM_TYPE_MEDFAST_ /**< Fairly fast memory type. */
900 #define _MEM_TYPE_FAST_ /**< Fast memory type. */
902 #define memcmp_ram2ram memcmp /**< Target-specific memcmp of RAM to RAM. */
903 #define memcmp_code2ram memcmp /**< Target-specific memcmp of RAM to NVRAM. */
904 #define memcpy_ram2ram memcpy /**< Target-specific memcpy from RAM to RAM. */
905 #define memcpy_code2ram memcpy /**< Target-specific memcpy from NVRAM to RAM. */
910 * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
911 * integer arithmetic.
913 * \param[in] a An integer
914 * \param[in] b Another integer
916 * \return (\a a / \a b) rounded up to the nearest integer.
918 #define div_ceil(a, b) (((a) + (b) - 1) / (b))
920 #endif /* #ifndef __ASSEMBLY__ */
922 /** \name Compiler Keywords
924 * Port of some keywords from GCC to IAR Embedded Workbench.
929 #define __inline__ inline
936 #define FUNC_PTR void *
939 * \brief Marking \a v as a unused parameter or value.
941 #define unused(v) do { (void)(v); } while(0)
943 /* Define RAMFUNC attribute */
944 #if defined ( __CC_ARM ) /* Keil uVision 4 */
945 # define RAMFUNC __attribute__ ((section(".ramfunc")))
946 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
947 # define RAMFUNC __ramfunc
948 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
949 # define RAMFUNC __attribute__ ((section(".ramfunc")))
952 /* Define OPTIMIZE_HIGH attribute */
953 #if defined ( __CC_ARM ) /* Keil uVision 4 */
954 # define OPTIMIZE_HIGH _Pragma("O3")
955 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
956 # define OPTIMIZE_HIGH _Pragma("optimize=high")
957 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
958 # define OPTIMIZE_HIGH __attribute__((optimize("s")))
960 //kmod #define PASS 0
961 //kmod #define FAIL 1
963 //kmod #define HIGH 1
965 typedef int8_t S8 ; //!< 8-bit signed integer.
966 typedef uint8_t U8 ; //!< 8-bit unsigned integer.
967 typedef int16_t S16; //!< 16-bit signed integer.
968 typedef uint16_t U16; //!< 16-bit unsigned integer.
969 typedef int32_t S32; //!< 32-bit signed integer.
970 typedef uint32_t U32; //!< 32-bit unsigned integer.
971 typedef int64_t S64; //!< 64-bit signed integer.
972 typedef uint64_t U64; //!< 64-bit unsigned integer.
973 typedef float F32; //!< 32-bit floating-point number.
974 typedef double F64; //!< 64-bit floating-point number.
976 #define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16.
977 #define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16.
979 #define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32.
980 #define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32.
981 #define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32.
982 #define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32.
983 #define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32.
984 #define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32.
985 #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32.
986 #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32.
987 #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32.
988 #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32.
990 #define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64.
991 #define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64.
992 #define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64.
993 #define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64.
994 #define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64.
995 #define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64.
996 #define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64.
997 #define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64.
998 #define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64.
999 #define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64.
1000 #define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64.
1001 #define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64.
1002 #define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64.
1003 #define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64.
1004 #define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64.
1005 #define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64.
1006 #define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64.
1007 #define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64.
1008 #define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64.
1009 #define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64.
1010 #define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64.
1011 #define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64.
1012 #define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64.
1013 #define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64.
1014 #define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64.
1015 #define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64.
1017 #define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32.
1018 #define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32.
1019 #define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32.
1020 #define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32.
1021 #define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32.
1022 #define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32.
1023 #define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32.
1024 #define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32.
1026 #if defined(__ICCARM__)
1027 #define SHORTENUM __packed
1028 #elif defined(__GNUC__)
1029 #define SHORTENUM __attribute__((packed))
1033 #if defined(__ICCARM__)
1034 #define nop() __no_operation()
1035 #elif defined(__GNUC__)
1036 #define nop() (__NOP())
1039 #define FLASH_DECLARE(x) const x
1040 #define FLASH_EXTERN(x) extern const x
1041 #define PGM_READ_BYTE(x) *(x)
1042 #define PGM_READ_WORD(x) *(x)
1043 #define MEMCPY_ENDIAN memcpy
1044 #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len))
1046 /*Defines the Flash Storage for the request and response of MAC*/
1047 #define CMD_ID_OCTET (0)
1049 /* Converting of values from CPU endian to little endian. */
1050 #define CPU_ENDIAN_TO_LE16(x) (x)
1051 #define CPU_ENDIAN_TO_LE32(x) (x)
1052 #define CPU_ENDIAN_TO_LE64(x) (x)
1054 /* Converting of values from little endian to CPU endian. */
1055 #define LE16_TO_CPU_ENDIAN(x) (x)
1056 #define LE32_TO_CPU_ENDIAN(x) (x)
1057 #define LE64_TO_CPU_ENDIAN(x) (x)
1059 /* Converting of constants from little endian to CPU endian. */
1060 #define CLE16_TO_CPU_ENDIAN(x) (x)
1061 #define CLE32_TO_CPU_ENDIAN(x) (x)
1062 #define CLE64_TO_CPU_ENDIAN(x) (x)
1064 /* Converting of constants from CPU endian to little endian. */
1065 #define CCPU_ENDIAN_TO_LE16(x) (x)
1066 #define CCPU_ENDIAN_TO_LE32(x) (x)
1067 #define CCPU_ENDIAN_TO_LE64(x) (x)
1069 #define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src))
1070 #define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src))
1073 * @brief Converts a 64-Bit value into a 8 Byte array
1075 * @param[in] value 64-Bit value
1076 * @param[out] data Pointer to the 8 Byte array to be updated with 64-Bit value
1077 * @ingroup apiPalApi
1079 static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data)
1085 data[index++] = value & 0xFF;
1091 * @brief Converts a 16-Bit value into a 2 Byte array
1093 * @param[in] value 16-Bit value
1094 * @param[out] data Pointer to the 2 Byte array to be updated with 16-Bit value
1095 * @ingroup apiPalApi
1097 static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1099 data[0] = value & 0xFF;
1100 data[1] = (value >> 8) & 0xFF;
1103 /* Converts a 16-Bit value into a 2 Byte array */
1104 static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1106 data[0] = value & 0xFF;
1107 data[1] = (value >> 8) & 0xFF;
1110 /* Converts a 16-Bit value into a 2 Byte array */
1111 static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data)
1113 data[0] = value & 0xFF;
1114 data[1] = (value >> 8) & 0xFF;
1118 * @brief Converts a 2 Byte array into a 16-Bit value
1120 * @param data Specifies the pointer to the 2 Byte array
1122 * @return 16-Bit value
1123 * @ingroup apiPalApi
1125 static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data)
1127 return (data[0] | ((uint16_t)data[1] << 8));
1130 /* Converts a 4 Byte array into a 32-Bit value */
1131 static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data)
1141 for (index = 0; index < 4; index++)
1143 long_addr.u8[index] = *data++;
1146 return long_addr.u32;
1150 * @brief Converts a 8 Byte array into a 64-Bit value
1152 * @param data Specifies the pointer to the 8 Byte array
1154 * @return 64-Bit value
1155 * @ingroup apiPalApi
1157 static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data)
1167 for (index = 0; index < 8; index++)
1169 long_addr.u8[index] = *data++;
1172 return long_addr.u64;
1177 #endif /* UTILS_COMPILER_H_INCLUDED */