Need to read and write 4 bytes data for SPI

Hi everyone,

I need to read and write ADAS1000 by Arduino Uno. Its each register is 32bits and 4 bytes data has to be read and write at the same time.

This is the datasheet link.

People are trying byYRDKRX62N spi proticol code.( ADAS1000 - Microcontroller No-OS Driver [Analog Devices Wiki] Looks complicated and device need to be configured.

Kindly, help me to following code. I need to read and write, send at least 1 register value.

// SPI
#define DOUT 11 //MOSI
#define DIN 12 //MISO
#define SPICK 13 //SCK

// pins
#define PIN_CS 7
#define PIN_DRDY 6

// the ADAS1000 communicates using SPI, so include the library:
#include <SPI.h>

int DRDY = 6;
int CS = 7;

union FourByte{
struct {
unsigned long value:32; //32bit register values go in here
byte command:8; //8bit command goes in here.
};
byte bit8[4]; //this is just used for efficient conversion of the above into 4 bytes.
};

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

// start the SPI library:
SPI.begin();
SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setBitOrder(MSBFIRST);

// initalize the data ready and chip select pins:
pinMode(DRDY, INPUT);
pinMode(CS, OUTPUT);

}

void loop(){
unsigned long register = SPI_read(0x01);
Serial.print(“register=”);
Serial.println(register);
delay(100);
}

void SPI_write(union FourByte data) {
digitalWrite(CS,LOW); //Using CS pin, so sync1/sync0 bytes not needed
for(char i = 3; i >= 0; i–){
SPI.transfer(data.bit8*); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 32bit value.*
}
digitalWrite(CS,HIGH);
}
unsigned long SPI_read(byte command){
digitalWrite(CS,LOW); //SS goes low to mark start of transmission
union FourByte data = {0xF804BE,command}; //generate the data to be sent, i.e. your command plus the Sync bytes.
for(char i = 3; i >= 0; i–){
data.bit8 = SPI.transfer(data.bit8*); //send the data whilst reading in the result*
}
digitalWrite(CS,HIGH); //SS goes high to mark end of transmission
return data.value; //return the 32bit value recieved.
}
Communication.h (3.68 KB)
ADAS1000.c (13.9 KB)
Communication.c (4.95 KB)

and 4 bytes data has to be read and write at the same time.

No. The 4 bytes need to be read one of the other, and reassembled into a long or unsigned long or float, as appropriate.

The code you posted incorrectly does something. You expected it to do something. You are 0 for 3. Down to the minors with you.

Sorry for my late replay. My eyes skipped your post.

Could you kindly explain it more ?
How to handle the 4 bytes data at a time?

The union doesn't look right to me for interconverting 4 bytes with one long. Here is what I would use:

union cvt { long x; byte b[4]} val;
//example of use
val.x = 12345678L;
byte b0 = val.b[0];