Malloc issue in lvgl_init with nRF52840 DK and ILI9341, causing crash

Hello,

I’m trying to run LVGL 9.3 on a nRF52840 DK with a 2.8" ILI9341 TFT display (SPI) using NCS v3.1.0 / Zephyr 4.1 / Toolchain v3.1.0.

Problem

The app compiles and starts, but I get some malloc fails.
The fault seems related to mallocs inside LVGL during initialization.

The crash happens later.
First lv_label_create returns a null pointer, then I call lv_label_set_text() and it crashes in main(). But the root seems to be in lv_image_decoder_init().

Error location

File:

C:\ncs\v3.1.0\modules\lib\gui\lvgl\src\draw\lv_image_decoder.c:65

Function where the first malloc error occurs:

void lv_image_decoder_init(uint32_t image_cache_size, uint32_t image_header_count)
{
    lv_ll_init(img_decoder_ll_p, sizeof(lv_image_decoder_t));

    /* Initialize the cache */
    lv_image_cache_init(image_cache_size);          // malloc(64) -> OK
    lv_image_header_cache_init(image_header_count); // malloc(64) -> fail
}

Fault log

lv_malloc: alloc of 1796 size succeeded - lv_mem.c:71
lv_malloc: alloc of 24 size succeeded - lv_mem.c:71
lv_malloc: alloc of 32 size succeeded - lv_mem.c:71
lv_malloc: alloc of 64 size succeeded - lv_mem.c:71
lv_malloc: alloc of 64 size failed - lv_mem.c:71
[Error] (0.000, +0)      alloc_cb: Asserted at expression: res != NULL (Out of memory) lv_cache_lru_rb.c:190
[Error] (0.000, +0)      alloc_cb: malloc failed lv_cache_lru_rb.c:192
[Error] (0.000, +0)      lv_cache_create: Asserted at expression: cache != NULL (Out of memory) lv_cache.c:51
lv_malloc: alloc of 32 size failed - lv_mem.c:71
[Error] (0.000, +0)      lv_image_decoder_create: Asserted at expression: decoder != NULL (Out of memory) lv_image_decoder.c:175
[Error] (0.000, +0)      lv_bin_decoder_init: Asserted at expression: decoder != NULL (Out of memory) lv_bin_decoder.c:100
lv_malloc: alloc of 804 size failed - lv_mem.c:71

[00:00:00.687,866] <err> os: ***** BUS FAULT *****
[00:00:00.687,896] <err> os:   Precise data bus error
[00:00:00.687,896] <err> os:   BFAR Address: 0x28808fa
[00:00:00.687,927] <err> os: r0/a1:  0x028808f3  r1/a2:  0x0000005a  r2/a3:  0x2000b080
[00:00:00.687,927] <err> os: r3/a4:  0x028808f3 r12/ip:  0x00000000 r14/lr:  0x0000894f
[00:00:00.687,957] <err> os:  xpsr:  0x21000000
[00:00:00.687,957] <err> os: Faulting instruction address (r15/pc): 0x00029b86
[00:00:00.687,988] <err> os: >>> ZEPHYR FATAL ERROR 25: Unknown error on CPU 0
[00:00:00.688,018] <err> os: Current thread: 0x200010c8 (main)
[00:00:00.821,044] <err> os: Halting system

prj.conf

CONFIG_DISPLAY=y
CONFIG_SPI=y
CONFIG_LVGL=y
CONFIG_GPIO=y
CONFIG_MIPI_DBI=y
CONFIG_MIPI_DBI_SPI=y

# Console and logging support
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_LOG=y
CONFIG_PRINTK=y

CONFIG_MAIN_STACK_SIZE=4096
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=65536

CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_HW_STACK_PROTECTION=n # Temporarily disable for debugging

CONFIG_DISPLAY=y
CONFIG_DISPLAY_LOG_LEVEL_ERR=y

DeviceTree overlay

/ {
    chosen {
        zephyr,display = &ili9341;
    };

    mipi_dbi {
        compatible = "zephyr,mipi-dbi-spi";
        dc-gpios = <&gpio1 11 GPIO_ACTIVE_HIGH>;      // P1.04
        reset-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;    // P1.03
        write-only;
        #address-cells = <1>;
        #size-cells = <0>;
        spi-dev = <&spi1>;

        pinctrl-0 = <&spi1_default>;
        pinctrl-names = "default";

        ili9341: ili9341@0 {
            compatible = "ilitek,ili9341";
            reg = <0>;
            mipi-max-frequency = <4000000>;
            width = <240>;
            height = <320>;
            rotation = <180>;
            ...
        };
    };
};

&spi1 {
    status = "okay";
    cs-gpios = <&gpio1 12 GPIO_ACTIVE_LOW>; // P1.05
    pinctrl-0 = <&spi1_default>;
    pinctrl-1 = <&spi1_sleep>;
    pinctrl-names = "default", "sleep";
};

What I tried

  • Verified the SPI pins & connections
  • Display initializes correctly (backlight on, no SPI errors)
  • Modified CONFIG_HEAP_MEM_POOL_SIZE in prj.conf
  • Modified CONFIG_MAIN_STACK_SIZE in prj.conf (increased to 4096)

Any hints or guidance would be much appreciated!

Thanks in advance,
Niels Ouvrard

Hi

That seems to be lack of memory for LVGL to allocate.

Not familiar on how to configure a project in Zephyr, but you can try to enable LV_LOG_LEVEL to LV_LOG_LEVEL_INFO in order to get this print enabled:

On lv_malloc to print some memory stats when it fails. This is only valid if LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN.

it was the config CONFIG_LV_Z_MEM_POOL_SIZE i forgot