9 Commits

4 changed files with 28 additions and 56 deletions

View File

@@ -2,7 +2,7 @@
## Tested on
1. [ESP32 ESP-IDF v5.5.1](https://docs.espressif.com/projects/esp-idf/en/v5.5.1/esp32/index.html)
1. [ESP32 ESP-IDF v5.5.2](https://docs.espressif.com/projects/esp-idf/en/v5.5.2/esp32/index.html)
## SAST Tools
@@ -26,6 +26,8 @@ For correct operation, please enable the following settings in the menuconfig:
```text
GPIO_CTRL_FUNC_IN_IRAM
I2C_ISR_IRAM_SAFE
I2C_MASTER_ISR_HANDLER_IN_IRAM
```
## Dependencies
@@ -57,7 +59,6 @@ One expander on bus. All GPIO's as output (except P0 - input). Interrupt is enab
#define I2C_PORT (I2C_NUM_MAX - 1)
i2c_master_bus_handle_t i2c_bus_handle = NULL;
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);
@@ -85,6 +86,7 @@ void app_main(void)
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t i2c_bus_handle = NULL;
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
esp_event_loop_create_default();
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL);

View File

@@ -112,9 +112,9 @@ extern "C"
*/
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. */
zh_pcf8574_gpio_num_t gpio_number; /*!< The GPIO that caused the interrupt. */
uint8_t i2c_address; /*!< The i2c address of PCF8574 expander that caused the interrupt. */
bool gpio_level; /*!< The GPIO level that caused the interrupt. */
} zh_pcf8574_event_on_isr_t;
/**

View File

@@ -1 +1 @@
2.4.0
2.5.0

View File

@@ -19,7 +19,6 @@ static SemaphoreHandle_t _interrupt_semaphore = NULL;
static uint8_t _interrupt_gpio = GPIO_NUM_MAX;
static uint8_t _i2c_matrix[16] = {0};
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};
@@ -39,7 +38,7 @@ ESP_EVENT_DEFINE_BASE(ZH_PCF8574);
esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle) // -V2008
{
ZH_LOGI("PCF8574 initialization started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 initialization failed. Invalid argument.");
ZH_ERROR_CHECK(config != NULL && 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_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 initialization failed. Initial configuration check failed.");
@@ -52,27 +51,11 @@ esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_han
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)
{
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.");
}
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.");
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_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.");
}
handle->is_initialized = true;
for (uint8_t i = 0; i < sizeof(_i2c_matrix); ++i)
@@ -110,13 +93,13 @@ esp_err_t zh_pcf8574_deinit(zh_pcf8574_handle_t *handle)
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;
}
}
if (handle->system != NULL)
{
heap_caps_free(handle->system);
}
i2c_master_bus_rm_device(handle->dev_handle);
handle->is_initialized = false;
for (uint8_t i = 0; i < sizeof(_i2c_matrix); ++i)
@@ -218,7 +201,6 @@ void zh_pcf8574_reset_stats(void)
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 >= 1 && config->stack_size >= configMINIMAL_STACK_SIZE, 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.");
@@ -232,14 +214,17 @@ static esp_err_t _zh_pcf8574_validate_config(const zh_pcf8574_init_config_t *con
static esp_err_t _zh_pcf8574_gpio_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle) // -V2008
{
uint8_t reg_temp = 0;
if (_interrupt_gpio != GPIO_NUM_MAX)
{
_zh_pcf8574_read_register(handle, &reg_temp);
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.")
_zh_pcf8574_read_register(handle, &reg_temp);
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 = {
@@ -253,19 +238,8 @@ static esp_err_t _zh_pcf8574_gpio_init(const zh_pcf8574_init_config_t *config, z
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.")
}
ZH_ERROR_CHECK(err == ESP_OK, err, 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;
}
@@ -334,7 +308,6 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
}
zh_pcf8574_event_on_isr_t event = {0};
event.i2c_address = handle->i2c_address;
event.gpio_number = 0xFF;
uint8_t old_reg = handle->gpio_status;
uint8_t new_reg = 0;
esp_err_t err = _zh_pcf8574_read_register(handle, &new_reg);
@@ -351,19 +324,16 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
{
event.gpio_number = j;
event.gpio_level = new_reg & _gpio_matrix[j];
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), 1000 / portTICK_PERIOD_MS);
if (err != ESP_OK)
{
++_stats.event_post_error;
ZH_LOGE("PCF8574 isr processing failed. Failed to post interrupt event.", err);
continue;
}
}
}
}
if (event.gpio_number != 0xFF)
{
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), 1000 / portTICK_PERIOD_MS);
if (err != ESP_OK)
{
++_stats.event_post_error;
ZH_LOGE("PCF8574 isr processing failed. Failed to post interrupt event.", err);
continue;
}
}
}
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
}