From 244eb10c2941180c90973552cdabecb031518972 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 24 Apr 2018 22:29:12 +0200 Subject: [PATCH] memory display & reduced task stack sizes & reduced bt loop pause --- src/blecsan.cpp | 8 ++++++-- src/globals.h | 3 ++- src/macsniff.cpp | 10 ++++++++-- src/main.cpp | 18 ++++++++++++------ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 1471f5f6..1d3c70da 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -242,6 +242,8 @@ void bt_loop(void * pvParameters) // Initialize BT controller to allocate task and other resource. ESP_LOGI(TAG, "Enabling Bluetooth Controller"); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + bt_cfg.controller_task_stack_size = 8192; // double BT stack size + if (esp_bt_controller_init(&bt_cfg) != ESP_OK) { ESP_LOGE(TAG, "Bluetooth controller initialize failed"); @@ -254,7 +256,9 @@ void bt_loop(void * pvParameters) ESP_LOGE(TAG, "Bluetooth controller enable failed"); goto end; } - + + esp_bt_controller_mem_release(ESP_BT_MODE_BTDM); // gives 30KB more RAM for heap + // Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff ESP_LOGI(TAG, "Init Bluetooth stack"); status = esp_bluedroid_init(); @@ -282,7 +286,7 @@ void bt_loop(void * pvParameters) while(1) { - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(10/portTICK_PERIOD_MS); yield(); } diff --git a/src/globals.h b/src/globals.h index eb238842..1b861002 100644 --- a/src/globals.h +++ b/src/globals.h @@ -48,11 +48,12 @@ extern uint8_t mydata[]; extern uint64_t uptimecounter; extern uint32_t currentMillis ; extern osjob_t sendjob; -extern char display_lora[], display_lmic[]; +extern char display_lora[], display_lmic[], display_mem[]; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters extern bool joinstate; extern std::set macs; +extern const uint32_t heapmem; #ifdef HAS_DISPLAY extern HAS_DISPLAY u8x8; diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 3e5db96b..a98f1cce 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -27,6 +27,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { bool added = false; uint32_t addr2int, vendor2int; // temporary buffer for MAC and Vendor OUI uint16_t hashedmac; // temporary buffer for generated hash value + uint16_t memlevel; // % of used heap mem // only last 3 MAC Address bytes are used for MAC address anonymization // but since it's uint32 we take 4 bytes to avoid 1st value to be 0 @@ -64,11 +65,16 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { #endif } + // Display heap memory left + memlevel = 1 - (ESP.getFreeHeap() / heapmem); + sprintf(display_mem, "%d.1%%", memlevel); + // Log scan result - ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", + ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s -> %d Bytes left", sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", rssi, buff, hashedmac, macs_wifi, macs_ble, - added ? "new" : "known"); + added ? "new " : "known", + ESP.getFreeHeap()); #ifdef VENDORFILTER } else { diff --git a/src/main.cpp b/src/main.cpp index 4cd60af2..e97b84e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,9 +47,10 @@ uint8_t DisplayState = 0; // globals for state machine uint16_t LEDBlinkduration = 0, LEDInterval = 0, color = COLOR_NONE; // state machine variables uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display uint8_t channel = 0; // wifi channel rotation counter global for display -char display_lora[16], display_lmic[16]; // display buffers +char display_lora[16], display_lmic[16], display_mem[16]; // display buffers enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag +const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) @@ -310,7 +311,7 @@ uint64_t uptime() { esp_chip_info_t chip_info; esp_chip_info(&chip_info); u8x8.printf("ESP32 %d cores\nWiFi%s%s\n", - chip_info.cores, + chip_info.cores, (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : ""); u8x8.printf("ESP Rev.%d\n", chip_info.revision); @@ -344,6 +345,10 @@ uint64_t uptime() { u8x8.printf("%-16s", "BLTH:off"); #endif + // update free heap memory display (line 4) + u8x8.setCursor(11,4); + u8x8.printf("%-5s", display_mem); + // update RSSI limiter status & wifi channel display (line 5) u8x8.setCursor(0,5); u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit); @@ -500,6 +505,7 @@ void setup() { #endif u8x8.setCursor(0,5); u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%d", cfg.rssilimit); + sprintf(display_lora, "Join wait"); #endif @@ -521,12 +527,12 @@ ESP_LOGI(TAG, "Starting Lora task on core 1"); xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 1); ESP_LOGI(TAG, "Starting Wifi task on core 0"); -xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 16384, ( void * ) 1, 1, NULL, 0); +xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 2048, ( void * ) 1, 1, NULL, 0); #ifdef BLECOUNTER if (cfg.blescan) { // start BLE task only if BLE function is enabled in NVRAM configuration ESP_LOGI(TAG, "Starting Bluetooth task on core 0"); - xTaskCreatePinnedToCore(bt_loop, "btscan", 16384, ( void * ) 1, 1, NULL, 0); + xTaskCreatePinnedToCore(bt_loop, "btscan", 2048, ( void * ) 1, 1, NULL, 0); } #endif @@ -547,8 +553,8 @@ void loop() { // simple state machine for controlling display, LED, button, etc. uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) - currentMillis = millis(); // timebase for state machine in milliseconds (32bit) - + currentMillis = millis(); // timebase for state machine in milliseconds (32bit) + #ifdef HAS_LED switchLEDstate(); switchLED();