Lab7
Lab7
Global level configurations of interrupts are handled at processor level. These are handled by
PRIMASK, FAULTMASK and BASEPRI registers. The default values of these registers are
such that all interrupts are enabled. However, in case of critical code execution, that should not
be interrupted, global interrupt enabling and disabling can be performed. For that purpose,
either the PRIMASK or FAULTMASK register can be used. The PRIMASK register prevents
activation of all exceptions with configurable priority. The FAULTMASK register prevents
activation of all exceptions except for Non-Maskable Interrupt (NMI).
Copyright © 2023 Department of Electrical Engineering, University of Engineering and Technology Lahore,
Pakistan. Permission is granted to copy and distribute for educational purpose. However, any commercial use
of the material, in any form, is not allowed.
Base Priority Register
The flexibility in masking the interrupts is introduced by the BASEPRI register. The interrupt
masking by the BASEPRI is performed depending on the current priority level configuration.
Since the number of priority levels in TM4C123GH6PM is 8, it requires 3 bits to be used by
the BASEPRI register (Figure 7.1).
When BASEPRI is set to a non-zero value, it blocks all the interrupts of the same and lower
priority, while it allows the processor to accept the interrupts of higher priority for process-
ing. When BASEPRI is set to 0, it is disabled. These interrupt masking registers are accessed
through special register access instructions. Move to special register from general-purpose regis-
ter (MSR) and move special register to general-purpose register (MRS) assembly programming
instructions are used for this purpose. Add the following lines of code before the Re-
set Handler in your startup file.
EXPORT DisableInterrupts
EXPORT EnableInterrupts
EXPORT EnablePriorityInterrupts
EXPORT WaitForInterrupt
DisableInterrupts
CPSID I ; s e t PRIMASK t o d i s a b l e interrupts
BX LR
EnableInterrupts
CPSIE I ; c l e a r PRIMASK t o e n a b l e i n t e r r u p t s
BX LR
EnablePriorityInterrupts
MOV R0 , #0 x 4 0 ; s e t b a s e p r i o r i t y 2
MSR BASEPRI , R0
BX LR
WaitForInterrupt
WFI
BX LR
NVIC Configuration
Note: The register map for NVIC is to be consulted from article 3.2 - Cortex-M4 Periph-
erals - Register Map of the controller datasheet.
Copyright © 2023 Department of Electrical Engineering, University of Engineering and Technology Lahore,
Pakistan. Permission is granted to copy and distribute for educational purpose. However, any commercial use
of the material, in any form, is not allowed.
To activate an interrupt source, following two steps must be followed:
For better understanding, we discuss the example of enabling the interrupt for Port F. Follow
the following steps to activate the interrupt for port F.
1. Find the interrupt number (i.e., bit position in interrupt register) from Table 2-9 (2nd
column) corresponding to GPIO Port F (Figure 7.2).
Interrupt Number
2. Find the interrupt register that is needed to enable IRQ30 from Table 3-8. It is NVIC EN0 R.
So, it tells you that you need to set bit 30 of NVIC EN0 R to 1 to enable interrupt on
Port F (Figure 7.3).
Interrupt Enable
3. From Table 3-8, find the register needed to set the priority of IRQ 30. It is NVIC PRI7 R
(Figure 7.4).
Interrupt Priority
To set a priority value, say 5, you may use the following statement in C:
NVIC_PRI7_R = ( NVIC_PRI7_R & 0 xFF00FFFF ) | 0 x00A00000 ;
Copyright © 2023 Department of Electrical Engineering, University of Engineering and Technology Lahore,
Pakistan. Permission is granted to copy and distribute for educational purpose. However, any commercial use
of the material, in any form, is not allowed.
Configuring GPIO (Device) as Interrupt
Note: The register map for GPIO is to be consulted from article 10.4 - General-Purpose
Input/Outputs (GPIOs) - Register Map of the controller datasheet.
To configure GPIO pin as interrupt and select the source of the interrupt, its polarity and edge
properties following steps must be followed:
1. Disable the interrupts before writing to the control registers from GPIO Interrupt Mask
register (GPIOIM) Figure 7.5.
2. Select whether the source of interrupt is edge-sensitive or level sensitive using GPIO
Interrupt Sense register (GPIOIS) Figure 7.6.
3. To enable interrupts for both edges write the appropriate value in the GPIO Interrupt
Copyright © 2023 Department of Electrical Engineering, University of Engineering and Technology Lahore,
Pakistan. Permission is granted to copy and distribute for educational purpose. However, any commercial use
of the material, in any form, is not allowed.
Both Edges register(GPIOIBE) Figure 7.7.
4. Write the appropriate value in GPIO Interrupt Event register (GPIOIEV) to configure
the corresponding pin to detect rising (high level) or falling (low level) edges depending
on the corresponding bit value in the GPIO Interrupt Sense (GPIOIS) register Figure 7.8.
5. Clear the interrupt flag for the corresponding pin by asserting the appropriate bit in the
GPIO Interrupt Clear Register (GPIOICR) Figure 7.9.
6. Enable the interrupts by asserting the corresponding bits in GPIO Interrupt Mask register
(GPIOIM) Figure 7.5.
Copyright © 2023 Department of Electrical Engineering, University of Engineering and Technology Lahore,
Pakistan. Permission is granted to copy and distribute for educational purpose. However, any commercial use
of the material, in any form, is not allowed.
GPIO Interrupt Clear Register
Exercise
Configure an interrupt on SW1 (PF4) that is falling edge triggered and has a priority 5. Write
a code that blinks LEDs of different colours on switch1 press. Template is provided on piazza.
Complete the missing portions. Consult the datasheet where needed.
Copyright © 2023 Department of Electrical Engineering, University of Engineering and Technology Lahore,
Pakistan. Permission is granted to copy and distribute for educational purpose. However, any commercial use
of the material, in any form, is not allowed.