SPI comm. between Arduino Uno and TI Analog Front End.. Help!!

Hi There,

I am programming ECG Analog Front End (AFE) from Texax Instruments with Arduino Uno using SPI pins (SCK, MISO, MOSI and SS)

I am facing a problem with synchronizing the serial clock and the timings between them . The code is on the bottom of the topic, but first I have general questions regarding Arduino clock:

  1. Is the max serial clock of Arduino 16 MHz? (the AFE serial clock = 20 MHz! I know both master and slave should work on the same clock!!)

  2. According to the AFE datasheet, SS pin should be driven low exactly at the first cycle of the clock and again high exactly at theend of the 16th cycle, for data to be sent… How I can make sure of that while the arduino itself has a delay in each function (for ex: digitalWrite() and SPI.transfer() which I used them frequently ) ??

  3. Do I have to care about setting these bits? (from SPI page of Arduino.com)

  • SPI.setBitOrder() function.
  • SPI.setDataMode() function.

AFE datasheet is here (although I dont thinl you are going to read it! too long!!), but for further info.
it is easier to ask me…

PS: you can refer to the SPI timings diagrams in the datasheet ( pages 9 and 32).

The code till now looks like this:

// this code is to test the SPI communcation between 2 arduino UNO 
// made on 15/3/2015 last modifcation in 10/5/1015

#include <SPI.h>

byte dredy = 6;
byte ECGInput; 

//define the needed adresess for the registers
//--------------------------------------writing address  R/Wb = 0
const byte CONFIG = 0x00;
const byte CH_CNFG  = 0x2F;
const byte DRDYB_SRC = 0x27;
//-------------------------------------reading address R/Wb = 1
const byte DATA_STATUS =0xB0;
const byte DATA_CH1_ECG_upper = 0xB7;
const byte DATA_CH1_ECG_middle = 0xB8;
const byte DATA_CH1_ECG_lower = 0xB9;
const byte DATA_LOOP = 0xA0;

// define the value of the registers
byte VALUE_CONFIG_1 = 0x00;
byte VALUE_CONFIG_2 = 0x01;
byte VALUE_CH_CNFG = 0x32;
byte VALUE_DRDYB_SRC = 0x01;
byte READ_VALUE = 0x01;
byte DATA_STATUS_VALUE =0x32;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  pinMode(dredy, INPUT);
  SPCR |= _BV(MSTR);// enable the UNO on the master mode 
}

void loop() {
  
  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  SPI.transfer(CONFIG);
  SPI.transfer(VALUE_CONFIG_1);
   
  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  SPI.transfer(CH_CNFG);
  SPI.transfer(VALUE_CH_CNFG);
  
  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  SPI.transfer(DRDYB_SRC);
  SPI.transfer(VALUE_DRDYB_SRC);
   
  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  SPI.transfer(CONFIG);
  SPI.transfer(VALUE_CONFIG_2);
   
  VALUE_CH_CNFG = 0x33;
  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  SPI.transfer(CONFIG);
  SPI.transfer(VALUE_CONFIG_1);
   
  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  SPI.transfer(CH_CNFG);
  SPI.transfer(VALUE_CH_CNFG);
  
  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  SPI.transfer(CONFIG);
  SPI.transfer(VALUE_CONFIG_2);

  digitalWrite(SS, HIGH);// diale the slave (AFE)
  digitalWrite(SS, LOW);
  dredy = SPI.transfer(DATA_STATUS);
  
  if(dredy == DATA_STATUS_VALUE){
    Serial.println("the data is ready");
    Serial.println("the Lower byte are : ");
    digitalWrite(SS, HIGH);// diale the slave (AFE)
    digitalWrite(SS, LOW);
    ECGInput = SPI.transfer(DATA_CH1_ECG_lower);
    Serial.println(ECGInput);
   
    Serial.println("the Middle byte are : ");
    digitalWrite(SS, HIGH);// diale the slave (AFE)
    digitalWrite(SS, LOW);
    ECGInput = SPI.transfer(DATA_CH1_ECG_middle);
    Serial.println(ECGInput);
    
    Serial.println("the upper byte are : ");
    digitalWrite(SS, HIGH);// diale the slave (AFE)
    digitalWrite(SS, LOW);
    ECGInput = SPI.transfer(DATA_CH1_ECG_upper);
    Serial.println(ECGInput);

    Serial.println("------------------------------");
  }
}

1) With SPI the master drives the clock. The slave will take whatever clock speed the master provides.

2) The DataMode setting takes care of that.

3) Yes, those are very important. That defines the behavior of the bus. That defines when the data is sampled in relation to the first clock signal after the SS pin goes low. The SPI protocol doesn't define those things, so there are 4 possibilities. You need to look at the specifications for your chip (the stuff you describe in question 2) do decide which one is appropriate.

Hi Delta_G,

I am glad you came in ..

1) In this situation the Arduino is the master. So r u saying that the AFE will work at 16 MHz directly without any need of any action??

2) Actually I didnt get this data mode thing. Kindly, can you explain more about clock polarity and phase? Specifically, I need to send 16 bits at the same time. these 16 bits include 8 bits address (on the rising edge) and 8 bits data (on the falling) ........