nrfx_glue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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_GLUE_H__
  41. #define NRFX_GLUE_H__
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * @defgroup nrfx_glue nrfx_glue.h
  47. * @{
  48. * @ingroup nrfx
  49. *
  50. * @brief This file contains macros that should be implemented according to
  51. * the needs of the host environment into which @em nrfx is integrated.
  52. */
  53. #include <legacy/apply_old_config.h>
  54. #include <soc/nrfx_irqs.h>
  55. //------------------------------------------------------------------------------
  56. #include <nrf_assert.h>
  57. /**
  58. * @brief Macro for placing a runtime assertion.
  59. *
  60. * @param expression Expression to evaluate.
  61. */
  62. #define NRFX_ASSERT(expression) ASSERT(expression)
  63. #include <app_util.h>
  64. /**
  65. * @brief Macro for placing a compile time assertion.
  66. *
  67. * @param expression Expression to evaluate.
  68. */
  69. #define NRFX_STATIC_ASSERT(expression) STATIC_ASSERT(expression)
  70. //------------------------------------------------------------------------------
  71. #ifdef NRF51
  72. #ifdef SOFTDEVICE_PRESENT
  73. #define INTERRUPT_PRIORITY_IS_VALID(pri) (((pri) == 1) || ((pri) == 3))
  74. #else
  75. #define INTERRUPT_PRIORITY_IS_VALID(pri) ((pri) < 4)
  76. #endif //SOFTDEVICE_PRESENT
  77. #else
  78. #ifdef SOFTDEVICE_PRESENT
  79. #define INTERRUPT_PRIORITY_IS_VALID(pri) ((((pri) > 1) && ((pri) < 4)) || \
  80. (((pri) > 4) && ((pri) < 8)))
  81. #else
  82. #define INTERRUPT_PRIORITY_IS_VALID(pri) ((pri) < 8)
  83. #endif //SOFTDEVICE_PRESENT
  84. #endif //NRF52
  85. /**
  86. * @brief Macro for setting the priority of a specific IRQ.
  87. *
  88. * @param irq_number IRQ number.
  89. * @param priority Priority to set.
  90. */
  91. #define NRFX_IRQ_PRIORITY_SET(irq_number, priority) \
  92. _NRFX_IRQ_PRIORITY_SET(irq_number, priority)
  93. static inline void _NRFX_IRQ_PRIORITY_SET(IRQn_Type irq_number,
  94. uint8_t priority)
  95. {
  96. ASSERT(INTERRUPT_PRIORITY_IS_VALID(priority));
  97. NVIC_SetPriority(irq_number, priority);
  98. }
  99. /**
  100. * @brief Macro for enabling a specific IRQ.
  101. *
  102. * @param irq_number IRQ number.
  103. */
  104. #define NRFX_IRQ_ENABLE(irq_number) _NRFX_IRQ_ENABLE(irq_number)
  105. static inline void _NRFX_IRQ_ENABLE(IRQn_Type irq_number)
  106. {
  107. NVIC_EnableIRQ(irq_number);
  108. }
  109. /**
  110. * @brief Macro for checking if a specific IRQ is enabled.
  111. *
  112. * @param irq_number IRQ number.
  113. *
  114. * @retval true If the IRQ is enabled.
  115. * @retval false Otherwise.
  116. */
  117. #define NRFX_IRQ_IS_ENABLED(irq_number) _NRFX_IRQ_IS_ENABLED(irq_number)
  118. static inline bool _NRFX_IRQ_IS_ENABLED(IRQn_Type irq_number)
  119. {
  120. return 0 != (NVIC->ISER[irq_number / 32] & (1UL << (irq_number % 32)));
  121. }
  122. /**
  123. * @brief Macro for disabling a specific IRQ.
  124. *
  125. * @param irq_number IRQ number.
  126. */
  127. #define NRFX_IRQ_DISABLE(irq_number) _NRFX_IRQ_DISABLE(irq_number)
  128. static inline void _NRFX_IRQ_DISABLE(IRQn_Type irq_number)
  129. {
  130. NVIC_DisableIRQ(irq_number);
  131. }
  132. /**
  133. * @brief Macro for setting a specific IRQ as pending.
  134. *
  135. * @param irq_number IRQ number.
  136. */
  137. #define NRFX_IRQ_PENDING_SET(irq_number) _NRFX_IRQ_PENDING_SET(irq_number)
  138. static inline void _NRFX_IRQ_PENDING_SET(IRQn_Type irq_number)
  139. {
  140. NVIC_SetPendingIRQ(irq_number);
  141. }
  142. /**
  143. * @brief Macro for clearing the pending status of a specific IRQ.
  144. *
  145. * @param irq_number IRQ number.
  146. */
  147. #define NRFX_IRQ_PENDING_CLEAR(irq_number) _NRFX_IRQ_PENDING_CLEAR(irq_number)
  148. static inline void _NRFX_IRQ_PENDING_CLEAR(IRQn_Type irq_number)
  149. {
  150. NVIC_ClearPendingIRQ(irq_number);
  151. }
  152. /**
  153. * @brief Macro for checking the pending status of a specific IRQ.
  154. *
  155. * @retval true If the IRQ is pending.
  156. * @retval false Otherwise.
  157. */
  158. #define NRFX_IRQ_IS_PENDING(irq_number) _NRFX_IRQ_IS_PENDING(irq_number)
  159. static inline bool _NRFX_IRQ_IS_PENDING(IRQn_Type irq_number)
  160. {
  161. return (NVIC_GetPendingIRQ(irq_number) == 1);
  162. }
  163. #include <nordic_common.h>
  164. #include <app_util_platform.h>
  165. /**
  166. * @brief Macro for entering into a critical section.
  167. */
  168. #define NRFX_CRITICAL_SECTION_ENTER() CRITICAL_REGION_ENTER()
  169. /**
  170. * @brief Macro for exiting from a critical section.
  171. */
  172. #define NRFX_CRITICAL_SECTION_EXIT() CRITICAL_REGION_EXIT()
  173. //------------------------------------------------------------------------------
  174. /**
  175. * @brief When set to a non-zero value, this macro specifies that
  176. * @ref nrfx_coredep_delay_us uses a precise DWT-based solution.
  177. * A compilation error is generated if the DWT unit is not present
  178. * in the SoC used.
  179. */
  180. #define NRFX_DELAY_DWT_BASED 0
  181. #include <soc/nrfx_coredep.h>
  182. #define NRFX_DELAY_US(us_time) nrfx_coredep_delay_us(us_time)
  183. //------------------------------------------------------------------------------
  184. #include <soc/nrfx_atomic.h>
  185. /**
  186. * @brief Atomic 32 bit unsigned type.
  187. */
  188. #define nrfx_atomic_t nrfx_atomic_u32_t
  189. /**
  190. * @brief Stores value to an atomic object and returns previously stored value.
  191. *
  192. * @param[in] p_data Atomic memory pointer.
  193. * @param[in] value Value to store.
  194. *
  195. * @return Old value stored into atomic object.
  196. */
  197. #define NRFX_ATOMIC_FETCH_STORE(p_data, value) nrfx_atomic_u32_fetch_store(p_data, value)
  198. /**
  199. * @brief Performs logical OR operation on an atomic object and returns previously stored value.
  200. *
  201. * @param[in] p_data Atomic memory pointer.
  202. * @param[in] value Value of second operand of OR operation.
  203. *
  204. * @return Old value stored into atomic object.
  205. */
  206. #define NRFX_ATOMIC_FETCH_OR(p_data, value) nrfx_atomic_u32_fetch_or(p_data, value)
  207. /**
  208. * @brief Performs logical AND operation on an atomic object and returns previously stored value.
  209. *
  210. * @param[in] p_data Atomic memory pointer.
  211. * @param[in] value Value of second operand of AND operation.
  212. *
  213. * @return Old value stored into atomic object.
  214. */
  215. #define NRFX_ATOMIC_FETCH_AND(p_data, value) nrfx_atomic_u32_fetch_and(p_data, value)
  216. /**
  217. * @brief Performs logical XOR operation on an atomic object and returns previously stored value.
  218. *
  219. * @param[in] p_data Atomic memory pointer.
  220. * @param[in] value Value of second operand of XOR operation.
  221. *
  222. * @return Old value stored into atomic object.
  223. */
  224. #define NRFX_ATOMIC_FETCH_XOR(p_data, value) nrfx_atomic_u32_fetch_xor(p_data, value)
  225. /**
  226. * @brief Performs logical ADD operation on an atomic object and returns previously stored value.
  227. *
  228. * @param[in] p_data Atomic memory pointer.
  229. * @param[in] value Value of second operand of ADD operation.
  230. *
  231. * @return Old value stored into atomic object.
  232. */
  233. #define NRFX_ATOMIC_FETCH_ADD(p_data, value) nrfx_atomic_u32_fetch_add(p_data, value)
  234. /**
  235. * @brief Performs logical SUB operation on an atomic object and returns previously stored value.
  236. *
  237. * @param[in] p_data Atomic memory pointer.
  238. * @param[in] value Value of second operand of SUB operation.
  239. *
  240. * @return Old value stored into atomic object.
  241. */
  242. #define NRFX_ATOMIC_FETCH_SUB(p_data, value) nrfx_atomic_u32_fetch_sub(p_data, value)
  243. //------------------------------------------------------------------------------
  244. #ifndef NRFX_CUSTOM_ERROR_CODES
  245. #include <sdk_errors.h>
  246. /**
  247. * @brief When set to a non-zero value, this macro specifies that the
  248. * @ref nrfx_error_codes and the @ref ret_code_t type itself are defined
  249. * in a customized way and the default definitions from @c <nrfx_error.h>
  250. * should not be used.
  251. */
  252. #define NRFX_CUSTOM_ERROR_CODES 1
  253. typedef ret_code_t nrfx_err_t;
  254. #define NRFX_SUCCESS NRF_SUCCESS
  255. #define NRFX_ERROR_INTERNAL NRF_ERROR_INTERNAL
  256. #define NRFX_ERROR_NO_MEM NRF_ERROR_NO_MEM
  257. #define NRFX_ERROR_NOT_SUPPORTED NRF_ERROR_NOT_SUPPORTED
  258. #define NRFX_ERROR_INVALID_PARAM NRF_ERROR_INVALID_PARAM
  259. #define NRFX_ERROR_INVALID_STATE NRF_ERROR_INVALID_STATE
  260. #define NRFX_ERROR_INVALID_LENGTH NRF_ERROR_INVALID_LENGTH
  261. #define NRFX_ERROR_TIMEOUT NRF_ERROR_TIMEOUT
  262. #define NRFX_ERROR_FORBIDDEN NRF_ERROR_FORBIDDEN
  263. #define NRFX_ERROR_NULL NRF_ERROR_NULL
  264. #define NRFX_ERROR_INVALID_ADDR NRF_ERROR_INVALID_ADDR
  265. #define NRFX_ERROR_BUSY NRF_ERROR_BUSY
  266. #define NRFX_ERROR_ALREADY_INITIALIZED NRF_ERROR_MODULE_ALREADY_INITIALIZED
  267. #define NRFX_ERROR_DRV_TWI_ERR_OVERRUN NRF_ERROR_DRV_TWI_ERR_OVERRUN
  268. #define NRFX_ERROR_DRV_TWI_ERR_ANACK NRF_ERROR_DRV_TWI_ERR_ANACK
  269. #define NRFX_ERROR_DRV_TWI_ERR_DNACK NRF_ERROR_DRV_TWI_ERR_DNACK
  270. #endif // NRFX_CUSTOM_ERROR_CODES
  271. //------------------------------------------------------------------------------
  272. #include <sdk_resources.h>
  273. /**
  274. * @brief Bitmask defining PPI channels reserved to be used outside of nrfx.
  275. */
  276. #define NRFX_PPI_CHANNELS_USED NRF_PPI_CHANNELS_USED
  277. /**
  278. * @brief Bitmask defining PPI groups reserved to be used outside of nrfx.
  279. */
  280. #define NRFX_PPI_GROUPS_USED NRF_PPI_GROUPS_USED
  281. /**
  282. * @brief Bitmask defining SWI instances reserved to be used outside of nrfx.
  283. */
  284. #define NRFX_SWI_USED NRF_SWI_USED
  285. /**
  286. * @brief Bitmask defining TIMER instances reserved to be used outside of nrfx.
  287. */
  288. #define NRFX_TIMERS_USED NRF_TIMERS_USED
  289. /** @} */
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif // NRFX_GLUE_H__