LVGL and SD Card won't share SPI bus

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Saul
Posts: 10
Joined: Wed Jun 10, 2020 10:20 pm

Re: LVGL and SD Card won't share SPI bus

Post by Saul » Wed Jun 10, 2020 10:50 pm

N/A
Last edited by Saul on Thu Oct 21, 2021 5:13 am, edited 1 time in total.

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: LVGL and SD Card won't share SPI bus

Post by PM-TPI » Fri Jun 12, 2020 9:00 pm

Mike Teachman wrote:
Sat May 23, 2020 3:03 pm

No. I have never used the ILI9341 micropython driver in any of my littlevgl projects. At this point I have no reason to switch to the micropython driver as the modified C driver does what I need. It's a case of "if it's not broken, don't fix it".
Mike
How would I go about adding the modxpt2046.c touch driver?

BTW the fix for the incorrect colors was to comment out...

Code: Select all

    /*Byte swapping is required   RMG I removed because I did'nt see any like code in ili9341.py
    uint32_t i;
    uint8_t * color_u8 = (uint8_t *) color_p;
    uint8_t color_tmp;
    for(i = 0; i < size * 2; i += 2) {
        color_tmp = color_u8[i + 1];
        color_u8[i + 1] = color_u8[i];
        color_u8[i] = color_tmp;
    }
    */
    

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: LVGL and SD Card won't share SPI bus

Post by Mike Teachman » Sat Jun 13, 2020 6:24 pm

PM-TPI wrote:
Fri Jun 12, 2020 9:00 pm
How would I go about adding the modxpt2046.c touch driver?

BTW the fix for the incorrect colors was to comment out...
For the touch driver: sorry, I do not have any experience with this feature. I have only used LVGL to display graphics. The MicroPython category at the LVGL forum might have some ideas on adding this to the build and getting it working?
https://forum.lvgl.io/c/micropython

Congrats on getting your display to work !

anichang
Posts: 4
Joined: Sun May 09, 2021 12:55 am

Re: LVGL and SD Card won't share SPI bus

Post by anichang » Sun May 09, 2021 1:08 am

Looks like no fix has been mainlined.

I tried to change the python driver by adding a sharedspi flag to the init functions, which would skip the gpios setup and spi master init but, if the sharedspi flag is set to true, the subsequent call spi_bus_add_device() fails with ret = 259 (0x103, ESP_ERR_INVALID_STATE in esp_err.h). No need to say that the SPI bus is already initialized by sd card driver.

So I modified modILI9341.c driver as Mike Teachman did, but I got an "ImportError: no module named 'ILI9341'". I guessed the C driver was replaced by the pure/hybrid python driver, so I re-added ${LV_BINDINGS_DIR}/driver/esp32/modlvesp32.c to lv_bindings/mkrules.cmake . And I can see the object file being built. But still, can't load ILI9341 module. What am I missing?

regards

PM-TPI
Posts: 75
Joined: Fri Jun 28, 2019 3:09 pm

Re: LVGL and SD Card won't share SPI bus

Post by PM-TPI » Mon May 10, 2021 3:47 pm

With these following changes I was able to Share SPI with TFT, Touch, and SDCard module that came with uP
by using modILI9341.c and modxpt2046.c drivers.

In file mpconfigport.h from the ports/esp32 directory un-comment the mp_module_ILI9341 and mp_module_xpt2046 in two locations.
These are the .c drivers to include in build

Code: Select all

extern const struct _mp_obj_module_t mp_module_rtch;
extern const struct _mp_obj_module_t mp_module_lodepng;
extern const struct _mp_obj_module_t mp_module_ILI9341;
extern const struct _mp_obj_module_t mp_module_xpt2046;

#if MICROPY_PY_LVGL
#define MICROPY_PORT_LVGL_DEF \
    { MP_OBJ_NEW_QSTR(MP_QSTR_lvgl), (mp_obj_t)&mp_module_lvgl }, \
    { MP_OBJ_NEW_QSTR(MP_QSTR_lvesp32), (mp_obj_t)&mp_module_lvesp32 }, \
    { MP_OBJ_NEW_QSTR(MP_QSTR_ILI9341), (mp_obj_t)&mp_module_ILI9341 }, \
    { MP_OBJ_NEW_QSTR(MP_QSTR_xpt2046), (mp_obj_t)&mp_module_xpt2046 },

// lvesp needs to delete the timer task upon soft reset
I remove ...
ili9XXX.py
ili9341.py
xpt2046.py
files from "lv_micropython\lib\lv_bindings\driver\esp32\" directory.

In modILI9341.c and modxpt2046.c files change...
.flags=SPI_DEVICE_HALFDUPLEX to .flags=0

and in modILI9341.c move */ down to comment out "Byte swapping" which caused color issue.

Code: Select all

	/*Byte swapping is required
	uint32_t i;
	uint8_t * color_u8 = (uint8_t *) color_p;
	uint8_t color_tmp;
	for(i = 0; i < size * 2; i += 2) {
		color_tmp = color_u8[i + 1];
		color_u8[i + 1] = color_u8[i];
		color_u8[i] = color_tmp;
	}
	*/
In Main.py code use your specific pins but use spihost=2, miso=19, mosi=23, clk=18 only.

Code: Select all

from machine import Pin, SPI
import lvgl as lv
import lvesp32  
import ILI9341 as ili
from xpt2046 import xpt2046

lv.init()
sd = SDCard(slot=2, cs=Pin(14), freq=20_000_000)
# vspi = SPI(2, miso=Pin(19), mosi=Pin(23), sck=Pin(18)) # use this if you don't want SDCard

# -----------------------------------------------------------------------------
disp = ili.display(spihost=2, miso=19, mosi=23, clk=18, cs=15, \
dc=13, rst=0, backlight=27, mhz=20) 
disp.init()

disp_buf1 = lv.disp_buf_t()
buf1_1 = bytearray(320*10)
disp_buf1.init(buf1_1, None, len(buf1_1) //4)
disp_drv = lv.disp_drv_t()
disp_drv.init()
disp_drv.buffer = disp_buf1
disp_drv.flush_cb = disp.flush
disp_drv.hor_res = 240
disp_drv.ver_res = 320
disp_drv.register()

# -----------------------------------------------------------------------------
touch = xpt2046(spihost=2, cs=5, irq=4, mhz=5, x_inv=False, \
y_inv=True, x_min=240, y_min=320, x_max=3783, y_max=3948)
touch.init()

indev_drv = lv.indev_drv_t()
indev_drv.init()
indev_drv.type = lv.INDEV_TYPE.POINTER
indev_drv.read_cb = touch.read
indev_drv.register()
This all worked with LVGL v6

But new problem with v7
Using esp-idf v4.1 lv_micropython dev micropython v1.14
I no longer get touch callbacks...
I believe the issue to be this line....
STATIC bool xpt2046_read(lv_indev_data_t *data); ~line119
but the doc states...
bool xpt2046_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
I can not build using this different line.
Someone who understands .c code would probably see the problem.

Hope This Helps

favnec5
Posts: 3
Joined: Mon Oct 04, 2021 1:42 pm

Re: LVGL and SD Card won't share SPI bus

Post by favnec5 » Mon Oct 04, 2021 1:50 pm

Hello guys,
I resolved this problem with buildin espidf functions on esp32 board and lv_micropython v1.15
Please see : https://github.com/favnec5/micropython- ... /README.md
1) init the SPI bus with ILI9XXX.py driver
2) use the "inside SPI" version of sdcard.py to add a second device (the sdcard) on the Bus.
It work on my M5 core2.
best reguards,
Thomas

divekr
Posts: 6
Joined: Tue Oct 05, 2021 8:02 am

Re: LVGL and SD Card won't share SPI bus

Post by divekr » Tue Oct 05, 2021 8:20 am

Great thanks to Thomas,
I saw the PR on lvgl repo. I am so happy to find it.

SD card works great , at the same time the display works.
My devices are,
LOLIN D32 pro with builtin SD card slot.
with GC9A01 display(no touch module)

Is there any performance difference between half_dulplex and full duplex?

Thanks again:)
Last edited by divekr on Thu Oct 21, 2021 6:25 am, edited 1 time in total.

favnec5
Posts: 3
Joined: Mon Oct 04, 2021 1:42 pm

Re: LVGL and SD Card won't share SPI bus

Post by favnec5 » Mon Oct 11, 2021 1:16 pm

hello divekr,
on my device (with illi9341 screen), the half-duplex=true do not work with the SDCard addon on the bus (white screen).
But i don't see difference on the speed result with half-duplex=false.
best reguards,
Thomas

Patrice
Posts: 13
Joined: Mon Feb 10, 2020 3:34 am

Re: LVGL and SD Card won't share SPI bus

Post by Patrice » Thu Oct 14, 2021 5:26 am

Hi all,

thank you for all these informations.
I experienced some problems by using sdcard driver.
The display and sdcard initialisation are OK but as soon as I try to mount the sdcard with

Code: Select all

sd = sdcard.SDCard(lv_spi.SPI(), machine.Pin(4))
, I have this error :

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV
I have the same error even if I initialise only the sdcard.
The SDCard works with the native micropython machine.SDCard driver.
An idea regarding this error code ?

Thanks

favnec5
Posts: 3
Joined: Mon Oct 04, 2021 1:42 pm

Re: LVGL and SD Card won't share SPI bus

Post by favnec5 » Thu Oct 14, 2021 7:24 am

Hi Patrice,
Before use lv_spi (just used to share the SPI Bus between screen and sdcard), you can try the sdcard.py driver with native machine.SPI without screen :

Code: Select all

import machine, sdcard, os
    sd = sdcard.SDCard(machine.SPI(1), machine.Pin(YOUR_SD_CARD_CS_PIN))
    os.mount(sd, '/sd')
    print(os.listdir('/'))
If it work, next step is sharing the bus with lv_spi.

Few comments :
- lv_spi.py use espidf module of ESP32 only (check if module exists)
- My 2GB SD card don't work with sdcard.py (i dont know why). I use 4GB without problem.
- Check if YOUR_SD_CARD_CS_PIN is the good
- To share the SPI bus, Screen driver need to be init first with half_duplex=False

Best Reguards,
Thomas

Post Reply