nrf_nvic.h 14 KB

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