diff --git a/README.md b/README.md index dd093a8..2c8403f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,139 @@ -# zh_avr_pcf8574 +# FreeRTOS based AVR library for PCF8574(A) 8-bit I/O expander -AVR library for PCF8574(A) 8-bit I/O expander. \ No newline at end of file +## Features + +1. Support of 16 expanders on one bus. +2. Support of output and input GPIO's work mode. +3. Support of interrupts from input GPIO's. + +## Note + +1. Enable interrupt support only if input GPIO's are used. +2. All the INT GPIO's on the extenders must be connected to the one GPIO on AVR. Only PORTD3 - PORTD7 are acceptable! +3. The input GPIO's are always pullup to the power supply. + +## Dependencies + +1. [zh_avr_free_rtos](http://git.zh.com.ru/avr_libraries/zh_avr_free_rtos) +2. [zh_avr_vector](http://git.zh.com.ru/avr_libraries/zh_avr_vector) +3. [zh_avr_common](http://git.zh.com.ru/avr_libraries/zh_avr_common) +4. [zh_avr_i2c](http://git.zh.com.ru/avr_libraries/zh_avr_i2c) + +## Using + +In an existing project, run the following command to install the components: + +```text +cd ../your_project/lib +git clone http://git.zh.com.ru/avr_libraries/zh_avr_free_rtos +git clone http://git.zh.com.ru/avr_libraries/zh_avr_vector +git clone http://git.zh.com.ru/avr_libraries/zh_avr_i2c +git clone http://git.zh.com.ru/avr_libraries/zh_avr_common +git clone http://git.zh.com.ru/avr_libraries/zh_avr_pcf8574 +``` + +In the application, add the component: + +```c +#include "zh_avr_pcf8574.h" +``` + +## Examples + +One expander on bus. All GPIO's as output (except P0 - input). Interrupt is enable: + +```c +#include "zh_avr_pcf8574.h" + +#define I2C_PORT (I2C_NUM_MAX - 1) + +#ifndef CONFIG_IDF_TARGET_ESP8266 +i2c_master_bus_handle_t i2c_bus_handle = NULL; +#endif + +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 print_gpio_status(const char *message, uint8_t reg) +{ + printf("%s", message); + for (uint8_t i = 0; i < 8; ++i) + { + printf("%c", (reg & 0x80) ? '1' : '0'); + reg <<= 1; + } + printf(".\n"); +} + +void app_main(void) +{ + esp_log_level_set("zh_pcf8574", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig. + esp_log_level_set("zh_vector", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig. +#ifdef CONFIG_IDF_TARGET_ESP8266 + i2c_config_t i2c_config = { + .mode = I2C_MODE_MASTER, + .sda_io_num = GPIO_NUM_4, // In accordance with used chip. + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = GPIO_NUM_5, // In accordance with used chip. + .scl_pullup_en = GPIO_PULLUP_ENABLE, + }; + i2c_driver_install(I2C_PORT, i2c_config.mode); + i2c_param_config(I2C_PORT, &i2c_config); +#else + i2c_master_bus_config_t i2c_bus_config = { + .clk_source = I2C_CLK_SRC_DEFAULT, + .i2c_port = I2C_PORT, + .scl_io_num = GPIO_NUM_22, // In accordance with used chip. + .sda_io_num = GPIO_NUM_21, // In accordance with used chip. + .glitch_ignore_cnt = 7, + .flags.enable_internal_pullup = true, + }; + i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle); +#endif + esp_event_loop_create_default(); // Required only if used input GPIO interrupts. +#ifdef CONFIG_IDF_TARGET_ESP8266 + esp_event_handler_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL); // Required only if used input GPIO interrupts. +#else + esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL); // Required only if used input GPIO interrupts. +#endif + zh_pcf8574_init_config_t pcf8574_init_config = ZH_PCF8574_INIT_CONFIG_DEFAULT(); +#ifdef CONFIG_IDF_TARGET_ESP8266 + pcf8574_init_config.i2c_port = I2C_PORT; +#else + pcf8574_init_config.i2c_handle = i2c_bus_handle; +#endif + pcf8574_init_config.i2c_address = 0x38; + pcf8574_init_config.p0_gpio_work_mode = true; // Required only for input GPIO. + pcf8574_init_config.interrupt_gpio = GPIO_NUM_14; // Required only if used input GPIO interrupts. + 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_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. + bool gpio = 0; + zh_pcf8574_read_gpio(&pcf8574_handle, 0, &gpio); + printf("P0 status: %d.\n", gpio); + printf("Set P1 to 0.\n"); + zh_pcf8574_write_gpio(&pcf8574_handle, 1, false); + zh_pcf8574_read_gpio(&pcf8574_handle, 1, &gpio); + printf("P1 status: %d.\n", gpio); + zh_pcf8574_read(&pcf8574_handle, ®); + print_gpio_status("GPIO status: ", reg); + printf("Reset all GPIO.\n"); + zh_pcf8574_reset(&pcf8574_handle); + zh_pcf8574_read(&pcf8574_handle, ®); + print_gpio_status("GPIO status: ", reg); +} + +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. +{ + 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); +} +``` diff --git a/include/zh_avr_pcf8574.h b/include/zh_avr_pcf8574.h new file mode 100644 index 0000000..da44386 --- /dev/null +++ b/include/zh_avr_pcf8574.h @@ -0,0 +1,138 @@ +#pragma once + +#include "FreeRTOS.h" +#include "semphr.h" +#include "zh_avr_i2c.h" +#include "zh_avr_vector.h" +#include "avr_err.h" + +#define ZH_AVR_PCF8574_INIT_CONFIG_DEFAULT() \ + { \ + .task_priority = configMAX_PRIORITIES, \ + .stack_size = 124, \ + .i2c_address = 0xFF, \ + .p0_gpio_work_mode = 0, \ + .p1_gpio_work_mode = 0, \ + .p2_gpio_work_mode = 0, \ + .p3_gpio_work_mode = 0, \ + .p4_gpio_work_mode = 0, \ + .p5_gpio_work_mode = 0, \ + .p6_gpio_work_mode = 0, \ + .p7_gpio_work_mode = 0, \ + .interrupt_gpio = 0xFF} + +#ifdef __cplusplus +extern "C" +{ +#endif + + typedef struct // Structure for initial initialization of PCF8574 expander. + { + uint8_t task_priority; // Task priority for the PCF8574 expander isr processing. @note It is not recommended to set a value less than configMAX_PRIORITIES. + uint8_t stack_size; // Stack size for task for the PCF8574 expander isr processing processing. @note The minimum size is 2048 bytes. + uint8_t i2c_address; // Expander I2C address. + bool p0_gpio_work_mode; // Expander GPIO PO work mode. True for input, false for output. + bool p1_gpio_work_mode; // Expander GPIO P1 work mode. True for input, false for output. + bool p2_gpio_work_mode; // Expander GPIO P2 work mode. True for input, false for output. + bool p3_gpio_work_mode; // Expander GPIO P3 work mode. True for input, false for output. + bool p4_gpio_work_mode; // Expander GPIO P4 work mode. True for input, false for output. + bool p5_gpio_work_mode; // Expander GPIO P5 work mode. True for input, false for output. + bool p6_gpio_work_mode; // Expander GPIO P6 work mode. True for input, false for output. + bool p7_gpio_work_mode; // Expander GPIO P7 work mode. True for input, false for output. + uint8_t interrupt_gpio; // Interrupt GPIO. @attention Must be same for all PCF8574 expanders. + } zh_avr_pcf8574_init_config_t; + + typedef struct // PCF8574 expander handle. + { + uint8_t i2c_address; // Expander I2C address. + uint8_t gpio_work_mode; // Expander GPIO's work mode. + uint8_t gpio_status; // Expander GPIO's status. + bool is_initialized; // Expander initialization flag. + void *system; // System pointer for use in another components. + } zh_avr_pcf8574_handle_t; + + typedef struct // Structure for sending data to the event handler when cause an interrupt. + { + 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_avr_pcf8574_event_on_isr_t; + + /** + * @brief Initialize PCF8574 expander. + * + * @param[in] config Pointer to PCF8574 initialized configuration structure. Can point to a temporary variable. + * @param[out] handle Pointer to unique PCF8574 handle. + * + * @attention I2C driver must be initialized first. + * + * @note Before initialize the expander recommend initialize zh_avr_pcf8574_init_config_t structure with default values. + * + * @code zh_avr_pcf8574_init_config_t config = ZH_AVR_PCF8574_INIT_CONFIG_DEFAULT() @endcode + * + * @return AVR_OK if success or an error code otherwise. + */ + avr_err_t zh_avr_pcf8574_init(const zh_avr_pcf8574_init_config_t *config, zh_avr_pcf8574_handle_t *handle); + + /** + * @brief Read PCF8574 all GPIO's status. + * + * @param[in] handle Pointer to unique PCF8574 handle. + * @param[out] reg Pointer to GPIO's status. + * + * @note For input GPIO's status will be 1 (HIGH) always. + * + * @return AVR_OK if success or an error code otherwise. + */ + avr_err_t zh_avr_pcf8574_read(zh_avr_pcf8574_handle_t *handle, uint8_t *reg); + + /** + * @brief Set PCF8574 all GPIO's status. + * + * @param[in] handle Pointer to unique PCF8574 handle. + * @param[in] reg GPIO's status. + * + * @attention Only the GPIO outputs are affected. + * + * @return AVR_OK if success or an error code otherwise. + */ + avr_err_t zh_avr_pcf8574_write(zh_avr_pcf8574_handle_t *handle, uint8_t reg); + + /** + * @brief Reset (set to initial) PCF8574 all GPIO's. + * + * @param[in] handle Pointer to unique PCF8574 handle. + * + * @return AVR_OK if success or an error code otherwise. + */ + avr_err_t zh_avr_pcf8574_reset(zh_avr_pcf8574_handle_t *handle); + + /** + * @brief Read PCF8574 GPIO status. + * + * @param[in] handle Pointer to unique PCF8574 handle. + * @param[in] gpio GPIO number. + * @param[out] status Pointer to GPIO status (true - HIGH, false - LOW). + * + * @note For input GPIO's status will be 1 (HIGH) always. + * + * @return AVR_OK if success or an error code otherwise. + */ + avr_err_t zh_avr_pcf8574_read_gpio(zh_avr_pcf8574_handle_t *handle, uint8_t gpio, bool *status); + + /** + * @brief Set PCF8574 GPIO status. + * + * @param[in] handle Pointer to unique PCF8574 handle. + * @param[in] gpio GPIO number. + * @param[in] status GPIO status (true - HIGH, false - LOW). + * + * @attention Only the GPIO output is affected. + * + * @return AVR_OK if success or an error code otherwise. + */ + avr_err_t zh_avr_pcf8574_write_gpio(zh_avr_pcf8574_handle_t *handle, uint8_t gpio, bool status); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/zh_avr_pcf8574.c b/zh_avr_pcf8574.c new file mode 100644 index 0000000..f7404d6 --- /dev/null +++ b/zh_avr_pcf8574.c @@ -0,0 +1,223 @@ +#include "zh_avr_pcf8574.h" + +// #define GPIO_NUM_MAX 15 // Delete. + +static uint8_t _interrupt_gpio = 0; // Check. PD3-PD7 only. +static SemaphoreHandle_t _interrupt_semaphore = NULL; +static uint8_t _gpio_matrix[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; + +static zh_avr_vector_t _vector = {0}; + +static avr_err_t _zh_avr_pcf8574_validate_config(const zh_avr_pcf8574_init_config_t *config); +static avr_err_t _zh_avr_pcf8574_configure_i2c_device(const zh_avr_pcf8574_init_config_t *config, zh_avr_pcf8574_handle_t *handle); +static avr_err_t _zh_avr_pcf8574_configure_interrupts(const zh_avr_pcf8574_init_config_t *config, zh_avr_pcf8574_handle_t handle); +static void _zh_avr_pcf8574_isr_processing_task(void *pvParameter); +static avr_err_t _zh_avr_pcf8574_read_register(zh_avr_pcf8574_handle_t *handle, uint8_t *reg); +static avr_err_t _zh_avr_pcf8574_write_register(zh_avr_pcf8574_handle_t *handle, uint8_t reg); + +avr_err_t zh_avr_pcf8574_init(const zh_avr_pcf8574_init_config_t *config, zh_avr_pcf8574_handle_t *handle) +{ + ZH_ERROR_CHECK(handle != NULL, AVR_ERR_INVALID_ARG); + ZH_ERROR_CHECK(handle->is_initialized == false, AVR_ERR_INVALID_STATE); + avr_err_t err = _zh_avr_pcf8574_validate_config(config); + ZH_ERROR_CHECK(err == AVR_OK, err); + err = _zh_avr_pcf8574_configure_i2c_device(config, handle); + ZH_ERROR_CHECK(err == AVR_OK, err); + err = _zh_avr_pcf8574_write_register(handle, handle->gpio_work_mode); + if (err != AVR_OK) + { + handle->is_initialized = false; + return err; + } + if (config->interrupt_gpio != 0xFF && handle->gpio_work_mode != 0) + { + err = _zh_avr_pcf8574_configure_interrupts(config, *handle); + if (err != AVR_OK) + { + handle->is_initialized = false; + return err; + } + } + handle->is_initialized = true; + return AVR_OK; +} + +avr_err_t zh_avr_pcf8574_read(zh_avr_pcf8574_handle_t *handle, uint8_t *reg) +{ + return _zh_avr_pcf8574_read_register(handle, reg); +} + +avr_err_t zh_avr_pcf8574_write(zh_avr_pcf8574_handle_t *handle, uint8_t reg) +{ + return _zh_avr_pcf8574_write_register(handle, (reg | handle->gpio_work_mode)); +} + +avr_err_t zh_avr_pcf8574_reset(zh_avr_pcf8574_handle_t *handle) +{ + return _zh_avr_pcf8574_write_register(handle, handle->gpio_work_mode); +} + +avr_err_t zh_avr_pcf8574_read_gpio(zh_avr_pcf8574_handle_t *handle, uint8_t gpio, bool *status) +{ + ZH_ERROR_CHECK(gpio <= 7, AVR_FAIL); + uint8_t gpio_temp = _gpio_matrix[gpio]; + uint8_t reg_temp = 0; + avr_err_t err = _zh_avr_pcf8574_read_register(handle, ®_temp); + *status = ((reg_temp & gpio_temp) ? 1 : 0); + return err; +} + +avr_err_t zh_avr_pcf8574_write_gpio(zh_avr_pcf8574_handle_t *handle, uint8_t gpio, bool status) +{ + ZH_ERROR_CHECK(gpio <= 7, AVR_FAIL); + uint8_t gpio_temp = _gpio_matrix[gpio]; + if (status == true) + { + return _zh_avr_pcf8574_write_register(handle, handle->gpio_status | handle->gpio_work_mode | gpio_temp); + } + return _zh_avr_pcf8574_write_register(handle, (handle->gpio_status ^ gpio_temp) | handle->gpio_work_mode); +} + +static avr_err_t _zh_avr_pcf8574_validate_config(const zh_avr_pcf8574_init_config_t *config) +{ + ZH_ERROR_CHECK(config != NULL, AVR_ERR_INVALID_ARG); + ZH_ERROR_CHECK((config->i2c_address >= 0x20 && config->i2c_address <= 0x27) || (config->i2c_address >= 0x38 && config->i2c_address <= 0x3F), AVR_ERR_INVALID_ARG); + // ZH_ERROR_CHECK(config->task_priority >= 10 && config->stack_size >= 2048, AVR_ERR_INVALID_ARG, "Invalid task settings."); + // ZH_ERROR_CHECK(config->interrupt_gpio >= 0 && config->interrupt_gpio <= GPIO_NUM_MAX, AVR_ERR_INVALID_ARG, "Invalid GPIO number."); + return AVR_OK; +} + +static avr_err_t _zh_avr_pcf8574_configure_i2c_device(const zh_avr_pcf8574_init_config_t *config, zh_avr_pcf8574_handle_t *handle) +{ + avr_err_t err = zh_avr_i2c_master_probe(config->i2c_address, 1000 / portTICK_PERIOD_MS); + if (err != AVR_OK) + { + return err; + } + handle->gpio_work_mode = (config->p7_gpio_work_mode << 7) | (config->p6_gpio_work_mode << 6) | (config->p5_gpio_work_mode << 5) | + (config->p4_gpio_work_mode << 4) | (config->p3_gpio_work_mode << 3) | (config->p2_gpio_work_mode << 2) | + (config->p1_gpio_work_mode << 1) | (config->p0_gpio_work_mode << 0); + handle->gpio_status = handle->gpio_work_mode; + handle->i2c_address = config->i2c_address; + handle->is_initialized = true; + return AVR_OK; +} + +static avr_err_t _zh_avr_pcf8574_configure_interrupts(const zh_avr_pcf8574_init_config_t *config, zh_avr_pcf8574_handle_t handle) +{ + if (_interrupt_gpio != 0xFF) + { + avr_err_t err = zh_avr_vector_push_back(&_vector, &handle); + ZH_ERROR_CHECK(err == AVR_OK, err) + return AVR_OK; + } + _interrupt_gpio = config->interrupt_gpio; + avr_err_t err = zh_avr_vector_init(&_vector, sizeof(zh_avr_pcf8574_handle_t)); + ZH_ERROR_CHECK(err == AVR_OK, err); + err = zh_avr_vector_push_back(&_vector, &handle); + ZH_ERROR_CHECK(err == AVR_OK, err); + // gpio_config_t isr_pin_config = { + // .intr_type = GPIO_INTR_NEGEDGE, + // .mode = GPIO_MODE_INPUT, + // .pin_bit_mask = (1ULL << _interrupt_gpio), + // .pull_down_en = GPIO_PULLDOWN_DISABLE, + // .pull_up_en = GPIO_PULLUP_ENABLE, + // }; + // err = gpio_config(&isr_pin_config); + // ZH_ERROR_CHECK(err == AVR_OK, err, "GPIO configuration failed.") + // err = gpio_install_isr_service(0); + // ZH_ERROR_CHECK(err == AVR_OK, err, "Failed install isr service.") + // err = gpio_isr_handler_add(_interrupt_gpio, _zh_avr_pcf8574_isr_handler, NULL); + // ZH_ERROR_CHECK(err == AVR_OK, err, "Failed add isr handler.") + _interrupt_semaphore = xSemaphoreCreateBinary(); + ZH_ERROR_CHECK(_interrupt_semaphore != NULL, AVR_ERR_NO_MEM); + BaseType_t x_err = xTaskCreate( + &_zh_avr_pcf8574_isr_processing_task, + "_zh_avr_pcf8574_isr_processing_task", + config->stack_size, + NULL, + config->task_priority, + NULL); + if (x_err != pdPASS) + { + vSemaphoreDelete(_interrupt_semaphore); + return AVR_FAIL; + } + return AVR_OK; +} + +ISR(PCINT2_vect) // Check. +{ + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + xSemaphoreGiveFromISR(_interrupt_semaphore, &xHigherPriorityTaskWoken); + if (xHigherPriorityTaskWoken == pdTRUE) + { + portYIELD(); + }; +} + +static void _zh_avr_pcf8574_isr_processing_task(void *pvParameter) +{ + for (;;) + { + xSemaphoreTake(_interrupt_semaphore, portMAX_DELAY); + for (uint8_t i = 0; i < zh_avr_vector_get_size(&_vector); ++i) + { + zh_avr_pcf8574_handle_t *handle = zh_avr_vector_get_item(&_vector, i); + if (handle == NULL) + { + continue; + } + zh_avr_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; + avr_err_t err = _zh_avr_pcf8574_read_register(handle, &new_reg); + if (err != AVR_OK) + { + continue; + } + for (uint8_t j = 0; j <= 7; ++j) + { + if ((handle->gpio_work_mode & _gpio_matrix[j]) != 0) + { + if ((old_reg & _gpio_matrix[j]) != (new_reg & _gpio_matrix[j])) + { + event.gpio_number = j; + event.gpio_level = new_reg & _gpio_matrix[j]; + } + } + } + if (event.gpio_number != 0xFF) + { + // err = esp_event_post(zh_avr_pcf8574, 0, &event, sizeof(event), portTICK_PERIOD_MS); + // if (err != AVR_OK) + // { + // zh_avr_pcf8574_LOGE_ERR("PCF8574 isr processing failed. Failed to post interrupt event.", err); + // } + } + } + } + vTaskDelete(NULL); +} + +static avr_err_t _zh_avr_pcf8574_read_register(zh_avr_pcf8574_handle_t *handle, uint8_t *reg) +{ + ZH_ERROR_CHECK(handle != NULL || reg != NULL, AVR_ERR_INVALID_ARG); + ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_NOT_FOUND); + avr_err_t err = zh_avr_i2c_master_receive(handle->i2c_address, &handle->gpio_status, sizeof(handle->gpio_status), 1000 / portTICK_PERIOD_MS); + ZH_ERROR_CHECK(err == AVR_OK, err); + *reg = handle->gpio_status; + return AVR_OK; +} + +static avr_err_t _zh_avr_pcf8574_write_register(zh_avr_pcf8574_handle_t *handle, uint8_t reg) +{ + ZH_ERROR_CHECK(handle != NULL, AVR_ERR_INVALID_ARG); + ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_NOT_FOUND); + avr_err_t err = zh_avr_i2c_master_transmit(handle->i2c_address, ®, sizeof(reg), 1000 / portTICK_PERIOD_MS); + ZH_ERROR_CHECK(err == AVR_OK, err); + handle->gpio_status = reg; + return AVR_OK; +} \ No newline at end of file