GPIO APIs

This topic provides details for the GPIO APIs.

gpio_request

Synopsis
int gpio_request(unsigned gpio, constchar * label)
Description Requests access to a GPIO interface.
Parameter
  • gpio: The GPIO index number to be requested

  • label: The GPIO name. Leave it null if you do not know the label.

Return
  • Success: 0
  • Failure: Error code

gpio_free

Synopsis
void gpio_free(unsigned gpio)
Description Releases a previously requested GPIO interface, making it available for other consumers.
Parameter gpio: The GPIO index number to be released.
Return None

gpio_direction_input

Synopsis
int gpio_direction_input(unsigned gpio)
Description Sets a GPIO interface as input.
Parameter gpio: The GPIO index number
Return
  • Success: 0
  • Failure: Error code

gpio_direction_output

Synopsis
int gpio_direction_output(unsigned gpio, int value)
Description Sets a GPIO interface as output.
Parameter
  • gpio: The GPIO index number
  • value: The expected level, 0 as low level, 1 as high level
Return
  • Success: 0
  • Failure: Error code

gpio_get_value

Synopsis
int gpio_get_value(unsigned gpio)
Description Gets the level from a target GPIO interface. The level is used to define whether the GPIO is an input or an output.
Parameter gpio: The GPIO index number
Return
  • Success: The level of the target GPIO interface: 0 as low level, 1 as high level.
  • Failure: 1

gpio_set_value

Synopsis
int gpio_set_value(unsigned gpio, int value)
Description Sets the level to a target GPIO interface. The level can be used to alter the GPIO from an output to an output.
Note: The GPIO has to be an output, otherwise, this function may have no effect.
Parameter
  • gpio: The GPIO index number
  • value: The expected level, 0 as low level, 1 as high level
Return None

of_get_named_gpio

Synopsis
int of_get_named_gpio(const struct device_node *np, const char *propname, int index)
Description Obtains a GPIO interface via which you can use the GPIO Application Interface (API).
Parameter
  • np: Device node where the target is obtained.
  • propname: Name of the property that contains GPIO specifier(s).
  • index: Index of the GPIO.
Return
  • Success: The GPIO index number via which you can use the GPIO API
  • Failure: 1

of_get_named_gpio_flags

Synopsis
int of_get_named_gpio_flags(const struct device_node *np, const char *list_name, int index, enum of_gpio_flags *flags)
Description Obtains a GPIO number from the DTS file and analyzes its GPIO properties.
Parameter
  • np: Device node where the target is obtained.
  • propname: Name of the property that contains GPIO specifier(s).
  • index: Index of the GPIO
  • flags: Enumerate of_gpio_flags variables, including IO configuration, pull-up and pull-down settings, and drive strength settings.
Return
  • Success: The GPIO index number
  • Failure: Error code