浏览代码

Add set_GPIO, handle GPIO 36/39 bug

philippe44 5 年之前
父节点
当前提交
dfebb8ceb6

+ 14 - 0
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;
+ }
+
 /****************************************************************************************
  * 
  */

+ 7 - 1
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);
 

+ 1 - 0
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

+ 2 - 0
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);
+

+ 12 - 10
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);
 	}	
 

+ 6 - 3
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	);
 

+ 2 - 2
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);