nrfx_common.h 12 KB

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