interrupt.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. //*****************************************************************************
  2. //
  3. // interrupt.c - Driver for the NVIC Interrupt Controller.
  4. //
  5. // Copyright (c) 2005-2020 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup interrupt_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_ints.h"
  48. #include "inc/hw_nvic.h"
  49. #include "inc/hw_types.h"
  50. #include "driverlib/cpu.h"
  51. #include "driverlib/debug.h"
  52. #include "driverlib/interrupt.h"
  53. //*****************************************************************************
  54. //
  55. // This is a mapping between priority grouping encodings and the number of
  56. // preemption priority bits.
  57. //
  58. //*****************************************************************************
  59. static const uint32_t g_pui32Priority[] =
  60. {
  61. NVIC_APINT_PRIGROUP_0_8, NVIC_APINT_PRIGROUP_1_7, NVIC_APINT_PRIGROUP_2_6,
  62. NVIC_APINT_PRIGROUP_3_5, NVIC_APINT_PRIGROUP_4_4, NVIC_APINT_PRIGROUP_5_3,
  63. NVIC_APINT_PRIGROUP_6_2, NVIC_APINT_PRIGROUP_7_1
  64. };
  65. //*****************************************************************************
  66. //
  67. // This is a mapping between interrupt number and the register that contains
  68. // the priority encoding for that interrupt.
  69. //
  70. //*****************************************************************************
  71. static const uint32_t g_pui32Regs[] =
  72. {
  73. 0, NVIC_SYS_PRI1, NVIC_SYS_PRI2, NVIC_SYS_PRI3, NVIC_PRI0, NVIC_PRI1,
  74. NVIC_PRI2, NVIC_PRI3, NVIC_PRI4, NVIC_PRI5, NVIC_PRI6, NVIC_PRI7,
  75. NVIC_PRI8, NVIC_PRI9, NVIC_PRI10, NVIC_PRI11, NVIC_PRI12, NVIC_PRI13,
  76. NVIC_PRI14, NVIC_PRI15, NVIC_PRI16, NVIC_PRI17, NVIC_PRI18, NVIC_PRI19,
  77. NVIC_PRI20, NVIC_PRI21, NVIC_PRI22, NVIC_PRI23, NVIC_PRI24, NVIC_PRI25,
  78. NVIC_PRI26, NVIC_PRI27, NVIC_PRI28, NVIC_PRI29, NVIC_PRI30, NVIC_PRI31,
  79. NVIC_PRI32, NVIC_PRI33, NVIC_PRI34
  80. };
  81. //*****************************************************************************
  82. //
  83. // This is a mapping between interrupt number (for the peripheral interrupts
  84. // only) and the register that contains the interrupt enable for that
  85. // interrupt.
  86. //
  87. //*****************************************************************************
  88. static const uint32_t g_pui32EnRegs[] =
  89. {
  90. NVIC_EN0, NVIC_EN1, NVIC_EN2, NVIC_EN3, NVIC_EN4
  91. };
  92. //*****************************************************************************
  93. //
  94. // This is a mapping between interrupt number (for the peripheral interrupts
  95. // only) and the register that contains the interrupt disable for that
  96. // interrupt.
  97. //
  98. //*****************************************************************************
  99. static const uint32_t g_pui32Dii16Regs[] =
  100. {
  101. NVIC_DIS0, NVIC_DIS1, NVIC_DIS2, NVIC_DIS3, NVIC_DIS4
  102. };
  103. //*****************************************************************************
  104. //
  105. // This is a mapping between interrupt number (for the peripheral interrupts
  106. // only) and the register that contains the interrupt pend for that interrupt.
  107. //
  108. //*****************************************************************************
  109. static const uint32_t g_pui32PendRegs[] =
  110. {
  111. NVIC_PEND0, NVIC_PEND1, NVIC_PEND2, NVIC_PEND3, NVIC_PEND4
  112. };
  113. //*****************************************************************************
  114. //
  115. // This is a mapping between interrupt number (for the peripheral interrupts
  116. // only) and the register that contains the interrupt unpend for that
  117. // interrupt.
  118. //
  119. //*****************************************************************************
  120. static const uint32_t g_pui32UnpendRegs[] =
  121. {
  122. NVIC_UNPEND0, NVIC_UNPEND1, NVIC_UNPEND2, NVIC_UNPEND3, NVIC_UNPEND4
  123. };
  124. //*****************************************************************************
  125. //
  126. //! \internal
  127. //! The default interrupt handler.
  128. //!
  129. //! This is the default interrupt handler for all interrupts. It simply loops
  130. //! forever so that the system state is preserved for observation by a
  131. //! debugger. Since interrupts must be disabled before unregistering the
  132. //! corresponding handler, this should never be called during normal operation.
  133. //!
  134. //! \return None.
  135. //
  136. //*****************************************************************************
  137. static void
  138. _IntDefaultHandler(void)
  139. {
  140. //
  141. // Go into an infinite loop.
  142. //
  143. while(1)
  144. {
  145. }
  146. }
  147. //*****************************************************************************
  148. //
  149. // The processor vector table.
  150. //
  151. // This contains a list of the handlers for the various interrupt sources in
  152. // the system. The layout of this list is defined by the hardware; assertion
  153. // of an interrupt causes the processor to start executing directly at the
  154. // address given in the corresponding location in this list.
  155. //
  156. //*****************************************************************************
  157. //
  158. // Set the size of the vector table to the largest number of interrupts of
  159. // any device
  160. //
  161. #undef NUM_INTERRUPTS
  162. #define NUM_INTERRUPTS 155
  163. #if defined(ewarm)
  164. #pragma data_alignment=1024
  165. static __no_init void (*g_pfnRAMVectors[NUM_INTERRUPTS])(void) @ "VTABLE";
  166. #elif defined(sourcerygxx)
  167. static __attribute__((section(".cs3.region-head.ram")))
  168. void (*g_pfnRAMVectors[NUM_INTERRUPTS])(void) __attribute__ ((aligned(1024)));
  169. #elif defined(ccs) || defined(DOXYGEN)
  170. #pragma DATA_ALIGN(g_pfnRAMVectors, 1024)
  171. #pragma DATA_SECTION(g_pfnRAMVectors, ".vtable")
  172. void (*g_pfnRAMVectors[NUM_INTERRUPTS])(void);
  173. #else
  174. static __attribute__((section("vtable")))
  175. void (*g_pfnRAMVectors[NUM_INTERRUPTS])(void) __attribute__((aligned(1024)));
  176. #endif
  177. //*****************************************************************************
  178. //
  179. //! Enables the processor interrupt.
  180. //!
  181. //! This function allows the processor to respond to interrupts. This function
  182. //! does not affect the set of interrupts enabled in the interrupt controller;
  183. //! it just gates the single interrupt from the controller to the processor.
  184. //!
  185. //! \b Example: Enable interrupts to the processor.
  186. //!
  187. //! \verbatim
  188. //! //
  189. //! // Enable interrupts to the processor.
  190. //! //
  191. //! IntMasterEnable();
  192. //!
  193. //! \endverbatim
  194. //!
  195. //! \return Returns \b true if interrupts were disabled when the function was
  196. //! called or \b false if they were initially enabled.
  197. //
  198. //*****************************************************************************
  199. bool
  200. IntMasterEnable(void)
  201. {
  202. //
  203. // Enable processor interrupts.
  204. //
  205. return(CPUcpsie());
  206. }
  207. //*****************************************************************************
  208. //
  209. //! Disables the processor interrupt.
  210. //!
  211. //! This function prevents the processor from receiving interrupts. This
  212. //! function does not affect the set of interrupts enabled in the interrupt
  213. //! controller; it just gates the single interrupt from the controller to the
  214. //! processor.
  215. //!
  216. //! \note Previously, this function had no return value. As such, it was
  217. //! possible to include <tt>interrupt.h</tt> and call this function without
  218. //! having included <tt>hw_types.h</tt>. Now that the return is a
  219. //! <tt>bool</tt>, a compiler error occurs in this case. The solution
  220. //! is to include <tt>hw_types.h</tt> before including <tt>interrupt.h</tt>.
  221. //!
  222. //! \b Example: Disable interrupts to the processor.
  223. //!
  224. //! \verbatim
  225. //! //
  226. //! // Disable interrupts to the processor.
  227. //! //
  228. //! IntMasterDisable();
  229. //!
  230. //! \endverbatim
  231. //!
  232. //! \return Returns \b true if interrupts were already disabled when the
  233. //! function was called or \b false if they were initially enabled.
  234. //
  235. //*****************************************************************************
  236. bool
  237. IntMasterDisable(void)
  238. {
  239. //
  240. // Disable processor interrupts.
  241. //
  242. return(CPUcpsid());
  243. }
  244. //*****************************************************************************
  245. //
  246. //! Registers a function to be called when an interrupt occurs.
  247. //!
  248. //! \param ui32Interrupt specifies the interrupt in question.
  249. //! \param pfnHandler is a pointer to the function to be called.
  250. //!
  251. //! This function is used to specify the handler function to be called when the
  252. //! given interrupt is asserted to the processor. The \e ui32Interrupt
  253. //! parameter must be one of the valid \b INT_* values listed in Peripheral
  254. //! Driver Library User's Guide and defined in the inc/hw_ints.h header file.
  255. //! When the interrupt occurs, if it is enabled (via IntEnable()), the handler
  256. //! function is called in interrupt context. Because the handler function can
  257. //! preempt other code, care must be taken to protect memory or peripherals
  258. //! that are accessed by the handler and other non-handler code.
  259. //!
  260. //! \note The use of this function (directly or indirectly via a peripheral
  261. //! driver interrupt register function) moves the interrupt vector table from
  262. //! flash to SRAM. Therefore, care must be taken when linking the application
  263. //! to ensure that the SRAM vector table is located at the beginning of SRAM;
  264. //! otherwise the NVIC does not look in the correct portion of memory for the
  265. //! vector table (it requires the vector table be on a 1 kB memory alignment).
  266. //! Normally, the SRAM vector table is so placed via the use of linker scripts.
  267. //! See the discussion of compile-time versus run-time interrupt handler
  268. //! registration in the introduction to this chapter.
  269. //!
  270. //! \b Example: Set the UART 0 interrupt handler.
  271. //!
  272. //! \verbatim
  273. //!
  274. //! //
  275. //! // UART 0 interrupt handler.
  276. //! //
  277. //! void
  278. //! UART0Handler(void)
  279. //! {
  280. //! //
  281. //! // Handle interrupt.
  282. //! //
  283. //! }
  284. //!
  285. //! //
  286. //! // Set the UART 0 interrupt handler.
  287. //! //
  288. //! IntRegister(INT_UART0, UART0Handler);
  289. //!
  290. //! \endverbatim
  291. //!
  292. //! \return None.
  293. //
  294. //*****************************************************************************
  295. void
  296. IntRegister(uint32_t ui32Interrupt, void (*pfnHandler)(void))
  297. {
  298. uint32_t ui32Idx, ui32Value;
  299. //
  300. // Check the arguments.
  301. //
  302. ASSERT(ui32Interrupt < NUM_INTERRUPTS);
  303. //
  304. // Make sure that the RAM vector table is correctly aligned.
  305. //
  306. ASSERT(((uint32_t)g_pfnRAMVectors & 0x000003ff) == 0);
  307. //
  308. // See if the RAM vector table has been initialized.
  309. //
  310. if(HWREG(NVIC_VTABLE) != (uint32_t)g_pfnRAMVectors)
  311. {
  312. //
  313. // Copy the vector table from the beginning of FLASH to the RAM vector
  314. // table.
  315. //
  316. ui32Value = HWREG(NVIC_VTABLE);
  317. for(ui32Idx = 0; ui32Idx < NUM_INTERRUPTS; ui32Idx++)
  318. {
  319. g_pfnRAMVectors[ui32Idx] = (void (*)(void))HWREG((ui32Idx * 4) +
  320. ui32Value);
  321. }
  322. //
  323. // Point the NVIC at the RAM vector table.
  324. //
  325. HWREG(NVIC_VTABLE) = (uint32_t)g_pfnRAMVectors;
  326. }
  327. //
  328. // Save the interrupt handler.
  329. //
  330. g_pfnRAMVectors[ui32Interrupt] = pfnHandler;
  331. }
  332. //*****************************************************************************
  333. //
  334. //! Unregisters the function to be called when an interrupt occurs.
  335. //!
  336. //! \param ui32Interrupt specifies the interrupt in question.
  337. //!
  338. //! This function is used to indicate that no handler is called when the
  339. //! given interrupt is asserted to the processor. The \e ui32Interrupt
  340. //! parameter must be one of the valid \b INT_* values listed in Peripheral
  341. //! Driver Library User's Guide and defined in the inc/hw_ints.h header file.
  342. //! The interrupt source is automatically disabled (via IntDisable()) if
  343. //! necessary.
  344. //!
  345. //! \sa IntRegister() for important information about registering interrupt
  346. //! handlers.
  347. //!
  348. //! \b Example: Reset the UART 0 interrupt handler to the default handler.
  349. //!
  350. //! \verbatim
  351. //! //
  352. //! // Reset the UART 0 interrupt handler to the default handler.
  353. //! //
  354. //! IntUnregister(INT_UART0);
  355. //!
  356. //! \endverbatim
  357. //!
  358. //! \return None.
  359. //
  360. //*****************************************************************************
  361. void
  362. IntUnregister(uint32_t ui32Interrupt)
  363. {
  364. //
  365. // Check the arguments.
  366. //
  367. ASSERT(ui32Interrupt < NUM_INTERRUPTS);
  368. //
  369. // Reset the interrupt handler.
  370. //
  371. g_pfnRAMVectors[ui32Interrupt] = _IntDefaultHandler;
  372. }
  373. //*****************************************************************************
  374. //
  375. //! Sets the priority grouping of the interrupt controller.
  376. //!
  377. //! \param ui32Bits specifies the number of bits of preemptable priority.
  378. //!
  379. //! This function specifies the split between preemptable priority levels and
  380. //! sub-priority levels in the interrupt priority specification. The range of
  381. //! the grouping values are dependent upon the hardware implementation; on
  382. //! the Tiva C and E Series family, three bits are available for hardware
  383. //! interrupt prioritization and therefore priority grouping values of three
  384. //! through seven have the same effect.
  385. //!
  386. //! \b Example: Set the priority grouping for the interrupt controller.
  387. //!
  388. //! \verbatim
  389. //! //
  390. //! // Set the priority grouping for the interrupt controller to 2 bits.
  391. //! //
  392. //! IntPriorityGroupingSet(2);
  393. //!
  394. //! \endverbatim
  395. //!
  396. //! \return None.
  397. //
  398. //*****************************************************************************
  399. void
  400. IntPriorityGroupingSet(uint32_t ui32Bits)
  401. {
  402. //
  403. // Check the arguments.
  404. //
  405. ASSERT(ui32Bits < NUM_PRIORITY);
  406. //
  407. // Set the priority grouping.
  408. //
  409. HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | g_pui32Priority[ui32Bits];
  410. }
  411. //*****************************************************************************
  412. //
  413. //! Gets the priority grouping of the interrupt controller.
  414. //!
  415. //! This function returns the split between preemptable priority levels and
  416. //! sub-priority levels in the interrupt priority specification.
  417. //!
  418. //! \b Example: Get the priority grouping for the interrupt controller.
  419. //!
  420. //! \verbatim
  421. //! //
  422. //! // Get the priority grouping for the interrupt controller.
  423. //! //
  424. //! IntPriorityGroupingGet();
  425. //!
  426. //! \endverbatim
  427. //!
  428. //! \return The number of bits of preemptable priority.
  429. //
  430. //*****************************************************************************
  431. uint32_t
  432. IntPriorityGroupingGet(void)
  433. {
  434. uint32_t ui32Loop, ui32Value;
  435. //
  436. // Read the priority grouping.
  437. //
  438. ui32Value = HWREG(NVIC_APINT) & NVIC_APINT_PRIGROUP_M;
  439. //
  440. // Loop through the priority grouping values.
  441. //
  442. for(ui32Loop = 0; ui32Loop < NUM_PRIORITY; ui32Loop++)
  443. {
  444. //
  445. // Stop looping if this value matches.
  446. //
  447. if(ui32Value == g_pui32Priority[ui32Loop])
  448. {
  449. break;
  450. }
  451. }
  452. //
  453. // Return the number of priority bits.
  454. //
  455. return(ui32Loop);
  456. }
  457. //*****************************************************************************
  458. //
  459. //! Sets the priority of an interrupt.
  460. //!
  461. //! \param ui32Interrupt specifies the interrupt in question.
  462. //! \param ui8Priority specifies the priority of the interrupt.
  463. //!
  464. //! This function is used to set the priority of an interrupt. The
  465. //! \e ui32Interrupt parameter must be one of the valid \b INT_* values listed
  466. //! in Peripheral Driver Library User's Guide and defined in the inc/hw_ints.h
  467. //! header file. The \e ui8Priority parameter specifies the interrupts
  468. //! hardware priority level of the interrupt in the interrupt controller.
  469. //! When multiple interrupts are asserted simultaneously, the ones with the
  470. //! highest priority are processed before the lower priority interrupts.
  471. //! Smaller numbers correspond to higher interrupt priorities; priority 0 is
  472. //! the highest interrupt priority.
  473. //!
  474. //! \note The hardware priority mechanism only looks at the upper 3 bits of the
  475. //! priority level, so any prioritization must be performed in those bits. The
  476. //! remaining bits can be used to sub-prioritize the interrupt sources, and may
  477. //! be used by the hardware priority mechanism on a future part. This
  478. //! arrangement allows priorities to migrate to different NVIC implementations
  479. //! without changing the gross prioritization of the interrupts.
  480. //!
  481. //! \b Example: Set priorities for UART 0 and USB interrupts.
  482. //!
  483. //! \verbatim
  484. //! //
  485. //! // Set the UART 0 interrupt priority to the lowest priority.
  486. //! //
  487. //! IntPrioritySet(INT_UART0, 0xE0);
  488. //!
  489. //! //
  490. //! // Set the USB 0 interrupt priority to the highest priority.
  491. //! //
  492. //! IntPrioritySet(INT_USB0, 0);
  493. //!
  494. //! \endverbatim
  495. //!
  496. //! \return None.
  497. //
  498. //*****************************************************************************
  499. void
  500. IntPrioritySet(uint32_t ui32Interrupt, uint8_t ui8Priority)
  501. {
  502. uint32_t ui32Temp;
  503. //
  504. // Check the arguments.
  505. //
  506. ASSERT((ui32Interrupt >= 4) && (ui32Interrupt < NUM_INTERRUPTS));
  507. //
  508. // Set the interrupt priority.
  509. //
  510. ui32Temp = HWREG(g_pui32Regs[ui32Interrupt >> 2]);
  511. ui32Temp &= ~(0xFF << (8 * (ui32Interrupt & 3)));
  512. ui32Temp |= ui8Priority << (8 * (ui32Interrupt & 3));
  513. HWREG(g_pui32Regs[ui32Interrupt >> 2]) = ui32Temp;
  514. }
  515. //*****************************************************************************
  516. //
  517. //! Gets the priority of an interrupt.
  518. //!
  519. //! \param ui32Interrupt specifies the interrupt in question.
  520. //!
  521. //! This function gets the priority of an interrupt. The \e ui32Interrupt
  522. //! parameter must be one of the valid \b INT_* values listed in Peripheral
  523. //! Driver Library User's Guide and defined in the inc/hw_ints.h header file.
  524. //! See IntPrioritySet() for a full definition of the priority value.
  525. //!
  526. //! \b Example: Get the current UART 0 interrupt priority.
  527. //!
  528. //! \verbatim
  529. //! //
  530. //! // Get the current UART 0 interrupt priority.
  531. //! //
  532. //! IntPriorityGet(INT_UART0);
  533. //!
  534. //! \endverbatim
  535. //!
  536. //! \return Returns the interrupt priority for the given interrupt.
  537. //
  538. //*****************************************************************************
  539. int32_t
  540. IntPriorityGet(uint32_t ui32Interrupt)
  541. {
  542. //
  543. // Check the arguments.
  544. //
  545. ASSERT((ui32Interrupt >= 4) && (ui32Interrupt < NUM_INTERRUPTS));
  546. //
  547. // Return the interrupt priority.
  548. //
  549. return((HWREG(g_pui32Regs[ui32Interrupt >> 2]) >>
  550. (8 * (ui32Interrupt & 3))) & 0xFF);
  551. }
  552. //*****************************************************************************
  553. //
  554. //! Enables an interrupt.
  555. //!
  556. //! \param ui32Interrupt specifies the interrupt to be enabled.
  557. //!
  558. //! The specified interrupt is enabled in the interrupt controller. The
  559. //! \e ui32Interrupt parameter must be one of the valid \b INT_* values listed
  560. //! in Peripheral Driver Library User's Guide and defined in the inc/hw_ints.h
  561. //! header file. Other enables for the interrupt (such as at the peripheral
  562. //! level) are unaffected by this function.
  563. //!
  564. //! \b Example: Enable the UART 0 interrupt.
  565. //!
  566. //! \verbatim
  567. //! //
  568. //! // Enable the UART 0 interrupt in the interrupt controller.
  569. //! //
  570. //! IntEnable(INT_UART0);
  571. //!
  572. //! \endverbatim
  573. //!
  574. //! \return None.
  575. //
  576. //*****************************************************************************
  577. void
  578. IntEnable(uint32_t ui32Interrupt)
  579. {
  580. //
  581. // Check the arguments.
  582. //
  583. ASSERT(ui32Interrupt < NUM_INTERRUPTS);
  584. //
  585. // Determine the interrupt to enable.
  586. //
  587. if(ui32Interrupt == FAULT_MPU)
  588. {
  589. //
  590. // Enable the MemManage interrupt.
  591. //
  592. HWREG(NVIC_SYS_HND_CTRL) |= NVIC_SYS_HND_CTRL_MEM;
  593. }
  594. else if(ui32Interrupt == FAULT_BUS)
  595. {
  596. //
  597. // Enable the bus fault interrupt.
  598. //
  599. HWREG(NVIC_SYS_HND_CTRL) |= NVIC_SYS_HND_CTRL_BUS;
  600. }
  601. else if(ui32Interrupt == FAULT_USAGE)
  602. {
  603. //
  604. // Enable the usage fault interrupt.
  605. //
  606. HWREG(NVIC_SYS_HND_CTRL) |= NVIC_SYS_HND_CTRL_USAGE;
  607. }
  608. else if(ui32Interrupt == FAULT_SYSTICK)
  609. {
  610. //
  611. // Enable the System Tick interrupt.
  612. //
  613. HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
  614. }
  615. else if(ui32Interrupt >= 16)
  616. {
  617. //
  618. // Enable the general interrupt.
  619. //
  620. HWREG(g_pui32EnRegs[(ui32Interrupt - 16) / 32]) =
  621. 1 << ((ui32Interrupt - 16) & 31);
  622. }
  623. }
  624. //*****************************************************************************
  625. //
  626. //! Disables an interrupt.
  627. //!
  628. //! \param ui32Interrupt specifies the interrupt to be disabled.
  629. //!
  630. //! The specified interrupt is disabled in the interrupt controller. The
  631. //! \e ui32Interrupt parameter must be one of the valid \b INT_* values listed
  632. //! in Peripheral Driver Library User's Guide and defined in the inc/hw_ints.h
  633. //! header file. Other enables for the interrupt (such as at the peripheral
  634. //! level) are unaffected by this function.
  635. //!
  636. //! \b Example: Disable the UART 0 interrupt.
  637. //!
  638. //! \verbatim
  639. //! //
  640. //! // Disable the UART 0 interrupt in the interrupt controller.
  641. //! //
  642. //! IntDisable(INT_UART0);
  643. //!
  644. //! \endverbatim
  645. //!
  646. //! \return None.
  647. //
  648. //*****************************************************************************
  649. void
  650. IntDisable(uint32_t ui32Interrupt)
  651. {
  652. //
  653. // Check the arguments.
  654. //
  655. ASSERT(ui32Interrupt < NUM_INTERRUPTS);
  656. //
  657. // Determine the interrupt to disable.
  658. //
  659. if(ui32Interrupt == FAULT_MPU)
  660. {
  661. //
  662. // Disable the MemManage interrupt.
  663. //
  664. HWREG(NVIC_SYS_HND_CTRL) &= ~(NVIC_SYS_HND_CTRL_MEM);
  665. }
  666. else if(ui32Interrupt == FAULT_BUS)
  667. {
  668. //
  669. // Disable the bus fault interrupt.
  670. //
  671. HWREG(NVIC_SYS_HND_CTRL) &= ~(NVIC_SYS_HND_CTRL_BUS);
  672. }
  673. else if(ui32Interrupt == FAULT_USAGE)
  674. {
  675. //
  676. // Disable the usage fault interrupt.
  677. //
  678. HWREG(NVIC_SYS_HND_CTRL) &= ~(NVIC_SYS_HND_CTRL_USAGE);
  679. }
  680. else if(ui32Interrupt == FAULT_SYSTICK)
  681. {
  682. //
  683. // Disable the System Tick interrupt.
  684. //
  685. HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
  686. }
  687. else if(ui32Interrupt >= 16)
  688. {
  689. //
  690. // Disable the general interrupt.
  691. //
  692. HWREG(g_pui32Dii16Regs[(ui32Interrupt - 16) / 32]) =
  693. 1 << ((ui32Interrupt - 16) & 31);
  694. }
  695. }
  696. //*****************************************************************************
  697. //
  698. //! Returns if a peripheral interrupt is enabled.
  699. //!
  700. //! \param ui32Interrupt specifies the interrupt to check.
  701. //!
  702. //! This function checks if the specified interrupt is enabled in the interrupt
  703. //! controller. The \e ui32Interrupt parameter must be one of the valid
  704. //! \b INT_* values listed in Peripheral Driver Library User's Guide and
  705. //! defined in the inc/hw_ints.h header file.
  706. //!
  707. //! \b Example: Disable the UART 0 interrupt if it is enabled.
  708. //!
  709. //! \verbatim
  710. //! //
  711. //! // Disable the UART 0 interrupt if it is enabled.
  712. //! //
  713. //! if(IntIsEnabled(INT_UART0))
  714. //! {
  715. //! IntDisable(INT_UART0);
  716. //! }
  717. //! \endverbatim
  718. //!
  719. //! \return A non-zero value if the interrupt is enabled.
  720. //
  721. //*****************************************************************************
  722. uint32_t
  723. IntIsEnabled(uint32_t ui32Interrupt)
  724. {
  725. uint32_t ui32Ret;
  726. //
  727. // Check the arguments.
  728. //
  729. ASSERT(ui32Interrupt < NUM_INTERRUPTS);
  730. //
  731. // Initialize the return value.
  732. //
  733. ui32Ret = 0;
  734. //
  735. // Determine the interrupt to disable.
  736. //
  737. if(ui32Interrupt == FAULT_MPU)
  738. {
  739. //
  740. // Check the MemManage interrupt.
  741. //
  742. ui32Ret = HWREG(NVIC_SYS_HND_CTRL) & NVIC_SYS_HND_CTRL_MEM;
  743. }
  744. else if(ui32Interrupt == FAULT_BUS)
  745. {
  746. //
  747. // Check the bus fault interrupt.
  748. //
  749. ui32Ret = HWREG(NVIC_SYS_HND_CTRL) & NVIC_SYS_HND_CTRL_BUS;
  750. }
  751. else if(ui32Interrupt == FAULT_USAGE)
  752. {
  753. //
  754. // Check the usage fault interrupt.
  755. //
  756. ui32Ret = HWREG(NVIC_SYS_HND_CTRL) & NVIC_SYS_HND_CTRL_USAGE;
  757. }
  758. else if(ui32Interrupt == FAULT_SYSTICK)
  759. {
  760. //
  761. // Check the System Tick interrupt.
  762. //
  763. ui32Ret = HWREG(NVIC_ST_CTRL) & NVIC_ST_CTRL_INTEN;
  764. }
  765. else if(ui32Interrupt >= 16)
  766. {
  767. //
  768. // Check the general interrupt.
  769. //
  770. ui32Ret = HWREG(g_pui32EnRegs[(ui32Interrupt - 16) / 32]) &
  771. (1 << ((ui32Interrupt - 16) & 31));
  772. }
  773. return(ui32Ret);
  774. }
  775. //*****************************************************************************
  776. //
  777. //! Pends an interrupt.
  778. //!
  779. //! \param ui32Interrupt specifies the interrupt to be pended.
  780. //!
  781. //! The specified interrupt is pended in the interrupt controller. The
  782. //! \e ui32Interrupt parameter must be one of the valid \b INT_* values listed
  783. //! in Peripheral Driver Library User's Guide and defined in the inc/hw_ints.h
  784. //! header file. Pending an interrupt causes the interrupt controller to
  785. //! execute the corresponding interrupt handler at the next available time,
  786. //! based on the current interrupt state priorities. For example, if called by
  787. //! a higher priority interrupt handler, the specified interrupt handler is not
  788. //! called until after the current interrupt handler has completed execution.
  789. //! The interrupt must have been enabled for it to be called.
  790. //!
  791. //! \b Example: Pend a UART 0 interrupt.
  792. //!
  793. //! \verbatim
  794. //! //
  795. //! // Pend a UART 0 interrupt.
  796. //! //
  797. //! IntPendSet(INT_UART0);
  798. //! \endverbatim
  799. //!
  800. //! \return None.
  801. //
  802. //*****************************************************************************
  803. void
  804. IntPendSet(uint32_t ui32Interrupt)
  805. {
  806. //
  807. // Check the arguments.
  808. //
  809. ASSERT(ui32Interrupt < NUM_INTERRUPTS);
  810. //
  811. // Determine the interrupt to pend.
  812. //
  813. if(ui32Interrupt == FAULT_NMI)
  814. {
  815. //
  816. // Pend the NMI interrupt.
  817. //
  818. HWREG(NVIC_INT_CTRL) |= NVIC_INT_CTRL_NMI_SET;
  819. }
  820. else if(ui32Interrupt == FAULT_PENDSV)
  821. {
  822. //
  823. // Pend the PendSV interrupt.
  824. //
  825. HWREG(NVIC_INT_CTRL) |= NVIC_INT_CTRL_PEND_SV;
  826. }
  827. else if(ui32Interrupt == FAULT_SYSTICK)
  828. {
  829. //
  830. // Pend the SysTick interrupt.
  831. //
  832. HWREG(NVIC_INT_CTRL) |= NVIC_INT_CTRL_PENDSTSET;
  833. }
  834. else if(ui32Interrupt >= 16)
  835. {
  836. //
  837. // Pend the general interrupt.
  838. //
  839. HWREG(g_pui32PendRegs[(ui32Interrupt - 16) / 32]) =
  840. 1 << ((ui32Interrupt - 16) & 31);
  841. }
  842. }
  843. //*****************************************************************************
  844. //
  845. //! Un-pends an interrupt.
  846. //!
  847. //! \param ui32Interrupt specifies the interrupt to be un-pended. The
  848. //! \e ui32Interrupt parameter must be one of the valid \b INT_* values listed
  849. //! in Peripheral Driver Library User's Guide and defined in the inc/hw_ints.h
  850. //! header file.
  851. //!
  852. //! The specified interrupt is un-pended in the interrupt controller. This
  853. //! causes any previously generated interrupts that have not been handled
  854. //! yet (due to higher priority interrupts or the interrupt not having been
  855. //! enabled yet) to be discarded.
  856. //!
  857. //! \b Example: Un-pend a UART 0 interrupt.
  858. //!
  859. //! \verbatim
  860. //! //
  861. //! // Un-pend a UART 0 interrupt.
  862. //! //
  863. //! IntPendClear(INT_UART0);
  864. //! \endverbatim
  865. //!
  866. //! \return None.
  867. //
  868. //*****************************************************************************
  869. void
  870. IntPendClear(uint32_t ui32Interrupt)
  871. {
  872. //
  873. // Check the arguments.
  874. //
  875. ASSERT(ui32Interrupt < NUM_INTERRUPTS);
  876. //
  877. // Determine the interrupt to unpend.
  878. //
  879. if(ui32Interrupt == FAULT_PENDSV)
  880. {
  881. //
  882. // Unpend the PendSV interrupt.
  883. //
  884. HWREG(NVIC_INT_CTRL) |= NVIC_INT_CTRL_UNPEND_SV;
  885. }
  886. else if(ui32Interrupt == FAULT_SYSTICK)
  887. {
  888. //
  889. // Unpend the SysTick interrupt.
  890. //
  891. HWREG(NVIC_INT_CTRL) |= NVIC_INT_CTRL_PENDSTCLR;
  892. }
  893. else if(ui32Interrupt >= 16)
  894. {
  895. //
  896. // Unpend the general interrupt.
  897. //
  898. HWREG(g_pui32UnpendRegs[(ui32Interrupt - 16) / 32]) =
  899. 1 << ((ui32Interrupt - 16) & 31);
  900. }
  901. }
  902. //*****************************************************************************
  903. //
  904. //! Sets the priority masking level
  905. //!
  906. //! \param ui32PriorityMask is the priority level that is masked.
  907. //!
  908. //! This function sets the interrupt priority masking level so that all
  909. //! interrupts at the specified or lesser priority level are masked. Masking
  910. //! interrupts can be used to globally disable a set of interrupts with
  911. //! priority below a predetermined threshold. A value of 0 disables priority
  912. //! masking.
  913. //!
  914. //! Smaller numbers correspond to higher interrupt priorities. So for example
  915. //! a priority level mask of 4 allows interrupts of priority level 0-3,
  916. //! and interrupts with a numerical priority of 4 and greater are blocked.
  917. //!
  918. //! \note The hardware priority mechanism only looks at the upper 3 bits of the
  919. //! priority level, so any prioritization must be performed in those bits.
  920. //!
  921. //! \b Example: Mask of interrupt priorities greater than or equal to 0x80.
  922. //!
  923. //! \verbatim
  924. //! //
  925. //! // Mask of interrupt priorities greater than or equal to 0x80.
  926. //! //
  927. //! IntPriorityMaskSet(0x80);
  928. //! \endverbatim
  929. //!
  930. //! \return None.
  931. //
  932. //*****************************************************************************
  933. void
  934. IntPriorityMaskSet(uint32_t ui32PriorityMask)
  935. {
  936. //
  937. // Set the priority mask.
  938. //
  939. CPUbasepriSet(ui32PriorityMask);
  940. }
  941. //*****************************************************************************
  942. //
  943. //! Gets the priority masking level
  944. //!
  945. //! This function gets the current setting of the interrupt priority masking
  946. //! level. The value returned is the priority level such that all interrupts
  947. //! of that and lesser priority are masked. A value of 0 means that priority
  948. //! masking is disabled.
  949. //!
  950. //! Smaller numbers correspond to higher interrupt priorities. So for example
  951. //! a priority level mask of 4 allows interrupts of priority level 0-3,
  952. //! and interrupts with a numerical priority of 4 and greater are blocked.
  953. //!
  954. //! The hardware priority mechanism only looks at the upper 3 bits of the
  955. //! priority level, so any prioritization must be performed in those bits.
  956. //!
  957. //! \b Example: Get the current interrupt priority mask.
  958. //!
  959. //! \verbatim
  960. //! //
  961. //! // Get the current interrupt priority mask.
  962. //! //
  963. //! IntPriorityMaskGet();
  964. //! \endverbatim
  965. //!
  966. //! \return Returns the value of the interrupt priority level mask.
  967. //
  968. //*****************************************************************************
  969. uint32_t
  970. IntPriorityMaskGet(void)
  971. {
  972. //
  973. // Return the current priority mask.
  974. //
  975. return(CPUbasepriGet());
  976. }
  977. //*****************************************************************************
  978. //
  979. //! Triggers an interrupt.
  980. //!
  981. //! \param ui32Interrupt specifies the interrupt to be triggered.
  982. //!
  983. //! This function performs a software trigger of an interrupt. The
  984. //! \e ui32Interrupt parameter must be one of the valid \b INT_* values listed
  985. //! in Peripheral Driver Library User's Guide and defined in the inc/hw_ints.h
  986. //! header file. The interrupt controller behaves as if the corresponding
  987. //! interrupt line was asserted, and the interrupt is handled in the same
  988. //! manner (meaning that it must be enabled in order to be processed, and the
  989. //! processing is based on its priority with respect to other unhandled
  990. //! interrupts).
  991. //!
  992. //! \return None.
  993. //
  994. //*****************************************************************************
  995. void
  996. IntTrigger(uint32_t ui32Interrupt)
  997. {
  998. //
  999. // Check the arguments.
  1000. //
  1001. ASSERT((ui32Interrupt >= 16) && (ui32Interrupt < NUM_INTERRUPTS));
  1002. //
  1003. // Trigger this interrupt.
  1004. //
  1005. HWREG(NVIC_SW_TRIG) = ui32Interrupt - 16;
  1006. }
  1007. //*****************************************************************************
  1008. //
  1009. // Close the Doxygen group.
  1010. //! @}
  1011. //
  1012. //*****************************************************************************