|
|
|
@@ -13,9 +13,12 @@ static const char *TAG = "zh_pcf8574";
|
|
|
|
return err; \
|
|
|
|
return err; \
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint8_t _interrupt_gpio = GPIO_NUM_MAX;
|
|
|
|
TaskHandle_t zh_pcf8574 = NULL;
|
|
|
|
static SemaphoreHandle_t _interrupt_semaphore = NULL;
|
|
|
|
static SemaphoreHandle_t _interrupt_semaphore = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static uint8_t _interrupt_gpio = GPIO_NUM_MAX;
|
|
|
|
static const uint8_t _gpio_matrix[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
|
|
|
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_vector_t _vector = {0};
|
|
|
|
static zh_vector_t _vector = {0};
|
|
|
|
|
|
|
|
|
|
|
|
@@ -47,11 +50,27 @@ esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_han
|
|
|
|
err = _zh_pcf8574_gpio_init(config, handle);
|
|
|
|
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.");
|
|
|
|
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);
|
|
|
|
err = _zh_pcf8574_resources_init(config);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove(config->interrupt_gpio); gpio_uninstall_isr_service();
|
|
|
|
if (_is_prev_gpio_isr_service == true)
|
|
|
|
gpio_reset_pin(config->interrupt_gpio); zh_vector_free(&_vector), "PCF8574 initialization failed. Resources initialization failed.");
|
|
|
|
{
|
|
|
|
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove(config->interrupt_gpio);
|
|
|
|
|
|
|
|
gpio_reset_pin(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(config->interrupt_gpio);
|
|
|
|
|
|
|
|
gpio_uninstall_isr_service(); gpio_reset_pin(config->interrupt_gpio); zh_vector_free(&_vector), "PCF8574 initialization failed. Resources initialization failed.");
|
|
|
|
|
|
|
|
}
|
|
|
|
err = _zh_pcf8574_task_init(config);
|
|
|
|
err = _zh_pcf8574_task_init(config);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove(config->interrupt_gpio); gpio_uninstall_isr_service();
|
|
|
|
if (_is_prev_gpio_isr_service == true)
|
|
|
|
gpio_reset_pin(config->interrupt_gpio); zh_vector_free(&_vector); vSemaphoreDelete(_interrupt_semaphore), "PCF8574 initialization failed. Task initialization failed.");
|
|
|
|
{
|
|
|
|
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove(config->interrupt_gpio);
|
|
|
|
|
|
|
|
gpio_reset_pin(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(config->interrupt_gpio);
|
|
|
|
|
|
|
|
gpio_uninstall_isr_service(); gpio_reset_pin(config->interrupt_gpio); zh_vector_free(&_vector); vSemaphoreDelete(_interrupt_semaphore), "PCF8574 initialization failed. Task initialization failed.");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handle->is_initialized = true;
|
|
|
|
handle->is_initialized = true;
|
|
|
|
ZH_LOGI("PCF8574 initialization completed successfully.");
|
|
|
|
ZH_LOGI("PCF8574 initialization completed successfully.");
|
|
|
|
@@ -61,7 +80,7 @@ esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_han
|
|
|
|
esp_err_t zh_pcf8574_read(zh_pcf8574_handle_t *handle, uint8_t *reg)
|
|
|
|
esp_err_t zh_pcf8574_read(zh_pcf8574_handle_t *handle, uint8_t *reg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ZH_LOGI("PCF8574 read register started.");
|
|
|
|
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 != 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.");
|
|
|
|
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);
|
|
|
|
esp_err_t err = _zh_pcf8574_read_register(handle, reg);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 read register failed.");
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 read register failed.");
|
|
|
|
@@ -94,7 +113,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_LOGI("PCF8574 read GPIO started.");
|
|
|
|
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(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(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.");
|
|
|
|
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 gpio_temp = _gpio_matrix[gpio];
|
|
|
|
@@ -159,9 +178,20 @@ static esp_err_t _zh_pcf8574_gpio_init(const zh_pcf8574_init_config_t *config, z
|
|
|
|
err = gpio_config(&interrupt_gpio_config);
|
|
|
|
err = gpio_config(&interrupt_gpio_config);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, zh_vector_free(&_vector), "GPIO configuration failed.")
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, zh_vector_free(&_vector), "GPIO configuration failed.")
|
|
|
|
err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
|
|
|
err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_reset_pin(config->interrupt_gpio); zh_vector_free(&_vector), "Failed install isr service.")
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, err, gpio_reset_pin(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(config->interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
|
|
|
|
err = gpio_isr_handler_add(config->interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_uninstall_isr_service(); gpio_reset_pin(config->interrupt_gpio); zh_vector_free(&_vector), "Failed add isr handler.")
|
|
|
|
if (_is_prev_gpio_isr_service == true)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_reset_pin(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(config->interrupt_gpio); zh_vector_free(&_vector), "Failed add isr handler.")
|
|
|
|
|
|
|
|
}
|
|
|
|
_interrupt_gpio = config->interrupt_gpio;
|
|
|
|
_interrupt_gpio = config->interrupt_gpio;
|
|
|
|
return ESP_OK;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -175,7 +205,7 @@ static esp_err_t _zh_pcf8574_i2c_init(const zh_pcf8574_init_config_t *config, zh
|
|
|
|
};
|
|
|
|
};
|
|
|
|
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);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL;, "Failed to add I2C device.");
|
|
|
|
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);
|
|
|
|
err = i2c_master_probe(config->i2c_handle, config->i2c_address, 1000 / portTICK_PERIOD_MS);
|
|
|
|
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(_dev_handle), "Expander not connected or not responding.");
|
|
|
|
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;
|
|
|
|
handle->dev_handle = _dev_handle;
|
|
|
|
@@ -196,7 +226,7 @@ static esp_err_t _zh_pcf8574_resources_init(const zh_pcf8574_init_config_t *conf
|
|
|
|
|
|
|
|
|
|
|
|
static esp_err_t _zh_pcf8574_task_init(const zh_pcf8574_init_config_t *config)
|
|
|
|
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, NULL, tskNO_AFFINITY);
|
|
|
|
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.")
|
|
|
|
ZH_ERROR_CHECK(err == pdPASS, err, NULL, "Failed to create isr processing task.")
|
|
|
|
return ESP_OK;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|