objc

Data Structures | Macros | Typedefs | Enumerations | Functions

General Purpose Input/Output (GPIO) interface for hardware platforms. More...

Collaboration diagram for GPIO:

Data Structures

struct  hw_gpio_t
 GPIO logical pin structure. More...
 

Macros

#define HW_GPIO_MAX_COUNT   50
 Maximum number of GPIO pins. More...
 

Typedefs

typedef struct hw_gpio_t hw_gpio_t
 GPIO logical pin structure.
 
typedef void(* hw_gpio_callback_t) (uint8_t pin, hw_gpio_event_t event, void *userdata)
 GPIO interrupt callback function pointer. More...
 

Enumerations

enum  hw_gpio_mode_t {
  HW_GPIO_NONE = 0, HW_GPIO_INPUT, HW_GPIO_PULLUP, HW_GPIO_PULLDOWN,
  HW_GPIO_OUTPUT, HW_GPIO_SPI, HW_GPIO_I2C, HW_GPIO_UART,
  HW_GPIO_PWM, HW_GPIO_ADC, HW_GPIO_UNKNOWN
}
 GPIO mode flags for configuring GPIO pins. More...
 
enum  hw_gpio_event_t { HW_GPIO_RISING = (1 << 0), HW_GPIO_FALLING = (1 << 1) }
 GPIO mode flags for configuring GPIO pins. More...
 

Functions

uint8_t hw_gpio_count (void)
 Get the total number of available GPIO pins. More...
 
static bool hw_gpio_valid (hw_gpio_t *gpio)
 Validate the GPIO pin. More...
 
void hw_gpio_set_callback (hw_gpio_callback_t callback, void *userdata)
 Set the global GPIO interrupt callback handler. More...
 
hw_gpio_t hw_gpio_init (uint8_t pin, hw_gpio_mode_t mode)
 Initialize a GPIO pin with the specified mode. More...
 
void hw_gpio_finalize (hw_gpio_t *gpio)
 Finalize and release a GPIO pin. More...
 
hw_gpio_mode_t hw_gpio_get_mode (hw_gpio_t *gpio)
 Get the current mode configuration of a GPIO pin. More...
 
void hw_gpio_set_mode (hw_gpio_t *gpio, hw_gpio_mode_t mode)
 Set the current mode configuration of a GPIO pin. More...
 
bool hw_gpio_get (hw_gpio_t *gpio)
 Read the current state of a GPIO pin. More...
 
void hw_gpio_set (hw_gpio_t *gpio, bool value)
 Set the state of a GPIO pin. More...
 

Detailed Description

General Purpose Input/Output (GPIO) interface for hardware platforms.

This module provides functions to initialize GPIO pins, set their modes, and handle interrupts.

Macro Definition Documentation

◆ HW_GPIO_MAX_COUNT

#define HW_GPIO_MAX_COUNT   50

Maximum number of GPIO pins.

The maximum number of GPIO pins that is supported in this runtime.

Definition at line 26 of file gpio.h.

Typedef Documentation

◆ hw_gpio_callback_t

typedef void(* hw_gpio_callback_t) (uint8_t pin, hw_gpio_event_t event, void *userdata)

GPIO interrupt callback function pointer.

Parameters
pinThe logical pin number that triggered the event.
eventThe type of GPIO event that occurred (rising or falling edge).
userdataUser-defined data pointer passed when setting the callback.

This callback function is called when a GPIO interrupt occurs on a pin that has been configured for interrupt detection. The callback is executed in interrupt context, so it should be kept short and avoid blocking operations.

See also
hw_gpio_set_callback() for setting up the callback handler.
hw_gpio_event_t for available event types.

Definition at line 80 of file gpio.h.

Enumeration Type Documentation

◆ hw_gpio_event_t

GPIO mode flags for configuring GPIO pins.

Enumerator
HW_GPIO_RISING 

GPIO pin rising edge interrupt.

HW_GPIO_FALLING 

GPIO pin falling edge interrupt.

Definition at line 50 of file gpio.h.

50  {
51  HW_GPIO_RISING = (1 << 0),
52  HW_GPIO_FALLING = (1 << 1),
hw_gpio_event_t
GPIO mode flags for configuring GPIO pins.
Definition: gpio.h:50
GPIO pin falling edge interrupt.
Definition: gpio.h:52
GPIO pin rising edge interrupt.
Definition: gpio.h:51

◆ hw_gpio_mode_t

GPIO mode flags for configuring GPIO pins.

Enumerator
HW_GPIO_NONE 

No GPIO mode set.

HW_GPIO_INPUT 

GPIO pin floating input.

HW_GPIO_PULLUP 

GPIO pin input with pull-up resistor.

HW_GPIO_PULLDOWN 

GPIO pin input with pull-down resistor.

HW_GPIO_OUTPUT 

GPIO pin configured as output.

HW_GPIO_SPI 

GPIO pin configured for SPI.

HW_GPIO_I2C 

GPIO pin configured for I2C.

HW_GPIO_UART 

GPIO pin configured for UART.

HW_GPIO_PWM 

GPIO pin configured for PWM.

HW_GPIO_ADC 

GPIO pin configured for analog input.

HW_GPIO_UNKNOWN 

GPIO pin mode is not implemented.

Definition at line 32 of file gpio.h.

32  {
33  HW_GPIO_NONE = 0,
38  HW_GPIO_SPI,
39  HW_GPIO_I2C,
40  HW_GPIO_UART,
41  HW_GPIO_PWM,
42  HW_GPIO_ADC,
GPIO pin configured for PWM.
Definition: gpio.h:41
GPIO pin floating input.
Definition: gpio.h:34
GPIO pin input with pull-down resistor.
Definition: gpio.h:36
No GPIO mode set.
Definition: gpio.h:33
GPIO pin configured as output.
Definition: gpio.h:37
GPIO pin configured for I2C.
Definition: gpio.h:39
hw_gpio_mode_t
GPIO mode flags for configuring GPIO pins.
Definition: gpio.h:32
GPIO pin configured for analog input.
Definition: gpio.h:42
GPIO pin mode is not implemented.
Definition: gpio.h:43
GPIO pin configured for SPI.
Definition: gpio.h:38
GPIO pin configured for UART.
Definition: gpio.h:40
GPIO pin input with pull-up resistor.
Definition: gpio.h:35

Function Documentation

◆ hw_gpio_count()

uint8_t hw_gpio_count ( void  )

Get the total number of available GPIO pins.

Returns
The number of GPIO pins available on the hardware platform.

Returns the number of logical GPIO pins that can be used in the system. These are usually numbered from 0 to hw_gpio_count() - 1. If zero is returned, it indicates that GPIO functionality is not available.

It will never return more than HW_GPIO_MAX_COUNT as this is a hard limit on the number of supported GPIO pins.

Examples:
examples/runtime/gpio/main.c.

◆ hw_gpio_finalize()

void hw_gpio_finalize ( hw_gpio_t gpio)

Finalize and release a GPIO pin.

Parameters
gpioPointer to the GPIO structure to finalize.
Note
After calling this function, the GPIO pin is released and should not be used.

◆ hw_gpio_get()

bool hw_gpio_get ( hw_gpio_t gpio)

Read the current state of a GPIO pin.

Parameters
gpioPointer to the GPIO structure.
Returns
true if the pin is high (logical 1), false if low (logical 0).

This function is typically used with pins configured as input.

◆ hw_gpio_get_mode()

hw_gpio_mode_t hw_gpio_get_mode ( hw_gpio_t gpio)

Get the current mode configuration of a GPIO pin.

Parameters
gpioPointer to the GPIO structure.
Returns
The current GPIO mode configuration.

◆ hw_gpio_init()

hw_gpio_t hw_gpio_init ( uint8_t  pin,
hw_gpio_mode_t  mode 
)

Initialize a GPIO pin with the specified mode.

Parameters
pinThe logical GPIO pin number to initialize.
modeThe GPIO mode configuration (input, output, pull-up, etc.).
Returns
A GPIO structure representing the initialized pin.
Note
The pin number should be valid (0 to hw_gpio_count()-1).
See also
hw_gpio_mode_t for available mode options.
Examples:
examples/runtime/gpio/main.c.

◆ hw_gpio_set()

void hw_gpio_set ( hw_gpio_t gpio,
bool  value 
)

Set the state of a GPIO pin.

Parameters
gpioPointer to the GPIO structure.
valuetrue to set the pin high (logical 1), false to set low (logical 0).

This function is typically used with pins configured as output.

◆ hw_gpio_set_callback()

void hw_gpio_set_callback ( hw_gpio_callback_t  callback,
void *  userdata 
)

Set the global GPIO interrupt callback handler.

Parameters
callbackPointer to the callback function to handle GPIO interrupts, or NULL to disable interrupt handling.
userdataUser-defined data pointer to pass to the callback.

This function registers a global callback handler that will be invoked whenever a GPIO interrupt occurs on any pin that has been configured for interrupt detection. Only one callback can be active at a time.

See also
hw_gpio_callback_t for callback function signature requirements.
Examples:
examples/runtime/gpio/main.c.

◆ hw_gpio_set_mode()

void hw_gpio_set_mode ( hw_gpio_t gpio,
hw_gpio_mode_t  mode 
)

Set the current mode configuration of a GPIO pin.

Parameters
gpioPointer to the GPIO structure.
modeThe new GPIO mode configuration to set.

This function is typically used to change the mode of a pin after it has been initialized.

◆ hw_gpio_valid()

static bool hw_gpio_valid ( hw_gpio_t gpio)
inlinestatic

Validate the GPIO pin.

Returns
True if the GPIO pin is valid, false otherwise.

The result of hw_gpio_init can return an empty GPIO structure if the initialization fails. This function checks if the GPIO pin is valid.

Definition at line 108 of file gpio.h.

108  {
109  return gpio && gpio->mask > 0;
110 }
uint64_t mask
GPIO pin mask for multiple pins.
Definition: gpio.h:61