Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
GPIO - General-Purpose Input/Output

GPIO gives access to the MCU pins.

Data Structures

struct  gpio_map_t
 A type definition of pins and modules connectivity. More...
 

Peripheral Bus Interface

static uint32_t gpio_configure_edge_detector (uint32_t pin, uint32_t mode)
 Configure the edge detector of an input pin. More...
 

Return Values of the GPIO API

#define GPIO_SUCCESS   0
 Function successfully completed. More...
 
#define GPIO_INVALID_ARGUMENT   1
 Input parameters are out of range. More...
 

Interrupt Trigger Modes

#define GPIO_PIN_CHANGE   0
 Interrupt triggered upon pin change. More...
 
#define GPIO_RISING_EDGE   1
 Interrupt triggered upon rising edge. More...
 
#define GPIO_FALLING_EDGE   2
 Interrupt triggered upon falling edge. More...
 

Common defines for GPIO_FLAGS parameter

#define GPIO_DIR_INPUT   (0 << 0)
 Pin is Input. More...
 
#define GPIO_DIR_OUTPUT   (1 << 0)
 Pin is Output. More...
 
#define GPIO_INIT_LOW   (0 << 1)
 Initial Output State is Low. More...
 
#define GPIO_INIT_HIGH   (1 << 1)
 Initial Output State is High. More...
 
#define GPIO_PULL_UP   (1 << 2)
 Pull-Up (when input) More...
 
#define GPIO_PULL_DOWN   (2 << 2)
 Pull-Down (when input) More...
 
#define GPIO_BUSKEEPER   (3 << 2)
 Bus Keeper. More...
 
#define GPIO_DRIVE_MIN   (0 << 4)
 Drive Min Configuration. More...
 
#define GPIO_DRIVE_LOW   (1 << 4)
 Drive Low Configuration. More...
 
#define GPIO_DRIVE_HIGH   (2 << 4)
 Drive High Configuration. More...
 
#define GPIO_DRIVE_MAX   (3 << 4)
 Drive Max Configuration. More...
 
#define GPIO_OPEN_DRAIN   (1 << 6)
 Open-Drain (when output) More...
 
#define GPIO_INTERRUPT   (1 << 7)
 Enable Pin/Group Interrupt. More...
 
#define GPIO_BOTHEDGES   (3 << 7)
 Sense Both Edges. More...
 
#define GPIO_RISING   (5 << 7)
 Sense Rising Edge. More...
 
#define GPIO_FALLING   (7 << 7)
 Sense Falling Edge. More...
 

Peripheral Bus Interface

Low-speed interface with a non-deterministic number of clock cycles per access.

This interface operates with lower clock frequencies (fPB <= fCPU), and its timing is not deterministic since it needs to access a shared bus which may be heavily loaded.

Note
This interface is immediately available without initialization.
uint32_t gpio_enable_module (const gpio_map_t gpiomap, uint32_t size)
 Enables specific module modes for a set of pins. More...
 
uint32_t gpio_enable_module_pin (uint32_t pin, uint32_t function)
 Enables a specific module mode for a pin. More...
 
void gpio_enable_gpio (const gpio_map_t gpiomap, uint32_t size)
 Enables the GPIO mode of a set of pins. More...
 
void gpio_enable_gpio_pin (uint32_t pin)
 Enables the GPIO mode of a pin. More...
 
void gpio_enable_pin_pull_up (uint32_t pin)
 Enables the pull-up resistor of a pin. More...
 
void gpio_disable_pin_pull_up (uint32_t pin)
 Disables the pull-up resistor of a pin. More...
 
void gpio_configure_pin (uint32_t pin, uint32_t flags)
 Configuration functionality on a pin. More...
 
void gpio_configure_group (uint32_t port, uint32_t mask, uint32_t flags)
 Configuration functionality on a port. More...
 
bool gpio_get_pin_value (uint32_t pin)
 Returns the value of a pin. More...
 
static __always_inline bool gpio_pin_is_low (uint32_t pin)
 Check if the pin is in low logical level. More...
 
static __always_inline bool gpio_pin_is_high (uint32_t pin)
 Check if the pin is in high logical level. More...
 
bool gpio_get_gpio_pin_output_value (uint32_t pin)
 Returns the output value set for a GPIO pin. More...
 
bool gpio_get_gpio_open_drain_pin_output_value (uint32_t pin)
 Returns the output value set for a GPIO pin using open drain. More...
 
void gpio_set_gpio_pin (uint32_t pin)
 Drives a GPIO pin to 1. More...
 
void gpio_set_pin_high (uint32_t pin)
 Drives a GPIO pin to 1. More...
 
void gpio_set_group_high (uint32_t port, uint32_t mask)
 Drives a GPIO port to 1. More...
 
void gpio_clr_gpio_pin (uint32_t pin)
 Drives a GPIO pin to 0. More...
 
void gpio_set_pin_low (uint32_t pin)
 Drives a GPIO pin to 0. More...
 
void gpio_set_group_low (uint32_t port, uint32_t mask)
 Drives a GPIO port to 0. More...
 
void gpio_tgl_gpio_pin (uint32_t pin)
 Toggles a GPIO pin. More...
 
void gpio_toggle_pin (uint32_t pin)
 Toggles a GPIO pin. More...
 
void gpio_toggle_group (uint32_t port, uint32_t mask)
 Toggles a GPIO group. More...
 
void gpio_set_gpio_open_drain_pin (uint32_t pin)
 Drives a GPIO pin to 1 using open drain. More...
 
void gpio_clr_gpio_open_drain_pin (uint32_t pin)
 Drives a GPIO pin to 0 using open drain. More...
 
void gpio_tgl_gpio_open_drain_pin (uint32_t pin)
 Toggles a GPIO pin using open drain. More...
 
void gpio_enable_pin_glitch_filter (uint32_t pin)
 Enables the glitch filter of a pin. More...
 
void gpio_disable_pin_glitch_filter (uint32_t pin)
 Disables the glitch filter of a pin. More...
 
uint32_t gpio_enable_pin_interrupt (uint32_t pin, uint32_t mode)
 Enables the interrupt of a pin with the specified settings. More...
 
void gpio_disable_pin_interrupt (uint32_t pin)
 Disables the interrupt of a pin. More...
 
bool gpio_get_pin_interrupt_flag (uint32_t pin)
 Gets the interrupt flag of a pin. More...
 
void gpio_clear_pin_interrupt_flag (uint32_t pin)
 Clears the interrupt flag of a pin. More...
 

Local Bus Interface

High-speed interface with only one clock cycle per access.

This interface operates with high clock frequency (fCPU), and its timing is deterministic since it does not need to access a shared bus which may be heavily loaded.

Warning
To use this interface, the clock frequency of the peripheral bus on which the GPIO peripheral is connected must be set to the CPU clock frequency (fPB = fCPU).
Note
This interface has to be initialized in order to be available.
static __always_inline void gpio_local_init (void)
 Enables the local bus interface for GPIO. More...
 
static __always_inline void gpio_local_enable_pin_output_driver (uint32_t pin)
 Enables the output driver of a pin. More...
 
static __always_inline void gpio_local_disable_pin_output_driver (uint32_t pin)
 Disables the output driver of a pin. More...
 
static __always_inline bool gpio_local_get_pin_value (uint32_t pin)
 Returns the value of a pin. More...
 
static __always_inline void gpio_local_set_gpio_pin (uint32_t pin)
 Drives a GPIO pin to 1. More...
 
static __always_inline void gpio_local_clr_gpio_pin (uint32_t pin)
 Drives a GPIO pin to 0. More...
 
static __always_inline void gpio_local_tgl_gpio_pin (uint32_t pin)
 Toggles a GPIO pin. More...
 
static __always_inline void gpio_local_init_gpio_open_drain_pin (uint32_t pin)
 Initializes the configuration of a GPIO pin so that it can be used with GPIO open-drain functions. More...
 
static __always_inline void gpio_local_set_gpio_open_drain_pin (uint32_t pin)
 Drives a GPIO pin to 1 using open drain. More...
 
static __always_inline void gpio_local_clr_gpio_open_drain_pin (uint32_t pin)
 Drives a GPIO pin to 0 using open drain. More...
 
static __always_inline void gpio_local_tgl_gpio_open_drain_pin (uint32_t pin)
 Toggles a GPIO pin using open drain. More...
 

Peripheral Event System support

The GPIO can be programmed to output peripheral events whenever an interrupt condition is detected, such as pin value change, or only when a rising or falling edge is detected.

static __always_inline void gpio_enable_pin_periph_event (uint32_t pin)
 Enables the peripheral event generation of a pin. More...
 
static __always_inline void gpio_disable_pin_periph_event (uint32_t pin)
 Disables the peripheral event generation of a pin. More...
 
uint32_t gpio_configure_pin_periph_event_mode (uint32_t pin, uint32_t mode, uint32_t use_igf)
 

#define GPIO_BOTHEDGES   (3 << 7)

Sense Both Edges.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_BUSKEEPER   (3 << 2)

Bus Keeper.

#define GPIO_DIR_INPUT   (0 << 0)

Pin is Input.

#define GPIO_DIR_OUTPUT   (1 << 0)

Pin is Output.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_DRIVE_HIGH   (2 << 4)

Drive High Configuration.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_DRIVE_LOW   (1 << 4)

Drive Low Configuration.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_DRIVE_MAX   (3 << 4)

Drive Max Configuration.

#define GPIO_DRIVE_MIN   (0 << 4)

Drive Min Configuration.

#define GPIO_FALLING   (7 << 7)

Sense Falling Edge.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_FALLING_EDGE   2

Interrupt triggered upon falling edge.

Referenced by gpio_configure_edge_detector().

#define GPIO_INIT_HIGH   (1 << 1)

Initial Output State is High.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_INIT_LOW   (0 << 1)

Initial Output State is Low.

#define GPIO_INTERRUPT   (1 << 7)

Enable Pin/Group Interrupt.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_INVALID_ARGUMENT   1

Input parameters are out of range.

Referenced by gpio_configure_edge_detector(), gpio_enable_module_pin(), and gpio_enable_pin_interrupt().

#define GPIO_OPEN_DRAIN   (1 << 6)

Open-Drain (when output)

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_PIN_CHANGE   0

Interrupt triggered upon pin change.

Referenced by gpio_configure_edge_detector().

#define GPIO_PULL_DOWN   (2 << 2)

Pull-Down (when input)

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_PULL_UP   (1 << 2)

Pull-Up (when input)

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_RISING   (5 << 7)

Sense Rising Edge.

Referenced by gpio_configure_group(), and gpio_configure_pin().

#define GPIO_RISING_EDGE   1

Interrupt triggered upon rising edge.

Referenced by gpio_configure_edge_detector().

#define GPIO_SUCCESS   0

void gpio_clear_pin_interrupt_flag ( uint32_t  pin)

Clears the interrupt flag of a pin.

Parameters
pinThe pin number.
void gpio_clr_gpio_open_drain_pin ( uint32_t  pin)

Drives a GPIO pin to 0 using open drain.

Parameters
pinThe pin number.
void gpio_clr_gpio_pin ( uint32_t  pin)

Drives a GPIO pin to 0.

Parameters
pinThe pin number.
static uint32_t gpio_configure_edge_detector ( uint32_t  pin,
uint32_t  mode 
)
static

Configure the edge detector of an input pin.

Parameters
pinThe pin number.
modeThe edge detection mode (GPIO_PIN_CHANGE, GPIO_RISING_EDGE or GPIO_FALLING_EDGE).
Returns
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

References GPIO_FALLING_EDGE, GPIO_INVALID_ARGUMENT, GPIO_PIN_CHANGE, GPIO_RISING_EDGE, and GPIO_SUCCESS.

Referenced by gpio_enable_pin_interrupt().

void gpio_configure_group ( uint32_t  port,
uint32_t  mask,
uint32_t  flags 
)

Configuration functionality on a port.

Parameters
portThe port number.
maskThe mask.
flagsThe configuration.

References GPIO_BOTHEDGES, GPIO_DIR_OUTPUT, GPIO_DRIVE_HIGH, GPIO_DRIVE_LOW, GPIO_FALLING, GPIO_INIT_HIGH, GPIO_INTERRUPT, GPIO_OPEN_DRAIN, GPIO_PULL_DOWN, GPIO_PULL_UP, and GPIO_RISING.

void gpio_configure_pin ( uint32_t  pin,
uint32_t  flags 
)

Configuration functionality on a pin.

Parameters
pinThe pin number.
flagsThe configuration.

References GPIO_BOTHEDGES, GPIO_DIR_OUTPUT, GPIO_DRIVE_HIGH, GPIO_DRIVE_LOW, GPIO_FALLING, GPIO_INIT_HIGH, GPIO_INTERRUPT, GPIO_OPEN_DRAIN, GPIO_PULL_DOWN, GPIO_PULL_UP, and GPIO_RISING.

uint32_t gpio_configure_pin_periph_event_mode ( uint32_t  pin,
uint32_t  mode,
uint32_t  use_igf 
)
void gpio_disable_pin_glitch_filter ( uint32_t  pin)

Disables the glitch filter of a pin.

Parameters
pinThe pin number.
void gpio_disable_pin_interrupt ( uint32_t  pin)

Disables the interrupt of a pin.

Parameters
pinThe pin number.
static __always_inline void gpio_disable_pin_periph_event ( uint32_t  pin)
static

Disables the peripheral event generation of a pin.

Parameters
pinThe pin number.
void gpio_disable_pin_pull_up ( uint32_t  pin)

Disables the pull-up resistor of a pin.

Parameters
pinThe pin number.
void gpio_enable_gpio ( const gpio_map_t  gpiomap,
uint32_t  size 
)

Enables the GPIO mode of a set of pins.

Parameters
gpiomapThe pin map.
sizeThe number of pins in gpiomap.

References gpio_enable_gpio_pin(), and gpio_map_t::pin.

void gpio_enable_gpio_pin ( uint32_t  pin)

Enables the GPIO mode of a pin.

Parameters
pinThe pin number.
Refer to the product header file `uc3x.h' (where x is the part number; e.g. x = a0512) for pin definitions. E.g., to enable the GPIO mode of PX21, AVR32_PIN_PX21 can be used. Module pins such as AVR32_PWM_3_PIN for PWM channel 3 can also be used to release module pins for GPIO.

Referenced by gpio_enable_gpio().

uint32_t gpio_enable_module ( const gpio_map_t  gpiomap,
uint32_t  size 
)

Enables specific module modes for a set of pins.

Parameters
gpiomapThe pin map.
sizeThe number of pins in gpiomap.
Returns
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

References gpio_map_t::function, gpio_enable_module_pin(), GPIO_SUCCESS, and gpio_map_t::pin.

Referenced by main().

uint32_t gpio_enable_module_pin ( uint32_t  pin,
uint32_t  function 
)

Enables a specific module mode for a pin.

Note
A pin and pin function index can be found in the device part header file. The AVR32_*_PIN constants map a GPIO number from the device datasheet to the appropriate pin function, while the corresponding AVR32_*_FUNCTION macro contains the appropriate function index.

For example, the constants AVR32_PWM_3_PIN and AVR32_PWM_3_FUNCTION contain the pin and function index of the PWM module, channel 3, for the current device (if available).
Parameters
pinThe pin number.
functionThe pin function.
Returns
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

References GPIO_INVALID_ARGUMENT, and GPIO_SUCCESS.

Referenced by gpio_enable_module().

void gpio_enable_pin_glitch_filter ( uint32_t  pin)

Enables the glitch filter of a pin.

When the glitch filter is enabled, a glitch with duration of less than 1 clock cycle is automatically rejected, while a pulse with duration of 2 clock cycles or more is accepted. For pulse durations between 1 clock cycle and 2 clock cycles, the pulse may or may not be taken into account, depending on the precise timing of its occurrence. Thus for a pulse to be guaranteed visible it must exceed 2 clock cycles, whereas for a glitch to be reliably filtered out, its duration must not exceed 1 clock cycle. The filter introduces 2 clock cycles latency.

Parameters
pinThe pin number.
uint32_t gpio_enable_pin_interrupt ( uint32_t  pin,
uint32_t  mode 
)

Enables the interrupt of a pin with the specified settings.

Parameters
pinThe pin number.
modeThe trigger mode (GPIO_PIN_CHANGE, GPIO_RISING_EDGE or GPIO_FALLING_EDGE).
Returns
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

References gpio_configure_edge_detector(), GPIO_INVALID_ARGUMENT, and GPIO_SUCCESS.

static __always_inline void gpio_enable_pin_periph_event ( uint32_t  pin)
static

Enables the peripheral event generation of a pin.

Parameters
pinThe pin number.
void gpio_enable_pin_pull_up ( uint32_t  pin)

Enables the pull-up resistor of a pin.

Parameters
pinThe pin number.
bool gpio_get_gpio_open_drain_pin_output_value ( uint32_t  pin)

Returns the output value set for a GPIO pin using open drain.

Parameters
pinThe pin number.
Returns
The pin output value.
Note
This function must be used in conjunction with gpio_set_gpio_open_drain_pin, gpio_clr_gpio_open_drain_pin and gpio_tgl_gpio_open_drain_pin.
bool gpio_get_gpio_pin_output_value ( uint32_t  pin)

Returns the output value set for a GPIO pin.

Parameters
pinThe pin number.
Returns
The pin output value.
Note
This function must be used in conjunction with gpio_set_gpio_pin, gpio_clr_gpio_pin and gpio_tgl_gpio_pin.
bool gpio_get_pin_interrupt_flag ( uint32_t  pin)

Gets the interrupt flag of a pin.

Parameters
pinThe pin number.
Returns
The pin interrupt flag.
bool gpio_get_pin_value ( uint32_t  pin)

Returns the value of a pin.

Parameters
pinThe pin number.
Returns
The pin value.

Referenced by gpio_pin_is_high(), and gpio_pin_is_low().

static __always_inline void gpio_local_clr_gpio_open_drain_pin ( uint32_t  pin)
static

Drives a GPIO pin to 0 using open drain.

Parameters
pinThe pin number.
Note
gpio_local_init and gpio_local_init_gpio_open_drain_pin must have been called beforehand.
This function does not enable the GPIO mode of the pin. gpio_enable_gpio_pin can be called for this purpose.
static __always_inline void gpio_local_clr_gpio_pin ( uint32_t  pin)
static

Drives a GPIO pin to 0.

Parameters
pinThe pin number.
Note
gpio_local_init must have been called beforehand.
This function does not enable the GPIO mode of the pin nor its output driver. gpio_enable_gpio_pin and gpio_local_enable_pin_output_driver can be called for this purpose.
static __always_inline void gpio_local_disable_pin_output_driver ( uint32_t  pin)
static

Disables the output driver of a pin.

Parameters
pinThe pin number.
Note
gpio_local_init must have been called beforehand.
static __always_inline void gpio_local_enable_pin_output_driver ( uint32_t  pin)
static

Enables the output driver of a pin.

Parameters
pinThe pin number.
Note
gpio_local_init must have been called beforehand.
This function does not enable the GPIO mode of the pin. gpio_enable_gpio_pin can be called for this purpose.
static __always_inline bool gpio_local_get_pin_value ( uint32_t  pin)
static

Returns the value of a pin.

Parameters
pinThe pin number.
Returns
The pin value.
Note
gpio_local_init must have been called beforehand.
static __always_inline void gpio_local_init ( void  )
static

Enables the local bus interface for GPIO.

Note
This function must have been called at least once before using other functions in this interface.

References Get_system_register, and Set_system_register.

static __always_inline void gpio_local_init_gpio_open_drain_pin ( uint32_t  pin)
static

Initializes the configuration of a GPIO pin so that it can be used with GPIO open-drain functions.

Note
This function must have been called at least once before using gpio_local_set_gpio_open_drain_pin, gpio_local_clr_gpio_open_drain_pin or gpio_local_tgl_gpio_open_drain_pin.
static __always_inline void gpio_local_set_gpio_open_drain_pin ( uint32_t  pin)
static

Drives a GPIO pin to 1 using open drain.

Parameters
pinThe pin number.
Note
gpio_local_init and gpio_local_init_gpio_open_drain_pin must have been called beforehand.
This function does not enable the GPIO mode of the pin. gpio_enable_gpio_pin can be called for this purpose.
static __always_inline void gpio_local_set_gpio_pin ( uint32_t  pin)
static

Drives a GPIO pin to 1.

Parameters
pinThe pin number.
Note
gpio_local_init must have been called beforehand.
This function does not enable the GPIO mode of the pin nor its output driver. gpio_enable_gpio_pin and gpio_local_enable_pin_output_driver can be called for this purpose.
static __always_inline void gpio_local_tgl_gpio_open_drain_pin ( uint32_t  pin)
static

Toggles a GPIO pin using open drain.

Parameters
pinThe pin number.
Note
gpio_local_init and gpio_local_init_gpio_open_drain_pin must have been called beforehand.
This function does not enable the GPIO mode of the pin. gpio_enable_gpio_pin can be called for this purpose.
static __always_inline void gpio_local_tgl_gpio_pin ( uint32_t  pin)
static

Toggles a GPIO pin.

Parameters
pinThe pin number.
Note
gpio_local_init must have been called beforehand.
This function does not enable the GPIO mode of the pin nor its output driver. gpio_enable_gpio_pin and gpio_local_enable_pin_output_driver can be called for this purpose.
static __always_inline bool gpio_pin_is_high ( uint32_t  pin)
static

Check if the pin is in high logical level.

Parameters
pinThe pin number.
Returns
bool true if the pin is in high logical level false if the pin is not in high logical level

References gpio_get_pin_value().

static __always_inline bool gpio_pin_is_low ( uint32_t  pin)
static

Check if the pin is in low logical level.

Parameters
pinThe pin number.
Returns
bool true if the pin is in low logical level false if the pin is not in low logical level

References gpio_get_pin_value().

void gpio_set_gpio_open_drain_pin ( uint32_t  pin)

Drives a GPIO pin to 1 using open drain.

Parameters
pinThe pin number.
void gpio_set_gpio_pin ( uint32_t  pin)

Drives a GPIO pin to 1.

Parameters
pinThe pin number.
void gpio_set_group_high ( uint32_t  port,
uint32_t  mask 
)

Drives a GPIO port to 1.

Parameters
portThe port number.
maskThe mask.
void gpio_set_group_low ( uint32_t  port,
uint32_t  mask 
)

Drives a GPIO port to 0.

Parameters
portThe port number.
maskThe mask.
void gpio_set_pin_high ( uint32_t  pin)

Drives a GPIO pin to 1.

Parameters
pinThe pin number.
Note
The function gpio_configure_pin must be called before.
void gpio_set_pin_low ( uint32_t  pin)

Drives a GPIO pin to 0.

Parameters
pinThe pin number.
Note
The function gpio_configure_pin must be called before.
void gpio_tgl_gpio_open_drain_pin ( uint32_t  pin)

Toggles a GPIO pin using open drain.

Parameters
pinThe pin number.
void gpio_tgl_gpio_pin ( uint32_t  pin)

Toggles a GPIO pin.

Parameters
pinThe pin number.
void gpio_toggle_group ( uint32_t  port,
uint32_t  mask 
)

Toggles a GPIO group.

Parameters
portThe port number.
maskThe mask.
void gpio_toggle_pin ( uint32_t  pin)

Toggles a GPIO pin.

Parameters
pinThe pin number.
Note
The function gpio_configure_pin must be called before.