ESP32 SPI - request support for hardware-managed devices
The ESP32 SPI peripherals support limited management of attached devices in their abstraction layer (I am not sure whether this is a software feature of their HAL or a hardware capability of the actual peripheral though).
The hardware SPI interface common to microcontroller boards in MicroPython does not support this capability - and neither should it. However it would be advantageous to add an additional API which allows ESP32 users to take advantage of these features for both speed and convenience.
Additional capabilities:
- hardware management of CS lines
- independent clock frequency settings
- independent phase and polarity settings
Proposed API:
# import from esp32 module because these features are ESP32 specific
from esp32 import SPIHost, SPIDev
# construct a SPIHost for the peripheral you want to use.
# similar to machine.SPI syntax except that device-specific properties are removed
vspi = SPIHost(2, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
# add devices to the host with independent settings
# optionally allow HAL-controlled cs pin
dev1 = SPIDev(vspi, baudrate=80000000, cs=Pin(25), polarity=0, phase=0, bits=8, firstbit=0)
dev2 = SPIDev(vspi, baudrate=5000000, bits=8)
# note: the esp-idf drivers allow many (untested total) spi devices, but only 3 per host may
# use a hw controlled cs pin. one may use a Pin to select additional spi devices
# do transactions on devices
devn.write(b'12345') # write 5 bytes on MOSI
devn.read(10) # read 10 bytes on MISO
This is also a different solution to #6340 (as opposed to calling spi.init(baudrate) to reconfigure)
I'd be willing to make a PR for this. Please let me know thoughts.
Support for hardware SD/MMC access on ESP32
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:
uos.mount(esp32.SDCard(), "/sd")
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 existing esp32 module (since the SDCard class for the PyBoard resides in the pyb module). Its inclusion is conditional on the MICROPY_HW_ENABLE_SDCARD macro being set to 1 in mpconfigport.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 are const 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 in mpconfigport.h.
The PR includes documentation of the new class as part of the ESP32-specific library documentation.