SPI with two external boards on ESP32-C6

Timmwardion
Posts: 5
Joined: Wed Nov 29, 2023 1:59 am

SPI with two external boards on ESP32-C6

Postby Timmwardion » Wed Nov 29, 2023 2:31 am

Hi all,

I'm struggling with connecting two ESP32 boards (sensors) onto a master receiver (ESP32-C6).

The documentation here: https://docs.espressif.com/projects/esp ... aster.html

On the C6, SPI0 and SPI1 are used internally to access the ESP32-C6’s attached flash memory. But the documentation says that "SPI2 is a general purpose SPI controller. It has an independent signal bus with the same name. The bus has 6 CS lines to drive up to 6 SPI slaves."

Great!

My question is - how can I initialize two SPI slaves here when only one host is available?

Here is a minimal version of my code:

Code: Select all

#define RCV_HOST    SPI2_HOST
#define GPIO_HANDSHAKE 15
#define GPIO_MOSI 7
#define GPIO_MISO 2
#define GPIO_SCLK 6
#define GPIO_CS1 16
#define GPIO_CS2 17


    //Configuration for the SPI bus
    spi_bus_config_t buscfg={
        .mosi_io_num=GPIO_MOSI,
        .miso_io_num=GPIO_MISO,
        .sclk_io_num=GPIO_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
    };

    //Configuration for the SPI slave interface for device 1
    spi_slave_interface_config_t slvcfg_A={
        .mode=0,
        .spics_io_num=GPIO_CS1,
        .queue_size=3,
        .flags=0,
        .post_setup_cb=my_post_setup_cb,
        .post_trans_cb=my_post_trans_cb
    };

    //Configuration for the SPI slave interface for device 2
    spi_slave_interface_config_t slvcfg_B={
        .mode=0,
        .spics_io_num=GPIO_CS2,
        .queue_size=3,
        .flags=0,
        .post_setup_cb=my_post_setup_cb,
        .post_trans_cb=my_post_trans_cb
    };

//I think I nee to initialize like this but it fails on the second line with the error:
// "E (337) spi: SPI2 already claimed by spi slave.
// E (337) spi_slave: spi_slave_initialize(151): host already in use"

ret_A=spi_slave_initialize(RCV_HOST, &buscfg, &slvcfg_A, SPI_DMA_CH_AUTO);
ret_B=spi_slave_initialize(RCV_HOST, &buscfg, &slvcfg_B, SPI_DMA_CH_AUTO);

ESP_Sprite
Posts: 9772
Joined: Thu Nov 26, 2015 4:08 am

Re: SPI with two external boards on ESP32-C6

Postby ESP_Sprite » Thu Nov 30, 2023 2:58 am

spi_slave_initialize tries to initialize the SPI host as an SPI slave. If you want to configure it as an SPI master (controlling multiple SPI slaves), you'd use spi_bus_add_device().

Who is online

Users browsing this forum: Bing [Bot] and 91 guests