SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
Advanced use case - Interrupt driven edge detection

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:

pmc_enable_periph_clk(ID_PIOA);
pio_set_output(PIOA, PIO_PA23, LOW, DISABLE, ENABLE);
NVIC_EnableIRQ(PIOA_IRQn);
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.
Definition pio.c:257
void pio_enable_interrupt(Pio *p_pio, const uint32_t ul_mask)
Enable the given interrupt source.
Definition pio.c:578
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.
Definition pio.c:310
#define PIO_PULLUP
Definition pio.h:86
#define PIO_IT_EDGE
Definition pio.h:101
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
Definition sam4sd32c.h:447
#define ID_PIOA
Parallel I/O Controller A (PIOA).
Definition sam4sd32c.h:331
@ PIOA_IRQn
11 SAM4SD32C Parallel I/O Controller A (PIOA)
Definition sam4sd32c.h:85

Workflow

  1. Enable the module clock to the PIOA peripheral:
    pmc_enable_periph_clk(ID_PIOA);
  2. Set pin 23 direction on PIOA as output, default low level:
    pio_set_output(PIOA, PIO_PA23, LOW, DISABLE, ENABLE);
  3. Set pin 16 direction on PIOA as input, with pullup:
  4. Configure the input pin 16 interrupt mode and handler:
  5. Enable the interrupt for the configured input pin:
  6. 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
}
}
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...
Definition pio.c:148
void pio_clear(Pio *p_pio, const uint32_t ul_mask)
Set a low output level on all the PIOs defined in ul_mask.
Definition pio.c:130
void pio_set(Pio *p_pio, const uint32_t ul_mask)
Set a high output level on all the PIOs defined in ul_mask.
Definition pio.c:117
#define PIO_TYPE_PIO_INPUT
Definition pio.h:64

Workflow

  1. We check the value of the pin:
  2. Then we set the new output value based on the read pin value: