SPI Slave, extra character

john_bul
Posts: 1
Joined: Wed Jan 09, 2019 9:09 am

SPI Slave, extra character

Postby john_bul » Wed Jan 09, 2019 9:37 am

Hello.
I am trying to link esp32 with stm32 by SPI.
Stm32 is a master, ESP32 is a slave.
I transfer from the master by 1 byte. On the slave accept. Since the transfer is small, I do not use DMA.
As an example, I used a SPI slave from the examples (example/esp-idf/examples/peripherals/spi_slave/receiver), slightly simplified.
Transmission occurs, but for some reason I see 2 extra bytes from the ESP side.
ESP32 code:

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "driver/spi_slave.h"
#include "esp_spi_flash.h"

#define GPIO_MOSI 13
#define GPIO_MISO 12
#define GPIO_SCLK 14
#define GPIO_CS 15

uint8_t n=0;
uint8_t recvbuf=0;
QueueHandle_t spi_data;

void my_post_trans_cb(spi_slave_transaction_t *trans) {
    xQueueSend(spi_data,&recvbuf,0);
}

void print_task(void *pvParameter)
{
    uint8_t data=0;
    while(1) {
        xQueueReceive(spi_data,(void *)&data,portMAX_DELAY);
        printf("Send (n): %d Received: %d\n", n, data);
    }
}

//Main application
void app_main()
{
    spi_slave_transaction_t t;

    spi_bus_config_t buscfg={
        .mosi_io_num=GPIO_MOSI,
        .miso_io_num=GPIO_MISO,
        .sclk_io_num=GPIO_SCLK
    };

    spi_slave_interface_config_t slvcfg={
        .mode=0,
        .spics_io_num=GPIO_CS,
        .queue_size=1,
        .post_trans_cb=my_post_trans_cb,
        .flags=0
    };

    spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, 0);
    spi_data=xQueueCreate(1,sizeof(uint8_t));
    xTaskCreate(&print_task, "print_task", 8*1024, NULL, 5, NULL);
 
    while(1) {
        t.length=8;
        t.trans_len=8;
        t.tx_buffer=&n;
        t.rx_buffer=&recvbuf;
        spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);
        n++;
    }
}
My changes: the queue parameter in the spi_slave_interface_config_t is 1, and the DMA parameter in spi_slave_initialize is zero.
Output:

Code: Select all

Send (n): 218 Received: 46
Send (n): 219 Received: 46
Send (n): 220 Received: 46
Send (n): 221 Received: 49
Send (n): 222 Received: 49
Send (n): 223 Received: 49
Send (n): 224 Received: 52
Send (n): 225 Received: 52
Send (n): 226 Received: 52
Stm32 simply sends a uint8_t value, increasing it each time by 3. It is a "Received" value at ESP32 side.
ESP32 send "n" value ("Send (n)" at output) as a reply, increasing it each time by 1 at spi_slave_transmit) call.
From the stm32 side, I can see the data that esp32 sends. This is only 1 byte out of 3.
For example (from the beginning of the output), stm32 sends a byte equal to 46, receives 218 in response. Then stm32 sends a byte equal to 49, receives 221 in response. Bytes with a value of 219 and 200 are only from the ESP32 side, they are not transmitted. Where do they come from? And how to do it right?

Return to “ESP32 IDF”

Who is online

Users browsing this forum: -Rudy- and 18 guests