first function pcx9535 version

This commit is contained in:
Philippe G
2021-11-30 21:28:52 -08:00
parent 4ee36c24f4
commit 62b0b1fac0
5 changed files with 27 additions and 25 deletions

View File

@@ -92,7 +92,7 @@ void display_init(char *welcome) {
if ((p = strcasestr(config, "reset")) != NULL) RST_pin = atoi(strchr(p, '=') + 1); if ((p = strcasestr(config, "reset")) != NULL) RST_pin = atoi(strchr(p, '=') + 1);
// Detect driver interface // Detect driver interface
if (strstr(config, "I2C") && i2c_system_port != -1) { if (strcasestr(config, "I2C") && i2c_system_port != -1) {
int address = 0x3C; int address = 0x3C;
if ((p = strcasestr(config, "address")) != NULL) address = atoi(strchr(p, '=') + 1); 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 ); GDS_I2CAttachDevice( display, width, height, address, RST_pin, backlight_pin );
ESP_LOGI(TAG, "Display is I2C on port %u", address); 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; int CS_pin = -1, speed = 0;
if ((p = strcasestr(config, "cs")) != NULL) CS_pin = atoi(strchr(p, '=') + 1); if ((p = strcasestr(config, "cs")) != NULL) CS_pin = atoi(strchr(p, '=') + 1);

View File

@@ -494,7 +494,7 @@ const gpio_exp_config_t* config_gpio_exp_get(void) {
config.phy.port = i2c_system_port; config.phy.port = i2c_system_port;
nvs_item = config_alloc_get(NVS_TYPE_STR, "gpio_exp_config"); 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, "addr")) != NULL) config.phy.addr = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(nvs_item, "intr")) != NULL) config.intr = atoi(strchr(p, '=') + 1); if ((p = strcasestr(nvs_item, "intr")) != NULL) config.intr = atoi(strchr(p, '=') + 1);

View File

@@ -31,7 +31,7 @@ static const char * TAG = "buttons";
static int n_buttons = 0; static int n_buttons = 0;
#define BUTTON_STACK_SIZE 4096 #define BUTTON_STACK_SIZE 4096
#define MAX_BUTTONS 16 #define MAX_BUTTONS 32
#define DEBOUNCE 50 #define DEBOUNCE 50
#define BUTTON_QUEUE_LEN 10 #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 ) { static void buttons_exp_timer_handler( TimerHandle_t xTimer ) {
struct gpio_exp_s *expander = (struct gpio_exp_s*) pvTimerGetTimerID (xTimer); struct gpio_exp_s *expander = (struct gpio_exp_s*) pvTimerGetTimerID (xTimer);
xQueueSend(button_exp_queue, &expander, 0); 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 * Create buttons
*/ */
void button_create(void *client, int gpio, int type, bool pull, int debounce, button_handler handler, int long_press, int shifter_gpio) { 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; 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); 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 // 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_pad_select_gpio(gpio);
gpio_set_direction(gpio, GPIO_MODE_INPUT); 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_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);
gpio_intr_enable(gpio); gpio_intr_enable(gpio);
} }
} else { } else if ((expander = gpio_exp_get_expander(gpio)) != NULL) {
// set GPIO as an ouptut and acquire value // 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); buttons[n_buttons].level = gpio_exp_get_level(gpio, 0, expander);
// create queue and timer for GPIO 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 ); xQueueAddToSet( button_exp_queue, common_queue_set );
gpio_exp_add_isr(gpio_exp_isr_handler, button_exp_timer, expander); 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++; n_buttons++;

View File

@@ -84,14 +84,14 @@ static EXT_RAM_ATTR struct gpio_exp_s {
/****************************************************************************** /******************************************************************************
* Retrieve base from an expander reference * 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; return expander->first;
} }
/****************************************************************************** /******************************************************************************
* Retrieve reference from a GPIO * 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; int _gpio = gpio;
return find_expander(NULL, &_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); 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; 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 * Set GPIO direction
*/ */
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_direction(int gpio, gpio_mode_t mode, struct gpio_exp_s *expander) {
if ((expander = find_expander(expander, &gpio)) == NULL) return NULL; 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)); xSemaphoreTake(expander->mutex, pdMS_TO_TICKS(portMAX_DELAY));
if (mode == GPIO_MODE_INPUT) { if (mode == GPIO_MODE_INPUT) {
@@ -203,15 +202,14 @@ int64_t v = esp_timer_get_time();
if (expander->r_mask & expander->w_mask) { if (expander->r_mask & expander->w_mask) {
xSemaphoreGive(expander->mutex); xSemaphoreGive(expander->mutex);
ESP_LOGE(TAG, "GPIO %d on expander base %u can't be r/w", gpio, expander->first); 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 // 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); if (expander->model->set_direction) expander->model->set_direction(&expander->phy, expander->r_mask, expander->w_mask);
xSemaphoreGive(expander->mutex); xSemaphoreGive(expander->mutex);
ESP_LOGW(TAG, "set took %lld µs", esp_timer_get_time() - v); return ESP_OK;
return expander;
} }
/****************************************************************************** /******************************************************************************
@@ -313,7 +311,7 @@ esp_err_t gpio_set_pull_mode_u(int gpio, gpio_pull_mode_t mode) {
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); 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) { int gpio_get_level_u(int gpio) {

View File

@@ -37,12 +37,12 @@ typedef BaseType_t (*gpio_exp_isr)(void *arg);
// set <intr> to -1 and <queue> to NULL if there is no interrupt // set <intr> to -1 and <queue> to NULL if there is no interrupt
struct gpio_exp_s* gpio_exp_create(const gpio_exp_config_t *config); 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); 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); uint32_t gpio_exp_get_base(struct gpio_exp_s *expander);
struct gpio_exp_s* gpio_exp_expander(int gpio); struct gpio_exp_s* gpio_exp_get_expander(int gpio);
/* For all functions below when <expander> is provided, GPIO's can be numbered from 0. If <expander> /* For all functions below when <expander> is provided, GPIO's can be numbered from 0. If <expander>
is NULL, then GPIO must start from base */ 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_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); 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); 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_level(int gpio, int level, bool direct, struct gpio_exp_s *expander);