1 /***************************************************************************//**
\r
3 * @brief Emlib general purpose utilities.
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
33 #ifndef __SILICON_LABS_EM_COMMON_H__
\r
34 #define __SILICON_LABS_EM_COMMON_H__
\r
36 #include "em_device.h"
\r
37 #include <stdbool.h>
\r
43 /***************************************************************************//**
\r
44 * @addtogroup EM_Library
\r
46 ******************************************************************************/
\r
48 /***************************************************************************//**
\r
49 * @addtogroup COMMON
\r
50 * @brief Emlib general purpose utilities.
\r
52 ******************************************************************************/
\r
54 #if !defined(__GNUC__)
\r
56 /** Macro for getting minimum value. */
\r
57 #define EFM32_MIN(a, b) ((a) < (b) ? (a) : (b))
\r
58 /** Macro for getting maximum value. */
\r
59 #define EFM32_MAX(a, b) ((a) > (b) ? (a) : (b))
\r
61 /** Macros for handling packed structs. */
\r
62 #define STRINGIZE(X) #X
\r
63 #define EFM32_PACK_START(X) _Pragma( STRINGIZE( pack( X ) ) )
\r
64 #define EFM32_PACK_END() _Pragma( "pack()" )
\r
65 #define __attribute__(...)
\r
68 /** Macros for handling aligned structs. */
\r
69 #define EFM32_ALIGN(X) __align(X)
\r
72 /** Macros for handling aligned structs. */
\r
73 #define EFM32_ALIGN(X) _Pragma( STRINGIZE( data_alignment=X ) )
\r
76 #else // !defined(__GNUC__)
\r
78 /** Macro for getting minimum value. No sideeffects, a and b are evaluated once only. */
\r
79 #define EFM32_MIN(a, b) ({ __typeof__(a) _a = (a); __typeof__(b) _b = (b); _a < _b ? _a : _b; })
\r
80 /** Macro for getting maximum value. No sideeffects, a and b are evaluated once only. */
\r
81 #define EFM32_MAX(a, b) ({ __typeof__(a) _a = (a); __typeof__(b) _b = (b); _a > _b ? _a : _b; })
\r
83 /** Macro for handling packed structs.
\r
84 * @n Use this macro before the struct definition.
\r
85 * @n X denotes the maximum alignment of struct members. X is not supported on
\r
86 * gcc, gcc always use 1 byte maximum alignment.
\r
88 #define EFM32_PACK_START( x )
\r
90 /** Macro for handling packed structs.
\r
91 * @n Use this macro after the struct definition.
\r
92 * @n On gcc add __attribute__ ((packed)) after the closing } of the struct
\r
95 #define EFM32_PACK_END()
\r
97 /** Macro for aligning a variable.
\r
98 * @n Use this macro before the variable definition.
\r
99 * @n X denotes the storage alignment value in bytes.
\r
100 * @n On gcc use __attribute__ ((aligned(X))) before the ; on normal variables.
\r
101 * Use __attribute__ ((aligned(X))) before the opening { on struct variables.
\r
103 #define EFM32_ALIGN(X)
\r
105 #endif // !defined(__GNUC__)
\r
107 /***************************************************************************//**
\r
109 * Count trailing number of zero's.
\r
112 * Disabling SWDClk will disable the debug interface, which may result in
\r
113 * a lockout if done early in startup (before debugger is able to halt core).
\r
116 * Data value to check for number of trailing zero bits.
\r
119 * Number of trailing zero's in value.
\r
120 ******************************************************************************/
\r
121 __STATIC_INLINE uint32_t EFM32_CTZ(uint32_t value)
\r
123 #if (__CORTEX_M >= 3)
\r
124 return __CLZ(__RBIT(value));
\r
128 for(zeros=0; (zeros<32) && ((value&0x1) == 0); zeros++, value>>=1);
\r
133 /** @} (end addtogroup COMMON) */
\r
134 /** @} (end addtogroup EM_Library) */
\r
140 #endif /* __SILICON_LABS_EM_COMMON_H__ */
\r