nrf_nvic.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (c) Nordic Semiconductor ASA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form, except as embedded into a Nordic
  12. * Semiconductor ASA integrated circuit in a product or a software update for
  13. * such product, must reproduce the above copyright notice, this list of
  14. * conditions and the following disclaimer in the documentation and/or other
  15. * materials provided with the distribution.
  16. *
  17. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * 4. This software, with or without modification, must only be used with a
  22. * Nordic Semiconductor ASA integrated circuit.
  23. *
  24. * 5. Any software provided in binary form under this license must not be reverse
  25. * engineered, decompiled, modified and/or disassembled.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  36. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /**
  39. * @defgroup nrf_nvic_api SoftDevice NVIC API
  40. * @{
  41. *
  42. * @note In order to use this module, the following code has to be added to a .c file:
  43. * \code
  44. * nrf_nvic_state_t nrf_nvic_state = {0};
  45. * \endcode
  46. *
  47. * @note Definitions and declarations starting with __ (double underscore) in this header file are
  48. * not intended for direct use by the application.
  49. *
  50. * @brief APIs for the accessing NVIC when using a SoftDevice.
  51. *
  52. */
  53. #ifndef NRF_NVIC_H__
  54. #define NRF_NVIC_H__
  55. #include <stdint.h>
  56. #include "nrf.h"
  57. #include "nrf_svc.h"
  58. #include "nrf_error.h"
  59. #include "nrf_error_soc.h"
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. /**@addtogroup NRF_NVIC_DEFINES Defines
  64. * @{ */
  65. /**@defgroup NRF_NVIC_ISER_DEFINES SoftDevice NVIC internal definitions
  66. * @{ */
  67. #define __NRF_NVIC_NVMC_IRQn (30) /**< The peripheral ID of the NVMC. IRQ numbers are used to identify peripherals, but the NVMC doesn't have an IRQ number in the MDK. */
  68. #define __NRF_NVIC_ISER_COUNT (2) /**< The number of ISER/ICER registers in the NVIC that are used. */
  69. /**@brief Interrupt priority levels used by the SoftDevice. */
  70. #define __NRF_NVIC_SD_IRQ_PRIOS ((uint8_t)( \
  71. (1U << 0) /**< Priority level high .*/ \
  72. | (1U << 1) /**< Priority level medium. */ \
  73. | (1U << 4) /**< Priority level low. */ \
  74. ))
  75. /**@brief Interrupt priority levels available to the application. */
  76. #define __NRF_NVIC_APP_IRQ_PRIOS ((uint8_t)~__NRF_NVIC_SD_IRQ_PRIOS)
  77. /**@brief Interrupts used by the SoftDevice, with IRQn in the range 0-31. */
  78. #define __NRF_NVIC_SD_IRQS_0 ((uint32_t)( \
  79. (1U << POWER_CLOCK_IRQn) \
  80. | (1U << RADIO_IRQn) \
  81. | (1U << RTC0_IRQn) \
  82. | (1U << TIMER0_IRQn) \
  83. | (1U << RNG_IRQn) \
  84. | (1U << ECB_IRQn) \
  85. | (1U << CCM_AAR_IRQn) \
  86. | (1U << TEMP_IRQn) \
  87. | (1U << __NRF_NVIC_NVMC_IRQn) \
  88. | (1U << (uint32_t)SWI5_IRQn) \
  89. ))
  90. /**@brief Interrupts used by the SoftDevice, with IRQn in the range 32-63. */
  91. #define __NRF_NVIC_SD_IRQS_1 ((uint32_t)0)
  92. /**@brief Interrupts available for to application, with IRQn in the range 0-31. */
  93. #define __NRF_NVIC_APP_IRQS_0 (~__NRF_NVIC_SD_IRQS_0)
  94. /**@brief Interrupts available for to application, with IRQn in the range 32-63. */
  95. #define __NRF_NVIC_APP_IRQS_1 (~__NRF_NVIC_SD_IRQS_1)
  96. /**@} */
  97. /**@} */
  98. /**@addtogroup NRF_NVIC_VARIABLES Variables
  99. * @{ */
  100. /**@brief Type representing the state struct for the SoftDevice NVIC module. */
  101. typedef struct
  102. {
  103. uint32_t volatile __irq_masks[__NRF_NVIC_ISER_COUNT]; /**< IRQs enabled by the application in the NVIC. */
  104. uint32_t volatile __cr_flag; /**< Non-zero if already in a critical region */
  105. } nrf_nvic_state_t;
  106. /**@brief Variable keeping the state for the SoftDevice NVIC module. This must be declared in an
  107. * application source file. */
  108. extern nrf_nvic_state_t nrf_nvic_state;
  109. /**@} */
  110. /**@addtogroup NRF_NVIC_INTERNAL_FUNCTIONS SoftDevice NVIC internal functions
  111. * @{ */
  112. /**@brief Disables IRQ interrupts globally, including the SoftDevice's interrupts.
  113. *
  114. * @retval The value of PRIMASK prior to disabling the interrupts.
  115. */
  116. __STATIC_INLINE int __sd_nvic_irq_disable(void);
  117. /**@brief Enables IRQ interrupts globally, including the SoftDevice's interrupts.
  118. */
  119. __STATIC_INLINE void __sd_nvic_irq_enable(void);
  120. /**@brief Checks if IRQn is available to application
  121. * @param[in] IRQn IRQ to check
  122. *
  123. * @retval 1 (true) if the IRQ to check is available to the application
  124. */
  125. __STATIC_INLINE uint32_t __sd_nvic_app_accessible_irq(IRQn_Type IRQn);
  126. /**@brief Checks if priority is available to application
  127. * @param[in] priority priority to check
  128. *
  129. * @retval 1 (true) if the priority to check is available to the application
  130. */
  131. __STATIC_INLINE uint32_t __sd_nvic_is_app_accessible_priority(uint32_t priority);
  132. /**@} */
  133. /**@addtogroup NRF_NVIC_FUNCTIONS SoftDevice NVIC public functions
  134. * @{ */
  135. /**@brief Enable External Interrupt.
  136. * @note Corresponds to NVIC_EnableIRQ in CMSIS.
  137. *
  138. * @pre IRQn is valid and not reserved by the stack.
  139. *
  140. * @param[in] IRQn See the NVIC_EnableIRQ documentation in CMSIS.
  141. *
  142. * @retval ::NRF_SUCCESS The interrupt was enabled.
  143. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE The interrupt is not available for the application.
  144. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED The interrupt has a priority not available for the application.
  145. */
  146. __STATIC_INLINE uint32_t sd_nvic_EnableIRQ(IRQn_Type IRQn);
  147. /**@brief Disable External Interrupt.
  148. * @note Corresponds to NVIC_DisableIRQ in CMSIS.
  149. *
  150. * @pre IRQn is valid and not reserved by the stack.
  151. *
  152. * @param[in] IRQn See the NVIC_DisableIRQ documentation in CMSIS.
  153. *
  154. * @retval ::NRF_SUCCESS The interrupt was disabled.
  155. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE The interrupt is not available for the application.
  156. */
  157. __STATIC_INLINE uint32_t sd_nvic_DisableIRQ(IRQn_Type IRQn);
  158. /**@brief Get Pending Interrupt.
  159. * @note Corresponds to NVIC_GetPendingIRQ in CMSIS.
  160. *
  161. * @pre IRQn is valid and not reserved by the stack.
  162. *
  163. * @param[in] IRQn See the NVIC_GetPendingIRQ documentation in CMSIS.
  164. * @param[out] p_pending_irq Return value from NVIC_GetPendingIRQ.
  165. *
  166. * @retval ::NRF_SUCCESS The interrupt is available for the application.
  167. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application.
  168. */
  169. __STATIC_INLINE uint32_t sd_nvic_GetPendingIRQ(IRQn_Type IRQn, uint32_t * p_pending_irq);
  170. /**@brief Set Pending Interrupt.
  171. * @note Corresponds to NVIC_SetPendingIRQ in CMSIS.
  172. *
  173. * @pre IRQn is valid and not reserved by the stack.
  174. *
  175. * @param[in] IRQn See the NVIC_SetPendingIRQ documentation in CMSIS.
  176. *
  177. * @retval ::NRF_SUCCESS The interrupt is set pending.
  178. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application.
  179. */
  180. __STATIC_INLINE uint32_t sd_nvic_SetPendingIRQ(IRQn_Type IRQn);
  181. /**@brief Clear Pending Interrupt.
  182. * @note Corresponds to NVIC_ClearPendingIRQ in CMSIS.
  183. *
  184. * @pre IRQn is valid and not reserved by the stack.
  185. *
  186. * @param[in] IRQn See the NVIC_ClearPendingIRQ documentation in CMSIS.
  187. *
  188. * @retval ::NRF_SUCCESS The interrupt pending flag is cleared.
  189. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application.
  190. */
  191. __STATIC_INLINE uint32_t sd_nvic_ClearPendingIRQ(IRQn_Type IRQn);
  192. /**@brief Set Interrupt Priority.
  193. * @note Corresponds to NVIC_SetPriority in CMSIS.
  194. *
  195. * @pre IRQn is valid and not reserved by the stack.
  196. * @pre Priority is valid and not reserved by the stack.
  197. *
  198. * @param[in] IRQn See the NVIC_SetPriority documentation in CMSIS.
  199. * @param[in] priority A valid IRQ priority for use by the application.
  200. *
  201. * @retval ::NRF_SUCCESS The interrupt and priority level is available for the application.
  202. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application.
  203. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED The interrupt priority is not available for the application.
  204. */
  205. __STATIC_INLINE uint32_t sd_nvic_SetPriority(IRQn_Type IRQn, uint32_t priority);
  206. /**@brief Get Interrupt Priority.
  207. * @note Corresponds to NVIC_GetPriority in CMSIS.
  208. *
  209. * @pre IRQn is valid and not reserved by the stack.
  210. *
  211. * @param[in] IRQn See the NVIC_GetPriority documentation in CMSIS.
  212. * @param[out] p_priority Return value from NVIC_GetPriority.
  213. *
  214. * @retval ::NRF_SUCCESS The interrupt priority is returned in p_priority.
  215. * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE - IRQn is not available for the application.
  216. */
  217. __STATIC_INLINE uint32_t sd_nvic_GetPriority(IRQn_Type IRQn, uint32_t * p_priority);
  218. /**@brief System Reset.
  219. * @note Corresponds to NVIC_SystemReset in CMSIS.
  220. *
  221. * @retval ::NRF_ERROR_SOC_NVIC_SHOULD_NOT_RETURN
  222. */
  223. __STATIC_INLINE uint32_t sd_nvic_SystemReset(void);
  224. /**@brief Enter critical region.
  225. *
  226. * @post Application interrupts will be disabled.
  227. * @note sd_nvic_critical_region_enter() and ::sd_nvic_critical_region_exit() must be called in matching pairs inside each
  228. * execution context
  229. * @sa sd_nvic_critical_region_exit
  230. *
  231. * @param[out] p_is_nested_critical_region If 1, the application is now in a nested critical region.
  232. *
  233. * @retval ::NRF_SUCCESS
  234. */
  235. __STATIC_INLINE uint32_t sd_nvic_critical_region_enter(uint8_t * p_is_nested_critical_region);
  236. /**@brief Exit critical region.
  237. *
  238. * @pre Application has entered a critical region using ::sd_nvic_critical_region_enter.
  239. * @post If not in a nested critical region, the application interrupts will restored to the state before ::sd_nvic_critical_region_enter was called.
  240. *
  241. * @param[in] is_nested_critical_region If this is set to 1, the critical region won't be exited. @sa sd_nvic_critical_region_enter.
  242. *
  243. * @retval ::NRF_SUCCESS
  244. */
  245. __STATIC_INLINE uint32_t sd_nvic_critical_region_exit(uint8_t is_nested_critical_region);
  246. /**@} */
  247. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  248. __STATIC_INLINE int __sd_nvic_irq_disable(void)
  249. {
  250. int pm = __get_PRIMASK();
  251. __disable_irq();
  252. return pm;
  253. }
  254. __STATIC_INLINE void __sd_nvic_irq_enable(void)
  255. {
  256. __enable_irq();
  257. }
  258. __STATIC_INLINE uint32_t __sd_nvic_app_accessible_irq(IRQn_Type IRQn)
  259. {
  260. if (IRQn < 32)
  261. {
  262. return ((1UL<<IRQn) & __NRF_NVIC_APP_IRQS_0) != 0;
  263. }
  264. else if (IRQn < 64)
  265. {
  266. return ((1UL<<(IRQn-32)) & __NRF_NVIC_APP_IRQS_1) != 0;
  267. }
  268. else
  269. {
  270. return 1;
  271. }
  272. }
  273. __STATIC_INLINE uint32_t __sd_nvic_is_app_accessible_priority(uint32_t priority)
  274. {
  275. if( (priority >= (1 << __NVIC_PRIO_BITS))
  276. || (((1 << priority) & __NRF_NVIC_APP_IRQ_PRIOS) == 0)
  277. )
  278. {
  279. return 0;
  280. }
  281. return 1;
  282. }
  283. __STATIC_INLINE uint32_t sd_nvic_EnableIRQ(IRQn_Type IRQn)
  284. {
  285. if (!__sd_nvic_app_accessible_irq(IRQn))
  286. {
  287. return NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE;
  288. }
  289. if (!__sd_nvic_is_app_accessible_priority(NVIC_GetPriority(IRQn)))
  290. {
  291. return NRF_ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED;
  292. }
  293. if (nrf_nvic_state.__cr_flag)
  294. {
  295. nrf_nvic_state.__irq_masks[(uint32_t)((int32_t)IRQn) >> 5] |= (uint32_t)(1 << ((uint32_t)((int32_t)IRQn) & (uint32_t)0x1F));
  296. }
  297. else
  298. {
  299. NVIC_EnableIRQ(IRQn);
  300. }
  301. return NRF_SUCCESS;
  302. }
  303. __STATIC_INLINE uint32_t sd_nvic_DisableIRQ(IRQn_Type IRQn)
  304. {
  305. if (!__sd_nvic_app_accessible_irq(IRQn))
  306. {
  307. return NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE;
  308. }
  309. if (nrf_nvic_state.__cr_flag)
  310. {
  311. nrf_nvic_state.__irq_masks[(uint32_t)((int32_t)IRQn) >> 5] &= ~(1UL << ((uint32_t)(IRQn) & 0x1F));
  312. }
  313. else
  314. {
  315. NVIC_DisableIRQ(IRQn);
  316. }
  317. return NRF_SUCCESS;
  318. }
  319. __STATIC_INLINE uint32_t sd_nvic_GetPendingIRQ(IRQn_Type IRQn, uint32_t * p_pending_irq)
  320. {
  321. if (__sd_nvic_app_accessible_irq(IRQn))
  322. {
  323. *p_pending_irq = NVIC_GetPendingIRQ(IRQn);
  324. return NRF_SUCCESS;
  325. }
  326. else
  327. {
  328. return NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE;
  329. }
  330. }
  331. __STATIC_INLINE uint32_t sd_nvic_SetPendingIRQ(IRQn_Type IRQn)
  332. {
  333. if (__sd_nvic_app_accessible_irq(IRQn))
  334. {
  335. NVIC_SetPendingIRQ(IRQn);
  336. return NRF_SUCCESS;
  337. }
  338. else
  339. {
  340. return NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE;
  341. }
  342. }
  343. __STATIC_INLINE uint32_t sd_nvic_ClearPendingIRQ(IRQn_Type IRQn)
  344. {
  345. if (__sd_nvic_app_accessible_irq(IRQn))
  346. {
  347. NVIC_ClearPendingIRQ(IRQn);
  348. return NRF_SUCCESS;
  349. }
  350. else
  351. {
  352. return NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE;
  353. }
  354. }
  355. __STATIC_INLINE uint32_t sd_nvic_SetPriority(IRQn_Type IRQn, uint32_t priority)
  356. {
  357. if (!__sd_nvic_app_accessible_irq(IRQn))
  358. {
  359. return NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE;
  360. }
  361. if (!__sd_nvic_is_app_accessible_priority(priority))
  362. {
  363. return NRF_ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED;
  364. }
  365. NVIC_SetPriority(IRQn, (uint32_t)priority);
  366. return NRF_SUCCESS;
  367. }
  368. __STATIC_INLINE uint32_t sd_nvic_GetPriority(IRQn_Type IRQn, uint32_t * p_priority)
  369. {
  370. if (__sd_nvic_app_accessible_irq(IRQn))
  371. {
  372. *p_priority = (NVIC_GetPriority(IRQn) & 0xFF);
  373. return NRF_SUCCESS;
  374. }
  375. else
  376. {
  377. return NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE;
  378. }
  379. }
  380. __STATIC_INLINE uint32_t sd_nvic_SystemReset(void)
  381. {
  382. NVIC_SystemReset();
  383. return NRF_ERROR_SOC_NVIC_SHOULD_NOT_RETURN;
  384. }
  385. __STATIC_INLINE uint32_t sd_nvic_critical_region_enter(uint8_t * p_is_nested_critical_region)
  386. {
  387. int was_masked = __sd_nvic_irq_disable();
  388. if (!nrf_nvic_state.__cr_flag)
  389. {
  390. nrf_nvic_state.__cr_flag = 1;
  391. nrf_nvic_state.__irq_masks[0] = ( NVIC->ICER[0] & __NRF_NVIC_APP_IRQS_0 );
  392. NVIC->ICER[0] = __NRF_NVIC_APP_IRQS_0;
  393. nrf_nvic_state.__irq_masks[1] = ( NVIC->ICER[1] & __NRF_NVIC_APP_IRQS_1 );
  394. NVIC->ICER[1] = __NRF_NVIC_APP_IRQS_1;
  395. *p_is_nested_critical_region = 0;
  396. }
  397. else
  398. {
  399. *p_is_nested_critical_region = 1;
  400. }
  401. if (!was_masked)
  402. {
  403. __sd_nvic_irq_enable();
  404. }
  405. return NRF_SUCCESS;
  406. }
  407. __STATIC_INLINE uint32_t sd_nvic_critical_region_exit(uint8_t is_nested_critical_region)
  408. {
  409. if (nrf_nvic_state.__cr_flag && (is_nested_critical_region == 0))
  410. {
  411. int was_masked = __sd_nvic_irq_disable();
  412. NVIC->ISER[0] = nrf_nvic_state.__irq_masks[0];
  413. NVIC->ISER[1] = nrf_nvic_state.__irq_masks[1];
  414. nrf_nvic_state.__cr_flag = 0;
  415. if (!was_masked)
  416. {
  417. __sd_nvic_irq_enable();
  418. }
  419. }
  420. return NRF_SUCCESS;
  421. }
  422. #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
  423. #ifdef __cplusplus
  424. }
  425. #endif
  426. #endif // NRF_NVIC_H__
  427. /**@} */