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/MSP432P401R: I2C read/write functions for BMI60 IMU

Part Number: MSP432P401R

Tool/software: Code Composer Studio

Hi

I am trying to  configure BMI160 IMU's (sensor) registers and then read the information. Based on the BMI160 datasheet:

1- The read access should be like

 The read function based on above figure is: (First master transmitter->slave address->register address->repeated start->slave address->read data->...->NACK->Stop)

int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)

{
    int8_t rslt = BMI160_OK; /* Return 0 for Success, non-zero for failure */
    // Ensure stop condition got sent
    while (EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTP);
    EUSCI_B0->CTLW0 |=EUSCI_B_CTLW0_TR;
    // I2C start condition
    EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT;
    EUSCI_B0->I2CSA = dev_id;    //Set slave address 
    while((EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTT));   //Wait for complete slave address to get transmitted

    while(!(EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG0) ); //Wait until the data in TXBUF(register address) is transmitted
    EUSCI_B0->IFG &= ~(EUSCI_B_IFG_TXIFG0); //Clear the flag
    EUSCI_B0->TXBUF = reg_addr;    //Send the register address



       EUSCI_B0->CTLW0 &= ~EUSCI_B_CTLW0_TR;
       // I2C start condition
       EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT;
       EUSCI_B0->I2CSA = dev_id;    //Set slave address 
       while((EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTT) );   //Wait for complete slave address to get transmitted

       /* Read len-1 bytes of data*/
       for (i=len; i>1; i-- )
     {
           __delay_cycles(300000); // ~100ms pause between transmissions
           /*Read Data*/
         *reg_data = EUSCI_B0->RXBUF;
         reg_data++;

         /*ACKM*/
         while(!(EUSCI_B0->IFG & EUSCI_B_IFG_RXIFG0)); //Wait until the data in TXBUF(register address) is transmitted
         EUSCI_B0->IFG &= ~(EUSCI_B_IFG_RXIFG); //Clear the flag
         if(i==2)/*For the last byte send the STOP command*/
         {EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTP;}
      }
       /*Read the last byte*/
     if (i)
     {  
         __delay_cycles(300000); // ~100ms pause between transmissions
         while(!(EUSCI_B0->IFG & EUSCI_B_IFG_RXIFG0)); //Wait until the data in TXBUF(register address) is transmitted
         EUSCI_B0->IFG &= ~(EUSCI_B_IFG_RXIFG0); //Clear the flag
         /*Read Data*/
         *reg_data = EUSCI_B0->RXBUF; 
         reg_data++;
     }
     
     EUSCI_B0->CTLW1 |= EUSCI_B_CTLW1_STPNACK; //Sent NACK
     // Ensure stop condition got sent
     while (EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTP);//check the ensure the STOP condition is sent out.

     return rslt;
    }

The problem I have here that the code get stuck in the 

 if (i)
     {  __delay_cycles(300000); // ~100ms pause between transmissions
         while(!(EUSCI_B0->IFG & EUSCI_B_IFG_RXIFG0)); //Wait until the data in TXBUF(register address) is transmitted
         EUSCI_B0->IFG &= ~(EUSCI_B_IFG_RXIFG0); //Clear the flag

My question is that did I write the correct format for read access?

Thanks

Saber 

**Attention** This is a public forum