-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Support for hardware SD/MMC access on ESP32 #4772
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
Conversation
hardware SD/MMC host controller.
Added documentation in the esp32 module for the SDCard class. Added more thorough checks on paramter values. Moved the SDCard class from a module of its own into the esp32 module (a la the pyb.SDCard() class).
Thank you! I had actually started a similar implementation myself, done this week, which is working but not nearly as comprehensive as what is done here. So I'll forget about mine :) From a brief look, it looks good. One thing to discuss is where to put the
It'd be great to make this generic documentation for all boards/ports that have an
I agree, it's a common feature to have an SD card slot, and the standard esp32 firmware is supposed to be able to run on all esp32 based boards. |
I was wondering into which module I should put this. When I first wrote it I put it in its own Either way, this is a capability that is going to exist on lots of different platforms so a single common place to put it would be good. If we go that route we should make sure that the parameters are common across all implementations as far as possible. I think that the parameters that I have here are probably the superset of what we will find elsewhere but I'm open to suggestions. |
Looking through the other ports it seems that the CC3200 port currently contains an SD Card driver that wraps up TI's driver included with the FatFS code. As if to highlight the need for consistency, this class is in the It seems that there are three related questions here:
On the signature front, we are talking about the same interface every time but different platforms and implementations have different degrees of flexibility. My inclination would be to require all implementations to:
If we get to that level of consistency in the driver initialisation process then I would be very happy to see this class land in a standard, platform-neutral location. Without consistency I'd be inclined to keep it in a platform-specific module. If we do end up putting it in a neutral location then my inclination would be to put it in |
It may have been called
Going by other things like To state the obvious, at the very least the sd = SDCard(...) # perhaps port-specific initialisation
uos.mount(sd, '/sd') The cc3200 already works this way.
In the end I don't think it really matters. Construction/initialisation/configuration is always going to be board/port specific. But if multiple ports will have this class it does make sense to put it somewhere common.
It doesn't seem right to me to put it in But I wouldn't actually advocate having an SDIO/MMC bus in Consider external SPI flash, which is logically very similar to an SD card: a driver for this wouldn't go in the In summary: I don't think
I agree with most of these points. Except that, if the underlying interface is a SPI port, it should accept a |
That would certainly make the construction cleaner in those cases. Do you ses supporting I am happy to move this to a new |
I see, I didn't realise it's this complicated. It seems that the SPI interface used for the SD card is actually SD-SPI, so not really
Well, maybe we can come up with a better module name. But at least it has scope for |
Irrespective of the complexity of implementation (which is my problem, no the users'), I think that the down side to passing in an As for the new module, if it were called |
Yes, true, the implementation should not directly dictate the API. But in this case the SDCard is not really using the SPI bus itself, it's just using the particular pin configuration of a SPI bus. As such I would agree with your original proposal to just let the user specify the pins directly to SDCard.
Yes, it seems attractive to put mount/umount/VfsFat/etc in such a |
To make progress here, how about we just put it in the |
Moved the SDCard class back to the machine module. Added documentation for the SDCard class for esp32 and stm32.
OK. I've moved the class back to the |
Thanks, it looks pretty good. I will squash all the commits when merging. I guess the changes to |
Yes. My mistake. Now that I've written then generic documentation that file should have no changes. I'll fix that. |
Ok, I squashed this into 2 separate commits (implementation and docs) and merged it in 8e3af7d and 6077d17 Thanks very much for your efforts! Re MMC: stm32 does support both SD card and MMC and they are actually different classes, so the docs would eventually need to be updated to reflect that fact (or combine them into a single SDCard class...). Since esp32 doesn't (yet) support MMC it's not clear how the API for MMC on esp32 would look at this stage. |
Thanks, and glad to be of service. Regarding MMC support, as far as I can see from the documentation the API for handling this on the ESP32 is expected to be the same as for SD cards. They two interfaces share the same logical protocol and only differ at the hardware level. The code here may need either new options or new slot numbers to enable MMC support just to tell the hardware which type of hardware is using the (shared) pins but otherwise it should look just the same. As a result I would lean towards a single class rather than a separate MMC class. |
The do share the same physical pins, so the difference lies between the physical IO and the high-level interface. On stm32 SD and MMC are two completely different HAL layers, although these layers do have a lot in common. I'm not sure how to distinguish between an SD card and MMC connected to the SD/MMC bus, so in stm32 there is currently
That does make sense (to have a single class) since the bus is the same it's just the mode that is different (SD vs MMC). In that case I would really like to rename |
Thank you very much for this, I was looking for something like this before. I'm hoping I can use this to serve a small spa webpage using micropython and javascript |
[resolves micropython#4771] DEBUG UART supported on ATMSAME5x
esp32/machine_sdcard.c: Invalid pin numbers are allowed. #8112 |
This PR implements support for the ESP32's hardware SD/MMC interface. This provides much, much faster access to SD cards. It also implements support for accessing SD cards over an SPI interface using DMA access and the ESP-IDF implementation of the SD/MMC protocol written in C.
The class constructor is designed to have sensible defaults. As a result, on ESP32 modules where the SD Card slot is wired to the usually-free SD/MMC hardware channel, mounting a card is as simple as:
Using the native implementations of the SD card protocol provides a radical speed-up compared to the soft
sdcard.py
module. On a TTGO 8 card read speeds of a FAT32 file mounted through VFS are between 25 and 30 times faster (depending on block size) using the hardware SD/MMC interface compared the software SD protocol with software SPI and 12 to 17 times faster than software SD with hardware SPI drivers, exceeding 750K bytes per second. Using the C implementation of the SD card protocol over SPI is typically 12 times faster compared to hardware SPI and the Python SD card protocol. Write speeds are typically 5 to 8 times faster and can exceed 250K bytes per second.The new
SDCard
class is added to the existingesp32
module (since theSDCard
class for the PyBoard resides in thepyb
module). Its inclusion is conditional on theMICROPY_HW_ENABLE_SDCARD
macro being set to 1 inmpconfigport.h
. The new code adds about 24K bytes to the ROM size but only 192 bytes to the RAM consumption on devices without SPI RAM and zero impact on devices with SPI RAM (I'm not quite sure why it even takes the 192 bytes, since all static variables areconst
so should be landing in the ROM too). Given that most ESP32 devices are not short of ROM space I would recommend having support switched on by default and the PR includes the appropriate setting inmpconfigport.h
.The PR includes documentation of the new class as part of the ESP32-specific library documentation.