[Bug] Core2 loads JPG from flash but crashes with image from micro SD card



  • I would like to load a JPEG file from storage and display it on the Core2 display. It will be hundreds of pictures, so I want to use the SD card to give less stress to the internal flash.

    Loading the image from flash works

    from m5stack import *
    lcd.clear()
    lcd.image(lcd.CENTER, lcd.CENTER, "/flash/images/sled240.jpg")
    

    But loading the image from the SD card crashes

    lcd.image(lcd.CENTER, lcd.CENTER, "/sd/images/sled240.jpg")
    
    Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
    Core 1 register dump:
    PC      : 0x4015b7d0  PS      : 0x00060c30  A0      : 0x8015c216  A1      : 0x3ffcf800  
    A2      : 0x3f871ed0  A3      : 0x3ffcf8f0  A4      : 0x3ffcf908  A5      : 0x0f006900  
    A6      : 0x00000006  A7      : 0x00000000  A8      : 0x0000002f  A9      : 0x3f4007d2  
    A10     : 0x00000000  A11     : 0x00000000  A12     : 0x00060021  A13     : 0x3ffcfe10  
    A14     : 0x34003243  A15     : 0x3ffcfd30  SAR     : 0x00000001  EXCCAUSE: 0x0000001c  
    EXCVADDR: 0x0f006920  LBEG    : 0x40093e48  LEND    : 0x40093e53  LCOUNT  : 0x00000000  
    
    ELF file SHA256: 0000000000000000
    
    Backtrace: 0x4015b7cd:0x3ffcf800 0x4015c213:0x3ffcf890 0x4015cb00:0x3ffcf8f0 0x40166cf3:0x3ffcf940 0x401706a9:0x3ffcfb90 0x400e4686:0x3ffcfd00 0x400e03a5:0x3ffcfd30 0x400e04e6:0x3ffcfd50 0x400eea49:0x3ffcfd70 0x400e4754:0x3ffcfe10 0x400e03a5:0x3ffcfe70 0x400e03ce:0x3ffcfe90 0x4017640e:0x3ffcfeb0 0x40176535:0x3ffcff40 0x400f83b1:0x3ffcff70 0x40096249:0x3ffcffa0
    

    I transferred the file with Thonny to both locations, using latest Core2 firmware v1.7.2. The SD card seems to be working fine.

    >>> import os
    >>> os.listdir("/sd/images")
    ['sled240.jpg']
    

    How can I fix this?

    Thanks
    Mike



  • Tried a different (newer) micro SD card - same result.



  • I checked the image from the micro SD card on my Mac, it seems ok.

    Reading a config file from the same micro SD card directory works, too:

    import ujson
    with open('/sd/images/config.json') as fp:
        config = ujson.load(fp)
        print('Config: ', config)
    

    And at least the size of both files is equal:

    >>> import os
        os.stat("/flash/images/sled240.jpg")
        os.stat("/sd/images/sled240.jpg")
    (32768, 0, 0, 0, 0, 0, 59928, -281259708, -281259708, -281259708)
    (32768, 0, 0, 0, 0, 0, 59928, 665424706, 665424706, 665424706)
    
    


  • @mb
    IIRC, M5stack has a limit of 50KB for images. Looks like your image is ~60KB, right? However, why it works with the image stored in flash and not from the SD card, I’m not sure. Can you try to compress the image to under 50KB to see if it works?

    Edit: Also try converting the image from jpg to png, keeping it under 50KB, to see if it helps.

    0_1612169529929_9B96338B-B51F-4265-9777-00E19EC9E5C1.png



  • Interesting, I thought that PNG is not supported.

    lcd.image(x, y, file [,scale, type])
    Display the image from the file file on position (x,y)
        JPG and BMP can be displayed.
    

    Source: M5Stack_MicroPython

    I was not successful to display an image in PNG format. But also some JPGs were not displayed at all, the command was just ignored, no error message.

    I resized my original image and tried again, but with the same image from micro SD card the Core2 crashes.

    >>> import os
        os.stat("/sd/images/sled150.jpg")
    (32768, 0, 0, 0, 0, 0, 47158, 665489636, 665489636, 665489636)
    
    >>> from m5stack import *
        lcd.clear()
        lcd.image(lcd.CENTER, lcd.CENTER, "/flash/images/sled150.jpg")
        lcd.image(lcd.CENTER, lcd.CENTER, "/sd/images/sled150.jpg")
    
    Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
    Core 1 register dump:
    PC      : 0x4015b7d0  PS      : 0x00060e30  A0      : 0x8015c216  A1      : 0x3ffcf800  
    A2      : 0x3f871ed0  A3      : 0x3ffcf8f0  A4      : 0x3ffcf908  A5      : 0x0f006900  
    A6      : 0x00000006  A7      : 0x00000000  A8      : 0x0000002f  A9      : 0x3f4007d2  
    A10     : 0x00000000  A11     : 0x00000000  A12     : 0x3ffcfe2c  A13     : 0x3ffcfe10  
    A14     : 0x34003243  A15     : 0x3ffcfe00  SAR     : 0x00000001  EXCCAUSE: 0x0000001c  
    EXCVADDR: 0x0f006920  LBEG    : 0x40093e48  LEND    : 0x40093e53  LCOUNT  : 0x00000000  
    
    ELF file SHA256: 0000000000000000
    
    Backtrace: 0x4015b7cd:0x3ffcf800 0x4015c213:0x3ffcf890 0x4015cb00:0x3ffcf8f0 0x40166cf3:0x3ffcf940 0x401706a9:0x3ffcfb90 0x400e4686:0x3ffcfd00 0x400e03a5:0x3ffcfd30 0x400e04e6:0x3ffcfd50 0x400eea49:0x3ffcfd70 0x400e4754:0x3ffcfe10 0x400e03a5:0x3ffcfe70 0x400e03ce:0x3ffcfe90 0x4017640e:0x3ffcfeb0 0x40176535:0x3ffcff40 0x400f83b1:0x3ffcff70 0x40096249:0x3ffcffa0
    
    Rebooting...
    ets Jul 29 2019 12:21:46
    
    rst:0xc (SW_CPU_RESET),boot:0x17 (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:1
    load:0x3fff0018,len:4
    load:0x3fff001c,len:5276
    load:0x40078000,len:14052
    load:0x40080400,len:3792
    entry 0x4008063c
    

    I tried to find the actual source code from lcd.image() but was not successful.



  • Reading the image from the micro SD card works, though this is very ugly ;-)

    with open('/sd/images/sled150.jpg') as fp:
        line = fp.readline()
        while line:
            print(line)
            line = fp.readline()
    

    So it seems to be an issue with the lcd.image(), not with the file system.



  • While lcd.image() does not seem to support PNG, I now got M5Img() warking and it only supports PNG. It does not crash when loading an image from SD card.

    from m5stack import *
    from m5stack_ui import *
    
    screen = M5Screen()
    screen.clean_screen()
    screen.set_screen_bg_color(0xFFFFFF)
    
    image0 = M5Img("/sd/images/sled150.png", x=0, y=0, parent=None)
    

    Too bad the web service I am targeting only delivers JPG files :-(



  • @mb

    Ahh I see now what was going on. The really early versions of M5stack MicroPython were based on Lobo's branch. About 18 months ago (give or take a few months), M5stack rebased from the mainline MicroPython releases and so much has changed since then.... new APIs, new firmware, new upy modules, etc. It didn't even occur to me which API you were using.... I totally missed that.

    Anyway, bmp/jpg works on the original Core devices (black/grey/Fire/Go) and png works on the Core2. I think that is the case due to M5stack using LVGL graphics engine on the Core2. Not sure that LVGL supports bmp/jpg yet according to this...
    https://forum.lvgl.io/t/jpg-decoder-do-we-have-ready-for-support-decoder-jpg-format/632

    Hope this helps some.



  • Thanks for the info, @world101 !

    I guess I just need to go to old M5Stack MicroPython (LoBo), find the JPG encoder and plug it into an LVGL image encoder. Never mind.

    So I need to find a different solution to display JPGs from SD on my Core2 display.



  • Here we go, easy work-around for the faulty lcd.image():

    from m5stack import *
    buffer = open("/sd/images/test.jpg").read()
    lcd.image_buff(lcd.CENTER, lcd.CENTER, buffer)