]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/usb/compiler.h
Fix process_combo which assign -1 to uint16_t (#3697)
[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(__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")
144 #endif
145 #endif
146
147 /**
148  * \def __no_inline
149  * \brief The function should never be inlined
150  *
151  * This annotation instructs the compiler to ignore its inlining
152  * heuristics and not inline the function no matter how small it thinks it
153  * becomes.
154  */
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")
161 #endif
162
163
164 /** \brief This macro is used to test fatal errors.
165  *
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.
170  *
171  * \param[in] expr  Expression to evaluate and supposed to be nonzero.
172  */
173 #if defined(_ASSERT_ENABLE_)
174 #  if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
175 #    include "unit_test/suite.h"
176 #  else
177 #    undef TEST_SUITE_DEFINE_ASSERT_MACRO
178 #    define Assert(expr) \
179         {\
180            if (!(expr)) asm("BKPT #0");\
181         }
182 #  endif
183 #else
184 #  define Assert(expr) ((void) 0)
185 #endif
186
187 /* Define WEAK attribute */
188 #if defined   ( __CC_ARM   )
189 #   define WEAK __attribute__ ((weak))
190 #elif defined ( __ICCARM__ )
191 #   define WEAK __weak
192 #elif defined (  __GNUC__  )
193 #   define WEAK __attribute__ ((weak))
194 #endif
195
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")))
203 #endif
204
205 //#include "interrupt.h"
206
207 /** \name Usual Types
208  * @{ */
209 #ifndef __cplusplus
210 #  if !defined(__bool_true_false_are_defined)
211 typedef unsigned char           bool;
212 #  endif
213 #endif
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;
219 /** @} */
220
221 /** \name Aliasing Aggregate Types
222  * @{ */
223
224 /** 16-bit union. */
225 typedef union
226 {
227   int16_t  s16;
228   uint16_t u16;
229   int8_t   s8[2];
230   uint8_t  u8[2];
231 } Union16;
232
233 /** 32-bit union. */
234 typedef union
235 {
236   int32_t  s32;
237   uint32_t u32;
238   int16_t  s16[2];
239   uint16_t u16[2];
240   int8_t   s8[4];
241   uint8_t  u8[4];
242 } Union32;
243
244 /** 64-bit union. */
245 typedef union
246 {
247   int64_t  s64;
248   uint64_t u64;
249   int32_t  s32[2];
250   uint32_t u32[2];
251   int16_t  s16[4];
252   uint16_t u16[4];
253   int8_t   s8[8];
254   uint8_t  u8[8];
255 } Union64;
256
257 /** Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
258 typedef union
259 {
260   int64_t  *s64ptr;
261   uint64_t *u64ptr;
262   int32_t  *s32ptr;
263   uint32_t *u32ptr;
264   int16_t  *s16ptr;
265   uint16_t *u16ptr;
266   int8_t   *s8ptr;
267   uint8_t  *u8ptr;
268 } UnionPtr;
269
270 /** Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
271 typedef union
272 {
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;
281 } UnionVPtr;
282
283 /** Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
284 typedef union
285 {
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;
292   const int8_t   *s8ptr;
293   const uint8_t  *u8ptr;
294 } UnionCPtr;
295
296 /** Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
297 typedef union
298 {
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;
307 } UnionCVPtr;
308
309 /** Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */
310 typedef struct
311 {
312   int64_t  *s64ptr;
313   uint64_t *u64ptr;
314   int32_t  *s32ptr;
315   uint32_t *u32ptr;
316   int16_t  *s16ptr;
317   uint16_t *u16ptr;
318   int8_t   *s8ptr;
319   uint8_t  *u8ptr;
320 } StructPtr;
321
322 /** Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */
323 typedef struct
324 {
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;
333 } StructVPtr;
334
335 /** Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */
336 typedef struct
337 {
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;
344   const int8_t   *s8ptr;
345   const uint8_t  *u8ptr;
346 } StructCPtr;
347
348 /** Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */
349 typedef struct
350 {
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;
359 } StructCVPtr;
360
361 /** @} */
362
363 #endif  /* #ifndef __ASSEMBLY__ */
364
365 /** \name Usual Constants
366  * @{ */
367 //kmod #define DISABLE   0
368 //kmod #define ENABLE    1
369
370 #ifndef __cplusplus
371 #  if !defined(__bool_true_false_are_defined)
372 #    define false     0
373 #    define true      1
374 #  endif
375 #endif
376 /** @} */
377
378 #ifndef __ASSEMBLY__
379
380 /** \name Optimization Control
381  * @{ */
382
383 /**
384  * \def likely(exp)
385  * \brief The expression \a exp is likely to be true
386  */
387 #if !defined(likely) || defined(__DOXYGEN__)
388 #   define likely(exp)    (exp)
389 #endif
390
391 /**
392  * \def unlikely(exp)
393  * \brief The expression \a exp is unlikely to be true
394  */
395 #if !defined(unlikely) || defined(__DOXYGEN__)
396 #   define unlikely(exp)  (exp)
397 #endif
398
399 /**
400  * \def is_constant(exp)
401  * \brief Determine if an expression evaluates to a constant value.
402  *
403  * \param[in] exp Any expression
404  *
405  * \return true if \a exp is constant, false otherwise.
406  */
407 #if (defined __GNUC__) || (defined __CC_ARM)
408 #   define is_constant(exp)       __builtin_constant_p(exp)
409 #else
410 #   define is_constant(exp)       (0)
411 #endif
412
413 /** @} */
414
415 /** \name Bit-Field Handling
416  * @{ */
417
418 /** \brief Reads the bits of a value specified by a given bit-mask.
419  *
420  * \param[in] value Value to read bits from.
421  * \param[in] mask  Bit-mask indicating bits to read.
422  *
423  * \return Read bits.
424  */
425 #define Rd_bits( value, mask)        ((value) & (mask))
426
427 /** \brief Writes the bits of a C lvalue specified by a given bit-mask.
428  *
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.
432  *
433  * \return Resulting value with written bits.
434  */
435 #define Wr_bits(lvalue, mask, bits)  ((lvalue) = ((lvalue) & ~(mask)) |\
436                                                  ((bits  ) &  (mask)))
437
438 /** \brief Tests the bits of a value specified by a given bit-mask.
439  *
440  * \param[in] value Value of which to test bits.
441  * \param[in] mask  Bit-mask indicating bits to test.
442  *
443  * \return \c 1 if at least one of the tested bits is set, else \c 0.
444  */
445 #define Tst_bits( value, mask)  (Rd_bits(value, mask) != 0)
446
447 /** \brief Clears the bits of a C lvalue specified by a given bit-mask.
448  *
449  * \param[in] lvalue  C lvalue of which to clear bits.
450  * \param[in] mask    Bit-mask indicating bits to clear.
451  *
452  * \return Resulting value with cleared bits.
453  */
454 #define Clr_bits(lvalue, mask)  ((lvalue) &= ~(mask))
455
456 /** \brief Sets the bits of a C lvalue specified by a given bit-mask.
457  *
458  * \param[in] lvalue  C lvalue of which to set bits.
459  * \param[in] mask    Bit-mask indicating bits to set.
460  *
461  * \return Resulting value with set bits.
462  */
463 #define Set_bits(lvalue, mask)  ((lvalue) |=  (mask))
464
465 /** \brief Toggles the bits of a C lvalue specified by a given bit-mask.
466  *
467  * \param[in] lvalue  C lvalue of which to toggle bits.
468  * \param[in] mask    Bit-mask indicating bits to toggle.
469  *
470  * \return Resulting value with toggled bits.
471  */
472 #define Tgl_bits(lvalue, mask)  ((lvalue) ^=  (mask))
473
474 /** \brief Reads the bit-field of a value specified by a given bit-mask.
475  *
476  * \param[in] value Value to read a bit-field from.
477  * \param[in] mask  Bit-mask indicating the bit-field to read.
478  *
479  * \return Read bit-field.
480  */
481 #define Rd_bitfield( value, mask)           (Rd_bits( value, mask) >> ctz(mask))
482
483 /** \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
484  *
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.
488  *
489  * \return Resulting value with written bit-field.
490  */
491 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask)))
492
493 /** @} */
494
495
496 /** \name Zero-Bit Counting
497  *
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
506  * expressions.
507  *
508  * @{ */
509
510 /** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
511  *
512  * \param[in] u Value of which to count the leading zero bits.
513  *
514  * \return The count of leading zero bits in \a u.
515  */
516 #if (defined __GNUC__) || (defined __CC_ARM)
517 #   define clz(u)              ((u) ? __builtin_clz(u) : 32)
518 #else
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 : \
551                                 31)
552 #endif
553
554 /** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
555  *
556  * \param[in] u Value of which to count the trailing zero bits.
557  *
558  * \return The count of trailing zero bits in \a u.
559  */
560 #if (defined __GNUC__) || (defined __CC_ARM)
561 #   define ctz(u)              ((u) ? __builtin_ctz(u) : 32)
562 #else
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 : \
595                                 32)
596 #endif
597
598 /** @} */
599
600
601 /** \name Bit Reversing
602  * @{ */
603
604 /** \brief Reverses the bits of \a u8.
605  *
606  * \param[in] u8  U8 of which to reverse the bits.
607  *
608  * \return Value resulting from \a u8 with reversed bits.
609  */
610 #define bit_reverse8(u8)    ((U8)(bit_reverse32((U8)(u8)) >> 24))
611
612 /** \brief Reverses the bits of \a u16.
613  *
614  * \param[in] u16 U16 of which to reverse the bits.
615  *
616  * \return Value resulting from \a u16 with reversed bits.
617  */
618 #define bit_reverse16(u16)  ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16))
619
620 /** \brief Reverses the bits of \a u32.
621  *
622  * \param[in] u32 U32 of which to reverse the bits.
623  *
624  * \return Value resulting from \a u32 with reversed bits.
625  */
626 #define bit_reverse32(u32)   __RBIT(u32)
627
628 /** \brief Reverses the bits of \a u64.
629  *
630  * \param[in] u64 U64 of which to reverse the bits.
631  *
632  * \return Value resulting from \a u64 with reversed bits.
633  */
634 #define bit_reverse64(u64)  ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) |\
635                                    ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32)))
636
637 /** @} */
638
639
640 /** \name Alignment
641  * @{ */
642
643 /** \brief Tests alignment of the number \a val with the \a n boundary.
644  *
645  * \param[in] val Input value.
646  * \param[in] n   Boundary.
647  *
648  * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
649  */
650 #define Test_align(val, n) (!Tst_bits( val, (n) - 1     )   )
651
652 /** \brief Gets alignment of the number \a val with respect to the \a n boundary.
653  *
654  * \param[in] val Input value.
655  * \param[in] n   Boundary.
656  *
657  * \return Alignment of the number \a val with respect to the \a n boundary.
658  */
659 #define Get_align(val, n) (  Rd_bits( val, (n) - 1     )   )
660
661 /** \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
662  *
663  * \param[in] lval  Input/output lvalue.
664  * \param[in] n     Boundary.
665  * \param[in] alg   Alignment.
666  *
667  * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
668  */
669 #define Set_align(lval, n, alg) (  Wr_bits(lval, (n) - 1, alg)   )
670
671 /** \brief Aligns the number \a val with the upper \a n boundary.
672  *
673  * \param[in] val Input value.
674  * \param[in] n   Boundary.
675  *
676  * \return Value resulting from the number \a val aligned with the upper \a n boundary.
677  */
678 #define Align_up(  val, n) (((val) + ((n) - 1)) & ~((n) - 1))
679
680 /** \brief Aligns the number \a val with the lower \a n boundary.
681  *
682  * \param[in] val Input value.
683  * \param[in] n   Boundary.
684  *
685  * \return Value resulting from the number \a val aligned with the lower \a n boundary.
686  */
687 #define Align_down(val, n) ( (val)              & ~((n) - 1))
688
689 /** @} */
690
691
692 /** \name Mathematics
693  *
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
699  *     compile time);
700  *   - abs, min and max to apply to non-constant expressions (values unknown at
701  *     compile time), abs is found in stdlib.h.
702  *
703  * @{ */
704
705 /** \brief Takes the absolute value of \a a.
706  *
707  * \param[in] a Input value.
708  *
709  * \return Absolute value of \a a.
710  *
711  * \note More optimized if only used with values known at compile time.
712  */
713 #define Abs(a)              (((a) <  0 ) ? -(a) : (a))
714
715 #ifndef __cplusplus
716 /** \brief Takes the minimal value of \a a and \a b.
717  *
718  * \param[in] a Input value.
719  * \param[in] b Input value.
720  *
721  * \return Minimal value of \a a and \a b.
722  *
723  * \note More optimized if only used with values known at compile time.
724  */
725 #define Min(a, b)           (((a) < (b)) ?  (a) : (b))
726
727 /** \brief Takes the maximal value of \a a and \a b.
728  *
729  * \param[in] a Input value.
730  * \param[in] b Input value.
731  *
732  * \return Maximal value of \a a and \a b.
733  *
734  * \note More optimized if only used with values known at compile time.
735  */
736 #define Max(a, b)           (((a) > (b)) ?  (a) : (b))
737
738 /** \brief Takes the minimal value of \a a and \a b.
739  *
740  * \param[in] a Input value.
741  * \param[in] b Input value.
742  *
743  * \return Minimal value of \a a and \a b.
744  *
745  * \note More optimized if only used with values unknown at compile time.
746  */
747 #define min(a, b)   Min(a, b)
748
749 /** \brief Takes the maximal value of \a a and \a b.
750  *
751  * \param[in] a Input value.
752  * \param[in] b Input value.
753  *
754  * \return Maximal value of \a a and \a b.
755  *
756  * \note More optimized if only used with values unknown at compile time.
757  */
758 #define max(a, b)   Max(a, b)
759 #endif
760
761 /** @} */
762
763
764 /** \brief Calls the routine at address \a addr.
765  *
766  * It generates a long call opcode.
767  *
768  * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
769  * it is invoked from the CPU supervisor mode.
770  *
771  * \param[in] addr  Address of the routine to call.
772  *
773  * \note It may be used as a long jump opcode in some special cases.
774  */
775 #define Long_call(addr)                   ((*(void (*)(void))(addr))())
776
777
778 /** \name MCU Endianism Handling
779  *  ARM is MCU little endian.
780  *
781  * @{ */
782 #define  BE16(x)        swap16(x)
783 #define  LE16(x)        (x)
784
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)
789
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)
794
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)
799
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)
804 /** @} */
805
806
807 /** \name Endianism Conversion
808  *
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
813  *     at compile time);
814  *   - swap16, swap32 and swap64 to apply to non-constant expressions (values
815  *     unknown at compile time).
816  *
817  * @{ */
818
819 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
820  *
821  * \param[in] u16 U16 of which to toggle the endianism.
822  *
823  * \return Value resulting from \a u16 with toggled endianism.
824  *
825  * \note More optimized if only used with values known at compile time.
826  */
827 #define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) |\
828                            ((uint16_t)(u16) << 8)))
829
830 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
831  *
832  * \param[in] u32 U32 of which to toggle the endianism.
833  *
834  * \return Value resulting from \a u32 with toggled endianism.
835  *
836  * \note More optimized if only used with values known at compile time.
837  */
838 #define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) |\
839                            ((uint32_t)Swap16((uint32_t)(u32)) << 16)))
840
841 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
842  *
843  * \param[in] u64 U64 of which to toggle the endianism.
844  *
845  * \return Value resulting from \a u64 with toggled endianism.
846  *
847  * \note More optimized if only used with values known at compile time.
848  */
849 #define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) |\
850                            ((uint64_t)Swap32((uint64_t)(u64)) << 32)))
851
852 /** \brief Toggles the endianism of \a u16 (by swapping its bytes).
853  *
854  * \param[in] u16 U16 of which to toggle the endianism.
855  *
856  * \return Value resulting from \a u16 with toggled endianism.
857  *
858  * \note More optimized if only used with values unknown at compile time.
859  */
860 #define swap16(u16) Swap16(u16)
861
862 /** \brief Toggles the endianism of \a u32 (by swapping its bytes).
863  *
864  * \param[in] u32 U32 of which to toggle the endianism.
865  *
866  * \return Value resulting from \a u32 with toggled endianism.
867  *
868  * \note More optimized if only used with values unknown at compile time.
869  */
870 #if (defined __GNUC__)
871 #  define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32)))
872 #else
873 #  define swap32(u32) Swap32(u32)
874 #endif
875
876 /** \brief Toggles the endianism of \a u64 (by swapping its bytes).
877  *
878  * \param[in] u64 U64 of which to toggle the endianism.
879  *
880  * \return Value resulting from \a u64 with toggled endianism.
881  *
882  * \note More optimized if only used with values unknown at compile time.
883  */
884 #if (defined __GNUC__)
885 #  define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64)))
886 #else
887 #  define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) |\
888                          ((uint64_t)swap32((uint64_t)(u64)) << 32)))
889 #endif
890
891 /** @} */
892
893
894 /** \name Target Abstraction
895  *
896  * @{ */
897
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. */
903
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. */
908
909 /** @} */
910
911 /**
912  * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
913  * integer arithmetic.
914  *
915  * \param[in] a An integer
916  * \param[in] b Another integer
917  *
918  * \return (\a a / \a b) rounded up to the nearest integer.
919  */
920 #define div_ceil(a, b)      (((a) + (b) - 1) / (b))
921
922 #endif  /* #ifndef __ASSEMBLY__ */
923 #ifdef __ICCARM__
924 /** \name Compiler Keywords
925  *
926  * Port of some keywords from GCC to IAR Embedded Workbench.
927  *
928  * @{ */
929
930 #define __asm__             asm
931 #define __inline__          inline
932 #define __volatile__
933
934 /** @} */
935
936 #endif
937
938 #define FUNC_PTR                            void *
939 /**
940  * \def unused
941  * \brief Marking \a v as a unused parameter or value.
942  */
943 #define unused(v)          do { (void)(v); } while(0)
944
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")))
952 #endif
953
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")))
961 #endif
962 //kmod #define PASS      0
963 //kmod #define FAIL      1
964 //kmod #define LOW       0
965 //kmod #define HIGH      1
966
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.
977
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.
980
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.
991
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.
1018
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.
1027
1028 #if defined(__ICCARM__)
1029 #define SHORTENUM           __packed
1030 #elif defined(__GNUC__)
1031 #define SHORTENUM           __attribute__((packed))
1032 #endif
1033
1034 /* No operation */
1035 #if defined(__ICCARM__)
1036 #define nop()               __no_operation()
1037 #elif defined(__GNUC__)
1038 #define nop()               (__NOP())
1039 #endif
1040
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))
1047
1048 /*Defines the Flash Storage for the request and response of MAC*/
1049 #define CMD_ID_OCTET    (0)
1050
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)
1055
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)
1060
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)
1065
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)
1070
1071 #define ADDR_COPY_DST_SRC_16(dst, src)  ((dst) = (src))
1072 #define ADDR_COPY_DST_SRC_64(dst, src)  ((dst) = (src))
1073
1074 /**
1075  * @brief Converts a 64-Bit value into  a 8 Byte array
1076  *
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
1080  */
1081 static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data)
1082 {
1083     uint8_t index = 0;
1084
1085     while (index < 8)
1086     {
1087         data[index++] = value & 0xFF;
1088         value = value >> 8;
1089     }
1090 }
1091
1092 /**
1093  * @brief Converts a 16-Bit value into  a 2 Byte array
1094  *
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
1098  */
1099 static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1100 {
1101     data[0] = value & 0xFF;
1102     data[1] = (value >> 8) & 0xFF;
1103 }
1104
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)
1107 {
1108     data[0] = value & 0xFF;
1109     data[1] = (value >> 8) & 0xFF;
1110 }
1111
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)
1114 {
1115     data[0] = value & 0xFF;
1116     data[1] = (value >> 8) & 0xFF;
1117 }
1118
1119 /*
1120  * @brief Converts a 2 Byte array into a 16-Bit value
1121  *
1122  * @param data Specifies the pointer to the 2 Byte array
1123  *
1124  * @return 16-Bit value
1125  * @ingroup apiPalApi
1126  */
1127 static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data)
1128 {
1129     return (data[0] | ((uint16_t)data[1] << 8));
1130 }
1131
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)
1134 {
1135     union
1136     {
1137         uint32_t u32;
1138         uint8_t u8[4];
1139     } long_addr;
1140
1141     uint8_t index;
1142
1143     for (index = 0; index < 4; index++)
1144     {
1145         long_addr.u8[index] = *data++;
1146     }
1147
1148     return long_addr.u32;
1149 }
1150
1151 /**
1152  * @brief Converts a 8 Byte array into a 64-Bit value
1153  *
1154  * @param data Specifies the pointer to the 8 Byte array
1155  *
1156  * @return 64-Bit value
1157  * @ingroup apiPalApi
1158  */
1159 static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data)
1160 {
1161     union
1162     {
1163         uint64_t u64;
1164         uint8_t u8[8];
1165     } long_addr;
1166
1167     uint8_t index;
1168
1169     for (index = 0; index < 8; index++)
1170     {
1171         long_addr.u8[index] = *data++;
1172     }
1173
1174     return long_addr.u64;
1175 }
1176
1177 /** @} */
1178
1179 #endif /* UTILS_COMPILER_H_INCLUDED */