PP6a.gpio Interrupts

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

GPIO Interrupt Example

Using GPIOs as interrupt sources

Interrupt Summary
Determine the name of the interrupt
handler (from vector table) & interrupt
index (from derivative.h)
Create interrupt handler function
Create interrupt setup function

11/09/2015

EEE20003 Embedded Microcontrollers

Interrupt handler
Use whatever name is given in the
vector table.
Should clear the interrupt flag (if it
exists) check hardware details.
Carry out whatever function is needed.
May need to do some setup for the
next interrupt e.g. schedule timer.

11/09/2015

EEE20003 Embedded Microcontrollers

Interrupt Setup
Initialise hardware
Do whatever setup is required e.g. set tick
rate etc.
Enable interrupt signal (unmask it) in
hardware

Enable interrupt signal (unmask it) in


NVIC
NVIC_EnableIrq(int interruptIndex)

11/09/2015

EEE20003 Embedded Microcontrollers

IRQ Handler or ISR


The interrupt handler resembles a subroutine
or C function.
Its address must be placed in the appropriate
entry of the vector table so that it may be
called when triggered.
The Cortex-M family uses a interrupt
technique that allows the direct use of C
functions as interrupt handlers.
The correct registers are saved and restored
by hardware (matches ARM-ABI used by C).
11/09/2015

EEE20003 Embedded Microcontrollers

Example - IRQ Handler for GPIO


Simple IRQ source button on port pin.
MK20
Internal
Pull-up
IRQ*

11/09/2015

IRQ PTC.0

EEE20003 Embedded Microcontrollers

IRQ on GPIO PCR setup


Set up PORTC->PCR[n]
as usual but also select an interrupt function.

Interrupt Flag
w1c = write-1-to-clear

Controls Interrupt
function

11/09/2015

0000
0001
0010
0011
0100
1000
1001
1010
1011
1100

Interrupt/DMA request disabled.


DMA request on rising edge.
DMA request on falling edge.
DMA request on either edge.
Reserved.
Interrupt when logic zero.
Interrupt on rising edge.
Interrupt on falling edge.
Interrupt on either edge.
Interrupt when logic one.

EEE20003 Embedded Microcontrollers

IRQ on GPIO Setup


/*!
* Initialises Port C.0 as an Interrupt pin
*/
void initialisePortInterrupt(void) {
// Enable clock to PORTC
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;

Write PORT_PCR_IRQC(10) to
enable falling edge
interrupts on port pin

// Configure as port pin with interrupts


PORTC->PCR[0] = PORT_PCR_MUX(1)|PORT_PCR_IRQC(10)|PORT_PCR_ISF_MASK|
PORT_PCR_PFE_MASK|PORT_PCR_PE_MASK|PORT_PCR_PS_MASK;

// Enable PORTC interrupts in NVIC


NVIC_EnableIrq(INT_PORTC);

Enable matching IRQ in NVIC

Controls Interrupt
function
11/09/2015

...
1010 Interrupt on falling edge.
...

EEE20003 Embedded Microcontrollers

IRQ Input on PORTC.0


Name of function must match the

/*!
weak name used in vector table
* PORTC Interrupt Handler
* Toggles a pin
*/
Writing PORT_PCR_ISF_MASK
void PORTC_IRQHandler(void) {
// Toggle the LED
PORTC_PCR will clear the
GPIOA->PTOR = LED_MASK;
// Clear the interrupt flag
PORTC->PCR[0] = PORT_PCR_MUX(1)|PORT_PCR_IRQC(10)|PORT_PCR_ISF_MASK|
PORT_PCR_PFE_MASK|PORT_PCR_PE_MASK|PORT_PCR_PS_MASK;
}

to
flag

Interrupt Flag
Set by hardware
Cleared by software (w1c = write-1-to-clear)
11/09/2015

EEE20003 Embedded Microcontrollers

Vector table (kinetis_sysinit.c)


void Default_Handler(void) {
uint32_t vectorNum = SCB_ICSR;
(void)vectorNum;
while (1) {
Unhandled exceptions
asm("bkpt #0");
end up here!
}
}
#define WEAK_DEFAULT_HANDLER __attribute__ ((__weak__, alias("Default_Handler")))

void PORTC_IRQHandler(void)

WEAK_DEFAULT_HANDLER;

VectorTable const __vector_table = {


/* ... */
PORTC_IRQHandler,
/* Vec #58 - Int #42 Port C interrupt */
/* ... */
}
Entry in vector table points to

PORTC_IRQHandler()

The above arrangement means:


Unhandled interrupts are caught by the Default_Handler()
No changes are required to the file to add an ISR just name the ISR
correctly (e.g. PORTC_IRQHandler) and it overrides the default handler.
11/09/2015

EEE20003 Embedded Microcontrollers

10

You might also like