This commit is contained in:
2025-08-11 16:39:04 +03:00
parent cd67f56533
commit 125c8d8f07
4 changed files with 74 additions and 99 deletions

View File

@@ -1,16 +1,15 @@
#include "zh_avr_pcf8574.h"
// #define GPIO_NUM_MAX 15 // Delete.
static uint8_t _interrupt_gpio = 0; // Check. PD3-PD7 only.
static uint8_t _interrupt_gpio = 0xFF;
static SemaphoreHandle_t _interrupt_semaphore = NULL;
static uint8_t _gpio_matrix[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
static uint8_t _gpio_matrix[8] = {AVR_BIT0, AVR_BIT1, AVR_BIT2, AVR_BIT3, AVR_BIT4, AVR_BIT5, AVR_BIT6, AVR_BIT7};
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);
extern void zh_avr_pcf8574_isr_handler(void);
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);
@@ -82,8 +81,8 @@ static avr_err_t _zh_avr_pcf8574_validate_config(const zh_avr_pcf8574_init_confi
{
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.");
ZH_ERROR_CHECK(config->task_priority > tskIDLE_PRIORITY && config->stack_size >= 124, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(config->interrupt_gpio == 0xFF || config->interrupt_gpio == PORTD4 || config->interrupt_gpio == PORTD5 || config->interrupt_gpio == PORTD6 || config->interrupt_gpio == PORTD7, AVR_ERR_INVALID_ARG);
return AVR_OK;
}
@@ -116,28 +115,13 @@ static avr_err_t _zh_avr_pcf8574_configure_interrupts(const zh_avr_pcf8574_init_
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.")
DDRD &= ~(1 << _interrupt_gpio);
PORTD |= (1 << _interrupt_gpio);
PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << _interrupt_gpio);
_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);
BaseType_t x_err = xTaskCreate(_zh_avr_pcf8574_isr_processing_task, NULL, config->stack_size, NULL, config->task_priority, NULL);
if (x_err != pdPASS)
{
vSemaphoreDelete(_interrupt_semaphore);
@@ -146,14 +130,17 @@ static avr_err_t _zh_avr_pcf8574_configure_interrupts(const zh_avr_pcf8574_init_
return AVR_OK;
}
ISR(PCINT2_vect) // Check.
void zh_avr_pcf8574_isr_handler(void)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(_interrupt_semaphore, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE)
if ((PIND & (1 << _interrupt_gpio)) == 0)
{
portYIELD();
};
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(_interrupt_semaphore, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE)
{
portYIELD();
};
}
}
static void _zh_avr_pcf8574_isr_processing_task(void *pvParameter)
@@ -186,17 +173,11 @@ static void _zh_avr_pcf8574_isr_processing_task(void *pvParameter)
{
event.gpio_number = j;
event.gpio_level = new_reg & _gpio_matrix[j];
extern void zh_avr_pcf8574_event_handler(zh_avr_pcf8574_event_on_isr_t * event);
zh_avr_pcf8574_event_handler(&event);
}
}
}
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);