ESP32 SPI Communication to 3rd party hardware

I have a CERES PCB and i need to communicate with this PCB as Slave using SPI bus. The problem is I very low on knowledge regarding any types of communication. Below is the command list i need to do. I have practice the SPI code with only 1 data transfered to Slave. But when i try to do on my actual project, I'm become confused with the Address, MOSI cmd+add. I dont know which data need to be send and how.

I tried to play around but i keep getting rubbish data

Command Reference Command List

Below is my code

#include <SPI.h>

// SPI pins
#define SCK 18
#define MISO 19
#define MOSI 23
#define SS 5

// SPI settings 
const int responseSize = 4;  

void setup() {
  Serial.begin(115200);
  pinMode(SS, OUTPUT);
  SPI.begin(SCK, MISO, MOSI, SS);
  
  SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
  
  byte commandSelAdc2Vdd[] = {0x21, 0x40, 0x84, 0x00, 0x00, 0x02, 0x00};
  byte commandEnAcd2[] = {0x09, 0x40, 0x24, 0x00, 0x20, 0x00, 0x00};
  byte commandReadAcd2[] = {0x05, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00};

  byte* commands[] = {commandSelAdc2Vdd, commandEnAcd2, commandReadAcd2};

  for (int i = 0; i < 3; i++) {

    digitalWrite(SS, LOW);

    SPI.transfer(commands[i], sizeof(commandSelAdc2Vdd)); 

    digitalWrite(SS, HIGH);
    delay(100);
    digitalWrite(SS, LOW); 

    byte response[4]; 
    SPI.transfer(response, sizeof(response)); 
    digitalWrite(SS, HIGH); 

    Serial.print("Response for ");
    if (i == 0) 
    {
      Serial.print("commandSelAdc2Vdd: ");
    } else if (i == 1) 
    {
      Serial.print("commandEnAcd2: ");
    }else{
      Serial.print ("commandReadAcd2: ");
    }
    for (int j = 0; j < 4; j++) {
      Serial.print(response[j],HEX);
      Serial.print(" ");
    }
    Serial.println();
    delay(500);
  }
}

void loop() {

}

Upvotes: 0

Views: 63

Answers (1)

hcheung
hcheung

Reputation: 4034

Based on the additional fraction of the datasheet, it provides important information to show how the SPI should work with the device.

  1. read the timing diagram carefully, the CS supposed to be remained LOW throughout the complete transaction, not toggle HIGH and LOW as per your code.
  2. the MOSI+Addr already included the Addr starting from bit 2, so to run the ReadSerialNo command, all you need is to send 6 bytes as uint8_t readSerCmd[] = {0x00, 0x08, 0x00, 0x00, 0x00, 0x00}.

Here is an example on how it could read the Serial Number of the device based on the limited information provided.

#include <SPI.h>

void setup() {
  Serial.begin(115200);

  digitalWrite(SS, HIGH);
  pinMode(SS, OUTPUT);
  SPI.begin();
  
  uint8_t readSerCmd[] = {0x00, 0x08, 0x00, 0x00, 0x00, 0x00};
  uint8_t reBuff[6]{0};

  SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
  digitalWrite(SS, LOW);
  for (int i=0; i<sizeof(readSerCmd); i++) {
    rxBuff++ = SPI.transfer(commands[i]);
  }
  digitalWrite(SS, HIGH);
  SPI.endTransaction();

  // first two bytes as status, followed by 4-byte serial no
  for (i=0; i<sizeof(rebuff); i++) {
    Serial.print(rebuff[i], HEX);
    Serial.print(" ");
  }

}

void loop() {

}

Upvotes: 0

Related Questions