sukesh-ak / ESP32-LVGL8x-SDSPI

ESP32 / ESP32-S3 with LVGL widgets using LovyanGFX + SD SPI working with multiple display size support
MIT License
39 stars 8 forks source link

SD card support #2

Closed dizcza closed 1 year ago

dizcza commented 1 year ago

The SD card support https://github.com/sukesh-ak/ESP32-LVGL8x-SDSPI/blob/master/main/helper_storage.hpp seems not finished: it lacks SPI initialization (spi_bus_initialize) prior mounting an SD card with SPI interface.

I (1425) lvgl_gui: Initializing SD card
I (1425) lvgl_gui: Mounting filesystem
E (1425) spi_master: spi_master_init_driver(202): host_id not initialized
E (1435) vfs_fat_sdmmc: Failed to attach sdspi device onto an SPI bus (rc=0x103), please initialize the bus first and check the device parameters.
E (1445) vfs_fat_sdmmc: slot init failed (0x103).
E (1455) lvgl_gui: Failed to initialize the card (ESP_ERR_INVALID_STATE). Make sure SD card lines have pull-up resistors in place.

But I also don't know how to wire an external SD card slot to WT32-SC01 with SPI. Since it should be shared with the TFT LCD, I first used

TFT_SCLK=14
TFT_CS=15
TFT_MOSI=13
TFT_MISO=-1

but then I noticed #define PIN_NUM_CS GPIO_NUM_33 in helper_storage.hpp. So I need to know the correct MISO and CS pins to finish SD card wiring. And get SPI initialization fixed, of course.

I'm using an HW-125 SD card slot, not an expansion board.

sukesh-ak commented 1 year ago

@dizcza The sample uses shared SPI so helper_storage.hpp needs only CS pin. LovyanGFX initialize SPI already so again initialising is not required.

By weekend will update the sample with more clarity in comments on this.

sukesh-ak commented 1 year ago

@dizcza Also make sure to add this in your config file for enabling SD support

define SD_ENABLED

dizcza commented 1 year ago

SD card requires a defined MISO pin, not -1.

dizcza commented 1 year ago

See https://github.com/lovyan03/LovyanGFX/blob/9de99d25496413f133ce3c057fa3edb60148f523/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32.hpp#L1717

sukesh-ak commented 1 year ago

@dizcza For using shared SPI bus you need to enable bus_shared = true in LovyanGFX. Once that's done you don't need to initialize the bus again.

https://github.com/sukesh-ak/ESP32-LVGL8x-SDSPI/blob/79db50c751f95922505254782518a930a10e4bf6/main/conf_FeatherS3_ILI9341.h#L86

After that if you add the following, SD card related code will be taken care of in helper_storage.hpp. You only need CS pin since SPI bus is initialized by LovyanGFX by default.

#define SD_ENABLED

Check conf_FeatherS3_ILI9341.h for example configuration.

dizcza commented 1 year ago

For using shared SPI bus you need to enable bus_shared = true in LovyanGFX. Once that's done you don't need to initialize the bus again.

The bus is shared by default in LGFX. I'm not initializing the bus again.

Have you tried connecting an external SD card slot to one of these boards? In particular, to WT32-SC01? I guess not, because

  1. you have SPI2_HOST in helper storage code while LGFX uses SPI3_HOST by default.
  2. SD SPI card cannot function without a specified MISO pin.
dizcza commented 1 year ago

The only way I got your example running was after I

  1. changed SPI2_HOST to SPI3_HOST
  2. connected SD card MISO pin to one of the board pins, say 32
  3. Set LGFX MISO pin to 32 https://github.com/lovyan03/LovyanGFX/blob/9de99d25496413f133ce3c057fa3edb60148f523/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32.hpp#L1717
sukesh-ak commented 1 year ago

That means SD card is not on the same (shared) bus as the display.

Sample is using shared bus for both display and SD Card.

sukesh-ak commented 1 year ago

This is the working sample config using shared bus

https://github.com/sukesh-ak/ESP32-LVGL8x-SDSPI/blob/master/main/conf_FeatherS3_ILI9341.h

sukesh-ak commented 1 year ago

The only way I got your example running was after I

  1. changed SPI2_HOST to SPI3_HOST
  2. connected SD card MISO pin to one of the board pins, say 32
  3. Set LGFX MISO pin to 32 https://github.com/lovyan03/LovyanGFX/blob/9de99d25496413f133ce3c057fa3edb60148f523/src/lgfx/v1_autodetect/LGFX_AutoDetect_ESP32.hpp#L1717

Didn't realize you were connecting external SD card to WT32-SC01. Will take a look for that scenario.

dizcza commented 1 year ago

conf_FeatherS3_ILI9341.h would work had I had the board. Because there you initialize everything manually rather than relying on LGFX autodetect functionality. In particular, you set a valid pin number for MISO, not -1. In other words, the connection is full-duplex.

In WT-SC01, however, the default SPI bus (in LGFX implementation) is half-duplex unless you forget about autodetect and write a custom script as you do with FeatherS3.

I don't know if an SD card can run in half-duplex. If I just set MISO pin to -1 in spi_bus_config_t, it fails to initialize.

I'm using HW-125 adapter

sukesh-ak commented 1 year ago

conf_FeatherS3_ILI9341.h would work had I had the board. Because there you initialize everything manually rather than relying on LGFX autodetect functionality. In particular, you set a valid pin number for MISO, not -1. In other words, the connection is full-duplex.

In WT-SC01, however, the default SPI bus (in LGFX implementation) is half-duplex unless you forget about autodetect and write a custom script as you do with FeatherS3.

I don't know if an SD card can run in half-duplex. If I just set MISO pin to -1 in spi_bus_config_t, it fails to initialize.

I'm using HW-125 adapter

Understood.

A new sample for WT32-SC01 & WT32-SC01 Plus is being cooked. Will take care of this issue in that sample by not using autodetect, so we can provide MISO there.

sukesh-ak commented 1 year ago

@dizcza Here is the new sample specially for WT32-SC01 and the new Plus version which has ESP32-S3 + SD Card.

https://github.com/sukesh-ak/WT32-SC01-PLUS-LVGL-IDF

dizcza commented 1 year ago

It works. But I'd like to see these changes in the template repository it has been generated from, as I suppose I'll be supporting the main repository, not the offspring. In particular, I see you've switched to lvgl v9 there. I don't need helper storage code since I'll be using my sdcard component anyway.

Once you backport lvgl v9 config to the template repository, I may come up with a PR to address some compilation warnings and other things I encounter.

sukesh-ak commented 1 year ago

@dizcza https://github.com/sukesh-ak/WT32-SC01-PLUS-LVGL-IDF

I have marked that repo also as a template. Other repo is a mix of different screens which also needs a lot of conditional compiler options.

Also the UI is not configured for Portrait/Landscape auto scaling.

I am planning to add several features to this one, since this has TFT and CTP. So this repo will support devices with different resolution with CTP only.

Few things I am going to add are

dizcza commented 1 year ago

Shouldn't https://github.com/sukesh-ak/ESP32-LVGL8x-SDSPI/blob/5d631101ce779991ecf40e700ce0d4ecdfc06561/CMakeLists.txt#L17 be -Imain?

sukesh-ak commented 1 year ago

Like I mentioned earlier, I have moved on so not sure. It was working then.

ESP32-TUX is the latest version and now it uses ESP-IDF 5.0 This supports 4 different devices now and more to come.

https://github.com/sukesh-ak/ESP32-TUX