SPI Question. Reading from registers

Hello,

I am trying to interface an Arduino Uno with a TDC GP22 chip (ultrasonic fluid flow time converter device) and I am trying to read from the registers. Here is the function/method that I have created to read from a register (used barometric sensor code as template):

unsigned int readRegister(uint8_t regAd, int numOfBytesToRead){
   byte inByte = 0;                         //incoming byte from the SPI
   
   byte read_opcode = B0;             //**B0 is not actual opcode**
   unsigned int result = 0;              //initialize variable that is to be returned
   Serial.print(regAd, BIN);             //display register that is to be read in binary
   Serial.print("\t");                           //creates seperation from register address that was previously printed
   digitalWrite(slaveSelectPin, LOW);     //enable chip to communicate via SPI
   //NOT SURE IF READ OPCODE OR ADDRESS SHOULD BE SENT FIRST
   SPI.transfer(read_opcode);           //send read opcode to GP22
   SPI.transfer(regAd);                  //send address of register to be read and should return a byte from  the register
   numOfBytesToRead--;              //Decrements the bytes to read
   /*if there are still bytes to be read, then bit shift the read value to the left
   to make room for the rest of the bytes that need to be read*/
   if (numOfBytesToRead > 0 ){
     result = result << 8;
     inByte = SPI.transfer(0x00);         //read value from the register
     //combine the byte(s) 
     result = result | inByte;
     numOfBytesToRead--;              //decrement bytes to read
   }
   digitalWrite(slaveSelectPin, HIGH);           //deselect the GP22
  return (result);            //return the value that was stored in the register
   
 }

I am not quite sure if my logic is correct. I have two questions:

  1. When specifying what register address to read from, do I first transfer the read opcode and then transfer the register address? (Like in the code above) or do I transfer in the reverse order?

  2. Also, how do I actually acquire the value that is read? What function or method do I use? How is the value returned after I transfer the read opcode and register address?

Thank you,
Anthony

The answer to both your questions is in the datasheet for the chip.

Mark

I presume you send the read opcode first, that would be normal. However I would certainly see what the datasheet says.

Thanks for the replies. But I was expecting there to be a function to retrieve the data within the register...

Doesn't this do it?

     inByte = SPI.transfer(0x00);         //read value from the register

Can you post a link to the datasheet?