New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support SD-SPI bus sharing. (IDFGH-160) #1597
Comments
This seems to be pretty hard because SPI protocol of SD cards requires CS to be low for the duration of SD transaction. Single SD transaction can consist of multiple SPI transactions. Because of this, SDSPI driver controls CS in software. If the SPI bus is shared, then nothing prevents another task from sending a transaction to a different device in between two parts of SD card transaction. However, since SDSPI driver keeps CS asserted, this extra transaction will be received by the card as well. If in your use case the SD card and another device are only used sequentially, then the workaround you mention will work. But in the general case of multiple tasks accessing multiple SPI devices on the same bus concurrently, it will likely not work. |
I would guess there are also other drivers/apps where it is desirable to have deterministic SPI bus use: guaranteeing a group of transactions complete sequentially on the bus without interruption. As a starting point, I propose the following:
With the above, all current code continues to work and exclusive use of the SPI device is transparent. |
@OtherCrashOverride how does it work for you both (SD & Display) on the same bus. i tried doing it but SD card always fails. I have a display which comes with the SD card, I have been trying to make display and SD work on the same bus but so far I have no luck. @OtherCrashOverride would you mind sharing your SD card configuration please. I am curious. Thank you. |
All the "magic" required was stated in my opening post. You need to patch the sdspi_host.c as stated and ensure you have the same duplex setting (full) for LCD and SD card. If you are using an "Arduino compatible" SPI LCD interface that includes the SD card port, you need to replace the board's SPI resistors with 0Ohm for 3.3V operation. |
Could there at least be a Kconfig option to ignore the "return" mentioned above? That way a developer can implement their own locking mechanism if they need the bus for other devices. |
@OtherCrashOverride I have modified esp-idf according to your suggestion, but cannot properly configure LCD and SPI SD. Could you please give me your configuration method for LCD and SPI SDCard? |
@Abol-Wang The LCD initialization is here: The SD card initialization is here: [edit] |
Thank's in advance! The ESP32 EVB works whit SD_MMC 1 bit protocol, and need only 3 lines, 02 DAT0, 14 CLK, 15 SD_CMD, and SPI whit default whit, 02 MISO, 14 CLK, 13 MOSI, and 17 CS. I tried to initialize before and end after every use but doesn't work. The pin MISO and MOSI are attached to the SPI or the SD and, for me, it's impossible to do. Anyone knows how use the SD_MMC and SPI simultaneously? |
I'm facing the same problem of not being able to share the SPI bus with an SD card using the SD SPI driver.
I already tried using a semaphore to control the access to the SPI bus. It does not work, the writing operation fails with the same error. I have even made some small changes to the sdspi driver to use the function spi_device_acquire_bus and spi_device_release_bus along to try to guarantee that there are no pending transactions on the bus before being used again. It doesn't work either. After @igrr 's first response to this thread I thought that a simple mutex would solve this problem but obviously it's not that easy. Is there any way to share the SPI bus with an SD card? |
Hi, Has this issue been fixed? Thanks |
If you initialize the SD card first the bus will then be available and you can add peripherals without any IDF code changes.
Robert Vogt IV
CEO
IOSiX, LLC
1300 Tefft Ct #1
Saline, MI 48176
P: +1-855-OBD-1939
C: +1-734-730-9690
robert@iosix.com
… On Nov 13, 2019, at 2:14 AM, Satyan Raj ***@***.***> wrote:
Hi,
Has this issue been fixed?
We are facing a similar problem, as because of the limited number of IO we decided to share same set of SPI signals with the SD card (SPI) and other SPI devices.
Thanks
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
What IDF version are you using? Because in my previous tests (witn an oldish version (mid april) of the main branch) initializing the SD card first did not make any difference. The problem was visible when the second device started using the bus (there was no problem when the second device was added to the bus). |
I had a hard time making SD card + TFT display work on the same SPI bus. It
would work for some time but then display would get stuck or SD card would
stop logging. I tried using the mutex/semaphore to control the CS line but
the dma contrl logic somehow did not like it. I ended up putting SD card on
a separate SPI bus.
--
Amar Dhore
…--
Amar
On Wed, Nov 20, 2019 at 11:11 AM Alfonso H ***@***.***> wrote:
If you initialize the SD card first the bus will then be available and you
can add peripherals without any IDF code changes. Robert Vogt IV CEO IOSiX,
LLC 1300 Tefft Ct #1 <#1>
Saline, MI 48176 P: +1-855-OBD-1939 C: +1-734-730-9690 ***@***.***
… <#m_7106927704071673194_>
On Nov 13, 2019, at 2:14 AM, Satyan Raj *@*.***> wrote: Hi, Has this
issue been fixed? We are facing a similar problem, as because of the
limited number of IO we decided to share same set of SPI signals with the
SD card (SPI) and other SPI devices. Thanks — You are receiving this
because you are subscribed to this thread. Reply to this email directly,
view it on GitHub, or unsubscribe.
What IDF version are you using? Because in my previous tests (witn an
oldish version (mid april) of the main branch) initializing the SD card
first did not make any difference. The problem was visible when the second
device started using the bus (there was no problem when the second device
was added to the bus).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1597?email_source=notifications&email_token=AFTF7M2OQ6OKAOGE4RVSFZLQUVOURA5CNFSM4EPYVEF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEESRGUA#issuecomment-556077904>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFTF7MYGI6K6BDRSLDYZG2DQUVOURANCNFSM4EPYVEFQ>
.
--
Amar Dhore
|
SPI bus sharing with SDSPI has been added in 067f3d2 — please see the new |
Hi, Thank you very much. |
Hi @zavovi, there has been a number of changes between v3.3 and v4.1... Best way to browse them is to start with git log. Assuming you are in the esp-idf directory:
|
Hi @igrr , thank you. Do you know something more about SPI driver? I have already right used xSemaphoreTake and xSemaphoreGive for all tasks, where I call the SPI transactions. But it seems, that when I call some functions from FatFs, it seems, that it is processed outside the call. For example, I call this:
I get error code 0x107, it is timeout. But when I disable my TFT driver, it is working properly. It seems, that something is processed outside my call. Or I don't understand, where is the difference from v3.3. Thank you. |
Hello, is SPI and SDSPI sharing working in esp-idf release v4.0 ? |
Hi all, I am sorry for long delay, but I still not solved my issue. I am really struggling with it and I am tired. Lot of month and it looks, that it is working, but not! My code:
(This behavior is only, when the same semaphore is used for I2C too. When not, then BAD SD not working for all - read/write and others). I don't understand. I tried disable all others tasks. The TFT display is still ok. The issue LOG in point 4:
The BAD SD card sdmmc_card_print_info:
The GOOD SD card sdmmc_card_print_info:
Please, could you help me? I am solving this issue lot of months and I am really tired from it :-( Please help!!! Thank you very much. |
Hi @zavovi , Due to the strange behavior of SD cards over SPI bus, you need to initialize the devices on the same bus in the following sequence strictly:
This is because, the SD card will respond to the data stream (even without CS active) of other devices when it's just powered up and not in the SPI mode. But to put SD card into SPI mode, you have to avoid conflicts from other SPI slaves first. (or you can try pullups on all CS wires). |
Hi @edouardreg , |
Hi @ginkgm , Here is part of my init code:
The initialization is OK for all my SD cards. The
Then I read the text file, and it is OK. Write is OK too. I am handling CS line for TFT by GPIO in each transaction by |
Here is example of my make directory function:
Here are transfer function for TFT (called both):
|
Hi @zavovi , The sdspi driver will control the CS at proper time, you don't need to manually controls it's CS. This depends on a bus acquiring feature (https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/spi_master.html#_CPPv422spi_device_acquire_bus19spi_device_handle_t10TickType_t) introduced in IDF v.3.2. This will also forbids other devices from using that bus. (https://github.com/espressif/esp-idf/blob/master/components/driver/sdspi_host.c#L455 introduced in v4.2). Maybe your manual control breaks the GPIO logic of SDSPI? |
Hi @ginkgm , I had the CS lines controlled from sdspi with my GOOD SD card, but now, when I am solving other my cards, I am trying all my ideas... |
Ok, I changed the CS handling back to the SPI driver and leave the SD init the first, it working same. Only read/write on BAD SD cards. |
Hello @ginkgm , I don't understand, why it is not working, the FreeRTOS semaphore should block right, or not? It seems, that mkdir take more time than read/write. Any other suggestions please? |
@zavovi i had the same problem and it turned out to be the dma access. Try this: assign dma channel 1 to tft and dma channel to 2 sd card and see if it make any difference. Good luck. |
Hello @amardhore , |
so sd init is working ok? how long is your file name? -- |
Hello @amardhore , I have enabled long names and maximum was increased to 60. SD initialization is good in all my tests (shared DMA, not shared DMA, etc...). I am initializing the SD card first. Thank you, |
Can you provide a demo code that coexists with SD card and LCD on the sharing SPI bus? |
@Alvin1Zhang: Looks like people are still struggling SPI bus issue issue with SD card. |
@zavovi Can you please share the sample code, for our reference. |
Hello @satyanraj, Thank you. |
sure |
Esp-idf 4.2 finally added shared SPI bus support to the SD card driver and it seems more stable than retro-go's lock system. Our lock system can't easily be fixed (many others seem to have tried and failed), so I decided to drop support for it. See: espressif/esp-idf#1597
Esp-idf 4.2 finally added shared SPI bus support to the SD card driver and it seems more stable than retro-go's lock system. Our lock system can't easily be fixed (many others seem to have tried and failed), so I decided to drop support for it. See: espressif/esp-idf#1597
Esp-idf 4.2 finally added shared SPI bus support to the SD card driver and it seems more stable than retro-go's lock system. Our lock system can't easily be fixed (many others seem to have tried and failed), so I decided to drop support for it. See: espressif/esp-idf#1597
Esp-idf 4.2 finally added shared SPI bus support to the SD card driver and it seems more stable than retro-go's lock system. Our lock system can't easily be fixed (many others seem to have tried and failed), so I decided to drop support for it. See: espressif/esp-idf#1597
Esp-idf 4.2 finally added shared SPI bus support to the SD card driver and it seems more stable than retro-go's lock system. Our lock system can't easily be fixed (many others seem to have tried and failed), so I decided to drop support for it. See: espressif/esp-idf#1597
Esp-idf 4.2 finally added shared SPI bus support to the SD card driver and it seems more stable than retro-go's lock system. Our lock system can't easily be fixed (many others seem to have tried and failed), so I decided to drop support for it. See: espressif/esp-idf#1597
I am writing this so that this does not happen to anyone else. I was able to read and write on the SD using VFS but some CRC errors appeared when initializing other SPI devices in the same bus such as: E (12401) sdmmc_cmd: sdmmc_read_sectors_dma: sdmmc_send_cmd returned 0x107 I could also see that when the SD card was inserted, the SPI communications somehow collided with the other SPI devices on the same bus. It was as if the SD always answered to SPI although its #CS pin was not asserted. This happened although the SD card was previously mounted as SPI. The issue is now solved. The problem was the SD card. I was using a random chinatown SD card, changing the SD card did the trick for me. |
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.
As a "workaround", I have commented out the following line:
esp-idf/components/driver/sdspi_host.c
Line 279 in 16de6bf
The above allows the SD card to share the bus with another SPI device. I have tested that both devices operate correctly when properly configured (#1080).
I would like to see SD-SPI bus sharing officially supported. An API revision will likely be required. The purpose of this post is to begin a conversation towards support.
The text was updated successfully, but these errors were encountered: