Compare commits
6 Commits
v2.2.0
...
a0e7add434
| Author | SHA1 | Date | |
|---|---|---|---|
| a0e7add434 | |||
| e03c88dc5d | |||
| d76eec1753 | |||
| f8aef52a9c | |||
| 277ffa2c70 | |||
| 364622ff58 |
24
README.md
24
README.md
@@ -60,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;
|
||||
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)
|
||||
{
|
||||
@@ -86,23 +86,23 @@ void app_main(void)
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
||||
esp_event_loop_create_default(); // Required only if used input GPIO interrupts.
|
||||
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL); // Required only if used input GPIO interrupts.
|
||||
zh_pcf8574_init_config_t config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
|
||||
config.i2c_handle = i2c_bus_handle;
|
||||
config.i2c_address = 0x38;
|
||||
config.p0_gpio_work_mode = true; // Required only for input GPIO.
|
||||
config.interrupt_gpio = GPIO_NUM_14; // Required only if used input GPIO interrupts.
|
||||
zh_pcf8574_init(&config, &pcf8574_handle);
|
||||
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();
|
||||
pcf8574_init_config.i2c_handle = i2c_bus_handle;
|
||||
pcf8574_init_config.i2c_address = 0x38;
|
||||
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, ®);
|
||||
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, ®);
|
||||
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,7 +128,7 @@ void app_main(void)
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -112,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.
|
||||
*
|
||||
|
||||
@@ -1 +1 @@
|
||||
2.2.0
|
||||
2.3.2
|
||||
38
zh_pcf8574.c
38
zh_pcf8574.c
@@ -78,6 +78,42 @@ esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_han
|
||||
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)
|
||||
{
|
||||
ZH_LOGI("PCF8574 read register started.");
|
||||
@@ -168,7 +204,7 @@ static esp_err_t _zh_pcf8574_validate_config(const zh_pcf8574_init_config_t *con
|
||||
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_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.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user