wip:
This commit is contained in:
223
zh_avr_pcf8574.c
Normal file
223
zh_avr_pcf8574.c
Normal file
@@ -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;
|
||||
}
|
Reference in New Issue
Block a user