1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 // ==== IRQ Controller API ====
4 \defgroup irq_ctrl_gr Interrupts and Exceptions
5 \brief Generic functions to access the Interrupt Controller.
7 \details This section describes the device agnostic interrupt API viable for a wide range of specific interrupt controllers.
8 The IRQ Controller API allows interrupt dependend applications to be easily portable across a wide range of controllers.
10 \note The default implementation for \ref GIC_functions "ARM GIC (Generic Interrupt Controller)" can be found in \ref irq_ctrl_gic.c.
11 It uses \c weak functions thus it can easily be overwritten by an alternative user implementation if needed.
13 The ARMv7-A architecture defines a common set of first level exceptions, see table below.
15 | Exception | CMSIS Handler | Offset | Description |
16 |-------------------------------|---------------|--------|-----------------------------------------------------------------------------|
17 | Reset | Reset_Handler | 0x0000 | First instruction executed after reset. |
18 | Undefined Instruction (Undef) | Undef_Handler | 0x0004 | Signals usage of an illegal instructions. |
19 | Supervisor Call (SVC) | SVC_Handler | 0x0008 | Issued by software using SVC instruction. |
20 | Prefetch Abort (PAbt) | PAbt_Handler | 0x000C | Signals a memory abort on istruction fetch. |
21 | Data Abort (DAbt) | DAbt_Handler | 0x0010 | Signals a memory abort on data read or write. |
22 | Hyp Trap | (NOP) | 0x0014 | Hypervisor instruction trap, only available with Virtualization Extensions. |
23 | IRQ interrupt | IRQ_Handler | 0x0018 | Interrupt Request (typically from Interrupt Controller) |
24 | FIQ interrupt | FIQ_Handler | 0x001C | Fast Interrupt Request (typically from Interrupt Controller) |
26 By default those handlers are defined as weak empty functions by the \ref startup_c_sec "device specific startup code".
27 Software and peripheral interrupts are all handled by one of the both central interrupt handlers (IRQ and FIQ). These needs to
28 be implemented application specific. If an RTOS is used the interrupt handlers are typically provided by the RTOS, e.g. when using
29 <a href="../../RTOS2/html/rtx5_impl.html">RTX5</a>.
31 The interrupts available depends on the actual device in use. According to CMSIS specification the interrupts are defined
32 as \ref IRQn_Type in \ref device_h_pg. Using the generic IRQ API one can easily enable and disable interrupts, set up priorities, modes
33 and preemption rules, and register interrupt callbacks.
43 IRQ_ClearPending((IRQn_ID_t)SGI0_IRQn);
47 /* Initialize the Interrupt Controller */
50 /* Register the user defined handler function */
51 IRQ_SetHandler((IRQn_ID_t)SGI0_IRQn, SGI0_Handler);
53 /* Set the priority considering the priority grouping */
54 const uint32_t subprio = IRQ_GetPriorityGroupBits();
55 IRQ_SetPriority((IRQn_ID_t)SGI0_IRQn, 1u << subprio);
57 /* Set interrupt mode to falling edge */
58 IRQ_SetMode((IRQn_ID_t)SGI0_IRQn, IRQ_MODE_TYPE_IRQ | IRQ_MODE_CPU_0 | IRQ_MODE_TRIG_EDGE | IRQ_MODE_TRIG_EDGE_FALLING);
60 IRQ_Enable((IRQn_ID_t)SGI0_IRQn);
62 /* Trigger interrupt */
63 IRQ_SetPending((IRQn_ID_t)SGI0_IRQn);
65 IRQ_Disable((IRQn_ID_t)SGI0_IRQn);
73 \defgroup irq_mode_defs IRQ Mode Bit-Masks
74 \brief Configure interrupt line mode
77 The following codes are used as values for the parameter \em mode of the function \ref IRQ_SetMode to configure interrupt line mode.
78 They are also returned by the function \ref IRQ_GetMode when retrieving interrupt line mode.
80 The values of \b IRQ_MODE_TRIG_x definitions specify
81 The values of \b IRQ_MODE_TYPE_x definitions specify
82 The values of \b IRQ_MODE_DOMAIN_x definitions specify
83 The values of \b IRQ_MODE_CPU_x definitions specify
85 // Interrupt mode bit-masks
86 \def IRQ_MODE_TRIG_LEVEL
87 \def IRQ_MODE_TRIG_LEVEL_LOW
88 \def IRQ_MODE_TRIG_LEVEL_HIGH
89 \def IRQ_MODE_TRIG_EDGE
90 \def IRQ_MODE_TRIG_EDGE_RISING
91 \def IRQ_MODE_TRIG_EDGE_FALLING
92 \def IRQ_MODE_TRIG_EDGE_BOTH
94 \def IRQ_MODE_TYPE_IRQ
95 \def IRQ_MODE_TYPE_FIQ
97 \def IRQ_MODE_DOMAIN_NONSECURE
98 \def IRQ_MODE_DOMAIN_SECURE
100 \def IRQ_MODE_CPU_ALL
115 \defgroup irq_priority_defs IRQ Priority Bit-Masks
116 \brief Definitions used by interrupt priority functions.
119 The following values are used by the interrupt priority functions.
121 The value of \b IRQ_PRIORITY_Msk specifies maximum interrupt priority value and can be used as parameter for the functions
122 \ref IRQ_GetPriority and \ref IRQ_SetPriorityGroupBits to retrieve implementation specific priority values.
124 The value of \b IRQ_PRIORITY_ERROR is used by functions \ref IRQ_GetPriority, IRQ_GetPriorityMask and \ref IRQ_GetPriorityGroupBits
125 to signal function execution error.
127 \def IRQ_PRIORITY_Msk
128 \def IRQ_PRIORITY_ERROR
132 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
134 \fn int32_t IRQ_Initialize (void)
135 \details This function initializes interrupt controller.
137 It disables all interrupt sources, clears all pending interrupts, sets interrupt priorities to highest priority and
138 configures priority mask to lowest priority. IRQ and FIQ signal lines should be enabled and all interrupt handlers should
141 For ARM GIC the default implementation looks like the following example:
144 /// Number of implemented interrupt lines
145 #ifndef IRQ_GIC_LINE_COUNT
146 #define IRQ_GIC_LINE_COUNT (1020U)
149 static IRQHandler IRQTable[IRQ_GIC_LINE_COUNT] = { 0U };
151 int32_t IRQ_Initialize (void) {
154 for (i = 0U; i < IRQ_GIC_LINE_COUNT; i++) {
155 IRQTable[i] = (IRQHandler)NULL;
163 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
165 \fn int32_t IRQ_SetHandler (IRQn_ID_t irqn, IRQHandler_t handler)
166 \details This function registers address of the interrupt handler callback function corresponding to the specified interrupt
169 For ARM GIC the default implementation looks like the following example:
172 int32_t IRQ_SetHandler (IRQn_ID_t irqn, IRQHandler_t handler) {
175 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
176 IRQTable[irqn] = handler;
187 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
189 \fn IRQHandler_t IRQ_GetHandler (IRQn_ID_t irqn)
190 \details This function retrieves address of the interrupt handler callback function corresponding to the specified interrupt
193 For ARM GIC the default implementation looks like the following example:
196 IRQHandler_t IRQ_GetHandler (IRQn_ID_t irqn) {
199 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
210 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
212 \fn int32_t IRQ_Enable (IRQn_ID_t irqn)
213 \details This function enables forwarding of the corresponding interrupt to the CPU.
215 For ARM GIC the default implementation looks like the following example:
218 int32_t IRQ_Enable (IRQn_ID_t irqn) {
221 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
222 GIC_EnableIRQ ((IRQn_Type)irqn);
233 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
235 \fn int32_t IRQ_Disable (IRQn_ID_t irqn)
236 \details This function disables forwarding of the corresponding interrupt to the CPU.
238 For ARM GIC the default implementation looks like the following example:
241 int32_t IRQ_Disable (IRQn_ID_t irqn) {
244 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
245 GIC_DisableIRQ ((IRQn_Type)irqn);
256 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
258 \fn uint32_t IRQ_GetEnableState (IRQn_ID_t irqn)
259 \details This function retrieves the interrupt enable status of the interrupt identified by the irqn parameter.
261 Interrupt enable status can be either disabled (0) or enabled (1). Disabled status is returned for interrupts
262 which cannot be identified by irqn.
264 For ARM GIC the default implementation looks like the following example:
267 uint32_t IRQ_GetEnableState (IRQn_ID_t irqn) {
270 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
271 enable = GIC_GetEnableIRQ((IRQn_Type)irqn);
281 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
283 \fn int32_t IRQ_SetMode (IRQn_ID_t irqn, uint32_t mode)
285 \details This function configures the interrupt triggering mode, type, secure access and target CPUs of the interrupt
286 (see \ref irq_mode_defs) identified by the irqn parameter.
288 For ARM GIC the default implementation looks like the following example:
291 int32_t IRQ_SetMode (IRQn_ID_t irqn, uint32_t mode) {
300 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
301 // Check triggering mode
302 val = (mode & IRQ_MODE_TRIG_Msk);
304 if (val == IRQ_MODE_TRIG_LEVEL) {
306 } else if (val == IRQ_MODE_TRIG_EDGE) {
312 // Check interrupt type
313 val = mode & IRQ_MODE_TYPE_Msk;
315 if (val != IRQ_MODE_TYPE_IRQ) {
319 // Check interrupt domain
320 val = mode & IRQ_MODE_DOMAIN_Msk;
322 if (val == IRQ_MODE_DOMAIN_NONSECURE) {
325 // Check security extensions support
326 val = GIC_DistributorInfo() & (1UL << 10U);
329 // Security extensions are supported
336 // Check interrupt CPU targets
337 val = mode & IRQ_MODE_CPU_Msk;
339 if (val == IRQ_MODE_CPU_ALL) {
342 cpu = val >> IRQ_MODE_CPU_Pos;
345 // Apply configuration if no mode error
347 GIC_SetConfiguration((IRQn_Type)irqn, cfg);
348 GIC_SetTarget ((IRQn_Type)irqn, cpu);
351 GIC_SetGroup ((IRQn_Type)irqn, secure);
361 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
363 \fn uint32_t IRQ_GetMode (IRQn_ID_t irqn)
364 \details This function retrieves interrupt mode configuration of the interrupt identified by the irqn parameter.
365 \ref IRQ_MODE_ERROR is returned for interrupts which cannot be identified by irqn.
367 For ARM GIC the default implementation looks like the following example:
370 uint32_t IRQ_GetMode (IRQn_ID_t irqn) {
374 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
375 mode = IRQ_MODE_TYPE_IRQ;
378 val = GIC_GetConfiguration((IRQn_Type)irqn);
380 if ((val & 2U) != 0U) {
381 // Corresponding interrupt is edge triggered
382 mode |= IRQ_MODE_TRIG_EDGE;
384 // Corresponding interrupt is level triggered
385 mode |= IRQ_MODE_TRIG_LEVEL;
388 // Get interrupt CPU targets
389 mode |= GIC_GetTarget ((IRQn_Type)irqn) << IRQ_MODE_CPU_Pos;
392 mode = IRQ_MODE_ERROR;
400 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
402 \fn IRQn_ID_t IRQ_GetActiveIRQ (void)
403 \details This function retrieves the interrupt ID number of current IRQ source and acknowledges the interrupt.
405 For ARM GIC the default implementation looks like the following example:
408 IRQn_ID_t IRQ_GetActiveIRQ (void) {
411 irqn = (IRQn_ID_t)GIC_AcknowledgePending();
418 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
420 \fn IRQn_ID_t IRQ_GetActiveFIQ (void)
421 \details This function retrieves the interrupt ID number of current FIQ source and acknowledges the interrupt.
423 For ARM GIC the default implementation looks like the following example:
426 IRQn_ID_t IRQ_GetActiveFIQ (void) {
427 // FIQ is not supported, return invalid ID
428 return ((IRQn_ID_t)-1);
433 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
435 \fn int32_t IRQ_EndOfInterrupt (IRQn_ID_t irqn)
436 \details This function informs the interrupt controller that the interrupt service routine processing of the currently
437 active interrupt request is completed.
439 The parameter irqn should specify the value previously returned by the \ref IRQ_GetActiveIRQ or \ref IRQ_GetActiveFIQ functions.
441 For ARM GIC the default implementation looks like the following example:
444 int32_t IRQ_EndOfInterrupt (IRQn_ID_t irqn) {
447 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
448 GIC_EndInterrupt ((IRQn_Type)irqn);
464 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
466 \fn int32_t IRQ_SetPending (IRQn_ID_t irqn)
467 \details This function sets the pending status of the interrupt identified by the irqn parameter.
469 For ARM GIC the default implementation looks like the following example:
472 int32_t IRQ_SetPending (IRQn_ID_t irqn) {
475 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
476 GIC_SetPendingIRQ ((IRQn_Type)irqn);
487 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
489 \fn uint32_t IRQ_GetPending (IRQn_ID_t irqn)
490 \details This function retrieves the pending status of the interrupt identified by the irqn parameter.
492 Interrupt pending status can be either not pending (0) or pending (1). Not pending status is returned for interrupts which
493 cannot be identified by irqn.
495 For ARM GIC the default implementation looks like the following example:
498 uint32_t IRQ_GetPending (IRQn_ID_t irqn) {
501 if ((irqn >= 16) && (irqn < IRQ_GIC_LINE_COUNT)) {
502 pending = GIC_GetPendingIRQ ((IRQn_Type)irqn);
507 return (pending & 1U);
512 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
514 \fn int32_t IRQ_ClearPending (IRQn_ID_t irqn)
515 \details This function clears the pending status of the interrupt identified by the irqn parameter.
517 For ARM GIC the default implementation looks like the following example:
520 int32_t IRQ_ClearPending (IRQn_ID_t irqn) {
523 if ((irqn >= 16) && (irqn < IRQ_GIC_LINE_COUNT)) {
524 GIC_ClearPendingIRQ ((IRQn_Type)irqn);
535 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
537 \fn int32_t IRQ_SetPriority (IRQn_ID_t irqn, uint32_t priority)
538 \details This function sets the priority of the interrupt identified by the irqn parameter.
540 Higher priority numbers have lower priority. The highest interrupt priority has priority value 0, while the lowest value
541 depends on the number of implemented priority levels.
543 The number of implemented priority bits can be determined by setting value \ref IRQ_PRIORITY_Msk to arbitrary irqn and by
544 retrieving the actual stored value with IRQ_GetPriority function.
546 For ARM GIC the default implementation looks like the following example:
549 int32_t IRQ_SetPriority (IRQn_ID_t irqn, uint32_t priority) {
552 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
553 GIC_SetPriority ((IRQn_Type)irqn, priority);
564 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
566 \fn uint32_t IRQ_GetPriority (IRQn_ID_t irqn)
567 \details This function retrieves the priority of the interrupt identified by the irqn parameter.
569 The valid priority value can be from zero (0) to the value of \ref IRQ_PRIORITY_Msk. \ref IRQ_PRIORITY_ERROR bit is set in
570 returned value for interrupts which cannot be identified by irqn.
572 For ARM GIC the default implementation looks like the following example:
575 uint32_t IRQ_GetPriority (IRQn_ID_t irqn) {
578 if ((irqn >= 0) && (irqn < IRQ_GIC_LINE_COUNT)) {
579 priority = GIC_GetPriority ((IRQn_Type)irqn);
581 priority = IRQ_PRIORITY_ERROR;
589 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
591 \fn int32_t IRQ_SetPriorityMask (uint32_t priority)
592 \details This function sets the priority masking threshold for the current processor.
594 It ensures that only interrupts with a higher priority than priority threshold value are signaled to the target processor.
595 Function returns error status -1 if priority masking is not supported.
597 For ARM GIC the default implementation looks like the following example:
600 IRQ_SetPriorityMask (uint32_t priority) {
601 GIC_SetInterfacePriorityMask (priority);
607 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
609 \fn uint32_t IRQ_GetPriorityMask (void)
610 \details This function retrieves the priority masking threshold for the current processor.
612 \ref IRQ_PRIORITY_ERROR value is returned if priority masking is not supported.
614 For ARM GIC the default implementation looks like the following example:
617 uint32_t IRQ_GetPriorityMask (void) {
618 return GIC_GetInterfacePriorityMask();
623 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
625 \fn int32_t IRQ_SetPriorityGroupBits (uint32_t bits)
626 \details This function sets the number of MSB priority bits used to determine whether a pending interrupt has sufficient
627 priority to preempt a currently active interrupt.
629 The number of implemented group priority bits can be determined by setting value \ref IRQ_PRIORITY_Msk and by retrieving the
630 actual stored value with \ref IRQ_GetPriorityGroupBits function.
631 Function returns error status -1 if priority grouping is not supported.
633 For ARM GIC the default implementation looks like the following example:
636 int32_t IRQ_SetPriorityGroupBits (uint32_t bits) {
640 GIC_SetBinaryPoint (bits);
651 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
653 \fn uint32_t IRQ_GetPriorityGroupBits (void)
654 \details This function retrieves the number of MSB bits used to determine whether a pending interrupt has sufficient
655 priority to preempt a currently active interrupt.
657 \ref IRQ_PRIORITY_ERROR value is returned when priority grouping is not supported.
659 For ARM GIC the default implementation looks like the following example:
662 uint32_t IRQ_GetPriorityGroupBits (void) {
663 return (GIC_GetBinaryPoint() & 0x07U);
668 /** @} */ /* group irq_ctrl_gr */