Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32 crashes in begin() call #40

Open
fabsenet opened this issue Aug 22, 2020 · 10 comments
Open

esp32 crashes in begin() call #40

fabsenet opened this issue Aug 22, 2020 · 10 comments

Comments

@fabsenet
Copy link

Hi,

I try to build a ble keyboard which is controlled via a webserver over wifi.

I had your sample script running and controlling the ble keyboard via serial.
Also I had a webserver running and sending commands via web sockets.
Combining both is where I fail.

Whenever I call blekeyboard.begin(), it crashes, when it leaves the setup() method. I decoded the stack trace:

Decoding stack results
0x40091f40: invoke_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 155
0x40092171: abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 170
0x40149137: __cxxabiv1::__terminate(void (*)()) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 47
0x4014917e: std::terminate() at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 57
0x40148849: __cxxabiv1::__cxa_allocate_exception(std::size_t) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_alloc.cc line 268
0x401485cc: operator new(unsigned int) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/new_op.cc line 54
0x4014d523: BLEService::createCharacteristic(BLEUUID, unsigned int) at C:\Users\fabse\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src\BLEService.cpp line 259
0x4014bee9: BLEHIDDevice::manufacturer() at C:\Users\fabse\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src\BLEHIDDevice.cpp line 78
0x400d1517: BleKeyboard::taskServer(void*) at .pio\libdeps\d\ESP32 BLE Keyboard\BleKeyboard.cpp line 135
0x4008e689: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

it feels to me like the ram is full, because new fails, but the build process shows only 11.3% used.

RAM:   [=         ]  11.3% (used 60188 bytes from 532480 bytes)
Flash: [=======   ]  74.3% (used 1460628 bytes from 1966080 bytes)

platform.io ini file:

[env:d]
platform = espressif32
board = az-delivery-devkit-v4
board_build.partitions = min_spiffs.csv
framework = arduino
monitor_speed = 115200
lib_deps =
  ESP32 BLE Arduino@>=1.0.1
  https://github.com/T-vK/ESP32-BLE-Keyboard#master
  ESPAsyncTCP-esphome@>=1.2.3
  ESPAsyncWebServer-esphome@>=1.2.7

relevant part of my main.cpp:

#include <FS.h>
#include "SPIFFS.h"
#include <WebServer.h>
#include <DNSServer.h>
#include <WiFi.h>

#include <FS.h>
#include <SPIFFS.h>
#include <ESPmDNS.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

#include <BleKeyboard.h>

AsyncWebServer asyncWebServer(80);
AsyncWebSocket ws("/ws");
BleKeyboard bleKeyboard("Generic Keyboard", "Generic", 82);

void setupWebServer();

void setup()
{
  Serial.begin(115200);

  Serial.println("setup");

  SPIFFS.begin();

  if (!MDNS.begin("bleKB"))
  {
    Serial.println("Error setting up MDNS responder!");
    while (1)
    {
      delay(1000);
    }
  }

  Serial.println("starting wifi auto connect");
  WiFi.begin("...", "...");

  if (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.printf("WiFi simple Failed!\n");
  }
  else
  {
    Serial.printf("WiFi simple succeeded!\n");
  }

  Serial.println("setting up web server");
  //configureWebServer();//left out, as it does not change anything
  asyncWebServer.begin();
  Serial.println("setting up web server: done");

  Serial.println("Starting BLE work!");
  bleKeyboard.begin();
  Serial.println("setup done!");
}

and a sample serial log:

starting wifi auto connect
WiFi simple succeeded!
setting up web server
setting up web server: done
Starting BLE work!
setup done!
abort() was called at PC 0x401492af on core 1

Backtrace: 0x40091f40:0x3ffecf60 0x4009               2171:0x3ffecf80 0x401492af:0x3ffecfa0 0x401492f6:0x3ffecfc0 0x40148663:0x3ffecfe0 0x40148752:0x3ffed000 0x4014d69b:0x3ffed020 0x4014c061:0x3ffed090 0x400d15ab:0x3ffed0e0 0x
[37m4008e689:0x3ffed120

Rebooting...

notice the spaces in the backtrace? seems wrong to me.

I do not know where I should continue at all with this.

@fabsenet
Copy link
Author

I think, the ble keyboard uses 130kb of ram, can this be true?

  Serial.printf("RAM left %d\n", esp_get_free_heap_size());
  bleKeyboard.begin();
  delay(1000);
  Serial.printf("RAM left %d\n", esp_get_free_heap_size());

yields

RAM left 139200
RAM left 8828

is there a way to reduce this?

@T-vK
Copy link
Owner

T-vK commented Sep 8, 2020

I'm actually not sure why it is so big in size. Maybe there is some sort of memory leak in my code or the issue is actually the underlying BLE libraries.
You could try this gist:
https://gist.github.com/manuelbl/66f059effc8a7be148adb1f104666467

It would be nice to see how RAM usage compares.

@TheStigh
Copy link

TheStigh commented Mar 3, 2021

@fabsenet Him, did you ever figure this out? I have the same experience when combining this library with others (IRRemote. PubSubCLient, ArduinoJson). I don't think this is a memory issue, rather there is a hidden interrupt somewhere in this library. @T-vK , do you have some interrupts in these libraries? The panic I receive points to this

Guru Meditation Error: Core 1 panic'ed (Cache disabled but cached memory region accessed)
14:05:54.619 -> Core 1 register dump:
14:05:54.619 -> PC : 0x400d2f70 PS : 0x00060034 A0 : 0x40084afc A1 : 0x3ffbe960
14:05:54.619 -> A2 : 0x00000001 A3 : 0x00000002 A4 : 0x000000ff A5 : 0x40091100
14:05:54.619 -> A6 : 0xc0100000 A7 : 0x00013ffc A8 : 0x80081648 A9 : 0x3ff5f024
14:05:54.619 -> A10 : 0x3ffbedcc A11 : 0x20000000 A12 : 0x00000400 A13 : 0x3ffdba40
14:05:54.619 -> A14 : 0x00000000 A15 : 0x3ffdba7c SAR : 0x00000018 EXCCAUSE: 0x00000007
14:05:54.619 -> EXCVADDR: 0x00000000 LBEG : 0x4000164d LEND : 0x40001667 LCOUNT : 0xfffffffc
14:05:54.664 -> Core 1 was running in ISR context:
14:05:54.664 -> EPC1 : 0x400985a6 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x400d2f70
14:05:54.664 ->
14:05:54.664 -> Backtrace: 0x400d2f70:0x3ffbe960 0x40084af9:0x3ffbe980 0x400985a3:0x3ffdbaa0 0x40098d75:0x3ffdbac0 0x40087720:0x3ffdbae0 0x400ec3a1:0x3ffdbb50 0x400eac6d:0x3ffdbb70 0x400eb0bb:0x3ffdbb90 0x400eb249:0x3ffdbc00 0x400e9fb6:0x3ffdbc80 0x400e8f79:0x3ffdbce0 0x400e955d:0x3ffdbd20 0x400e0065:0x3ffdbd40 0x400e0504:0x3ffdbdb0 0x400e089a:0x3ffdbde0 0x4013831a:0x3ffdbe10 0x400dc3f6:0x3ffdbe30 0x400d89b5:0x3ffdbe70 0x400d6161:0x3ffdbea0 0x4008e99d:0x3ffdbee0
14:05:54.709 ->

This points to an interrupt issue: 14:05:54.664 -> Core 1 was running in ISR context:

@jason-kong
Copy link

I've used the BleKeyboard with a few projects without issue but my latest one has also run into a problem related to what @TheStigh was saying, where my program cant even seem to get analog reads from a couple of pins after calling begin(). I feel it is related to an interrupt or something to that like. I've tried modifying the library to use some of the modifications @rousir has mentioned without any luck.

After calling begin() the analog pins no longer get any reading. I'm using it with the Arduino FFT library to do a simple audio visualization. Removing the begin() call and the program works fine.

@TheStigh
Copy link

After calling begin() the analog pins no longer get any reading. I'm using it with the Arduino FFT library to do a simple audio visualization. Removing the begin() call and the program works fine.

@jason-kong, if it doesn't crash/panic, I don't think it is interrupt issues, perhaps the pins are overrun somehow? For me, the solution was to discard the custom IRRemote library and use the official one. Now it runs smoothly.

@Sampozzo
Copy link

Hello, i'm using BLECombo library derived from your library and i have the same issue, around 130kb ram used after begin();
Any news on this?
Anyone know how to free memory?

@T-vK
Copy link
Owner

T-vK commented Mar 31, 2021

I think #53 might help reduce RAM usage.

@Sampozzo
Copy link

Hi @T-vK thanks for your fast reply.
I just tested id and it free around 20kb of 130. Good but not not enough yet for my project.
Thanks anyway!

@T-vK
Copy link
Owner

T-vK commented Apr 1, 2021

I'm not sure if the RAM usage can be further reduced. I don't really know to figure out where it is coming from. It could be the BLE libraries, it could be the HID libraries, ... but it could also be that I'm just using them wrong.

dvv added a commit to dvv/ESP32-BLE-Keyboard that referenced this issue Aug 20, 2021
Hi!

This patch allowed me to run the keyboard beside to esphome's WiFi and OTA. Might be related to T-vK#40
Please, consider applying.

TIA
@T-vK
Copy link
Owner

T-vK commented Aug 30, 2021

#111 vastly reduces memory consumption. Also if you add #define USE_NIMBLE to the top of your code, it will use the NimBLE libraries which have a much lower memory foot print to reduce memory consumption even further.
If you want to use the USE_NIMBLE option, you need to install the NimBLE library first:
Sketch -> Include Library -> Manage Libraries -> Search "NimBLE-Arduino" - > install

We still need people with Mac OS and iOS devices to test it before we can create an official release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants