Change SPI speed on the fly?

DFL1973
Posts: 2
Joined: Sun Jul 26, 2020 10:30 pm

Change SPI speed on the fly?

Postby DFL1973 » Sun Jul 26, 2020 10:40 pm

Is it possible to change the clock speed of the SPI after initializing the interface?

Something like: write to the first device at 80Mhz, change to 40Mhz, write to a second device, then change back to 80Mhz...

Thanks!

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

Re: Change SPI speed on the fly?

Postby ESP_Sprite » Mon Jul 27, 2020 10:01 am

The ESP-IDF way would be to initialize a bus and add the two devices to it; you can specify different speeds for each device and the driver will automatically handle the speed switch.

DFL1973
Posts: 2
Joined: Sun Jul 26, 2020 10:30 pm

Re: Change SPI speed on the fly?

Postby DFL1973 » Mon Jul 27, 2020 12:12 pm

Thanks for the quick reply.

As I understood:

Code: Select all

void setupSPI()
{
    esp_err_t ret;

    spi_bus_config_t buscfg={
        .miso_io_num=PIN_NUM_MISO,
        .mosi_io_num=PIN_NUM_MOSI,
        .sclk_io_num=PIN_NUM_CLK,
        .quadwp_io_num=-1,
        .quadhd_io_num=-1,
        .max_transfer_sz=PARALLEL_LINES*320*2+8
    };
    
    spi_device_interface_config_t devcfg40=
    {

        .clock_speed_hz=40*1000*1000,           //Clock out at 40 MHz ok (25ns)
        .mode=0,                                //SPI mode 0
        .spics_io_num=-1,         //CS software controlled
        .queue_size=1,                          //Transactions at a time
        .pre_cb=NULL,
    };

    spi_device_interface_config_t devcfg80=
    {
        .clock_speed_hz=80*1000*1000,           //Clock out at 80 MHz ok (12.5ns)
        .mode=0,                                //SPI mode 0
        .spics_io_num=-1,          //CS software controlled
        .queue_size=1,                          //Transactions at a time
        .pre_cb=NULL, 
    };

    //Initialize the SPI bus
    ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
    ESP_ERROR_CHECK(ret);

    //Attach the SPI bus (80mhz)
    ret=spi_bus_add_device(HSPI_HOST, &devcfg80, &spi);
    ESP_ERROR_CHECK(ret);

    //Attach the SPI bus (40Mhz)
    ret=spi_bus_add_device(HSPI_HOST, &devcfg40, &spi);
    ESP_ERROR_CHECK(ret);
}
If I attach the 80Mhz first, it always uses the 40Mhz or, If I attach the 40Mhz first it´s always 80Mhz. How to point one or another at the transaction?

[EDIT] I just figured out, I need to create two handles.

Code: Select all

spi_device_handle_t spi40;
spi_device_handle_t spi80;
It´s working, thanks for your help!

Who is online

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