Page 1 of 1

ESP-IDF SPI not working but equivilant code working with arduino IDE code

Posted: Mon May 19, 2025 6:19 am
by dill_bot
I have a ESP32-S3-DevkitC-1 and am trying to get spi running in master mode with spi pins assigned to the following GPIOs

#define PIN_NUM_MISO GPIO_NUM_9
#define PIN_NUM_MOSI GPIO_NUM_12
#define PIN_NUM_SCLK GPIO_NUM_11
#define PIN_NUM_nSCS GPIO_NUM_10

I have got this running with a simple arduino script (confirmed via oscilliscope) but my ESP-IDF implementation isnt working.

relevant code

Code: Select all

//SPI
#define PIN_NUM_MISO        GPIO_NUM_9//13//11//9
#define PIN_NUM_MOSI        GPIO_NUM_12//12//13//12
#define PIN_NUM_SCLK        GPIO_NUM_11//15//12//11
#define PIN_NUM_nSCS        GPIO_NUM_10//14//10//10
#define HOST SPI2_HOST
spi_device_handle_t spi;

void spi_setup(){
    
    spi_bus_config_t spi_bus_config = {
        .miso_io_num = PIN_NUM_MISO,
        .mosi_io_num = PIN_NUM_MOSI,
        .sclk_io_num = PIN_NUM_SCLK,
        .flags = SPICOMMON_BUSFLAG_MASTER, | SPICOMMON_BUSFLAG_GPIO_PINS,
        .max_transfer_sz = 32,
    };

    if(ESP_OK == spi_bus_initialize(HOST, &spi_bus_config, SPI_DMA_DISABLED)){
    }
    else{
    }

    spi_device_interface_config_t dev_config = {
        .clock_speed_hz = 1000000,
        .mode = 0,
        .spics_io_num = PIN_NUM_nSCS,
        .queue_size = 8,
        .flags = 0
        //.flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_GPIO_PINS
    };
   
    if(ESP_OK == spi_bus_add_device(HOST, &dev_config, &spi)){
    }
    else{
    }

}

spi_transaction_t spi_trans;
    memset(&spi_trans, 0, sizeof(spi_trans));
    spi_trans.cmd = 0;
    spi_trans.addr = 0;
    spi_trans.length = 8;
    spi_trans.tx_data[0] = payload1;
    spi_trans.tx_buffer = &testdata;
    spi_trans.user = (void*)0;
    //spi_trans.flags = SPI_TR;

    if(ESP_OK == spi_device_acquire_bus(spi, portMAX_DELAY)){
    }
    else{
    }


    if(ESP_OK == spi_device_transmit(spi, &spi_trans)){
    }
    else{}

    spi_device_release_bus(spi);

i'm not seeing any output on any of the GPIOs, appreciate any ideas

Re: ESP-IDF SPI not working but equivilant code working with arduino IDE code

Posted: Tue May 20, 2025 4:51 am
by dill_bot
Solved the the issue
Needed to add the following flags to the bus config to get SPI working on desired IO pins

Code: Select all

.flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_GPIO_PINS | SPICOMMON_BUSFLAG_SCLK | SPICOMMON_BUSFLAG_MOSI | SPICOMMON_BUSFLAG_MISO