AnsweredAssumed Answered

TWI Burst Read Problems

Question asked by NTan on Sep 30, 2013
Latest reply on Oct 18, 2013 by Prashant

Good Day Everyone! I'm having a problem with my burst read function below.


void burst_Read(unsigned short DeviceAddr, unsigned short TWI_Addr, unsigned short TWI_TX_Length, unsigned short RX_Count)



          single_Write(DeviceAddr, TWI_Addr, TWI_TX_Length);



          #define PRESCALE120M          12                    // factor = 12, 120MHz/10MHz

          int i, j;

          int x[RX_Count];


              *pTWI_FIFO_CTL                               |= XMTFLUSH;                                        // Clear the TX FIFO

              *pTWI_MASTER_STAT                     = BUFWRERR | BUFRDERR | DNAK | ANAK | LOSTARB; // Clear all status error



  *pTWI_FIFO_CTL                               = 0;

                    *pTWI_CONTROL                              = TWI_ENA | PRESCALE120M;

                    *pTWI_CLKDIV                              = CLKLOW(50) | CLKHI(50);

                    *pTWI_MASTER_ADDR                    = DeviceAddr;


                    *pTWI_MASTER_CTL                    = (RX_Count << 6) | MEN | MDIR; //RX_Count is number of bytes to be sent.



                    for (j = 0; j < RX_Count; j++){                              // # of transfers before stop condition

       while (*pTWI_FIFO_STAT == RCVSTAT)                                                  // wait to read the next sample from the RX FIFO


       x[j] = *pTWI_RCV_DATA8;                                        // BREAKPOINT1 Read the next sample from the RX FIFO.


       //put extra delay here?



  //BREAKPOINT2 - Checked DCNT (data transfer count of MASTER_CTL register) here

                    while ((*pTWI_INT_STAT & MCOMP) == 0)                                        // Wait until transmission complete and MCOMP is set



                    *pTWI_INT_STAT = RCVSTAT | MCOMP;                                                  // service TWI for next transmission


          for (j = 0; j < RX_Count; j++){                              // Print Received data

     printf("%i\n", x[j]);




Everything works well until it gets to the code colored in red. From the Blackfin Hardware Manual, it states that "DCNT Indicates the number of data bytes to transfer. As each data word is transferred, DCNT is decremented. It seems that when I reach BREAKPOINT2, the DCNT (data transfer count) is not equal to 0, when I expected it should be. Thus, the transmission cannot be completed and the program gets stuck in the while loop - while ((*pTWI_INT_STAT & MCOMP) == 0).


However, during debug mode, I tried placing a breakpoint (BREAKPOINT1) where the transfer of data occurs and monitored the TWI Registers. So whenever it reaches this part of the code, the program halts temporarily until I run it again.  With this, I saw that DCNT(transfer data) successfully decrements, and MCOMP is set, completing the transfer. Now, the program doesn't get stuck in the while loop - while ((*pTWI_INT_STAT & MCOMP) == 0). like earlier. There is no problem and my burst read function works fine.


I'm not sure what the problem is, should I add an extra delay after each transfer (since whenever I temporarily halted the program it worked)? Sorry If the explanation is kind of confusing.  I am using a Blackfin BF537 EZ Kit Lite and Visual DSP++ 5.0. Thank you!!