PV198 - Timers and Interrupts One-chip Controllers Dávid Danaj, Marek Vrbka Faculty of Informatics, Masaryk University Content Content Interrupts What are Interrupts How Interrupts Work Interrupt Code Timers Timer overview Timer specifics D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 2 / 21 Content Intro Do you have any questions related to HW2? Switch your branch to Week_03! D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 3 / 21 Interrupts What are Interrupts What are Interrupts Signal to the processor indicating an event https://www.motioncontroltips.com/what-is-nested-vector-interrupt-control-nvic/ D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 4 / 21 Interrupts What are Interrupts Interrupt Types Level vs. Edge trigger HW vs. SW interrupt https://www.motioncontroltips.com/what-is-nested-vector-interrupt-control-nvic/ D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 5 / 21 Interrupts What are Interrupts NVIC NVIC – Nested Vector Interrupt Controller Up to 120 interrupt sources Up to 16 priority levels Nested interrupts 12 clock cycles to enter/exit an ISR (Interrupt Service Routine) 6 clock cycles when switching from one ISR to another D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 6 / 21 Interrupts How Interrupts Work Interrupt Operations 1. Interrupt is triggered 2. MCU finishes execution of the current instruction 3. Save current state (PC, flags, registers) 4. Get address of the ISR (Interrupt Service Routine) from the Interrupt Vector Table 5. Execute the ISR 6. Load previously saved state D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 7 / 21 Interrupts How Interrupts Work Tail Chaining https://www.nxp.com/docs/en/supporting-information/ Nested-Vector-Interrupt-Controller-Training.pdf D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 8 / 21 Interrupts How Interrupts Work Preemption https://www.nxp.com/docs/en/supporting-information/ Nested-Vector-Interrupt-Controller-Training.pdf D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 9 / 21 Interrupts Interrupt Code Preparation Task 1. Prepare a new empty project 2. Setup functional group for buttons 3. Enable GPIO peripheral (note: device-specific driver) D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 10 / 21 Interrupts Interrupt Code Interrupt Code 1. Configure interrupt for a pin Can be done in code or in the Pins tool 2. Enable interrupts for a port GPIOA for SW3 Can be done in code or in the Peripherals tool Define your handler name (Peripherals tool) 3. Write interrupt handler Function with the same name as you defined the handler name Clear the flag that triggers the interrupt (what will happen if you don’t?) void SW3_BUTTON_PRESSED_IRQ(void) { GPIO_PortClearInterruptFlags(BOARD_SW3_GPIO, 1U << BOARD_SW3_GPIO_PIN); g_ButtonSw3Press = true; } D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 11 / 21 Interrupts Interrupt Code Task Write an application that prints text to console when SW3 is pressed Use interrupts D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 12 / 21 Timers Timer overview Timers We could also call them counters They measure time intervals by counting Clock cycles Events External clocks We can use them to measure time intervals Periodic execution Delay implementation D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 13 / 21 Timers Timer overview Periodic Interrupt Timer (PIT) https://www.mouser.com/datasheet/2/813/K66P144M180SF5RMV2-1074869.pdf, page 1268 D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 14 / 21 Timers Timer overview FRDM-K66F Timers Programmable delay block (PDB) Flexible timer modules (FTM) Low Power TPM (TPM) Periodic interrupt timers (PIT) Low-power timer (LPTimer) Carrier modulator timer (CMT) Real-time clock (RTC) IEEE 1588 timers D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 15 / 21 Timers Timer overview PIT Features 4 timers Ability of timers to generate interrupts Independent timeout periods for each timer Chaining of timers D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 16 / 21 Timers Timer specifics PIT configuration behavior D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 17 / 21 Timers Timer specifics Simple use example Set value to a timer register Timer Load Value Register (PIT_LDVALn) The timer will count down until it reaches 0, then it will generate an interrupt and load this register value again D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 18 / 21 Timers Timer specifics Which value to set? Interrupt every 0.5 seconds Bus clock: 60 MHz → clock period = 16.67 ns LDVAL trigger = (period/clockperiod) − 1 0.5 s/16.67 ns = 30000000 − 1 = 29999999 cycles D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 19 / 21 Timers Timer specifics Task 2 Write an application that prints text to console every 1 second Update it to toggle LED every 0.5 seconds Setup the system in a way that timer is enabled with SW3 button D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 20 / 21 Timers Timer specifics Homework - Debounce a button using Timer Interrupts Use GPIO interrupt to detect first edge on the SW3 button When GPIO is triggered, start Timer and wait for constant time When Timer interrupt triggers, check GPIO value Print anything to console when press signal is accepted D. Danaj, M. Vrbka ·PV198 - Timers and Interrupts · 21 / 21