ports/esp32:a bug with Hardware SPI Initialize on ESP32-S3
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))

- 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))

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.
Possible bug with Hardware SPI(1) on ESP32-S3
Hi all.
I'm using the ESP32-S3-WROOM-1 (N8R2 - 8MB Flash and 2 MB RAM), specifically this kit: ESP32-S3-DEVKITC-1-N8R2
The hardware SPI is working just on the SPI(2). The SPI(1) do not works.
As I know, the ESP32-S3, like as the ESP32 has two free Hardware SPI to use (SPI 1 and SPI 2). It has 4 in total, but two SPI (SPI 3 and SPI 4) are used by the RAM/FLASH. So, should works Hardware SPI number 1 and number 2, right?
Follow the tests with the ESP32-S3:
>>> import os
>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.18.0', version='v1.18 on 2022-05-05', machine='ESP32S3 module (spiram) with ESP32S3')
>>>
>>> from machine import SPI, Pin
>>>
>>> spi = SPI(1, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: invalid configuration
>>>
>>> spi = SPI(2, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
>>>
>>> spi = SPI(3, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: SPI(3) doesn't exist
>>>
>>> spi = SPI(4, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: SPI(4) doesn't exist
>>>
Follow below the same test with the ESP32:
>>> import os
>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.18.0', version='v1.18 on 2022-01-17', machine='ESP32 module with ESP32')
>>>
>>> from machine import SPI, Pin
>>> spi = SPI(1, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
>>>
>>> spi = SPI(2, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
>>>
>>> spi = SPI(3, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: SPI(3) doesn't exist
>>> spi = SPI(4, baudrate=5_000_000, phase=1, sck=Pin(15), mosi=Pin(16), miso=Pin(17))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: SPI(4) doesn't exist
>>>
I missed some configuration or is really a BUG?
Thank you.