Multiple SS/CS for a single SPI

bunfight
Posts: 14
Joined: Mon Mar 08, 2021 5:33 pm

Multiple SS/CS for a single SPI

Postby bunfight » Wed Jul 21, 2021 4:11 pm

In the example https://github.com/espressif/arduino-es ... _Buses.ino an abbreviated form of the example is copied at below.

What I find odd is that the xspi->begin statement has SS as an argument whereas the xspi->beginTransaction does not.

As the XSPI_SCLK, XSPI_MISO, XSPI_MOSI, pins are fixed once xspi is instantiated but XSPI_SS could have several distinct values (how many?) then should not XSPI_SS be specified in xspi->beginTransaction?

Or should it be that to address a different device one should have:

Code: Select all

xspi->begin(SS1) 
xspi->beginTransaction
xspi->end

xspi->begin(SS2) 
xspi->beginTransaction
xspi->end
Or perhaps something else?

Thanks.

Code: Select all

void setup() {
  //initialise two instances of the SPIClass attached to VSPI and HSPI respectively
  vspi = new SPIClass(VSPI);
  hspi = new SPIClass(HSPI);

  vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); 
  hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS);
  
  //set up slave select pins as outputs as the Arduino API
  //doesn't handle automatically pulling SS low
  pinMode(VSPI_SS, OUTPUT); //VSPI SS
  pinMode(HSPI_SS, OUTPUT); //HSPI SS
}

void loop() {

  vspiCommand();
  delay(100);
  hspiCommand();
  delay(100);
}

void vspiCommand() {
  byte data = 0b01010101; // junk data to illustrate usage

  //use it as you would the regular arduino SPI API
  vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(VSPI_SS, LOW); //pull SS slow to prep other end for transfer
  vspi->transfer(data);  
  digitalWrite(VSPI_SS, HIGH); //pull ss high to signify end of data transfer
  vspi->endTransaction();
}

void hspiCommand() {
  byte stuff = 0b11001100;
  
  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(HSPI_SS, LOW);
  hspi->transfer(stuff);
  digitalWrite(HSPI_SS, HIGH);
  hspi->endTransaction();
}

bunfight
Posts: 14
Joined: Mon Mar 08, 2021 5:33 pm

Re: Multiple SS/CS for a single SPI

Postby bunfight » Thu Jul 22, 2021 1:40 pm

Perhaps the presentation below is clearer.

What I find odd is that the xspi->begin statement has SS as an argument whereas the xspi->beginTransaction does not.

One might have several devices indexed by SS, but all attached to the same MOSI, MISO & SCK pins. When one selects a different device by changing the value of SS then as SPISettings in xspi->beginTransaction(SPISettings(...)) may vary by the value of SS it would make sense to specify SS as an argument in "beginTransaction". As this is not possible with the existing library, perhaps
begin
beginTransaction
.......
endTransaction
end
might work but could be inefficient. Or should one just use an external SPI Expander?

Who is online

Users browsing this forum: ESP_Sprite and 85 guests