nrfx_common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * Copyright (c) 2017 - 2019, 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. #ifndef NRFX_COMMON_H__
  41. #define NRFX_COMMON_H__
  42. #include <stdint.h>
  43. #include <stddef.h>
  44. #include <stdbool.h>
  45. #include <nrf.h>
  46. #include <nrf_peripherals.h>
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**
  51. * @defgroup nrfx_common Common module
  52. * @{
  53. * @ingroup nrfx
  54. * @brief Common module.
  55. */
  56. /**
  57. * @brief Macro for checking if the specified identifier is defined and it has
  58. * a non-zero value.
  59. *
  60. * Normally, preprocessors treat all undefined identifiers as having the value
  61. * zero. However, some tools, like static code analyzers, may issue a warning
  62. * when such identifier is evaluated. This macro gives the possibility to suppress
  63. * such warnings only in places where this macro is used for evaluation, not in
  64. * the whole analyzed code.
  65. */
  66. #define NRFX_CHECK(module_enabled) (module_enabled)
  67. /**
  68. * @brief Macro for concatenating two tokens in macro expansion.
  69. *
  70. * @note This macro is expanded in two steps so that tokens given as macros
  71. * themselves are fully expanded before they are merged.
  72. *
  73. * @param p1 First token.
  74. * @param p2 Second token.
  75. *
  76. * @return The two tokens merged into one, unless they cannot together form
  77. * a valid token (in such case, the preprocessor issues a warning and
  78. * does not perform the concatenation).
  79. *
  80. * @sa NRFX_CONCAT_3
  81. */
  82. #define NRFX_CONCAT_2(p1, p2) NRFX_CONCAT_2_(p1, p2)
  83. /**
  84. * @brief Internal macro used by @ref NRFX_CONCAT_2 to perform the expansion
  85. * in two steps.
  86. */
  87. #define NRFX_CONCAT_2_(p1, p2) p1 ## p2
  88. /**
  89. * @brief Macro for concatenating three tokens in macro expansion.
  90. *
  91. * @note This macro is expanded in two steps so that tokens given as macros
  92. * themselves are fully expanded before they are merged.
  93. *
  94. * @param p1 First token.
  95. * @param p2 Second token.
  96. * @param p3 Third token.
  97. *
  98. * @return The three tokens merged into one, unless they cannot together form
  99. * a valid token (in such case, the preprocessor issues a warning and
  100. * does not perform the concatenation).
  101. *
  102. * @sa NRFX_CONCAT_2
  103. */
  104. #define NRFX_CONCAT_3(p1, p2, p3) NRFX_CONCAT_3_(p1, p2, p3)
  105. /**
  106. * @brief Internal macro used by @ref NRFX_CONCAT_3 to perform the expansion
  107. * in two steps.
  108. */
  109. #define NRFX_CONCAT_3_(p1, p2, p3) p1 ## p2 ## p3
  110. /**@brief Macro for performing rounded integer division (as opposed to
  111. * truncating the result).
  112. *
  113. * @param a Numerator.
  114. * @param b Denominator.
  115. *
  116. * @return Rounded (integer) result of dividing @c a by @c b.
  117. */
  118. #define NRFX_ROUNDED_DIV(a, b) (((a) + ((b) / 2)) / (b))
  119. /**@brief Macro for performing integer division, making sure the result is rounded up.
  120. *
  121. * @details A typical use case for this macro is to compute the number of objects
  122. * with size @c b required to hold @c a number of bytes.
  123. *
  124. * @param a Numerator.
  125. * @param b Denominator.
  126. *
  127. * @return Integer result of dividing @c a by @c b, rounded up.
  128. */
  129. #define NRFX_CEIL_DIV(a, b) ((((a) - 1) / (b)) + 1)
  130. /**
  131. * @brief Macro for getting the number of elements in an array.
  132. *
  133. * @param array Name of the array.
  134. *
  135. * @return Array element count.
  136. */
  137. #define NRFX_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
  138. /**@brief Macro for checking if given lengths of EasyDMA transfers do not exceed
  139. * the limit of the specified peripheral.
  140. *
  141. * @param peripheral Peripheral to check the lengths against.
  142. * @param length1 First length to be checked.
  143. * @param length2 Second length to be checked (pass 0 if not needed).
  144. *
  145. * @return
  146. */
  147. #define NRFX_EASYDMA_LENGTH_VALIDATE(peripheral, length1, length2) \
  148. (((length1) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))) && \
  149. ((length2) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))))
  150. /**@brief Macro for waiting until condition is met.
  151. *
  152. * @param[in] condition Condition to meet.
  153. * @param[in] attempts Maximum number of condition checks. Must not be 0.
  154. * @param[in] delay_us Delay between consecutive checks, in microseconds.
  155. * @param[out] result Boolean variable to store the result of the wait process.
  156. * Set to true if the condition is met or false otherwise.
  157. */
  158. #define NRFX_WAIT_FOR(condition, attempts, delay_us, result) \
  159. do { \
  160. result = false; \
  161. uint32_t remaining_attempts = (attempts); \
  162. do { \
  163. if (condition) \
  164. { \
  165. result = true; \
  166. break; \
  167. } \
  168. NRFX_DELAY_US(delay_us); \
  169. } while (--remaining_attempts); \
  170. } while(0)
  171. /**
  172. * @brief Macro for getting the ID number of the specified peripheral.
  173. *
  174. * For peripherals in Nordic SoCs, there is a direct relationship between their
  175. * ID numbers and their base addresses. See the chapter "Peripheral interface"
  176. * (section "Peripheral ID") in the Product Specification.
  177. *
  178. * @param[in] base_addr Peripheral base address or pointer.
  179. *
  180. * @return ID number associated with the specified peripheral.
  181. */
  182. #define NRFX_PERIPHERAL_ID_GET(base_addr) (uint8_t)((uint32_t)(base_addr) >> 12)
  183. /**
  184. * @brief Macro for getting the interrupt number assigned to a specific
  185. * peripheral.
  186. *
  187. * For peripherals in Nordic SoCs, the IRQ number assigned to a peripheral is
  188. * equal to its ID number. See the chapter "Peripheral interface" (sections
  189. * "Peripheral ID" and "Interrupts") in the Product Specification.
  190. *
  191. * @param[in] base_addr Peripheral base address or pointer.
  192. *
  193. * @return Interrupt number associated with the specified peripheral.
  194. */
  195. #define NRFX_IRQ_NUMBER_GET(base_addr) NRFX_PERIPHERAL_ID_GET(base_addr)
  196. /**
  197. * @brief IRQ handler type.
  198. */
  199. typedef void (* nrfx_irq_handler_t)(void);
  200. /**
  201. * @brief Driver state.
  202. */
  203. typedef enum
  204. {
  205. NRFX_DRV_STATE_UNINITIALIZED, ///< Uninitialized.
  206. NRFX_DRV_STATE_INITIALIZED, ///< Initialized but powered off.
  207. NRFX_DRV_STATE_POWERED_ON, ///< Initialized and powered on.
  208. } nrfx_drv_state_t;
  209. /**
  210. * @brief Function for checking if an object is placed in the Data RAM region.
  211. *
  212. * Several peripherals (the ones using EasyDMA) require the transfer buffers
  213. * to be placed in the Data RAM region. This function can be used to check if
  214. * this condition is met.
  215. *
  216. * @param[in] p_object Pointer to an object whose location is to be checked.
  217. *
  218. * @retval true If the pointed object is located in the Data RAM region.
  219. * @retval false Otherwise.
  220. */
  221. __STATIC_INLINE bool nrfx_is_in_ram(void const * p_object);
  222. /**
  223. * @brief Function for checking if an object is aligned to a 32-bit word
  224. *
  225. * Several peripherals (the ones using EasyDMA) require the transfer buffers
  226. * to be aligned to a 32-bit word. This function can be used to check if
  227. * this condition is met.
  228. *
  229. * @param[in] p_object Pointer to an object whose location is to be checked.
  230. *
  231. * @retval true if the pointed object is aligned to a 32-bit word.
  232. * @retval false otherwise.
  233. */
  234. __STATIC_INLINE bool nrfx_is_word_aligned(void const * p_object);
  235. /**
  236. * @brief Function for getting the interrupt number for a specific peripheral.
  237. *
  238. * @param[in] p_reg Peripheral base pointer.
  239. *
  240. * @return Interrupt number associated with the pointed peripheral.
  241. */
  242. __STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg);
  243. /**
  244. * @brief Function for converting an INTEN register bit position to the
  245. * corresponding event identifier.
  246. *
  247. * The event identifier is the offset between the event register address and
  248. * the peripheral base address, and is equal (thus, can be directly cast) to
  249. * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
  250. * @param bit INTEN register bit position.
  251. *
  252. * @return Event identifier.
  253. *
  254. * @sa nrfx_event_to_bitpos
  255. */
  256. __STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit);
  257. /**
  258. * @brief Function for converting an event identifier to the corresponding
  259. * INTEN register bit position.
  260. *
  261. * The event identifier is the offset between the event register address and
  262. * the peripheral base address, and is equal (thus, can be directly cast) to
  263. * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
  264. *
  265. * @param event Event identifier.
  266. *
  267. * @return INTEN register bit position.
  268. *
  269. * @sa nrfx_bitpos_to_event
  270. */
  271. __STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event);
  272. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  273. __STATIC_INLINE bool nrfx_is_in_ram(void const * p_object)
  274. {
  275. return ((((uint32_t)p_object) & 0xE0000000u) == 0x20000000u);
  276. }
  277. __STATIC_INLINE bool nrfx_is_word_aligned(void const * p_object)
  278. {
  279. return ((((uint32_t)p_object) & 0x3u) == 0u);
  280. }
  281. __STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg)
  282. {
  283. return (IRQn_Type)NRFX_IRQ_NUMBER_GET(p_reg);
  284. }
  285. __STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit)
  286. {
  287. static const uint32_t event_reg_offset = 0x100u;
  288. return event_reg_offset + (bit * sizeof(uint32_t));
  289. }
  290. __STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event)
  291. {
  292. static const uint32_t event_reg_offset = 0x100u;
  293. return (event - event_reg_offset) / sizeof(uint32_t);
  294. }
  295. #endif
  296. /** @} */
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. #endif // NRFX_COMMON_H__