Skip to content

Stack overflow when used with embedded graphics #264

@orhun

Description

@orhun

Description

I have one of these ESP32 devices:

LILYGO T-Display ESP32

Image

I'm creating a new project using the template from this repository with minimal configuration. On top of that, I'm using the embedded-graphics crate for drawing on the display.

Here is a code snippet of what I'm doing:

use std::error::Error;
use std::thread;

use embedded_graphics::{
    mono_font::{ascii::FONT_6X10, MonoTextStyle},
    pixelcolor::Rgb565,
    prelude::*,
    text::Text,
};

use embedded_hal::spi::MODE_3;

use esp_idf_svc::hal::{
    delay::Ets,
    gpio::{AnyIOPin, PinDriver},
    peripherals::Peripherals,
    spi::{SpiConfig, SpiDeviceDriver, SpiDriverConfig},
    units::*,
};
use mipidsi::{interface::SpiInterface, models::ST7789, Builder};

use mipidsi::options::ColorInversion;

const DISPLAY_OFFSET: (u16, u16) = (52, 40);
const DISPLAY_SIZE: (u16, u16) = (135, 240);

fn main() -> Result<(), Box<dyn Error>> {
    esp_idf_svc::sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();

    let peripherals = Peripherals::take()?;

    // Turn on display backlight
    let mut backlight = PinDriver::output(peripherals.pins.gpio4)?;
    backlight.set_high()?;

    // Configure SPI
    let config = SpiConfig::new().baudrate(26.MHz().into()).data_mode(MODE_3);
    let spi_device = SpiDeviceDriver::new_single(
        peripherals.spi2,
        peripherals.pins.gpio18,
        peripherals.pins.gpio19,
        Option::<AnyIOPin>::None,
        Some(peripherals.pins.gpio5),
        &SpiDriverConfig::new(),
        &config,
    )?;
    let mut buffer = [0_u8; 512];
    let spi_interface = SpiInterface::new(
        spi_device,
        PinDriver::output(peripherals.pins.gpio16)?,
        &mut buffer,
    );

    // Configure display
    let mut delay = Ets;
    let mut display = Builder::new(ST7789, spi_interface)
        .invert_colors(ColorInversion::Inverted)
        .reset_pin(PinDriver::output(peripherals.pins.gpio23)?)
        .display_offset(DISPLAY_OFFSET.0, DISPLAY_OFFSET.1)
        .display_size(DISPLAY_SIZE.0, DISPLAY_SIZE.1)
        .init(&mut delay)
        .expect("Failed to init display");

    // Reset pixels
    display
        .clear(Rgb565::BLACK)
        .expect("Failed to clear display");

    // Draw text
    let style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
    Text::new("hello", Point::new(34, 230), style)
        .draw(&mut display)
        .expect("Failed to draw text");

    // Do nothing, forever
    thread::park();
    Ok(())
}

The code is available in this repository.

I expect this to simply draw "hello" on the display. I can flash this without issues but when I try to run it it starts throwing the following error:

***ERROR*** A stack overflow in task main has been detected.

Backtrace: 0x40082746:0x3ffb53b0 0x400863a1:0x3ffb53d0 0x40086fbe:0x3ffb53f0 0x40087cf3:0x3ffb5470 0x40087130:0x3ffb54a0 0x400870e2:0x00000006 |<-CORRUPTED
0x40082746 - panic_abort
    at /drv/cargo/.embuild/espressif/esp-idf/v5.3.2/components/esp_system/panic.c:463
0x400863a1 - esp_system_abort
    at /drv/cargo/.embuild/espressif/esp-idf/v5.3.2/components/esp_system/port/esp_system_chip.c:92
0x40086fbe - vApplicationStackOverflowHook
    at /drv/cargo/.embuild/espressif/esp-idf/v5.3.2/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:563
0x40087cf3 - vTaskSwitchContext
    at /drv/cargo/.embuild/espressif/esp-idf/v5.3.2/components/freertos/FreeRTOS-Kernel/tasks.c:3701
0x40087130 - _frxt_dispatch
    at /drv/cargo/.embuild/espressif/esp-idf/v5.3.2/components/freertos/FreeRTOS-Kernel/portable/xtensa/portasm.S:451

ELF file SHA256: 000000000

Rebooting...

Then it goes into an infinite crash->reboot loop.

Environment

  • ESP-IDF branch or tag: v5.3.2
  • Target device (MCU): esp32
  • OS: Arch Linux
  • How did you install the environment: espup 0.15.1 (simply ran espup install)

More information about the device:

Chip type: esp32 (revision v3.1)
Crystal frequency: 40 MHz
Flash size: 4MB
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC address: 34:98:7a:b9:32:68
App/part. size: 1,087,744/4,128,768 bytes, 26.35%

Contents of export-esp.sh script:

export PATH="/home/orhun/.rustup/toolchains/esp/xtensa-esp-elf/esp-14.2.0_20240906/xtensa-esp-elf/bin:$PATH"
export LIBCLANG_PATH="/home/orhun/.rustup/toolchains/esp/xtensa-esp32-elf-clang/esp-19.1.2_20250225/esp-clang/lib"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions