[SPI Slave] lost rx_buffer data after spi_slave_transmit
Posted: 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.
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!
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++;
}
Who can tell me the reason?
Thank you!