LVGL and SD Card won't share SPI bus

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Sun May 17, 2020 6:25 am

PM-TPI wrote:
Sat May 16, 2020 11:57 am
...
I believe most failures are due to SD Cards that do not release the SPI bus back to the cpu...
It is the case that SD cards don't release the bus immediately. The CS signal is clocked into the chip by sck. So when CS goes False, the bus is not released until 8 sck edges have occurred.

The official driver takes this into account and, in this respect, does not require change.

I found another problem where the bus is shared, which can be demonstrated very easily. If you mount and run an SD card, then write 0 to the bus (with CS False) the SD interface fails on next access. This simulates shared access. The solution is to modify the SD card driver so that, prior to access, it writes 0xff to the bus with CS False. In my application this works. Unfortunately despite requests nobody has yet explained why.
PM-TPI wrote:
Sat May 16, 2020 11:57 am
...
When I saw your explanation Peter I thought your method was going to work.
it's possible I could still be implementing it incorrectly.
It's just a drop-in replacement for the official driver.

However, reading this thread it's clear that there are other problems with this application. I suggest you guys use my version, at least until you've debugged the other issues, because the issue I found could muddy the waters. You might then revert back to the official version to see if the problem I identified affects you.
Peter Hinch
Index to my micropython libraries.

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 » Sun May 17, 2020 3:03 pm

fstengel wrote:
Sat May 16, 2020 11:59 am
I have used @Mike Teachman's solution (and the derived code I found in that repo https://github.com/sci-bots/lv_binding_ ... fed5fac8a2). It works.
Please elaborate on what, files were used and edits made.

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 » Wed May 20, 2020 5:03 pm

Mike Teachman wrote:
Sat May 16, 2020 12:20 am
I faced the same problem and found a workaround by modifying the C based ILI9341 display driver,
I build with your modILI9341.c file.
SDcard and LVGL work, but button colors are rainbow striped.

Code: Select all

import machine
import uos
import lvgl as lv
import lvesp32

uos.mount(machine.SDCard(slot=3, sck=18, mosi=23, miso=19, cs=4,), '/sd')
print(uos.listdir('/sd/data'))

lv.init()

# from ili9341 import ili9341
# disp = ili9341(spihost=1, miso=19, mosi=23, clk=18, cs=14, dc=27, rst=33, backlight=32, backlight_on=1, mhz=40)

import ILI9341
disp = ILI9341.display(clk=18, cs=14, dc=27, rst=33, backlight=32, mhz=25, share=ILI9341.SHARED)
disp.init()

disp_buf1 = lv.disp_buf_t()
buf1_1 = bytearray(320*10)
lv.disp_buf_init(disp_buf1,buf1_1, None, len(buf1_1)//4)
disp_drv = lv.disp_drv_t()
lv.disp_drv_init(disp_drv)
disp_drv.buffer = disp_buf1
disp_drv.flush_cb = disp.flush
disp_drv.hor_res = 320
disp_drv.ver_res = 240
disp_drv.rotated = 0
lv.disp_drv_register(disp_drv)

#SCREEN----------------------------------------
scr = lv.obj()
btn = lv.btn(scr)
btn.align(lv.scr_act(), lv.ALIGN.CENTER, 0, 0)
label = lv.label(btn)
label.set_text("Button")
lv.scr_load(scr)

Have you encountered this?
The Pure/Hybrid driver works fine except for SPI issue
Attachments
2020-05-20 12_50_14-DSC_1373.JPG - Windows Photo Viewer.png
2020-05-20 12_50_14-DSC_1373.JPG - Windows Photo Viewer.png (132.53 KiB) Viewed 5628 times

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 » Wed May 20, 2020 9:01 pm

I tried out your posted code and unfortunately I don't see the striped button.
Capture (Phone) (Custom).PNG
Capture (Phone) (Custom).PNG (147.28 KiB) Viewed 5617 times
One suggestion is to try setting the background color of the button to see if it changes. Here is a screenshot from my display and the code I added to your example to set the background color and a border.
WP_20200520_007 (Custom).jpg
WP_20200520_007 (Custom).jpg (58.9 KiB) Viewed 5617 times

Code: Select all

#SCREEN----------------------------------------
scr = lv.obj()
btn = lv.btn(scr)

# set background color
buttonstyle = lv.style_t(lv.style_plain)
buttonstyle.body.main_color = lv.color_make(0xFF, 0xA5, 0x00)
buttonstyle.body.grad_color = lv.color_make(0xFF, 0xA5, 0x00)
buttonstyle.body.border.color = lv.color_hex(0xe32a19)
buttonstyle.body.border.width = 5
btn.set_style(lv.table.STYLE.BG, buttonstyle)

btn.align(lv.scr_act(), lv.ALIGN.CENTER, 0, 0)
label = lv.label(btn)
label.set_text("Button")
lv.scr_load(scr)

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 » Wed May 20, 2020 9:27 pm

still bad colors...
Attachments
button2.png
button2.png (238.43 KiB) Viewed 5613 times

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 » Wed May 20, 2020 10:34 pm

Well, at least it changed. Perhaps some other customization is needed to see correct colors? I looked at my commits and there is a color related change in the config file lv_conf.h (change color depth to 16 from 32). I can not recall why I made this change, but maybe compare against your setting? Googling ARGB8888 leads to wikipedia, https://en.wikipedia.org/wiki/RGBA_color_model, where that rainbow pattern is shown. Hopeful !

/* Color depth:
* - 1: 1 byte per pixel
* - 8: RGB233
* - 16: RGB565
* - 32: ARGB8888
*/
#ifndef LV_COLOR_DEPTH
#define LV_COLOR_DEPTH 16
#endif

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 » Thu May 21, 2020 2:47 pm

yea there all set correctly.

I'd like to compare build info...
I used IDF v4.0 with git checkout 463a9d8b7f9af8205222b80707f9bdbba7c530e1
boot shows...
MicroPython v1.12-277-g6b32fae73-dirty on 2020-05-21; ESP32 module (spiram) with ESP32
My dev board is a wemos Lolin D32 Pro v2 (esp32 WROVER-B 16mb flash).

Is -DLV_COLOR_16_SWAP=1 implemented in modILI9341.c?
In ili9341.py...
colormode = COLOR_MODE_BGRCOLOR_= const(0x08)
rot = LANDSCAPE = MADCTL_MV = const(0x20)
so..
{'cmd': 0x36, 'data': bytes([rot | colormode])}, # Memory Access Control
or...
{'cmd': 0x36, 'data': bytes([0x20| 0x08])}, # Memory Access Control

in modILI9341.c...
{0x36, {0x28}, 1}, /*Memory Access Control - sets Landscape format*/

Do you think this BGRCOLOR could be the issue?

This Pure/Hybrid ili9341.py repo at
https://github.com/sci-bots/lv_binding_ ... fed5fac8a2
He attempted to use your fixes but it just locks up mcu/repl ? on run

Have you tried to apply you fix to ili9341.py?
Last edited by PM-TPI on Thu May 21, 2020 4:09 pm, edited 2 times 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 » Thu May 21, 2020 3:39 pm

I re-built with original lvgl..
and color problem is in modILI9341.c driver but not in Pure/Hybrid ili9341.py driver...

so its not your code Mike.
How current is you build?

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 » Thu May 21, 2020 7:14 pm

-
SEE...
but that commit is not included yet in Micropython
(at least not on the last release v1.12, which lv_micropython is aligned to).


is this true?

from https://github.com/lvgl/lv_binding_micr ... /issues/69
Did you see this issue: https://github.com/espressif/esp-idf/issues/1597 ?

Due to the limited amount of IO pins on ESP32, it is desirable to share a SPI bus used for SD cards with other SPI devices such as a LCD display. This is currently not possible with esp-idf since the sdspi_host driver assumes the SD card will be the only device on the SPI bus.

This problem was fixed in https://github.com/espressif/esp-idf/co ... 216f2583ff, but that commit is not included yet in Micropython (at least not on the last release v1.12, which lv_micropython is aligned to).
If you want, you can try building lv_micropython with a more up to date esp-idf that includes this fix, but you'll probably need to change the SD driver to use the new sdspi_host_init_device, sdspi_host_remove_device functions.
amirgon of LittlevGL claims if micropython implemented
espressif SPI bus sharing with SDSPI has been added in
https://github.com/espressif/esp-idf/co ... 216f2583ff
that this would solve the LVGL and SDcard SPI issue

anyone?

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 May 23, 2020 3:03 pm

PM-TPI wrote:
Thu May 21, 2020 2:47 pm
Have you tried to apply you fix to ili9341.py?

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".
PM-TPI wrote:
Thu May 21, 2020 3:39 pm
How current is you build?
For MicroPython, the last rebase to the master was done on Feb 18, 2020:
branch: https://github.com/miketeachman/micropy ... -littlevgl

For littlevgl, the last rebase to the master was done on Sept 28, 2019:
branch: https://github.com/miketeachman/lv_bind ... treetsense

Post Reply