Skip to main content
added 433 characters in body
Source Link
SoreDakeNoKoto
  • 2.4k
  • 2
  • 14
  • 23

The connections are:

Arduino MISO = SDA
Arduino SCK = SCL
Arduino D4 = SCS
Arduino D5 = SYN

Each call of SPI.transfer() yields 8 serial clocks i.e. a clock for each bit, so 32 serial clocks is merely 4 SPI.transfer() calls. Your code is actually producing 32 x 8 = 256 serial clocks. I suggest you read up on SPI.

I didn't include some of the delays in the datasheet because digitalWrite() on an Uno takes about 5us which is more than enough for this case. Of course, the code is untested so...I've done most of your work already, almost feels like I should be getting paid or something...

I didn't include some of the delays in the datasheet because digitalWrite() on an Uno takes about 5us which is more than enough for this case. Of course, the code is untested so...

The connections are:

Arduino MISO = SDA
Arduino SCK = SCL
Arduino D4 = SCS
Arduino D5 = SYN

Each call of SPI.transfer() yields 8 serial clocks i.e. a clock for each bit, so 32 serial clocks is merely 4 SPI.transfer() calls. Your code is actually producing 32 x 8 = 256 serial clocks. I suggest you read up on SPI.

I didn't include some of the delays in the datasheet because digitalWrite() on an Uno takes about 5us which is more than enough for this case. Of course, the code is untested so...I've done most of your work already, almost feels like I should be getting paid or something...

Source Link
SoreDakeNoKoto
  • 2.4k
  • 2
  • 14
  • 23

Like the datasheet says, its best if you implement writing in software and use the hardware SPI for reading. The steps for writing to the registers are as follows:

  1. disable the SPI peripheral; 2. set MISO, SCLK and SS to be output; 3. set the pin which is connected to SYN to be output high; 4. activate SCS first and then SYN; 5. activate SCL; 6. apply a bit value to SDA and deactivate SCL; 7. repeat the last two steps seven times to complete one byte transfer; 8. repeat the last three steps for any remaining byte transfer; 9. deactivate SYN and the SCS; 10. enable again the SPI module;

In Arduino-speak:

#define SCS 4  // define your own CS to have better control

void setup(){
  pinMode(SS, OUTPUT);  //needed to use the SPI

  pinMode(SCS, OUTPUT);  // set SCS and SYN as output
  pinMode(SYN, OUTPUT);
  digitalWrite(SCS, HIGH);  // idle state
  digitalWrite(SYN, HIGH);
  SPI.beginTransaction(SPISettings(32000000, MSBFIRST, SPI_MODE3)) // mode 3, max read clock is 32mhz
}

void write_reg(byte addr, byte val){
  val = (val << 7) | (addr << 1);  // 6-bit addresses!
  SPI.end();  // disable SPI
  pinMode(MISO, OUTPUT);
  digitalWrite(SYN, HIGH);
  delayMicroseconds(10);

  digitalWrite(SCS, LOW);  // assert SCS and SYN
  digitalWrite(SYN, LOW);
  for (int i = 0; i < 8; i++){  // shift out a byte
    delayMicroseconds(10);
    digitalWrite(SCK, LOW);  // max clock is 100kHz
    digitalWrite(MISO, (val & 0x80) ? HIGH : LOW);
    delayMicroseconds(10);
    digitalWrite(SCK, HIGH);
    val <<= 1;
  }
  
  digitalWrite(SYN, HIGH); // deassert SCS and SYN
  digitalWrite(SCS, HIGH);

  SPI.beginTransaction(SPISettings(32000000, MSBFIRST, SPI_MODE3)); //restart SPI
}

For reading, you use the hardware SPI like this:

void read_regs(uint32_t data[], uint8_t len){  // an array to hold register data is passed
  digitalWrite(SYN, LOW);  // latching operation
  digitalWrite(SCS, LOW);
  digitalWrite(SYN, HIGH);

  uint32_t val = 0;
  for (int i = 0; i < len; i++){  // read the 8 32-bit registers into the array
    val = SPI.transfer(0);  // your 32 serial clocks
    val |= (uint32_t)SPI.transfer(0) << 8;
    val |= (uint32_t)SPI.transfer(0) << 16;
    val |= (uint32_t)SPI.transfer(0) << 24;
    data[i] = val;
  }
  
  digitalWrite(SCS, HIGH);  // deassert SCS
}

// Use the functions this way:

uint32_t reg_data[8];  // array to hold read data, max is 8

void loop(){
  write_reg(47, 1);  // set config bit 47
  read_regs(reg_data, 8);  // read all reg data into array

  for (int i = 0; i < 8; i++)
    Serial.println(reg_data[i], HEX);
}

I didn't include some of the delays in the datasheet because digitalWrite() on an Uno takes about 5us which is more than enough for this case. Of course, the code is untested so...