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