nrfx_glue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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_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. #define NRFX_GET_MINI_NS(void) nrfx_coredep_get_mini_ns(void)
  184. #define NRFX_DELAY_NS(ns_time) nrfx_coredep_delay_ns(ns_time)
  185. //------------------------------------------------------------------------------
  186. #include <soc/nrfx_atomic.h>
  187. /**
  188. * @brief Atomic 32 bit unsigned type.
  189. */
  190. #define nrfx_atomic_t nrfx_atomic_u32_t
  191. /**
  192. * @brief Stores value to an atomic object and returns previously stored value.
  193. *
  194. * @param[in] p_data Atomic memory pointer.
  195. * @param[in] value Value to store.
  196. *
  197. * @return Old value stored into atomic object.
  198. */
  199. #define NRFX_ATOMIC_FETCH_STORE(p_data, value) nrfx_atomic_u32_fetch_store(p_data, value)
  200. /**
  201. * @brief Performs logical OR operation on an atomic object and returns previously stored value.
  202. *
  203. * @param[in] p_data Atomic memory pointer.
  204. * @param[in] value Value of second operand of OR operation.
  205. *
  206. * @return Old value stored into atomic object.
  207. */
  208. #define NRFX_ATOMIC_FETCH_OR(p_data, value) nrfx_atomic_u32_fetch_or(p_data, value)
  209. /**
  210. * @brief Performs logical AND operation on an atomic object and returns previously stored value.
  211. *
  212. * @param[in] p_data Atomic memory pointer.
  213. * @param[in] value Value of second operand of AND operation.
  214. *
  215. * @return Old value stored into atomic object.
  216. */
  217. #define NRFX_ATOMIC_FETCH_AND(p_data, value) nrfx_atomic_u32_fetch_and(p_data, value)
  218. /**
  219. * @brief Performs logical XOR operation on an atomic object and returns previously stored value.
  220. *
  221. * @param[in] p_data Atomic memory pointer.
  222. * @param[in] value Value of second operand of XOR operation.
  223. *
  224. * @return Old value stored into atomic object.
  225. */
  226. #define NRFX_ATOMIC_FETCH_XOR(p_data, value) nrfx_atomic_u32_fetch_xor(p_data, value)
  227. /**
  228. * @brief Performs logical ADD operation on an atomic object and returns previously stored value.
  229. *
  230. * @param[in] p_data Atomic memory pointer.
  231. * @param[in] value Value of second operand of ADD operation.
  232. *
  233. * @return Old value stored into atomic object.
  234. */
  235. #define NRFX_ATOMIC_FETCH_ADD(p_data, value) nrfx_atomic_u32_fetch_add(p_data, value)
  236. /**
  237. * @brief Performs logical SUB operation on an atomic object and returns previously stored value.
  238. *
  239. * @param[in] p_data Atomic memory pointer.
  240. * @param[in] value Value of second operand of SUB operation.
  241. *
  242. * @return Old value stored into atomic object.
  243. */
  244. #define NRFX_ATOMIC_FETCH_SUB(p_data, value) nrfx_atomic_u32_fetch_sub(p_data, value)
  245. //------------------------------------------------------------------------------
  246. #ifndef NRFX_CUSTOM_ERROR_CODES
  247. #include <sdk_errors.h>
  248. /**
  249. * @brief When set to a non-zero value, this macro specifies that the
  250. * @ref nrfx_error_codes and the @ref ret_code_t type itself are defined
  251. * in a customized way and the default definitions from @c <nrfx_error.h>
  252. * should not be used.
  253. */
  254. #define NRFX_CUSTOM_ERROR_CODES 1
  255. typedef ret_code_t nrfx_err_t;
  256. #define NRFX_SUCCESS NRF_SUCCESS
  257. #define NRFX_ERROR_INTERNAL NRF_ERROR_INTERNAL
  258. #define NRFX_ERROR_NO_MEM NRF_ERROR_NO_MEM
  259. #define NRFX_ERROR_NOT_SUPPORTED NRF_ERROR_NOT_SUPPORTED
  260. #define NRFX_ERROR_INVALID_PARAM NRF_ERROR_INVALID_PARAM
  261. #define NRFX_ERROR_INVALID_STATE NRF_ERROR_INVALID_STATE
  262. #define NRFX_ERROR_INVALID_LENGTH NRF_ERROR_INVALID_LENGTH
  263. #define NRFX_ERROR_TIMEOUT NRF_ERROR_TIMEOUT
  264. #define NRFX_ERROR_FORBIDDEN NRF_ERROR_FORBIDDEN
  265. #define NRFX_ERROR_NULL NRF_ERROR_NULL
  266. #define NRFX_ERROR_INVALID_ADDR NRF_ERROR_INVALID_ADDR
  267. #define NRFX_ERROR_BUSY NRF_ERROR_BUSY
  268. #define NRFX_ERROR_ALREADY_INITIALIZED NRF_ERROR_MODULE_ALREADY_INITIALIZED
  269. #define NRFX_ERROR_DRV_TWI_ERR_OVERRUN NRF_ERROR_DRV_TWI_ERR_OVERRUN
  270. #define NRFX_ERROR_DRV_TWI_ERR_ANACK NRF_ERROR_DRV_TWI_ERR_ANACK
  271. #define NRFX_ERROR_DRV_TWI_ERR_DNACK NRF_ERROR_DRV_TWI_ERR_DNACK
  272. #endif // NRFX_CUSTOM_ERROR_CODES
  273. //------------------------------------------------------------------------------
  274. #include <sdk_resources.h>
  275. /**
  276. * @brief Bitmask defining PPI channels reserved to be used outside of nrfx.
  277. */
  278. #define NRFX_PPI_CHANNELS_USED NRF_PPI_CHANNELS_USED
  279. /**
  280. * @brief Bitmask defining PPI groups reserved to be used outside of nrfx.
  281. */
  282. #define NRFX_PPI_GROUPS_USED NRF_PPI_GROUPS_USED
  283. /**
  284. * @brief Bitmask defining SWI instances reserved to be used outside of nrfx.
  285. */
  286. #define NRFX_SWI_USED NRF_SWI_USED
  287. /**
  288. * @brief Bitmask defining TIMER instances reserved to be used outside of nrfx.
  289. */
  290. #define NRFX_TIMERS_USED NRF_TIMERS_USED
  291. /** @} */
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif // NRFX_GLUE_H__