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:
  1. // Pin configuration for ST7789
  2. #define ST7789_MISO_PIN -1  // Not used
  3. #define ST7789_MOSI_PIN 11  // Master Out Slave In pin
  4. #define ST7789_CLK_PIN  12  // Clock pin
  5. #define ST7789_DC_PIN   2   // Data/Command pin
  6. #define ST7789_RST_PIN  6   // Reset pin
  7. #define ST7789_BCKL_PIN 1   // Backlight pin
  8.  
  9. esp_err_t display_init(void) {
  10.     esp_err_t ret;
  11.  
  12.     // Configuration for the SPI bus
  13.     spi_bus_config_t buscfg = {
  14.         .miso_io_num = ST7789_MISO_PIN,
  15.         .mosi_io_num = ST7789_MOSI_PIN,
  16.         .sclk_io_num = ST7789_CLK_PIN,
  17.         .quadwp_io_num = -1,
  18.         .quadhd_io_num = -1,
  19.         .max_transfer_sz = 16 * 320 * 2 + 8
  20.     };
  21.  
  22.     ESP_LOGI(TAG, "Initializing SPI bus...");
  23.     ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
  24.     if (ret != ESP_OK) {
  25.         ESP_LOGE(TAG, "Failed to initialize SPI bus: %s", esp_err_to_name(ret));
  26.         return ret;
  27.     }
  28.     ESP_LOGI(TAG, "SPI bus initialized successfully");
  29.  
  30.     // Configuration for the SPI device on the bus
  31.     spi_device_interface_config_t devcfg = {
  32.         .clock_speed_hz = 1 * 1000 * 1000, // Clock out at 10 MHz
  33.         .mode = 3, // SPI mode 3
  34.         .spics_io_num = 42, // No CS pin
  35.         .queue_size = 7,
  36.         .flags = SPI_DEVICE_NO_DUMMY,        // No dummy phase
  37.         .pre_cb = NULL,
  38.     };
  39.  
  40.     ESP_LOGI(TAG, "Adding device to SPI bus...");
  41.     ret = spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
  42.     if (ret != ESP_OK) {
  43.         ESP_LOGE(TAG, "Failed to add device to SPI bus: %s", esp_err_to_name(ret));
  44.         return ret;
  45.     }
  46.     ESP_LOGI(TAG, "Device added to SPI bus successfully");