PlatformIO Community

OutOfMemory error - TTGO-Camera-Plus - OpenCV

Hi everyone!

I’m trying to make this project [1] work with this board [2] using PlatformIO.

My setup looks like this:

PLATFORM: Espressif 32 (4.4.0) > TTGO T-Camera
HARDWARE: ESP32 240MHz, 8MB RAM, 4MB Flash

PACKAGES: 
 - framework-espidf @ 3.40302.0 (4.3.2) 
 - tool-cmake @ 3.16.4 
 - tool-esptoolpy @ 1.30300.0 (3.3.0) 
 - tool-mkfatfs @ 2.0.1 
 - tool-mklittlefs @ 1.203.210628 (2.3) 
 - tool-mkspiffs @ 2.230.0 (2.30) 
 - tool-ninja @ 1.7.1 
 - toolchain-esp32ulp @ 1.22851.191205 (2.28.51) 
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3

It compiles like this:

Reading CMake configuration...
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
Retrieving maximum program size .pio/build/ttgo_demo/firmware.elf
Checking size .pio/build/ttgo_demo/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   1.3% (used 108820 bytes from 8388608 bytes)
Flash: [=====     ]  48.9% (used 1539657 bytes from 3145728 bytes)

Rebooting...
ets Jul 29 2019 12:21:46

And will reboot every 30 seconds stating this:

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7916
ho 0 tail 12 room 4
load:0x40078000,len:16064
load:0x40080400,len:4452
entry 0x4008069c
I (29) boot: ESP-IDF 4.3.2 2nd stage bootloader
I (29) boot: compile time 17:30:50
I (29) boot: chip revision: 3
I (32) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (39) qio_mode: Enabling default flash chip QIO
I (44) boot.esp32: SPI Speed      : 40MHz
I (49) boot.esp32: SPI Mode       : QIO
I (53) boot.esp32: SPI Flash Size : 4MB
I (58) boot: Enabling RNG early entropy source...
I (63) boot: Partition Table:
I (67) boot: ## Label            Usage          Type ST Offset   Length
I (74) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (82) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (89) boot:  2 factory          factory app      00 00 00010000 00300000
I (96) boot:  3 storage          Unknown data     01 82 00310000 00096000
I (104) boot: End of partition table
I (108) boot_comm: chip revision: 3, min. application chip revision: 0
I (115) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=56514h (353556) map
I (240) esp_image: segment 1: paddr=0006653c vaddr=3ffb0000 size=085f4h ( 34292) load
I (253) esp_image: segment 2: paddr=0006eb38 vaddr=40080000 size=014e0h (  5344) load
I (255) esp_image: segment 3: paddr=00070020 vaddr=400d0020 size=10483ch (1067068) map
I (610) esp_image: segment 4: paddr=00174864 vaddr=400814e0 size=13728h ( 79656) load
I (641) esp_image: segment 5: paddr=00187f94 vaddr=50000000 size=00010h (    16) load
I (653) boot: Loaded app from partition at offset 0x10000
I (653) boot: Disabling RNG early entropy source...
I (665) psram: This chip is ESP32-D0WD
I (665) spiram: Found 64MBit SPI RAM device
I (666) spiram: SPI RAM mode: flash 80m sram 80m
I (670) spiram: PSRAM initialized, cache is in low/high (2-core) mode.
I (677) cpu_start: Pro cpu up.
I (681) cpu_start: Starting app cpu, entry point is 0x4008330c
I (673) cpu_start: App cpu up.
I (1181) spiram: SPI SRAM memory test OK
I (1188) cpu_start: Pro cpu start user code
I (1188) cpu_start: cpu freq: 240000000
I (1189) cpu_start: Application information:
I (1192) cpu_start: Project name:     ttgo_demo
I (1197) cpu_start: App version:      1
I (1201) cpu_start: Compile time:     Jun 20 2022 17:19:51
I (1207) cpu_start: ELF file SHA256:  83ea3e4fbc8144e1...
I (1213) cpu_start: ESP-IDF:          4.3.2
I (1218) heap_init: Initializing. RAM available for dynamic allocation:
I (1226) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (1232) heap_init: At 3FFCA918 len 000156E8 (85 KiB): DRAM
I (1238) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (1244) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1251) heap_init: At 40094C08 len 0000B3F8 (44 KiB): IRAM
I (1257) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (1266) spi_flash: detected chip: generic
I (1270) spi_flash: flash io: qio
I (1277) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (1290) main: Starting main
I (1293) gpio: GPIO[13]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1303) sccb: pin_sda 18 pin_scl 23

I (1425) camera: Detected OV2640 camera
I (1425) gpio: GPIO[36]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1428) gpio: GPIO[37]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1438) gpio: GPIO[38]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1447) gpio: GPIO[39]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1456) gpio: GPIO[35]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1466) gpio: GPIO[26]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1475) gpio: GPIO[13]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1484) gpio: GPIO[34]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1494) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1503) gpio: GPIO[27]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1512) gpio: GPIO[25]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (1522) camera: Allocating 1 frame buffers (150 KB total)
I (1528) camera: Allocating 150 KB frame buffer in PSRAM
I (1804) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (5148) SYSTEM: task main stack high watermark: 1728 Bytes
I (5148) SYSTEM: heap left: 4212971 Bytes
I (5148) main: Display width = 240, height = 240
I (5153) main: Starting demo_task
I (5157) SYSTEM: task demo stack high watermark: 7184 Bytes
I (5163) SYSTEM: heap left: 4203411 Bytes

abort() was called at PC 0x401adc92 on core 0

Backtrace:0x400837ea:0x3ffd5c70 0x4008db11:0x3ffd5c90 0x40093952:0x3ffd5cb0 0x401adc92:0x3ffd5d20 0x401adcbf:0x3ffd5d40 0x4018f45b:0x3ffd5d60 0x40153822:0x3ffd5d80 0x4015417d:0x3ffd5dc0 0x4012c283:0x3ffd5e80 0x4012c2b1:0x3ffd5ec0 0x401425ed:0x3ffd5ee0 0x40151fed:0x3ffd5fc0 0x4013fe25:0x3ffd6010 0x400d2237:0x3ffd6180 0x400d2a47:0x3ffd61f0 0x400907f5:0x3ffd6370
  #0  0x400837ea:0x3ffd5c70 in panic_abort at /home/mongoq/.platformio/packages/framework-espidf/components/esp_system/panic.c:393
  #1  0x4008db11:0x3ffd5c90 in esp_system_abort at /home/mongoq/.platformio/packages/framework-espidf/components/esp_system/system_api.c:112
  #2  0x40093952:0x3ffd5cb0 in abort at /home/mongoq/.platformio/packages/framework-espidf/components/newlib/abort.c:46
  #3  0x401adc92:0x3ffd5d20 in int abort_return<int>() at /home/mongoq/.platformio/packages/framework-espidf/components/cxx/cxx_exception_stubs.cpp:28
  #4  0x401adcbf:0x3ffd5d40 in __wrap__Unwind_RaiseException at /home/mongoq/.platformio/packages/framework-espidf/components/cxx/cxx_exception_stubs.cpp:153
  #5  0x4018f45b:0x3ffd5d60 in __cxa_throw at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:90
  #6  0x40153822:0x3ffd5d80 in cv::error(cv::Exception const&) at ??:?
  #7  0x4015417d:0x3ffd5dc0 in cv::error(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*, char const*, int) at ??:?
  #8  0x4012c283:0x3ffd5e80 in cv::OutOfMemoryError(unsigned int) at alloc.cpp:?
  #9  0x4012c2b1:0x3ffd5ec0 in cv::fastMalloc(unsigned int) at ??:?
  #10 0x401425ed:0x3ffd5ee0 in cv::Mat::create(int, int const*, int) at ??:?
  #11 0x40151fed:0x3ffd5fc0 in cv::_OutputArray::create(int, int, int, int, bool, cv::_OutputArray::DepthMask) const at ??:?
  #12 0x4013fe25:0x3ffd6010 in cv::Mat::copyTo(cv::_OutputArray const&) const at ??:?
  #13 0x400d2237:0x3ffd6180 in updateCameraImage(cv::Mat const&) at main/main.cpp:88
  #14 0x400d2a47:0x3ffd61f0 in demo_task(void*) at main/main.cpp:179 (discriminator 1)
  #15 0x400907f5:0x3ffd6370 in vPortTaskWrapper at /home/mongoq/.platformio/packages/framework-espidf/components/freertos/port/xtensa/port.c:168


ELF file SHA256: 83ea3e4fbc8144e1

Rebooting...

I can’t find any solution …

The sdkconfig I used is from the original project [3].

Thank you for any help and suggestions.

mongoq

[1] TTGO T-Camera Plus ESP32 Development Board - Review, Pinout, and Libraries - Maker Advisor
[2] https://github.com/joachimBurket/esp32-opencv
[3] esp32-opencv/sdkconfig at master · joachimBurket/esp32-opencv · GitHub

What’s your platformio.ini and sdkconfig? Especially the PSRAM config with the bankswitch to use memory beyond the 4MB window seems critical

My sdkconfig looks like this:

#
# ESP32-specific
#
CONFIG_ESP32_ECO3_CACHE_LOCK_FIX=y
CONFIG_ESP32_REV_MIN_0=y
# CONFIG_ESP32_REV_MIN_1 is not set
# CONFIG_ESP32_REV_MIN_2 is not set
# CONFIG_ESP32_REV_MIN_3 is not set
CONFIG_ESP32_REV_MIN=0
CONFIG_ESP32_DPORT_WORKAROUND=y
# CONFIG_ESP32_DEFAULT_CPU_FREQ_80 is not set
# CONFIG_ESP32_DEFAULT_CPU_FREQ_160 is not set
CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y
CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240
CONFIG_ESP32_SPIRAM_SUPPORT=y

#
# SPI RAM config
#
CONFIG_SPIRAM_TYPE_AUTO=y
# CONFIG_SPIRAM_TYPE_ESPPSRAM16 is not set
# CONFIG_SPIRAM_TYPE_ESPPSRAM32 is not set
# CONFIG_SPIRAM_TYPE_ESPPSRAM64 is not set
CONFIG_SPIRAM_SIZE=-1
# CONFIG_SPIRAM_SPEED_40M is not set
CONFIG_SPIRAM_SPEED_80M=y
CONFIG_SPIRAM=y
CONFIG_SPIRAM_BOOT_INIT=y
# CONFIG_SPIRAM_USE_MEMMAP is not set
# CONFIG_SPIRAM_USE_CAPS_ALLOC is not set
CONFIG_SPIRAM_USE_MALLOC=y
CONFIG_SPIRAM_MEMTEST=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=16384
CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=32768
CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y
CONFIG_SPIRAM_CACHE_WORKAROUND=y

#
# SPIRAM cache workaround debugging
#
CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_MEMW=y
# CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_DUPLDST is not set
# CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_NOPS is not set
# end of SPIRAM cache workaround debugging

CONFIG_SPIRAM_BANKSWITCH_ENABLE=y
CONFIG_SPIRAM_BANKSWITCH_RESERVE=8
# CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY is not set
# CONFIG_SPIRAM_OCCUPY_HSPI_HOST is not set
CONFIG_SPIRAM_OCCUPY_VSPI_HOST=y
# CONFIG_SPIRAM_OCCUPY_NO_HOST is not set

My platformio.ini looks like this:

[platformio]
src_dir = main

[env:ttgo_demo]
platform = espressif32
board = ttgo-t-camera
framework = espidf
board_build.partitions = partitions.csv
build_type = debug

monitor_filters = esp32_exception_decoder
monitor_speed = 115200

A possible problem would be that this, in the latest platform-espressif32 version, gives you ESP-IDF 4.3.2, while Final link can not find sysconf · Issue #2 · joachimBurket/esp32-opencv · GitHub states that a specific commit in 4.2 was used (which could map to platform = espressif32@3.2.1 as that uses 4.2.1). Could you give that a try?

A good sanity check would also be to do a heap exhaustion test, where you repeadedly call cv::fastMalloc(<some size, e.g. 1024>) until it crashes with the cv::OutOfMemoryError, and thus compare the PlatformIO firmware and the one built by following the exact instructions in the repository.

With a platformio.ini of:

[platformio]
src_dir = main

[env:ttgo_demo]

;https://community.platformio.org/t/outofmemory-error-ttgo-camera-plus-opencv/28414/4
platform = espressif32@3.2.1

board = ttgo-t-camera
framework = espidf
board_build.partitions = partitions.csv  

;https://community.platformio.org/t/unknown-abort-of-execution/21470/4
monitor_filters = esp32_exception_decoder
build_type = debug
;build_type = release

;https://community.platformio.org/t/serial-monitor-baudrate/5837/5
monitor_speed = 115200

;https://thingpulse.com/esp32-how-to-use-psram/
;https://docs.platformio.org/en/latest/platforms/espressif32.html
build_flags = -DCORE_DEBUG_LEVEL=5
              -DBOARD_HAS_PSRAM
              -mfix-esp32-psram-cache-issue

I get:

PLATFORM: Espressif 32 (3.2.1) > TTGO T-Camera
HARDWARE: ESP32 240MHz, 8MB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-espidf @ 3.40201.210513 (4.2.1) 
 - tool-cmake @ 3.16.4 
 - tool-esptoolpy @ 1.30000.201119 (3.0.0) 
 - tool-mkspiffs @ 2.230.0 (2.30) 
 - tool-ninja @ 1.7.1 
 - toolchain-esp32ulp @ 1.22851.191205 (2.28.51) 
 - toolchain-xtensa32 @ 2.80400.210211 (8.4.0)
Reading CMake configuration...
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
Retrieving maximum program size .pio/build/ttgo_demo/firmware.elf
Checking size .pio/build/ttgo_demo/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   1.3% (used 108300 bytes from 8388608 bytes)
Flash: [=====     ]  49.4% (used 1552576 bytes from 3145728 bytes)
Configuring upload protocol...
AVAILABLE: esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa
CURRENT: upload_protocol = esptool
Looking for upload port...

Auto-detected: /dev/ttyACM0
Uploading .pio/build/ttgo_demo/firmware.bin
usage: esptool write_flash [-h] [--erase-all]
                           [--flash_freq {keep,40m,26m,20m,80m}]
                           [--flash_mode {keep,qio,qout,dio,dout}]
                           [--flash_size FLASH_SIZE]
                           [--spi-connection SPI_CONNECTION] [--no-progress]
                           [--verify] [--encrypt]
                           [--ignore-flash-encryption-efuse-setting]
                           [--compress | --no-compress]
                           <address> <filename> [<address> <filename> ...]
esptool write_flash: error: argument <address> <filename>: Detected overlap at address: 0x8000 for file: /home/mongoq/Dokumente/PlatformIO/Projects/ttgo_demo/.pio/build/ttgo_demo/partitions.bin
*** [upload] Error 2

The partitions.csv wasn’t changed:

# Espressif ESP32 Partition Table
# Name,   Type, SubType, Offset,  Size, Flags
nvs,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 3M,
storage,  data, spiffs,  ,        600K,

The Board .json file looks like this:

{
    "build": {
        "core": "esp32",
        "extra_flags": "",
        "f_cpu": "240000000L",
        "f_flash": "40000000L",
        "flash_mode": "dio",
        "mcu": "esp32",
        "variant": "ttgo-t-camera"
    },
    "connectivity": [
        "wifi",
        "bluetooth",
        "ethernet",
        "can"
    ],
    "frameworks": [
        "arduino",
        "espidf"
    ],
    "name": "TTGO T-Camera",
    "upload": {
        "flash_size": "4MB",
        "maximum_ram_size": 8388608,
        "maximum_size": 4194304,
        "require_upload_port": true,
        "speed": 460800
    },
    "url": "https://github.com/Xinyuan-LilyGo/esp32-camera-screen",
    "vendor": "TTGO"
}

Hm this might be a dilema where PlatformIO still had bugs with partition table parsing like in Partition table parsing wrong · Issue #627 · platformio/platform-espressif32 · GitHub so it may not correctly know where the firmware, partition table or the NVS are and overlap them. :confused:

It works now, I can flash the project and the camera image is shown on the display.

I decreased the CONFIG_LOG_BOOTLOADER_LEVEL from info to error [1] as it was already set to info (not verbose).
Haven’t changed anything of the partition information.
Used platform = espressif32@3.2.1

[1] [TW#28632] esptool write_flash: error: argument <address> <filename>: Detected overlap at address: 0x8000 for file: native_ota_example/build/partitions_two_ota.bin · Issue #3013 · espressif/esp-idf · GitHub

1 Like