MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_gpio.h
Go to the documentation of this file.
1/**
2 * @file meshx_gpio.h
3 * @brief MeshX GPIO Interface
4 *
5 * This file defines the MeshX GPIO interface for platform-abstracted GPIO operations.
6 * The interface supports digital I/O, interrupts, and function-based API for extensibility.
7 *
8 * @author MeshX Team
9 * @date 2024
10 */
11
12#ifndef __MESHX_GPIO_H
13#define __MESHX_GPIO_H
14
15#include <stdint.h>
16#include <stdbool.h>
17#include <stddef.h>
18#include "../../inc/meshx_err.h"
19#include "meshx_gpio_types.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25/**
26 * @brief GPIO Pin Modes
27 */
28typedef enum {
29 MESHX_GPIO_MODE_INPUT = 0, /**< Input only mode */
30 MESHX_GPIO_MODE_OUTPUT, /**< Output only mode */
31 MESHX_GPIO_MODE_INPUT_OUTPUT, /**< Input and output mode */
32 MESHX_GPIO_MODE_OPEN_DRAIN, /**< Open drain output mode */
33 MESHX_GPIO_MODE_OPEN_DRAIN_INPUT_OUTPUT,/**< Open drain input/output mode */
34 MESHX_GPIO_MODE_PWM_OUTPUT, /**< PWM output mode */
35 MESHX_GPIO_MODE_MAX /**< Maximum mode value */
37
38/**
39 * @brief GPIO Pull Resistor Settings
40 */
41typedef enum {
42 MESHX_GPIO_PULL_NONE = 0, /**< No pull resistor */
43 MESHX_GPIO_PULL_UP, /**< Pull-up resistor */
44 MESHX_GPIO_PULL_DOWN, /**< Pull-down resistor */
45 MESHX_GPIO_PULL_UP_DOWN, /**< Both pull-up and pull-down */
46 MESHX_GPIO_PULL_MAX /**< Maximum pull value */
48
49/**
50 * @brief GPIO Drive Strength
51 */
52typedef enum {
53 MESHX_GPIO_DRIVE_WEAK = 0, /**< Weak drive strength */
54 MESHX_GPIO_DRIVE_MEDIUM, /**< Medium drive strength */
55 MESHX_GPIO_DRIVE_STRONG, /**< Strong drive strength */
56 MESHX_GPIO_DRIVE_MAX_STRONG, /**< Maximum strong drive strength */
57 MESHX_GPIO_DRIVE_MAX /**< Maximum drive value */
59
60/**
61 * @brief IO Function Types for function-based API
62 */
63typedef enum {
64 MESHX_IO_FUNCTION_SET_LEVEL = 0, /**< Set pin level function */
65 MESHX_IO_FUNCTION_GET_LEVEL, /**< Get pin level function */
66 MESHX_IO_FUNCTION_TOGGLE, /**< Toggle pin function */
67 MESHX_IO_FUNCTION_SET_PWM_DUTY, /**< Set PWM duty cycle function */
68 MESHX_IO_FUNCTION_SET_PWM_FREQUENCY, /**< Set PWM frequency function */
69 MESHX_IO_FUNCTION_REGISTER_INTERRUPT, /**< Register interrupt function */
70 MESHX_IO_FUNCTION_CUSTOM, /**< Custom function (for extensibility) */
71 MESHX_IO_FUNCTION_MAX /**< Maximum function type */
73
74/**
75 * @brief GPIO Interrupt Callback Function
76 *
77 * @param logical_pin Logical pin number (0-255)
78 * @param user_data User data passed during registration
79 */
80typedef void (*meshx_gpio_intr_cb_t)(uint8_t logical_pin, void *user_data);
81
82/**
83 * @brief Initialize GPIO subsystem
84 *
85 * This function initializes the GPIO subsystem and configures all pins
86 * according to the compiled configuration.
87 *
88 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
89 */
91
92/**
93 * @brief Deinitialize GPIO subsystem
94 *
95 * This function deinitializes the GPIO subsystem and releases all resources.
96 *
97 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
98 */
100
101/**
102 * @brief Set GPIO pin level
103 *
104 * @param logical_pin Logical pin number (0-255)
105 * @param level Pin level (0 = low, 1 = high)
106 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
107 */
108meshx_err_t meshx_gpio_set_level(uint8_t logical_pin, uint8_t level);
109
110/**
111 * @brief Get GPIO pin level
112 *
113 * @param logical_pin Logical pin number (0-255)
114 * @param[out] level Pointer to store pin level
115 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
116 */
117meshx_err_t meshx_gpio_get_level(uint8_t logical_pin, uint8_t *level);
118
119/**
120 * @brief Toggle GPIO pin level
121 *
122 * @param logical_pin Logical pin number (0-255)
123 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
124 */
125meshx_err_t meshx_gpio_toggle(uint8_t logical_pin);
126
127/**
128 * @brief Execute IO function on GPIO pin
129 *
130 * This is the function-based API for extensible GPIO operations.
131 *
132 * @param logical_pin Logical pin number (0-255)
133 * @param function IO function to execute
134 * @param args Function arguments vector
135 * @param arg_count Number of arguments in the vector
136 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
137 */
138meshx_err_t meshx_gpio_execute_function(uint8_t logical_pin,
139 meshx_io_function_t function,
140 const uint32_t *args,
141 uint8_t arg_count);
142
143/**
144 * @brief Register GPIO interrupt handler
145 *
146 * @param logical_pin Logical pin number (0-255)
147 * @param intr_type Interrupt trigger type
148 * @param callback Interrupt callback function
149 * @param user_data User data passed to callback
150 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
151 */
152meshx_err_t meshx_gpio_register_intr(uint8_t logical_pin,
153 meshx_gpio_intr_type_t intr_type,
154 meshx_gpio_intr_cb_t callback,
155 void *user_data);
156
157/**
158 * @brief Unregister GPIO interrupt handler
159 *
160 * @param logical_pin Logical pin number (0-255)
161 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
162 */
163meshx_err_t meshx_gpio_unregister_intr(uint8_t logical_pin);
164
165/**
166 * @brief Enable/disable GPIO interrupt
167 *
168 * @param logical_pin Logical pin number (0-255)
169 * @param enable true to enable, false to disable
170 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
171 */
172meshx_err_t meshx_gpio_intr_enable(uint8_t logical_pin, bool enable);
173
174/**
175 * @brief Set hosted mode for GPIO subsystem
176 *
177 * This function switches the GPIO subsystem between hosted and non-hosted modes.
178 * In hosted mode, GPIO events are serialized and sent via UART transport.
179 * In non-hosted mode, GPIO operations directly control hardware.
180 *
181 * @param mode Hosted mode setting (MESHX_GPIO_MODE_HOSTED or MESHX_GPIO_MODE_NON_HOSTED)
182 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
183 */
185
186/**
187 * @brief Get current hosted mode
188 *
189 * @return meshx_gpio_hosted_mode_t Current hosted mode
190 */
192
193/**
194 * @brief Check if GPIO subsystem is in hosted mode
195 *
196 * @return true if in hosted mode, false otherwise
197 */
199
200/**
201 * @brief Register callback for GPIO hosted mode events
202 *
203 * This callback is invoked when GPIO events need to be sent to the host MCU
204 * in hosted mode.
205 *
206 * @param event GPIO hosted mode event
207 */
209
210/**
211 * @brief Register callback function for hosted mode events
212 *
213 * @param callback Callback function for hosted mode events
214 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
215 */
217
218/**
219 * @brief Process GPIO interrupt event from host (hosted mode)
220 *
221 * This function is called when the host sends an interrupt notification
222 * for a GPIO pin that was registered for interrupts.
223 *
224 * @param logical_pin Logical pin number
225 * @param value Interrupt value (trigger type or pin state)
226 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
227 */
228meshx_err_t meshx_gpio_process_hosted_interrupt(uint8_t logical_pin, uint8_t value);
229
230/**
231 * @brief Check if GPIO subsystem is initialized
232 *
233 * @return true if initialized, false otherwise
234 */
236
237/**
238 * @brief Get number of configured GPIO pins
239 *
240 * @return uint8_t Number of configured pins
241 */
242uint8_t meshx_gpio_get_pin_count(void);
243
244/**
245 * @brief Get pin configuration
246 *
247 * @param logical_pin Logical pin number
248 * @param[out] config Pointer to store pin configuration
249 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
250 */
252
253/**
254 * @brief Get pin runtime state
255 *
256 * @param logical_pin Logical pin number
257 * @param[out] state Pointer to store pin state
258 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
259 */
261
262#ifdef __cplusplus
263}
264#endif
265
266#endif /* __MESHX_GPIO_H */
MeshX Error Codes.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
void(* meshx_gpio_hosted_event_cb_t)(const meshx_gpio_hosted_event_t *event)
Register callback for GPIO hosted mode events.
Definition meshx_gpio.h:208
meshx_gpio_pull_t
GPIO Pull Resistor Settings.
Definition meshx_gpio.h:41
@ MESHX_GPIO_PULL_DOWN
Definition meshx_gpio.h:44
@ MESHX_GPIO_PULL_NONE
Definition meshx_gpio.h:42
@ MESHX_GPIO_PULL_UP
Definition meshx_gpio.h:43
@ MESHX_GPIO_PULL_UP_DOWN
Definition meshx_gpio.h:45
@ MESHX_GPIO_PULL_MAX
Definition meshx_gpio.h:46
meshx_err_t meshx_gpio_register_hosted_event_cb(meshx_gpio_hosted_event_cb_t callback)
Register callback function for hosted mode events.
Definition meshx_gpio.c:802
meshx_err_t meshx_gpio_set_level(uint8_t logical_pin, uint8_t level)
Set GPIO pin level.
Definition meshx_gpio.c:322
meshx_err_t meshx_gpio_execute_function(uint8_t logical_pin, meshx_io_function_t function, const uint32_t *args, uint8_t arg_count)
Execute IO function on GPIO pin.
Definition meshx_gpio.c:616
meshx_err_t meshx_gpio_process_hosted_interrupt(uint8_t logical_pin, uint8_t value)
Process GPIO interrupt event from host (hosted mode).
Definition meshx_gpio.c:856
meshx_err_t meshx_gpio_get_level(uint8_t logical_pin, uint8_t *level)
Get GPIO pin level.
Definition meshx_gpio.c:392
meshx_err_t meshx_gpio_intr_enable(uint8_t logical_pin, bool enable)
Enable/disable GPIO interrupt.
Definition meshx_gpio.c:591
meshx_err_t meshx_gpio_unregister_intr(uint8_t logical_pin)
Unregister GPIO interrupt handler.
Definition meshx_gpio.c:557
meshx_err_t meshx_gpio_get_pin_state(uint8_t logical_pin, meshx_gpio_pin_state_t *state)
Get pin runtime state.
Definition meshx_gpio.c:689
meshx_err_t meshx_gpio_init(void)
Initialize GPIO subsystem.
Definition meshx_gpio.c:95
meshx_err_t meshx_gpio_set_hosted_mode(meshx_gpio_hosted_mode_t mode)
Set hosted mode for GPIO subsystem.
Definition meshx_gpio.c:711
meshx_err_t meshx_gpio_deinit(void)
Deinitialize GPIO subsystem.
Definition meshx_gpio.c:189
uint8_t meshx_gpio_get_pin_count(void)
Get number of configured GPIO pins.
Definition meshx_gpio.c:651
meshx_gpio_hosted_mode_t meshx_gpio_get_hosted_mode(void)
Get current hosted mode.
Definition meshx_gpio.c:786
bool meshx_gpio_is_initialized(void)
Check if GPIO subsystem is initialized.
Definition meshx_gpio.c:641
meshx_err_t meshx_gpio_get_pin_config(uint8_t logical_pin, meshx_gpio_pin_config_t *config)
Get pin configuration.
Definition meshx_gpio.c:663
meshx_err_t meshx_gpio_toggle(uint8_t logical_pin)
Toggle GPIO pin level.
Definition meshx_gpio.c:453
meshx_gpio_mode_t
GPIO Pin Modes.
Definition meshx_gpio.h:28
@ MESHX_GPIO_MODE_INPUT_OUTPUT
Definition meshx_gpio.h:31
@ MESHX_GPIO_MODE_OPEN_DRAIN_INPUT_OUTPUT
Definition meshx_gpio.h:33
@ MESHX_GPIO_MODE_INPUT
Definition meshx_gpio.h:29
@ MESHX_GPIO_MODE_PWM_OUTPUT
Definition meshx_gpio.h:34
@ MESHX_GPIO_MODE_OPEN_DRAIN
Definition meshx_gpio.h:32
@ MESHX_GPIO_MODE_MAX
Definition meshx_gpio.h:35
@ MESHX_GPIO_MODE_OUTPUT
Definition meshx_gpio.h:30
meshx_io_function_t
IO Function Types for function-based API.
Definition meshx_gpio.h:63
@ MESHX_IO_FUNCTION_MAX
Definition meshx_gpio.h:71
@ MESHX_IO_FUNCTION_CUSTOM
Definition meshx_gpio.h:70
@ MESHX_IO_FUNCTION_REGISTER_INTERRUPT
Definition meshx_gpio.h:69
@ MESHX_IO_FUNCTION_SET_PWM_FREQUENCY
Definition meshx_gpio.h:68
@ MESHX_IO_FUNCTION_SET_LEVEL
Definition meshx_gpio.h:64
@ MESHX_IO_FUNCTION_TOGGLE
Definition meshx_gpio.h:66
@ MESHX_IO_FUNCTION_GET_LEVEL
Definition meshx_gpio.h:65
@ MESHX_IO_FUNCTION_SET_PWM_DUTY
Definition meshx_gpio.h:67
meshx_err_t meshx_gpio_register_intr(uint8_t logical_pin, meshx_gpio_intr_type_t intr_type, meshx_gpio_intr_cb_t callback, void *user_data)
Register GPIO interrupt handler.
Definition meshx_gpio.c:488
void(* meshx_gpio_intr_cb_t)(uint8_t logical_pin, void *user_data)
GPIO Interrupt Callback Function.
Definition meshx_gpio.h:80
meshx_gpio_drive_t
GPIO Drive Strength.
Definition meshx_gpio.h:52
@ MESHX_GPIO_DRIVE_WEAK
Definition meshx_gpio.h:53
@ MESHX_GPIO_DRIVE_MEDIUM
Definition meshx_gpio.h:54
@ MESHX_GPIO_DRIVE_STRONG
Definition meshx_gpio.h:55
@ MESHX_GPIO_DRIVE_MAX_STRONG
Definition meshx_gpio.h:56
@ MESHX_GPIO_DRIVE_MAX
Definition meshx_gpio.h:57
bool meshx_gpio_is_hosted_mode(void)
Check if GPIO subsystem is in hosted mode.
Definition meshx_gpio.c:794
MeshX GPIO Type Definitions.
meshx_gpio_intr_type_t
GPIO Interrupt Trigger Types.
Definition meshx_gpio_types.h:28
meshx_gpio_hosted_mode_t
Hosted Mode State.
Definition meshx_gpio_types.h:169
uint8_t state
Definition meshx_serial.c:39
Hosted Mode GPIO Event.
Definition meshx_gpio_types.h:180
GPIO Pin Configuration Structure.
Definition meshx_gpio_types.h:51
GPIO Pin State Structure.
Definition meshx_gpio_types.h:91