This section describes the programming interface of the GPIO Peripheral driver.The GPIO Peripheral driver configures pins to digital input/output and provides API functions for input/output operations.
GPIO Pin Definitions
Define GPIO pins according to the target board configuration and ensure that the definitions are correct. Define a header file to store the GPIO pin names and the input/output configuration arrays defined in source files.
Include this header file in source files where you want to use GPIO driver to operate GPIO pins.
GPIO pin header file example:
GPIO Initialization
-
To initialize the GPIO driver, define two arrays, the gpio_input_pin_t inputPin[], and the gpio_output_pin_t outputPin[].
-
Then, call the GPIO_DRV_Init() function and pass these two arrays.
This is an example of the inputPin and outputPin array definition:
#include "gpio/fsl_gpio_driver.h"
#include "gpio_pin_header_file.h"
{
.pinName = kGpioPTA2,
.config.isPullEnable = false,
.config.isPassiveFilterEnabled = false,
},
{
}
};
{
.pinName = kGpioLED4,
},
{
.pinName = kGpioPTB9,
.config.outputLogic = 0,
},
{
}
};
<note> If the digital filter is enabled, the digital filter clock source and width must be configured before calling the GPIO_DRV_Init function. To configure the digital filter, use this API function from porting the HAL driver. Each pin in the same port shares the same digital filter settings.</note>
void PORT_HAL_SetDigitalFilterClock(uint32_t baseAddr, port_digital_filter_clock_source_t clockSource);
void PORT_HAL_SetDigitalFilterWidth(uint32_t baseAddr, uint8_t width);
Output Operations
To use the output operation, configure the target GPIO pin as a digital output in the GPIO_DRV_Init function. The output operations include set, clear, and toggle of the output logic level. Three API functions are provided for these operations:
These functions are used when the logic level of the GPIO output is known.
Otherwise, a different function is provided to configure the output logic level according to a passed parameter:
Use this function to change the GPIO output according to the results of other code. Pass an integer as the "uint32_t output" parameter. If that integer is not 0, it generates the high logic. If the integer is 0, it generates the low logic.
Input Operations
To use the input operation, configure the target GPIO pin as a digital input in the GPIO_DRV_Init function. For the input operation, this is the most commonly used API function:
This function returns the logic level read from a specific GPIO pin.
If the digital filter is enabled, use this function to disable it:
void GPIO_DRV_SetDigitalFilterCmd(uint32_t pinName, bool isDigitalFilterEnabled);
GPIO Interrupt
Enable a specific pin interrupt in GPIO initialization structures. Both output and input can trigger an interrupt. This API function clears the interrupt flag inside the interrupt handler:
|
file | fsl_gpio_driver.h |
| The GPIO driver uses the virtual GPIO name rather than an actual port and a pin number.
|
|
|
GPIO_Type *const | g_gpioBase [GPIO_INSTANCE_COUNT] |
| Table of base addresses for GPIO instances. More...
|
|
PORT_Type *const | g_portBase [PORT_INSTANCE_COUNT] |
| Table of base addresses for PORT instances. More...
|
|
Although every pin is configurable, valid configurations depend on a specific device. Users should check the related reference manual to ensure that the specific feature is valid in an individual pin. A configuration of unavailable features is harmless, but takes no effect.
Although every pin is configurable, valid configurations depend on a specific device. Users should check the related reference manual to ensure that the specific feature is valid in an individual pin. The configuration of unavailable features is harmless, but takes no effect.
uint32_t gpio_output_pin_t::outputLogic |
struct gpio_input_pin_user_config_t |
Although the pinName is defined as a uint32_t type, values assigned to the pinName should be the enumeration names defined in the enum _gpio_pins.
uint32_t gpio_input_pin_user_config_t::pinName |
struct gpio_output_pin_user_config_t |
Although the pinName is defined as a uint32_t type, values assigned to the pinName should be the enumeration names defined in the enum _gpio_pins.
uint32_t gpio_output_pin_user_config_t::pinName |
#define GPIO_PINS_OUT_OF_RANGE (0xFFFFFFFFU) |
#define GPIO_PORT_SHIFT (0x8U) |
#define GPIO_EXTRACT_PIN |
( |
|
v | ) |
((v) & 0xFFU) |
To initialize the GPIO driver, define two arrays similar to the gpio_input_pin_user_config_t inputPin[] array and the gpio_output_pin_user_config_t outputPin[] array in the user file. Then, call the GPIO_DRV_Init() function and pass in the two arrays. If the input or output pins are not needed, pass in a NULL.
This is an example to define an input pin array:
{
.pinName = kGpioPTA2,
.config.isPullEnable = false,
.config.isPassiveFilterEnabled = false,
},
{
}
};
- Parameters
-
inputPins | input GPIO pins pointer. |
outputPins | output GPIO pins pointer. |
- Parameters
-
inputPin | input GPIO pins pointer. |
- Parameters
-
outputPin | output GPIO pins pointer. |
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
- Returns
- GPIO directions.
- kGpioDigitalInput: corresponding pin is set as digital input.
- kGpioDigitalOutput: corresponding pin is set as digital output.
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
direction | GPIO directions.
- kGpioDigitalInput: corresponding pin is set as digital input.
- kGpioDigitalOutput: corresponding pin is set as digital output.
|
void GPIO_DRV_WritePinOutput |
( |
uint32_t |
pinName, |
|
|
uint32_t |
output |
|
) |
| |
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
output | pin output logic level.
- 0: corresponding pin output low logic level.
- Non-0: corresponding pin output high logic level.
|
void GPIO_DRV_SetPinOutput |
( |
uint32_t |
pinName | ) |
|
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
void GPIO_DRV_ClearPinOutput |
( |
uint32_t |
pinName | ) |
|
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
void GPIO_DRV_TogglePinOutput |
( |
uint32_t |
pinName | ) |
|
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
uint32_t GPIO_DRV_ReadPinInput |
( |
uint32_t |
pinName | ) |
|
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
- Returns
- GPIO port input value.
- 0: Pin logic level is 0, or is not configured for use by digital function.
- 1: Pin logic level is 1.
bool GPIO_DRV_IsPinIntPending |
( |
uint32_t |
pinName | ) |
|
If a pin is configured to generate the DMA request, the corresponding flag is cleared automatically at the completion of the requested DMA transfer. Otherwise, the flag remains set until a logic one is written to that flag. If configured for a level sensitive interrupt that remains asserted, the flag is set again immediately.
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
- Returns
- current pin interrupt status flag
- 0: interrupt is not detected.
- 1: interrupt is detected.
void GPIO_DRV_ClearPinIntFlag |
( |
uint32_t |
pinName | ) |
|
- Parameters
-
pinName | GPIO pin name defined by the user in the GPIO pin enumeration list. |
GPIO_Type* const g_gpioBase[GPIO_INSTANCE_COUNT] |
PORT_Type* const g_portBase[PORT_INSTANCE_COUNT] |