16#ifndef __MESHX_GPIO_KV_H
17#define __MESHX_GPIO_KV_H
35#define MESHX_GPIO_CONFIG_VERSION_CURRENT 1
38#define MESHX_GPIO_CONFIG_VERSION_MIN 1
41#define MESHX_GPIO_CONFIG_VERSION_MAX 1
48#define MESHX_GPIO_KV_PRODUCT_NAME_MAX_LEN 16
51#define MESHX_GPIO_KV_KEY_MAX_LEN 32
54#define MESHX_GPIO_KV_KEY_CONFIG "config"
57#define MESHX_GPIO_KV_KEY_STATE "state"
60#define MESHX_GPIO_KV_KEY_PWM "pwm"
63#define MESHX_GPIO_KV_KEY_INTR "intr"
73 char *buf, uint8_t buf_len)
78 return (uint8_t)snprintf(buf, buf_len,
"gpio_%.*s_%s",
94 char *buf, uint8_t buf_len)
99 return (uint8_t)snprintf(buf, buf_len,
"gpio_%.*s_%s_%u",
116 char *buf, uint8_t buf_len)
121 return (uint8_t)snprintf(buf, buf_len,
"gpio_%.*s_%s_%u",
138 char *buf, uint8_t buf_len)
143 return (uint8_t)snprintf(buf, buf_len,
"gpio_%.*s_%s_%u",
240#define MESHX_GPIO_CONFIG_HEADER_SIZE sizeof(meshx_gpio_config_header_kv_t)
243#define MESHX_GPIO_PIN_CONFIG_SIZE sizeof(meshx_gpio_pin_config_kv_t)
246#define MESHX_GPIO_PWM_CONFIG_SIZE sizeof(meshx_gpio_pwm_config_kv_t)
249#define MESHX_GPIO_INTR_CONFIG_SIZE sizeof(meshx_gpio_intr_config_kv_t)
252#define MESHX_GPIO_PIN_STATE_SIZE sizeof(meshx_gpio_pin_state_kv_t)
255#define MESHX_GPIO_PIN_MAX_SIZE (MESHX_GPIO_PIN_CONFIG_SIZE + \
256 MESHX_GPIO_PWM_CONFIG_SIZE)
259#define MESHX_GPIO_CONFIG_TOTAL_SIZE(pin_count) \
260 (MESHX_GPIO_CONFIG_HEADER_SIZE + ((pin_count) * MESHX_GPIO_PIN_CONFIG_SIZE))
267#define MESHX_GPIO_CONFIG_FLAG_HAS_PWM (1 << 0)
270#define MESHX_GPIO_CONFIG_FLAG_HAS_INTR (1 << 1)
273#define MESHX_GPIO_CONFIG_FLAG_MIGRATED (1 << 2)
276#define MESHX_GPIO_CONFIG_FLAG_READ_ONLY (1 << 3)
283#define MESHX_GPIO_CONFIG_TYPE_NONE 0
286#define MESHX_GPIO_CONFIG_TYPE_PWM 1
289#define MESHX_GPIO_CONFIG_TYPE_INTR 2
308 uint16_t crc = 0xFFFF;
309 for (uint32_t i = 0; i < len; i++) {
311 for (
int j = 0; j < 8; j++) {
313 crc = (crc >> 1) ^ 0xA001;
343 return (calculated == header->
crc16);
399 if (!src || !dst)
return;
410 if (src->
mode == 5) {
429 if (!src || !dst)
return;
449 if (!src || !dst)
return;
467 if (!src || !dst)
return;
484 if (!src || !dst)
return;
504 if (!src || !dst)
return;
537 return (from_version != to_version);
555 const char *product_name);
585 uint8_t *out_pin_count);
654 uint8_t *out_pin_count);
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
Flash Abstraction Layer (FAL) Interface for MeshX.
#define MESHX_GPIO_KV_KEY_STATE
Key suffix for GPIO pin state.
Definition meshx_gpio_kv.h:57
#define MESHX_GPIO_CONFIG_TOTAL_SIZE(pin_count)
Calculate total config size for given pin count.
Definition meshx_gpio_kv.h:259
static uint8_t meshx_gpio_kv_make_pwm_key(const char *product_name, uint8_t logical_pin, char *buf, uint8_t buf_len)
Construct KV Engine key for PWM configuration.
Definition meshx_gpio_kv.h:114
static void meshx_gpio_kv_deserialize_pin(const meshx_gpio_pin_config_kv_t *src, meshx_gpio_pin_config_t *dst)
Convert serialized pin config to runtime format.
Definition meshx_gpio_kv.h:426
static void meshx_gpio_kv_serialize_pin(const meshx_gpio_pin_config_t *src, meshx_gpio_pin_config_kv_t *dst)
Convert runtime pin config to serialized format.
Definition meshx_gpio_kv.h:396
static bool meshx_gpio_kv_verify_crc16(const uint8_t *data, uint32_t len)
Verify CRC16 of serialized GPIO configuration.
Definition meshx_gpio_kv.h:329
static uint8_t meshx_gpio_kv_make_state_key(const char *product_name, uint8_t logical_pin, char *buf, uint8_t buf_len)
Construct KV Engine key for GPIO pin state.
Definition meshx_gpio_kv.h:92
#define MESHX_GPIO_CONFIG_VERSION_MIN
Minimum supported version (for backward compatibility).
Definition meshx_gpio_kv.h:38
#define MESHX_GPIO_KV_KEY_CONFIG
Key suffix for main GPIO configuration.
Definition meshx_gpio_kv.h:54
static bool meshx_gpio_kv_is_version_supported(uint8_t version)
Check if a configuration version is supported.
Definition meshx_gpio_kv.h:521
#define MESHX_GPIO_CONFIG_TYPE_NONE
No extended configuration.
Definition meshx_gpio_kv.h:283
#define MESHX_GPIO_CONFIG_TYPE_PWM
PWM configuration follows.
Definition meshx_gpio_kv.h:286
meshx_err_t meshx_gpio_kv_init(const meshx_fal_partition_t *kv_partition, const char *product_name)
Initialize GPIO KV Engine persistence.
Definition meshx_gpio_kv.c:230
static void meshx_gpio_kv_serialize_pwm(const meshx_gpio_pin_config_t *src, meshx_gpio_pwm_config_kv_t *dst)
Serialize PWM configuration.
Definition meshx_gpio_kv.h:446
meshx_err_t meshx_gpio_save_pin_state_to_kv(uint8_t logical_pin, const meshx_gpio_pin_state_t *state)
Save current pin state to KV Engine.
Definition meshx_gpio_kv.c:420
#define MESHX_GPIO_KV_KEY_MAX_LEN
Maximum total key length (KV Engine limit is 32).
Definition meshx_gpio_kv.h:51
bool meshx_gpio_kv_is_initialized(void)
Check if GPIO KV persistence is initialized.
Definition meshx_gpio_kv.c:639
meshx_err_t meshx_gpio_load_config_from_kv(meshx_gpio_pin_config_t *configs, uint8_t max_pins, uint8_t *out_pin_count)
Load GPIO configuration from KV Engine.
Definition meshx_gpio_kv.c:331
#define MESHX_GPIO_CONFIG_HEADER_SIZE
Size of configuration header.
Definition meshx_gpio_kv.h:240
static uint8_t meshx_gpio_kv_make_config_key(const char *product_name, char *buf, uint8_t buf_len)
Construct KV Engine key for GPIO configuration.
Definition meshx_gpio_kv.h:72
#define MESHX_GPIO_KV_PRODUCT_NAME_MAX_LEN
Maximum length for product name in key prefix.
Definition meshx_gpio_kv.h:48
meshx_err_t meshx_gpio_kv_deinit(void)
Deinitialize GPIO KV Engine persistence.
Definition meshx_gpio_kv.c:620
static uint8_t meshx_gpio_kv_make_intr_key(const char *product_name, uint8_t logical_pin, char *buf, uint8_t buf_len)
Construct KV Engine key for interrupt configuration.
Definition meshx_gpio_kv.h:136
static void meshx_gpio_kv_finalize_crc16(uint8_t *data, uint32_t len)
Calculate and set CRC16 for serialized configuration.
Definition meshx_gpio_kv.h:377
static void meshx_gpio_kv_deserialize_intr(const meshx_gpio_intr_config_kv_t *src, meshx_gpio_pin_config_t *dst)
Deserialize interrupt configuration.
Definition meshx_gpio_kv.h:501
meshx_err_t meshx_gpio_export_config(const meshx_gpio_pin_config_t *configs, uint8_t pin_count, uint8_t *buf, uint16_t buf_len, uint16_t *out_size)
Export GPIO configuration to serialized format.
Definition meshx_gpio_kv.c:532
meshx_err_t meshx_gpio_load_pin_state_from_kv(uint8_t logical_pin, meshx_gpio_pin_state_t *state)
Load pin state from KV Engine.
Definition meshx_gpio_kv.c:477
static bool meshx_gpio_kv_needs_migration(uint8_t from_version, uint8_t to_version)
Get migration requirement between versions.
Definition meshx_gpio_kv.h:534
#define MESHX_GPIO_CONFIG_TYPE_INTR
Interrupt configuration follows.
Definition meshx_gpio_kv.h:289
meshx_err_t meshx_gpio_import_config(const uint8_t *buf, uint16_t buf_len, meshx_gpio_pin_config_t *configs, uint8_t max_pins, uint8_t *out_pin_count)
Import GPIO configuration from serialized format.
Definition meshx_gpio_kv.c:558
#define MESHX_GPIO_CONFIG_VERSION_CURRENT
Current GPIO configuration serialization format version.
Definition meshx_gpio_kv.h:35
static void meshx_gpio_kv_deserialize_pwm(const meshx_gpio_pwm_config_kv_t *src, meshx_gpio_pin_config_t *dst)
Deserialize PWM configuration.
Definition meshx_gpio_kv.h:464
#define MESHX_GPIO_KV_KEY_INTR
Key suffix for interrupt configuration.
Definition meshx_gpio_kv.h:63
static void meshx_gpio_kv_serialize_intr(const meshx_gpio_pin_config_t *src, meshx_gpio_intr_config_kv_t *dst)
Serialize interrupt configuration.
Definition meshx_gpio_kv.h:481
meshx_err_t meshx_gpio_clear_config_in_kv(void)
Clear GPIO configuration from KV Engine.
Definition meshx_gpio_kv.c:580
meshx_err_t meshx_gpio_save_config_to_kv(const meshx_gpio_pin_config_t *configs, uint8_t pin_count)
Save GPIO configuration to KV Engine.
Definition meshx_gpio_kv.c:269
meshx_err_t meshx_gpio_config_exists_in_kv(bool *exists)
Check if GPIO configuration exists in KV Engine.
Definition meshx_gpio_kv.c:384
static void meshx_gpio_kv_init_header(meshx_gpio_config_header_kv_t *header, uint8_t pin_count)
Initialize a GPIO configuration header.
Definition meshx_gpio_kv.h:356
#define MESHX_GPIO_CONFIG_VERSION_MAX
Maximum supported version (for forward compatibility).
Definition meshx_gpio_kv.h:41
static uint16_t meshx_gpio_kv_calc_crc16(const uint8_t *data, uint32_t len)
Calculate CRC16 compatible with KV Engine.
Definition meshx_gpio_kv.h:306
#define MESHX_GPIO_KV_KEY_PWM
Key suffix for PWM configuration.
Definition meshx_gpio_kv.h:60
MeshX GPIO Type Definitions.
uint8_t state
Definition meshx_serial.c:39
Definition meshx_fal_interface.h:27
Serialized interrupt configuration for KV Engine storage.
Definition meshx_gpio_kv.h:211
uint8_t trigger_type
Definition meshx_gpio_kv.h:212
uint8_t flags
Definition meshx_gpio_kv.h:215
uint8_t reserved[3]
Definition meshx_gpio_kv.h:216
uint16_t task_stack_size
Definition meshx_gpio_kv.h:214
uint8_t task_priority
Definition meshx_gpio_kv.h:213
Serialized GPIO pin configuration for KV Engine storage.
Definition meshx_gpio_kv.h:178
uint8_t logical_pin
Definition meshx_gpio_kv.h:179
uint8_t pull
Definition meshx_gpio_kv.h:182
uint8_t mode
Definition meshx_gpio_kv.h:181
uint8_t config_type
Definition meshx_gpio_kv.h:186
uint8_t physical_pin
Definition meshx_gpio_kv.h:180
uint8_t drive_strength
Definition meshx_gpio_kv.h:183
uint8_t signal_inversion
Definition meshx_gpio_kv.h:185
uint8_t initial_level
Definition meshx_gpio_kv.h:184
GPIO Pin Configuration Structure.
Definition meshx_gpio_types.h:51
uint8_t logical_pin
Definition meshx_gpio_types.h:52
uint8_t channel
Definition meshx_gpio_types.h:74
uint8_t resolution
Definition meshx_gpio_types.h:73
uint32_t frequency
Definition meshx_gpio_types.h:71
union meshx_gpio_pin_config_t::@052245231341204203052073036272225075106126036255 mode_config
Mode-specific configuration (union based on mode).
uint8_t task_priority
Definition meshx_gpio_types.h:65
uint8_t mode
Definition meshx_gpio_types.h:54
uint8_t drive_strength
Definition meshx_gpio_types.h:56
uint16_t task_stack_size
Definition meshx_gpio_types.h:66
uint8_t duty_cycle
Definition meshx_gpio_types.h:72
struct meshx_gpio_pin_config_t::@052245231341204203052073036272225075106126036255::@372143037256331057042353173011302234062346240366 interrupt
Interrupt configuration (for input pins with interrupts).
uint8_t initial_level
Definition meshx_gpio_types.h:57
uint8_t physical_pin
Definition meshx_gpio_types.h:53
uint8_t trigger
Definition meshx_gpio_types.h:64
struct meshx_gpio_pin_config_t::@052245231341204203052073036272225075106126036255::@134165302231261162316035331261052006324312255312 pwm
PWM configuration (for PWM output pins).
bool signal_inversion
Definition meshx_gpio_types.h:58
uint8_t pull
Definition meshx_gpio_types.h:55
Serialized GPIO pin state for KV Engine storage.
Definition meshx_gpio_kv.h:226
uint8_t current_duty
Definition meshx_gpio_kv.h:230
uint8_t current_level
Definition meshx_gpio_kv.h:228
uint8_t logical_pin
Definition meshx_gpio_kv.h:227
uint8_t pwm_started
Definition meshx_gpio_kv.h:229
uint32_t reserved
Definition meshx_gpio_kv.h:231
GPIO Pin State Structure.
Definition meshx_gpio_types.h:91
Serialized PWM configuration for KV Engine storage.
Definition meshx_gpio_kv.h:196
uint8_t duty_cycle
Definition meshx_gpio_kv.h:198
uint8_t channel
Definition meshx_gpio_kv.h:200
uint8_t reserved
Definition meshx_gpio_kv.h:201
uint8_t resolution
Definition meshx_gpio_kv.h:199
uint32_t frequency
Definition meshx_gpio_kv.h:197