From dfebb8ceb65dcc4cdb0694929dd9a80b21776860 Mon Sep 17 00:00:00 2001 From: philippe44 Date: Sat, 1 Feb 2020 01:53:50 -0800 Subject: [PATCH] Add set_GPIO, handle GPIO 36/39 bug --- components/services/battery.c | 14 ++++++++++++++ components/services/buttons.c | 8 +++++++- components/services/globdefs.h | 1 + components/services/monitor.h | 2 ++ components/services/services.c | 22 ++++++++++++---------- components/wifi-manager/wifi_manager.c | 9 ++++++--- main/esp_app_main.c | 4 ++-- 7 files changed, 44 insertions(+), 16 deletions(-) diff --git a/components/services/battery.c b/components/services/battery.c index 0eb78f59..d9a3e681 100644 --- a/components/services/battery.c +++ b/components/services/battery.c @@ -17,6 +17,13 @@ #include "driver/adc.h" #include "battery.h" +/* + There is a bug in esp32 which causes a spurious interrupt on gpio 36/39 when + using ADC, AMP and HALL sensor. Rather than making battery aware, we just ignore + if as the interrupt lasts 80ns and should be debounced (and the ADC read does not + happen very often) +*/ + #define BATTERY_TIMER (10*1000) static const char *TAG = "battery"; @@ -27,6 +34,13 @@ static struct { TimerHandle_t timer; } battery; +/**************************************************************************************** + * + */ + int battery_value_svc(void) { + return battery.avg; + } + /**************************************************************************************** * */ diff --git a/components/services/buttons.c b/components/services/buttons.c index 4b260b98..bf7aeea4 100644 --- a/components/services/buttons.c +++ b/components/services/buttons.c @@ -31,6 +31,9 @@ #include "esp_task.h" #include "driver/gpio.h" #include "buttons.h" +#include "globdefs.h" + +bool gpio36_39_used; static const char * TAG = "buttons"; @@ -208,7 +211,10 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu ESP_LOGW(TAG, "cannot set pull up/down for gpio %u", gpio); } } - + + // nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall which WiFi when PS is activated + if (gpio == 36 || gpio == 39) gpio36_39_used = true; + gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]); gpio_intr_enable(gpio); diff --git a/components/services/globdefs.h b/components/services/globdefs.h index 3d2b1f50..7c12565c 100644 --- a/components/services/globdefs.h +++ b/components/services/globdefs.h @@ -22,6 +22,7 @@ #define I2C_SYSTEM_PORT 1 extern int i2c_system_port; +extern bool gpio36_39_used; #ifdef CONFIG_SQUEEZEAMP #define JACK_GPIO 34 diff --git a/components/services/monitor.h b/components/services/monitor.h index 65fad04f..c0bb8f68 100644 --- a/components/services/monitor.h +++ b/components/services/monitor.h @@ -26,3 +26,5 @@ extern bool jack_inserted_svc(void); extern void (*spkfault_handler_svc)(bool inserted); extern bool spkfault_svc(void); +extern int battery_value_svc(void); + diff --git a/components/services/services.c b/components/services/services.c index 671a4be1..cd13d950 100644 --- a/components/services/services.c +++ b/components/services/services.c @@ -41,17 +41,19 @@ void services_init(void) { #endif // set fixed gpio if any - if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "Vcc_GPIO")) != NULL) { - char *p = nvs_item; - while (p && *p) { - int gpio = atoi(p); - gpio_pad_select_gpio(gpio); - gpio_set_direction(gpio, GPIO_MODE_OUTPUT); - gpio_set_level(gpio, 1); + if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) != NULL) { + char *p = nvs_item, type[4]; + int gpio; + do { + if (sscanf(p, "%d=%3[^,]", &gpio, type) > 0) { + gpio_pad_select_gpio(gpio); + gpio_set_direction(gpio, GPIO_MODE_OUTPUT); + if (!strcasecmp(type, "vcc")) gpio_set_level(gpio, 1); + else if (!strcasecmp(type, "gnd")) gpio_set_level(gpio, 0); + ESP_LOGI(TAG, "set GPIO %u to %s", gpio, type); + } p = strchr(p, ','); - ESP_LOGI(TAG, "set GPIO %u to Vcc", gpio); - if (p) p++; - } + } while (p++); free(nvs_item); } diff --git a/components/wifi-manager/wifi_manager.c b/components/wifi-manager/wifi_manager.c index 2e3cdc56..4acbda23 100644 --- a/components/wifi-manager/wifi_manager.c +++ b/components/wifi-manager/wifi_manager.c @@ -57,13 +57,12 @@ Contains the freeRTOS task and all necessary support #include "lwip/ip4_addr.h" #include "esp_ota_ops.h" #include "esp_app_format.h" -#include "driver/gpio.h" -#include "driver/adc.h" #include "cJSON.h" #include "config.h" #include "trace.h" #include "cmd_system.h" #include "monitor.h" +#include "globdefs.h" #ifndef RECOVERY_APPLICATION #define RECOVERY_APPLICATION 0 @@ -270,6 +269,10 @@ void wifi_manager_init_wifi(){ ESP_LOGD(TAG, "Initializing wifi. Setting WiFi mode to WIFI_MODE_NULL"); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) ); ESP_LOGD(TAG, "Initializing wifi. Starting wifi"); + if (gpio36_39_used) { + ESP_LOGW(TAG, "GPIO 36 or 39 are in use, need to disable WiFi PowerSave!"); + esp_wifi_set_ps(WIFI_PS_NONE); + } ESP_ERROR_CHECK( esp_wifi_start() ); taskYIELD(); ESP_LOGD(TAG, "Initializing wifi. done"); @@ -453,7 +456,7 @@ cJSON * wifi_manager_get_basic_info(cJSON **old){ cJSON_AddItemToObject(root, "ota_dsc", cJSON_CreateString(ota_get_status())); cJSON_AddNumberToObject(root,"ota_pct", ota_get_pct_complete() ); cJSON_AddItemToObject(root, "Jack", cJSON_CreateString(jack_inserted_svc() ? "1" : "0")); - cJSON_AddNumberToObject(root,"Voltage", adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1); + cJSON_AddNumberToObject(root,"Voltage", battery_value_svc()); cJSON_AddNumberToObject(root,"disconnect_count", num_disconnect ); cJSON_AddNumberToObject(root,"avg_conn_time", num_disconnect>0?(total_connected_time/num_disconnect):0 ); diff --git a/main/esp_app_main.c b/main/esp_app_main.c index e6303379..37090474 100644 --- a/main/esp_app_main.c +++ b/main/esp_app_main.c @@ -306,8 +306,8 @@ void register_default_nvs(){ ESP_LOGD(TAG,"Registering default value for key %s", "i2c_config"); config_set_default(NVS_TYPE_STR, "i2c_config", "", 0); - ESP_LOGD(TAG,"Registering default value for key %s", "Vcc_GPIO"); - config_set_default(NVS_TYPE_STR, "Vcc_GPIO", "", 0); + ESP_LOGD(TAG,"Registering default value for key %s", "set_GPIO"); + config_set_default(NVS_TYPE_STR, "set_GPIO", "", 0); ESP_LOGD(TAG,"Registering default value for key %s", "metadata_config"); config_set_default(NVS_TYPE_STR, "metadata_config", "", 0);