Answer the question
In order to leave comments, you need to log in
ESP32, SPI-Slave, where are the "extra" bytes from?
Good afternoon. I'm trying to connect stm32 and esp32 via SPI. Stm32 as master, ESP as slave.
From the master I send in 8-bit format. The goal is to receive a byte on the ESP32, respond with a byte back.
Since the exchange is 1 byte, I do not use DMA.
As an example, I took the code from ESP-IDF, (esp-idf/examples/peripherals/spi_slave/receiver).
I simplified it a bit, removed the receive buffers, as a result it turned out:
#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++;
}
}
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
Answer the question
In order to leave comments, you need to log in
Che something I suspect that it is necessary to clear the reception queue. Because here they write:
"Receive an item from a queue without removing the item from the queue...."
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question