Page 1 of 1

SPI mode 3 on ESP32S3 problem

Posted: Thu Jul 11, 2024 1:28 pm
by Quadglide
I have some problem setting SPI mode 3 on my ESP32S3. I did find some info about solutions and how to fix it. I tried it but it did not solve the problem. CPOL=1 and CPHA =1 for mode 0. But it is stuck on CPHA = 0. Seems to have been an issue for many years. It works in the Arduino framework but not ESP-IF. (I tried 5.1, 5.2 and 5.3 and same issue)

My code below:

Code: Untitled.c Select all


// Pin configuration for ST7789
#define ST7789_MISO_PIN -1 // Not used
#define ST7789_MOSI_PIN 11 // Master Out Slave In pin
#define ST7789_CLK_PIN 12 // Clock pin
#define ST7789_DC_PIN 2 // Data/Command pin
#define ST7789_RST_PIN 6 // Reset pin
#define ST7789_BCKL_PIN 1 // Backlight pin

esp_err_t display_init(void) {
esp_err_t ret;

// Configuration for the SPI bus
spi_bus_config_t buscfg = {
.miso_io_num = ST7789_MISO_PIN,
.mosi_io_num = ST7789_MOSI_PIN,
.sclk_io_num = ST7789_CLK_PIN,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 16 * 320 * 2 + 8
};

ESP_LOGI(TAG, "Initializing SPI bus...");
ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize SPI bus: %s", esp_err_to_name(ret));
return ret;
}
ESP_LOGI(TAG, "SPI bus initialized successfully");

// Configuration for the SPI device on the bus
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 1 * 1000 * 1000, // Clock out at 10 MHz
.mode = 3, // SPI mode 3
.spics_io_num = 42, // No CS pin
.queue_size = 7,
.flags = SPI_DEVICE_NO_DUMMY, // No dummy phase
.pre_cb = NULL,
};

ESP_LOGI(TAG, "Adding device to SPI bus...");
ret = spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to add device to SPI bus: %s", esp_err_to_name(ret));
return ret;
}
ESP_LOGI(TAG, "Device added to SPI bus successfully");