Page 1 of 1

Unwanted delay before and after SPI transaction

Posted: Wed Nov 15, 2023 6:04 pm
by rezarethink
Image

I toggle the CS low, do an SPI transfer, then toggle the CS high. The time between the CS going low and the transfer is 3.24us and 3.7us after the transfer is done and the CS goes high. This really slows down the transfer time. What's causing this delay and is there some way to reduce it?

Re: Unwanted delay before and after SPI transaction

Posted: Thu Nov 16, 2023 3:17 am
by username
Is there a specific reason your manually toggling CS ?

Re: Unwanted delay before and after SPI transaction

Posted: Thu Nov 16, 2023 3:30 am
by rezarethink
That’s what the example code did. Is there a faster way?

Re: Unwanted delay before and after SPI transaction

Posted: Thu Nov 16, 2023 3:40 am
by username
Yes, you can specify the CS pin so it does it automatically.

I just noticed that you posted in the Arduino section, I only use ESP32-IDF. In IDF you can specify the CS pin so it does it for you. Did a little googling and found this.

Code: Select all

#include <SPI.h>

#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_CS   15
static const int spiClk = 240000000; // 1 MHz
SPIClass * hspi = NULL;

char buff[]="Hello Slave\n";

//byte buff[] = {0xAA, 0xBB, 0xAA, 0x01,
                  0x89, 0xAB, 0xCD, 0xEF};



void setup() {
Serial.begin(9600);
hspi = new SPIClass(HSPI);
hspi->begin();
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_CS); //SCLK, MISO, MOSI, SS
pinMode(HSPI_CS, OUTPUT); //HSPI SS
}

void loop() {
 for(int i=0; i<sizeof buff; i++)
 {
  SPI.transfer(buff[i]);
  Serial.println(buff[i]);

 }
 delay(1000);  
}

Re: Unwanted delay before and after SPI transaction

Posted: Thu Nov 16, 2023 4:44 am
by rezarethink
I’ll test it out and see if that speeds anything up. I found some docs that discussed a 1us delay acquiring a bus but I’m seeing a much larger delay.

Re: Unwanted delay before and after SPI transaction

Posted: Thu Nov 16, 2023 6:16 pm
by rezarethink
This worked for toggling the CS automatically

Code: Select all

  SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI, SPI_SS);  // SCK, MISO, MOSI, SS
  SPI.setHwCs(true);
However, it didn't result in a speedup of the overall SPI process. I measured 3uS before the SPI transfer and 6uS after the SPI transfer worth of delay when the SPI.transfer() call was in a tight loop. Right now, I can get the transfer to occur at around 30ksps which I can make work. Though occasionally, it will have an additional delay which does mess up the timing and I'm not sure what the origin of that is. You can see the spacing of the transfers has wider gaps at various points..

Image