This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TDC7200: SPI reading an save Data

Part Number: TDC7200

Tool/software: Code Composer Studio

Hi there,

i have some trouble with my SPI functions.

The Communication is working and all Bytes are reading if i check it.

My Problem is to save this reading data.

I try to read and save Register Values with different functions, one function use the autoincrement of the TDC7200 and save the data in an array (SPI_readall_Reg()) and the other function read only one register with bit shift operation (SPI_read_longbyte()).

It seems i have in both functions the same Problem to save the reading Values.

Because everytime the last Value is lost and the RX Buffer doesn't copy the Value immediately. Why the RX buff doesn't copy the Values immediately??? i try a lot of things and I get only with the SPI_readall_Reg() all values in my data[] if i write the for loop with i<40 but this make a additionally clock cycle. (see picture below)

this clock cycle, with zero read value make me a unwanted delay in my communication and i need a very fast communication for my application

What happend wrong in my code?? it is nearly the same like the SPI example function from the TDC7200!! and how can i fix it ???? please help me.

#include <msp430.h> 
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/**
 * main.c
 */



void SPI_init(void);
void SPI_write_config(uint8_t,uint8_t);

uint8_t SPI_read_singlebyte(uint8_t);
uint32_t SPI_read_longbyte(uint8_t);
void SPI_readall_Reg(uint8_t);
uint8_t data[40];
uint32_t wert;
int main(void)
{

uint8_t i;
      WDTCTL = WDTPW | WDTHOLD;            // stop watchdog timer

      SPI_init();
      SPI_write_config(0x40,0x03);         // write register adress 00h with commands Meas Mode2/StartMeas Config1
      SPI_write_config(0x41,0x40);         // write register adress 01h with commands Single Stop/1 Measurement_cycle/ 10 Clock_periods Config1

      wert = SPI_read_longbyte(0x10);     // read only one register with bit shifting

      SPI_readall_Reg(0x90);             // autoinc Read register read all registers in an array


      for (i = 0; i <= 39; i++)
      {
        printf("%x\n", data[i]);
      }
      printf("%x\n",wert);

      return 0;

}

void SPI_write_config(uint8_t adress,uint8_t value)
{

    P2OUT &= (~BIT2);                // Select Device

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = adress;              // send adress to register 00h

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = value;               // write a command to register 00h

    while (UCB0STAT & UCBUSY);       // Wait for TX complete

    P2OUT |= (BIT2);                 // Unselect Device
}

/*
uint8_t SPI_read_singlebyte(uint8_t adress)
{
    uint8_t rcv;

    P2OUT &= (~BIT2);                // Select Device

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = adress;              // send adress to register 02h

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = 0;                   // Dummy write to read data

    while(!(UCB0IFG&UCRXIFG));       // Wait for RXBUF ready
    rcv = UCB0RXBUF;

    while (UCB0STAT & UCBUSY);       // Wait for TX complete

    P2OUT |= (BIT2);                 // Unselect Device

    return rcv;
}
*/

void SPI_readall_Reg(uint8_t adress)
{

uint8_t i,a=0;


P2OUT &= (~BIT2);                               // Select Device

while (!(UCB0IFG&UCTXIFG));                                                     // Wait for TXBUF ready
  UCB0TXBUF = adress;

  for(i=0; i<39; i++)             // if the i condition is ste to i<40 i get all Values in my data[] array correctly but with an additionaly clock cycle
   {
     while (!(UCB0IFG&UCTXIFG));                                                // Wait for TXBUF ready
     UCB0TXBUF = 0;                                                             // Dummy write so we can read data

     while(!(UCB0IFG&UCRXIFG));                 // Wait for RXBUF ready
     a = UCB0RXBUF;

     data[i] = a;
     while (UCB0STAT & UCBUSY);       // Wait for RX complete

   }

  P2OUT |= (BIT2);                 // Unselect Device

}

uint32_t SPI_read_longbyte(uint8_t adress)
{
    uint32_t  y = 0,x = 0;

    P2OUT &= (~BIT2);                // Select Device

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = adress;              // send adress to register 02h

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = 0;                   // Dummy write to read data

    while(!(UCB0IFG&UCRXIFG));       // Wait for RXBUF ready
    y = UCB0RXBUF;
    x |= (y << 16);

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = 0;                   // Dummy write to read data

    while(!(UCB0IFG&UCRXIFG));       // Wait for RXBUF ready
    y = UCB0RXBUF;
    x |= (y << 8);

    while(!(UCB0IFG & UCTXIFG));     // wait for TXBUF
    UCB0TXBUF = 0;                   // Dummy write to read data

    while(!(UCB0IFG&UCRXIFG));       // Wait for RXBUF ready
    y = UCB0RXBUF;
    x |= y;

    while (UCB0STAT & UCBUSY);       // Wait for TX complete

    P2OUT |= (BIT2);                 // Unselect Device

    printf("%x\n",x);

    return x;

}

void SPI_init(void)
{
/*
    UCSCTL3 |= SELREF_2;                      // Set DCO FLL reference = REFO
     UCSCTL4 |= SELA_2;                        // Set ACLK = REFO

     __bis_SR_register(SCG0);                  // Disable the FLL control loop
     UCSCTL0 = 0x0000;                         // Set lowest possible DCOx, MODx
     UCSCTL1 = DCORSEL_5;                      // Select DCO range 24MHz operation
     UCSCTL2 = FLLD_1 + 374;                   // Set DCO Multiplier for 12MHz
                                               // (N + 1) * FLLRef = Fdco
                                               // (374 + 1) * 32768 = 12MHz
                                               // Set FLL Div = fDCOCLK/2
     __bic_SR_register(SCG0);                  // Enable the FLL control loop

     // Worst-case settling time for the DCO when the DCO range bits have been
     // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
     // UG for optimization.
     // 32 x 32 x 12 MHz / 32,768 Hz = 375000 = MCLK cycles for DCO to settle
     __delay_cycles(375000);

     // Loop until XT1,XT2 & DCO fault flag is cleared
     do
     {
       UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
                                               // Clear XT2,XT1,DCO fault flags
       SFRIFG1 &= ~OFIFG;                      // Clear fault flags
     }while (SFRIFG1&OFIFG);                   // Test oscillator fault flag
*/
// Enable PIN aktivieren für Register Zugriff//
P6OUT |=BIT5;                          // Output HIGH enable Pin aktiv
P6DIR |=BIT5;                          // OUTput enable Pin for TDC7200


// SPI Pins Auswählen//
P3SEL = BIT0 + BIT1 + BIT2;
P2OUT |= BIT2;                        // PIN2.2 set the chipselect Pin high
P2DIR |= BIT2;                        // PIN2.2 set as Chipselect


// Konfiguration des SPI-Registers//
UCB0CTL1 |= UCSWRST;                  // enable statemachine
UCB0CTL1 |= UCSSEL_2;

UCB0CTL0 = UCMST+UCMSB+UCCKPH+UCSYNC; // SPI Konfiguration (MasterMode;SynchroneMode;Clock_polarity;MSBfirstMode)

UCB0BR0 = 0x00;                       // Prescaler
UCB0BR1 = 0;


UCA0MCTL = 0;                         // No Modulation
UCB0CTL1 &= ~UCSWRST;                 // disable software reset
//UCB0IE |= UCRXIE;                   // Enable USCI_B0 RX interrupt
}



greets Ben

  • Hello Ben,

    In the future when choosing a device to get help with in the forum, it would be advantageous to use the part you asking about rather than the part you are connecting to. In other words, what MSP430 are you using here?

    I also see in your code that you are mismatching USCI B0 and USCI A0 sometimes. These are different physical USCIs on the device, but have similar registers.

    Also, please check out the following application note for some very common issues with USCI SPI interface. If this application note does not cover your issue, please post again here . http://www.ti.com/lit/slaa734
  • Hi Jace,

    sorry for the missing information.

    I use the MSP 430 5529 µC.

    You wrote i mismatch the USCI B0 but i only find this mismatch for the no Modulation Bit.

    Because there are only defined USCI A0  in the library in know this have no effect for my code but I read everwhere that this Modulation bit normally is set zero and i dont care about it anymore.

    But my Problem persists.

    I have no issues directly with the SPI Comunnication.

    Only to save the reading data makes Problems because there always missing the last value of each SPI sequence. 

    I dont understand this issue because i Implement nearly the same code like the example code from TDC7200.

    Can anybody help me with this missing byte???

    greets Ben

  • i have solved my Problem.
    I must read the RX BUF after all TX Buf write commands to clear the waiting RXbuffer Values.

    My confusion at this Problem is that your TI example code for the TDC7200 doesnt read this RXBUFF values after each TXBUFF transmission.

    greets Ben
  • Ben,

    Glad you were able to solve your issue, and thank you for following up and posting about it here!

**Attention** This is a public forum