Appearance
GPIO
GPIO API.
A simple interface for setting and reading pin states on the MCU. It allows to define pin direction and weak pull-up or pull-down as well as the registration of GPIO interrupts. Be aware, that the API at the moment does not check, if the pin has been assigned to a peripheral (like I2C, ADC, ...). So if you access a pin that is otherwise used in an alternate context you might mess things up. For the pin identifier, the OS's HAL requirements specify that common Pin Names like "PA3" or "PB5" shall be translated into pin numbers as follows: 100 * Port (A=0, B=1,...) + Pin. So PA3 becomes 3, PB5 becomes 105. This is only a recommendation, so check the HAL implementation's docs for compliance.
Sysprops
The GPIO driver does not have any sysprops defined.
Example
This is an example of how to use the GPIO functions.
cpp
#include "gpio.h"
// Define our callback function for GPIO interrupts
void gpio_irq_callback() {
// Handle GPIO interrupt
}
int main() {
uint16_t pin = 5; // define GPIO pin 5
// Set the direction of GPIO pin to output
gpio_set_dir(pin, gpioPinDirOutput);
// Set the mode of GPIO pin to pull up
gpio_set_mode(pin, gpioModePullUp);
// Get the logic state of GPIO pin
cw_driver_return_t pin_state = gpio_get(pin);
if (pin_state == gpioLogicLow) {
// If the pin state is low, set it to high
gpio_set(pin, gpioLogicHigh);
} else {
// If the pin state is high, set it to low
gpio_set(pin, gpioLogicLow);
}
// Register an interrupt for the GPIO pin with falling edge.
gpio_register_interrupt(pin, gpioIrqFallingEdge, gpio_irq_callback);
// Enable the interrupt
gpio_enable_interrupt(pin);
// insert code for wait or delay here
// Disable the interrupt
gpio_disable_interrupt(pin);
// Unregister the interrupt
gpio_unregister_interrupt(pin);
return 0;
}
Types
Name | |
---|---|
enum | gpio_irq_edge_t Enumeration for GPIO interrupt edge type. |
enum | gpio_pin_dir_t Enumeration representing different directions of a GPIO pin. |
enum | gpio_pin_logic_state_t Defines the logic states for GPIO pins. |
enum | gpio_pin_state_t Enumeration for GPIO pin states. |
enum | gpio_pin_mode_t Enumeration for GPIO pin modes. |
typedef void(*)(void) | gpio_irq_cb_t |
Functions Overview
Name | |
---|---|
cw_driver_return_t | gpio_get(uint16_t pin) Get the value of a GPIO pin. |
cw_driver_return_t | gpio_set(uint16_t pin, gpio_pin_state_t state) Sets the state of a GPIO pin. |
cw_driver_return_t | gpio_set_dir(uint16_t gpio, gpio_pin_dir_t dir) Sets the direction of a GPIO pin. |
cw_driver_return_t | gpio_set_mode(uint16_t gpio, gpio_pin_mode_t mode) Sets the mode of a GPIO pin. |
cw_driver_return_t | gpio_register_interrupt(uint16_t gpio, gpio_irq_edge_t edge_type, gpio_irq_cb_t callback) Registers an interrupt for a GPIO pin with a specified edge type and callback function. |
cw_driver_return_t | gpio_unregister_interrupt(uint16_t gpio) Unregisters an interrupt for a GPIO pin. |
cw_driver_return_t | gpio_enable_interrupt(uint16_t gpio) Enables interrupt for a GPIO pin. |
cw_driver_return_t | gpio_disable_interrupt(uint16_t gpio) Disables interrupt for a GPIO pin. |
Defines
Name | |
---|---|
GPIO_PIN(p) | |
GPIO_PORT(p) |
Types Documentation
enum gpio_irq_edge_t
Enumerator | Description |
---|---|
gpioIrqFallingEdge | Indicates interrupt trigger on the falling edge of the signal. |
gpioIrqRisingEdge | Indicates interrupt trigger on the rising edge of the signal. |
gpioIrqBothEdges | Indicates interrupt trigger on both the falling and rising edges of the signal. |
Enumeration for GPIO interrupt edge type.
Note: Depending on the specific GPIO implementation, not all edge types may be supported
This enumeration defines the available options for GPIO interrupt edge type.
enum gpio_pin_dir_t
Enumerator | Description |
---|---|
gpioPinDirInput | Specifies that the pin is set as an input. |
gpioPinDirOutput | Specifies that the pin is set as an output. |
gpioPinDirOutputOpenDrain | Specifies that the pin is set as an output in open-drain mode. |
Enumeration representing different directions of a GPIO pin.
See: gpio_init
Note: When a pin is set as an output in open-drain mode, it can either be driven low or left floating to be pulled high externally.
The gpio_pin_dir_t
enum represents the various directions of a GPIO pin. It can be used to specify whether a pin is set as an input, output, or output in open-drain mode.
enum gpio_pin_logic_state_t
Enumerator | Description |
---|---|
gpioActiveLow | |
gpioActiveHigh |
Defines the logic states for GPIO pins.
This enumeration defines the possible logic states for GPIO pins, which can be either active low or active high.
enum gpio_pin_state_t
Enumerator | Description |
---|---|
gpioLogicLow | |
gpioLogicHigh |
Enumeration for GPIO pin states.
Note: The underlying values for the states are 0 for logic low and 1 for logic high.
This enumeration defines the possible states for a GPIO pin. The states can be either logic low or logic high.
enum gpio_pin_mode_t
Enumerator | Description |
---|---|
gpioModePullOff | No internal pull resistor enabled. |
gpioModePullUp | Internal pull-up resistor enabled. |
gpioModePullDown | Internal pull-down resistor enabled. |
Enumeration for GPIO pin modes.
This enumeration defines the possible pull-resistor modes for a GPIO pin.
typedef gpio_irq_cb_t
cpp
typedef void(* gpio_irq_cb_t) (void);
Function Details
function gpio_get
cpp
cw_driver_return_t gpio_get(
uint16_t pin
)
Get the value of a GPIO pin.
Parameters:
- pin The GPIO pin number.
Return: The value of the GPIO pin, or -ENODEV if the device is not available.
This function retrieves the value of the specified GPIO pin.
function gpio_set
cpp
cw_driver_return_t gpio_set(
uint16_t pin,
gpio_pin_state_t state
)
Sets the state of a GPIO pin.
Parameters:
- pin The identifier of the GPIO pin.
- state The logic state to set the pin to. Valid values are gpioLogicLow and gpioLogicHigh.
Return: A cw_driver_return_t value indicating the success of the operation. Return value -ENODEV indicates that the GPIO device is not available or not opened successfully. Otherwise, returns the result of the gpio_set_pin function.
This function sets the logic state of the specified GPIO pin. The GPIO pin should be a unique identifier associated with a physical pin on the hardware. The state parameter determines whether the pin should be set to logic low or logic high.
function gpio_set_dir
cpp
cw_driver_return_t gpio_set_dir(
uint16_t gpio,
gpio_pin_dir_t dir
)
Sets the direction of a GPIO pin.
Parameters:
- gpio The number of the GPIO pin to set the direction for.
- dir The direction to set for the GPIO pin.
See: gpio_set_pin_dir()
Return: CW_DRIVER_RETURN_OK on success, or a negative error code on failure. Possible error codes:
- ENODEV: The GPIO device has not been opened or is not available.
This function sets the direction of the specified GPIO pin to the given direction. It internally calls the function gpio_set_pin_dir() to perform the actual operation.
function gpio_set_mode
cpp
cw_driver_return_t gpio_set_mode(
uint16_t gpio,
gpio_pin_mode_t mode
)
Sets the mode of a GPIO pin.
Parameters:
- gpio The pin number of the GPIO pin.
- mode The mode to set for the GPIO pin. Must be one of the values from the gpio_pin_mode_t enum.
Return: cw_driver_return_t The result of the operation. Returns -ENODEV if the GPIO device is not available.
This function sets the mode of a specified GPIO pin to either "Pull Off", "Pull Up", or "Pull Down".
function gpio_register_interrupt
cpp
cw_driver_return_t gpio_register_interrupt(
uint16_t gpio,
gpio_irq_edge_t edge_type,
gpio_irq_cb_t callback
)
Registers an interrupt for a GPIO pin with a specified edge type and callback function.
Parameters:
- gpio The GPIO pin number.
- edge_type The edge type for the interrupt (gpioIrqFallingEdge, gpioIrqRisingEdge, gpioIrqBothEdges).
- callback The callback function to be executed when the specified interrupt occurs.
Return: Returns the status of the driver operation (cw_driver_return_t).
This function registers an interrupt for the specified GPIO pin with the specified edge type and callback function. The function first checks if the GPIO device is already opened. If not, it opens the GPIO device by calling the "open" function. If the device opening fails, it returns -ENODEV (Device not found). If the device opening is successful, it then calls the "gpio_register_pin_interrupt" function to register the interrupt. The "gpio_register_pin_interrupt" function returns the status of the driver operation, which is returned by this function.
Note: The "cw_gpio_dev" variable holds the handle to the opened GPIO device. It is initially set to -1 and is used to check if the device is already opened. The "open" function is responsible for opening the GPIO device and returning a handle to it.
Example usage: cw_driver_return_t ret = gpio_register_interrupt(5, gpioIrqFallingEdge, callback_func); if (ret != CW_DRIVER_OK)
function gpio_unregister_interrupt
cpp
cw_driver_return_t gpio_unregister_interrupt(
uint16_t gpio
)
Unregisters an interrupt for a GPIO pin.
Parameters:
- gpio The GPIO pin number to unregister the interrupt for.
Return: cw_driver_return_t The return status of the function. Returns -ENODEV if the GPIO device is not open.
This function unregisters an interrupt for the specified GPIO pin.
function gpio_enable_interrupt
cpp
cw_driver_return_t gpio_enable_interrupt(
uint16_t gpio
)
Enables interrupt for a GPIO pin.
Parameters:
- gpio The GPIO pin number for which interrupt needs to be enabled.
Return: If the interrupt is enabled successfully, it returns 0. If there is an error opening the GPIO device, it returns -ENODEV.
This function enables interrupt for the given GPIO pin. Before enabling the interrupt, it checks if the GPIO device is already opened. If the device is not open, it opens the device using the open() function. If the open operation fails, it returns -ENODEV.
function gpio_disable_interrupt
cpp
cw_driver_return_t gpio_disable_interrupt(
uint16_t gpio
)
Disables interrupt for a GPIO pin.
Parameters:
- gpio The pin number of the GPIO.
Return: The status of the operation. -ENODEV if the GPIO device is not available, otherwise the return value of gpio_disable_pin_interrupt function.
This function disables interrupt for the specified GPIO pin. It first checks if the GPIO device is already open. If not, it opens the GPIO device. Then it calls the gpio_disable_pin_interrupt function with the GPIO device handle and the pin number.
Macros Documentation
define GPIO_PIN
cpp
#define GPIO_PIN(
p
)
(p % 100U)
define GPIO_PORT
cpp
#define GPIO_PORT(
p
)
(p / 100U)