Driver for GPIO peripheral
The GPIO driver provides functions to configure and control GPIO pins and ports. Features include:
- Single-pin configuration and I/O
- Port-level fast read/write operations
- Interrupt configuration and status handling
- Debounce support
Configuration Considerations
To initialize and use the GPIO driver:
Example usage:
gpio_init
Initialize the specified GPIO port. Enables clocks and resets registers. This function must be called first before any other GPIO functions are called.
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_init(GPIO_PORT1) failed with %d\n", rc);
}
gpio_pin_set_config
Configure a single GPIO pin using provided settings.
pin_cfg.
out_en = GPIO_SAMPLE_OUT_EN_TRUE;
pin_cfg.
int_en = GPIO_SAMPLE_INT_EN_FALSE;
pin_cfg.
int_mask = GPIO_SAMPLE_INT_MASK_TRUE;
pin_cfg.
edge_int = GPIO_SAMPLE_EDGE_INT_FALSE;
pin_cfg.
level = GPIO_SAMPLE_LEVEL_HIGH;
pin_cfg.
debounce = GPIO_SAMPLE_DEBOUNCE_FALSE;
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_set_config() failed for Port 0 with %d\n", rc);
}
gpio_pin_write
Write output value to a GPIO pin.
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_write() for pin %d failed"
"for Port 0 with %d\n", i, rc);
}
gpio_pin_read
Read input value from a GPIO pin.
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_read() for pin %d failed"
"for Port 0 with %d\n", i, rc);
}
LOG_INFO(LOG_MOD_GPIO, "Pin %d read back value for port 0: %d\n", i, pin_value);
Example of port-level operations
LOG_INFO(LOG_MOD_GPIO, "Direction mask for Port 0 : 0x%X\n", dir_mask);
LOG_INFO(LOG_MOD_GPIO, "Port read value for Port 0 : 0x%X\n", port_value);
GPIO Interrupts
The GPIO driver supports interrupt configuration on individual pins. Interrupts can be configured for level or edge sensitivity, and with optional debounce.
Example of configuring a GPIO pin for interrupt:
pin_cfg.
out_en = GPIO_SAMPLE_OUT_EN_FALSE;
pin_cfg.
int_en = GPIO_SAMPLE_INT_EN_TRUE;
pin_cfg.
int_mask = GPIO_SAMPLE_INT_MASK_TRUE;
pin_cfg.
edge_int = GPIO_SAMPLE_EDGE_INT_TRUE;
pin_cfg.
level = GPIO_SAMPLE_LEVEL_HIGH;
pin_cfg.
debounce = GPIO_SAMPLE_DEBOUNCE_FALSE;
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_set_config() for interrupt pin"
" failed for Port 0 with %d\n", rc);
}
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_enable_int(true) failed"
" for Port 0 with %d\n", rc);
}
NVIC_EnableIRQ(PERIF_GPIO1_IRQn);
while(isr_event == 0);
isr_event = 0;
Sample ISR.
ISR_CODE void PERIF_GPIO0_Handler(void)
{
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_enable_int(false) failed in PERIF_GPIO0_Handler"
" for Port 0 with %d\n", rc);
return;
}
NVIC_ClearPendingIRQ(PERIF_GPIO0_IRQn);
NVIC_DisableIRQ(PERIF_GPIO0_IRQn);
isr_event = 1;
LOG_INFO(LOG_MOD_GPIO, "Interrupt handled and flag set to: %d\n", isr_event);
return;
}