High level interface for configuring and interacting with general purpose input/outputs (GPIO).
The GPIO driver provides functions to configure and initialize GPIO, and to read and write data to the pin. The driver also supports interrupt generation on GPIO signals with rising, falling or both edges.
cyhal_gpio_init can be used for a simple GPIO initialization by providing the pin number (pin), pin direction (direction), pin drive mode (drive_mode) and the initial value on the pin (init_val).
The following snippet initializes GPIO pin as an input with high impedance digital drive mode and initial value = false (low). A value is read from the pin and stored to a uint8_t variable (read_val).
The following snippet initializes GPIO pin as an output pin with strong drive mode and initial value = false (low). A value = true (high) is written to the output driver.
The following snippet shows how to reconfigure a GPIO pin during run-time using the firmware. The GPIO pin is first initialized as an output pin with strong drive mode. The pin is then reconfigured as an input with high impedance digital drive mode.
GPIO events can be mapped to an interrupt and assigned to a callback function. The callback function needs to be first registered and then the event needs to be enabled. The following snippet initializes GPIO pin as an input pin. It registers a callback function and enables detection of a falling edge event to trigger the callback.
API Reference | |
GPIO HAL Results | |
GPIO specific return codes. | |
Data Structures | |
struct | cyhal_gpio_callback_data_t |
Structure containing callback data for pins. More... | |
Macros | |
#define | CYHAL_NC_PIN_VALUE (NC) |
Integer representation of no connect pin (required to exist in all BSPs) | |
Typedefs | |
typedef void(* | cyhal_gpio_event_callback_t) (void *callback_arg, cyhal_gpio_event_t event) |
GPIO callback function type. | |
Enumerations | |
enum | cyhal_gpio_event_t { CYHAL_GPIO_IRQ_NONE = 0 , CYHAL_GPIO_IRQ_RISE = 1 << 0 , CYHAL_GPIO_IRQ_FALL = 1 << 1 , CYHAL_GPIO_IRQ_BOTH = (CYHAL_GPIO_IRQ_RISE | CYHAL_GPIO_IRQ_FALL) } |
Pin events. More... | |
enum | cyhal_gpio_direction_t { CYHAL_GPIO_DIR_INPUT , CYHAL_GPIO_DIR_OUTPUT , CYHAL_GPIO_DIR_BIDIRECTIONAL } |
Pin direction. More... | |
enum | cyhal_gpio_drive_mode_t { CYHAL_GPIO_DRIVE_NONE , CYHAL_GPIO_DRIVE_ANALOG , CYHAL_GPIO_DRIVE_PULLUP , CYHAL_GPIO_DRIVE_PULLDOWN , CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW , CYHAL_GPIO_DRIVE_OPENDRAINDRIVESHIGH , CYHAL_GPIO_DRIVE_STRONG , CYHAL_GPIO_DRIVE_PULLUPDOWN , CYHAL_GPIO_DRIVE_PULL_NONE } |
Pin drive mode. More... | |
Functions | |
cy_rslt_t | cyhal_gpio_init (cyhal_gpio_t pin, cyhal_gpio_direction_t direction, cyhal_gpio_drive_mode_t drive_mode, bool init_val) |
Initialize the GPIO pin See Snippet 1: Reading value from GPIO. More... | |
void | cyhal_gpio_free (cyhal_gpio_t pin) |
Uninitialize the gpio peripheral and the cyhal_gpio_t object. More... | |
cy_rslt_t | cyhal_gpio_configure (cyhal_gpio_t pin, cyhal_gpio_direction_t direction, cyhal_gpio_drive_mode_t drive_mode) |
Configure the GPIO pin See Snippet 3: Reconfiguring a GPIO. More... | |
void | cyhal_gpio_write (cyhal_gpio_t pin, bool value) |
Set the output value for the pin. More... | |
bool | cyhal_gpio_read (cyhal_gpio_t pin) |
Read the input value. More... | |
void | cyhal_gpio_toggle (cyhal_gpio_t pin) |
Toggle the output value See Snippet 4: Interrupts on GPIO events. More... | |
void | cyhal_gpio_register_callback (cyhal_gpio_t pin, cyhal_gpio_callback_data_t *callback_data) |
Register/clear a callback handler for pin events More... | |
void | cyhal_gpio_enable_event (cyhal_gpio_t pin, cyhal_gpio_event_t event, uint8_t intr_priority, bool enable) |
Enable or Disable the specified GPIO event More... | |
cy_rslt_t | cyhal_gpio_connect_digital (cyhal_gpio_t pin, cyhal_source_t source) |
Connects a source signal and enables an input to a pin that, when triggered, will set the pins output. More... | |
cy_rslt_t | cyhal_gpio_enable_output (cyhal_gpio_t pin, cyhal_signal_type_t type, cyhal_source_t *source) |
Enables an output signal from a pin that is triggered by the pins input. More... | |
cy_rslt_t | cyhal_gpio_disconnect_digital (cyhal_gpio_t pin, cyhal_source_t source) |
Disconnects a source signal and disables an input to a pin. More... | |
cy_rslt_t | cyhal_gpio_disable_output (cyhal_gpio_t pin) |
Disables an output signal from a pin. More... | |
struct cyhal_gpio_callback_data_t |
Data Fields | ||
---|---|---|
cyhal_gpio_event_callback_t | callback | The callback function to run. |
void * | callback_arg | Optional argument for the callback. |
struct cyhal_gpio_callback_data_s * | next |
NULL. Filled in by the HAL driver |
cyhal_gpio_t | pin |
NC. Filled in by the HAL driver |
enum cyhal_gpio_event_t |
Pin drive mode.
cy_rslt_t cyhal_gpio_init | ( | cyhal_gpio_t | pin, |
cyhal_gpio_direction_t | direction, | ||
cyhal_gpio_drive_mode_t | drive_mode, | ||
bool | init_val | ||
) |
Initialize the GPIO pin
See Snippet 1: Reading value from GPIO.
[in] | pin | The GPIO pin to initialize |
[in] | direction | The pin direction |
[in] | drive_mode | The pin drive mode |
[in] | init_val | Initial value on the pin |
Guidance for using gpio drive modes ( cyhal_gpio_drive_mode_t for details). For default use drive modes: Input GPIO direction - CYHAL_GPIO_DRIVE_NONE Output GPIO direction - CYHAL_GPIO_DRIVE_STRONG Bidirectional GPIO - CYHAL_GPIO_DRIVE_PULLUPDOWN
void cyhal_gpio_free | ( | cyhal_gpio_t | pin | ) |
Uninitialize the gpio peripheral and the cyhal_gpio_t object.
[in] | pin | Pin number |
cy_rslt_t cyhal_gpio_configure | ( | cyhal_gpio_t | pin, |
cyhal_gpio_direction_t | direction, | ||
cyhal_gpio_drive_mode_t | drive_mode | ||
) |
Configure the GPIO pin
See Snippet 3: Reconfiguring a GPIO.
[in] | pin | The GPIO pin |
[in] | direction | The pin direction |
[in] | drive_mode | The pin drive mode |
void cyhal_gpio_write | ( | cyhal_gpio_t | pin, |
bool | value | ||
) |
Set the output value for the pin.
This only works for output & in_out pins.
See Snippet 2: Writing value to a GPIO.
[in] | pin | The GPIO object |
[in] | value | The value to be set (high = true, low = false) |
bool cyhal_gpio_read | ( | cyhal_gpio_t | pin | ) |
Read the input value.
This only works for CYHAL_GPIO_DIR_INPUT & CYHAL_GPIO_DIR_BIDIRECTIONAL pins.
See Snippet 1: Reading value from GPIO.
[in] | pin | The GPIO object |
void cyhal_gpio_toggle | ( | cyhal_gpio_t | pin | ) |
Toggle the output value
See Snippet 4: Interrupts on GPIO events.
[in] | pin | The GPIO object |
void cyhal_gpio_register_callback | ( | cyhal_gpio_t | pin, |
cyhal_gpio_callback_data_t * | callback_data | ||
) |
Register/clear a callback handler for pin events
The referenced function will be called when one of the events enabled by cyhal_gpio_enable_event occurs.
See Snippet 4: Interrupts on GPIO events.
[in] | pin | The GPIO object |
[in] | callback_data | The callback data to register. Use NULL to unregister. This object must persist for the length of time the callback is registered. As such, it should not be declared on the stack. |
void cyhal_gpio_enable_event | ( | cyhal_gpio_t | pin, |
cyhal_gpio_event_t | event, | ||
uint8_t | intr_priority, | ||
bool | enable | ||
) |
Enable or Disable the specified GPIO event
When an enabled event occurs, the function specified by cyhal_gpio_register_callback will be called.
See Snippet 4: Interrupts on GPIO events.
[in] | pin | The GPIO object |
[in] | event | The GPIO event |
[in] | intr_priority | The priority for NVIC interrupt events. Interrupt priorities specific to a pin may not be supported on all platforms. Refer to platform implementation specific documentation for details. |
[in] | enable | True to turn on interrupts, False to turn off |
cy_rslt_t cyhal_gpio_connect_digital | ( | cyhal_gpio_t | pin, |
cyhal_source_t | source | ||
) |
Connects a source signal and enables an input to a pin that, when triggered, will set the pins output.
[in] | pin | GPIO object |
[in] | source | Source signal obtained from another driver's cyhal_<PERIPH>_enable_output |
cy_rslt_t cyhal_gpio_enable_output | ( | cyhal_gpio_t | pin, |
cyhal_signal_type_t | type, | ||
cyhal_source_t * | source | ||
) |
Enables an output signal from a pin that is triggered by the pins input.
[in] | pin | GPIO object |
[in] | type | Whether the signal will act as a edge or level input |
[out] | source | Pointer to user-allocated source signal object which will be initialized by enable_output. source should be passed to (dis)connect_digital functions to (dis)connect the associated endpoints. |
cy_rslt_t cyhal_gpio_disconnect_digital | ( | cyhal_gpio_t | pin, |
cyhal_source_t | source | ||
) |
Disconnects a source signal and disables an input to a pin.
[in] | pin | GPIO object |
[in] | source | Source signal from cyhal_<PERIPH>_enable_output to disable |
cy_rslt_t cyhal_gpio_disable_output | ( | cyhal_gpio_t | pin | ) |
Disables an output signal from a pin.
[in] | pin | GPIO object |