18 Commits

Author SHA1 Message Date
a0e7add434 doc: updated example 2025-12-24 18:10:40 +03:00
e03c88dc5d fix: not full deinit 2025-12-24 15:15:25 +03:00
d76eec1753 fix: incorrect uninstall isr service 2025-12-23 19:02:49 +03:00
f8aef52a9c refactor: removed unused check 2025-12-21 22:08:17 +03:00
277ffa2c70 refactor: removed unused variable 2025-12-21 21:16:10 +03:00
364622ff58 feat: added deinit 2025-12-21 09:10:43 +03:00
d2b5dd7120 feat: added statistics 2025-12-21 09:05:01 +03:00
b1c38b4a96 refactor: refactored by pvs-studio 2025-12-14 21:20:31 +03:00
1a8f4711e8 feat: added task handle 2025-12-07 12:42:08 +03:00
0a770c360c fix: gpio isr service reinitialization error 2025-12-07 12:27:47 +03:00
379fae940b doc: updated readme 2025-11-30 22:43:44 +03:00
157aa7f49b doc: update example 2025-11-09 21:38:48 +03:00
e5efc03098 perf: updated error checks 2025-11-04 09:22:43 +03:00
e2808d0176 feat!: removed esp8266 support 2025-11-03 11:02:12 +03:00
8f5727c759 refactor: major error checks refactoring 2025-11-03 10:25:15 +03:00
9ac44a186e perf: updated error checks 2025-11-01 19:22:23 +03:00
47ca5cf2cb doc: update some comments for doxygen style 2025-11-01 10:04:32 +03:00
c9365e40ba fix: not working several interrupts 2025-06-07 12:20:23 +03:00
7 changed files with 353 additions and 255 deletions

View File

@@ -1,6 +1 @@
if(${IDF_TARGET} STREQUAL esp8266)
set(requires driver zh_vector)
else()
set(requires driver esp_event zh_vector)
endif()
idf_component_register(SRCS "zh_pcf8574.c" INCLUDE_DIRS "include" REQUIRES ${requires})
idf_component_register(SRCS "zh_pcf8574.c" INCLUDE_DIRS "include" REQUIRES driver esp_event zh_vector)

View File

@@ -1,9 +1,12 @@
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for PCF8574(A) 8-bit I/O expander
# ESP32 ESP-IDF component for PCF8574(A) 8-bit I/O expander
## Tested on
1. [ESP8266 RTOS_SDK v3.4](https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/index.html#)
2. [ESP32 ESP-IDF v5.4](https://docs.espressif.com/projects/esp-idf/en/release-v5.4/esp32/index.html)
1. [ESP32 ESP-IDF v5.5.1](https://docs.espressif.com/projects/esp-idf/en/v5.5.1/esp32/index.html)
## SAST Tools
[PVS-Studio](https://pvs-studio.com/pvs-studio/?utm_source=website&utm_medium=github&utm_campaign=open_source) - static analyzer for C, C++, C#, and Java code.
## Features
@@ -17,9 +20,17 @@
2. All the INT GPIO's on the extenders must be connected to the one GPIO on ESP.
3. The input GPIO's are always pullup to the power supply.
## Attention
For correct operation, please enable the following settings in the menuconfig:
```text
GPIO_CTRL_FUNC_IN_IRAM
```
## Dependencies
1. [zh_vector](http://git.zh.com.ru/alexey.zholtikov/zh_vector)
1. [zh_vector](http://git.zh.com.ru/esp_components/zh_vector)
## Using
@@ -27,8 +38,8 @@ In an existing project, run the following command to install the components:
```text
cd ../your_project/components
git clone http://git.zh.com.ru/alexey.zholtikov/zh_pcf8574
git clone http://git.zh.com.ru/alexey.zholtikov/zh_vector
git clone http://git.zh.com.ru/esp_components/zh_pcf8574
git clone http://git.zh.com.ru/esp_components/zh_vector
```
In the application, add the component:
@@ -46,13 +57,10 @@ One expander on bus. All GPIO's as output (except P0 - input). Interrupt is enab
#define I2C_PORT (I2C_NUM_MAX - 1)
#ifndef CONFIG_IDF_TARGET_ESP8266
i2c_master_bus_handle_t i2c_bus_handle = NULL;
#endif
zh_pcf8574_handle_t pcf8574_handle = {0};
void zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); // Required only if used input GPIO interrupts.
void zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
void print_gpio_status(const char *message, uint8_t reg)
{
@@ -67,54 +75,34 @@ void print_gpio_status(const char *message, uint8_t reg)
void app_main(void)
{
esp_log_level_set("zh_pcf8574", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
esp_log_level_set("zh_vector", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_4, // In accordance with used chip.
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = GPIO_NUM_5, // In accordance with used chip.
.scl_pullup_en = GPIO_PULLUP_ENABLE,
};
i2c_driver_install(I2C_PORT, i2c_config.mode);
i2c_param_config(I2C_PORT, &i2c_config);
#else
esp_log_level_set("zh_pcf8574", ESP_LOG_ERROR);
esp_log_level_set("zh_vector", ESP_LOG_ERROR);
i2c_master_bus_config_t i2c_bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_PORT,
.scl_io_num = GPIO_NUM_22, // In accordance with used chip.
.sda_io_num = GPIO_NUM_21, // In accordance with used chip.
.scl_io_num = GPIO_NUM_22,
.sda_io_num = GPIO_NUM_21,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
#endif
esp_event_loop_create_default(); // Required only if used input GPIO interrupts.
#ifdef CONFIG_IDF_TARGET_ESP8266
esp_event_handler_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL); // Required only if used input GPIO interrupts.
#else
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL); // Required only if used input GPIO interrupts.
#endif
esp_event_loop_create_default();
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL);
zh_pcf8574_init_config_t pcf8574_init_config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
#ifdef CONFIG_IDF_TARGET_ESP8266
pcf8574_init_config.i2c_port = I2C_PORT;
#else
pcf8574_init_config.i2c_handle = i2c_bus_handle;
#endif
pcf8574_init_config.i2c_address = 0x38;
pcf8574_init_config.p0_gpio_work_mode = true; // Required only for input GPIO.
pcf8574_init_config.interrupt_gpio = GPIO_NUM_14; // Required only if used input GPIO interrupts.
pcf8574_init_config.p0_gpio_work_mode = true;
pcf8574_init_config.interrupt_gpio = GPIO_NUM_14;
zh_pcf8574_init(&pcf8574_init_config, &pcf8574_handle);
uint8_t reg = 0;
zh_pcf8574_read(&pcf8574_handle, &reg);
print_gpio_status("GPIO status: ", reg);
printf("Set P7 to 1, P1 to 1 and P0 to 0.\n");
zh_pcf8574_write(&pcf8574_handle, 0b10000010); // GPIO P0 will not be changed because it is operating in input mode.
zh_pcf8574_write(&pcf8574_handle, 0b10000010);
zh_pcf8574_read(&pcf8574_handle, &reg);
print_gpio_status("GPIO status: ", reg);
printf("Sets P0 to 0.\n");
zh_pcf8574_write_gpio(&pcf8574_handle, 0, false); // GPIO P0 will not be changed because it is operating in input mode.
zh_pcf8574_write_gpio(&pcf8574_handle, 0, false);
bool gpio = 0;
zh_pcf8574_read_gpio(&pcf8574_handle, 0, &gpio);
printf("P0 status: %d.\n", gpio);
@@ -128,9 +116,19 @@ void app_main(void)
zh_pcf8574_reset(&pcf8574_handle);
zh_pcf8574_read(&pcf8574_handle, &reg);
print_gpio_status("GPIO status: ", reg);
for (;;)
{
const zh_pcf8574_stats_t *stats = zh_pcf8574_get_stats();
printf("Number of i2c driver error: %ld.\n", stats->i2c_driver_error);
printf("Number of event post error: %ld.\n", stats->event_post_error);
printf("Number of vector error: %ld.\n", stats->vector_error);
printf("Number of queue overflow error: %ld.\n", stats->queue_overflow_error);
printf("Minimum free stack size: %ld.\n", stats->min_stack_size);
vTaskDelay(60000 / portTICK_PERIOD_MS);
}
}
void zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) // Required only if used input GPIO interrupts.
void zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
zh_pcf8574_event_on_isr_t *event = event_data;
printf("Interrupt happened on device address 0x%02X on GPIO number %d at level %d.\n", event->i2c_address, event->gpio_number, event->gpio_level);

View File

117
include/zh_pcf8574.h Normal file → Executable file
View File

@@ -1,17 +1,20 @@
/**
* @file zh_pcf8574.h
*/
#pragma once
#include "esp_log.h"
#include "driver/gpio.h"
#ifdef CONFIG_IDF_TARGET_ESP8266
#include "driver/i2c.h"
#else
#include "driver/i2c_master.h"
#endif
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_event.h"
#include "zh_vector.h"
/**
* @brief PCF8574 expander initial default values.
*/
#define ZH_PCF8574_INIT_CONFIG_DEFAULT() \
{ \
.task_priority = 10, \
@@ -25,55 +28,72 @@
.p5_gpio_work_mode = 0, \
.p6_gpio_work_mode = 0, \
.p7_gpio_work_mode = 0, \
.interrupt_gpio = GPIO_NUM_MAX, \
.i2c_port = 0}
.interrupt_gpio = GPIO_NUM_MAX}
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct // Structure for initial initialization of PCF8574 expander.
extern TaskHandle_t zh_pcf8574; /*!< Unique task handle. */
/**
* @brief Structure for initial initialization of PCF8574 expander.
*/
typedef struct
{
uint8_t task_priority; // Task priority for the PCF8574 expander isr processing. @note It is not recommended to set a value less than 10.
uint16_t stack_size; // Stack size for task for the PCF8574 expander isr processing processing. @note The minimum size is 2048 bytes.
uint8_t i2c_address; // Expander I2C address.
bool p0_gpio_work_mode; // Expander GPIO PO work mode. True for input, false for output.
bool p1_gpio_work_mode; // Expander GPIO P1 work mode. True for input, false for output.
bool p2_gpio_work_mode; // Expander GPIO P2 work mode. True for input, false for output.
bool p3_gpio_work_mode; // Expander GPIO P3 work mode. True for input, false for output.
bool p4_gpio_work_mode; // Expander GPIO P4 work mode. True for input, false for output.
bool p5_gpio_work_mode; // Expander GPIO P5 work mode. True for input, false for output.
bool p6_gpio_work_mode; // Expander GPIO P6 work mode. True for input, false for output.
bool p7_gpio_work_mode; // Expander GPIO P7 work mode. True for input, false for output.
uint8_t interrupt_gpio; // Interrupt GPIO. @attention Must be same for all PCF8574 expanders.
bool i2c_port; // I2C port. @attention Must be same for all PCF8574 expanders.
#ifndef CONFIG_IDF_TARGET_ESP8266
i2c_master_bus_handle_t i2c_handle; // Unique I2C bus handle. @attention Must be same for all PCF8574 expanders.
#endif
uint8_t task_priority; /*!< Task priority for the PCF8574 expander isr processing. @note It is not recommended to set a value less than 10. */
uint16_t stack_size; /*!< Stack size for task for the PCF8574 expander isr processing processing. @note The minimum size is 2048 bytes. */
uint8_t i2c_address; /*!< Expander I2C address. */
bool p0_gpio_work_mode; /*!< Expander GPIO PO work mode. True for input, false for output. */
bool p1_gpio_work_mode; /*!< Expander GPIO P1 work mode. True for input, false for output. */
bool p2_gpio_work_mode; /*!< Expander GPIO P2 work mode. True for input, false for output. */
bool p3_gpio_work_mode; /*!< Expander GPIO P3 work mode. True for input, false for output. */
bool p4_gpio_work_mode; /*!< Expander GPIO P4 work mode. True for input, false for output. */
bool p5_gpio_work_mode; /*!< Expander GPIO P5 work mode. True for input, false for output. */
bool p6_gpio_work_mode; /*!< Expander GPIO P6 work mode. True for input, false for output. */
bool p7_gpio_work_mode; /*!< Expander GPIO P7 work mode. True for input, false for output. */
uint8_t interrupt_gpio; /*!< Interrupt GPIO. @attention Must be same for all PCF8574 expanders. */
i2c_master_bus_handle_t i2c_handle; /*!< Unique I2C bus handle. @attention Must be same for all PCF8574 expanders. */
} zh_pcf8574_init_config_t;
typedef struct // PCF8574 expander handle.
/**
* @brief PCF8574 expander handle.
*/
typedef struct
{
uint8_t i2c_address; // Expander I2C address.
uint8_t gpio_work_mode; // Expander GPIO's work mode.
uint8_t gpio_status; // Expander GPIO's status.
bool is_initialized; // Expander initialization flag.
bool i2c_port; // I2C port.
#ifndef CONFIG_IDF_TARGET_ESP8266
i2c_master_bus_handle_t i2c_handle; // Unique I2C bus handle.
i2c_master_dev_handle_t dev_handle; // Unique I2C device handle.
#endif
void *system; // System pointer for use in another components.
uint8_t i2c_address; /*!< Expander I2C address. */
uint8_t gpio_work_mode; /*!< Expander GPIO's work mode. */
uint8_t gpio_status; /*!< Expander GPIO's status. */
bool is_initialized; /*!< Expander initialization flag. */
i2c_master_dev_handle_t dev_handle; /*!< Unique I2C device handle. */
void *system; /*!< System pointer for use in another components. */
} zh_pcf8574_handle_t;
/**
* @brief Structure for error statistics storage.
*/
typedef struct
{
uint32_t i2c_driver_error; /*!< Number of i2c driver error. */
uint32_t event_post_error; /*!< Number of event post error. */
uint32_t vector_error; /*!< Number of vector error. */
uint32_t queue_overflow_error; /*!< Number of queue overflow error. */
uint32_t min_stack_size; /*!< Minimum free stack size. */
} zh_pcf8574_stats_t;
ESP_EVENT_DECLARE_BASE(ZH_PCF8574);
typedef struct // Structure for sending data to the event handler when cause an interrupt. @note Should be used with ZH_PCF8574W event base.
/**
* @brief Structure for sending data to the event handler when cause an interrupt.
*
* @note Should be used with ZH_PCF8574 event base.
*/
typedef struct
{
uint8_t i2c_address; // The i2c address of PCF8574 expander that caused the interrupt.
uint8_t gpio_number; // The GPIO that caused the interrupt.
bool gpio_level; // The GPIO level that caused the interrupt.
uint8_t i2c_address; /*!< The i2c address of PCF8574 expander that caused the interrupt. */
uint8_t gpio_number; /*!< The GPIO that caused the interrupt. */
bool gpio_level; /*!< The GPIO level that caused the interrupt. */
} zh_pcf8574_event_on_isr_t;
/**
@@ -92,6 +112,15 @@ extern "C"
*/
esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle);
/**
* @brief Deinitialize PCF8574 expander.
*
* @param[in] handle Pointer to unique PCF8574 handle.
*
* @return ESP_OK if success or an error code otherwise.
*/
esp_err_t zh_pcf8574_deinit(zh_pcf8574_handle_t *handle);
/**
* @brief Read PCF8574 all GPIO's status.
*
@@ -151,6 +180,18 @@ extern "C"
*/
esp_err_t zh_pcf8574_write_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool status);
/**
* @brief Get error statistics.
*
* @return Pointer to the statistics structure.
*/
const zh_pcf8574_stats_t *zh_pcf8574_get_stats(void);
/**
* @brief Reset error statistics.
*/
void zh_pcf8574_reset_stats(void);
#ifdef __cplusplus
}
#endif

View File

@@ -1 +1 @@
1.3.0
2.3.2

402
zh_pcf8574.c Normal file → Executable file
View File

@@ -2,27 +2,32 @@
static const char *TAG = "zh_pcf8574";
#define ZH_PCF8574_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
#define ZH_PCF8574_LOGW(msg, ...) ESP_LOGW(TAG, msg, ##__VA_ARGS__)
#define ZH_PCF8574_LOGE(msg, ...) ESP_LOGE(TAG, msg, ##__VA_ARGS__)
#define ZH_PCF8574_LOGE_ERR(msg, err, ...) ESP_LOGE(TAG, "[%s:%d:%s] " msg, __FILE__, __LINE__, esp_err_to_name(err), ##__VA_ARGS__)
#define ZH_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
#define ZH_LOGE(msg, err, ...) ESP_LOGE(TAG, "[%s:%d:%s] " msg, __FILE__, __LINE__, esp_err_to_name(err), ##__VA_ARGS__)
#define ZH_PCF8574_CHECK(cond, err, msg, ...) \
if (!(cond)) \
{ \
ZH_PCF8574_LOGE_ERR(msg, err); \
return err; \
#define ZH_ERROR_CHECK(cond, err, cleanup, msg, ...) \
if (!(cond)) \
{ \
ZH_LOGE(msg, err, ##__VA_ARGS__); \
cleanup; \
return err; \
}
static uint8_t _interrupt_gpio = GPIO_NUM_MAX;
TaskHandle_t zh_pcf8574 = NULL;
static SemaphoreHandle_t _interrupt_semaphore = NULL;
static uint8_t _gpio_matrix[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
static uint8_t _interrupt_gpio = GPIO_NUM_MAX;
static const uint8_t _gpio_matrix[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
static bool _is_prev_gpio_isr_service = false;
static zh_pcf8574_stats_t _stats = {0};
static zh_vector_t _vector = {0};
static esp_err_t _zh_pcf8574_validate_config(const zh_pcf8574_init_config_t *config);
static esp_err_t _zh_pcf8574_configure_i2c_device(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle);
static esp_err_t _zh_pcf8574_configure_interrupts(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t handle);
static esp_err_t _zh_pcf8574_gpio_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle);
static esp_err_t _zh_pcf8574_i2c_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle);
static esp_err_t _zh_pcf8574_resources_init(const zh_pcf8574_init_config_t *config);
static esp_err_t _zh_pcf8574_task_init(const zh_pcf8574_init_config_t *config);
static void _zh_pcf8574_isr_handler(void *arg);
static void _zh_pcf8574_isr_processing_task(void *pvParameter);
static esp_err_t _zh_pcf8574_read_register(zh_pcf8574_handle_t *handle, uint8_t *reg);
@@ -30,92 +35,222 @@ static esp_err_t _zh_pcf8574_write_register(zh_pcf8574_handle_t *handle, uint8_t
ESP_EVENT_DEFINE_BASE(ZH_PCF8574);
esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle)
esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle) // -V2008
{
ZH_PCF8574_LOGI("PCF8574 initialization started.");
ZH_PCF8574_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "PCF8574 initialization failed. Invalid argument.");
ZH_PCF8574_CHECK(handle->is_initialized == false, ESP_ERR_INVALID_STATE, "PCF8574 initialization failed. PCF8574 is already initialized.");
ZH_LOGI("PCF8574 initialization started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 initialization failed. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "PCF8574 initialization failed. PCF8574 is already initialized.");
esp_err_t err = _zh_pcf8574_validate_config(config);
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 initialization failed. Initial configuration check failed.");
ZH_PCF8574_LOGI("PCF8574 initial configuration check completed successfully.");
err = _zh_pcf8574_configure_i2c_device(config, handle);
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 initialization failed. Failed to add I2C device.");
ZH_PCF8574_LOGI("PCF8574 add I2C device completed successfully.");
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 initialization failed. Initial configuration check failed.");
err = _zh_pcf8574_i2c_init(config, handle);
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 initialization failed. Failed to add I2C device.");
err = _zh_pcf8574_write_register(handle, handle->gpio_work_mode);
if (err != ESP_OK)
{
handle->is_initialized = false;
ZH_PCF8574_LOGE_ERR("PCF8574 initialization failed. Failed GPIO setup.", err);
return err;
}
ZH_PCF8574_LOGI("GPIO setup completed successfully.");
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle), "PCF8574 initialization failed. Failed extender initial GPIO setup.");
if (config->interrupt_gpio < GPIO_NUM_MAX && handle->gpio_work_mode != 0)
{
err = _zh_pcf8574_configure_interrupts(config, *handle);
if (err != ESP_OK)
err = _zh_pcf8574_gpio_init(config, handle);
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle), "PCF8574 initialization failed. Interrupt GPIO initialization failed.");
err = _zh_pcf8574_resources_init(config);
if (_is_prev_gpio_isr_service == true)
{
handle->is_initialized = false;
ZH_PCF8574_LOGE_ERR("PCF8574 initialization failed. Interrupt setup failed.", err);
return err;
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove((gpio_num_t)config->interrupt_gpio);
gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector), "PCF8574 initialization failed. Resources initialization failed.");
}
else
{
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove((gpio_num_t)config->interrupt_gpio);
gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector), "PCF8574 initialization failed. Resources initialization failed.");
}
err = _zh_pcf8574_task_init(config);
if (_is_prev_gpio_isr_service == true)
{
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove((gpio_num_t)config->interrupt_gpio);
gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector); vSemaphoreDelete(_interrupt_semaphore), "PCF8574 initialization failed. Task initialization failed.");
}
else
{
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove((gpio_num_t)config->interrupt_gpio);
gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector); vSemaphoreDelete(_interrupt_semaphore), "PCF8574 initialization failed. Task initialization failed.");
}
ZH_PCF8574_LOGI("Interrupt setup completed successfully.");
}
handle->is_initialized = true;
ZH_PCF8574_LOGI("PCF8574 initialization completed successfully.");
ZH_LOGI("PCF8574 initialization completed successfully.");
return ESP_OK;
}
esp_err_t zh_pcf8574_deinit(zh_pcf8574_handle_t *handle)
{
ZH_LOGI("PCF8574 deinitialization started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 deinitialization failed. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "PCF8574 deinitialization failed. PCF8574 not initialized.");
if (_interrupt_gpio < GPIO_NUM_MAX && handle->gpio_work_mode != 0)
{
for (uint16_t i = 0; i < zh_vector_get_size(&_vector); ++i)
{
zh_pcf8574_handle_t *temp_handle = zh_vector_get_item(&_vector, i);
if (handle->i2c_address == temp_handle->i2c_address)
{
zh_vector_delete_item(&_vector, i);
break;
}
}
if (zh_vector_get_size(&_vector) == 0)
{
gpio_isr_handler_remove((gpio_num_t)_interrupt_gpio);
gpio_reset_pin((gpio_num_t)_interrupt_gpio);
zh_vector_free(&_vector);
vSemaphoreDelete(_interrupt_semaphore);
vTaskDelete(zh_pcf8574);
if (_is_prev_gpio_isr_service == false)
{
gpio_uninstall_isr_service();
}
_interrupt_gpio = GPIO_NUM_MAX;
}
}
i2c_master_bus_rm_device(handle->dev_handle);
handle->is_initialized = false;
ZH_LOGI("PCF8574 deinitialization completed successfully.");
return ESP_OK;
}
esp_err_t zh_pcf8574_read(zh_pcf8574_handle_t *handle, uint8_t *reg)
{
return _zh_pcf8574_read_register(handle, reg);
ZH_LOGI("PCF8574 read register started.");
ZH_ERROR_CHECK(handle != NULL && reg != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 read register failed. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "PCF8574 read register failed. PCF8574 not initialized.");
esp_err_t err = _zh_pcf8574_read_register(handle, reg);
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 read register failed.");
ZH_LOGI("PCF8574 read register completed successfully.");
return ESP_OK;
}
esp_err_t zh_pcf8574_write(zh_pcf8574_handle_t *handle, uint8_t reg)
{
return _zh_pcf8574_write_register(handle, (reg | handle->gpio_work_mode));
ZH_LOGI("PCF8574 write register started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 write register failed. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "PCF8574 write register failed. PCF8574 not initialized.");
esp_err_t err = _zh_pcf8574_write_register(handle, (reg | handle->gpio_work_mode));
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 write register failed.");
ZH_LOGI("PCF8574 write register completed successfully.");
return ESP_OK;
}
esp_err_t zh_pcf8574_reset(zh_pcf8574_handle_t *handle)
{
return _zh_pcf8574_write_register(handle, handle->gpio_work_mode);
}
esp_err_t zh_pcf8574_read_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool *status)
{
ZH_PCF8574_CHECK(gpio <= 7, ESP_FAIL, "Invalid GPIO number.")
uint8_t gpio_temp = _gpio_matrix[gpio];
uint8_t reg_temp = 0;
esp_err_t err = _zh_pcf8574_read_register(handle, &reg_temp);
*status = ((reg_temp & gpio_temp) ? 1 : 0);
return err;
}
esp_err_t zh_pcf8574_write_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool status)
{
ZH_PCF8574_CHECK(gpio <= 7, ESP_FAIL, "Invalid GPIO number.")
uint8_t gpio_temp = _gpio_matrix[gpio];
if (status == true)
{
return _zh_pcf8574_write_register(handle, handle->gpio_status | handle->gpio_work_mode | gpio_temp);
}
return _zh_pcf8574_write_register(handle, (handle->gpio_status ^ gpio_temp) | handle->gpio_work_mode);
}
static esp_err_t _zh_pcf8574_validate_config(const zh_pcf8574_init_config_t *config)
{
ZH_PCF8574_CHECK(config != NULL, ESP_ERR_INVALID_ARG, "Invalid configuration.");
ZH_PCF8574_CHECK((config->i2c_address >= 0x20 && config->i2c_address <= 0x27) || (config->i2c_address >= 0x38 && config->i2c_address <= 0x3F), ESP_ERR_INVALID_ARG, "Invalid I2C address.");
ZH_PCF8574_CHECK(config->task_priority >= 10 && config->stack_size >= 2048, ESP_ERR_INVALID_ARG, "Invalid task settings.");
ZH_PCF8574_CHECK(config->interrupt_gpio >= 0 && config->interrupt_gpio <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, "Invalid GPIO number.");
#ifndef CONFIG_IDF_TARGET_ESP8266
ZH_PCF8574_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, "Invalid I2C handle.");
#endif
ZH_LOGI("PCF8574 reset register started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 reset register failed. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "PCF8574 reset register failed. PCF8574 not initialized.");
esp_err_t err = _zh_pcf8574_write_register(handle, handle->gpio_work_mode);
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 reset register failed.");
ZH_LOGI("PCF8574 reset register completed successfully.");
return ESP_OK;
}
static esp_err_t _zh_pcf8574_configure_i2c_device(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle)
esp_err_t zh_pcf8574_read_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool *status) // -V2008
{
ZH_LOGI("PCF8574 read GPIO started.");
ZH_ERROR_CHECK(handle != NULL && status != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 read GPIO failed. Invalid argument.");
ZH_ERROR_CHECK(gpio <= 7, ESP_FAIL, NULL, "PCF8574 read GPIO failed. Invalid GPIO number.")
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "PCF8574 read GPIO failed. PCF8574 not initialized.");
uint8_t gpio_temp = _gpio_matrix[gpio];
uint8_t reg_temp = 0;
esp_err_t err = _zh_pcf8574_read_register(handle, &reg_temp);
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 read GPIO failed.");
*status = ((reg_temp & gpio_temp) ? 1 : 0);
ZH_LOGI("PCF8574 read GPIO completed successfully.");
return ESP_OK;
}
esp_err_t zh_pcf8574_write_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool status) // -V2008
{
ZH_LOGI("PCF8574 write GPIO started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 write GPIO failed. Invalid argument.");
ZH_ERROR_CHECK(gpio <= 7, ESP_FAIL, NULL, "PCF8574 write GPIO failed. Invalid GPIO number.")
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "PCF8574 write GPIO failed. PCF8574 not initialized.");
uint8_t gpio_temp = _gpio_matrix[gpio];
esp_err_t err = ESP_OK;
if (status == true)
{
err = _zh_pcf8574_write_register(handle, handle->gpio_status | handle->gpio_work_mode | gpio_temp);
}
else
{
err = _zh_pcf8574_write_register(handle, (handle->gpio_status ^ gpio_temp) | handle->gpio_work_mode);
}
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 write GPIO failed.");
ZH_LOGI("PCF8574 write GPIO completed successfully.");
return ESP_OK;
}
const zh_pcf8574_stats_t *zh_pcf8574_get_stats(void)
{
return &_stats;
}
void zh_pcf8574_reset_stats(void)
{
ZH_LOGI("Error statistic reset started.");
_stats.i2c_driver_error = 0;
_stats.event_post_error = 0;
_stats.vector_error = 0;
_stats.queue_overflow_error = 0;
_stats.min_stack_size = 0;
ZH_LOGI("Error statistic reset successfully.");
}
static esp_err_t _zh_pcf8574_validate_config(const zh_pcf8574_init_config_t *config) // -V2008
{
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Initial config is NULL.");
ZH_ERROR_CHECK((config->i2c_address >= 0x20 && config->i2c_address <= 0x27) || (config->i2c_address >= 0x38 && config->i2c_address <= 0x3F), ESP_ERR_INVALID_ARG, NULL, "Invalid I2C address.");
ZH_ERROR_CHECK(config->task_priority >= 10 && config->stack_size >= 2048, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
ZH_ERROR_CHECK(config->interrupt_gpio <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.");
ZH_ERROR_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C handle.");
return ESP_OK;
}
static esp_err_t _zh_pcf8574_gpio_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle) // -V2008
{
if (_interrupt_gpio != GPIO_NUM_MAX)
{
esp_err_t err = zh_vector_push_back(&_vector, handle);
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed add item to vector.")
return ESP_OK;
}
esp_err_t err = zh_vector_init(&_vector, sizeof(zh_pcf8574_handle_t));
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed create vector.")
err = zh_vector_push_back(&_vector, handle);
ZH_ERROR_CHECK(err == ESP_OK, err, zh_vector_free(&_vector), "Failed add item to vector.")
gpio_config_t interrupt_gpio_config = {
.intr_type = GPIO_INTR_NEGEDGE,
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = (1ULL << config->interrupt_gpio),
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLUP_ENABLE,
};
err = gpio_config(&interrupt_gpio_config);
ZH_ERROR_CHECK(err == ESP_OK, err, zh_vector_free(&_vector), "GPIO configuration failed.")
err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, err, gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector), "Failed install isr service.")
if (err == ESP_ERR_INVALID_STATE)
{
_is_prev_gpio_isr_service = true;
}
err = gpio_isr_handler_add((gpio_num_t)config->interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
if (_is_prev_gpio_isr_service == true)
{
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector), "Failed add isr handler.")
}
else
{
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector), "Failed add isr handler.")
}
_interrupt_gpio = config->interrupt_gpio;
return ESP_OK;
}
static esp_err_t _zh_pcf8574_i2c_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle)
{
#ifndef CONFIG_IDF_TARGET_ESP8266
i2c_device_config_t pcf8574_config = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = config->i2c_address,
@@ -123,83 +258,39 @@ static esp_err_t _zh_pcf8574_configure_i2c_device(const zh_pcf8574_init_config_t
};
i2c_master_dev_handle_t _dev_handle = NULL;
esp_err_t err = i2c_master_bus_add_device(config->i2c_handle, &pcf8574_config, &_dev_handle);
if (err != ESP_OK)
{
ZH_PCF8574_LOGE_ERR("Failed to add I2C device.", err);
i2c_master_bus_rm_device(_dev_handle);
handle->dev_handle = NULL;
return err;
}
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed to add I2C device.");
err = i2c_master_probe(config->i2c_handle, config->i2c_address, 1000 / portTICK_PERIOD_MS);
if (err != ESP_OK)
{
ZH_PCF8574_LOGE_ERR("Expander not connected or not responding.", err);
i2c_master_bus_rm_device(_dev_handle);
handle->dev_handle = NULL;
return err;
}
handle->i2c_handle = config->i2c_handle;
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(_dev_handle), "Expander not connected or not responding.");
handle->dev_handle = _dev_handle;
#endif
handle->gpio_work_mode = (config->p7_gpio_work_mode << 7) | (config->p6_gpio_work_mode << 6) | (config->p5_gpio_work_mode << 5) |
(config->p4_gpio_work_mode << 4) | (config->p3_gpio_work_mode << 3) | (config->p2_gpio_work_mode << 2) |
(config->p1_gpio_work_mode << 1) | (config->p0_gpio_work_mode << 0);
handle->gpio_status = handle->gpio_work_mode;
handle->i2c_address = config->i2c_address;
handle->i2c_port = config->i2c_port;
handle->is_initialized = true;
return ESP_OK;
}
static esp_err_t _zh_pcf8574_configure_interrupts(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t handle)
static esp_err_t _zh_pcf8574_resources_init(const zh_pcf8574_init_config_t *config)
{
if (_interrupt_gpio != GPIO_NUM_MAX)
{
esp_err_t err = zh_vector_push_back(&_vector, &handle);
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed add item to vector.")
return ESP_OK;
}
_interrupt_gpio = config->interrupt_gpio;
esp_err_t err = zh_vector_init(&_vector, sizeof(zh_pcf8574_handle_t));
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed create vector.")
err = zh_vector_push_back(&_vector, &handle);
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed add item to vector.")
gpio_config_t isr_pin_config = {
.intr_type = GPIO_INTR_NEGEDGE,
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = (1ULL << _interrupt_gpio),
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLUP_ENABLE,
};
err = gpio_config(&isr_pin_config);
ZH_PCF8574_CHECK(err == ESP_OK, err, "GPIO configuration failed.")
err = gpio_install_isr_service(0);
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed install isr service.")
err = gpio_isr_handler_add(_interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed add isr handler.")
_interrupt_semaphore = xSemaphoreCreateBinary();
ZH_PCF8574_CHECK(_interrupt_semaphore != NULL, ESP_ERR_NO_MEM, "Failed to create semaphore.")
BaseType_t x_err = xTaskCreatePinnedToCore(
&_zh_pcf8574_isr_processing_task,
"_zh_pcf8574_isr_processing_task",
config->stack_size,
NULL,
config->task_priority,
NULL,
tskNO_AFFINITY);
if (x_err != pdPASS)
{
ZH_PCF8574_LOGE("Failed to create isr processing task.");
vSemaphoreDelete(_interrupt_semaphore);
return ESP_FAIL;
}
ZH_ERROR_CHECK(_interrupt_semaphore != NULL, ESP_ERR_NO_MEM, NULL, "Failed to create semaphore.")
return ESP_OK;
}
static esp_err_t _zh_pcf8574_task_init(const zh_pcf8574_init_config_t *config)
{
BaseType_t err = xTaskCreatePinnedToCore(&_zh_pcf8574_isr_processing_task, "zh_pcf8574_isr_processing_task", config->stack_size, NULL, config->task_priority, &zh_pcf8574, tskNO_AFFINITY);
ZH_ERROR_CHECK(err == pdPASS, err, NULL, "Failed to create isr processing task.")
return ESP_OK;
}
static void IRAM_ATTR _zh_pcf8574_isr_handler(void *arg)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(_interrupt_semaphore, &xHigherPriorityTaskWoken);
if (xSemaphoreGiveFromISR(_interrupt_semaphore, &xHigherPriorityTaskWoken) != pdTRUE)
{
++_stats.queue_overflow_error;
}
if (xHigherPriorityTaskWoken == pdTRUE)
{
portYIELD_FROM_ISR();
@@ -211,13 +302,13 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
for (;;)
{
xSemaphoreTake(_interrupt_semaphore, portMAX_DELAY);
ZH_PCF8574_LOGI("PCF8574 isr processing begin.");
for (uint16_t i = 0; i < zh_vector_get_size(&_vector); ++i)
{
zh_pcf8574_handle_t *handle = zh_vector_get_item(&_vector, i);
if (handle == NULL)
{
ZH_PCF8574_LOGE("PCF8574 isr processing failed. Failed to get vector item data.");
++_stats.vector_error;
ZH_LOGE("PCF8574 isr processing failed. Failed to get vector item data.", ESP_FAIL);
continue;
}
zh_pcf8574_event_on_isr_t event = {0};
@@ -228,7 +319,7 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
esp_err_t err = _zh_pcf8574_read_register(handle, &new_reg);
if (err != ESP_OK)
{
ZH_PCF8574_LOGE_ERR("PCF8574 isr processing failed. Failed to read expander register.", err);
ZH_LOGE("PCF8574 isr processing failed. Failed to read expander register.", err);
continue;
}
for (uint8_t j = 0; j <= 7; ++j)
@@ -240,63 +331,36 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
event.gpio_number = j;
event.gpio_level = new_reg & _gpio_matrix[j];
}
break;
}
}
if (event.gpio_number != 0xFF)
{
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), portTICK_PERIOD_MS);
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), 1000 / portTICK_PERIOD_MS);
if (err != ESP_OK)
{
ZH_PCF8574_LOGE_ERR("PCF8574 isr processing failed. Failed to post interrupt event.", err);
++_stats.event_post_error;
ZH_LOGE("PCF8574 isr processing failed. Failed to post interrupt event.", err);
continue;
}
}
}
ZH_PCF8574_LOGI("PCF8574 isr processing completed successfully.");
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
}
vTaskDelete(NULL);
}
static esp_err_t _zh_pcf8574_read_register(zh_pcf8574_handle_t *handle, uint8_t *reg)
{
ZH_PCF8574_LOGI("PCF8574 read started.");
ZH_PCF8574_CHECK(handle != NULL || reg != NULL, ESP_ERR_INVALID_ARG, "PCF8574 read failed. Invalid argument.");
ZH_PCF8574_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, "PCF8574 read failed. PCF8574 not initialized.");
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, handle->i2c_address << 1 | I2C_MASTER_READ, true);
i2c_master_read_byte(i2c_cmd_handle, &handle->gpio_status, I2C_MASTER_NACK);
i2c_master_stop(i2c_cmd_handle);
esp_err_t err = i2c_master_cmd_begin(handle->i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle);
#else
esp_err_t err = i2c_master_receive(handle->dev_handle, &handle->gpio_status, sizeof(handle->gpio_status), 1000 / portTICK_PERIOD_MS);
#endif
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 read failed. I2C driver error.");
ZH_ERROR_CHECK(err == ESP_OK, err, ++_stats.i2c_driver_error, "I2C driver error.");
*reg = handle->gpio_status;
ZH_PCF8574_LOGI("PCF8574 read completed successfully.");
return ESP_OK;
}
static esp_err_t _zh_pcf8574_write_register(zh_pcf8574_handle_t *handle, uint8_t reg)
{
ZH_PCF8574_LOGI("PCF8574 write started.");
ZH_PCF8574_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "PCF8574 write failed. Invalid argument.");
ZH_PCF8574_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, "PCF8574 write failed. PCF8574 not initialized.");
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
i2c_master_start(i2c_cmd_handle);
i2c_master_write_byte(i2c_cmd_handle, handle->i2c_address << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(i2c_cmd_handle, reg, true);
i2c_master_stop(i2c_cmd_handle);
esp_err_t err = i2c_master_cmd_begin(handle->i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(i2c_cmd_handle);
#else
esp_err_t err = i2c_master_transmit(handle->dev_handle, &reg, sizeof(reg), 1000 / portTICK_PERIOD_MS);
#endif
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 write failed. I2C driver error.");
ZH_ERROR_CHECK(err == ESP_OK, err, ++_stats.i2c_driver_error, "I2C driver error.");
handle->gpio_status = reg;
ZH_PCF8574_LOGI("PCF8574 write completed successfully.");
return ESP_OK;
}