Recently when using ESP32 to drive an SPI device, the SPI device did not answer, strange when I used stm32 successfully driven the SPI device, how to transplant to ESP32 is not possible.
When using Stm32 I use SPI mode3, i.e. cpol=1, Cpha=1, refer to the generic SPI mode definition:
SPI four mode SPI phase (CPHA) and polarity (Cpol) can be 0 or 1 respectively, the corresponding 4 combinations constitute the SPI 4 modes (mode) mode
0 cpol=0, cpha=0
mode 1 cpol=0, cpha=1
Mode 2 cpol=1, cpha=0
Mode 3 cpol=1, cpha=1
clock polarity Cpol: When the SPI is idle, the clock signal sclk the level (1: Idle high level; 0: Idle low)
clock phase cpha: That is, the SPI starts sampling at the edge of the SCLK (0: The first edge begins; 1: Start of the second edge)
The timing diagram is as follows:
In the esp32 above I should also use MODE3, finally I changed ESP32 SPI mode =spi_mode2 when the Protocol analyzer obtained the data is correct, all kind of Let me doubt life. Isn't the standard specification wrong.
Spi.begin (sck, miso, Mosi, CS);
Spi.begintransaction (Spisettings (500000, Msbfirst, Spi_mode2));
Or ESP32 is not identified by the generic specification, view ESP32 's underlying driver file discovery is true:
Source file name: ESP32-HAL-SPI.C
void Spisetdatamode (spi_t * SPI, uint8_t datamode)
{
if (!SPI) {
return;
}
Spi_mutex_lock ();
Switch (datamode) {case
spi_mode1:
spi->dev->pin.ck_idle_edge = 0;
Spi->dev->user.ck_out_edge = 1;
break;
Case SPI_MODE2:
spi->dev->pin.ck_idle_edge = 1;
Spi->dev->user.ck_out_edge = 1;
break;
Case SPI_MODE3:
spi->dev->pin.ck_idle_edge = 1;
Spi->dev->user.ck_out_edge = 0;
break;
Case SPI_MODE0:
default:
spi->dev->pin.ck_idle_edge = 0;
Spi->dev->user.ck_out_edge = 0;
break;
}
Spi_mutex_unlock ();
}
The original ESP32 is to use "Spi_mode2" to represent:
Cpol=1, Cpha=1
At this point, it seems sometimes necessary to inquisitive to get the truth.
Resources:
"1" SPI four modes of difference
"2" Linux SPI Driver Development Learning (iii)-----SPI_BITBANG.C detailed
"3" GitHub source file: Arduino-esp32/cores/esp32/esp32-hal-spi.c