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.
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.
The following snippet shows how to configure a GPIO port to do port-wide reads and writes. The GPIO pins are first initialized as output pins with strong drive mode. These are then accessed at a port level.
API Reference | |
GPIO HAL Results | |
GPIO specific return codes. | |
Macros | |
#define | MTB_HAL_GPIO_NC_VALUE (0xFF) |
Integer representation of no connect gpio (required to exist in all BSPs) | |
#define | NC 0xFF |
No Connect/Invalid Pin. | |
Typedefs | |
typedef void(* | mtb_hal_gpio_event_callback_t) (void *callback_arg, mtb_hal_gpio_event_t event) |
GPIO callback function type. | |
Enumerations | |
enum | mtb_hal_gpio_event_t { MTB_HAL_GPIO_IRQ_NONE = 0 , MTB_HAL_GPIO_IRQ_RISE = 1 << 0 , MTB_HAL_GPIO_IRQ_FALL = 1 << 1 , MTB_HAL_GPIO_IRQ_BOTH = (MTB_HAL_GPIO_IRQ_RISE | MTB_HAL_GPIO_IRQ_FALL) } |
Pin events. More... | |
enum | mtb_hal_gpio_direction_t { MTB_HAL_GPIO_DIR_INPUT , MTB_HAL_GPIO_DIR_OUTPUT , MTB_HAL_GPIO_DIR_BIDIRECTIONAL } |
Pin direction. More... | |
enum | mtb_hal_gpio_drive_mode_t { MTB_HAL_GPIO_DRIVE_ANALOG = (MTB_HAL_MAP_GPIO_DRIVE_ANALOG) , MTB_HAL_GPIO_DRIVE_NONE = (MTB_HAL_MAP_GPIO_DRIVE_NONE) , MTB_HAL_GPIO_DRIVE_PULLUP = (MTB_HAL_MAP_GPIO_DRIVE_PULLUP) , MTB_HAL_GPIO_DRIVE_PULLDOWN = (MTB_HAL_MAP_GPIO_DRIVE_PULLDOWN) , MTB_HAL_GPIO_DRIVE_OPENDRAINDRIVESLOW = (MTB_HAL_MAP_GPIO_DRIVE_OPENDRAINDRIVESLOW) , MTB_HAL_GPIO_DRIVE_OPENDRAINDRIVESHIGH = (MTB_HAL_MAP_GPIO_DRIVE_OPENDRAINDRIVESHIGH) , MTB_HAL_GPIO_DRIVE_STRONG = (MTB_HAL_MAP_GPIO_DRIVE_STRONG) , MTB_HAL_GPIO_DRIVE_PULLUPDOWN = (MTB_HAL_MAP_GPIO_DRIVE_PULLUPDOWN) , MTB_HAL_GPIO_DRIVE_PULL_NONE = (MTB_HAL_MAP_GPIO_DRIVE_PULL_NONE) } |
Pin drive mode. More... | |
Functions | |
void | mtb_hal_gpio_setup (mtb_hal_gpio_t *obj, const uint8_t port, const uint8_t pin) |
Init the GPIO object More... | |
void | mtb_hal_gpio_configure (mtb_hal_gpio_t *obj, mtb_hal_gpio_direction_t direction, mtb_hal_gpio_drive_mode_t drive_mode) |
Configure the GPIO pin See Snippet 3: Reconfiguring a GPIO. More... | |
void | mtb_hal_gpio_write (mtb_hal_gpio_t *obj, bool value) |
Set the output value for the pin. More... | |
bool | mtb_hal_gpio_read (mtb_hal_gpio_t *obj) |
Read the input value. More... | |
void | mtb_hal_gpio_toggle (mtb_hal_gpio_t *obj) |
Toggle the output value See Snippet 4: Interrupts on GPIO events. More... | |
void | mtb_hal_gpio_port_setup (mtb_hal_gpio_port_t *port, uint32_t port_number) |
Setup the port object. More... | |
void | mtb_hal_gpio_port_set (mtb_hal_gpio_port_t *port, uint32_t pin_mask) |
Set the output value for specified port with pin_mask. More... | |
void | mtb_hal_gpio_port_clear (mtb_hal_gpio_port_t *port, uint32_t pin_mask) |
Clear the output value with pin_mask. More... | |
void | mtb_hal_gpio_port_toggle (mtb_hal_gpio_port_t *port, uint32_t pin_mask) |
Toggle the output value with pin_mask. More... | |
void | mtb_hal_gpio_port_write (mtb_hal_gpio_port_t *port, uint32_t value) |
Set the output value for specified port. More... | |
void | mtb_hal_gpio_port_read (mtb_hal_gpio_port_t *port, uint32_t *value) |
Reads all bits from specified port. More... | |
void | mtb_hal_gpio_register_callback (mtb_hal_gpio_t *obj, mtb_hal_gpio_event_callback_t callback, void *callback_arg) |
Register/clear a callback handler for pin events More... | |
void | mtb_hal_gpio_enable_event (mtb_hal_gpio_t *obj, mtb_hal_gpio_event_t event, bool enable) |
Enable or Disable the specified GPIO event More... | |
cy_rslt_t | mtb_hal_gpio_process_interrupt (mtb_hal_gpio_t *obj) |
Process interrupts related related to a GPIO instance. More... | |
enum mtb_hal_gpio_event_t |
Pin drive mode.
void mtb_hal_gpio_setup | ( | mtb_hal_gpio_t * | obj, |
const uint8_t | port, | ||
const uint8_t | pin | ||
) |
Init the GPIO object
[in] | obj | The GPIO object |
[in] | port | The port number |
[in] | pin | The pin number |
void mtb_hal_gpio_configure | ( | mtb_hal_gpio_t * | obj, |
mtb_hal_gpio_direction_t | direction, | ||
mtb_hal_gpio_drive_mode_t | drive_mode | ||
) |
Configure the GPIO pin
See Snippet 3: Reconfiguring a GPIO.
[in] | obj | The GPIO object |
[in] | direction | The pin direction |
[in] | drive_mode | The pin drive mode |
void mtb_hal_gpio_write | ( | mtb_hal_gpio_t * | obj, |
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] | obj | The GPIO object |
[in] | value | The value to be set (high = true, low = false) |
bool mtb_hal_gpio_read | ( | mtb_hal_gpio_t * | obj | ) |
Read the input value.
This only works for MTB_HAL_GPIO_DIR_INPUT & MTB_HAL_GPIO_DIR_BIDIRECTIONAL pins.
See Snippet 1: Reading value from GPIO.
[in] | obj | The GPIO object |
void mtb_hal_gpio_toggle | ( | mtb_hal_gpio_t * | obj | ) |
Toggle the output value
See Snippet 4: Interrupts on GPIO events.
[in] | obj | The GPIO object |
void mtb_hal_gpio_port_setup | ( | mtb_hal_gpio_port_t * | port, |
uint32_t | port_number | ||
) |
Setup the port object.
[out] | port | Pointer to a port object. The caller must allocate the memory for this object but the init function will initialize its contents. mtb_hal_gpio_port_t is an output that is populated based on the input(port_number). |
[in] | port_number | The port number |
void mtb_hal_gpio_port_set | ( | mtb_hal_gpio_port_t * | port, |
uint32_t | pin_mask | ||
) |
Set the output value for specified port with pin_mask.
[in] | port | The port object |
[in] | pin_mask | The pin mask (LSB means pin 0) |
Sets output data of specific IO pins in the corresponding port to '1', without affecting the output data of the other IO pads in the port. All bits in the bitmask are modified at once.
void mtb_hal_gpio_port_clear | ( | mtb_hal_gpio_port_t * | port, |
uint32_t | pin_mask | ||
) |
Clear the output value with pin_mask.
[in] | port | The GPIO object |
[in] | pin_mask | The pin mask (LSB means pin 0) |
Sets output data of specific IO pins in the corresponding port to '0', without affecting the output data of the other IO pads in the port. All bits in the bitmask are modified at once.
void mtb_hal_gpio_port_toggle | ( | mtb_hal_gpio_port_t * | port, |
uint32_t | pin_mask | ||
) |
Toggle the output value with pin_mask.
[in] | port | The GPIO object |
[in] | pin_mask | The pin mask (LSB means pin 0) |
Inverts the output data of specific IO pins in the corresponding port, without affecting the output data of the other IO pads in the port. All bits in the bitmask are modified at once.
void mtb_hal_gpio_port_write | ( | mtb_hal_gpio_port_t * | port, |
uint32_t | value | ||
) |
Set the output value for specified port.
[in] | port | The port object |
[in] | value | The value to be set (high = true, low = false) |
All GPIO output bits in the port are modified.
void mtb_hal_gpio_port_read | ( | mtb_hal_gpio_port_t * | port, |
uint32_t * | value | ||
) |
Reads all bits from specified port.
[in] | port | The port object |
[in] | value | The value of the IO (high = true, low = false) |
void mtb_hal_gpio_register_callback | ( | mtb_hal_gpio_t * | obj, |
mtb_hal_gpio_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register/clear a callback handler for pin events
The referenced function will be called when one of the events enabled by mtb_hal_gpio_enable_event occurs.
See Snippet 4: Interrupts on GPIO events.
[in] | obj | The GPIO object |
[in] | callback | The callback handler which will be invoked when the event occurs |
[in] | callback_arg | Generic argument that will be provided to the callback when called |
void mtb_hal_gpio_enable_event | ( | mtb_hal_gpio_t * | obj, |
mtb_hal_gpio_event_t | event, | ||
bool | enable | ||
) |
Enable or Disable the specified GPIO event
When an enabled event occurs, the function specified by mtb_hal_gpio_register_callback will be called.
See Snippet 4: Interrupts on GPIO events.
[in] | obj | The GPIO object |
[in] | event | The GPIO event |
[in] | enable | True to turn on interrupts, False to turn off |
cy_rslt_t mtb_hal_gpio_process_interrupt | ( | mtb_hal_gpio_t * | obj | ) |
Process interrupts related related to a GPIO instance.
obj | HAL object for which the interrupt should be processed |