Advanced use case - Interrupt driven edge detection
This section will present a more advanced use case for the PIO driver. This use case will configure pin 23 on port A as output and pin 16 as an input with pullup, and then toggle the output pin's value to match that of the input pin using the interrupt controller within the device.
Prerequisites
- Power Management Controller driver
Initialization code
Add to the application initialization code:
pio_handler_set(PIOA, ID_PIOA, PIO_PA16,
PIO_IT_EDGE, pin_edge_handler);
NVIC_EnableIRQ(PIOA_IRQn);
Workflow
- Enable the module clock to the PIOA peripheral:
- Set pin 23 direction on PIOA as output, default low level:
- Set pin 16 direction on PIOA as input, with pullup:
- Configure the input pin 16 interrupt mode and handler:
pio_handler_set(PIOA, ID_PIOA, PIO_PA16,
PIO_IT_EDGE, pin_edge_handler);
- Enable the interrupt for the configured input pin:
- Enable interrupt handling from the PIOA module:
NVIC_EnableIRQ(PIOA_IRQn);
Example code
Add the following function to your application:
void pin_edge_handler(const uint32_t id, const uint32_t index)
{
if ((id == ID_PIOA) && (index == PIO_PA16)){
else
}
}
Workflow
- We check the value of the pin:
- Then we set the new output value based on the read pin value: