]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/usb/compiler.h
Fix macro redefinition for GNUC compilers > 6
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / usb / compiler.h
1 /**
2  * \file
3  *
4  * \brief Commonly used includes, types and macros.
5  *
6  * Copyright (C) 2012-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  *
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.
19  *
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.
22  *
23  * 4. This software may only be redistributed and used in connection with an
24  *    Atmel microcontroller product.
25  *
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.
37  *
38  * \asf_license_stop
39  *
40  */
41 /*
42  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
43  */
44
45 #ifndef UTILS_COMPILER_H_INCLUDED
46 #define UTILS_COMPILER_H_INCLUDED
47
48 /**
49  * \defgroup group_sam0_utils Compiler abstraction layer and code utilities
50  *
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.
53  *
54  * @{
55  */
56
57 #if (defined __ICCARM__)
58 #  include <intrinsics.h>
59 #endif
60
61 #include <stddef.h>
62 //#include <parts.h>
63 //#include <status_codes.h>
64 //#include <preprocessor.h>
65 //#include <io.h>
66
67 #ifndef __ASSEMBLY__
68
69 #include <stdio.h>
70 #include <stdbool.h>
71 #include <stdint.h>
72 #include <stdlib.h>
73
74 /**
75  * \def UNUSED
76  * \brief Marking \a v as a unused parameter or value.
77  */
78 #define UNUSED(v)          (void)(v)
79
80 /**
81  * \def barrier
82  * \brief Memory barrier
83  */
84 #ifdef __GNUC__
85 #  define barrier()        asm volatile("" ::: "memory")
86 #else
87 #  define barrier()        asm ("")
88 #endif
89
90 /**
91  * \brief Emit the compiler pragma \a arg.
92  *
93  * \param[in] arg  The pragma directive as it would appear after \e \#pragma
94  *             (i.e. not stringified).
95  */
96 #define COMPILER_PRAGMA(arg)          _Pragma(#arg)
97
98 /**
99  * \def COMPILER_PACK_SET(alignment)
100  * \brief Set maximum alignment for subsequent struct and union definitions to \a alignment.
101  */
102 #define COMPILER_PACK_SET(alignment)  COMPILER_PRAGMA(pack(alignment))
103
104 /**
105  * \def COMPILER_PACK_RESET()
106  * \brief Set default alignment for subsequent struct and union definitions.
107  */
108 #define COMPILER_PACK_RESET()         COMPILER_PRAGMA(pack())
109
110
111 /**
112  * \brief Set aligned boundary.
113  */
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)
118 #endif
119
120 /**
121  * \brief Set word-aligned boundary.
122  */
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)
127 #endif
128
129 /**
130  * \def __always_inline
131  * \brief The function should always be inlined.
132  *
133  * This annotation instructs the compiler to ignore its inlining
134  * heuristics and inline the function no matter how big it thinks it
135  * becomes.
136  */
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")
143 #endif
144
145 /**
146  * \def __no_inline
147  * \brief The function should never be inlined
148  *
149  * This annotation instructs the compiler to ignore its inlining
150  * heuristics and not inline the function no matter how small it thinks it
151  * becomes.
152  */
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")
159 #endif
160
161
162 /** \brief This macro is used to test fatal errors.
163  *
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.
168  *
169  * \param[in] expr  Expression to evaluate and supposed to be nonzero.
170  */
171 #if defined(_ASSERT_ENABLE_)
172 #  if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
173 #    include "unit_test/suite.h"
174 #  else
175 #    undef TEST_SUITE_DEFINE_ASSERT_MACRO
176 #    define Assert(expr) \
177         {\
178            if (!(expr)) asm("BKPT #0");\
179         }
180 #  endif
181 #else
182 #  define Assert(expr) ((void) 0)
183 #endif
184
185 /* Define WEAK attribute */
186 #if defined   ( __CC_ARM   )
187 #   define WEAK __attribute__ ((weak))
188 #elif defined ( __ICCARM__ )
189 #   define WEAK __weak
190 #elif defined (  __GNUC__  )
191 #   define WEAK __attribute__ ((weak))
192 #endif
193
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")))
201 #endif
202
203 //#include "interrupt.h"
204
205 /** \name Usual Types
206  * @{ */
207 #ifndef __cplusplus
208 #  if !defined(__bool_true_false_are_defined)
209 typedef unsigned char           bool;
210 #  endif
211 #endif
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;
217 /** @} */
218
219 /** \name Aliasing Aggregate Types
220  * @{ */
221
222 /** 16-bit union. */
223 typedef union
224 {
225   int16_t  s16;
226   uint16_t u16;
227   int8_t   s8[2];
228   uint8_t  u8[2];
229 } Union16;
230
231 /** 32-bit union. */
232 typedef union
233 {
234   int32_t  s32;
235   uint32_t u32;
236   int16_t  s16[2];
237   uint16_t u16[2];
238   int8_t   s8[4];
239   uint8_t  u8[4];
240 } Union32;
241
242 /** 64-bit union. */
243 typedef union
244 {
245   int64_t  s64;
246   uint64_t u64;
247   int32_t  s32[2];
248   uint32_t u32[2];
249   int16_t  s16[4];
250   uint16_t u16[4];
251   int8_t   s8[8];
252   uint8_t  u8[8];
253 } Union64;
254
255 /** Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
256 typedef union
257 {
258   int64_t  *s64ptr;
259   uint64_t *u64ptr;
260   int32_t  *s32ptr;
261   uint32_t *u32ptr;
262   int16_t  *s16ptr;
263   uint16_t *u16ptr;
264   int8_t   *s8ptr;
265   uint8_t  *u8ptr;
266 } UnionPtr;
267
268 /** Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
269 typedef union
270 {
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;
279 } UnionVPtr;
280
281 /** Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
282 typedef union
283 {
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;
290   const int8_t   *s8ptr;
291   const uint8_t  *u8ptr;
292 } UnionCPtr;
293
294 /** Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
295 typedef union
296 {
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;
305 } UnionCVPtr;
306
307 /** Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
308 typedef struct
309 {
310   int64_t  *s64ptr;
311   uint64_t *u64ptr;
312   int32_t  *s32ptr;
313   uint32_t *u32ptr;
314   int16_t  *s16ptr;
315   uint16_t *u16ptr;
316   int8_t   *s8ptr;
317   uint8_t  *u8ptr;
318 } StructPtr;
319
320 /** Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
321 typedef struct
322 {
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;
331 } StructVPtr;
332
333 /** Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
334 typedef struct
335 {
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;
342   const int8_t   *s8ptr;
343   const uint8_t  *u8ptr;
344 } StructCPtr;
345
346 /** Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
347 typedef struct
348 {
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;
357 } StructCVPtr;
358
359 /** @} */
360
361 #endif  /* #ifndef __ASSEMBLY__ */
362
363 /** \name Usual Constants
364  * @{ */
365 //kmod #define DISABLE   0
366 //kmod #define ENABLE    1
367
368 #ifndef __cplusplus
369 #  if !defined(__bool_true_false_are_defined)
370 #    define false     0
371 #    define true      1
372 #  endif
373 #endif
374 /** @} */
375
376 #ifndef __ASSEMBLY__
377
378 /** \name Optimization Control
379  * @{ */
380
381 /**
382  * \def likely(exp)
383  * \brief The expression \a exp is likely to be true
384  */
385 #if !defined(likely) || defined(__DOXYGEN__)
386 #   define likely(exp)    (exp)
387 #endif
388
389 /**
390  * \def unlikely(exp)
391  * \brief The expression \a exp is unlikely to be true
392  */
393 #if !defined(unlikely) || defined(__DOXYGEN__)
394 #   define unlikely(exp)  (exp)
395 #endif
396
397 /**
398  * \def is_constant(exp)
399  * \brief Determine if an expression evaluates to a constant value.
400  *
401  * \param[in] exp Any expression
402  *
403  * \return true if \a exp is constant, false otherwise.
404  */
405 #if (defined __GNUC__) || (defined __CC_ARM)
406 #   define is_constant(exp)       __builtin_constant_p(exp)
407 #else
408 #   define is_constant(exp)       (0)
409 #endif
410
411 /** @} */
412
413 /** \name Bit-Field Handling
414  * @{ */
415
416 /** \brief Reads the bits of a value specified by a given bit-mask.
417  *
418  * \param[in] value Value to read bits from.
419  * \param[in] mask  Bit-mask indicating bits to read.
420  *
421  * \return Read bits.
422  */
423 #define Rd_bits( value, mask)        ((value) & (mask))
424
425 /** \brief Writes the bits of a C lvalue specified by a given bit-mask.
426  *
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.
430  *
431  * \return Resulting value with written bits.
432  */
433 #define Wr_bits(lvalue, mask, bits)  ((lvalue) = ((lvalue) & ~(mask)) |\
434                                                  ((bits  ) &  (mask)))
435
436 /** \brief Tests the bits of a value specified by a given bit-mask.
437  *
438  * \param[in] value Value of which to test bits.
439  * \param[in] mask  Bit-mask indicating bits to test.
440  *
441  * \return \c 1 if at least one of the tested bits is set, else \c 0.
442  */
443 #define Tst_bits( value, mask)  (Rd_bits(value, mask) != 0)
444
445 /** \brief Clears the bits of a C lvalue specified by a given bit-mask.
446  *
447  * \param[in] lvalue  C lvalue of which to clear bits.
448  * \param[in] mask    Bit-mask indicating bits to clear.
449  *
450  * \return Resulting value with cleared bits.
451  */
452 #define Clr_bits(lvalue, mask)  ((lvalue) &= ~(mask))
453
454 /** \brief Sets the bits of a C lvalue specified by a given bit-mask.
455  *
456  * \param[in] lvalue  C lvalue of which to set bits.
457  * \param[in] mask    Bit-mask indicating bits to set.
458  *
459  * \return Resulting value with set bits.
460  */
461 #define Set_bits(lvalue, mask)  ((lvalue) |=  (mask))
462
463 /** \brief Toggles the bits of a C lvalue specified by a given bit-mask.
464  *
465  * \param[in] lvalue  C lvalue of which to toggle bits.
466  * \param[in] mask    Bit-mask indicating bits to toggle.
467  *
468  * \return Resulting value with toggled bits.
469  */
470 #define Tgl_bits(lvalue, mask)  ((lvalue) ^=  (mask))
471
472 /** \brief Reads the bit-field of a value specified by a given bit-mask.
473  *
474  * \param[in] value Value to read a bit-field from.
475  * \param[in] mask  Bit-mask indicating the bit-field to read.
476  *
477  * \return Read bit-field.
478  */
479 #define Rd_bitfield( value, mask)           (Rd_bits( value, mask) >> ctz(mask))
480
481 /** \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
482  *
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.
486  *
487  * \return Resulting value with written bit-field.
488  */
489 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask)))
490
491 /** @} */
492
493
494 /** \name Zero-Bit Counting
495  *
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
504  * expressions.
505  *
506  * @{ */
507
508 /** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
509  *
510  * \param[in] u Value of which to count the leading zero bits.
511  *
512  * \return The count of leading zero bits in \a u.
513  */
514 #if (defined __GNUC__) || (defined __CC_ARM)
515 #   define clz(u)              ((u) ? __builtin_clz(u) : 32)
516 #else
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 : \
549                                 31)
550 #endif
551
552 /** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
553  *
554  * \param[in] u Value of which to count the trailing zero bits.
555  *
556  * \return The count of trailing zero bits in \a u.
557  */
558 #if (defined __GNUC__) || (defined __CC_ARM)
559 #   define ctz(u)              ((u) ? __builtin_ctz(u) : 32)
560 #else
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 : \
593                                 32)
594 #endif
595
596 /** @} */
597
598
599 /** \name Bit Reversing
600  * @{ */
601
602 /** \brief Reverses the bits of \a u8.
603  *
604  * \param[in] u8  U8 of which to reverse the bits.
605  *
606  * \return Value resulting from \a u8 with reversed bits.
607  */
608 #define bit_reverse8(u8)    ((U8)(bit_reverse32((U8)(u8)) >> 24))
609
610 /** \brief Reverses the bits of \a u16.
611  *
612  * \param[in] u16 U16 of which to reverse the bits.
613  *
614  * \return Value resulting from \a u16 with reversed bits.
615  */
616 #define bit_reverse16(u16)  ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16))
617
618 /** \brief Reverses the bits of \a u32.
619  *
620  * \param[in] u32 U32 of which to reverse the bits.
621  *
622  * \return Value resulting from \a u32 with reversed bits.
623  */
624 #define bit_reverse32(u32)   __RBIT(u32)
625
626 /** \brief Reverses the bits of \a u64.
627  *
628  * \param[in] u64 U64 of which to reverse the bits.
629  *
630  * \return Value resulting from \a u64 with reversed bits.
631  */
632 #define bit_reverse64(u64)  ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) |\
633                                    ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32)))
634
635 /** @} */
636
637
638 /** \name Alignment
639  * @{ */
640
641 /** \brief Tests alignment of the number \a val with the \a n boundary.
642  *
643  * \param[in] val Input value.
644  * \param[in] n   Boundary.
645  *
646  * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
647  */
648 #define Test_align(val, n) (!Tst_bits( val, (n) - 1     )   )
649
650 /** \brief Gets alignment of the number \a val with respect to the \a n boundary.
651  *
652  * \param[in] val Input value.
653  * \param[in] n   Boundary.
654  *
655  * \return Alignment of the number \a val with respect to the \a n boundary.
656  */
657 #define Get_align(val, n) (  Rd_bits( val, (n) - 1     )   )
658
659 /** \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
660  *
661  * \param[in] lval  Input/output lvalue.
662  * \param[in] n     Boundary.
663  * \param[in] alg   Alignment.
664  *
665  * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
666  */
667 #define Set_align(lval, n, alg) (  Wr_bits(lval, (n) - 1, alg)   )
668
669 /** \brief Aligns the number \a val with the upper \a n boundary.
670  *
671  * \param[in] val Input value.
672  * \param[in] n   Boundary.
673  *
674  * \return Value resulting from the number \a val aligned with the upper \a n boundary.
675  */
676 #define Align_up(  val, n) (((val) + ((n) - 1)) & ~((n) - 1))
677
678 /** \brief Aligns the number \a val with the lower \a n boundary.
679  *
680  * \param[in] val Input value.
681  * \param[in] n   Boundary.
682  *
683  * \return Value resulting from the number \a val aligned with the lower \a n boundary.
684  */
685 #define Align_down(val, n) ( (val)              & ~((n) - 1))
686
687 /** @} */
688
689
690 /** \name Mathematics
691  *
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
697  *     compile time);
698  *   - abs, min and max to apply to non-constant expressions (values unknown at
699  *     compile time), abs is found in stdlib.h.
700  *
701  * @{ */
702
703 /** \brief Takes the absolute value of \a a.
704  *
705  * \param[in] a Input value.
706  *
707  * \return Absolute value of \a a.
708  *
709  * \note More optimized if only used with values known at compile time.
710  */
711 #define Abs(a)              (((a) <  0 ) ? -(a) : (a))
712
713 #ifndef __cplusplus
714 /** \brief Takes the minimal value of \a a and \a b.
715  *
716  * \param[in] a Input value.
717  * \param[in] b Input value.
718  *
719  * \return Minimal value of \a a and \a b.
720  *
721  * \note More optimized if only used with values known at compile time.
722  */
723 #define Min(a, b)           (((a) < (b)) ?  (a) : (b))
724
725 /** \brief Takes the maximal value of \a a and \a b.
726  *
727  * \param[in] a Input value.
728  * \param[in] b Input value.
729  *
730  * \return Maximal value of \a a and \a b.
731  *
732  * \note More optimized if only used with values known at compile time.
733  */
734 #define Max(a, b)           (((a) > (b)) ?  (a) : (b))
735
736 /** \brief Takes the minimal value of \a a and \a b.
737  *
738  * \param[in] a Input value.
739  * \param[in] b Input value.
740  *
741  * \return Minimal value of \a a and \a b.
742  *
743  * \note More optimized if only used with values unknown at compile time.
744  */
745 #define min(a, b)   Min(a, b)
746
747 /** \brief Takes the maximal value of \a a and \a b.
748  *
749  * \param[in] a Input value.
750  * \param[in] b Input value.
751  *
752  * \return Maximal value of \a a and \a b.
753  *
754  * \note More optimized if only used with values unknown at compile time.
755  */
756 #define max(a, b)   Max(a, b)
757 #endif
758
759 /** @} */
760
761
762 /** \brief Calls the routine at address \a addr.
763  *
764  * It generates a long call opcode.
765  *
766  * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
767  * it is invoked from the CPU supervisor mode.
768  *
769  * \param[in] addr  Address of the routine to call.
770  *
771  * \note It may be used as a long jump opcode in some special cases.
772  */
773 #define Long_call(addr)                   ((*(void (*)(void))(addr))())
774
775
776 /** \name MCU Endianism Handling
777  *  ARM is MCU little endian.
778  *
779  * @{ */
780 #define  BE16(x)        swap16(x)
781 #define  LE16(x)        (x)
782
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)
787
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)
792
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)
797
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)
802 /** @} */
803
804
805 /** \name Endianism Conversion
806  *
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
811  *     at compile time);
812  *   - swap16, swap32 and swap64 to apply to non-constant expressions (values
813  *     unknown at compile time).
814  *
815  * @{ */
816
817 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
818  *
819  * \param[in] u16 U16 of which to toggle the endianism.
820  *
821  * \return Value resulting from \a u16 with toggled endianism.
822  *
823  * \note More optimized if only used with values known at compile time.
824  */
825 #define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) |\
826                            ((uint16_t)(u16) << 8)))
827
828 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
829  *
830  * \param[in] u32 U32 of which to toggle the endianism.
831  *
832  * \return Value resulting from \a u32 with toggled endianism.
833  *
834  * \note More optimized if only used with values known at compile time.
835  */
836 #define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) |\
837                            ((uint32_t)Swap16((uint32_t)(u32)) << 16)))
838
839 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
840  *
841  * \param[in] u64 U64 of which to toggle the endianism.
842  *
843  * \return Value resulting from \a u64 with toggled endianism.
844  *
845  * \note More optimized if only used with values known at compile time.
846  */
847 #define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) |\
848                            ((uint64_t)Swap32((uint64_t)(u64)) << 32)))
849
850 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
851  *
852  * \param[in] u16 U16 of which to toggle the endianism.
853  *
854  * \return Value resulting from \a u16 with toggled endianism.
855  *
856  * \note More optimized if only used with values unknown at compile time.
857  */
858 #define swap16(u16) Swap16(u16)
859
860 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
861  *
862  * \param[in] u32 U32 of which to toggle the endianism.
863  *
864  * \return Value resulting from \a u32 with toggled endianism.
865  *
866  * \note More optimized if only used with values unknown at compile time.
867  */
868 #if (defined __GNUC__)
869 #  define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32)))
870 #else
871 #  define swap32(u32) Swap32(u32)
872 #endif
873
874 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
875  *
876  * \param[in] u64 U64 of which to toggle the endianism.
877  *
878  * \return Value resulting from \a u64 with toggled endianism.
879  *
880  * \note More optimized if only used with values unknown at compile time.
881  */
882 #if (defined __GNUC__)
883 #  define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64)))
884 #else
885 #  define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) |\
886                          ((uint64_t)swap32((uint64_t)(u64)) << 32)))
887 #endif
888
889 /** @} */
890
891
892 /** \name Target Abstraction
893  *
894  * @{ */
895
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. */
901
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. */
906
907 /** @} */
908
909 /**
910  * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
911  * integer arithmetic.
912  *
913  * \param[in] a An integer
914  * \param[in] b Another integer
915  *
916  * \return (\a a / \a b) rounded up to the nearest integer.
917  */
918 #define div_ceil(a, b)      (((a) + (b) - 1) / (b))
919
920 #endif  /* #ifndef __ASSEMBLY__ */
921 #ifdef __ICCARM__
922 /** \name Compiler Keywords
923  *
924  * Port of some keywords from GCC to IAR Embedded Workbench.
925  *
926  * @{ */
927
928 #define __asm__             asm
929 #define __inline__          inline
930 #define __volatile__
931
932 /** @} */
933
934 #endif
935
936 #define FUNC_PTR                            void *
937 /**
938  * \def unused
939  * \brief Marking \a v as a unused parameter or value.
940  */
941 #define unused(v)          do { (void)(v); } while(0)
942
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")))
950 #endif
951
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")))
959 #endif
960 //kmod #define PASS      0
961 //kmod #define FAIL      1
962 //kmod #define LOW       0
963 //kmod #define HIGH      1
964
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.
975
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.
978
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.
989
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.
1016
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.
1025
1026 #if defined(__ICCARM__)
1027 #define SHORTENUM           __packed
1028 #elif defined(__GNUC__)
1029 #define SHORTENUM           __attribute__((packed))
1030 #endif
1031
1032 /* No operation */
1033 #if defined(__ICCARM__)
1034 #define nop()               __no_operation()
1035 #elif defined(__GNUC__)
1036 #define nop()               (__NOP())
1037 #endif
1038
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))
1045
1046 /*Defines the Flash Storage for the request and response of MAC*/
1047 #define CMD_ID_OCTET    (0)
1048
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)
1053
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)
1058
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)
1063
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)
1068
1069 #define ADDR_COPY_DST_SRC_16(dst, src)  ((dst) = (src))
1070 #define ADDR_COPY_DST_SRC_64(dst, src)  ((dst) = (src))
1071
1072 /**
1073  * @brief Converts a 64-Bit value into  a 8 Byte array
1074  *
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
1078  */
1079 static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data)
1080 {
1081     uint8_t index = 0;
1082
1083     while (index < 8)
1084     {
1085         data[index++] = value & 0xFF;
1086         value = value >> 8;
1087     }
1088 }
1089
1090 /**
1091  * @brief Converts a 16-Bit value into  a 2 Byte array
1092  *
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
1096  */
1097 static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1098 {
1099     data[0] = value & 0xFF;
1100     data[1] = (value >> 8) & 0xFF;
1101 }
1102
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)
1105 {
1106     data[0] = value & 0xFF;
1107     data[1] = (value >> 8) & 0xFF;
1108 }
1109
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)
1112 {
1113     data[0] = value & 0xFF;
1114     data[1] = (value >> 8) & 0xFF;
1115 }
1116
1117 /*
1118  * @brief Converts a 2 Byte array into a 16-Bit value
1119  *
1120  * @param data Specifies the pointer to the 2 Byte array
1121  *
1122  * @return 16-Bit value
1123  * @ingroup apiPalApi
1124  */
1125 static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data)
1126 {
1127     return (data[0] | ((uint16_t)data[1] << 8));
1128 }
1129
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)
1132 {
1133     union
1134     {
1135         uint32_t u32;
1136         uint8_t u8[4];
1137     } long_addr;
1138
1139     uint8_t index;
1140
1141     for (index = 0; index < 4; index++)
1142     {
1143         long_addr.u8[index] = *data++;
1144     }
1145
1146     return long_addr.u32;
1147 }
1148
1149 /**
1150  * @brief Converts a 8 Byte array into a 64-Bit value
1151  *
1152  * @param data Specifies the pointer to the 8 Byte array
1153  *
1154  * @return 64-Bit value
1155  * @ingroup apiPalApi
1156  */
1157 static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data)
1158 {
1159     union
1160     {
1161         uint64_t u64;
1162         uint8_t u8[8];
1163     } long_addr;
1164
1165     uint8_t index;
1166
1167     for (index = 0; index < 8; index++)
1168     {
1169         long_addr.u8[index] = *data++;
1170     }
1171
1172     return long_addr.u64;
1173 }
1174
1175 /** @} */
1176
1177 #endif /* UTILS_COMPILER_H_INCLUDED */