Problems with SPI Reading and Writing to External Registers

Hi All,

I'm new to the Arduino platform, and I'm currently using an Arduino Pro to interface with this chip from TI.

I searched the forums here and actually saw a few posts from someone also trying to interface with this chip in the past. I decided to test out the code suggested in the following post. I'm just testing to see if I can write to and read from the chip's registers correctly.

See below for my code and the console output. It's obvious that the 16 bits (lower 2 bytes) are being written correctly, but the rest is not.

I would appreciate some help on debugging this issue.

Btw, AFE4490.h simply contains the register addresses of the chip. Also, I believe SPIMODE0 is the correct mode to be used based on Atmega328 data sheet and the TI chip's datasheet, but let me know if you disagree.

#include <intrinsics.h>
#include <string.h>
#include <SPI.h>
#include "AFE4490.h"

const int SCLK = 13;
const int SOMI = 12;
const int SIMO = 11;
const int SPISTE = 10;
const int ADC_RDY = 9;

void AFE4490Initialize (void);
void RegisterWrite (uint8_t address, uint32_t data);
uint32_t RegisterRead (uint8_t address);

void setup()
{
   Serial.begin(9600);
    
   SPI.begin();
   pinMode (SOMI, INPUT);
   pinMode (SPISTE, OUTPUT);
   pinMode (SCLK, OUTPUT);
   pinMode (SIMO, OUTPUT);
   pinMode (ADC_RDY, INPUT); //To be used for interrupt.
   
   SPI.setClockDivider (SPI_CLOCK_DIV128); //Divides 8 MHZ clock by 128 = 62.5 KHz
   SPI.setDataMode (SPI_MODE0);
   SPI.setBitOrder (MSBFIRST);
 
   AFE4490Initialize(); //Function used for initializing the AFE4490
   
}

void loop()
{
  RegisterWrite(CONTROL0, 0x000001); //SPI_Read Enabled
  Serial.println(RegisterRead(LEDCNTRL), HEX);
  Serial.println(RegisterRead(TIAGAIN), HEX);
  Serial.println(RegisterRead(TIA_AMB_GAIN), HEX);
  Serial.println("\n");
  delay(1000);
}

////////////////////Functions Below/////////////////////////

void AFE4490Initialize (void)
{
    Serial.println("Initializing \n");
    RegisterWrite(CONTROL0, 0x000008); //Reset Chip to Default Values
    RegisterWrite(LEDCNTRL, 0x11414);
    RegisterWrite(TIAGAIN,0x008002);
    RegisterWrite(TIA_AMB_GAIN,0x004404);
    Serial.println("Initialization Complete. \n");
}

void RegisterWrite(uint8_t address, uint32_t data)
{
    digitalWrite (SPISTE, LOW); // enable device
    SPI.transfer (address); // send address to device
    SPI.transfer ((data >> 16) & 0xFF); // writing top 8 bits
    SPI.transfer ((data >> 8) & 0xFF); // writing middle 8 bits
    SPI.transfer (data & 0xFF); // writing bottom 8 bits    
    digitalWrite (SPISTE, HIGH); // disables device
}

uint32_t RegisterRead(uint8_t address)
{       
    uint32_t data=0;
    digitalWrite (SPISTE, LOW); // enables device
    SPI.transfer (address); // sends address to device
    data |= (SPI.transfer (0)<<16); // reading top 8 bits data
    data |= (SPI.transfer (0)<<8); // reading middle 8 bits  data
    data |= SPI.transfer (0); // reading bottom 8 bits data
    digitalWrite (SPISTE, HIGH); // disables device
    return data; // returns with 24 bits of read data
}

Console Output Below

Initializing

Initialization Complete.

1414
FFFF8002
4404

1414
FFFF8002
4404

1414
FFFF8002
4404

Any ideas? I was thinking maybe the delays could have an affect?

I can't see anything obviously wrong with your code. I doubt the delays would affect it.

You may want to get a logic analyzer and see what is really happening. You can get one that would handle SPI for $US 99.

I find logic analyzers help find things like wiring problems, or code issues (perhaps timing issues) that are rather opaque when just staring at something that "doesn't work".

I wonder if it's a hardware issue then. I'm still not sure. I got my hands on a logic analyzer and will try to post results.

So there are no issues in the read and write functions?

No obvious issues. I haven't compared your code, line by line, to what the datasheet says. I presume you have the bytes in the right order, that sort of thing.

Ok, here are the screencaps of what the logic analyzer shows me. This is the first time I'm using a logic analyzer, but I think I set all the settings right. Let me know what you think.

Please right click on the images and click on "view image" or similar to see it in full resolution.

Thanks.