|
| 1 | +/* |
| 2 | + * Copyright (c) 2008-2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +/** @file |
| 8 | + * @brief Common defines and macros for firmware developed by Nordic Semiconductor. |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef NORDIC_COMMON_H__ |
| 12 | +#define NORDIC_COMMON_H__ |
| 13 | + |
| 14 | +#ifdef __cplusplus |
| 15 | +extern "C" { |
| 16 | +#endif |
| 17 | + |
| 18 | +/** |
| 19 | + * @brief Check if selected module is enabled |
| 20 | + * |
| 21 | + * This is save function for driver enable checking. |
| 22 | + * Correct from Lint point of view (not using default of undefined value). |
| 23 | + * |
| 24 | + * Usage: |
| 25 | + * @code |
| 26 | + #if NRF_MODULE_ENABLED(UART) |
| 27 | + ... |
| 28 | + #endif |
| 29 | + * @endcode |
| 30 | + * |
| 31 | + * @param module The module name. |
| 32 | + * |
| 33 | + * @retval 1 The macro <module>_ENABLE is defined and is non-zero. |
| 34 | + * @retval 0 The macro <module>_ENABLE is not defined or it equals zero. |
| 35 | + * |
| 36 | + * @note |
| 37 | + * This macro intentionally does not implement second expansion level. |
| 38 | + * The name of the module to be checked has to be given directly as a parameter. |
| 39 | + * And given parameter would be connected with @c _ENABLED postfix directly |
| 40 | + * without evaluating its value. |
| 41 | + */ |
| 42 | +#ifdef NRF_MODULE_ENABLE_ALL |
| 43 | +#warning "Do not use NRF_MODULE_ENABLE_ALL for real builds." |
| 44 | +#define NRF_MODULE_ENABLED(module) 1 |
| 45 | +#else |
| 46 | +#define NRF_MODULE_ENABLED(module) ((defined(module##_ENABLED) && (module##_ENABLED)) ? 1 : 0) |
| 47 | +#endif |
| 48 | +/** The upper 8 bits of a 32 bit value */ |
| 49 | +#define MSB_32(a) (((a)&0xFF000000) >> 24) |
| 50 | +/** The lower 8 bits (of a 32 bit value) */ |
| 51 | +#define LSB_32(a) ((a)&0x000000FF) |
| 52 | + |
| 53 | +/** The upper 8 bits of a 16 bit value */ |
| 54 | +#define MSB_16(a) (((a)&0xFF00) >> 8) |
| 55 | +/** The lower 8 bits (of a 16 bit value) */ |
| 56 | +#define LSB_16(a) ((a)&0x00FF) |
| 57 | + |
| 58 | +/** Leaves the minimum of the two 32-bit arguments */ |
| 59 | +#define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 60 | +/** Leaves the maximum of the two 32-bit arguments */ |
| 61 | +#define MAX(a, b) ((a) < (b) ? (b) : (a)) |
| 62 | + |
| 63 | +/**@brief Concatenates two parameters. |
| 64 | + * |
| 65 | + * It realizes two level expansion to make it sure that all the parameters |
| 66 | + * are actually expanded before gluing them together. |
| 67 | + * |
| 68 | + * @param p1 First parameter to concatenating |
| 69 | + * @param p2 Second parameter to concatenating |
| 70 | + * |
| 71 | + * @return Two parameters glued together. |
| 72 | + * They have to create correct C mnemonic in other case |
| 73 | + * preprocessor error would be generated. |
| 74 | + * |
| 75 | + * @sa CONCAT_3 |
| 76 | + */ |
| 77 | +#define CONCAT_2(p1, p2) CONCAT_2_(p1, p2) |
| 78 | +/** Auxiliary macro used by @ref CONCAT_2 */ |
| 79 | +#define CONCAT_2_(p1, p2) p1##p2 |
| 80 | + |
| 81 | +/**@brief Concatenates three parameters. |
| 82 | + * |
| 83 | + * It realizes two level expansion to make it sure that all the parameters |
| 84 | + * are actually expanded before gluing them together. |
| 85 | + * |
| 86 | + * @param p1 First parameter to concatenating |
| 87 | + * @param p2 Second parameter to concatenating |
| 88 | + * @param p3 Third parameter to concatenating |
| 89 | + * |
| 90 | + * @return Three parameters glued together. |
| 91 | + * They have to create correct C mnemonic in other case |
| 92 | + * preprocessor error would be generated. |
| 93 | + * |
| 94 | + * @sa CONCAT_2 |
| 95 | + */ |
| 96 | +#define CONCAT_3(p1, p2, p3) CONCAT_3_(p1, p2, p3) |
| 97 | +/** Auxiliary macro used by @ref CONCAT_3 */ |
| 98 | +#define CONCAT_3_(p1, p2, p3) p1##p2##p3 |
| 99 | + |
| 100 | +#define STRINGIFY_(val) #val |
| 101 | +/** Converts a macro argument into a character constant. |
| 102 | + */ |
| 103 | +#define STRINGIFY(val) STRINGIFY_(val) |
| 104 | + |
| 105 | +/** Counts number of elements inside the array |
| 106 | + */ |
| 107 | +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) |
| 108 | + |
| 109 | +/**@brief Set a bit in the uint32 word. |
| 110 | + * |
| 111 | + * @param[in] W Word whose bit is being set. |
| 112 | + * @param[in] B Bit number in the word to be set. |
| 113 | + */ |
| 114 | +#define SET_BIT(W, B) ((W) |= (uint32_t)(1U << (B))) |
| 115 | + |
| 116 | +/**@brief Clears a bit in the uint32 word. |
| 117 | + * |
| 118 | + * @param[in] W Word whose bit is to be cleared. |
| 119 | + * @param[in] B Bit number in the word to be cleared. |
| 120 | + */ |
| 121 | +#define CLR_BIT(W, B) ((W) &= (~(uint32_t)(1U << (B)))) |
| 122 | + |
| 123 | +/**@brief Checks if a bit is set. |
| 124 | + * |
| 125 | + * @param[in] W Word whose bit is to be checked. |
| 126 | + * @param[in] B Bit number in the word to be checked. |
| 127 | + * |
| 128 | + * @retval 1 if bit is set. |
| 129 | + * @retval 0 if bit is not set. |
| 130 | + */ |
| 131 | +#define IS_SET(W, B) (((W) >> (B)) & 1) |
| 132 | + |
| 133 | +#define BIT_0 0x01 /**< The value of bit 0 */ |
| 134 | +#define BIT_1 0x02 /**< The value of bit 1 */ |
| 135 | +#define BIT_2 0x04 /**< The value of bit 2 */ |
| 136 | +#define BIT_3 0x08 /**< The value of bit 3 */ |
| 137 | +#define BIT_4 0x10 /**< The value of bit 4 */ |
| 138 | +#define BIT_5 0x20 /**< The value of bit 5 */ |
| 139 | +#define BIT_6 0x40 /**< The value of bit 6 */ |
| 140 | +#define BIT_7 0x80 /**< The value of bit 7 */ |
| 141 | +#define BIT_8 0x0100 /**< The value of bit 8 */ |
| 142 | +#define BIT_9 0x0200 /**< The value of bit 9 */ |
| 143 | +#define BIT_10 0x0400 /**< The value of bit 10 */ |
| 144 | +#define BIT_11 0x0800 /**< The value of bit 11 */ |
| 145 | +#define BIT_12 0x1000 /**< The value of bit 12 */ |
| 146 | +#define BIT_13 0x2000 /**< The value of bit 13 */ |
| 147 | +#define BIT_14 0x4000 /**< The value of bit 14 */ |
| 148 | +#define BIT_15 0x8000 /**< The value of bit 15 */ |
| 149 | +#define BIT_16 0x00010000 /**< The value of bit 16 */ |
| 150 | +#define BIT_17 0x00020000 /**< The value of bit 17 */ |
| 151 | +#define BIT_18 0x00040000 /**< The value of bit 18 */ |
| 152 | +#define BIT_19 0x00080000 /**< The value of bit 19 */ |
| 153 | +#define BIT_20 0x00100000 /**< The value of bit 20 */ |
| 154 | +#define BIT_21 0x00200000 /**< The value of bit 21 */ |
| 155 | +#define BIT_22 0x00400000 /**< The value of bit 22 */ |
| 156 | +#define BIT_23 0x00800000 /**< The value of bit 23 */ |
| 157 | +#define BIT_24 0x01000000 /**< The value of bit 24 */ |
| 158 | +#define BIT_25 0x02000000 /**< The value of bit 25 */ |
| 159 | +#define BIT_26 0x04000000 /**< The value of bit 26 */ |
| 160 | +#define BIT_27 0x08000000 /**< The value of bit 27 */ |
| 161 | +#define BIT_28 0x10000000 /**< The value of bit 28 */ |
| 162 | +#define BIT_29 0x20000000 /**< The value of bit 29 */ |
| 163 | +#define BIT_30 0x40000000 /**< The value of bit 30 */ |
| 164 | +#define BIT_31 0x80000000 /**< The value of bit 31 */ |
| 165 | + |
| 166 | +#define UNUSED_VARIABLE(X) ((void)(X)) |
| 167 | +#define UNUSED_PARAMETER(X) UNUSED_VARIABLE(X) |
| 168 | +#define UNUSED_RETURN_VALUE(X) UNUSED_VARIABLE(X) |
| 169 | + |
| 170 | +#ifdef __cplusplus |
| 171 | +} |
| 172 | +#endif |
| 173 | + |
| 174 | +#endif /* NORDIC_COMMON_H__ */ |
0 commit comments