perf: updated error checks
This commit is contained in:
128
zh_pcf8574.c
Normal file → Executable file
128
zh_pcf8574.c
Normal file → Executable file
@@ -2,16 +2,15 @@
|
|||||||
|
|
||||||
static const char *TAG = "zh_pcf8574";
|
static const char *TAG = "zh_pcf8574";
|
||||||
|
|
||||||
#define ZH_PCF8574_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
|
#define ZH_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
|
||||||
#define ZH_PCF8574_LOGW(msg, ...) ESP_LOGW(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_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_PCF8574_CHECK(cond, err, msg, ...) \
|
#define ZH_ERROR_CHECK(cond, err, cleanup, msg, ...) \
|
||||||
if (!(cond)) \
|
if (!(cond)) \
|
||||||
{ \
|
{ \
|
||||||
ZH_PCF8574_LOGE_ERR(msg, err); \
|
ZH_LOGE(msg, err, ##__VA_ARGS__); \
|
||||||
return err; \
|
cleanup; \
|
||||||
|
return err; \
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t _interrupt_gpio = GPIO_NUM_MAX;
|
static uint8_t _interrupt_gpio = GPIO_NUM_MAX;
|
||||||
@@ -32,36 +31,22 @@ 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)
|
||||||
{
|
{
|
||||||
ZH_PCF8574_LOGI("PCF8574 initialization started.");
|
ZH_LOGI("PCF8574 initialization started.");
|
||||||
ZH_PCF8574_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "PCF8574 initialization failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 initialization failed. Invalid argument.");
|
||||||
ZH_PCF8574_CHECK(handle->is_initialized == false, ESP_ERR_INVALID_STATE, "PCF8574 initialization failed. PCF8574 is already initialized.");
|
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);
|
esp_err_t err = _zh_pcf8574_validate_config(config);
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 initialization failed. Initial configuration check failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 initialization failed. Initial configuration check failed.");
|
||||||
ZH_PCF8574_LOGI("PCF8574 initial configuration check completed successfully.");
|
|
||||||
err = _zh_pcf8574_configure_i2c_device(config, handle);
|
err = _zh_pcf8574_configure_i2c_device(config, handle);
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 initialization failed. Failed to add I2C device.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 initialization failed. Failed to add I2C device.");
|
||||||
ZH_PCF8574_LOGI("PCF8574 add I2C device completed successfully.");
|
|
||||||
err = _zh_pcf8574_write_register(handle, handle->gpio_work_mode);
|
err = _zh_pcf8574_write_register(handle, handle->gpio_work_mode);
|
||||||
if (err != ESP_OK)
|
ZH_ERROR_CHECK(err == ESP_OK, err, handle->is_initialized = false, "PCF8574 initialization failed. Failed initial GPIO setup.");
|
||||||
{
|
|
||||||
handle->is_initialized = false;
|
|
||||||
ZH_PCF8574_LOGE_ERR("PCF8574 initialization failed. Failed GPIO setup.", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
ZH_PCF8574_LOGI("GPIO setup completed successfully.");
|
|
||||||
if (config->interrupt_gpio < GPIO_NUM_MAX && handle->gpio_work_mode != 0)
|
if (config->interrupt_gpio < GPIO_NUM_MAX && handle->gpio_work_mode != 0)
|
||||||
{
|
{
|
||||||
err = _zh_pcf8574_configure_interrupts(config, *handle);
|
err = _zh_pcf8574_configure_interrupts(config, *handle);
|
||||||
if (err != ESP_OK)
|
ZH_ERROR_CHECK(err == ESP_OK, err, handle->is_initialized = false, "PCF8574 initialization failed. Interrupt setup failed.");
|
||||||
{
|
|
||||||
handle->is_initialized = false;
|
|
||||||
ZH_PCF8574_LOGE_ERR("PCF8574 initialization failed. Interrupt setup failed.", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
ZH_PCF8574_LOGI("Interrupt setup completed successfully.");
|
|
||||||
}
|
}
|
||||||
handle->is_initialized = true;
|
handle->is_initialized = true;
|
||||||
ZH_PCF8574_LOGI("PCF8574 initialization completed successfully.");
|
ZH_LOGI("PCF8574 initialization completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +67,7 @@ esp_err_t zh_pcf8574_reset(zh_pcf8574_handle_t *handle)
|
|||||||
|
|
||||||
esp_err_t zh_pcf8574_read_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool *status)
|
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.")
|
ZH_ERROR_CHECK(gpio <= 7, ESP_FAIL, NULL, "Invalid GPIO number.")
|
||||||
uint8_t gpio_temp = _gpio_matrix[gpio];
|
uint8_t gpio_temp = _gpio_matrix[gpio];
|
||||||
uint8_t reg_temp = 0;
|
uint8_t reg_temp = 0;
|
||||||
esp_err_t err = _zh_pcf8574_read_register(handle, ®_temp);
|
esp_err_t err = _zh_pcf8574_read_register(handle, ®_temp);
|
||||||
@@ -92,7 +77,7 @@ esp_err_t zh_pcf8574_read_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool *
|
|||||||
|
|
||||||
esp_err_t zh_pcf8574_write_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool status)
|
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.")
|
ZH_ERROR_CHECK(gpio <= 7, ESP_FAIL, NULL, "Invalid GPIO number.")
|
||||||
uint8_t gpio_temp = _gpio_matrix[gpio];
|
uint8_t gpio_temp = _gpio_matrix[gpio];
|
||||||
if (status == true)
|
if (status == true)
|
||||||
{
|
{
|
||||||
@@ -103,12 +88,12 @@ esp_err_t zh_pcf8574_write_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool
|
|||||||
|
|
||||||
static esp_err_t _zh_pcf8574_validate_config(const zh_pcf8574_init_config_t *config)
|
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_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "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_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_PCF8574_CHECK(config->task_priority >= 10 && config->stack_size >= 2048, ESP_ERR_INVALID_ARG, "Invalid task settings.");
|
ZH_ERROR_CHECK(config->task_priority >= 10 && config->stack_size >= 2048, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
|
||||||
ZH_PCF8574_CHECK(config->interrupt_gpio >= 0 && config->interrupt_gpio <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, "Invalid GPIO number.");
|
ZH_ERROR_CHECK(config->interrupt_gpio >= 0 && config->interrupt_gpio <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.");
|
||||||
#ifndef CONFIG_IDF_TARGET_ESP8266
|
#ifndef CONFIG_IDF_TARGET_ESP8266
|
||||||
ZH_PCF8574_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, "Invalid I2C handle.");
|
ZH_ERROR_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C handle.");
|
||||||
#endif
|
#endif
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -123,21 +108,9 @@ static esp_err_t _zh_pcf8574_configure_i2c_device(const zh_pcf8574_init_config_t
|
|||||||
};
|
};
|
||||||
i2c_master_dev_handle_t _dev_handle = NULL;
|
i2c_master_dev_handle_t _dev_handle = NULL;
|
||||||
esp_err_t err = i2c_master_bus_add_device(config->i2c_handle, &pcf8574_config, &_dev_handle);
|
esp_err_t err = i2c_master_bus_add_device(config->i2c_handle, &pcf8574_config, &_dev_handle);
|
||||||
if (err != ESP_OK)
|
ZH_ERROR_CHECK(err == ESP_OK, err, handle->dev_handle = NULL;, "Failed to add I2C device.");
|
||||||
{
|
|
||||||
ZH_PCF8574_LOGE_ERR("Failed to add I2C device.", err);
|
|
||||||
i2c_master_bus_rm_device(_dev_handle);
|
|
||||||
handle->dev_handle = NULL;
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
err = i2c_master_probe(config->i2c_handle, config->i2c_address, 1000 / portTICK_PERIOD_MS);
|
err = i2c_master_probe(config->i2c_handle, config->i2c_address, 1000 / portTICK_PERIOD_MS);
|
||||||
if (err != ESP_OK)
|
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(_dev_handle); handle->dev_handle = NULL, "Expander not connected or not responding.");
|
||||||
{
|
|
||||||
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;
|
handle->i2c_handle = config->i2c_handle;
|
||||||
handle->dev_handle = _dev_handle;
|
handle->dev_handle = _dev_handle;
|
||||||
#endif
|
#endif
|
||||||
@@ -156,14 +129,14 @@ static esp_err_t _zh_pcf8574_configure_interrupts(const zh_pcf8574_init_config_t
|
|||||||
if (_interrupt_gpio != GPIO_NUM_MAX)
|
if (_interrupt_gpio != GPIO_NUM_MAX)
|
||||||
{
|
{
|
||||||
esp_err_t err = zh_vector_push_back(&_vector, &handle);
|
esp_err_t err = zh_vector_push_back(&_vector, &handle);
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed add item to vector.")
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed add item to vector.")
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
_interrupt_gpio = config->interrupt_gpio;
|
_interrupt_gpio = config->interrupt_gpio;
|
||||||
esp_err_t err = zh_vector_init(&_vector, sizeof(zh_pcf8574_handle_t));
|
esp_err_t err = zh_vector_init(&_vector, sizeof(zh_pcf8574_handle_t));
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed create vector.")
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed create vector.")
|
||||||
err = zh_vector_push_back(&_vector, &handle);
|
err = zh_vector_push_back(&_vector, &handle);
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed add item to vector.")
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed add item to vector.")
|
||||||
gpio_config_t isr_pin_config = {
|
gpio_config_t isr_pin_config = {
|
||||||
.intr_type = GPIO_INTR_NEGEDGE,
|
.intr_type = GPIO_INTR_NEGEDGE,
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
@@ -172,13 +145,13 @@ static esp_err_t _zh_pcf8574_configure_interrupts(const zh_pcf8574_init_config_t
|
|||||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||||
};
|
};
|
||||||
err = gpio_config(&isr_pin_config);
|
err = gpio_config(&isr_pin_config);
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "GPIO configuration failed.")
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "GPIO configuration failed.")
|
||||||
err = gpio_install_isr_service(0);
|
err = gpio_install_isr_service(0);
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed install isr service.")
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed install isr service.")
|
||||||
err = gpio_isr_handler_add(_interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
|
err = gpio_isr_handler_add(_interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "Failed add isr handler.")
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed add isr handler.")
|
||||||
_interrupt_semaphore = xSemaphoreCreateBinary();
|
_interrupt_semaphore = xSemaphoreCreateBinary();
|
||||||
ZH_PCF8574_CHECK(_interrupt_semaphore != NULL, ESP_ERR_NO_MEM, "Failed to create semaphore.")
|
ZH_ERROR_CHECK(_interrupt_semaphore != NULL, ESP_ERR_NO_MEM, NULL, "Failed to create semaphore.")
|
||||||
BaseType_t x_err = xTaskCreatePinnedToCore(
|
BaseType_t x_err = xTaskCreatePinnedToCore(
|
||||||
&_zh_pcf8574_isr_processing_task,
|
&_zh_pcf8574_isr_processing_task,
|
||||||
"_zh_pcf8574_isr_processing_task",
|
"_zh_pcf8574_isr_processing_task",
|
||||||
@@ -187,12 +160,7 @@ static esp_err_t _zh_pcf8574_configure_interrupts(const zh_pcf8574_init_config_t
|
|||||||
config->task_priority,
|
config->task_priority,
|
||||||
NULL,
|
NULL,
|
||||||
tskNO_AFFINITY);
|
tskNO_AFFINITY);
|
||||||
if (x_err != pdPASS)
|
ZH_ERROR_CHECK(x_err == pdPASS, ESP_FAIL, vSemaphoreDelete(_interrupt_semaphore), "Failed to create isr processing task.")
|
||||||
{
|
|
||||||
ZH_PCF8574_LOGE("Failed to create isr processing task.");
|
|
||||||
vSemaphoreDelete(_interrupt_semaphore);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,13 +179,13 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
|
|||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
xSemaphoreTake(_interrupt_semaphore, portMAX_DELAY);
|
xSemaphoreTake(_interrupt_semaphore, portMAX_DELAY);
|
||||||
ZH_PCF8574_LOGI("PCF8574 isr processing begin.");
|
ZH_LOGI("PCF8574 isr processing begin.");
|
||||||
for (uint16_t i = 0; i < zh_vector_get_size(&_vector); ++i)
|
for (uint16_t i = 0; i < zh_vector_get_size(&_vector); ++i)
|
||||||
{
|
{
|
||||||
zh_pcf8574_handle_t *handle = zh_vector_get_item(&_vector, i);
|
zh_pcf8574_handle_t *handle = zh_vector_get_item(&_vector, i);
|
||||||
if (handle == NULL)
|
if (handle == NULL)
|
||||||
{
|
{
|
||||||
ZH_PCF8574_LOGE("PCF8574 isr processing failed. Failed to get vector item data.");
|
ZH_LOGE("PCF8574 isr processing failed. Failed to get vector item data.", ESP_FAIL);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
zh_pcf8574_event_on_isr_t event = {0};
|
zh_pcf8574_event_on_isr_t event = {0};
|
||||||
@@ -228,7 +196,7 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
|
|||||||
esp_err_t err = _zh_pcf8574_read_register(handle, &new_reg);
|
esp_err_t err = _zh_pcf8574_read_register(handle, &new_reg);
|
||||||
if (err != ESP_OK)
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
for (uint8_t j = 0; j <= 7; ++j)
|
for (uint8_t j = 0; j <= 7; ++j)
|
||||||
@@ -247,20 +215,20 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
|
|||||||
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), portTICK_PERIOD_MS);
|
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), portTICK_PERIOD_MS);
|
||||||
if (err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
ZH_PCF8574_LOGE_ERR("PCF8574 isr processing failed. Failed to post interrupt event.", err);
|
ZH_LOGE("PCF8574 isr processing failed. Failed to post interrupt event.", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ZH_PCF8574_LOGI("PCF8574 isr processing completed successfully.");
|
ZH_LOGI("PCF8574 isr processing completed successfully.");
|
||||||
}
|
}
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_pcf8574_read_register(zh_pcf8574_handle_t *handle, uint8_t *reg)
|
static esp_err_t _zh_pcf8574_read_register(zh_pcf8574_handle_t *handle, uint8_t *reg)
|
||||||
{
|
{
|
||||||
ZH_PCF8574_LOGI("PCF8574 read started.");
|
ZH_LOGI("PCF8574 read started.");
|
||||||
ZH_PCF8574_CHECK(handle != NULL || reg != NULL, ESP_ERR_INVALID_ARG, "PCF8574 read failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL || reg != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 read failed. Invalid argument.");
|
||||||
ZH_PCF8574_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, "PCF8574 read failed. PCF8574 not initialized.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "PCF8574 read failed. PCF8574 not initialized.");
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||||
i2c_master_start(i2c_cmd_handle);
|
i2c_master_start(i2c_cmd_handle);
|
||||||
@@ -272,17 +240,17 @@ static esp_err_t _zh_pcf8574_read_register(zh_pcf8574_handle_t *handle, uint8_t
|
|||||||
#else
|
#else
|
||||||
esp_err_t err = i2c_master_receive(handle->dev_handle, &handle->gpio_status, sizeof(handle->gpio_status), 1000 / portTICK_PERIOD_MS);
|
esp_err_t err = i2c_master_receive(handle->dev_handle, &handle->gpio_status, sizeof(handle->gpio_status), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 read failed. I2C driver error.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 read failed. I2C driver error.");
|
||||||
*reg = handle->gpio_status;
|
*reg = handle->gpio_status;
|
||||||
ZH_PCF8574_LOGI("PCF8574 read completed successfully.");
|
ZH_LOGI("PCF8574 read completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_pcf8574_write_register(zh_pcf8574_handle_t *handle, uint8_t reg)
|
static esp_err_t _zh_pcf8574_write_register(zh_pcf8574_handle_t *handle, uint8_t reg)
|
||||||
{
|
{
|
||||||
ZH_PCF8574_LOGI("PCF8574 write started.");
|
ZH_LOGI("PCF8574 write started.");
|
||||||
ZH_PCF8574_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "PCF8574 write failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 write failed. Invalid argument.");
|
||||||
ZH_PCF8574_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, "PCF8574 write failed. PCF8574 not initialized.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "PCF8574 write failed. PCF8574 not initialized.");
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||||
i2c_master_start(i2c_cmd_handle);
|
i2c_master_start(i2c_cmd_handle);
|
||||||
@@ -294,8 +262,8 @@ static esp_err_t _zh_pcf8574_write_register(zh_pcf8574_handle_t *handle, uint8_t
|
|||||||
#else
|
#else
|
||||||
esp_err_t err = i2c_master_transmit(handle->dev_handle, ®, sizeof(reg), 1000 / portTICK_PERIOD_MS);
|
esp_err_t err = i2c_master_transmit(handle->dev_handle, ®, sizeof(reg), 1000 / portTICK_PERIOD_MS);
|
||||||
#endif
|
#endif
|
||||||
ZH_PCF8574_CHECK(err == ESP_OK, err, "PCF8574 write failed. I2C driver error.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 write failed. I2C driver error.");
|
||||||
handle->gpio_status = reg;
|
handle->gpio_status = reg;
|
||||||
ZH_PCF8574_LOGI("PCF8574 write completed successfully.");
|
ZH_LOGI("PCF8574 write completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user