nrfx_common.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * Copyright (c) 2017 - 2018, 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 checking if given lengths of EasyDMA transfers do not exceed
  120. * the limit of the specified peripheral.
  121. *
  122. * @param peripheral Peripheral to check the lengths against.
  123. * @param length1 First length to be checked.
  124. * @param length2 Second length to be checked (pass 0 if not needed).
  125. *
  126. * @return
  127. */
  128. #define NRFX_EASYDMA_LENGTH_VALIDATE(peripheral, length1, length2) \
  129. (((length1) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))) && \
  130. ((length2) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))))
  131. /**@brief Macro for waiting until condition is met.
  132. *
  133. * @param[in] condition Condition to meet.
  134. * @param[in] attempts Maximum number of condition checks. Must not be 0.
  135. * @param[in] delay_us Delay between consecutive checks, in microseconds.
  136. * @param[out] result Boolean variable to store the result of the wait process.
  137. * Set to true if the condition is met or false otherwise.
  138. */
  139. #define NRFX_WAIT_FOR(condition, attempts, delay_us, result) \
  140. do { \
  141. result = false; \
  142. uint32_t remaining_attempts = (attempts); \
  143. do { \
  144. if (condition) \
  145. { \
  146. result = true; \
  147. break; \
  148. } \
  149. NRFX_DELAY_US(delay_us); \
  150. } while (--remaining_attempts); \
  151. } while(0)
  152. /**
  153. * @brief Macro for getting the interrupt number assigned to a specific
  154. * peripheral.
  155. *
  156. * In Nordic SoCs the IRQ number assigned to a peripheral is equal to the ID
  157. * of this peripheral, and there is a direct relationship between this ID and
  158. * the peripheral base address, i.e. the address of a fixed block of 0x1000
  159. * bytes of address space assigned to this peripheral.
  160. * See the chapter "Peripheral interface" (sections "Peripheral ID" and
  161. * "Interrupts") in the product specification of a given SoC.
  162. *
  163. * @param[in] base_addr Peripheral base address or pointer.
  164. *
  165. * @return Interrupt number associated with the specified peripheral.
  166. */
  167. #define NRFX_IRQ_NUMBER_GET(base_addr) (uint8_t)((uint32_t)(base_addr) >> 12)
  168. /**
  169. * @brief IRQ handler type.
  170. */
  171. typedef void (* nrfx_irq_handler_t)(void);
  172. /**
  173. * @brief Driver state.
  174. */
  175. typedef enum
  176. {
  177. NRFX_DRV_STATE_UNINITIALIZED, ///< Uninitialized.
  178. NRFX_DRV_STATE_INITIALIZED, ///< Initialized but powered off.
  179. NRFX_DRV_STATE_POWERED_ON, ///< Initialized and powered on.
  180. } nrfx_drv_state_t;
  181. /**
  182. * @brief Function for checking if an object is placed in the Data RAM region.
  183. *
  184. * Several peripherals (the ones using EasyDMA) require the transfer buffers
  185. * to be placed in the Data RAM region. This function can be used to check if
  186. * this condition is met.
  187. *
  188. * @param[in] p_object Pointer to an object whose location is to be checked.
  189. *
  190. * @retval true If the pointed object is located in the Data RAM region.
  191. * @retval false Otherwise.
  192. */
  193. __STATIC_INLINE bool nrfx_is_in_ram(void const * p_object);
  194. /**
  195. * @brief Function for getting the interrupt number for a specific peripheral.
  196. *
  197. * @param[in] p_reg Peripheral base pointer.
  198. *
  199. * @return Interrupt number associated with the pointed peripheral.
  200. */
  201. __STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg);
  202. /**
  203. * @brief Function for converting an INTEN register bit position to the
  204. * corresponding event identifier.
  205. *
  206. * The event identifier is the offset between the event register address and
  207. * the peripheral base address, and is equal (thus, can be directly cast) to
  208. * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
  209. * @param bit INTEN register bit position.
  210. *
  211. * @return Event identifier.
  212. *
  213. * @sa nrfx_event_to_bitpos
  214. */
  215. __STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit);
  216. /**
  217. * @brief Function for converting an event identifier to the corresponding
  218. * INTEN register bit position.
  219. *
  220. * The event identifier is the offset between the event register address and
  221. * the peripheral base address, and is equal (thus, can be directly cast) to
  222. * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
  223. *
  224. * @param event Event identifier.
  225. *
  226. * @return INTEN register bit position.
  227. *
  228. * @sa nrfx_bitpos_to_event
  229. */
  230. __STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event);
  231. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  232. __STATIC_INLINE bool nrfx_is_in_ram(void const * p_object)
  233. {
  234. return ((((uint32_t)p_object) & 0xE0000000u) == 0x20000000u);
  235. }
  236. __STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg)
  237. {
  238. return (IRQn_Type)NRFX_IRQ_NUMBER_GET(p_reg);
  239. }
  240. __STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit)
  241. {
  242. static const uint32_t event_reg_offset = 0x100u;
  243. return event_reg_offset + (bit * sizeof(uint32_t));
  244. }
  245. __STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event)
  246. {
  247. static const uint32_t event_reg_offset = 0x100u;
  248. return (event - event_reg_offset) / sizeof(uint32_t);
  249. }
  250. #endif
  251. /** @} */
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. #endif // NRFX_COMMON_H__