Change SPI configuration in runtime

Massimo87
Posts: 3
Joined: Mon Mar 21, 2022 2:19 pm

Change SPI configuration in runtime

Postby Massimo87 » Tue May 23, 2023 7:47 am

Hi all,
I've a system configured like in attached figure.
The ESP32 is SPI MASTER and exchange data (small packets of 64bit) every 2mS with MCU1 (SPI SLAVE).
Through the handshake line, the slave informs the master when it is ready to receive data.
My problem is that the MCU1 must write data to the DAC (for debug purpose) every 100uS, so i thought of doing this:

1) ESP32: Wait 2ms and rising edge of handshakeline
2) ESP32: Set SPI MASTER and exchange data with MCU1
3) ESP32: Free SPI bus
4) MCU1: Set SPI MASTER and write to DAC
5) MCU1: Set SPI SLAVE
Repeat from point1....



I tried to do this by modifying the IDF 5.0.1 sample code:

Code: Select all

/*
This ISR is called when the handshake line goes high.
*/
static void IRAM_ATTR gpio_handshake_isr_handler(void* arg)
{
    //Sometimes due to interference or ringing or something, we get two irqs after eachother. This is solved by
    //looking at the time between interrupts and refusing any interrupt too close to another one.
    static uint32_t lasthandshaketime_us;
    uint32_t currtime_us = esp_timer_get_time();
    uint32_t diff = currtime_us - lasthandshaketime_us;
    if (diff < 1000) {
        return; //ignore everything <1ms after an earlier irq
    }
    lasthandshaketime_us = currtime_us;

    //Give the semaphore.
    BaseType_t mustYield = false;
    xSemaphoreGiveFromISR(rdySem, &mustYield);
    if (mustYield)
    {
        portYIELD_FROM_ISR();
    }
}



void app_main(void)
{
    esp_err_t ret;
    spi_device_handle_t handle;

    //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 device on the other side of the bus
    spi_device_interface_config_t devcfg={
        .command_bits=0,
        .address_bits=0,
        .dummy_bits=0,
        //.clock_speed_hz=5000000,
		//.clock_speed_hz=1000000,
		.clock_speed_hz=2000000,	//2Mhz
        .duty_cycle_pos=128,        //50% duty cycle
        //.mode=0,
		.mode=3,
        .spics_io_num=GPIO_CS,
        .cs_ena_posttrans=3,        //Keep the CS low 3 cycles after transaction, to stop slave from missing the last bit when CS has less propagation delay than CLK
        .queue_size=3
    };


    //GPIO config for the handshake line.
    gpio_config_t io_conf={
        .intr_type=GPIO_INTR_POSEDGE,
        .mode=GPIO_MODE_INPUT,
        .pull_up_en=1,
		//.pin_bit_mask=(1<<GPIO_HANDSHAKE)
        .pin_bit_mask=(1ULL<<GPIO_HANDSHAKE)
    };

    int n=0;
    char sendbuf[8] = {0};
    char recvbuf[8] = {0};
    spi_transaction_t t;
    memset(&t, 0, sizeof(t));

    //Create the semaphore.
    rdySem=xSemaphoreCreateBinary();

    //Set up handshake line interrupt.
    gpio_config(&io_conf);
    gpio_install_isr_service(0);
    gpio_set_intr_type(GPIO_HANDSHAKE, GPIO_INTR_POSEDGE);
    gpio_isr_handler_add(GPIO_HANDSHAKE, gpio_handshake_isr_handler, NULL);


    //Assume the slave is ready for the first transmission: if the slave started up before us, we will not detect
    //positive edge on the handshake line.
    xSemaphoreGive(rdySem);


    while (1)
    {
		//Initialize the SPI bus and add the device we want to send stuff to.
		ret=spi_bus_initialize(SENDER_HOST, &buscfg, SPI_DMA_CH_AUTO);
		assert(ret==ESP_OK);
		ret=spi_bus_add_device(SENDER_HOST, &devcfg, &handle);
		assert(ret==ESP_OK);

        sendbuf[0]=1;
        sendbuf[1]=2;
        sendbuf[2]=3;
        sendbuf[3]=4;
        sendbuf[4]=5;
        sendbuf[5]=6;
        sendbuf[6]=7;
        sendbuf[7]=8;


        t.length=sizeof(sendbuf)*8;
        t.tx_buffer=sendbuf;
        t.rx_buffer=recvbuf;
        //Wait for slave to be ready for next byte before sending
        xSemaphoreTake(rdySem, portMAX_DELAY); //Wait until slave is ready
        ret=spi_device_transmit(handle, &t);
        printf("Received: %s\n", recvbuf);
        n++;

		spi_bus_remove_device(handle);
		spi_bus_free(SENDER_HOST);
    }
}

I simply moved the spi initialization inside the while loop, and i added the spi_bus_remove() and spi_bus_free() after the data is exchanged with the slave.
The problem is that i can't trasnsimt data every 2mS becouse it seems that the initialization procedure take a longer time (or somthing else, i don't know...).
Is there other ways to do that (for example change only te gpio function)...?

Thanks,
Massimo.
Attachments
SPI diagram.png
SPI diagram.png (6.92 KiB) Viewed 1415 times

MicroController
Posts: 1709
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Change SPI configuration in runtime

Postby MicroController » Tue May 23, 2023 6:17 pm

or somthing else, i don't know...
Well then how about logging some time measurements to see if/where the time is lost?
What's the frequency of the handshake line toggling?
Is there other ways to do that (for example change only te gpio function)...?
Yes, you should be able to disable and re-enable the GPIO pins without tearing down and re-initializing the SPI driver.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 67 guests