1*1b2596b5SMatthias Ringwald /**
2*1b2596b5SMatthias Ringwald * \file
3*1b2596b5SMatthias Ringwald *
4*1b2596b5SMatthias Ringwald * \brief Commonly used includes, types and macros.
5*1b2596b5SMatthias Ringwald *
6*1b2596b5SMatthias Ringwald * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
7*1b2596b5SMatthias Ringwald *
8*1b2596b5SMatthias Ringwald * \asf_license_start
9*1b2596b5SMatthias Ringwald *
10*1b2596b5SMatthias Ringwald * \page License
11*1b2596b5SMatthias Ringwald *
12*1b2596b5SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
13*1b2596b5SMatthias Ringwald * modification, are permitted provided that the following conditions are met:
14*1b2596b5SMatthias Ringwald *
15*1b2596b5SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright notice,
16*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer.
17*1b2596b5SMatthias Ringwald *
18*1b2596b5SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright notice,
19*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer in the documentation
20*1b2596b5SMatthias Ringwald * and/or other materials provided with the distribution.
21*1b2596b5SMatthias Ringwald *
22*1b2596b5SMatthias Ringwald * 3. The name of Atmel may not be used to endorse or promote products derived
23*1b2596b5SMatthias Ringwald * from this software without specific prior written permission.
24*1b2596b5SMatthias Ringwald *
25*1b2596b5SMatthias Ringwald * 4. This software may only be redistributed and used in connection with an
26*1b2596b5SMatthias Ringwald * Atmel microcontroller product.
27*1b2596b5SMatthias Ringwald *
28*1b2596b5SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29*1b2596b5SMatthias Ringwald * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30*1b2596b5SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31*1b2596b5SMatthias Ringwald * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32*1b2596b5SMatthias Ringwald * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33*1b2596b5SMatthias Ringwald * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34*1b2596b5SMatthias Ringwald * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35*1b2596b5SMatthias Ringwald * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36*1b2596b5SMatthias Ringwald * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37*1b2596b5SMatthias Ringwald * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38*1b2596b5SMatthias Ringwald * POSSIBILITY OF SUCH DAMAGE.
39*1b2596b5SMatthias Ringwald *
40*1b2596b5SMatthias Ringwald * \asf_license_stop
41*1b2596b5SMatthias Ringwald *
42*1b2596b5SMatthias Ringwald */
43*1b2596b5SMatthias Ringwald /*
44*1b2596b5SMatthias Ringwald * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45*1b2596b5SMatthias Ringwald */
46*1b2596b5SMatthias Ringwald
47*1b2596b5SMatthias Ringwald #ifndef UTILS_COMPILER_H
48*1b2596b5SMatthias Ringwald #define UTILS_COMPILER_H
49*1b2596b5SMatthias Ringwald
50*1b2596b5SMatthias Ringwald /**
51*1b2596b5SMatthias Ringwald * \defgroup group_sam_utils Compiler abstraction layer and code utilities
52*1b2596b5SMatthias Ringwald *
53*1b2596b5SMatthias Ringwald * Compiler abstraction layer and code utilities for AT91SAM.
54*1b2596b5SMatthias Ringwald * This module provides various abstraction layers and utilities to make code compatible between different compilers.
55*1b2596b5SMatthias Ringwald *
56*1b2596b5SMatthias Ringwald * \{
57*1b2596b5SMatthias Ringwald */
58*1b2596b5SMatthias Ringwald #include <stddef.h>
59*1b2596b5SMatthias Ringwald
60*1b2596b5SMatthias Ringwald #if (defined __ICCARM__)
61*1b2596b5SMatthias Ringwald # include <intrinsics.h>
62*1b2596b5SMatthias Ringwald #endif
63*1b2596b5SMatthias Ringwald
64*1b2596b5SMatthias Ringwald #include <parts.h>
65*1b2596b5SMatthias Ringwald #include "preprocessor.h"
66*1b2596b5SMatthias Ringwald
67*1b2596b5SMatthias Ringwald #include <io.h>
68*1b2596b5SMatthias Ringwald
69*1b2596b5SMatthias Ringwald //_____ D E C L A R A T I O N S ____________________________________________
70*1b2596b5SMatthias Ringwald
71*1b2596b5SMatthias Ringwald #ifndef __ASSEMBLY__ // Not defined for assembling.
72*1b2596b5SMatthias Ringwald
73*1b2596b5SMatthias Ringwald #include <stdio.h>
74*1b2596b5SMatthias Ringwald #include <stdbool.h>
75*1b2596b5SMatthias Ringwald #include <stdint.h>
76*1b2596b5SMatthias Ringwald #include <stdlib.h>
77*1b2596b5SMatthias Ringwald
78*1b2596b5SMatthias Ringwald #ifdef __ICCARM__
79*1b2596b5SMatthias Ringwald /*! \name Compiler Keywords
80*1b2596b5SMatthias Ringwald *
81*1b2596b5SMatthias Ringwald * Port of some keywords from GCC to IAR Embedded Workbench.
82*1b2596b5SMatthias Ringwald */
83*1b2596b5SMatthias Ringwald //! @{
84*1b2596b5SMatthias Ringwald #define __asm__ asm
85*1b2596b5SMatthias Ringwald #define __inline__ inline
86*1b2596b5SMatthias Ringwald #define __volatile__
87*1b2596b5SMatthias Ringwald //! @}
88*1b2596b5SMatthias Ringwald
89*1b2596b5SMatthias Ringwald #endif
90*1b2596b5SMatthias Ringwald
91*1b2596b5SMatthias Ringwald #define FUNC_PTR void *
92*1b2596b5SMatthias Ringwald /**
93*1b2596b5SMatthias Ringwald * \def UNUSED
94*1b2596b5SMatthias Ringwald * \brief Marking \a v as a unused parameter or value.
95*1b2596b5SMatthias Ringwald */
96*1b2596b5SMatthias Ringwald #define UNUSED(v) (void)(v)
97*1b2596b5SMatthias Ringwald
98*1b2596b5SMatthias Ringwald /**
99*1b2596b5SMatthias Ringwald * \def unused
100*1b2596b5SMatthias Ringwald * \brief Marking \a v as a unused parameter or value.
101*1b2596b5SMatthias Ringwald */
102*1b2596b5SMatthias Ringwald #define unused(v) do { (void)(v); } while(0)
103*1b2596b5SMatthias Ringwald
104*1b2596b5SMatthias Ringwald /**
105*1b2596b5SMatthias Ringwald * \def barrier
106*1b2596b5SMatthias Ringwald * \brief Memory barrier
107*1b2596b5SMatthias Ringwald */
108*1b2596b5SMatthias Ringwald #define barrier() __DMB()
109*1b2596b5SMatthias Ringwald
110*1b2596b5SMatthias Ringwald /**
111*1b2596b5SMatthias Ringwald * \brief Emit the compiler pragma \a arg.
112*1b2596b5SMatthias Ringwald *
113*1b2596b5SMatthias Ringwald * \param arg The pragma directive as it would appear after \e \#pragma
114*1b2596b5SMatthias Ringwald * (i.e. not stringified).
115*1b2596b5SMatthias Ringwald */
116*1b2596b5SMatthias Ringwald #define COMPILER_PRAGMA(arg) _Pragma(#arg)
117*1b2596b5SMatthias Ringwald
118*1b2596b5SMatthias Ringwald /**
119*1b2596b5SMatthias Ringwald * \def COMPILER_PACK_SET(alignment)
120*1b2596b5SMatthias Ringwald * \brief Set maximum alignment for subsequent struct and union
121*1b2596b5SMatthias Ringwald * definitions to \a alignment.
122*1b2596b5SMatthias Ringwald */
123*1b2596b5SMatthias Ringwald #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
124*1b2596b5SMatthias Ringwald
125*1b2596b5SMatthias Ringwald /**
126*1b2596b5SMatthias Ringwald * \def COMPILER_PACK_RESET()
127*1b2596b5SMatthias Ringwald * \brief Set default alignment for subsequent struct and union
128*1b2596b5SMatthias Ringwald * definitions.
129*1b2596b5SMatthias Ringwald */
130*1b2596b5SMatthias Ringwald #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
131*1b2596b5SMatthias Ringwald
132*1b2596b5SMatthias Ringwald
133*1b2596b5SMatthias Ringwald /**
134*1b2596b5SMatthias Ringwald * \brief Set aligned boundary.
135*1b2596b5SMatthias Ringwald */
136*1b2596b5SMatthias Ringwald #if (defined __GNUC__) || (defined __CC_ARM)
137*1b2596b5SMatthias Ringwald # define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
138*1b2596b5SMatthias Ringwald #elif (defined __ICCARM__)
139*1b2596b5SMatthias Ringwald # define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
140*1b2596b5SMatthias Ringwald #endif
141*1b2596b5SMatthias Ringwald
142*1b2596b5SMatthias Ringwald /**
143*1b2596b5SMatthias Ringwald * \brief Set word-aligned boundary.
144*1b2596b5SMatthias Ringwald */
145*1b2596b5SMatthias Ringwald #if (defined __GNUC__) || defined(__CC_ARM)
146*1b2596b5SMatthias Ringwald #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4)))
147*1b2596b5SMatthias Ringwald #elif (defined __ICCARM__)
148*1b2596b5SMatthias Ringwald #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4)
149*1b2596b5SMatthias Ringwald #endif
150*1b2596b5SMatthias Ringwald
151*1b2596b5SMatthias Ringwald /**
152*1b2596b5SMatthias Ringwald * \def __always_inline
153*1b2596b5SMatthias Ringwald * \brief The function should always be inlined.
154*1b2596b5SMatthias Ringwald *
155*1b2596b5SMatthias Ringwald * This annotation instructs the compiler to ignore its inlining
156*1b2596b5SMatthias Ringwald * heuristics and inline the function no matter how big it thinks it
157*1b2596b5SMatthias Ringwald * becomes.
158*1b2596b5SMatthias Ringwald */
159*1b2596b5SMatthias Ringwald
160*1b2596b5SMatthias Ringwald // HACK BK: avoid warning
161*1b2596b5SMatthias Ringwald #ifdef __always_inline
162*1b2596b5SMatthias Ringwald # undef __always_inline
163*1b2596b5SMatthias Ringwald #endif
164*1b2596b5SMatthias Ringwald
165*1b2596b5SMatthias Ringwald #if defined(__CC_ARM)
166*1b2596b5SMatthias Ringwald # define __always_inline __forceinline
167*1b2596b5SMatthias Ringwald #elif (defined __GNUC__)
168*1b2596b5SMatthias Ringwald # define __always_inline inline __attribute__((__always_inline__))
169*1b2596b5SMatthias Ringwald #elif (defined __ICCARM__)
170*1b2596b5SMatthias Ringwald # define __always_inline _Pragma("inline=forced")
171*1b2596b5SMatthias Ringwald #endif
172*1b2596b5SMatthias Ringwald
173*1b2596b5SMatthias Ringwald /**
174*1b2596b5SMatthias Ringwald * \def __no_inline
175*1b2596b5SMatthias Ringwald * \brief The function should not be inlined.
176*1b2596b5SMatthias Ringwald *
177*1b2596b5SMatthias Ringwald * This annotation instructs the compiler to ignore its inlining
178*1b2596b5SMatthias Ringwald * heuristics and not inline the function.
179*1b2596b5SMatthias Ringwald */
180*1b2596b5SMatthias Ringwald #if defined(__CC_ARM)
181*1b2596b5SMatthias Ringwald # define __no_inline __attribute__((noinline))
182*1b2596b5SMatthias Ringwald #elif (defined __GNUC__)
183*1b2596b5SMatthias Ringwald # define __no_inline __attribute__((__noinline__))
184*1b2596b5SMatthias Ringwald #elif (defined __ICCARM__)
185*1b2596b5SMatthias Ringwald # define __no_inline _Pragma("inline=never")
186*1b2596b5SMatthias Ringwald #endif
187*1b2596b5SMatthias Ringwald
188*1b2596b5SMatthias Ringwald /*! \brief This macro is used to test fatal errors.
189*1b2596b5SMatthias Ringwald *
190*1b2596b5SMatthias Ringwald * The macro tests if the expression is false. If it is, a fatal error is
191*1b2596b5SMatthias Ringwald * detected and the application hangs up. If TEST_SUITE_DEFINE_ASSERT_MACRO
192*1b2596b5SMatthias Ringwald * is defined, a unit test version of the macro is used, to allow execution
193*1b2596b5SMatthias Ringwald * of further tests after a false expression.
194*1b2596b5SMatthias Ringwald *
195*1b2596b5SMatthias Ringwald * \param expr Expression to evaluate and supposed to be nonzero.
196*1b2596b5SMatthias Ringwald */
197*1b2596b5SMatthias Ringwald #if defined(_ASSERT_ENABLE_)
198*1b2596b5SMatthias Ringwald # if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
199*1b2596b5SMatthias Ringwald // Assert() is defined in unit_test/suite.h
200*1b2596b5SMatthias Ringwald # include "unit_test/suite.h"
201*1b2596b5SMatthias Ringwald # else
202*1b2596b5SMatthias Ringwald #undef TEST_SUITE_DEFINE_ASSERT_MACRO
203*1b2596b5SMatthias Ringwald # define Assert(expr) \
204*1b2596b5SMatthias Ringwald {\
205*1b2596b5SMatthias Ringwald if (!(expr)) while (true);\
206*1b2596b5SMatthias Ringwald }
207*1b2596b5SMatthias Ringwald # endif
208*1b2596b5SMatthias Ringwald #else
209*1b2596b5SMatthias Ringwald # define Assert(expr) ((void) 0)
210*1b2596b5SMatthias Ringwald #endif
211*1b2596b5SMatthias Ringwald
212*1b2596b5SMatthias Ringwald /* Define WEAK attribute */
213*1b2596b5SMatthias Ringwald #if defined ( __CC_ARM ) /* Keil µVision 4 */
214*1b2596b5SMatthias Ringwald # define WEAK __attribute__ ((weak))
215*1b2596b5SMatthias Ringwald #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
216*1b2596b5SMatthias Ringwald # define WEAK __weak
217*1b2596b5SMatthias Ringwald #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
218*1b2596b5SMatthias Ringwald # define WEAK __attribute__ ((weak))
219*1b2596b5SMatthias Ringwald #endif
220*1b2596b5SMatthias Ringwald
221*1b2596b5SMatthias Ringwald /* Define NO_INIT attribute */
222*1b2596b5SMatthias Ringwald #if defined ( __CC_ARM )
223*1b2596b5SMatthias Ringwald # define NO_INIT __attribute__((zero_init))
224*1b2596b5SMatthias Ringwald #elif defined ( __ICCARM__ )
225*1b2596b5SMatthias Ringwald # define NO_INIT __no_init
226*1b2596b5SMatthias Ringwald #elif defined ( __GNUC__ )
227*1b2596b5SMatthias Ringwald # define NO_INIT __attribute__((section(".no_init")))
228*1b2596b5SMatthias Ringwald #endif
229*1b2596b5SMatthias Ringwald
230*1b2596b5SMatthias Ringwald /* Define RAMFUNC attribute */
231*1b2596b5SMatthias Ringwald #if defined ( __CC_ARM ) /* Keil µVision 4 */
232*1b2596b5SMatthias Ringwald # define RAMFUNC __attribute__ ((section(".ramfunc")))
233*1b2596b5SMatthias Ringwald #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
234*1b2596b5SMatthias Ringwald # define RAMFUNC __ramfunc
235*1b2596b5SMatthias Ringwald #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
236*1b2596b5SMatthias Ringwald # define RAMFUNC __attribute__ ((section(".ramfunc")))
237*1b2596b5SMatthias Ringwald #endif
238*1b2596b5SMatthias Ringwald
239*1b2596b5SMatthias Ringwald /* Define OPTIMIZE_HIGH attribute */
240*1b2596b5SMatthias Ringwald #if defined ( __CC_ARM ) /* Keil µVision 4 */
241*1b2596b5SMatthias Ringwald # define OPTIMIZE_HIGH _Pragma("O3")
242*1b2596b5SMatthias Ringwald #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */
243*1b2596b5SMatthias Ringwald # define OPTIMIZE_HIGH _Pragma("optimize=high")
244*1b2596b5SMatthias Ringwald #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
245*1b2596b5SMatthias Ringwald # define OPTIMIZE_HIGH __attribute__((optimize(s)))
246*1b2596b5SMatthias Ringwald #endif
247*1b2596b5SMatthias Ringwald
248*1b2596b5SMatthias Ringwald #include "interrupt.h"
249*1b2596b5SMatthias Ringwald
250*1b2596b5SMatthias Ringwald /*! \name Usual Types
251*1b2596b5SMatthias Ringwald */
252*1b2596b5SMatthias Ringwald //! @{
253*1b2596b5SMatthias Ringwald typedef unsigned char Bool; //!< Boolean.
254*1b2596b5SMatthias Ringwald #ifndef __cplusplus
255*1b2596b5SMatthias Ringwald #if !defined(__bool_true_false_are_defined)
256*1b2596b5SMatthias Ringwald typedef unsigned char bool; //!< Boolean.
257*1b2596b5SMatthias Ringwald #endif
258*1b2596b5SMatthias Ringwald #endif
259*1b2596b5SMatthias Ringwald typedef int8_t S8 ; //!< 8-bit signed integer.
260*1b2596b5SMatthias Ringwald typedef uint8_t U8 ; //!< 8-bit unsigned integer.
261*1b2596b5SMatthias Ringwald typedef int16_t S16; //!< 16-bit signed integer.
262*1b2596b5SMatthias Ringwald typedef uint16_t U16; //!< 16-bit unsigned integer.
263*1b2596b5SMatthias Ringwald typedef uint16_t le16_t;
264*1b2596b5SMatthias Ringwald typedef uint16_t be16_t;
265*1b2596b5SMatthias Ringwald typedef int32_t S32; //!< 32-bit signed integer.
266*1b2596b5SMatthias Ringwald typedef uint32_t U32; //!< 32-bit unsigned integer.
267*1b2596b5SMatthias Ringwald typedef uint32_t le32_t;
268*1b2596b5SMatthias Ringwald typedef uint32_t be32_t;
269*1b2596b5SMatthias Ringwald typedef int64_t S64; //!< 64-bit signed integer.
270*1b2596b5SMatthias Ringwald typedef uint64_t U64; //!< 64-bit unsigned integer.
271*1b2596b5SMatthias Ringwald typedef float F32; //!< 32-bit floating-point number.
272*1b2596b5SMatthias Ringwald typedef double F64; //!< 64-bit floating-point number.
273*1b2596b5SMatthias Ringwald typedef uint32_t iram_size_t;
274*1b2596b5SMatthias Ringwald //! @}
275*1b2596b5SMatthias Ringwald
276*1b2596b5SMatthias Ringwald
277*1b2596b5SMatthias Ringwald /*! \name Status Types
278*1b2596b5SMatthias Ringwald */
279*1b2596b5SMatthias Ringwald //! @{
280*1b2596b5SMatthias Ringwald typedef bool Status_bool_t; //!< Boolean status.
281*1b2596b5SMatthias Ringwald typedef U8 Status_t; //!< 8-bit-coded status.
282*1b2596b5SMatthias Ringwald //! @}
283*1b2596b5SMatthias Ringwald
284*1b2596b5SMatthias Ringwald
285*1b2596b5SMatthias Ringwald /*! \name Aliasing Aggregate Types
286*1b2596b5SMatthias Ringwald */
287*1b2596b5SMatthias Ringwald //! @{
288*1b2596b5SMatthias Ringwald
289*1b2596b5SMatthias Ringwald //! 16-bit union.
290*1b2596b5SMatthias Ringwald typedef union
291*1b2596b5SMatthias Ringwald {
292*1b2596b5SMatthias Ringwald S16 s16 ;
293*1b2596b5SMatthias Ringwald U16 u16 ;
294*1b2596b5SMatthias Ringwald S8 s8 [2];
295*1b2596b5SMatthias Ringwald U8 u8 [2];
296*1b2596b5SMatthias Ringwald } Union16;
297*1b2596b5SMatthias Ringwald
298*1b2596b5SMatthias Ringwald //! 32-bit union.
299*1b2596b5SMatthias Ringwald typedef union
300*1b2596b5SMatthias Ringwald {
301*1b2596b5SMatthias Ringwald S32 s32 ;
302*1b2596b5SMatthias Ringwald U32 u32 ;
303*1b2596b5SMatthias Ringwald S16 s16[2];
304*1b2596b5SMatthias Ringwald U16 u16[2];
305*1b2596b5SMatthias Ringwald S8 s8 [4];
306*1b2596b5SMatthias Ringwald U8 u8 [4];
307*1b2596b5SMatthias Ringwald } Union32;
308*1b2596b5SMatthias Ringwald
309*1b2596b5SMatthias Ringwald //! 64-bit union.
310*1b2596b5SMatthias Ringwald typedef union
311*1b2596b5SMatthias Ringwald {
312*1b2596b5SMatthias Ringwald S64 s64 ;
313*1b2596b5SMatthias Ringwald U64 u64 ;
314*1b2596b5SMatthias Ringwald S32 s32[2];
315*1b2596b5SMatthias Ringwald U32 u32[2];
316*1b2596b5SMatthias Ringwald S16 s16[4];
317*1b2596b5SMatthias Ringwald U16 u16[4];
318*1b2596b5SMatthias Ringwald S8 s8 [8];
319*1b2596b5SMatthias Ringwald U8 u8 [8];
320*1b2596b5SMatthias Ringwald } Union64;
321*1b2596b5SMatthias Ringwald
322*1b2596b5SMatthias Ringwald //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
323*1b2596b5SMatthias Ringwald typedef union
324*1b2596b5SMatthias Ringwald {
325*1b2596b5SMatthias Ringwald S64 *s64ptr;
326*1b2596b5SMatthias Ringwald U64 *u64ptr;
327*1b2596b5SMatthias Ringwald S32 *s32ptr;
328*1b2596b5SMatthias Ringwald U32 *u32ptr;
329*1b2596b5SMatthias Ringwald S16 *s16ptr;
330*1b2596b5SMatthias Ringwald U16 *u16ptr;
331*1b2596b5SMatthias Ringwald S8 *s8ptr ;
332*1b2596b5SMatthias Ringwald U8 *u8ptr ;
333*1b2596b5SMatthias Ringwald } UnionPtr;
334*1b2596b5SMatthias Ringwald
335*1b2596b5SMatthias Ringwald //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
336*1b2596b5SMatthias Ringwald typedef union
337*1b2596b5SMatthias Ringwald {
338*1b2596b5SMatthias Ringwald volatile S64 *s64ptr;
339*1b2596b5SMatthias Ringwald volatile U64 *u64ptr;
340*1b2596b5SMatthias Ringwald volatile S32 *s32ptr;
341*1b2596b5SMatthias Ringwald volatile U32 *u32ptr;
342*1b2596b5SMatthias Ringwald volatile S16 *s16ptr;
343*1b2596b5SMatthias Ringwald volatile U16 *u16ptr;
344*1b2596b5SMatthias Ringwald volatile S8 *s8ptr ;
345*1b2596b5SMatthias Ringwald volatile U8 *u8ptr ;
346*1b2596b5SMatthias Ringwald } UnionVPtr;
347*1b2596b5SMatthias Ringwald
348*1b2596b5SMatthias Ringwald //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
349*1b2596b5SMatthias Ringwald typedef union
350*1b2596b5SMatthias Ringwald {
351*1b2596b5SMatthias Ringwald const S64 *s64ptr;
352*1b2596b5SMatthias Ringwald const U64 *u64ptr;
353*1b2596b5SMatthias Ringwald const S32 *s32ptr;
354*1b2596b5SMatthias Ringwald const U32 *u32ptr;
355*1b2596b5SMatthias Ringwald const S16 *s16ptr;
356*1b2596b5SMatthias Ringwald const U16 *u16ptr;
357*1b2596b5SMatthias Ringwald const S8 *s8ptr ;
358*1b2596b5SMatthias Ringwald const U8 *u8ptr ;
359*1b2596b5SMatthias Ringwald } UnionCPtr;
360*1b2596b5SMatthias Ringwald
361*1b2596b5SMatthias Ringwald //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
362*1b2596b5SMatthias Ringwald typedef union
363*1b2596b5SMatthias Ringwald {
364*1b2596b5SMatthias Ringwald const volatile S64 *s64ptr;
365*1b2596b5SMatthias Ringwald const volatile U64 *u64ptr;
366*1b2596b5SMatthias Ringwald const volatile S32 *s32ptr;
367*1b2596b5SMatthias Ringwald const volatile U32 *u32ptr;
368*1b2596b5SMatthias Ringwald const volatile S16 *s16ptr;
369*1b2596b5SMatthias Ringwald const volatile U16 *u16ptr;
370*1b2596b5SMatthias Ringwald const volatile S8 *s8ptr ;
371*1b2596b5SMatthias Ringwald const volatile U8 *u8ptr ;
372*1b2596b5SMatthias Ringwald } UnionCVPtr;
373*1b2596b5SMatthias Ringwald
374*1b2596b5SMatthias Ringwald //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
375*1b2596b5SMatthias Ringwald typedef struct
376*1b2596b5SMatthias Ringwald {
377*1b2596b5SMatthias Ringwald S64 *s64ptr;
378*1b2596b5SMatthias Ringwald U64 *u64ptr;
379*1b2596b5SMatthias Ringwald S32 *s32ptr;
380*1b2596b5SMatthias Ringwald U32 *u32ptr;
381*1b2596b5SMatthias Ringwald S16 *s16ptr;
382*1b2596b5SMatthias Ringwald U16 *u16ptr;
383*1b2596b5SMatthias Ringwald S8 *s8ptr ;
384*1b2596b5SMatthias Ringwald U8 *u8ptr ;
385*1b2596b5SMatthias Ringwald } StructPtr;
386*1b2596b5SMatthias Ringwald
387*1b2596b5SMatthias Ringwald //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
388*1b2596b5SMatthias Ringwald typedef struct
389*1b2596b5SMatthias Ringwald {
390*1b2596b5SMatthias Ringwald volatile S64 *s64ptr;
391*1b2596b5SMatthias Ringwald volatile U64 *u64ptr;
392*1b2596b5SMatthias Ringwald volatile S32 *s32ptr;
393*1b2596b5SMatthias Ringwald volatile U32 *u32ptr;
394*1b2596b5SMatthias Ringwald volatile S16 *s16ptr;
395*1b2596b5SMatthias Ringwald volatile U16 *u16ptr;
396*1b2596b5SMatthias Ringwald volatile S8 *s8ptr ;
397*1b2596b5SMatthias Ringwald volatile U8 *u8ptr ;
398*1b2596b5SMatthias Ringwald } StructVPtr;
399*1b2596b5SMatthias Ringwald
400*1b2596b5SMatthias Ringwald //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
401*1b2596b5SMatthias Ringwald typedef struct
402*1b2596b5SMatthias Ringwald {
403*1b2596b5SMatthias Ringwald const S64 *s64ptr;
404*1b2596b5SMatthias Ringwald const U64 *u64ptr;
405*1b2596b5SMatthias Ringwald const S32 *s32ptr;
406*1b2596b5SMatthias Ringwald const U32 *u32ptr;
407*1b2596b5SMatthias Ringwald const S16 *s16ptr;
408*1b2596b5SMatthias Ringwald const U16 *u16ptr;
409*1b2596b5SMatthias Ringwald const S8 *s8ptr ;
410*1b2596b5SMatthias Ringwald const U8 *u8ptr ;
411*1b2596b5SMatthias Ringwald } StructCPtr;
412*1b2596b5SMatthias Ringwald
413*1b2596b5SMatthias Ringwald //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
414*1b2596b5SMatthias Ringwald typedef struct
415*1b2596b5SMatthias Ringwald {
416*1b2596b5SMatthias Ringwald const volatile S64 *s64ptr;
417*1b2596b5SMatthias Ringwald const volatile U64 *u64ptr;
418*1b2596b5SMatthias Ringwald const volatile S32 *s32ptr;
419*1b2596b5SMatthias Ringwald const volatile U32 *u32ptr;
420*1b2596b5SMatthias Ringwald const volatile S16 *s16ptr;
421*1b2596b5SMatthias Ringwald const volatile U16 *u16ptr;
422*1b2596b5SMatthias Ringwald const volatile S8 *s8ptr ;
423*1b2596b5SMatthias Ringwald const volatile U8 *u8ptr ;
424*1b2596b5SMatthias Ringwald } StructCVPtr;
425*1b2596b5SMatthias Ringwald
426*1b2596b5SMatthias Ringwald //! @}
427*1b2596b5SMatthias Ringwald
428*1b2596b5SMatthias Ringwald #endif // #ifndef __ASSEMBLY__
429*1b2596b5SMatthias Ringwald
430*1b2596b5SMatthias Ringwald /*! \name Usual Constants
431*1b2596b5SMatthias Ringwald */
432*1b2596b5SMatthias Ringwald //! @{
433*1b2596b5SMatthias Ringwald #define DISABLE 0
434*1b2596b5SMatthias Ringwald #define ENABLE 1
435*1b2596b5SMatthias Ringwald #ifndef __cplusplus
436*1b2596b5SMatthias Ringwald #if !defined(__bool_true_false_are_defined)
437*1b2596b5SMatthias Ringwald #define false 0
438*1b2596b5SMatthias Ringwald #define true 1
439*1b2596b5SMatthias Ringwald #endif
440*1b2596b5SMatthias Ringwald #endif
441*1b2596b5SMatthias Ringwald #define PASS 0
442*1b2596b5SMatthias Ringwald #define FAIL 1
443*1b2596b5SMatthias Ringwald #define LOW 0
444*1b2596b5SMatthias Ringwald #define HIGH 1
445*1b2596b5SMatthias Ringwald //! @}
446*1b2596b5SMatthias Ringwald
447*1b2596b5SMatthias Ringwald
448*1b2596b5SMatthias Ringwald #ifndef __ASSEMBLY__ // not for assembling.
449*1b2596b5SMatthias Ringwald
450*1b2596b5SMatthias Ringwald //! \name Optimization Control
451*1b2596b5SMatthias Ringwald //@{
452*1b2596b5SMatthias Ringwald
453*1b2596b5SMatthias Ringwald /**
454*1b2596b5SMatthias Ringwald * \def likely(exp)
455*1b2596b5SMatthias Ringwald * \brief The expression \a exp is likely to be true
456*1b2596b5SMatthias Ringwald */
457*1b2596b5SMatthias Ringwald #ifndef likely
458*1b2596b5SMatthias Ringwald # define likely(exp) (exp)
459*1b2596b5SMatthias Ringwald #endif
460*1b2596b5SMatthias Ringwald
461*1b2596b5SMatthias Ringwald /**
462*1b2596b5SMatthias Ringwald * \def unlikely(exp)
463*1b2596b5SMatthias Ringwald * \brief The expression \a exp is unlikely to be true
464*1b2596b5SMatthias Ringwald */
465*1b2596b5SMatthias Ringwald #ifndef unlikely
466*1b2596b5SMatthias Ringwald # define unlikely(exp) (exp)
467*1b2596b5SMatthias Ringwald #endif
468*1b2596b5SMatthias Ringwald
469*1b2596b5SMatthias Ringwald /**
470*1b2596b5SMatthias Ringwald * \def is_constant(exp)
471*1b2596b5SMatthias Ringwald * \brief Determine if an expression evaluates to a constant value.
472*1b2596b5SMatthias Ringwald *
473*1b2596b5SMatthias Ringwald * \param exp Any expression
474*1b2596b5SMatthias Ringwald *
475*1b2596b5SMatthias Ringwald * \return true if \a exp is constant, false otherwise.
476*1b2596b5SMatthias Ringwald */
477*1b2596b5SMatthias Ringwald #if (defined __GNUC__) || (defined __CC_ARM)
478*1b2596b5SMatthias Ringwald # define is_constant(exp) __builtin_constant_p(exp)
479*1b2596b5SMatthias Ringwald #else
480*1b2596b5SMatthias Ringwald # define is_constant(exp) (0)
481*1b2596b5SMatthias Ringwald #endif
482*1b2596b5SMatthias Ringwald
483*1b2596b5SMatthias Ringwald //! @}
484*1b2596b5SMatthias Ringwald
485*1b2596b5SMatthias Ringwald /*! \name Bit-Field Handling
486*1b2596b5SMatthias Ringwald */
487*1b2596b5SMatthias Ringwald //! @{
488*1b2596b5SMatthias Ringwald
489*1b2596b5SMatthias Ringwald /*! \brief Reads the bits of a value specified by a given bit-mask.
490*1b2596b5SMatthias Ringwald *
491*1b2596b5SMatthias Ringwald * \param value Value to read bits from.
492*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating bits to read.
493*1b2596b5SMatthias Ringwald *
494*1b2596b5SMatthias Ringwald * \return Read bits.
495*1b2596b5SMatthias Ringwald */
496*1b2596b5SMatthias Ringwald #define Rd_bits( value, mask) ((value) & (mask))
497*1b2596b5SMatthias Ringwald
498*1b2596b5SMatthias Ringwald /*! \brief Writes the bits of a C lvalue specified by a given bit-mask.
499*1b2596b5SMatthias Ringwald *
500*1b2596b5SMatthias Ringwald * \param lvalue C lvalue to write bits to.
501*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating bits to write.
502*1b2596b5SMatthias Ringwald * \param bits Bits to write.
503*1b2596b5SMatthias Ringwald *
504*1b2596b5SMatthias Ringwald * \return Resulting value with written bits.
505*1b2596b5SMatthias Ringwald */
506*1b2596b5SMatthias Ringwald #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
507*1b2596b5SMatthias Ringwald ((bits ) & (mask)))
508*1b2596b5SMatthias Ringwald
509*1b2596b5SMatthias Ringwald /*! \brief Tests the bits of a value specified by a given bit-mask.
510*1b2596b5SMatthias Ringwald *
511*1b2596b5SMatthias Ringwald * \param value Value of which to test bits.
512*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating bits to test.
513*1b2596b5SMatthias Ringwald *
514*1b2596b5SMatthias Ringwald * \return \c 1 if at least one of the tested bits is set, else \c 0.
515*1b2596b5SMatthias Ringwald */
516*1b2596b5SMatthias Ringwald #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
517*1b2596b5SMatthias Ringwald
518*1b2596b5SMatthias Ringwald /*! \brief Clears the bits of a C lvalue specified by a given bit-mask.
519*1b2596b5SMatthias Ringwald *
520*1b2596b5SMatthias Ringwald * \param lvalue C lvalue of which to clear bits.
521*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating bits to clear.
522*1b2596b5SMatthias Ringwald *
523*1b2596b5SMatthias Ringwald * \return Resulting value with cleared bits.
524*1b2596b5SMatthias Ringwald */
525*1b2596b5SMatthias Ringwald #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
526*1b2596b5SMatthias Ringwald
527*1b2596b5SMatthias Ringwald /*! \brief Sets the bits of a C lvalue specified by a given bit-mask.
528*1b2596b5SMatthias Ringwald *
529*1b2596b5SMatthias Ringwald * \param lvalue C lvalue of which to set bits.
530*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating bits to set.
531*1b2596b5SMatthias Ringwald *
532*1b2596b5SMatthias Ringwald * \return Resulting value with set bits.
533*1b2596b5SMatthias Ringwald */
534*1b2596b5SMatthias Ringwald #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
535*1b2596b5SMatthias Ringwald
536*1b2596b5SMatthias Ringwald /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.
537*1b2596b5SMatthias Ringwald *
538*1b2596b5SMatthias Ringwald * \param lvalue C lvalue of which to toggle bits.
539*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating bits to toggle.
540*1b2596b5SMatthias Ringwald *
541*1b2596b5SMatthias Ringwald * \return Resulting value with toggled bits.
542*1b2596b5SMatthias Ringwald */
543*1b2596b5SMatthias Ringwald #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
544*1b2596b5SMatthias Ringwald
545*1b2596b5SMatthias Ringwald /*! \brief Reads the bit-field of a value specified by a given bit-mask.
546*1b2596b5SMatthias Ringwald *
547*1b2596b5SMatthias Ringwald * \param value Value to read a bit-field from.
548*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating the bit-field to read.
549*1b2596b5SMatthias Ringwald *
550*1b2596b5SMatthias Ringwald * \return Read bit-field.
551*1b2596b5SMatthias Ringwald */
552*1b2596b5SMatthias Ringwald #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
553*1b2596b5SMatthias Ringwald
554*1b2596b5SMatthias Ringwald /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
555*1b2596b5SMatthias Ringwald *
556*1b2596b5SMatthias Ringwald * \param lvalue C lvalue to write a bit-field to.
557*1b2596b5SMatthias Ringwald * \param mask Bit-mask indicating the bit-field to write.
558*1b2596b5SMatthias Ringwald * \param bitfield Bit-field to write.
559*1b2596b5SMatthias Ringwald *
560*1b2596b5SMatthias Ringwald * \return Resulting value with written bit-field.
561*1b2596b5SMatthias Ringwald */
562*1b2596b5SMatthias Ringwald #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))
563*1b2596b5SMatthias Ringwald
564*1b2596b5SMatthias Ringwald //! @}
565*1b2596b5SMatthias Ringwald
566*1b2596b5SMatthias Ringwald
567*1b2596b5SMatthias Ringwald /*! \name Zero-Bit Counting
568*1b2596b5SMatthias Ringwald *
569*1b2596b5SMatthias Ringwald * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
570*1b2596b5SMatthias Ringwald * applied to constant expressions (values known at compile time), so they are
571*1b2596b5SMatthias Ringwald * more optimized than the use of the corresponding assembly instructions and
572*1b2596b5SMatthias Ringwald * they can be used as constant expressions e.g. to initialize objects having
573*1b2596b5SMatthias Ringwald * static storage duration, and like the corresponding assembly instructions
574*1b2596b5SMatthias Ringwald * when applied to non-constant expressions (values unknown at compile time), so
575*1b2596b5SMatthias Ringwald * they are more optimized than an assembly periphrasis. Hence, clz and ctz
576*1b2596b5SMatthias Ringwald * ensure a possible and optimized behavior for both constant and non-constant
577*1b2596b5SMatthias Ringwald * expressions.
578*1b2596b5SMatthias Ringwald */
579*1b2596b5SMatthias Ringwald //! @{
580*1b2596b5SMatthias Ringwald
581*1b2596b5SMatthias Ringwald /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
582*1b2596b5SMatthias Ringwald *
583*1b2596b5SMatthias Ringwald * \param u Value of which to count the leading zero bits.
584*1b2596b5SMatthias Ringwald *
585*1b2596b5SMatthias Ringwald * \return The count of leading zero bits in \a u.
586*1b2596b5SMatthias Ringwald */
587*1b2596b5SMatthias Ringwald #if (defined __GNUC__) || (defined __CC_ARM)
588*1b2596b5SMatthias Ringwald # define clz(u) __builtin_clz(u)
589*1b2596b5SMatthias Ringwald #elif (defined __ICCARM__)
590*1b2596b5SMatthias Ringwald # define clz(u) __CLZ(u)
591*1b2596b5SMatthias Ringwald #else
592*1b2596b5SMatthias Ringwald # define clz(u) (((u) == 0) ? 32 : \
593*1b2596b5SMatthias Ringwald ((u) & (1ul << 31)) ? 0 : \
594*1b2596b5SMatthias Ringwald ((u) & (1ul << 30)) ? 1 : \
595*1b2596b5SMatthias Ringwald ((u) & (1ul << 29)) ? 2 : \
596*1b2596b5SMatthias Ringwald ((u) & (1ul << 28)) ? 3 : \
597*1b2596b5SMatthias Ringwald ((u) & (1ul << 27)) ? 4 : \
598*1b2596b5SMatthias Ringwald ((u) & (1ul << 26)) ? 5 : \
599*1b2596b5SMatthias Ringwald ((u) & (1ul << 25)) ? 6 : \
600*1b2596b5SMatthias Ringwald ((u) & (1ul << 24)) ? 7 : \
601*1b2596b5SMatthias Ringwald ((u) & (1ul << 23)) ? 8 : \
602*1b2596b5SMatthias Ringwald ((u) & (1ul << 22)) ? 9 : \
603*1b2596b5SMatthias Ringwald ((u) & (1ul << 21)) ? 10 : \
604*1b2596b5SMatthias Ringwald ((u) & (1ul << 20)) ? 11 : \
605*1b2596b5SMatthias Ringwald ((u) & (1ul << 19)) ? 12 : \
606*1b2596b5SMatthias Ringwald ((u) & (1ul << 18)) ? 13 : \
607*1b2596b5SMatthias Ringwald ((u) & (1ul << 17)) ? 14 : \
608*1b2596b5SMatthias Ringwald ((u) & (1ul << 16)) ? 15 : \
609*1b2596b5SMatthias Ringwald ((u) & (1ul << 15)) ? 16 : \
610*1b2596b5SMatthias Ringwald ((u) & (1ul << 14)) ? 17 : \
611*1b2596b5SMatthias Ringwald ((u) & (1ul << 13)) ? 18 : \
612*1b2596b5SMatthias Ringwald ((u) & (1ul << 12)) ? 19 : \
613*1b2596b5SMatthias Ringwald ((u) & (1ul << 11)) ? 20 : \
614*1b2596b5SMatthias Ringwald ((u) & (1ul << 10)) ? 21 : \
615*1b2596b5SMatthias Ringwald ((u) & (1ul << 9)) ? 22 : \
616*1b2596b5SMatthias Ringwald ((u) & (1ul << 8)) ? 23 : \
617*1b2596b5SMatthias Ringwald ((u) & (1ul << 7)) ? 24 : \
618*1b2596b5SMatthias Ringwald ((u) & (1ul << 6)) ? 25 : \
619*1b2596b5SMatthias Ringwald ((u) & (1ul << 5)) ? 26 : \
620*1b2596b5SMatthias Ringwald ((u) & (1ul << 4)) ? 27 : \
621*1b2596b5SMatthias Ringwald ((u) & (1ul << 3)) ? 28 : \
622*1b2596b5SMatthias Ringwald ((u) & (1ul << 2)) ? 29 : \
623*1b2596b5SMatthias Ringwald ((u) & (1ul << 1)) ? 30 : \
624*1b2596b5SMatthias Ringwald 31)
625*1b2596b5SMatthias Ringwald #endif
626*1b2596b5SMatthias Ringwald
627*1b2596b5SMatthias Ringwald /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
628*1b2596b5SMatthias Ringwald *
629*1b2596b5SMatthias Ringwald * \param u Value of which to count the trailing zero bits.
630*1b2596b5SMatthias Ringwald *
631*1b2596b5SMatthias Ringwald * \return The count of trailing zero bits in \a u.
632*1b2596b5SMatthias Ringwald */
633*1b2596b5SMatthias Ringwald #if (defined __GNUC__) || (defined __CC_ARM)
634*1b2596b5SMatthias Ringwald # define ctz(u) __builtin_ctz(u)
635*1b2596b5SMatthias Ringwald #else
636*1b2596b5SMatthias Ringwald # define ctz(u) ((u) & (1ul << 0) ? 0 : \
637*1b2596b5SMatthias Ringwald (u) & (1ul << 1) ? 1 : \
638*1b2596b5SMatthias Ringwald (u) & (1ul << 2) ? 2 : \
639*1b2596b5SMatthias Ringwald (u) & (1ul << 3) ? 3 : \
640*1b2596b5SMatthias Ringwald (u) & (1ul << 4) ? 4 : \
641*1b2596b5SMatthias Ringwald (u) & (1ul << 5) ? 5 : \
642*1b2596b5SMatthias Ringwald (u) & (1ul << 6) ? 6 : \
643*1b2596b5SMatthias Ringwald (u) & (1ul << 7) ? 7 : \
644*1b2596b5SMatthias Ringwald (u) & (1ul << 8) ? 8 : \
645*1b2596b5SMatthias Ringwald (u) & (1ul << 9) ? 9 : \
646*1b2596b5SMatthias Ringwald (u) & (1ul << 10) ? 10 : \
647*1b2596b5SMatthias Ringwald (u) & (1ul << 11) ? 11 : \
648*1b2596b5SMatthias Ringwald (u) & (1ul << 12) ? 12 : \
649*1b2596b5SMatthias Ringwald (u) & (1ul << 13) ? 13 : \
650*1b2596b5SMatthias Ringwald (u) & (1ul << 14) ? 14 : \
651*1b2596b5SMatthias Ringwald (u) & (1ul << 15) ? 15 : \
652*1b2596b5SMatthias Ringwald (u) & (1ul << 16) ? 16 : \
653*1b2596b5SMatthias Ringwald (u) & (1ul << 17) ? 17 : \
654*1b2596b5SMatthias Ringwald (u) & (1ul << 18) ? 18 : \
655*1b2596b5SMatthias Ringwald (u) & (1ul << 19) ? 19 : \
656*1b2596b5SMatthias Ringwald (u) & (1ul << 20) ? 20 : \
657*1b2596b5SMatthias Ringwald (u) & (1ul << 21) ? 21 : \
658*1b2596b5SMatthias Ringwald (u) & (1ul << 22) ? 22 : \
659*1b2596b5SMatthias Ringwald (u) & (1ul << 23) ? 23 : \
660*1b2596b5SMatthias Ringwald (u) & (1ul << 24) ? 24 : \
661*1b2596b5SMatthias Ringwald (u) & (1ul << 25) ? 25 : \
662*1b2596b5SMatthias Ringwald (u) & (1ul << 26) ? 26 : \
663*1b2596b5SMatthias Ringwald (u) & (1ul << 27) ? 27 : \
664*1b2596b5SMatthias Ringwald (u) & (1ul << 28) ? 28 : \
665*1b2596b5SMatthias Ringwald (u) & (1ul << 29) ? 29 : \
666*1b2596b5SMatthias Ringwald (u) & (1ul << 30) ? 30 : \
667*1b2596b5SMatthias Ringwald (u) & (1ul << 31) ? 31 : \
668*1b2596b5SMatthias Ringwald 32)
669*1b2596b5SMatthias Ringwald #endif
670*1b2596b5SMatthias Ringwald
671*1b2596b5SMatthias Ringwald //! @}
672*1b2596b5SMatthias Ringwald
673*1b2596b5SMatthias Ringwald
674*1b2596b5SMatthias Ringwald /*! \name Bit Reversing
675*1b2596b5SMatthias Ringwald */
676*1b2596b5SMatthias Ringwald //! @{
677*1b2596b5SMatthias Ringwald
678*1b2596b5SMatthias Ringwald /*! \brief Reverses the bits of \a u8.
679*1b2596b5SMatthias Ringwald *
680*1b2596b5SMatthias Ringwald * \param u8 U8 of which to reverse the bits.
681*1b2596b5SMatthias Ringwald *
682*1b2596b5SMatthias Ringwald * \return Value resulting from \a u8 with reversed bits.
683*1b2596b5SMatthias Ringwald */
684*1b2596b5SMatthias Ringwald #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
685*1b2596b5SMatthias Ringwald
686*1b2596b5SMatthias Ringwald /*! \brief Reverses the bits of \a u16.
687*1b2596b5SMatthias Ringwald *
688*1b2596b5SMatthias Ringwald * \param u16 U16 of which to reverse the bits.
689*1b2596b5SMatthias Ringwald *
690*1b2596b5SMatthias Ringwald * \return Value resulting from \a u16 with reversed bits.
691*1b2596b5SMatthias Ringwald */
692*1b2596b5SMatthias Ringwald #define bit_reverse16(u16) ((U16)(bit_reverse32((U16)(u16)) >> 16))
693*1b2596b5SMatthias Ringwald
694*1b2596b5SMatthias Ringwald /*! \brief Reverses the bits of \a u32.
695*1b2596b5SMatthias Ringwald *
696*1b2596b5SMatthias Ringwald * \param u32 U32 of which to reverse the bits.
697*1b2596b5SMatthias Ringwald *
698*1b2596b5SMatthias Ringwald * \return Value resulting from \a u32 with reversed bits.
699*1b2596b5SMatthias Ringwald */
700*1b2596b5SMatthias Ringwald #define bit_reverse32(u32) __RBIT(u32)
701*1b2596b5SMatthias Ringwald
702*1b2596b5SMatthias Ringwald /*! \brief Reverses the bits of \a u64.
703*1b2596b5SMatthias Ringwald *
704*1b2596b5SMatthias Ringwald * \param u64 U64 of which to reverse the bits.
705*1b2596b5SMatthias Ringwald *
706*1b2596b5SMatthias Ringwald * \return Value resulting from \a u64 with reversed bits.
707*1b2596b5SMatthias Ringwald */
708*1b2596b5SMatthias Ringwald #define bit_reverse64(u64) ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\
709*1b2596b5SMatthias Ringwald ((U64)bit_reverse32((U64)(u64)) << 32)))
710*1b2596b5SMatthias Ringwald
711*1b2596b5SMatthias Ringwald //! @}
712*1b2596b5SMatthias Ringwald
713*1b2596b5SMatthias Ringwald
714*1b2596b5SMatthias Ringwald /*! \name Alignment
715*1b2596b5SMatthias Ringwald */
716*1b2596b5SMatthias Ringwald //! @{
717*1b2596b5SMatthias Ringwald
718*1b2596b5SMatthias Ringwald /*! \brief Tests alignment of the number \a val with the \a n boundary.
719*1b2596b5SMatthias Ringwald *
720*1b2596b5SMatthias Ringwald * \param val Input value.
721*1b2596b5SMatthias Ringwald * \param n Boundary.
722*1b2596b5SMatthias Ringwald *
723*1b2596b5SMatthias Ringwald * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
724*1b2596b5SMatthias Ringwald */
725*1b2596b5SMatthias Ringwald #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) )
726*1b2596b5SMatthias Ringwald
727*1b2596b5SMatthias Ringwald /*! \brief Gets alignment of the number \a val with respect to the \a n boundary.
728*1b2596b5SMatthias Ringwald *
729*1b2596b5SMatthias Ringwald * \param val Input value.
730*1b2596b5SMatthias Ringwald * \param n Boundary.
731*1b2596b5SMatthias Ringwald *
732*1b2596b5SMatthias Ringwald * \return Alignment of the number \a val with respect to the \a n boundary.
733*1b2596b5SMatthias Ringwald */
734*1b2596b5SMatthias Ringwald #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) )
735*1b2596b5SMatthias Ringwald
736*1b2596b5SMatthias Ringwald /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
737*1b2596b5SMatthias Ringwald *
738*1b2596b5SMatthias Ringwald * \param lval Input/output lvalue.
739*1b2596b5SMatthias Ringwald * \param n Boundary.
740*1b2596b5SMatthias Ringwald * \param alg Alignment.
741*1b2596b5SMatthias Ringwald *
742*1b2596b5SMatthias Ringwald * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
743*1b2596b5SMatthias Ringwald */
744*1b2596b5SMatthias Ringwald #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
745*1b2596b5SMatthias Ringwald
746*1b2596b5SMatthias Ringwald /*! \brief Aligns the number \a val with the upper \a n boundary.
747*1b2596b5SMatthias Ringwald *
748*1b2596b5SMatthias Ringwald * \param val Input value.
749*1b2596b5SMatthias Ringwald * \param n Boundary.
750*1b2596b5SMatthias Ringwald *
751*1b2596b5SMatthias Ringwald * \return Value resulting from the number \a val aligned with the upper \a n boundary.
752*1b2596b5SMatthias Ringwald */
753*1b2596b5SMatthias Ringwald #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1))
754*1b2596b5SMatthias Ringwald
755*1b2596b5SMatthias Ringwald /*! \brief Aligns the number \a val with the lower \a n boundary.
756*1b2596b5SMatthias Ringwald *
757*1b2596b5SMatthias Ringwald * \param val Input value.
758*1b2596b5SMatthias Ringwald * \param n Boundary.
759*1b2596b5SMatthias Ringwald *
760*1b2596b5SMatthias Ringwald * \return Value resulting from the number \a val aligned with the lower \a n boundary.
761*1b2596b5SMatthias Ringwald */
762*1b2596b5SMatthias Ringwald #define Align_down(val, n ) ( (val) & ~((n) - 1))
763*1b2596b5SMatthias Ringwald
764*1b2596b5SMatthias Ringwald //! @}
765*1b2596b5SMatthias Ringwald
766*1b2596b5SMatthias Ringwald
767*1b2596b5SMatthias Ringwald /*! \name Mathematics
768*1b2596b5SMatthias Ringwald *
769*1b2596b5SMatthias Ringwald * The same considerations as for clz and ctz apply here but GCC does not
770*1b2596b5SMatthias Ringwald * provide built-in functions to access the assembly instructions abs, min and
771*1b2596b5SMatthias Ringwald * max and it does not produce them by itself in most cases, so two sets of
772*1b2596b5SMatthias Ringwald * macros are defined here:
773*1b2596b5SMatthias Ringwald * - Abs, Min and Max to apply to constant expressions (values known at
774*1b2596b5SMatthias Ringwald * compile time);
775*1b2596b5SMatthias Ringwald * - abs, min and max to apply to non-constant expressions (values unknown at
776*1b2596b5SMatthias Ringwald * compile time), abs is found in stdlib.h.
777*1b2596b5SMatthias Ringwald */
778*1b2596b5SMatthias Ringwald //! @{
779*1b2596b5SMatthias Ringwald
780*1b2596b5SMatthias Ringwald /*! \brief Takes the absolute value of \a a.
781*1b2596b5SMatthias Ringwald *
782*1b2596b5SMatthias Ringwald * \param a Input value.
783*1b2596b5SMatthias Ringwald *
784*1b2596b5SMatthias Ringwald * \return Absolute value of \a a.
785*1b2596b5SMatthias Ringwald *
786*1b2596b5SMatthias Ringwald * \note More optimized if only used with values known at compile time.
787*1b2596b5SMatthias Ringwald */
788*1b2596b5SMatthias Ringwald #define Abs(a) (((a) < 0 ) ? -(a) : (a))
789*1b2596b5SMatthias Ringwald
790*1b2596b5SMatthias Ringwald /*! \brief Takes the minimal value of \a a and \a b.
791*1b2596b5SMatthias Ringwald *
792*1b2596b5SMatthias Ringwald * \param a Input value.
793*1b2596b5SMatthias Ringwald * \param b Input value.
794*1b2596b5SMatthias Ringwald *
795*1b2596b5SMatthias Ringwald * \return Minimal value of \a a and \a b.
796*1b2596b5SMatthias Ringwald *
797*1b2596b5SMatthias Ringwald * \note More optimized if only used with values known at compile time.
798*1b2596b5SMatthias Ringwald */
799*1b2596b5SMatthias Ringwald #define Min(a, b) (((a) < (b)) ? (a) : (b))
800*1b2596b5SMatthias Ringwald
801*1b2596b5SMatthias Ringwald /*! \brief Takes the maximal value of \a a and \a b.
802*1b2596b5SMatthias Ringwald *
803*1b2596b5SMatthias Ringwald * \param a Input value.
804*1b2596b5SMatthias Ringwald * \param b Input value.
805*1b2596b5SMatthias Ringwald *
806*1b2596b5SMatthias Ringwald * \return Maximal value of \a a and \a b.
807*1b2596b5SMatthias Ringwald *
808*1b2596b5SMatthias Ringwald * \note More optimized if only used with values known at compile time.
809*1b2596b5SMatthias Ringwald */
810*1b2596b5SMatthias Ringwald #define Max(a, b) (((a) > (b)) ? (a) : (b))
811*1b2596b5SMatthias Ringwald
812*1b2596b5SMatthias Ringwald // abs() is already defined by stdlib.h
813*1b2596b5SMatthias Ringwald
814*1b2596b5SMatthias Ringwald /*! \brief Takes the minimal value of \a a and \a b.
815*1b2596b5SMatthias Ringwald *
816*1b2596b5SMatthias Ringwald * \param a Input value.
817*1b2596b5SMatthias Ringwald * \param b Input value.
818*1b2596b5SMatthias Ringwald *
819*1b2596b5SMatthias Ringwald * \return Minimal value of \a a and \a b.
820*1b2596b5SMatthias Ringwald *
821*1b2596b5SMatthias Ringwald * \note More optimized if only used with values unknown at compile time.
822*1b2596b5SMatthias Ringwald */
823*1b2596b5SMatthias Ringwald #define min(a, b) Min(a, b)
824*1b2596b5SMatthias Ringwald
825*1b2596b5SMatthias Ringwald /*! \brief Takes the maximal value of \a a and \a b.
826*1b2596b5SMatthias Ringwald *
827*1b2596b5SMatthias Ringwald * \param a Input value.
828*1b2596b5SMatthias Ringwald * \param b Input value.
829*1b2596b5SMatthias Ringwald *
830*1b2596b5SMatthias Ringwald * \return Maximal value of \a a and \a b.
831*1b2596b5SMatthias Ringwald *
832*1b2596b5SMatthias Ringwald * \note More optimized if only used with values unknown at compile time.
833*1b2596b5SMatthias Ringwald */
834*1b2596b5SMatthias Ringwald #define max(a, b) Max(a, b)
835*1b2596b5SMatthias Ringwald
836*1b2596b5SMatthias Ringwald //! @}
837*1b2596b5SMatthias Ringwald
838*1b2596b5SMatthias Ringwald
839*1b2596b5SMatthias Ringwald /*! \brief Calls the routine at address \a addr.
840*1b2596b5SMatthias Ringwald *
841*1b2596b5SMatthias Ringwald * It generates a long call opcode.
842*1b2596b5SMatthias Ringwald *
843*1b2596b5SMatthias Ringwald * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
844*1b2596b5SMatthias Ringwald * it is invoked from the CPU supervisor mode.
845*1b2596b5SMatthias Ringwald *
846*1b2596b5SMatthias Ringwald * \param addr Address of the routine to call.
847*1b2596b5SMatthias Ringwald *
848*1b2596b5SMatthias Ringwald * \note It may be used as a long jump opcode in some special cases.
849*1b2596b5SMatthias Ringwald */
850*1b2596b5SMatthias Ringwald #define Long_call(addr) ((*(void (*)(void))(addr))())
851*1b2596b5SMatthias Ringwald
852*1b2596b5SMatthias Ringwald
853*1b2596b5SMatthias Ringwald /*! \name MCU Endianism Handling
854*1b2596b5SMatthias Ringwald * ARM is MCU little endianism.
855*1b2596b5SMatthias Ringwald */
856*1b2596b5SMatthias Ringwald //! @{
857*1b2596b5SMatthias Ringwald #define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16.
858*1b2596b5SMatthias Ringwald #define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16.
859*1b2596b5SMatthias Ringwald
860*1b2596b5SMatthias Ringwald #define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32.
861*1b2596b5SMatthias Ringwald #define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32.
862*1b2596b5SMatthias Ringwald #define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32.
863*1b2596b5SMatthias Ringwald #define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32.
864*1b2596b5SMatthias Ringwald #define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32.
865*1b2596b5SMatthias Ringwald #define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32.
866*1b2596b5SMatthias Ringwald #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32.
867*1b2596b5SMatthias Ringwald #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32.
868*1b2596b5SMatthias Ringwald #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32.
869*1b2596b5SMatthias Ringwald #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32.
870*1b2596b5SMatthias Ringwald
871*1b2596b5SMatthias Ringwald #define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64.
872*1b2596b5SMatthias Ringwald #define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64.
873*1b2596b5SMatthias Ringwald #define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64.
874*1b2596b5SMatthias Ringwald #define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64.
875*1b2596b5SMatthias Ringwald #define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64.
876*1b2596b5SMatthias Ringwald #define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64.
877*1b2596b5SMatthias Ringwald #define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64.
878*1b2596b5SMatthias Ringwald #define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64.
879*1b2596b5SMatthias Ringwald #define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64.
880*1b2596b5SMatthias Ringwald #define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64.
881*1b2596b5SMatthias Ringwald #define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64.
882*1b2596b5SMatthias Ringwald #define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64.
883*1b2596b5SMatthias Ringwald #define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64.
884*1b2596b5SMatthias Ringwald #define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64.
885*1b2596b5SMatthias Ringwald #define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64.
886*1b2596b5SMatthias Ringwald #define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64.
887*1b2596b5SMatthias Ringwald #define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64.
888*1b2596b5SMatthias Ringwald #define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64.
889*1b2596b5SMatthias Ringwald #define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64.
890*1b2596b5SMatthias Ringwald #define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64.
891*1b2596b5SMatthias Ringwald #define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64.
892*1b2596b5SMatthias Ringwald #define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64.
893*1b2596b5SMatthias Ringwald #define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64.
894*1b2596b5SMatthias Ringwald #define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64.
895*1b2596b5SMatthias Ringwald #define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64.
896*1b2596b5SMatthias Ringwald #define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64.
897*1b2596b5SMatthias Ringwald
898*1b2596b5SMatthias Ringwald #define BE16(x) Swap16(x)
899*1b2596b5SMatthias Ringwald #define LE16(x) (x)
900*1b2596b5SMatthias Ringwald
901*1b2596b5SMatthias Ringwald #define le16_to_cpu(x) (x)
902*1b2596b5SMatthias Ringwald #define cpu_to_le16(x) (x)
903*1b2596b5SMatthias Ringwald #define LE16_TO_CPU(x) (x)
904*1b2596b5SMatthias Ringwald #define CPU_TO_LE16(x) (x)
905*1b2596b5SMatthias Ringwald
906*1b2596b5SMatthias Ringwald #define be16_to_cpu(x) Swap16(x)
907*1b2596b5SMatthias Ringwald #define cpu_to_be16(x) Swap16(x)
908*1b2596b5SMatthias Ringwald #define BE16_TO_CPU(x) Swap16(x)
909*1b2596b5SMatthias Ringwald #define CPU_TO_BE16(x) Swap16(x)
910*1b2596b5SMatthias Ringwald
911*1b2596b5SMatthias Ringwald #define le32_to_cpu(x) (x)
912*1b2596b5SMatthias Ringwald #define cpu_to_le32(x) (x)
913*1b2596b5SMatthias Ringwald #define LE32_TO_CPU(x) (x)
914*1b2596b5SMatthias Ringwald #define CPU_TO_LE32(x) (x)
915*1b2596b5SMatthias Ringwald
916*1b2596b5SMatthias Ringwald #define be32_to_cpu(x) swap32(x)
917*1b2596b5SMatthias Ringwald #define cpu_to_be32(x) swap32(x)
918*1b2596b5SMatthias Ringwald #define BE32_TO_CPU(x) swap32(x)
919*1b2596b5SMatthias Ringwald #define CPU_TO_BE32(x) swap32(x)
920*1b2596b5SMatthias Ringwald //! @}
921*1b2596b5SMatthias Ringwald
922*1b2596b5SMatthias Ringwald
923*1b2596b5SMatthias Ringwald /*! \name Endianism Conversion
924*1b2596b5SMatthias Ringwald *
925*1b2596b5SMatthias Ringwald * The same considerations as for clz and ctz apply here but GCC's
926*1b2596b5SMatthias Ringwald * __builtin_bswap_32 and __builtin_bswap_64 do not behave like macros when
927*1b2596b5SMatthias Ringwald * applied to constant expressions, so two sets of macros are defined here:
928*1b2596b5SMatthias Ringwald * - Swap16, Swap32 and Swap64 to apply to constant expressions (values known
929*1b2596b5SMatthias Ringwald * at compile time);
930*1b2596b5SMatthias Ringwald * - swap16, swap32 and swap64 to apply to non-constant expressions (values
931*1b2596b5SMatthias Ringwald * unknown at compile time).
932*1b2596b5SMatthias Ringwald */
933*1b2596b5SMatthias Ringwald //! @{
934*1b2596b5SMatthias Ringwald
935*1b2596b5SMatthias Ringwald /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
936*1b2596b5SMatthias Ringwald *
937*1b2596b5SMatthias Ringwald * \param u16 U16 of which to toggle the endianism.
938*1b2596b5SMatthias Ringwald *
939*1b2596b5SMatthias Ringwald * \return Value resulting from \a u16 with toggled endianism.
940*1b2596b5SMatthias Ringwald *
941*1b2596b5SMatthias Ringwald * \note More optimized if only used with values known at compile time.
942*1b2596b5SMatthias Ringwald */
943*1b2596b5SMatthias Ringwald #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\
944*1b2596b5SMatthias Ringwald ((U16)(u16) << 8)))
945*1b2596b5SMatthias Ringwald
946*1b2596b5SMatthias Ringwald /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
947*1b2596b5SMatthias Ringwald *
948*1b2596b5SMatthias Ringwald * \param u32 U32 of which to toggle the endianism.
949*1b2596b5SMatthias Ringwald *
950*1b2596b5SMatthias Ringwald * \return Value resulting from \a u32 with toggled endianism.
951*1b2596b5SMatthias Ringwald *
952*1b2596b5SMatthias Ringwald * \note More optimized if only used with values known at compile time.
953*1b2596b5SMatthias Ringwald */
954*1b2596b5SMatthias Ringwald #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\
955*1b2596b5SMatthias Ringwald ((U32)Swap16((U32)(u32)) << 16)))
956*1b2596b5SMatthias Ringwald
957*1b2596b5SMatthias Ringwald /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
958*1b2596b5SMatthias Ringwald *
959*1b2596b5SMatthias Ringwald * \param u64 U64 of which to toggle the endianism.
960*1b2596b5SMatthias Ringwald *
961*1b2596b5SMatthias Ringwald * \return Value resulting from \a u64 with toggled endianism.
962*1b2596b5SMatthias Ringwald *
963*1b2596b5SMatthias Ringwald * \note More optimized if only used with values known at compile time.
964*1b2596b5SMatthias Ringwald */
965*1b2596b5SMatthias Ringwald #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\
966*1b2596b5SMatthias Ringwald ((U64)Swap32((U64)(u64)) << 32)))
967*1b2596b5SMatthias Ringwald
968*1b2596b5SMatthias Ringwald /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
969*1b2596b5SMatthias Ringwald *
970*1b2596b5SMatthias Ringwald * \param u16 U16 of which to toggle the endianism.
971*1b2596b5SMatthias Ringwald *
972*1b2596b5SMatthias Ringwald * \return Value resulting from \a u16 with toggled endianism.
973*1b2596b5SMatthias Ringwald *
974*1b2596b5SMatthias Ringwald * \note More optimized if only used with values unknown at compile time.
975*1b2596b5SMatthias Ringwald */
976*1b2596b5SMatthias Ringwald #define swap16(u16) Swap16(u16)
977*1b2596b5SMatthias Ringwald
978*1b2596b5SMatthias Ringwald /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
979*1b2596b5SMatthias Ringwald *
980*1b2596b5SMatthias Ringwald * \param u32 U32 of which to toggle the endianism.
981*1b2596b5SMatthias Ringwald *
982*1b2596b5SMatthias Ringwald * \return Value resulting from \a u32 with toggled endianism.
983*1b2596b5SMatthias Ringwald *
984*1b2596b5SMatthias Ringwald * \note More optimized if only used with values unknown at compile time.
985*1b2596b5SMatthias Ringwald */
986*1b2596b5SMatthias Ringwald #if (defined __GNUC__)
987*1b2596b5SMatthias Ringwald # define swap32(u32) ((U32)__builtin_bswap32((U32)(u32)))
988*1b2596b5SMatthias Ringwald #else
989*1b2596b5SMatthias Ringwald # define swap32(u32) Swap32(u32)
990*1b2596b5SMatthias Ringwald #endif
991*1b2596b5SMatthias Ringwald
992*1b2596b5SMatthias Ringwald /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
993*1b2596b5SMatthias Ringwald *
994*1b2596b5SMatthias Ringwald * \param u64 U64 of which to toggle the endianism.
995*1b2596b5SMatthias Ringwald *
996*1b2596b5SMatthias Ringwald * \return Value resulting from \a u64 with toggled endianism.
997*1b2596b5SMatthias Ringwald *
998*1b2596b5SMatthias Ringwald * \note More optimized if only used with values unknown at compile time.
999*1b2596b5SMatthias Ringwald */
1000*1b2596b5SMatthias Ringwald #if (defined __GNUC__)
1001*1b2596b5SMatthias Ringwald # define swap64(u64) ((U64)__builtin_bswap64((U64)(u64)))
1002*1b2596b5SMatthias Ringwald #else
1003*1b2596b5SMatthias Ringwald # define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\
1004*1b2596b5SMatthias Ringwald ((U64)swap32((U64)(u64)) << 32)))
1005*1b2596b5SMatthias Ringwald #endif
1006*1b2596b5SMatthias Ringwald
1007*1b2596b5SMatthias Ringwald //! @}
1008*1b2596b5SMatthias Ringwald
1009*1b2596b5SMatthias Ringwald
1010*1b2596b5SMatthias Ringwald /*! \name Target Abstraction
1011*1b2596b5SMatthias Ringwald */
1012*1b2596b5SMatthias Ringwald //! @{
1013*1b2596b5SMatthias Ringwald
1014*1b2596b5SMatthias Ringwald #define _GLOBEXT_ extern //!< extern storage-class specifier.
1015*1b2596b5SMatthias Ringwald #define _CONST_TYPE_ const //!< const type qualifier.
1016*1b2596b5SMatthias Ringwald #define _MEM_TYPE_SLOW_ //!< Slow memory type.
1017*1b2596b5SMatthias Ringwald #define _MEM_TYPE_MEDFAST_ //!< Fairly fast memory type.
1018*1b2596b5SMatthias Ringwald #define _MEM_TYPE_FAST_ //!< Fast memory type.
1019*1b2596b5SMatthias Ringwald
1020*1b2596b5SMatthias Ringwald typedef U8 Byte; //!< 8-bit unsigned integer.
1021*1b2596b5SMatthias Ringwald
1022*1b2596b5SMatthias Ringwald #define memcmp_ram2ram memcmp //!< Target-specific memcmp of RAM to RAM.
1023*1b2596b5SMatthias Ringwald #define memcmp_code2ram memcmp //!< Target-specific memcmp of RAM to NVRAM.
1024*1b2596b5SMatthias Ringwald #define memcpy_ram2ram memcpy //!< Target-specific memcpy from RAM to RAM.
1025*1b2596b5SMatthias Ringwald #define memcpy_code2ram memcpy //!< Target-specific memcpy from NVRAM to RAM.
1026*1b2596b5SMatthias Ringwald
1027*1b2596b5SMatthias Ringwald #define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32.
1028*1b2596b5SMatthias Ringwald #define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32.
1029*1b2596b5SMatthias Ringwald #define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32.
1030*1b2596b5SMatthias Ringwald #define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32.
1031*1b2596b5SMatthias Ringwald #define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32.
1032*1b2596b5SMatthias Ringwald #define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32.
1033*1b2596b5SMatthias Ringwald #define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32.
1034*1b2596b5SMatthias Ringwald #define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32.
1035*1b2596b5SMatthias Ringwald
1036*1b2596b5SMatthias Ringwald //! @}
1037*1b2596b5SMatthias Ringwald
1038*1b2596b5SMatthias Ringwald /**
1039*1b2596b5SMatthias Ringwald * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
1040*1b2596b5SMatthias Ringwald * integer arithmetic.
1041*1b2596b5SMatthias Ringwald *
1042*1b2596b5SMatthias Ringwald * \param a An integer
1043*1b2596b5SMatthias Ringwald * \param b Another integer
1044*1b2596b5SMatthias Ringwald *
1045*1b2596b5SMatthias Ringwald * \return (\a a / \a b) rounded up to the nearest integer.
1046*1b2596b5SMatthias Ringwald */
1047*1b2596b5SMatthias Ringwald #define div_ceil(a, b) (((a) + (b) - 1) / (b))
1048*1b2596b5SMatthias Ringwald
1049*1b2596b5SMatthias Ringwald #endif // #ifndef __ASSEMBLY__
1050*1b2596b5SMatthias Ringwald
1051*1b2596b5SMatthias Ringwald
1052*1b2596b5SMatthias Ringwald #if defined(__ICCARM__)
1053*1b2596b5SMatthias Ringwald #define SHORTENUM __packed
1054*1b2596b5SMatthias Ringwald #elif defined(__GNUC__)
1055*1b2596b5SMatthias Ringwald #define SHORTENUM __attribute__((packed))
1056*1b2596b5SMatthias Ringwald #endif
1057*1b2596b5SMatthias Ringwald
1058*1b2596b5SMatthias Ringwald /* No operation */
1059*1b2596b5SMatthias Ringwald #if defined(__ICCARM__)
1060*1b2596b5SMatthias Ringwald #define nop() __no_operation()
1061*1b2596b5SMatthias Ringwald #elif defined(__GNUC__)
1062*1b2596b5SMatthias Ringwald #define nop() (__NOP())
1063*1b2596b5SMatthias Ringwald #endif
1064*1b2596b5SMatthias Ringwald
1065*1b2596b5SMatthias Ringwald #define FLASH_DECLARE(x) const x
1066*1b2596b5SMatthias Ringwald #define FLASH_EXTERN(x) extern const x
1067*1b2596b5SMatthias Ringwald #define PGM_READ_BYTE(x) *(x)
1068*1b2596b5SMatthias Ringwald #define PGM_READ_WORD(x) *(x)
1069*1b2596b5SMatthias Ringwald #define PGM_READ_DWORD(x) *(x)
1070*1b2596b5SMatthias Ringwald #define MEMCPY_ENDIAN memcpy
1071*1b2596b5SMatthias Ringwald #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len))
1072*1b2596b5SMatthias Ringwald
1073*1b2596b5SMatthias Ringwald /*Defines the Flash Storage for the request and response of MAC*/
1074*1b2596b5SMatthias Ringwald #define CMD_ID_OCTET (0)
1075*1b2596b5SMatthias Ringwald
1076*1b2596b5SMatthias Ringwald /* Converting of values from CPU endian to little endian. */
1077*1b2596b5SMatthias Ringwald #define CPU_ENDIAN_TO_LE16(x) (x)
1078*1b2596b5SMatthias Ringwald #define CPU_ENDIAN_TO_LE32(x) (x)
1079*1b2596b5SMatthias Ringwald #define CPU_ENDIAN_TO_LE64(x) (x)
1080*1b2596b5SMatthias Ringwald
1081*1b2596b5SMatthias Ringwald /* Converting of values from little endian to CPU endian. */
1082*1b2596b5SMatthias Ringwald #define LE16_TO_CPU_ENDIAN(x) (x)
1083*1b2596b5SMatthias Ringwald #define LE32_TO_CPU_ENDIAN(x) (x)
1084*1b2596b5SMatthias Ringwald #define LE64_TO_CPU_ENDIAN(x) (x)
1085*1b2596b5SMatthias Ringwald
1086*1b2596b5SMatthias Ringwald /* Converting of constants from little endian to CPU endian. */
1087*1b2596b5SMatthias Ringwald #define CLE16_TO_CPU_ENDIAN(x) (x)
1088*1b2596b5SMatthias Ringwald #define CLE32_TO_CPU_ENDIAN(x) (x)
1089*1b2596b5SMatthias Ringwald #define CLE64_TO_CPU_ENDIAN(x) (x)
1090*1b2596b5SMatthias Ringwald
1091*1b2596b5SMatthias Ringwald /* Converting of constants from CPU endian to little endian. */
1092*1b2596b5SMatthias Ringwald #define CCPU_ENDIAN_TO_LE16(x) (x)
1093*1b2596b5SMatthias Ringwald #define CCPU_ENDIAN_TO_LE32(x) (x)
1094*1b2596b5SMatthias Ringwald #define CCPU_ENDIAN_TO_LE64(x) (x)
1095*1b2596b5SMatthias Ringwald
1096*1b2596b5SMatthias Ringwald #define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src))
1097*1b2596b5SMatthias Ringwald #define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src))
1098*1b2596b5SMatthias Ringwald
1099*1b2596b5SMatthias Ringwald /**
1100*1b2596b5SMatthias Ringwald * @brief Converts a 64-Bit value into a 8 Byte array
1101*1b2596b5SMatthias Ringwald *
1102*1b2596b5SMatthias Ringwald * @param[in] value 64-Bit value
1103*1b2596b5SMatthias Ringwald * @param[out] data Pointer to the 8 Byte array to be updated with 64-Bit value
1104*1b2596b5SMatthias Ringwald * @ingroup apiPalApi
1105*1b2596b5SMatthias Ringwald */
convert_64_bit_to_byte_array(uint64_t value,uint8_t * data)1106*1b2596b5SMatthias Ringwald static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data)
1107*1b2596b5SMatthias Ringwald {
1108*1b2596b5SMatthias Ringwald uint8_t val_index = 0;
1109*1b2596b5SMatthias Ringwald
1110*1b2596b5SMatthias Ringwald while (val_index < 8)
1111*1b2596b5SMatthias Ringwald {
1112*1b2596b5SMatthias Ringwald data[val_index++] = value & 0xFF;
1113*1b2596b5SMatthias Ringwald value = value >> 8;
1114*1b2596b5SMatthias Ringwald }
1115*1b2596b5SMatthias Ringwald }
1116*1b2596b5SMatthias Ringwald
1117*1b2596b5SMatthias Ringwald /**
1118*1b2596b5SMatthias Ringwald * @brief Converts a 16-Bit value into a 2 Byte array
1119*1b2596b5SMatthias Ringwald *
1120*1b2596b5SMatthias Ringwald * @param[in] value 16-Bit value
1121*1b2596b5SMatthias Ringwald * @param[out] data Pointer to the 2 Byte array to be updated with 16-Bit value
1122*1b2596b5SMatthias Ringwald * @ingroup apiPalApi
1123*1b2596b5SMatthias Ringwald */
convert_16_bit_to_byte_array(uint16_t value,uint8_t * data)1124*1b2596b5SMatthias Ringwald static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1125*1b2596b5SMatthias Ringwald {
1126*1b2596b5SMatthias Ringwald data[0] = value & 0xFF;
1127*1b2596b5SMatthias Ringwald data[1] = (value >> 8) & 0xFF;
1128*1b2596b5SMatthias Ringwald }
1129*1b2596b5SMatthias Ringwald
1130*1b2596b5SMatthias Ringwald /* Converts a 16-Bit value into a 2 Byte array */
convert_spec_16_bit_to_byte_array(uint16_t value,uint8_t * data)1131*1b2596b5SMatthias Ringwald static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data)
1132*1b2596b5SMatthias Ringwald {
1133*1b2596b5SMatthias Ringwald data[0] = value & 0xFF;
1134*1b2596b5SMatthias Ringwald data[1] = (value >> 8) & 0xFF;
1135*1b2596b5SMatthias Ringwald }
1136*1b2596b5SMatthias Ringwald
1137*1b2596b5SMatthias Ringwald /* Converts a 16-Bit value into a 2 Byte array */
convert_16_bit_to_byte_address(uint16_t value,uint8_t * data)1138*1b2596b5SMatthias Ringwald static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data)
1139*1b2596b5SMatthias Ringwald {
1140*1b2596b5SMatthias Ringwald data[0] = value & 0xFF;
1141*1b2596b5SMatthias Ringwald data[1] = (value >> 8) & 0xFF;
1142*1b2596b5SMatthias Ringwald }
1143*1b2596b5SMatthias Ringwald
1144*1b2596b5SMatthias Ringwald /*
1145*1b2596b5SMatthias Ringwald * @brief Converts a 2 Byte array into a 16-Bit value
1146*1b2596b5SMatthias Ringwald *
1147*1b2596b5SMatthias Ringwald * @param data Specifies the pointer to the 2 Byte array
1148*1b2596b5SMatthias Ringwald *
1149*1b2596b5SMatthias Ringwald * @return 16-Bit value
1150*1b2596b5SMatthias Ringwald * @ingroup apiPalApi
1151*1b2596b5SMatthias Ringwald */
convert_byte_array_to_16_bit(uint8_t * data)1152*1b2596b5SMatthias Ringwald static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data)
1153*1b2596b5SMatthias Ringwald {
1154*1b2596b5SMatthias Ringwald return (data[0] | ((uint16_t)data[1] << 8));
1155*1b2596b5SMatthias Ringwald }
1156*1b2596b5SMatthias Ringwald
1157*1b2596b5SMatthias Ringwald /* Converts a 8 Byte array into a 32-Bit value */
convert_byte_array_to_32_bit(uint8_t * data)1158*1b2596b5SMatthias Ringwald static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data)
1159*1b2596b5SMatthias Ringwald {
1160*1b2596b5SMatthias Ringwald union
1161*1b2596b5SMatthias Ringwald {
1162*1b2596b5SMatthias Ringwald uint32_t u32;
1163*1b2596b5SMatthias Ringwald uint8_t u8[8];
1164*1b2596b5SMatthias Ringwald }long_addr;
1165*1b2596b5SMatthias Ringwald uint8_t index;
1166*1b2596b5SMatthias Ringwald for (index = 0; index < 4; index++)
1167*1b2596b5SMatthias Ringwald {
1168*1b2596b5SMatthias Ringwald long_addr.u8[index] = *data++;
1169*1b2596b5SMatthias Ringwald }
1170*1b2596b5SMatthias Ringwald return long_addr.u32;
1171*1b2596b5SMatthias Ringwald }
1172*1b2596b5SMatthias Ringwald
1173*1b2596b5SMatthias Ringwald /**
1174*1b2596b5SMatthias Ringwald * @brief Converts a 8 Byte array into a 64-Bit value
1175*1b2596b5SMatthias Ringwald *
1176*1b2596b5SMatthias Ringwald * @param data Specifies the pointer to the 8 Byte array
1177*1b2596b5SMatthias Ringwald *
1178*1b2596b5SMatthias Ringwald * @return 64-Bit value
1179*1b2596b5SMatthias Ringwald * @ingroup apiPalApi
1180*1b2596b5SMatthias Ringwald */
convert_byte_array_to_64_bit(uint8_t * data)1181*1b2596b5SMatthias Ringwald static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data)
1182*1b2596b5SMatthias Ringwald {
1183*1b2596b5SMatthias Ringwald union
1184*1b2596b5SMatthias Ringwald {
1185*1b2596b5SMatthias Ringwald uint64_t u64;
1186*1b2596b5SMatthias Ringwald uint8_t u8[8];
1187*1b2596b5SMatthias Ringwald } long_addr;
1188*1b2596b5SMatthias Ringwald
1189*1b2596b5SMatthias Ringwald uint8_t val_index;
1190*1b2596b5SMatthias Ringwald
1191*1b2596b5SMatthias Ringwald for (val_index = 0; val_index < 8; val_index++)
1192*1b2596b5SMatthias Ringwald {
1193*1b2596b5SMatthias Ringwald long_addr.u8[val_index] = *data++;
1194*1b2596b5SMatthias Ringwald }
1195*1b2596b5SMatthias Ringwald
1196*1b2596b5SMatthias Ringwald return long_addr.u64;
1197*1b2596b5SMatthias Ringwald }
1198*1b2596b5SMatthias Ringwald /**
1199*1b2596b5SMatthias Ringwald * \}
1200*1b2596b5SMatthias Ringwald */
1201*1b2596b5SMatthias Ringwald
1202*1b2596b5SMatthias Ringwald #endif /* UTILS_COMPILER_H */
1203