LVGL + BluetoothSerial memory problems

ae.9000
Posts: 1
Joined: Tue Apr 30, 2024 1:23 am

LVGL + BluetoothSerial memory problems

Postby ae.9000 » Tue Apr 30, 2024 2:07 am

Hello,
I am currently working on a display that mounts in a car to display engine data accessed from it's OBD2 port. The OBD2 data is gathered by an elm327-based bluetooth OBD scanner, and the screen I am using is the esp32-3248s035c with a built in esp32-wroom module. In terms of the code, I'm using the elmduino library and BluetoothSerial to connect to the scanner, and I am using this driver library:https://github.com/rzeldent/esp32-smartdisplay and lvgl to run the screen interface.

Both components (bluetooth and screen) work as expected when tested separately, but the issue lies when I combine the two. If I initialize the display before the bluetooth connection, the connection to the scanner fails. If instead I connect bluetooth first, the connection works as expected but as the display gets initialized there is a guru meditation error and the processor resets. Here is the error and stack trace. Everything was initializing and connecting as expected up until this point:

Code: Select all

y:0x3ffd5cbe, strength:0x00000000, point_num:0x3ffd5cbb, max_point_num:1
Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x401c1ce8  PS      : 0x00060e30  A0      : 0x800f0aa4  A1      : 0x3ffd5770  
A2      : 0x00000000  A3      : 0x00000010  A4      : 0x000001e0  A5      : 0x00000050  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0xbef7bef7  A9      : 0x0000bef7  
A10     : 0x00000000  A11     : 0x000001df  A12     : 0x00000015  A13     : 0x00000013  
A14     : 0x00000001  A15     : 0x3ffc468c  SAR     : 0x0000001f  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000000  LBEG    : 0x40091898  LEND    : 0x400918a3  LCOUNT  : 0x00000000  


Backtrace: 0x401c1ce5:0x3ffd5770 0x400f0aa1:0x3ffd5790 0x400f070b:0x3ffd5810 0x400f2c81:0x3ffd5840 0x400f4009:0x3ffd58e0 0x401c19d9:0x3ffd59b0 0x400d5c48:0x3ffd59d0 0x400d63cb:0x3ffd5a80 0x401c141a:0x3ffd5ab0 0x400d3adb:0x3ffd5ad0 0x400d3c86:0x3ffd5af0 0x400db00d:0x3ffd5b30 0x400db0d9:0x3ffd5b80 0x400db432:0x3ffd5c00 0x400db563:0x3ffd5c30 0x400dc2f3:0x3ffd5d00 0x400e5ad8:0x3ffd5d90 0x400d2f0e:0x3ffd5dc0 0x400fe092:0x3ffd5e10

  #0  0x401c1ce5:0x3ffd5770 in lv_color_fill at .pio/libdeps/esp32-3248S035C/lvgl/src/misc/lv_color.c:73
  #1  0x400f0aa1:0x3ffd5790 in fill_normal at .pio/libdeps/esp32-3248S035C/lvgl/src/draw/sw/lv_draw_sw_blend.c:256
      (inlined by) lv_draw_sw_blend_basic at .pio/libdeps/esp32-3248S035C/lvgl/src/draw/sw/lv_draw_sw_blend.c:188
  #2  0x400f070b:0x3ffd5810 in lv_draw_sw_blend at .pio/libdeps/esp32-3248S035C/lvgl/src/draw/sw/lv_draw_sw_blend.c:105
  #3  0x400f2c81:0x3ffd5840 in draw_bg at .pio/libdeps/esp32-3248S035C/lvgl/src/draw/sw/lv_draw_sw_rect.c:128
  #4  0x400f4009:0x3ffd58e0 in lv_draw_sw_rect at .pio/libdeps/esp32-3248S035C/lvgl/src/draw/sw/lv_draw_sw_rect.c:73
  #5  0x401c19d9:0x3ffd59b0 in lv_draw_rect at .pio/libdeps/esp32-3248S035C/lvgl/src/draw/lv_draw_rect.c:66
  #6  0x400d5c48:0x3ffd59d0 in lv_obj_draw at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_obj.c:561
  #7  0x400d63cb:0x3ffd5a80 in lv_obj_event at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_obj.c:873
  #8  0x401c141a:0x3ffd5ab0 in lv_obj_event_base at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_event.c:96
  #9  0x400d3adb:0x3ffd5ad0 in event_send_core at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_event.c:452
  #10 0x400d3c86:0x3ffd5af0 in lv_event_send at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_event.c:74
  #11 0x400db00d:0x3ffd5b30 in lv_obj_redraw at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:148
  #12 0x400db0d9:0x3ffd5b80 in refr_obj at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:970
      (inlined by) refr_obj at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:964
  #13 0x400db432:0x3ffd5c00 in refr_obj_and_children at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:854
  #14 0x400db563:0x3ffd5c30 in refr_area_part at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:789
  #15 0x400dc2f3:0x3ffd5d00 in refr_area at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:678
      (inlined by) refr_invalid_areas at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:616
      (inlined by) _lv_disp_refr_timer at .pio/libdeps/esp32-3248S035C/lvgl/src/core/lv_refr.c:324
  #16 0x400e5ad8:0x3ffd5d90 in lv_timer_exec at .pio/libdeps/esp32-3248S035C/lvgl/src/misc/lv_timer.c:313
      (inlined by) lv_timer_handler at .pio/libdeps/esp32-3248S035C/lvgl/src/misc/lv_timer.c:109
  #17 0x400d2f0e:0x3ffd5dc0 in setup() at src/main.cpp:54
  #18 0x400fe092:0x3ffd5e10 in loopTask(void*) at /home/------/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:42




ELF file SHA256: e3f80a5c5cb1288b

Rebooting...

The errors I am getting seem to be pointing towards running out of memory. It seems like bluetooth consumes a considerable amount of ram, over 170kB out of the 320kB available, if my measurements are correct, and as expected the screen requires a considerable amount of ram as well. The problem with running out of memory in this case is that I don't know how to cut down on the usage since it is failing in the setup. I don't know if there are some optimizations that I could do in either of the three libraries to make them less ram intensive, or whether there's nothing I can really do. Here is the main file for reference:

Code: Select all

#include <Arduino.h>
#include <esp32_smartdisplay.h>
#include <ui/ui.h>
#include "BluetoothSerial.h"
#include "ELMduino.h"

BluetoothSerial SerialBT;
ELM327 myELM327;
#define ELM_PORT   SerialBT



unsigned long previousMillis = 0;  // will store last time LED was updated

void setup() {
  Serial.begin(115200);
 
  
  //OBD2 Setup
  SerialBT.setPin("1234");
  SerialBT.begin("ArduHUD", true);
  SerialBT.setTimeout(15000);

  Serial.println("Connecting to bluetooth-------------------");
  //connect to bluetooth
  if (!ELM_PORT.connect("OBDII"))
  {
    //buetooth connect fail
    //smartdisplay_led_set_rgb(1, 0, 0);
    Serial.print("Free heap: ");
    Serial.println(ESP.getFreeHeap());
    Serial.print("TOtal heap: ");
    Serial.println(ESP.getHeapSize());
  
    while(1);
  }
  //connect to car ecu
  if (!myELM327.begin(ELM_PORT, true, 2000))
  {
    //ecu connect fail
    //smartdisplay_led_set_rgb(1, 1, 0);
    Serial.print("Free heap: ");
    Serial.println(ESP.getFreeHeap());
    Serial.print("Total heap: ");
    Serial.println(ESP.getHeapSize());
    while (1);
  } 

  Serial.println("Display setup");
  Serial.print("Free heap: ");
  Serial.println(ESP.getFreeHeap());
  Serial.print("Total heap: ");
  Serial.println(ESP.getHeapSize());
  delay(3000);

  
  //success
  //setup display

  smartdisplay_init(); //setup display and touch

  //rotate display horizontally
  auto disp = lv_disp_get_default();
  lv_disp_set_rotation(disp, LV_DISP_ROT_90);

  ui_init();//don't forget

  lv_timer_handler();//run the update function once just to remove the startup randomness
  
  Serial.println("Display setup");
  Serial.print("Free heap: ");
  Serial.println(ESP.getFreeHeap());
  Serial.print("Total heap: ");
  Serial.println(ESP.getHeapSize());
  

  smartdisplay_led_set_rgb(0, 1, 0);
}
void loop() {
  lv_timer_handler();
  float volts = myELM327.batteryVoltage();

    if (myELM327.nb_rx_state == ELM_SUCCESS)
    {
        Serial.print("Battery Voltage: ");
        Serial.println(volts);
        delay(10000);
    }
    else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
    {
        myELM327.printError();
    }
}

This has left me scratching my head for some time now, so any help would be greatly appreciated.

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: LVGL + BluetoothSerial memory problems

Postby lbernstone » Tue Apr 30, 2024 5:21 pm

I'm sure there are optimizations that could reduce your memory consumption (particularly in the buffer size), but when you are using pre-built libraries like this, you would need to understand the code internals in order to know how to optimize. This would take significant time and knowledge.
So, my recommendation would be to optimize your hardware. If you get a BLE-based OBD dongle, you can switch to using the NimBLE library, which is significantly lower in memory (and power) consumption. If you switch to a different module, eg the esp32-s3, you will have more memory and a better memory layout. You might even get enough RAM migrated externally if you use a ESP32-WROVER module or add a psram chip. You could probably test the memory consumption using wokwi+VSCode emulating a WROVER before buying anything.

Who is online

Users browsing this forum: No registered users and 100 guests