|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
* @date 24.04.2025
|
|
|
|
|
* @version 0.0.1
|
|
|
|
|
**************************************************************************************************
|
|
|
|
|
* @brief Platform-independent GPIO interface for the KED library.
|
|
|
|
|
* @brief Platform-independent GPIO interface for the KED library. For more details, see [KED Wiki - GPIO](https://wiki.kynsight.com/bin/view/Kynsight/Projects/KED/Peripherals/GPIO/).
|
|
|
|
|
*
|
|
|
|
|
* **Detailed Description :**
|
|
|
|
|
* This header defines a hardware abstraction layer (HAL) for General Purpose I/O (GPIO)
|
|
|
|
@ -19,10 +19,12 @@
|
|
|
|
|
*
|
|
|
|
|
* Usage Example:
|
|
|
|
|
* @code
|
|
|
|
|
* gpio_t* pin = KED_GPIO.init(&config);
|
|
|
|
|
* KED_GPIO.set(pin, true);
|
|
|
|
|
* bool state = KED_GPIO.read(pin);
|
|
|
|
|
* KED_GPIO.toggle(pin);
|
|
|
|
|
* gpio_config cfg = { ... };
|
|
|
|
|
* gpio_t pin = ked_gpio_init(&cfg);
|
|
|
|
|
* ked_gpio_set(&pin, true);
|
|
|
|
|
* bool state = ked_gpio_read(&pin);
|
|
|
|
|
* ked_gpio_toggle(&pin);
|
|
|
|
|
* ked_gpio_deinit(&pin);
|
|
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* @todo
|
|
|
|
@ -30,148 +32,146 @@
|
|
|
|
|
* - 24.04.2025: Implement Defined States, Errors, Configuration Options
|
|
|
|
|
**************************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
#ifndef KED_PERIPHERALS_GPIO_GPIO_H_
|
|
|
|
|
#define KED_PERIPHERALS_GPIO_GPIO_H_
|
|
|
|
|
#ifndef KED_PERIPHERALS_GPIO_H_
|
|
|
|
|
#define KED_PERIPHERALS_GPIO_H_
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Enumeration for GPIO functional modes.
|
|
|
|
|
*/
|
|
|
|
|
/** GPIO Mode Types */
|
|
|
|
|
typedef enum {
|
|
|
|
|
GPIO_MODE_DEFAULT,
|
|
|
|
|
GPIO_MODE_INPUT,
|
|
|
|
|
GPIO_MODE_OUTPUT,
|
|
|
|
|
GPIO_MODE_ANALOG,
|
|
|
|
|
GPIO_MODE_ALTERNATE
|
|
|
|
|
} gpio_mode_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Enumeration for GPIO direction settings.
|
|
|
|
|
*/
|
|
|
|
|
T_GPIO_MODE_DEFAULT,
|
|
|
|
|
T_GPIO_MODE_INPUT,
|
|
|
|
|
T_GPIO_MODE_OUTPUT,
|
|
|
|
|
T_GPIO_MODE_ANALOG,
|
|
|
|
|
T_GPIO_MODE_ALTERNATE
|
|
|
|
|
} gpio_function_t;
|
|
|
|
|
|
|
|
|
|
/** GPIO Direction (explicit) */
|
|
|
|
|
typedef enum {
|
|
|
|
|
GPIO_DIRECTION_INPUT,
|
|
|
|
|
GPIO_DIRECTION_OUTPUT
|
|
|
|
|
T_GPIO_DIRECTION_INPUT,
|
|
|
|
|
T_GPIO_DIRECTION_OUTPUT
|
|
|
|
|
} gpio_direction_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Enumeration for GPIO drive configurations.
|
|
|
|
|
*/
|
|
|
|
|
/** Drive types for output pins */
|
|
|
|
|
typedef enum {
|
|
|
|
|
GPIO_DRIVE_DEFAULT,
|
|
|
|
|
GPIO_DRIVE_PUSH_PULL,
|
|
|
|
|
GPIO_DRIVE_OPEN_DRAIN
|
|
|
|
|
T_GPIO_DRIVE_DEFAULT,
|
|
|
|
|
T_GPIO_DRIVE_PUSH_PULL,
|
|
|
|
|
T_GPIO_DRIVE_OPEN_DRAIN
|
|
|
|
|
} gpio_drive_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Enumeration for internal bias resistor settings.
|
|
|
|
|
*/
|
|
|
|
|
/** Pull-up/pull-down configurations */
|
|
|
|
|
typedef enum {
|
|
|
|
|
GPIO_BIAS_NONE,
|
|
|
|
|
GPIO_BIAS_DISABLE,
|
|
|
|
|
GPIO_BIAS_PULL_UP,
|
|
|
|
|
GPIO_BIAS_PULL_DOWN
|
|
|
|
|
T_GPIO_BIAS_DEFAULT,
|
|
|
|
|
T_GPIO_BIAS_DISABLE,
|
|
|
|
|
T_GPIO_BIAS_PULL_UP,
|
|
|
|
|
T_GPIO_BIAS_PULL_DOWN
|
|
|
|
|
} gpio_bias_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Enumeration for GPIO speed settings.
|
|
|
|
|
*/
|
|
|
|
|
/** Optional pin speed enumeration */
|
|
|
|
|
typedef enum {
|
|
|
|
|
GPIO_SPEED_DEFAULT,
|
|
|
|
|
GPIO_SPEED_LOW,
|
|
|
|
|
GPIO_SPEED_MEDIUM,
|
|
|
|
|
GPIO_SPEED_HIGH,
|
|
|
|
|
GPIO_SPEED_VERY_HIGH
|
|
|
|
|
T_GPIO_SPEED_DEFAULT,
|
|
|
|
|
T_GPIO_SPEED_LOW,
|
|
|
|
|
T_GPIO_SPEED_MEDIUM,
|
|
|
|
|
T_GPIO_SPEED_HIGH,
|
|
|
|
|
T_GPIO_SPEED_VERY_HIGH
|
|
|
|
|
} gpio_speed_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Enumeration for enabling or disabling GPIO interrupts.
|
|
|
|
|
*/
|
|
|
|
|
/** Enable/disable pin interrupt */
|
|
|
|
|
typedef enum {
|
|
|
|
|
GPIO_IRQ_DISABLED,
|
|
|
|
|
GPIO_IRQ_ENABLED
|
|
|
|
|
T_GPIO_IRQ_DISABLED,
|
|
|
|
|
T_GPIO_IRQ_ENABLED
|
|
|
|
|
} gpio_irq_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief GPIO error status codes.
|
|
|
|
|
*/
|
|
|
|
|
/** GPIO Status */
|
|
|
|
|
typedef enum {
|
|
|
|
|
T_GPIO_STATUS_NOT_INIT, /**< GPIO is not initialized */
|
|
|
|
|
T_GPIO_STATUS_READY, /**< GPIO is ready for use */
|
|
|
|
|
T_GPIO_STATUS_BUSY, /**< GPIO is currently in use */
|
|
|
|
|
T_GPIO_STATUS_ERROR, /**< GPIO encountered an error */
|
|
|
|
|
T_GPIO_STATUS_UPDATING /**< GPIO is currently being reconfigured */
|
|
|
|
|
} gpio_status_t;
|
|
|
|
|
|
|
|
|
|
/** GPIO error status codes */
|
|
|
|
|
typedef enum {
|
|
|
|
|
GPIO_OK,
|
|
|
|
|
GPIO_ERR_UNSUPPORTED_MODE,
|
|
|
|
|
GPIO_ERR_LINE_BUSY,
|
|
|
|
|
GPIO_ERR_NOT_AVAILABLE,
|
|
|
|
|
GPIO_ERR_NO_PULL,
|
|
|
|
|
GPIO_ERR_NOT_ANALOG,
|
|
|
|
|
GPIO_ERR_UNKNOWN
|
|
|
|
|
T_GPIO_ERR_OK, /*!< Suscess flag for the Init Function */
|
|
|
|
|
T_GPIO_ERR_UNSUPPORTED_MODE, /*!< Mode is either not valid orsupported by **Hardwaware** */
|
|
|
|
|
T_GPIO_ERR_UNSUPPORTED_ALTERNATE, /*!< Alternateive mode is unvalid */
|
|
|
|
|
T_GPIO_ERR_BUS_BUSY, /*!< The chosen GPIO BUS is already in Use */
|
|
|
|
|
T_GPIO_ERR_NOT_AVAILABLE, /*!< Pin does Not aloow GPIO functionalities or does not Exists */
|
|
|
|
|
T_GPIO_ERR_NOT_BIASABLE, /*!< No Pull-Up or Pull-Down Option are awailable */
|
|
|
|
|
T_GPIO_ERR_NO_INTERRUPT_SUPPORT, /*!< This GPIO Doesn't allow interrupts */
|
|
|
|
|
T_GPIO_ERR_UNSUPPORTED_DRIVE, /*!< This gate griving option is Unsupported (Pushpull, open drain etc..) */
|
|
|
|
|
T_GPIO_ERR_UNSUPPORTED_SPEED, /*!< The choosen bus speed is not supported */
|
|
|
|
|
T_GPIO_ERR_UNKNOWN /*!< Goal Would be To never be Used, but can be helpful during Developpment */
|
|
|
|
|
} gpio_error_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Function pointer type for writing a logic level.
|
|
|
|
|
*/
|
|
|
|
|
typedef void (*gpio_write_fn)(bool value);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Function pointer type for reading a logic level.
|
|
|
|
|
*/
|
|
|
|
|
typedef bool (*gpio_read_fn)(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Function pointer type for toggling a GPIO pin.
|
|
|
|
|
*/
|
|
|
|
|
typedef void (*gpio_toggle_fn)(void);
|
|
|
|
|
/** GPIO configuration structure for Linux */
|
|
|
|
|
#ifdef KED_USES_LINUX
|
|
|
|
|
typedef struct {
|
|
|
|
|
const char* chipname;
|
|
|
|
|
unsigned int line_offset;
|
|
|
|
|
gpio_function_t mode;
|
|
|
|
|
gpio_direction_t direction;
|
|
|
|
|
gpio_bias_t bias;
|
|
|
|
|
gpio_status_t status;
|
|
|
|
|
} gpio_t;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Function pointer type for GPIO deinitialization.
|
|
|
|
|
* @brief Initialize a GPIO pin.
|
|
|
|
|
* @param gpio Pointer to target gpio_t object
|
|
|
|
|
* @param config Pointer to configuration structure
|
|
|
|
|
* @return gpio_error_t status
|
|
|
|
|
*/
|
|
|
|
|
typedef void (*gpio_deinit_fn)(void);
|
|
|
|
|
gpio_error_t ked_gpio_init(const gpio_t* gpio);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Abstract GPIO handle with callback operations.
|
|
|
|
|
* @brief Update configuration of an already initialized GPIO.
|
|
|
|
|
*
|
|
|
|
|
* This function allows changing selected configuration parameters such as direction,
|
|
|
|
|
* bias, or other runtime-reconfigurable options without a full re-init.
|
|
|
|
|
*
|
|
|
|
|
* @param gpio Pointer to initialized gpio object
|
|
|
|
|
* @param config Pointer to new configuration structure
|
|
|
|
|
* @return gpio_error_t indicating success or failure of update
|
|
|
|
|
*/
|
|
|
|
|
typedef void (*gpio_error_fn)(gpio_error_t);
|
|
|
|
|
gpio_error_t ked_gpio_update(gpio_t* gpio);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Abstract GPIO handle with callback operations.
|
|
|
|
|
* @brief Deinitialize and release GPIO
|
|
|
|
|
* @param gpio Pointer to initialized gpio object
|
|
|
|
|
*/
|
|
|
|
|
typedef struct {
|
|
|
|
|
gpio_write_fn set;
|
|
|
|
|
gpio_read_fn read;
|
|
|
|
|
gpio_toggle_fn toggle;
|
|
|
|
|
gpio_deinit_fn deinit; /**< Deinitialization function pointer */
|
|
|
|
|
gpio_error_fn on_error; /**< Error handling callback */
|
|
|
|
|
} gpio_t;
|
|
|
|
|
void ked_gpio_deinit(gpio_t* gpio);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Function pointer type for platform-specific GPIO initialization.
|
|
|
|
|
* @brief Set GPIO output logic level
|
|
|
|
|
* @param gpio Pointer to initialized gpio object
|
|
|
|
|
* @param value True for HIGH, false for LOW
|
|
|
|
|
*/
|
|
|
|
|
typedef gpio_error_t (*gpio_init_fn)(gpio_t* gpio, const void* config);
|
|
|
|
|
void ked_gpio_set(gpio_t* gpio, uint8_t value);
|
|
|
|
|
|
|
|
|
|
#ifdef KED_USES_LINUX
|
|
|
|
|
/**
|
|
|
|
|
* @brief Platform-specific GPIO configuration structure for Linux.
|
|
|
|
|
* @brief Read the GPIO logic level
|
|
|
|
|
* @param gpio Pointer to initialized gpio object
|
|
|
|
|
* @return Logic level (true or false)
|
|
|
|
|
*/
|
|
|
|
|
typedef struct {
|
|
|
|
|
const char* chipname;
|
|
|
|
|
unsigned int line_offset;
|
|
|
|
|
gpio_mode_t mode;
|
|
|
|
|
gpio_direction_t direction;
|
|
|
|
|
gpio_bias_t bias;
|
|
|
|
|
} gpio_config;
|
|
|
|
|
#endif
|
|
|
|
|
uint8_t ked_gpio_read(gpio_t* gpio);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Initialize a GPIO using the specified configuration.
|
|
|
|
|
*
|
|
|
|
|
* @param gpio Pointer to target gpio_t handle
|
|
|
|
|
* @param config Platform-specific GPIO configuration struct
|
|
|
|
|
* @return GPIO_OK on success, or an appropriate gpio_error_t
|
|
|
|
|
* @brief Toggle GPIO output state
|
|
|
|
|
* @param gpio Pointer to initialized gpio object
|
|
|
|
|
*/
|
|
|
|
|
gpio_error_t ked_gpio_init(gpio_t* gpio, const gpio_config* config);
|
|
|
|
|
void ked_gpio_toggle(gpio_t* gpio);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Deinitialize and release resources associated with a GPIO pin.
|
|
|
|
|
*/
|
|
|
|
|
void ked_gpio_deinit(gpio_t* gpio);
|
|
|
|
|
void ked_gpio_handle_error(gpio_error_t err);
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif /* KED_PERIPHERALS_GPIO_GPIO_H_ */
|
|
|
|
|