[SPI Slave] lost rx_buffer data after spi_slave_transmit

paul719
Posts: 3
Joined: Fri Jul 06, 2018 4:45 am

[SPI Slave] lost rx_buffer data after spi_slave_transmit

Postby paul719 » Mon Jul 09, 2018 9:06 am

we set esp32 as a SPI Slave Device, and call spi_slave_transmit to receive data. But we found the receive buffer lost last 2~3 bytes after each successful transmit.
I just modified the example project in ESP32-IDF\examples\peripherals\spi_slave\receiver for receiving hex data from master device.

Code: Select all

void app_main()
{
    int n=0;
    esp_err_t ret;

    //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
    };

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

    //Configuration for the handshake line
    gpio_config_t io_conf={
        .intr_type=GPIO_INTR_DISABLE,
        .mode=GPIO_MODE_OUTPUT,
        .pin_bit_mask=(1<<GPIO_HANDSHAKE)
    };

    //Configure handshake line as output
    gpio_config(&io_conf);
    //Enable pull-ups on SPI lines so we don't detect rogue pulses when no master is connected.
    gpio_set_pull_mode(GPIO_MOSI, GPIO_PULLUP_ONLY);
    gpio_set_pull_mode(GPIO_SCLK, GPIO_PULLUP_ONLY);
    gpio_set_pull_mode(GPIO_CS, GPIO_PULLUP_ONLY);

    //Initialize SPI slave interface
    ret=spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, 1);
    assert(ret==ESP_OK);

    char sendbuf[129]="";
    char recvbuf[129]="";
    memset(recvbuf, 0, 33);
    spi_slave_transaction_t t;
    memset(&t, 0, sizeof(t));

    while(1) {
        //Clear receive buffer, set send buffer to something sane
        memset(recvbuf, 0xA5, 129);
		memset(sendbuf, 0x55, 129);
		
        //sprintf(sendbuf, "This is the receiver, sending data for transmission number %04d.", n);

        //Set up a transaction of 128 bytes to send/receive
        t.length=128*8;
        t.tx_buffer=sendbuf;
        t.rx_buffer=recvbuf;

        ret=spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);

        ESP_LOG_BUFFER_HEX("SlaveReceived", recvbuf, t.trans_len/8);
        n++;
    }
recv.png
Actually,the master send 38 byte data, just like AA 55 AA 55...... finally, it lost last 3 bytes in recvbuf, but t.trans_len is correct.
Who can tell me the reason?
Thank you!

paul719
Posts: 3
Joined: Fri Jul 06, 2018 4:45 am

Re: [SPI Slave] lost rx_buffer data after spi_slave_transmit

Postby paul719 » Tue Jul 10, 2018 8:23 am

the master gives a good MOSI signal as shown in Fig:
mosi.png
mosi.png (212.3 KiB) Viewed 5661 times
after spi_slave_transmit, the rx buffer data is shown througt debug uart.
uart.png
uart.png (5.97 KiB) Viewed 5661 times
The last 2 byte in rx buffer is 0xA5 which is the initial value, but the correct value should be 0x03 0xA3.

squonk11
Posts: 69
Joined: Wed Mar 01, 2017 6:53 pm
Location: Germany

Re: [SPI Slave] lost rx_buffer data after spi_slave_transmit

Postby squonk11 » Wed Jan 09, 2019 7:40 pm

@paul719 : did you find a solution for this problem? I am having the same problem. Is there some workaround?
"Whoever believes to be someone has stopped becoming someone"
Sokrates

Carlos Leite
Posts: 1
Joined: Fri Nov 08, 2019 4:04 pm

Re: [SPI Slave] lost rx_buffer data after spi_slave_transmit

Postby Carlos Leite » Fri Nov 08, 2019 4:17 pm

I have the same situation here. My workaroud is to send extra bytes to ESP.
Every data sent to ESP used to be like this:

[0x7F, SZ, SZ, <data>, CRC]

where 0x7F is a start delimiter
SZ, SZ is the <data> size in bytes

CRC = CRC-8

Now, my package looks like this

[0x7F, SZ, SZ, <data>, CRC, 0x00, 0x00, 0x00, 0x00, 0x00]

In this case, the two or three bytes lost are not part of the real message.
This solution is far from ideal, but works.

Who is online

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