lv_binding_micropython icon indicating copy to clipboard operation
lv_binding_micropython copied to clipboard

Micropython easily gets panic after registering LVGL log print callback function

Open garywill opened this issue 3 months ago • 1 comments

LVGL doesn't provide feature to change log level during runtime, so I had to use lv.log_register_print_cb() to set a python function as the print callback to control log level. But I encountered memory problem, which always cause MPY system panic.

Microython 1.26.1 LVGL 9.3 (latest lv_binding_micropython commit f33add0) On ESP32-C3, IDF 5.4.2

A fatal error occurred. The crash dump printed below ....
....
Guru Meditation Error: Core  0 panic'ed (Instruction access fault). Exception was unhandled.

My lv_conf.h

#if LV_USE_LOG 
    /** Set value to one of the following levels of logging detail:
     *  - LV_LOG_LEVEL_TRACE    Log detailed information.
     *  - LV_LOG_LEVEL_INFO     Log important events.
     *  - LV_LOG_LEVEL_WARN     Log if something unwanted happened but didn't cause a problem.
     *  - LV_LOG_LEVEL_ERROR    Log only critical issues, when system may fail.
     *  - LV_LOG_LEVEL_USER     Log only custom log messages added by the user.
     *  - LV_LOG_LEVEL_NONE     Do not log anything. */
    #define LV_LOG_LEVEL LV_LOG_LEVEL_TRACE

    /** - 1: Print log with 'printf';
     *  - 0: User needs to register a callback with `lv_log_register_print_cb()`. */
    #define LV_LOG_PRINTF 0

    /** Set callback to print logs.
     *  E.g `my_print`. The prototype should be `void my_print(lv_log_level_t level, const char * buf)`.
     *  Can be overwritten by `lv_log_register_print_cb`. */

    #define LV_LOG_PRINT_CB mp_lv_log_cb

    /** - 1: Enable printing timestamp;
     *  - 0: Disable printing timestamp. */
    #define LV_LOG_USE_TIMESTAMP 1

    /** - 1: Print file and line number of the log;
     *  - 0: Do not print file and line number of the log. */
    #define LV_LOG_USE_FILE_LINE 1

    /* Enable/disable LV_LOG_TRACE in modules that produces a huge number of logs. */
    #define LV_LOG_TRACE_MEM        1   /**< Enable/disable trace logs in memory operations. */
    #define LV_LOG_TRACE_TIMER      1   /**< Enable/disable trace logs in timer operations. */
    #define LV_LOG_TRACE_INDEV      1   /**< Enable/disable trace logs in input device operations. */
    #define LV_LOG_TRACE_DISP_REFR  1   /**< Enable/disable trace logs in display re-draw operations. */
    #define LV_LOG_TRACE_EVENT      1   /**< Enable/disable trace logs in event dispatch logic. */
    #define LV_LOG_TRACE_OBJ_CREATE 1   /**< Enable/disable trace logs in object creation (core `obj` creation plus every widget). */
    #define LV_LOG_TRACE_LAYOUT     1   /**< Enable/disable trace logs in flex- and grid-layout operations. */
    #define LV_LOG_TRACE_ANIM       1   /**< Enable/disable trace logs in animation logic. */
    #define LV_LOG_TRACE_CACHE      1   /**< Enable/disable trace logs in cache operations. */
#endif  /*LV_USE_LOG*/

My python code:

import lvgl as lv

# {'TRACE': 0, 'INFO': 1, 'WARN': 2, 'ERROR': 3, 'USER': 4, 'NONE': 5}
LV_LOG_LEVEL = lv.LOG_LEVEL.WARN
def lv_log_cb(level, content):
    if level >= LV_LOG_LEVEL:
        print(content)
    # gc.collect() # without this, MPY easily panic. But adding this will make it very slow
lv.log_register_print_cb(lv_log_cb)

disp1 = None
def init_lv():
    global disp1

    lv.init()

    disp1 = lv.display_create(LCD_W, LCD_H)
    disp1.set_color_format(lv.COLOR_FORMAT.I1)

    disp1.set_buffers(lv_disp_bufarr, 
                    None,  
                    len(lv_disp_bufarr),
                    lv.DISPLAY_RENDER_MODE.FULL,
                    )

    disp1.set_flush_cb(flush_cb)

init_lv()


scr = lv.obj()
lv.screen_load(scr)

lv.refr_now(disp1)
# add some widgets
lv.refr_now(disp1)
# add some widgets . Easily MPY system panic !!!

garywill avatar Oct 08 '25 16:10 garywill

At first I disabled some default items in lv_conf.h (was trying to make a small firmware). The result was MPY+LVGL system crashed frequently.

Now I've re-enabled some items, and make some stack/cache larger. Then it don't crash.

This is how my lv_conf.h is, compared to when I opened this issue: (not all diff item. Only some items which I think may matter)

diff --git a/lv_conf.h b/lv_conf.h
--- a/lv_conf.h
+++ b/lv_conf.h
@@ -54,7 +54,7 @@
 
 #if LV_USE_STDLIB_MALLOC == LV_STDLIB_BUILTIN
     /** Size of memory available for `lv_malloc()` in bytes (>= 2kB) */
-    #define LV_MEM_SIZE (64 * 1024U)          /**< [bytes] */  // The default value
+    #define LV_MEM_SIZE (128 * 1024U)          /**< [bytes] */ // I increase this
 
     /** Size of the memory expand for `lv_malloc()` in bytes */
     #define LV_MEM_POOL_EXPAND_SIZE 0
@@ -140,7 +140,7 @@
 /** Stack size of drawing thread.
  * NOTE: If FreeType or ThorVG is enabled, it is recommended to set it to 32KB or more.
  */
-/* #define LV_DRAW_THREAD_STACK_SIZE    (8 * 1024) */  // This was disabled by default
+#define LV_DRAW_THREAD_STACK_SIZE    (8 * 1024)        // Now I enable 
 /**< [bytes]*/
 
 #define LV_USE_DRAW_SW 1
@@ -425,7 +425,7 @@
 
 /* PRIVATE API */
 
-#define LV_USE_PRIVATE_API 0  // I used to disable this
+#define LV_USE_PRIVATE_API 1  // This was enabled by default. Now I enable it again
 
 /*Garbage Collector settings
  *Used if LVGL is bound to higher level language and the memory is managed by that language*/
@@ -434,7 +434,7 @@ extern void mp_lv_deinit_gc();
 #define LV_GC_INIT() mp_lv_init_gc()
 #define LV_GC_DEINIT() mp_lv_deinit_gc()
 
-#define LV_ENABLE_GLOBAL_CUSTOM 0  // I used to disable this
+#define LV_ENABLE_GLOBAL_CUSTOM 1  // This was enabled by default. Now I enable it again
 #if LV_ENABLE_GLOBAL_CUSTOM
     extern void *mp_lv_roots;
     #define LV_GLOBAL_CUSTOM() ((lv_global_t*)mp_lv_roots)

So it is the lacking of doc that made me chose some wrong item value at first.

Although have fixed it by luck, I'm not yet sure which item is the key, and don't know why. Loving the project. LVGL is a great work, and having MPY binding is awesome! Wish better doc for MPY binding. Please make it more detailed and beginner-friendly.

garywill avatar Oct 13 '25 03:10 garywill