← index #10239Issue #15181
Related · high · value 1.637
QUERY · ISSUE

ports/esp32:a bug with Hardware SPI Initialize on ESP32-S3

openby EngineerWillopened 2022-12-16updated 2022-12-17
bugport-esp32

Hi,all:

Problem description and analysis

  • Hardware: ESP32S3
  • Firmware version: esp32/GENERIC_S3 v1.19.1 (2022-06-18) or master branch
  • Abstract: SPI(1) and SPI(2) need two initializations to work perfectly in each operation. If two initializations of different Baudrates are not carried out, SPI may not work.

code and pictures

  • Initialize with different baudrates()
self.spi = SPI(2,baudrate=4_000_000,polarity=0, phase=0,sck=Pin(SCK,Pin.OUT),mosi=Pin(MOSI,Pin.OUT))
self.spi = SPI(2,baudrate=8_000_000,polarity=0, phase=0,sck=Pin(SCK,Pin.OUT),mosi=Pin(MOSI,Pin.OUT))

image

  • Initialize with same baudrates(In order to reproduce the problem 100%, you also can initialize it once and run it again)
self.spi = SPI(2,baudrate=8_000_000,polarity=0, phase=0,sck=Pin(SCK,Pin.OUT),mosi=Pin(MOSI,Pin.OUT))
self.spi = SPI(2,baudrate=8_000_000,polarity=0, phase=0,sck=Pin(SCK,Pin.OUT),mosi=Pin(MOSI,Pin.OUT))

image

details

First time

I used the firmware V1.91.1 and found that SPI (1) initialization was abnormal. SPI(2) can work normally.
However, when I tried for the second time (after stop), I found that it could not work.
I tried to restart it and it could work again, but it still couldn't work the second time.
Then I tried various methods and accidentally found that baudrate could work after modification. So I conducted two initializations,The baudrate needs of the first initialization were different from that of the second initialization,The program could run perfectly, no matter whether it was STOP or not.

  • like
self.spi = SPI(2,baudrate=4_000_000,polarity=0, phase=0,sck=Pin(SCK,Pin.OUT),mosi=Pin(MOSI,Pin.OUT))
self.spi = SPI(2,baudrate=8_000_000,polarity=0, phase=0,sck=Pin(SCK,Pin.OUT),mosi=Pin(MOSI,Pin.OUT))

I found out that lssues #8634 suffers from a similar problem and saw the initialization issue solved in #9498

The second time

I tried to use the master branch to build a new firmware to try,
and then I found that SPI(1) and SPI(2) can be initialized normally and work, but the problem still exists that the initialization needs to be twice.

CANDIDATE · ISSUE

ESP32: Unable to use SDCard and any other SPI device in the same bus

closedby kdschlosseropened 2024-06-01updated 2025-03-03
bug

Port, board and/or hardware

ESP32

MicroPython version

1.22.2

Reproduction

If what I am seeing in the code is correct there is no way to use SPI SDCard with any other SPI device. This is due to the code in both the SDCard class wanting to initialize the SPI bus without checking the error output to see if the SPI bus has been initialized. In the SPI class the error is checked specifically for the error for the bus being initialized but still goes on and raises an exception for that error.

The other issue is the inability to set the frequency based on the device that is attached to the bus. The ESP-IDF has a separation of the "bus" and the "device" for this specific reason. This allows devices running at different speeds to be used on the same bus. It also allows devices to have different settings as well. With how it is currently written is causes a lot of limitations in what is able to be done and it forces the use of additional pins when it shouldn't be necessary.

Expected behaviour

To be able to use SPI SDCard on the same bus as other SPI devices.

Observed behaviour

Exception occurs for the bus already being active.

Additional Information

It would be an ideal arrangement is a single "bus" object could be created and that bus object would then get passed to the different things that use SPI, That Bus object would need to be declared in a header file where it can be included for use elsewhere. From the looks of it it should be something that can be done across most other ports without to much of an issue. The only one that would be a little more of a pain would be the STM32's and this is due to no easy way to change the bitrate without having to reinitialize the SPI driver each time a device needs to be communicated with but it is still able to be done.

In my use case the board is something that is bought with the ESP32 already on board and the SDCard, LCD panel and touch panel are all on the same bus. It is also quite common to buy boards that don't have an MCU but have an SDCard and LCD panel that are attached to the same SPI bus. It is an impossibility to use these kinds of boards with MicroPython due to how the SPI is currently done in MicroPython.

Code of Conduct

Yes, I agree

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