nordic_common.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Copyright (c) 2008 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. * @brief Common defines and macros for firmware developed by Nordic Semiconductor.
  42. */
  43. #ifndef NORDIC_COMMON_H__
  44. #define NORDIC_COMMON_H__
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @brief Check if selected module is enabled
  50. *
  51. * This is save function for driver enable checking.
  52. * Correct from Lint point of view (not using default of undefined value).
  53. *
  54. * Usage:
  55. * @code
  56. #if NRF_MODULE_ENABLED(UART)
  57. ...
  58. #endif
  59. * @endcode
  60. *
  61. * @param module The module name.
  62. *
  63. * @retval 1 The macro <module>_ENABLE is defined and is non-zero.
  64. * @retval 0 The macro <module>_ENABLE is not defined or it equals zero.
  65. *
  66. * @note
  67. * This macro intentionally does not implement second expansion level.
  68. * The name of the module to be checked has to be given directly as a parameter.
  69. * And given parameter would be connected with @c _ENABLED postfix directly
  70. * without evaluating its value.
  71. */
  72. //lint -emacro(491,NRF_MODULE_ENABLED) // Suppers warning 491 "non-standard use of 'defined' preprocessor operator"
  73. #ifdef NRF_MODULE_ENABLE_ALL
  74. #warning "Do not use NRF_MODULE_ENABLE_ALL for real builds."
  75. #define NRF_MODULE_ENABLED(module) 1
  76. #else
  77. #define NRF_MODULE_ENABLED(module) \
  78. ((defined(module ## _ENABLED) && (module ## _ENABLED)) ? 1 : 0)
  79. #endif
  80. /** The upper 8 bits of a 32 bit value */
  81. //lint -emacro(572,MSB_32) // Suppress warning 572 "Excessive shift value"
  82. #define MSB_32(a) (((a) & 0xFF000000) >> 24)
  83. /** The lower 8 bits (of a 32 bit value) */
  84. #define LSB_32(a) ((a) & 0x000000FF)
  85. /** The upper 8 bits of a 16 bit value */
  86. //lint -emacro(572,MSB_16) // Suppress warning 572 "Excessive shift value"
  87. #define MSB_16(a) (((a) & 0xFF00) >> 8)
  88. /** The lower 8 bits (of a 16 bit value) */
  89. #define LSB_16(a) ((a) & 0x00FF)
  90. /** Leaves the minimum of the two 32-bit arguments */
  91. /*lint -emacro(506, MIN) */ /* Suppress "Constant value Boolean */
  92. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  93. /** Leaves the maximum of the two 32-bit arguments */
  94. /*lint -emacro(506, MAX) */ /* Suppress "Constant value Boolean */
  95. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  96. /**@brief Concatenates two parameters.
  97. *
  98. * It realizes two level expansion to make it sure that all the parameters
  99. * are actually expanded before gluing them together.
  100. *
  101. * @param p1 First parameter to concatenating
  102. * @param p2 Second parameter to concatenating
  103. *
  104. * @return Two parameters glued together.
  105. * They have to create correct C mnemonic in other case
  106. * preprocessor error would be generated.
  107. *
  108. * @sa CONCAT_3
  109. */
  110. #define CONCAT_2(p1, p2) CONCAT_2_(p1, p2)
  111. /** Auxiliary macro used by @ref CONCAT_2 */
  112. #define CONCAT_2_(p1, p2) p1##p2
  113. /**@brief Concatenates three parameters.
  114. *
  115. * It realizes two level expansion to make it sure that all the parameters
  116. * are actually expanded before gluing them together.
  117. *
  118. * @param p1 First parameter to concatenating
  119. * @param p2 Second parameter to concatenating
  120. * @param p3 Third parameter to concatenating
  121. *
  122. * @return Three parameters glued together.
  123. * They have to create correct C mnemonic in other case
  124. * preprocessor error would be generated.
  125. *
  126. * @sa CONCAT_2
  127. */
  128. #define CONCAT_3(p1, p2, p3) CONCAT_3_(p1, p2, p3)
  129. /** Auxiliary macro used by @ref CONCAT_3 */
  130. #define CONCAT_3_(p1, p2, p3) p1##p2##p3
  131. #define STRINGIFY_(val) #val
  132. /** Converts a macro argument into a character constant.
  133. */
  134. #define STRINGIFY(val) STRINGIFY_(val)
  135. /** Counts number of elements inside the array
  136. */
  137. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  138. /**@brief Set a bit in the uint32 word.
  139. *
  140. * @param[in] W Word whose bit is being set.
  141. * @param[in] B Bit number in the word to be set.
  142. */
  143. #define SET_BIT(W, B) ((W) |= (uint32_t)(1U << (B)))
  144. /**@brief Clears a bit in the uint32 word.
  145. *
  146. * @param[in] W Word whose bit is to be cleared.
  147. * @param[in] B Bit number in the word to be cleared.
  148. */
  149. #define CLR_BIT(W, B) ((W) &= (~(uint32_t)(1U << (B))))
  150. /**@brief Checks if a bit is set.
  151. *
  152. * @param[in] W Word whose bit is to be checked.
  153. * @param[in] B Bit number in the word to be checked.
  154. *
  155. * @retval 1 if bit is set.
  156. * @retval 0 if bit is not set.
  157. */
  158. #define IS_SET(W, B) (((W) >> (B)) & 1)
  159. #define BIT_0 0x01 /**< The value of bit 0 */
  160. #define BIT_1 0x02 /**< The value of bit 1 */
  161. #define BIT_2 0x04 /**< The value of bit 2 */
  162. #define BIT_3 0x08 /**< The value of bit 3 */
  163. #define BIT_4 0x10 /**< The value of bit 4 */
  164. #define BIT_5 0x20 /**< The value of bit 5 */
  165. #define BIT_6 0x40 /**< The value of bit 6 */
  166. #define BIT_7 0x80 /**< The value of bit 7 */
  167. #define BIT_8 0x0100 /**< The value of bit 8 */
  168. #define BIT_9 0x0200 /**< The value of bit 9 */
  169. #define BIT_10 0x0400 /**< The value of bit 10 */
  170. #define BIT_11 0x0800 /**< The value of bit 11 */
  171. #define BIT_12 0x1000 /**< The value of bit 12 */
  172. #define BIT_13 0x2000 /**< The value of bit 13 */
  173. #define BIT_14 0x4000 /**< The value of bit 14 */
  174. #define BIT_15 0x8000 /**< The value of bit 15 */
  175. #define BIT_16 0x00010000 /**< The value of bit 16 */
  176. #define BIT_17 0x00020000 /**< The value of bit 17 */
  177. #define BIT_18 0x00040000 /**< The value of bit 18 */
  178. #define BIT_19 0x00080000 /**< The value of bit 19 */
  179. #define BIT_20 0x00100000 /**< The value of bit 20 */
  180. #define BIT_21 0x00200000 /**< The value of bit 21 */
  181. #define BIT_22 0x00400000 /**< The value of bit 22 */
  182. #define BIT_23 0x00800000 /**< The value of bit 23 */
  183. #define BIT_24 0x01000000 /**< The value of bit 24 */
  184. #define BIT_25 0x02000000 /**< The value of bit 25 */
  185. #define BIT_26 0x04000000 /**< The value of bit 26 */
  186. #define BIT_27 0x08000000 /**< The value of bit 27 */
  187. #define BIT_28 0x10000000 /**< The value of bit 28 */
  188. #define BIT_29 0x20000000 /**< The value of bit 29 */
  189. #define BIT_30 0x40000000 /**< The value of bit 30 */
  190. #define BIT_31 0x80000000 /**< The value of bit 31 */
  191. #define UNUSED_VARIABLE(X) ((void)(X))
  192. #define UNUSED_PARAMETER(X) UNUSED_VARIABLE(X)
  193. #define UNUSED_RETURN_VALUE(X) UNUSED_VARIABLE(X)
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif // NORDIC_COMMON_H__