I have got XIAO ESP32S3 and ESP32-WROOM32 units. I have got exactly the same problem on both. SPI works on Arduino but it does not on ESP-IDF. I use ESP-IDF 5.1.2.
I setup SPI by using code below. Later I send 4 bytes
- //On XIAO I tried
- //gpio_iomux_in(GPIO_NUM_9, SPI3_D_IN_IDX);
- //gpio_iomux_out(GPIO_NUM_8, SPI3_Q_OUT_IDX, false);
- //gpio_iomux_in(GPIO_NUM_7, SPI3_CLK_IN_IDX);
- //gpio_iomux_in(GPIO_NUM_1, SPI3_CS0_IN_IDX);
- // and this
- //gpio_iomux_out(GPIO_NUM_9, SPI3_D_OUT_IDX, false);
- //gpio_iomux_in(GPIO_NUM_8, SPI3_Q_IN_IDX);
- //gpio_iomux_out(GPIO_NUM_7, SPI3_CLK_OUT_IDX, false);
- //gpio_iomux_out(GPIO_NUM_1, SPI3_CS0_IN_IDX,false);
- spi_bus_config_t Config;
- Config.mosi_io_num = GPIO_NUM_23;//GPIO_NUM_9;//GPIO_NUM_13;//
- Config.miso_io_num = GPIO_NUM_19;//GPIO_NUM_8;//GPIO_NUM_12;//
- Config.sclk_io_num = GPIO_NUM_18;//GPIO_NUM_7;//GPIO_NUM_14;//;
- //Config.mosi_io_num = GPIO_NUM_9;
- //Config.miso_io_num = GPIO_NUM_8;
- //Config.sclk_io_num = GPIO_NUM_7;
- Config.max_transfer_sz = 0;
- Config.quadwp_io_num = -1; // -1 not used
- Config.quadhd_io_num = -1; // -1 not used
- Config.intr_flags = 0;
- Config.data0_io_num = -1;
- Config.data1_io_num = -1;
- Config.data2_io_num = -1;
- Config.data3_io_num = -1;
- Config.data4_io_num = -1;
- Config.data5_io_num = -1;
- Config.data6_io_num = -1;
- Config.data7_io_num = -1;
- //Tried with SPICOMMON_BUSFLAG_NATIVE_PINS, SPICOMMON_BUSFLAG_NATIVE_PINS, SPICOMMON_BUSFLAG_GPIO_PINS
- Config.flags = SPICOMMON_BUSFLAG_MASTER;// | SPICOMMON_BUSFLAG_NATIVE_PINS;// | SPICOMMON_BUSFLAG_MISO | SPICOMMON_BUSFLAG_MOSI;
- Config.isr_cpu_id = INTR_CPU_ID_0;
- if(ESP_OK == spi_bus_initialize(SPI3_HOST, &Config, SPI_DMA_CH_AUTO))//SPI_DMA_DISABLED);//SPI_DMA_CH_AUTO);
- {
- printf("-----OK-----");
- }
- //spicommon_bus_initialize_io
- spi_device_interface_config_t ConfigInterface;
- ConfigInterface.command_bits = 0;
- ConfigInterface.address_bits = 0;
- ConfigInterface.dummy_bits = 0;
- ConfigInterface.mode = 0;
- ConfigInterface.duty_cycle_pos = 128; // default 128 = 50%/50% duty
- ConfigInterface.cs_ena_pretrans = 0; // 0 not used
- ConfigInterface.cs_ena_posttrans = 0; // 0 not used
- ConfigInterface.clock_speed_hz = 500000;
- ConfigInterface.clock_source = SPI_CLK_SRC_APB; //SPI_CLK_SRC_DEFAULT
- ConfigInterface.spics_io_num = GPIO_NUM_5;
- ConfigInterface.flags = 0;//SPI_DEVICE_HALFDUPLEX | SPI_DEVICE_NO_DUMMY; //| SPI_DEVICE_HALFDUPLEX;
- ConfigInterface.queue_size = 1;
- ConfigInterface.pre_cb = NULL;
- ConfigInterface.post_cb = NULL;
- ConfigInterface.input_delay_ns = 0;
- if(ESP_OK == spi_bus_add_device(SPI3_HOST, &ConfigInterface, &SPIHandle))
- {
- printf("-----OK-----");
- }
I receive wrong data, always the same bytes no matter what 4 bytes are sent. Screenshot from logic analyzer:
When I use Arduino
- #include <Arduino.h>
- #include <SPI.h>
- #define VSPI_MISO 19
- #define VSPI_MOSI 23
- #define VSPI_SCLK 18
- #define VSPI_SS 5
- SPIClass * vspi = NULL;
- void setup()
- {
- printf("Start");
- pinMode(VSPI_SS, OUTPUT);
- vspi = new SPIClass(2);
- vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); //SCLK, MISO, MOSI, SS
- }
- void loop()
- {
- //uint16_t addressToRead = 0x0740;
- uint16_t addressToRead = 0x08E7;
- vspi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
- delay(10);
- digitalWrite(VSPI_SS, 0);
- vspi->transfer(0x0D);
- vspi->transfer16(addressToRead);
- vspi->transfer(0x38);
- digitalWrite(VSPI_SS, 1);
- vspi->endTransaction();
- delay(2000);
- vspi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
- delay(10);
- addressToRead = 0x08E7;
- digitalWrite(VSPI_SS, 0);
- vspi->transfer(0x1D);
- vspi->transfer16(addressToRead);
- vspi->transfer(0x00);
- uint8_t regValue = vspi->transfer(0x00);
- digitalWrite(VSPI_SS, 1);
- vspi->endTransaction();
- printf("Reg %u", regValue);
- delay(2000);
- printf("Stop\n");
- }
Everything is correct. I can write and read data without problems. Screenshot from logic analyzer below. Everything is fine.
When I start program on Arduino later on ESP-IDF you can clearly see differences on logic analyzer
Hardware connection is fine because it works on Arduino. It is something wrong with configuration on ESP-IDF (I have spent lots of time trying different configurations). It is something incorrect with PINS or MOSI or CLOCK or ... . Behavior is the same on XIAO ESP32s3 and ESP32 units ( I reconfigure pins of course ). Tried VSPI and HSPI.
Did someone have similar issue?