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 P0_0 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 P0_0 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 P0_0 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 P0_0 as an input pin. It registers a callback function and enables detection of a falling edge event to trigger the callback.
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_event_callback_t callback, void *callback_arg) |
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... | |
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_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register/clear a callback handler for pin events
This 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 pin number |
[in] | callback | The function to call when the specified event happens. Pass NULL to unregister the handler. |
[in] | callback_arg | Generic argument that will be provided to the callback when called, can be NULL |
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 |
[in] | enable | True to turn on interrupts, False to turn off |