am_hal_interrupt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //*****************************************************************************
  2. //
  3. // am_hal_interrupt.c
  4. //! @file
  5. //!
  6. //! @brief Helper functions supporting interrupts and NVIC operation.
  7. //!
  8. //! These functions may be used for NVIC-level interrupt configuration.
  9. //!
  10. //! @addtogroup interrupt2 Interrupt (ARM NVIC support functions)
  11. //! @ingroup apollo2hal
  12. //! @{
  13. //
  14. //*****************************************************************************
  15. //*****************************************************************************
  16. //
  17. // Copyright (c) 2017, Ambiq Micro
  18. // All rights reserved.
  19. //
  20. // Redistribution and use in source and binary forms, with or without
  21. // modification, are permitted provided that the following conditions are met:
  22. //
  23. // 1. Redistributions of source code must retain the above copyright notice,
  24. // this list of conditions and the following disclaimer.
  25. //
  26. // 2. Redistributions in binary form must reproduce the above copyright
  27. // notice, this list of conditions and the following disclaimer in the
  28. // documentation and/or other materials provided with the distribution.
  29. //
  30. // 3. Neither the name of the copyright holder nor the names of its
  31. // contributors may be used to endorse or promote products derived from this
  32. // software without specific prior written permission.
  33. //
  34. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  35. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  38. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  39. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  40. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  41. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  42. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  44. // POSSIBILITY OF SUCH DAMAGE.
  45. //
  46. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  47. //
  48. //*****************************************************************************
  49. #include <stdint.h>
  50. #include <stdbool.h>
  51. #include "am_mcu_apollo.h"
  52. //*****************************************************************************
  53. //
  54. //! @brief Enable an interrupt.
  55. //!
  56. //! @param ui32Interrupt The ISR number of the interrupt to be enabled.
  57. //!
  58. //! This function enables an interrupt signal to the NVIC based on the provided
  59. //! ISR number.
  60. //!
  61. //! @return None
  62. //
  63. //*****************************************************************************
  64. void
  65. am_hal_interrupt_enable(uint32_t ui32Interrupt)
  66. {
  67. //
  68. // Check to see what type of interrupt this is.
  69. //
  70. if ( ui32Interrupt > 15 )
  71. {
  72. //
  73. // If this ISR number corresponds to a "normal" peripheral interrupt,
  74. // enable it using the NVIC register.
  75. //
  76. AM_REG(NVIC, ISER0) = 0x1 << ((ui32Interrupt - 16) & 0x1F);
  77. }
  78. else
  79. {
  80. //
  81. // If this is an ARM internal interrupt number, route it to the
  82. // appropriate enable register.
  83. //
  84. switch(ui32Interrupt)
  85. {
  86. case AM_HAL_INTERRUPT_BUSFAULT:
  87. AM_BFW(SYSCTRL, SHCSR, BUSFAULTENA, 1);
  88. break;
  89. case AM_HAL_INTERRUPT_USAGEFAULT:
  90. AM_BFW(SYSCTRL, SHCSR, USAGEFAULTENA, 1);
  91. break;
  92. case AM_HAL_INTERRUPT_MPUFAULT:
  93. AM_BFW(SYSCTRL, SHCSR, MEMFAULTENA, 1);
  94. break;
  95. }
  96. }
  97. }
  98. //*****************************************************************************
  99. //
  100. //! @brief Disable an interrupt.
  101. //!
  102. //! @param ui32Interrupt The ISR number of the interrupt to be disabled.
  103. //!
  104. //! This function disables an interrupt signal to the NVIC based on the
  105. //! provided ISR number.
  106. //!
  107. //! @return None
  108. //
  109. //*****************************************************************************
  110. void
  111. am_hal_interrupt_disable(uint32_t ui32Interrupt)
  112. {
  113. //
  114. // Check to see what type of interrupt this is.
  115. //
  116. if ( ui32Interrupt > 15 )
  117. {
  118. //
  119. // If this ISR number corresponds to a "normal" peripheral interrupt,
  120. // disable it using the NVIC register.
  121. //
  122. AM_REG(NVIC, ICER0) = 0x1 << ((ui32Interrupt - 16) & 0x1F);
  123. }
  124. else
  125. {
  126. //
  127. // If this is an ARM internal interrupt number, route it to the
  128. // appropriate enable register.
  129. //
  130. switch(ui32Interrupt)
  131. {
  132. case AM_HAL_INTERRUPT_BUSFAULT:
  133. AM_BFW(SYSCTRL, SHCSR, BUSFAULTENA, 0);
  134. break;
  135. case AM_HAL_INTERRUPT_USAGEFAULT:
  136. AM_BFW(SYSCTRL, SHCSR, USAGEFAULTENA, 0);
  137. break;
  138. case AM_HAL_INTERRUPT_MPUFAULT:
  139. AM_BFW(SYSCTRL, SHCSR, MEMFAULTENA, 0);
  140. break;
  141. }
  142. }
  143. }
  144. //*****************************************************************************
  145. //
  146. //! @brief Set the priority of an interrupt vector.
  147. //!
  148. //! @param ui32Interrupt is the ISR number of the interrupt to change.
  149. //! @param ui32Priority is the new ISR priority value.
  150. //!
  151. //! This function changes the priority value in the NVIC for the given
  152. //! interrupt vector number.
  153. //!
  154. //! @return None
  155. //
  156. //*****************************************************************************
  157. void
  158. am_hal_interrupt_priority_set(uint32_t ui32Interrupt, uint32_t ui32Priority)
  159. {
  160. volatile uint32_t *pui32PriorityReg;
  161. volatile uint32_t ui32OldPriority;
  162. uint32_t ui32Shift;
  163. //
  164. // Find the correct priority register.
  165. //
  166. pui32PriorityReg = (volatile uint32_t *) AM_REG_NVIC_IPR0_O;
  167. pui32PriorityReg += ((ui32Interrupt - 16) >> 2);
  168. //
  169. // Find the correct shift value.
  170. //
  171. ui32Shift = (((ui32Interrupt - 16) & 0x3) * 8);
  172. //
  173. // Mask out the old priority.
  174. //
  175. ui32OldPriority = *pui32PriorityReg;
  176. ui32OldPriority &= ~(0xFF << ui32Shift);
  177. //
  178. // OR in the new priority.
  179. //
  180. *pui32PriorityReg = ui32OldPriority | (ui32Priority << ui32Shift);
  181. }
  182. //*****************************************************************************
  183. //
  184. //! @brief Set a pending interrupt bit in the NVIC (Software Interrupt)
  185. //!
  186. //! @param ui32Interrupt is the ISR number of the interrupt to change.
  187. //!
  188. //! This function sets the specified bit in the Interrupt Set Pending (ISPR0)
  189. //! register. For future MCUs there may be more than one ISPR.
  190. //!
  191. //! @return None
  192. //
  193. //*****************************************************************************
  194. void am_hal_interrupt_pend_set(uint32_t ui32Interrupt)
  195. {
  196. //
  197. // Check to see if the specified interrupt is valid for this MCU
  198. //
  199. if ( ui32Interrupt > AM_HAL_INTERRUPT_MAX )
  200. {
  201. return;
  202. }
  203. //
  204. // Check to see what type of interrupt this is.
  205. //
  206. if ( ui32Interrupt > 15 )
  207. {
  208. //
  209. // If this ISR number corresponds to a "normal" peripheral interrupt,
  210. // disable it using the NVIC register.
  211. //
  212. AM_REG(NVIC, ISPR0) = 0x1 << ((ui32Interrupt - 16) & 0x1F);
  213. }
  214. }
  215. //*****************************************************************************
  216. //
  217. //! @brief Clear a pending interrupt bit in the NVIC without servicing it
  218. //!
  219. //! @param ui32Interrupt is the ISR number of the interrupt to change.
  220. //!
  221. //! This function clears the specified bit in the Interrupt Clear Pending
  222. //! (ICPR0) register. For future MCUs there may be more than one ICPR. This
  223. //! function is useful immediately following a WFI before interrupts are
  224. //! re-enabled.
  225. //!
  226. //! @return None
  227. //
  228. //*****************************************************************************
  229. void am_hal_interrupt_pend_clear(uint32_t ui32Interrupt)
  230. {
  231. //
  232. // Check to see if the specified interrupt is valid for this MCU
  233. //
  234. if ( ui32Interrupt > AM_HAL_INTERRUPT_MAX )
  235. {
  236. return;
  237. }
  238. //
  239. // Check to see what type of interrupt this is.
  240. //
  241. if ( ui32Interrupt > 15 )
  242. {
  243. //
  244. // If this ISR number corresponds to a "normal" peripheral interrupt,
  245. // disable it using the NVIC register.
  246. //
  247. AM_REG(NVIC, ICPR0) = 0x1 << ((ui32Interrupt - 16) & 0x1F);
  248. }
  249. }
  250. //*****************************************************************************
  251. //
  252. //! @brief Globally enable interrupt service routines
  253. //!
  254. //! This function allows interrupt signals from the NVIC to trigger ISR entry
  255. //! in the CPU. This function must be called if interrupts are to be serviced
  256. //! in software.
  257. //!
  258. //! @return 1 if interrupts were previously disabled, 0 otherwise.
  259. //
  260. //*****************************************************************************
  261. #if defined(__GNUC_STDC_INLINE__)
  262. uint32_t __attribute__((naked))
  263. am_hal_interrupt_master_enable(void)
  264. {
  265. __asm(" mrs r0, PRIMASK");
  266. __asm(" cpsie i");
  267. __asm(" bx lr");
  268. }
  269. #elif defined(__ARMCC_VERSION)
  270. __asm uint32_t
  271. am_hal_interrupt_master_enable(void)
  272. {
  273. mrs r0, PRIMASK
  274. cpsie i
  275. bx lr
  276. }
  277. #elif defined(__IAR_SYSTEMS_ICC__)
  278. #pragma diag_suppress = Pe940 // Suppress IAR compiler warning about missing
  279. // return statement on a non-void function
  280. __stackless uint32_t
  281. am_hal_interrupt_master_enable(void)
  282. {
  283. __asm(" mrs r0, PRIMASK");
  284. __asm(" cpsie i");
  285. __asm(" bx lr");
  286. }
  287. #pragma diag_default = Pe940 // Restore IAR compiler warning
  288. #endif
  289. //*****************************************************************************
  290. //
  291. //! @brief Globally disable interrupt service routines
  292. //!
  293. //! This function prevents interrupt signals from the NVIC from triggering ISR
  294. //! entry in the CPU. This will effectively stop incoming interrupt sources
  295. //! from triggering their corresponding ISRs.
  296. //!
  297. //! @note Any external interrupt signal that occurs while the master interrupt
  298. //! disable is active will still reach the "pending" state in the NVIC, but it
  299. //! will not be allowed to reach the "active" state or trigger the
  300. //! corresponding ISR. Instead, these interrupts are essentially "queued" until
  301. //! the next time the master interrupt enable instruction is executed. At that
  302. //! time, the interrupt handlers will be executed in order of decreasing
  303. //! priority.
  304. //!
  305. //! @return 1 if interrupts were previously disabled, 0 otherwise.
  306. //
  307. //*****************************************************************************
  308. #if defined(__GNUC_STDC_INLINE__)
  309. uint32_t __attribute__((naked))
  310. am_hal_interrupt_master_disable(void)
  311. {
  312. __asm(" mrs r0, PRIMASK");
  313. __asm(" cpsid i");
  314. __asm(" bx lr");
  315. }
  316. #elif defined(__ARMCC_VERSION)
  317. __asm uint32_t
  318. am_hal_interrupt_master_disable(void)
  319. {
  320. mrs r0, PRIMASK
  321. cpsid i
  322. bx lr
  323. }
  324. #elif defined(__IAR_SYSTEMS_ICC__)
  325. #pragma diag_suppress = Pe940 // Suppress IAR compiler warning about missing
  326. // return statement on a non-void function
  327. __stackless uint32_t
  328. am_hal_interrupt_master_disable(void)
  329. {
  330. __asm(" mrs r0, PRIMASK");
  331. __asm(" cpsid i");
  332. __asm(" bx lr");
  333. }
  334. #pragma diag_default = Pe940 // Restore IAR compiler warning
  335. #endif
  336. //*****************************************************************************
  337. //
  338. //! @brief Sets the master interrupt state based on the input.
  339. //!
  340. //! @param ui32InterruptState - Desired PRIMASK value.
  341. //!
  342. //! This function directly writes the PRIMASK register in the ARM core. A value
  343. //! of 1 will disable interrupts, while a value of zero will enable them.
  344. //!
  345. //! This function may be used along with am_hal_interrupt_master_disable() to
  346. //! implement a nesting critical section. To do this, call
  347. //! am_hal_interrupt_master_disable() to start the critical section, and save
  348. //! its return value. To complete the critical section, call
  349. //! am_hal_interrupt_master_set() using the saved return value as \e
  350. //! ui32InterruptState. This will safely restore PRIMASK to the value it
  351. //! contained just before the start of the critical section.
  352. //!
  353. //! @return None.
  354. //
  355. //*****************************************************************************
  356. #if defined(__GNUC_STDC_INLINE__)
  357. void __attribute__((naked))
  358. am_hal_interrupt_master_set(uint32_t ui32InterruptState)
  359. {
  360. __asm(" msr PRIMASK, r0");
  361. __asm(" bx lr");
  362. }
  363. #elif defined(__ARMCC_VERSION)
  364. __asm void
  365. am_hal_interrupt_master_set(uint32_t ui32InterruptState)
  366. {
  367. msr PRIMASK, r0
  368. bx lr
  369. }
  370. #elif defined(__IAR_SYSTEMS_ICC__)
  371. #pragma diag_suppress = Pe940 // Suppress IAR compiler warning about missing
  372. // return statement on a non-void function
  373. __stackless void
  374. am_hal_interrupt_master_set(uint32_t ui32InterruptState)
  375. {
  376. __asm(" msr PRIMASK, r0");
  377. __asm(" bx lr");
  378. }
  379. #pragma diag_default = Pe940 // Restore IAR compiler warning
  380. #endif
  381. //*****************************************************************************
  382. //
  383. // End Doxygen group.
  384. //! @}
  385. //
  386. //*****************************************************************************