From 62b0b1fac02d1e225fea15d9a3630ba8304e4733 Mon Sep 17 00:00:00 2001 From: Philippe G Date: Tue, 30 Nov 2021 21:28:52 -0800 Subject: [PATCH] first function pcx9535 version --- components/display/display.c | 4 ++-- components/services/accessors.c | 2 +- components/services/buttons.c | 14 +++++++++----- components/services/gpio_exp.c | 20 +++++++++----------- components/services/gpio_exp.h | 12 ++++++------ 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/components/display/display.c b/components/display/display.c index 6e0fb338..0f30ee5e 100644 --- a/components/display/display.c +++ b/components/display/display.c @@ -92,7 +92,7 @@ void display_init(char *welcome) { if ((p = strcasestr(config, "reset")) != NULL) RST_pin = atoi(strchr(p, '=') + 1); // Detect driver interface - if (strstr(config, "I2C") && i2c_system_port != -1) { + if (strcasestr(config, "I2C") && i2c_system_port != -1) { int address = 0x3C; if ((p = strcasestr(config, "address")) != NULL) address = atoi(strchr(p, '=') + 1); @@ -102,7 +102,7 @@ void display_init(char *welcome) { GDS_I2CAttachDevice( display, width, height, address, RST_pin, backlight_pin ); ESP_LOGI(TAG, "Display is I2C on port %u", address); - } else if (strstr(config, "SPI") && spi_system_host != -1) { + } else if (strcasestr(config, "SPI") && spi_system_host != -1) { int CS_pin = -1, speed = 0; if ((p = strcasestr(config, "cs")) != NULL) CS_pin = atoi(strchr(p, '=') + 1); diff --git a/components/services/accessors.c b/components/services/accessors.c index 91bd2716..e8c194b0 100644 --- a/components/services/accessors.c +++ b/components/services/accessors.c @@ -494,7 +494,7 @@ const gpio_exp_config_t* config_gpio_exp_get(void) { config.phy.port = i2c_system_port; nvs_item = config_alloc_get(NVS_TYPE_STR, "gpio_exp_config"); - if (!nvs_item) return NULL; + if (!nvs_item || !*nvs_item) return NULL; if ((p = strcasestr(nvs_item, "addr")) != NULL) config.phy.addr = atoi(strchr(p, '=') + 1); if ((p = strcasestr(nvs_item, "intr")) != NULL) config.intr = atoi(strchr(p, '=') + 1); diff --git a/components/services/buttons.c b/components/services/buttons.c index 487ab68b..2a9ebb7c 100644 --- a/components/services/buttons.c +++ b/components/services/buttons.c @@ -31,7 +31,7 @@ static const char * TAG = "buttons"; static int n_buttons = 0; #define BUTTON_STACK_SIZE 4096 -#define MAX_BUTTONS 16 +#define MAX_BUTTONS 32 #define DEBOUNCE 50 #define BUTTON_QUEUE_LEN 10 @@ -127,7 +127,7 @@ static BaseType_t IRAM_ATTR gpio_exp_isr_handler(void* arg) static void buttons_exp_timer_handler( TimerHandle_t xTimer ) { struct gpio_exp_s *expander = (struct gpio_exp_s*) pvTimerGetTimerID (xTimer); xQueueSend(button_exp_queue, &expander, 0); - ESP_LOGI(TAG, "Button expander base %u debounced", gpio_exp_base(expander)); + ESP_LOGD(TAG, "Button expander base %u debounced", gpio_exp_get_base(expander)); } /**************************************************************************************** @@ -267,6 +267,8 @@ void dummy_handler(void *id, button_event_e event, button_press_e press) { * Create buttons */ void button_create(void *client, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio) { + struct gpio_exp_s *expander; + if (n_buttons >= MAX_BUTTONS) return; ESP_LOGI(TAG, "Creating button using GPIO %u, type %u, pull-up/down %u, long press %u shifter %d", gpio, type, pull, long_press, shifter_gpio); @@ -306,7 +308,7 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu } // creation is different is this is a native or an expanded GPIO - if (gpio < GPIO_EXP_BASE_MIN) { + if (gpio < GPIO_NUM_MAX) { gpio_pad_select_gpio(gpio); gpio_set_direction(gpio, GPIO_MODE_INPUT); @@ -345,9 +347,9 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]); gpio_intr_enable(gpio); } - } else { + } else if ((expander = gpio_exp_get_expander(gpio)) != NULL) { // set GPIO as an ouptut and acquire value - struct gpio_exp_s *expander = gpio_exp_set_direction(gpio, GPIO_MODE_INPUT, NULL); + gpio_exp_set_direction(gpio, GPIO_MODE_INPUT, expander); buttons[n_buttons].level = gpio_exp_get_level(gpio, 0, expander); // create queue and timer for GPIO expander @@ -357,6 +359,8 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu xQueueAddToSet( button_exp_queue, common_queue_set ); gpio_exp_add_isr(gpio_exp_isr_handler, button_exp_timer, expander); } + } else { + ESP_LOGE(TAG, "Can't create button, GPIO %d does not exist", gpio); } n_buttons++; diff --git a/components/services/gpio_exp.c b/components/services/gpio_exp.c index eb3c6e48..e5950b32 100644 --- a/components/services/gpio_exp.c +++ b/components/services/gpio_exp.c @@ -84,14 +84,14 @@ static EXT_RAM_ATTR struct gpio_exp_s { /****************************************************************************** * Retrieve base from an expander reference */ -uint32_t gpio_exp_base(struct gpio_exp_s *expander) { +uint32_t gpio_exp_get_base(struct gpio_exp_s *expander) { return expander->first; } /****************************************************************************** * Retrieve reference from a GPIO */ -struct gpio_exp_s *gpio_exp_expander(int gpio) { +struct gpio_exp_s *gpio_exp_get_expander(int gpio) { int _gpio = gpio; return find_expander(NULL, &_gpio); } @@ -159,7 +159,7 @@ struct gpio_exp_s* gpio_exp_create(const gpio_exp_config_t *config) { gpio_intr_enable(config->intr); } - ESP_LOGI(TAG, "Create GPIO expander at base %u with INT %u at @%x", config->base, config->intr, config->phy.addr); + ESP_LOGI(TAG, "Create GPIO expander at base %u with INT %u at @%x on port %d", config->base, config->intr, config->phy.addr, config->phy.port); return expander; } @@ -187,10 +187,9 @@ bool gpio_exp_add_isr(gpio_exp_isr isr, void *arg, struct gpio_exp_s *expander) /****************************************************************************** * Set GPIO direction */ -struct gpio_exp_s* gpio_exp_set_direction(int gpio, gpio_mode_t mode, struct gpio_exp_s *expander) { - if ((expander = find_expander(expander, &gpio)) == NULL) return NULL; +esp_err_t gpio_exp_set_direction(int gpio, gpio_mode_t mode, struct gpio_exp_s *expander) { + if ((expander = find_expander(expander, &gpio)) == NULL) return ESP_ERR_INVALID_ARG; -int64_t v = esp_timer_get_time(); xSemaphoreTake(expander->mutex, pdMS_TO_TICKS(portMAX_DELAY)); if (mode == GPIO_MODE_INPUT) { @@ -203,15 +202,14 @@ int64_t v = esp_timer_get_time(); if (expander->r_mask & expander->w_mask) { xSemaphoreGive(expander->mutex); ESP_LOGE(TAG, "GPIO %d on expander base %u can't be r/w", gpio, expander->first); - return false; + return ESP_ERR_INVALID_ARG; } // most expanders want unconfigured GPIO to be set to output if (expander->model->set_direction) expander->model->set_direction(&expander->phy, expander->r_mask, expander->w_mask); xSemaphoreGive(expander->mutex); -ESP_LOGW(TAG, "set took %lld µs", esp_timer_get_time() - v); - return expander; + return ESP_OK; } /****************************************************************************** @@ -311,9 +309,9 @@ esp_err_t gpio_set_pull_mode_u(int gpio, gpio_pull_mode_t mode) { return gpio_exp_set_pull_mode(gpio, mode, NULL); } -esp_err_t gpio_set_direction_u(int gpio, gpio_mode_t mode) { +esp_err_t gpio_set_direction_u(int gpio, gpio_mode_t mode) { if (gpio < GPIO_EXP_BASE_MIN) return gpio_set_direction(gpio, mode); - return gpio_exp_set_direction(gpio, mode, NULL) ? ESP_OK : ESP_ERR_INVALID_ARG; + return gpio_exp_set_direction(gpio, mode, NULL); } int gpio_get_level_u(int gpio) { diff --git a/components/services/gpio_exp.h b/components/services/gpio_exp.h index 1b3cea5c..04404bbf 100644 --- a/components/services/gpio_exp.h +++ b/components/services/gpio_exp.h @@ -37,15 +37,15 @@ typedef BaseType_t (*gpio_exp_isr)(void *arg); // set to -1 and to NULL if there is no interrupt struct gpio_exp_s* gpio_exp_create(const gpio_exp_config_t *config); bool gpio_exp_add_isr(gpio_exp_isr isr, void *arg, struct gpio_exp_s *expander); -uint32_t gpio_exp_base(struct gpio_exp_s *expander); -struct gpio_exp_s* gpio_exp_expander(int gpio); +uint32_t gpio_exp_get_base(struct gpio_exp_s *expander); +struct gpio_exp_s* gpio_exp_get_expander(int gpio); /* For all functions below when is provided, GPIO's can be numbered from 0. If is NULL, then GPIO must start from base */ -struct gpio_exp_s* gpio_exp_set_direction(int gpio, gpio_mode_t mode, struct gpio_exp_s *expander); -esp_err_t gpio_exp_set_pull_mode(int gpio, gpio_pull_mode_t mode, struct gpio_exp_s *expander); -int gpio_exp_get_level(int gpio, uint32_t age, struct gpio_exp_s *expander); -esp_err_t gpio_exp_set_level(int gpio, int level, bool direct, struct gpio_exp_s *expander); +esp_err_t gpio_exp_set_direction(int gpio, gpio_mode_t mode, struct gpio_exp_s *expander); +esp_err_t gpio_exp_set_pull_mode(int gpio, gpio_pull_mode_t mode, struct gpio_exp_s *expander); +int gpio_exp_get_level(int gpio, uint32_t age, struct gpio_exp_s *expander); +esp_err_t gpio_exp_set_level(int gpio, int level, bool direct, struct gpio_exp_s *expander); /* This can be called to enumerate modified GPIO since last read. Note that can be NULL to initialize all GPIOs */