Arduino SPI with AFE4490

Hey, all!
I am currently making a pulse oximetre that use arduino pro 328 (3.3V,8 MHz) with texas instrument’s Analogue Front End chip AFE4490.

I am a problem with its SPI communication. So, when i plug the MOSI from arduino to the chip, it results nothing. But, when I unplug it, it results random number. So I concluded from that, I have no problem with the hardware.
But, I am confused why it still results zero, despite of input is available. I am suspecting there is something wrong with my code.
I am not good with finding error myself, perhaps, anyone here knows where is the problem from my following code?

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

/* Arduino Mega
//SPI connection
const int SOMI = 50; //data out connect to MISO
const int SIMO = 51; //data in connect to MOSI //butuh logic level converter
const int SCLK  = 52;
const int SPISTE = 53; //butuh logic level converter
const int CLKOUT = 47;
const int AFEADCRDY = 48; //butuh logic l
evel converter
const int AFEPDNZ = 49;
*/

const int SOMI = 16; //data out connect to MISO
const int SIMO = 15; //data in connect to MOSI //butuh logic level converter
const int SCLK  = 17;
const int SPISTE = 14; //butuh logic level converter

int count = 0;

//function declarations
//void AFE4490EnableInterrupt (void);
//void AFE4490DisableInterrupt (void);
void AFE4490Write (uint8_t address, uint32_t data);
uint32_t AFE4490Read (uint8_t address);
void SPIInit (void);

void setup()
{
   Serial.begin(9600);
    
   pinMode (SOMI,INPUT);
   pinMode (SPISTE,OUTPUT);
   pinMode (SCLK, INPUT);
   pinMode (SIMO, OUTPUT);
   //pinMode (CLKOUT, INPUT); 
   //pinMode (AFEADCRDY, OUTPUT);
   //pinMode (AFEPDNZ, OUTPUT);   
   
   //digitalWrite (SPISTE, HIGH);
   //digitalWrite (SCLK,HIGH);
   //digitalWrite (SIMO, 0X00000000);
   //digitalWrite (AFEPDNZ, HIGH);     
   
     AFE4490Init (); 
   //AFE4490DisableInterrupt(); 

            for (int count= 0; count<60; count++) 
            {
                    Serial.println(count);
                    delay (1000);
                    
                    Serial.print("Red Data:"); 
                    Serial.println(AFE4490Read(LED2VAL)); 
                    Serial.print("Red Ambient Data:"); 
                    Serial.println(AFE4490Read(ALED2VAL)); 
                    Serial.print("Red Different:");
                    Serial.println(AFE4490Read(LED2ABSVAL));
                    Serial.print("IR Data:"); 
                    Serial.println(AFE4490Read(LED1VAL)); 
                    Serial.print("IR Ambient:"); 
                    Serial.println(AFE4490Read(ALED1VAL)); 
                    Serial.print("IR Different:"); 
                    Serial.println(AFE4490Read(LED1ABSVAL)); 
            }
 
   //AFE4490EnableInterrupt ();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop()
{
         delay (1000);
}


/*
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AFE4490EnableInterrupt (void)
{
   digitalWrite(AFEADCRDY, HIGH);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AFE4490DisableInterrupt (void)
{
   digitalWrite(AFEADCRDY,LOW);
}
*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void AFE4490Init (void)
{ 
    Serial.println("AFE4490 Initialization Starts"); 
    AFE4490Write(CONTROL0, 0x000000);
    AFE4490Write(CONTROL1, 0x000101);
    AFE4490Write(CONTROL2, 0x000000); // Disable crystal, we supply 8MHz directly 
    AFE4490Write(PRPCOUNT, 0X001F3F);

    AFE4490Write(LED2STC, 0X001770); //timer control
    AFE4490Write(LED2ENDC,0X001F3E); //timer control
    AFE4490Write(LED2LEDSTC,0X001770); //timer control
    AFE4490Write(LED2LEDENDC,0X001F3F); //timer control
    AFE4490Write(ALED2STC, 0X000000); //timer control
    AFE4490Write(ALED2ENDC, 0X0007CE); //timer control
    AFE4490Write(LED2CONVST,0X000002); //timer control
    AFE4490Write(LED2CONVEND, 0X0007CF); //timer control
    AFE4490Write(ALED2CONVST, 0X0007D2); //timer control
    AFE4490Write(ALED2CONVEND,0X000F9F); //timer control

    AFE4490Write(LED1STC, 0X0007D0); //timer control
    AFE4490Write(LED1ENDC, 0X000F9E); //timer control
    AFE4490Write(LED1LEDSTC, 0X0007D0); //timer control
    AFE4490Write(LED1LEDENDC, 0X000F9F); //timer control
    AFE4490Write(ALED1STC, 0X000FA0); //timer control
    AFE4490Write(ALED1ENDC, 0X00176E); //timer control
    AFE4490Write(LED1CONVST, 0X000FA2); //timer control
    AFE4490Write(LED1CONVEND, 0X00176F); //timer control
    AFE4490Write(ALED1CONVST, 0X001772); //timer control
    AFE4490Write(ALED1CONVEND, 0X001F3F); //timer control

    AFE4490Write(ADCRSTCNT0, 0X000000); //timer control
    AFE4490Write(ADCRSTENDCT0,0X000000); //timer control
    AFE4490Write(ADCRSTCNT1, 0X0007D0); //timer control
    AFE4490Write(ADCRSTENDCT1, 0X0007D0); //timer control
    AFE4490Write(ADCRSTCNT2, 0X000FA0); //timer control
    AFE4490Write(ADCRSTENDCT2, 0X000FA0); //timer control
    AFE4490Write(ADCRSTCNT3, 0X001770); //timer control
    AFE4490Write(ADCRSTENDCT3, 0X001770);
    
    AFE4490Write(CONTROL2,0x020200);		// Disable crystal, 0.5V TX_REF 
    AFE4490Write(LEDCNTRL,0x008080);		// LED_RANGE=100mA, LED=50mA 
    AFE4490Write(CONTROL1,0x000103);		// Timers ON, average 3 samples 
    AFE4490Write(CONTROL0,0x000001);		// Switch to READ mode 
    
    delay(1000);
    Serial.println("AFE4490 Initialization Done"); 
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SPIInit (void)
{    
	SPI.setClockDivider (SPI_CLOCK_DIV128);
	SPI.setDataMode (SPI_MODE0);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void AFE4490Write (uint8_t address, uint32_t data)
{
     if (digitalRead (SOMI) == HIGH)
   {
          Serial.println ("SOMI pin is connected!Writing register is proceeded!");
          SPIInit();             
          SPI.begin();
          digitalWrite (SPISTE, LOW);
          SPI.transfer (address);
          Serial.println (address);
          SPI.transfer ((data>>16)&0XFF);
          Serial.println ((data>>16)&0XFF);
          SPI.transfer ((data>>8)&0XFF);
          Serial.println ((data>>8)&0XFF);
          SPI.transfer (data &0XFF);
          Serial.println (data &0XFF);
          digitalWrite (SPISTE, HIGH); 
          Serial.println("Writing Register Succeded!"); 
          SPI.end();
   }       
   else
   {
     Serial.println ("SOMI is not connected, please check the wire!");
   }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t AFE4490Read (uint8_t address)
{       
          SPIInit();
          uint32_t data = 0;
          digitalWrite (SPISTE, LOW);
          SPI.begin();
          SPI.transfer (address);
          Serial.print (address);
          Serial.print ("  ");
          data |= (SPI.transfer (0) <<16);
          Serial.print (data,BIN);
          Serial.print ("  ");
          data |= (SPI.transfer (0) <<8);
          Serial.print (data,BIN);
          Serial.print ("  ");
          data |= SPI.transfer (0);
          Serial.print (data,BIN);
          Serial.print ("  ");
          digitalWrite (SPISTE, HIGH);
          SPI.end();
        return data;
}

Special thanks for Krupski, on helping me with the Read and Writing register :slight_smile:

Best Regards,

Stella

So, when i plug the MOSI from arduino to the chip, it results nothing. But, when I unplug it, it results random number. So I concluded from that, I have no problem with the hardware.

That conclusion is wrong. The SPI interface consists of at least 4 signal lines: SS (slave select, sometimes called CS for chip select), MOSI (master out, slave in), MISO (master in, slave out) and SCKL (serial clock). All of them have to be connected for the SPI to work. Make a photograph of your setup and post it. Also provide a link to the datasheet of your AFE4490. Do the same for the used libraries.

pinMode (SCLK, INPUT);

SCLK is an output, not an input.

with texas instrument's Analogue Front End chip AFE4490.

A link would be useful.

#include "AFE4490.h"

Where did you get this library? Where do you tell it which pins the device is connected to? The values for the Mega ARE the SPI pins for the Mega. The values you are using are NOT the SPI pins for the 328-based Arduinos. Why did you change the values?

Hey, thanks for the respond!
@ pylon:
this is the datasheet of AFE4490: http://www.ti.com/lit/ds/symlink/afe4490.pdf

and for the library AFE4490:

#ifndef AFE4490_H_
#define AFE4490_H_

#include <stdio.h>
#include <types.h>

/* Register definitions */
#define CONTROL0		0x00
#define LED2STC			0x01
#define LED2ENDC		0x02
#define LED2LEDSTC		0x03
#define LED2LEDENDC		0x04
#define ALED2STC		0x05
#define ALED2ENDC		0x06
#define LED1STC			0x07
#define LED1ENDC		0x08
#define LED1LEDSTC		0x09
#define LED1LEDENDC		0x0a
#define ALED1STC		0x0b
#define ALED1ENDC		0x0c
#define LED2CONVST		0x0d
#define LED2CONVEND		0x0e
#define ALED2CONVST		0x0f
#define ALED2CONVEND	0x10
#define LED1CONVST		0x11
#define LED1CONVEND		0x12
#define ALED1CONVST		0x13
#define ALED1CONVEND	0x14
#define ADCRSTCNT0		0x15
#define ADCRSTENDCT0	0x16
#define ADCRSTCNT1		0x17
#define ADCRSTENDCT1	0x18
#define ADCRSTCNT2		0x19
#define ADCRSTENDCT2	0x1a
#define ADCRSTCNT3		0x1b
#define ADCRSTENDCT3	0x1c
#define PRPCOUNT		0x1d
#define CONTROL1		0x1e
#define SPARE1			0x1f
#define TIAGAIN			0x20
#define TIA_AMB_GAIN	0x21
#define LEDCNTRL		0x22
#define CONTROL2		0x23
#define SPARE2			0x24
#define SPARE3			0x25
#define SPARE4			0x26
#define SPARE4			0x26
#define RESERVED1		0x27
#define RESERVED2		0x28
#define ALARM			0x29
#define LED2VAL			0x2a
#define ALED2VAL		0x2b
#define LED1VAL			0x2c
#define ALED1VAL		0x2d
#define LED2ABSVAL		0x2e
#define LED1ABSVAL		0x2f
#define DIAG			0x30

#endif /* AFE4490_H_ */

@PaulS
I previously used arduino mega, but due the SPI pinin AFE4490 voltage requires below 5V, i used arduino pro instead.
So, if you see in my code above, the pin for the mega is commented, it isn’t used anymore, i used the pinout from arduino pro instead.

and for the library AFE4490:

That's just the header file, not the actual library file (that probably ends in .cpp). If possible, provide a link to where you downloaded it from.

@PaulS
I previously used arduino mega, but due the SPI pinin AFE4490 voltage requires below 5V, i used arduino pro instead.
So, if you see in my code above, the pin for the mega is commented, it isn’t used anymore, i used the pinout from arduino pro instead.

I see:
http://arduino.cc/en/Main/ArduinoBoardPro

SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI communication, which, although provided by the underlying hardware, is not currently included in the Arduino language.

and

const int SOMI = 16; //data out connect to MISO
const int SIMO = 15; //data in connect to MOSI //butuh logic level converter
const int SCLK  = 17;
const int SPISTE = 14; //butuh logic level converter

What the hell SOMI is supposed to mean isn’t clear. What the hell those assignments mean isn’t clear, since you never seem to tell the library about those pins. Why you think that telling the library about those pins means anything, when SPI is hardware based, I don’t understand.

I’d suggest that you connect the device to the proper pins. Your call, though.