Advanced Use Case 1
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
Initialization code
Add to the application initialization code:
void pio_set_input(Pio *p_pio, const uint32_t ul_mask, const uint32_t ul_attribute)
Configure one or more pin(s) or a PIO controller as inputs.
void pio_enable_interrupt(Pio *p_pio, const uint32_t ul_mask)
Enable the given interrupt source.
void pio_set_output(Pio *p_pio, const uint32_t ul_mask, const uint32_t ul_default_level, const uint32_t ul_multidrive_enable, const uint32_t ul_pull_up_enable)
Configure one or more pin(s) of a PIO controller as outputs, with the given default value.
uint32_t pio_handler_set(Pio *p_pio, uint32_t ul_id, uint32_t ul_mask, uint32_t ul_attr, void(*p_handler)(uint32_t, uint32_t))
Set an interrupt handler for the provided pins.
#define PIO_PA16
Pin Controlled by PA16.
#define PIO_PA23
Pin Controlled by PA23.
#define PIOA
(PIOA ) Base Address
#define ID_PIOA
Parallel I/O Controller A (PIOA).
@ PIOA_IRQn
11 SAM4SD32C Parallel I/O Controller A (PIOA)
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:
- Enable the interrupt for the configured input pin:
- Enable interrupt handling from the PIOA module:
Example code
Add the following function to your application:
void pin_edge_handler(const uint32_t id, const uint32_t index)
{
else
}
}
uint32_t pio_get(Pio *p_pio, const pio_type_t ul_type, const uint32_t ul_mask)
Return 1 if one or more PIOs of the given Pin instance currently have a high level; otherwise returns...
void pio_clear(Pio *p_pio, const uint32_t ul_mask)
Set a low output level on all the PIOs defined in ul_mask.
void pio_set(Pio *p_pio, const uint32_t ul_mask)
Set a high output level on all the PIOs defined in ul_mask.
#define PIO_TYPE_PIO_INPUT
Workflow
- We check the value of the pin:
- Then we set the new output value based on the read pin value: