Unwanted delay before and after SPI transaction

rezarethink
Posts: 20
Joined: Fri Mar 24, 2017 4:15 am

Unwanted delay before and after SPI transaction

Postby rezarethink » Wed Nov 15, 2023 6:04 pm

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?

username
Posts: 530
Joined: Thu May 03, 2018 1:18 pm

Re: Unwanted delay before and after SPI transaction

Postby username » Thu Nov 16, 2023 3:17 am

Is there a specific reason your manually toggling CS ?

rezarethink
Posts: 20
Joined: Fri Mar 24, 2017 4:15 am

Re: Unwanted delay before and after SPI transaction

Postby rezarethink » Thu Nov 16, 2023 3:30 am

That’s what the example code did. Is there a faster way?

username
Posts: 530
Joined: Thu May 03, 2018 1:18 pm

Re: Unwanted delay before and after SPI transaction

Postby username » Thu Nov 16, 2023 3:40 am

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

rezarethink
Posts: 20
Joined: Fri Mar 24, 2017 4:15 am

Re: Unwanted delay before and after SPI transaction

Postby rezarethink » Thu Nov 16, 2023 4:44 am

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.

rezarethink
Posts: 20
Joined: Fri Mar 24, 2017 4:15 am

Re: Unwanted delay before and after SPI transaction

Postby rezarethink » Thu Nov 16, 2023 6:16 pm

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

Who is online

Users browsing this forum: No registered users and 43 guests