I am trying to interface the AFE4400SPO2 EVM to the Arduino Uno by removing the connections R29, R31, R33, and R35 and connecting to the SPI pins on the Arduino.
The connections are as follows:
Arduino Pin 10 (Chip Select) --> R29
Arduino Pin 11 (SIMO) --> R31
Arduino Pin 12 (MISO) --> R33
Arduino Pin 13 (SCLK) --> R35
I am using the following code to read from and write on to the register LED2STC on the AFE4400, but have been unsuccessful. Can you tell me if the SPI mode needs to be set differently for both reads and writes, and if so, what modes they are? Please also let me know if there is any issue with the code (if I am doing something wrong).
#include<SPI.h>
#define chipSelectPin 10
#define LED2STC 0x01
#define CONTROL0 0x00
void SPIWriteReg(byte regAddress, uint32_t regValue)
{
byte LSB, midByte, MSB;
digitalWrite(chipSelectPin, LOW);
SPI.transfer(regAddress);
uint32_t maskLSB = 0xFF;
uint32_t maskMidByte = 0xFF00;
uint32_t maskMSB = 0xFF0000;
LSB = regValue & maskLSB; // Extract the least significant byte by masking all other bits
midByte = (regValue & maskMidByte) >> 8; // Mask all bits except the second byte and then shift right by one byte to extract it
MSB = (regValue & maskMSB) >> 16; // Mask all bits except the most significant byte and then shift right by two bytes to extract it
SPI.transfer(MSB);
SPI.transfer(midByte);
SPI.transfer(LSB);
digitalWrite(chipSelectPin, LOW);
}
void SPIEnableRead()
{
SPIWriteReg(CONTROL0, 1);
delay(1);
}
void SPIDisableRead()
{
SPIWriteReg(CONTROL0, 0);
delay(1);
}
uint32_t SPIReadReg(byte regAddress)
{
byte temp_byte_1, temp_byte_2, temp_byte_3;
uint32_t regValue = 0;
// enable reading from registers
SPIEnableRead();
digitalWrite(chipSelectPin, LOW);
// set address
SPI.transfer(regAddress);
// get first byte
temp_byte_1 = SPI.transfer(0x00);
Serial.print("First Byte read = "); Serial.println(temp_byte_1, HEX);
delay(10);
temp_byte_2 = SPI.transfer(0x00);
Serial.print("Second Byte read = "); Serial.println(temp_byte_2, HEX);
delay(10);
temp_byte_3 = SPI.transfer(0x00);
Serial.print("Third Byte read = "); Serial.println(temp_byte_3, HEX);
delay(10);
regValue = (temp_byte_3 << 16) | (temp_byte_2 << 8) | temp_byte_1;
digitalWrite(chipSelectPin, HIGH);
// disable reading from registers
SPIDisableRead();
return regValue;
}
void setup()
{
Serial.begin(115200);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
pinMode(chipSelectPin, OUTPUT);
}
void loop()
{
SPIWriteReg(LED2STC, 7998);
delay(1000);
uint32_t value = SPIReadReg(LED2STC);
Serial.print("Value returned from LED2STC = "); Serial.println(value, HEX);
Serial.println(); Serial.println();
}