PV198 – One-chip Controllers, Interrupts / Dávid Danaj 1 PV198 – One-chip Controllers Interrupts PV198 – One-chip Controllers, Interrupts / Dávid Danaj 2 Content 1.What is Interrupt 2.How does it work 3.NVIC – Nested Vector Interrupt Controller 4.Code 5.Application 6. 6. 6. PV198 – One-chip Controllers, Interrupts / Dávid Danaj 3 Interrupts – What it is §Signal to the processor indicating an event § https://www.motioncontroltips.com/what-is-nested-vector-interrupt-control-nvic/ PV198 – One-chip Controllers, Interrupts / Dávid Danaj 4 Interrupts – Types §Level / Edge trigger §HW / SW interrupt § § § https://www.motioncontroltips.com/what-is-nested-vector-interrupt-control-nvic/ PV198 – One-chip Controllers, Interrupts / Dávid Danaj 5 Interrupts – How does it work §Interrupt is triggered §MCU finish execution of current instruction §Save current state (PC, flags, registers) §Get address of an ISR (Interrupt Service Routine) from Interrupt Vector Table §Execute ISR §Load previously saved state and continue § § § § § PV198 – One-chip Controllers, Interrupts / Dávid Danaj 6 Interrupts – NVIC (FRDM-K66F) §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 § § § § PV198 – One-chip Controllers, Interrupts / Dávid Danaj 7 Interrupts §Tail Chaining http://cache.freescale.com/files/training/Nested-Vector-Interrupt-Controller-Training.pdf PV198 – One-chip Controllers, Interrupts / Dávid Danaj 8 Interrupts §Preemption http://cache.freescale.com/files/training/Nested-Vector-Interrupt-Controller-Training.pdf PV198 – One-chip Controllers, Interrupts / Dávid Danaj 9 Interrupts – Code (GPIO button) 1.Configure interrupt for a pin 2.Enable interrupts for a port 3.Write interrupt handler PV198 – One-chip Controllers, Interrupts / Dávid Danaj 10 Interrupts – Code (GPIO button) §Configure interrupt for a pin (button) – can be done in Pins tool /* Interrupt configuration on PORTA10 (pin M9): Interrupt on falling edge */ PORT_SetPinInterruptConfig(BOARD_SW3_PORT, BOARD_SW3_PIN, kPORT_InterruptFallingEdge); PV198 – One-chip Controllers, Interrupts / Dávid Danaj 11 Interrupts – Code (GPIO button) §Enable interrupts for a port (GPIOA for SW3) – can be done in Peripherals tool §(Optionally) Define your handler name – Peripherals tool /* Enable interrupt PORTA_IRQn request in the NVIC */ EnableIRQ(PORTA_IRQn); /* GPIO_1 interrupt handler identifier. */ #define SW3_BUTTON_PRESSED_IRQ PORTA_IRQHandler PV198 – One-chip Controllers, Interrupts / Dávid Danaj 12 Interrupts – Code (GPIO button) §Write interrupt handler §Don’t forget to clear a flag that triggers interrupt § /** * Interrupt handler for button SW3. * Set flag when SW3 button is pressed. */ void SW3_BUTTON_PRESSED_IRQ(void) { /* Clear external interrupt flag. */ GPIO_PortClearInterruptFlags(BOARD_SW3_GPIO, 1U << BOARD_SW3_GPIO_PIN); /* Change state of button. */ g_ButtonSw3Press = true; } Application §Goal: §Write an application that prints text to a console when SW3 button is pressed. Use interrupts. § §You can use the application from the previous lesson and modify it to use interrupt instead of polling PV198 – One-chip Controllers, Interrupts / Dávid Danaj 13