Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de9ece9c0d | |||
| b73ce0fee2 | |||
| 046dc48d0a | |||
| 80fc61936c | |||
| a0e7add434 | |||
| e03c88dc5d | |||
| d76eec1753 | |||
| f8aef52a9c | |||
| 277ffa2c70 | |||
| 364622ff58 | |||
| d2b5dd7120 | |||
| b1c38b4a96 |
BIN
PCF8574(A) datasheet.pdf
Normal file
BIN
PCF8574(A) datasheet.pdf
Normal file
Binary file not shown.
36
README.md
36
README.md
@@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
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.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
|
## Features
|
||||||
|
|
||||||
1. Support of 16 expanders on one bus.
|
1. Support of 16 expanders on one bus.
|
||||||
@@ -56,7 +60,7 @@ One expander on bus. All GPIO's as output (except P0 - input). Interrupt is enab
|
|||||||
i2c_master_bus_handle_t i2c_bus_handle = NULL;
|
i2c_master_bus_handle_t i2c_bus_handle = NULL;
|
||||||
zh_pcf8574_handle_t pcf8574_handle = {0};
|
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)
|
void print_gpio_status(const char *message, uint8_t reg)
|
||||||
{
|
{
|
||||||
@@ -82,29 +86,29 @@ void app_main(void)
|
|||||||
.flags.enable_internal_pullup = true,
|
.flags.enable_internal_pullup = true,
|
||||||
};
|
};
|
||||||
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
||||||
esp_event_loop_create_default(); // Required only if used input GPIO interrupts.
|
esp_event_loop_create_default();
|
||||||
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL); // Required only if used input GPIO interrupts.
|
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL);
|
||||||
zh_pcf8574_init_config_t config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
|
zh_pcf8574_init_config_t config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
|
||||||
config.i2c_handle = i2c_bus_handle;
|
config.i2c_handle = i2c_bus_handle;
|
||||||
config.i2c_address = 0x38;
|
config.i2c_address = 0x38;
|
||||||
config.p0_gpio_work_mode = true; // Required only for input GPIO.
|
config.p0_gpio_work_mode = ZH_PCF8574_GPIO_INPUT;
|
||||||
config.interrupt_gpio = GPIO_NUM_14; // Required only if used input GPIO interrupts.
|
config.interrupt_gpio = GPIO_NUM_14;
|
||||||
zh_pcf8574_init(&config, &pcf8574_handle);
|
zh_pcf8574_init(&config, &pcf8574_handle);
|
||||||
uint8_t reg = 0;
|
uint8_t reg = 0;
|
||||||
zh_pcf8574_read(&pcf8574_handle, ®);
|
zh_pcf8574_read(&pcf8574_handle, ®);
|
||||||
print_gpio_status("GPIO status: ", reg);
|
print_gpio_status("GPIO status: ", reg);
|
||||||
printf("Set P7 to 1, P1 to 1 and P0 to 0.\n");
|
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, ®);
|
zh_pcf8574_read(&pcf8574_handle, ®);
|
||||||
print_gpio_status("GPIO status: ", reg);
|
print_gpio_status("GPIO status: ", reg);
|
||||||
printf("Sets P0 to 0.\n");
|
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, ZH_PCF8574_GPIO_NUM_P0, ZH_PCF8574_GPIO_LOW);
|
||||||
bool gpio = 0;
|
bool gpio = 0;
|
||||||
zh_pcf8574_read_gpio(&pcf8574_handle, 0, &gpio);
|
zh_pcf8574_read_gpio(&pcf8574_handle, ZH_PCF8574_GPIO_NUM_P0, &gpio);
|
||||||
printf("P0 status: %d.\n", gpio);
|
printf("P0 status: %d.\n", gpio);
|
||||||
printf("Set P1 to 0.\n");
|
printf("Set P1 to 0.\n");
|
||||||
zh_pcf8574_write_gpio(&pcf8574_handle, 1, false);
|
zh_pcf8574_write_gpio(&pcf8574_handle, ZH_PCF8574_GPIO_NUM_P1, ZH_PCF8574_GPIO_LOW);
|
||||||
zh_pcf8574_read_gpio(&pcf8574_handle, 1, &gpio);
|
zh_pcf8574_read_gpio(&pcf8574_handle, ZH_PCF8574_GPIO_NUM_P1, &gpio);
|
||||||
printf("P1 status: %d.\n", gpio);
|
printf("P1 status: %d.\n", gpio);
|
||||||
zh_pcf8574_read(&pcf8574_handle, ®);
|
zh_pcf8574_read(&pcf8574_handle, ®);
|
||||||
print_gpio_status("GPIO status: ", reg);
|
print_gpio_status("GPIO status: ", reg);
|
||||||
@@ -112,9 +116,19 @@ void app_main(void)
|
|||||||
zh_pcf8574_reset(&pcf8574_handle);
|
zh_pcf8574_reset(&pcf8574_handle);
|
||||||
zh_pcf8574_read(&pcf8574_handle, ®);
|
zh_pcf8574_read(&pcf8574_handle, ®);
|
||||||
print_gpio_status("GPIO status: ", 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;
|
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);
|
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);
|
||||||
|
|||||||
@@ -12,22 +12,27 @@
|
|||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "zh_vector.h"
|
#include "zh_vector.h"
|
||||||
|
|
||||||
|
#define ZH_PCF8574_GPIO_OUTPUT false
|
||||||
|
#define ZH_PCF8574_GPIO_INPUT true
|
||||||
|
#define ZH_PCF8574_GPIO_LOW false
|
||||||
|
#define ZH_PCF8574_GPIO_HIGH true
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief PCF8574 expander initial default values.
|
* @brief PCF8574 expander initial default values.
|
||||||
*/
|
*/
|
||||||
#define ZH_PCF8574_INIT_CONFIG_DEFAULT() \
|
#define ZH_PCF8574_INIT_CONFIG_DEFAULT() \
|
||||||
{ \
|
{ \
|
||||||
.task_priority = 10, \
|
.task_priority = 1, \
|
||||||
.stack_size = 2048, \
|
.stack_size = configMINIMAL_STACK_SIZE, \
|
||||||
.i2c_address = 0xFF, \
|
.i2c_address = 0xFF, \
|
||||||
.p0_gpio_work_mode = 0, \
|
.p0_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.p1_gpio_work_mode = 0, \
|
.p1_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.p2_gpio_work_mode = 0, \
|
.p2_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.p3_gpio_work_mode = 0, \
|
.p3_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.p4_gpio_work_mode = 0, \
|
.p4_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.p5_gpio_work_mode = 0, \
|
.p5_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.p6_gpio_work_mode = 0, \
|
.p6_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.p7_gpio_work_mode = 0, \
|
.p7_gpio_work_mode = ZH_PCF8574_GPIO_OUTPUT, \
|
||||||
.interrupt_gpio = GPIO_NUM_MAX}
|
.interrupt_gpio = GPIO_NUM_MAX}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@@ -37,22 +42,38 @@ extern "C"
|
|||||||
|
|
||||||
extern TaskHandle_t zh_pcf8574; /*!< Unique task handle. */
|
extern TaskHandle_t zh_pcf8574; /*!< Unique task handle. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Enumeration of PCF8574 expander GPIO.
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
ZH_PCF8574_GPIO_NUM_P0 = 0,
|
||||||
|
ZH_PCF8574_GPIO_NUM_P1,
|
||||||
|
ZH_PCF8574_GPIO_NUM_P2,
|
||||||
|
ZH_PCF8574_GPIO_NUM_P3,
|
||||||
|
ZH_PCF8574_GPIO_NUM_P4,
|
||||||
|
ZH_PCF8574_GPIO_NUM_P5,
|
||||||
|
ZH_PCF8574_GPIO_NUM_P6,
|
||||||
|
ZH_PCF8574_GPIO_NUM_P7,
|
||||||
|
ZH_PCF8574_GPIO_NUM_MAX
|
||||||
|
} zh_pcf8574_gpio_num_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Structure for initial initialization of PCF8574 expander.
|
* @brief Structure for initial initialization of PCF8574 expander.
|
||||||
*/
|
*/
|
||||||
typedef struct
|
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. */
|
uint8_t task_priority; /*!< Task priority for the PCF8574 expander isr processing. @note Minimum value is 1. */
|
||||||
uint16_t stack_size; /*!< Stack size for task for the PCF8574 expander isr processing processing. @note The minimum size is 2048 bytes. */
|
uint16_t stack_size; /*!< Stack size for task for the PCF8574 expander isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
|
||||||
uint8_t i2c_address; /*!< Expander I2C address. */
|
uint8_t i2c_address; /*!< Expander I2C address. */
|
||||||
bool p0_gpio_work_mode; /*!< Expander GPIO PO work mode. True for input, false for output. */
|
bool p0_gpio_work_mode; /*!< Expander GPIO PO work mode. */
|
||||||
bool p1_gpio_work_mode; /*!< Expander GPIO P1 work mode. True for input, false for output. */
|
bool p1_gpio_work_mode; /*!< Expander GPIO P1 work mode. */
|
||||||
bool p2_gpio_work_mode; /*!< Expander GPIO P2 work mode. True for input, false for output. */
|
bool p2_gpio_work_mode; /*!< Expander GPIO P2 work mode. */
|
||||||
bool p3_gpio_work_mode; /*!< Expander GPIO P3 work mode. True for input, false for output. */
|
bool p3_gpio_work_mode; /*!< Expander GPIO P3 work mode. */
|
||||||
bool p4_gpio_work_mode; /*!< Expander GPIO P4 work mode. True for input, false for output. */
|
bool p4_gpio_work_mode; /*!< Expander GPIO P4 work mode. */
|
||||||
bool p5_gpio_work_mode; /*!< Expander GPIO P5 work mode. True for input, false for output. */
|
bool p5_gpio_work_mode; /*!< Expander GPIO P5 work mode. */
|
||||||
bool p6_gpio_work_mode; /*!< Expander GPIO P6 work mode. True for input, false for output. */
|
bool p6_gpio_work_mode; /*!< Expander GPIO P6 work mode. */
|
||||||
bool p7_gpio_work_mode; /*!< Expander GPIO P7 work mode. True for input, false for output. */
|
bool p7_gpio_work_mode; /*!< Expander GPIO P7 work mode. */
|
||||||
uint8_t interrupt_gpio; /*!< Interrupt GPIO. @attention Must be same for all PCF8574 expanders. */
|
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. */
|
i2c_master_bus_handle_t i2c_handle; /*!< Unique I2C bus handle. @attention Must be same for all PCF8574 expanders. */
|
||||||
} zh_pcf8574_init_config_t;
|
} zh_pcf8574_init_config_t;
|
||||||
@@ -70,6 +91,18 @@ extern "C"
|
|||||||
void *system; /*!< System pointer for use in another components. */
|
void *system; /*!< System pointer for use in another components. */
|
||||||
} zh_pcf8574_handle_t;
|
} 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);
|
ESP_EVENT_DECLARE_BASE(ZH_PCF8574);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -100,6 +133,15 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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.
|
* @brief Read PCF8574 all GPIO's status.
|
||||||
*
|
*
|
||||||
@@ -144,7 +186,7 @@ extern "C"
|
|||||||
*
|
*
|
||||||
* @return ESP_OK if success or an error code otherwise.
|
* @return ESP_OK if success or an error code otherwise.
|
||||||
*/
|
*/
|
||||||
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, zh_pcf8574_gpio_num_t gpio, bool *status);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set PCF8574 GPIO status.
|
* @brief Set PCF8574 GPIO status.
|
||||||
@@ -157,7 +199,19 @@ extern "C"
|
|||||||
*
|
*
|
||||||
* @return ESP_OK if success or an error code otherwise.
|
* @return ESP_OK if success or an error code otherwise.
|
||||||
*/
|
*/
|
||||||
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, zh_pcf8574_gpio_num_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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
2.1.0
|
2.4.0
|
||||||
128
zh_pcf8574.c
128
zh_pcf8574.c
@@ -17,8 +17,10 @@ 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 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 const uint8_t _gpio_matrix[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
||||||
static bool _is_prev_gpio_isr_service = false;
|
static bool _is_prev_gpio_isr_service = false;
|
||||||
|
static zh_pcf8574_stats_t _stats = {0};
|
||||||
|
|
||||||
static zh_vector_t _vector = {0};
|
static zh_vector_t _vector = {0};
|
||||||
|
|
||||||
@@ -34,7 +36,7 @@ static esp_err_t _zh_pcf8574_write_register(zh_pcf8574_handle_t *handle, uint8_t
|
|||||||
|
|
||||||
ESP_EVENT_DEFINE_BASE(ZH_PCF8574);
|
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_LOGI("PCF8574 initialization started.");
|
ZH_LOGI("PCF8574 initialization started.");
|
||||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 initialization failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "PCF8574 initialization failed. Invalid argument.");
|
||||||
@@ -52,31 +54,83 @@ esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_han
|
|||||||
err = _zh_pcf8574_resources_init(config);
|
err = _zh_pcf8574_resources_init(config);
|
||||||
if (_is_prev_gpio_isr_service == true)
|
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(config->interrupt_gpio);
|
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(config->interrupt_gpio); zh_vector_free(&_vector), "PCF8574 initialization failed. Resources initialization failed.");
|
gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector), "PCF8574 initialization failed. Resources initialization failed.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove(config->interrupt_gpio);
|
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(config->interrupt_gpio); zh_vector_free(&_vector), "PCF8574 initialization failed. Resources initialization failed.");
|
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);
|
err = _zh_pcf8574_task_init(config);
|
||||||
if (_is_prev_gpio_isr_service == true)
|
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(config->interrupt_gpio);
|
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(config->interrupt_gpio); zh_vector_free(&_vector); vSemaphoreDelete(_interrupt_semaphore), "PCF8574 initialization failed. Task initialization failed.");
|
gpio_reset_pin((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector); vSemaphoreDelete(_interrupt_semaphore), "PCF8574 initialization failed. Task initialization failed.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, i2c_master_bus_rm_device(handle->dev_handle); gpio_isr_handler_remove(config->interrupt_gpio);
|
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(config->interrupt_gpio); zh_vector_free(&_vector); vSemaphoreDelete(_interrupt_semaphore), "PCF8574 initialization failed. Task initialization failed.");
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
handle->is_initialized = true;
|
handle->is_initialized = true;
|
||||||
|
for (uint8_t i = 0; i < sizeof(_i2c_matrix); ++i)
|
||||||
|
{
|
||||||
|
if (_i2c_matrix[i] == 0)
|
||||||
|
{
|
||||||
|
_i2c_matrix[i] = handle->i2c_address;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
ZH_LOGI("PCF8574 initialization completed successfully.");
|
ZH_LOGI("PCF8574 initialization completed successfully.");
|
||||||
return ESP_OK;
|
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;
|
||||||
|
for (uint8_t i = 0; i < sizeof(_i2c_matrix); ++i)
|
||||||
|
{
|
||||||
|
if (_i2c_matrix[i] == handle->i2c_address)
|
||||||
|
{
|
||||||
|
_i2c_matrix[i] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ZH_LOGI("PCF8574 deinitialization completed successfully.");
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
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.");
|
||||||
@@ -110,11 +164,11 @@ esp_err_t zh_pcf8574_reset(zh_pcf8574_handle_t *handle)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, zh_pcf8574_gpio_num_t gpio, bool *status) // -V2008
|
||||||
{
|
{
|
||||||
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 >= ZH_PCF8574_GPIO_NUM_P0 && gpio < ZH_PCF8574_GPIO_NUM_MAX, 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];
|
||||||
uint8_t reg_temp = 0;
|
uint8_t reg_temp = 0;
|
||||||
@@ -125,11 +179,11 @@ esp_err_t zh_pcf8574_read_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool *
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, zh_pcf8574_gpio_num_t gpio, bool status) // -V2008
|
||||||
{
|
{
|
||||||
ZH_LOGI("PCF8574 write GPIO started.");
|
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(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(gpio >= ZH_PCF8574_GPIO_NUM_P0 && gpio < ZH_PCF8574_GPIO_NUM_MAX, 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.");
|
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];
|
uint8_t gpio_temp = _gpio_matrix[gpio];
|
||||||
esp_err_t err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
@@ -146,17 +200,37 @@ esp_err_t zh_pcf8574_write_gpio(zh_pcf8574_handle_t *handle, uint8_t gpio, bool
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_pcf8574_validate_config(const zh_pcf8574_init_config_t *config)
|
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 != 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->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->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_0 && config->interrupt_gpio <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.");
|
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.");
|
ZH_ERROR_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C handle.");
|
||||||
|
for (uint8_t i = 0; i < sizeof(_i2c_matrix); ++i)
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(config->i2c_address != _i2c_matrix[i], ESP_ERR_INVALID_ARG, NULL, "I2C address already present.");
|
||||||
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
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_gpio_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle) // -V2008
|
||||||
{
|
{
|
||||||
if (_interrupt_gpio != GPIO_NUM_MAX)
|
if (_interrupt_gpio != GPIO_NUM_MAX)
|
||||||
{
|
{
|
||||||
@@ -178,19 +252,19 @@ 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 == ESP_ERR_INVALID_STATE, 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((gpio_num_t)config->interrupt_gpio); zh_vector_free(&_vector), "Failed install isr service.")
|
||||||
if (err == ESP_ERR_INVALID_STATE)
|
if (err == ESP_ERR_INVALID_STATE)
|
||||||
{
|
{
|
||||||
_is_prev_gpio_isr_service = true;
|
_is_prev_gpio_isr_service = true;
|
||||||
}
|
}
|
||||||
err = gpio_isr_handler_add(config->interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
|
err = gpio_isr_handler_add((gpio_num_t)config->interrupt_gpio, _zh_pcf8574_isr_handler, NULL);
|
||||||
if (_is_prev_gpio_isr_service == true)
|
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.")
|
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
|
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.")
|
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;
|
_interrupt_gpio = config->interrupt_gpio;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -234,7 +308,10 @@ static esp_err_t _zh_pcf8574_task_init(const zh_pcf8574_init_config_t *config)
|
|||||||
static void IRAM_ATTR _zh_pcf8574_isr_handler(void *arg)
|
static void IRAM_ATTR _zh_pcf8574_isr_handler(void *arg)
|
||||||
{
|
{
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
xSemaphoreGiveFromISR(_interrupt_semaphore, &xHigherPriorityTaskWoken);
|
if (xSemaphoreGiveFromISR(_interrupt_semaphore, &xHigherPriorityTaskWoken) != pdTRUE)
|
||||||
|
{
|
||||||
|
++_stats.queue_overflow_error;
|
||||||
|
}
|
||||||
if (xHigherPriorityTaskWoken == pdTRUE)
|
if (xHigherPriorityTaskWoken == pdTRUE)
|
||||||
{
|
{
|
||||||
portYIELD_FROM_ISR();
|
portYIELD_FROM_ISR();
|
||||||
@@ -251,6 +328,7 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
|
|||||||
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)
|
||||||
{
|
{
|
||||||
|
++_stats.vector_error;
|
||||||
ZH_LOGE("PCF8574 isr processing failed. Failed to get vector item data.", ESP_FAIL);
|
ZH_LOGE("PCF8574 isr processing failed. Failed to get vector item data.", ESP_FAIL);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -281,11 +359,13 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
|
|||||||
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), 1000 / portTICK_PERIOD_MS);
|
err = esp_event_post(ZH_PCF8574, 0, &event, sizeof(event), 1000 / portTICK_PERIOD_MS);
|
||||||
if (err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
|
++_stats.event_post_error;
|
||||||
ZH_LOGE("PCF8574 isr processing failed. Failed to post interrupt event.", err);
|
ZH_LOGE("PCF8574 isr processing failed. Failed to post interrupt event.", err);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
||||||
}
|
}
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
@@ -293,7 +373,7 @@ static void IRAM_ATTR _zh_pcf8574_isr_processing_task(void *pvParameter)
|
|||||||
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)
|
||||||
{
|
{
|
||||||
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);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "I2C driver error.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, ++_stats.i2c_driver_error, "I2C driver error.");
|
||||||
*reg = handle->gpio_status;
|
*reg = handle->gpio_status;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -301,7 +381,7 @@ static esp_err_t _zh_pcf8574_read_register(zh_pcf8574_handle_t *handle, uint8_t
|
|||||||
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)
|
||||||
{
|
{
|
||||||
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);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "I2C driver error.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, ++_stats.i2c_driver_error, "I2C driver error.");
|
||||||
handle->gpio_status = reg;
|
handle->gpio_status = reg;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user