Astra MCU SDK Peripheral Driver Library
 
Loading...
Searching...
No Matches

Topics

 Enumerated Types
 Enumerated Type for GPIO Driver.
 
 Data Structures
 Data Structures for GPIO Driver.
 
 Functions
 

Detailed Description

Driver for GPIO peripheral

The GPIO driver provides functions to configure and control GPIO pins and ports. Features include:

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.

if ((rc = gpio_init(GPIO_PORT1)) != GPIO_OK)
{
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.

/* Configure a pin as output */
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.both_edge_int = GPIO_SAMPLE_BOTH_EDGE_INT_FALSE;
pin_cfg.level = GPIO_SAMPLE_LEVEL_HIGH;
pin_cfg.debounce = GPIO_SAMPLE_DEBOUNCE_FALSE;
if ((rc = gpio_pin_set_config(GPIO_PORT0, test_pins_0[0], &pin_cfg)) != GPIO_OK)
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_set_config() failed for Port 0 with %d\n", rc);
return GPIO_ERROR;
}

gpio_pin_write

Write output value to a GPIO pin.

/* Write high */
if ((rc = gpio_pin_write(GPIO_PORT0, test_pins_0[i], 1)) != GPIO_OK)
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_write() for pin %d failed"
"for Port 0 with %d\n", i, rc);
return GPIO_ERROR;
}

gpio_pin_read

Read input value from a GPIO pin.

/* Read back */
if ((rc = gpio_pin_read(GPIO_PORT0, test_pins_0[i], &pin_value)) != GPIO_OK)
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_read() for pin %d failed"
"for Port 0 with %d\n", i, rc);
return GPIO_ERROR;
}
LOG_INFO(LOG_MOD_GPIO, "Pin %d read back value for port 0: %d\n", i, pin_value);

Example of port-level operations

/* Set direction as output for test pins */
/* Read back direction and verify */
LOG_INFO(LOG_MOD_GPIO, "Direction mask for Port 0 : 0x%X\n", dir_mask);
/* Write test pattern to port */
/* Read back port value and verify */
port_value = gpio_port_read(GPIO_PORT0);
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:

/* Configure pin */
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.both_edge_int = GPIO_SAMPLE_BOTH_EDGE_INT_FALSE;
pin_cfg.level = GPIO_SAMPLE_LEVEL_HIGH;
pin_cfg.debounce = GPIO_SAMPLE_DEBOUNCE_FALSE;
if ((rc = gpio_pin_set_config(GPIO_PORT1, test_int_pin_1, &pin_cfg)) != GPIO_OK)
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_set_config() for interrupt pin"
" failed for Port 0 with %d\n", rc);
return GPIO_ERROR;
}
/* Test interrupt enable/disable */
if ((rc = gpio_pin_enable_int(GPIO_PORT1, test_int_pin_1, true)) != GPIO_OK)
{
LOG_ERROR(LOG_MOD_GPIO, "gpio_pin_enable_int(true) failed"
" for Port 0 with %d\n", rc);
return GPIO_ERROR;
}
NVIC_EnableIRQ(PERIF_GPIO1_IRQn); /* Enable and Trigger Interrupt */
while(isr_event == 0); /* Wait till the interrupt is handled by the ISR
* and the flag is set */
isr_event = 0; /* Resetting the flag */

Sample ISR.

ISR_CODE void PERIF_GPIO0_Handler(void)
{
if ((rc = gpio_pin_enable_int(GPIO_PORT0, test_int_pin, false)) != GPIO_OK)
{
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); /* Handled and cleared Interrupts */
isr_event = 1; /* Flag is set */
LOG_INFO(LOG_MOD_GPIO, "Interrupt handled and flag set to: %d\n", isr_event);
return;
}