← index #2509Issue #7663
Related · high · value 1.415
QUERY · ISSUE

machine.SPI(): add CS/NSS pin

openby dmartauzopened 2016-10-12updated 2018-11-20

It would be nice if CS/NSS pin for SPI could be driven by machine.SPI() itself. Manual toggling in user application slows it down.

CANDIDATE · ISSUE

ESP32 SPI - request support for hardware-managed devices

openby oclykeopened 2021-08-14updated 2022-06-21
port-esp32

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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied