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/TMS570LC4357: I2C Issue

Part Number: TMS570LC4357

Tool/software: Code Composer Studio

HI,

I'm trying to read data from a sensor with the I2C but I don't success.

This is the function that I use to read the data from sensor:

void I2CReceive(uint8_t SLAVE_ADDRESS, uint8_t reg, uint8 *buff)
{
while(i2cIsBusBusy (i2cREG2) == true);
i2cREG2->MDR = I2C_RESET_OUT;
i2cSetMode(i2cREG2, I2C_MASTER);
i2cSetSlaveAdd(i2cREG2, SLAVE_ADDRESS);
i2cSetDirection(i2cREG2, I2C_TRANSMITTER);
i2cSetCount(i2cREG2, 1);
i2cSetStart(i2cREG2);
while((i2cREG2->STR & (I2C_TX | I2C_ARDY)) == false); // To wait for the tx ready and register access ready
i2cSendByte(i2cREG2, reg);

// Start receiving the data from slave
// wait for ARDY before beginning the read phase of the transaction
while((i2cREG2->STR & I2C_ARDY) == false); // BLOCKED--------------------------------------------------
i2cREG2->MDR = I2C_RESET_OUT; // To set the MDR with I"C is out of reset

i2cSetMode(i2cREG2, I2C_MASTER);
i2cSetDirection(i2cREG2, I2C_RECEIVER);
i2cSetCount(i2cREG2, 1);
i2cSetStop(i2cREG2);
i2cSetStart(i2cREG2);
// To receive one byte

while((i2cREG2->STR &(I2C_RX | I2C_ARDY)) == true);
buff[0] = i2cREG2->DRR;


while(i2cIsBusBusy (i2cREG2) == true);
/* Wait until Stop is detected */
while(i2cIsStopDetected(i2cREG2) == false);
/* Clear the Stop condition */
i2cClearSCD(i2cREG2);

}

it remains blocked in the following line:

// Start receiving the data from slave
// wait for ARDY before beginning the read phase of the transaction
while((i2cREG2->STR & I2C_ARDY) == false); // BLOCKED--------------------------------------------------

what mistake did I make?

Best Regards

Pisc

  • Hello Pisc,

    Is there special I2C communication requirements from sensor? For example, another register address is needed to get the data.
  • If you have access to, better to sniff the I2C bus using a logic analyzer.
  • Hello QJ Wang,

    Yes , It needs to send the slave address and internal register address. The sensor is gy-86 (IMU 10 dof) .

    Thanks for reply,

    Best Regards

    Pisc

  • I solved the issue
    Using the following functions:

    // Write to a register
    void I2CWrt(uint8_t SLAVE_ADDRESS, uint8_t reg, uint8 buff)
    {
    // Master Transfer Functionality //
    /* Configure address of Slave to talk to */
    i2cSetSlaveAdd(i2cREG2, SLAVE_ADDRESS);
    /* Set direction to Transmitter */
    /* Note: Optional - It is done in Init */
    i2cSetDirection(i2cREG2, I2C_TRANSMITTER);
    /* Configure Data count */
    /* Data Count + 1 ( Word Address) */
    i2cSetCount(i2cREG2, 1 + 1);
    /* Set mode as Master */
    i2cSetMode(i2cREG2, I2C_MASTER);
    /* Set Stop after programmed Count */
    i2cSetStop(i2cREG2);
    /* Transmit Start Condition */
    i2cSetStart(i2cREG2);
    /* Send the Word Address */
    i2cSendByte(i2cREG2, reg);
    /* Tranmit DATA_COUNT number of data in Polling mode */
    i2cSend(i2cREG2, 1, &buff);
    /* Wait until Bus Busy is cleared */
    while(i2cIsBusBusy(i2cREG2) == true);
    /* Wait until Stop is detected */
    while(i2cIsStopDetected(i2cREG2) == 0);
    /* Clear the Stop condition */
    i2cClearSCD(i2cREG2);
    }

    // Read from a register
    void I2CRec(uint8_t SLAVE_ADDRESS, uint8_t reg, uint8 *buff)
    {
    // Master Receive Functionality //
    /*****************************************/
    /*****************************************/
    /* wait until MST bit gets cleared, this takes
    * * few cycles after Bus Busy is cleared */
    while(i2cIsMasterReady(i2cREG2) != true);
    /* Configure address of Slave to talk to */
    i2cSetSlaveAdd(i2cREG2, SLAVE_ADDRESS);
    /* Set direction to Transmitter */
    /* Note: Optional - It is done in Init */
    i2cSetDirection(i2cREG2, I2C_TRANSMITTER);
    /* Configure Data count */
    /* Slave address + Word address write operation before reading */
    i2cSetCount(i2cREG2, 1);
    /* Set mode as Master */
    i2cSetMode(i2cREG2, I2C_MASTER);
    /* Set Stop after programmed Count */
    i2cSetStop(i2cREG2);
    /* Transmit Start Condition */
    i2cSetStart(i2cREG2);
    /* Send the Word Address */
    i2cSendByte(i2cREG2, reg);
    /* Wait until Bus Busy is cleared */
    while(i2cIsBusBusy(i2cREG2) == true);
    /* Wait until Stop is detected */
    while(i2cIsStopDetected(i2cREG2) == 0);
    /* Clear the Stop condition */
    i2cClearSCD(i2cREG2);
    /*****************************************/
    /*****************************************/
    /* wait until MST bit gets cleared, this takes
    * * few cycles after Bus Busy is cleared */
    while(i2cIsMasterReady(i2cREG2) != true);
    /* Configure address of Slave to talk to */
    i2cSetSlaveAdd(i2cREG2, SLAVE_ADDRESS);
    /* Set direction to receiver */
    i2cSetDirection(i2cREG2, I2C_RECEIVER);
    /* Configure Data count */
    /* Note: Optional - It is done in Init, unless user want to change */
    i2cSetCount(i2cREG2, 1);
    /* Set mode as Master */
    i2cSetMode(i2cREG2, I2C_MASTER);
    /* Set Stop after programmed Count */
    i2cSetStop(i2cREG2);
    /* Transmit Start Condition */
    i2cSetStart(i2cREG2);
    /* Tranmit DATA_COUNT number of data in Polling mode */
    i2cReceive(i2cREG2, 1, buff);
    /* Wait until Bus Busy is cleared */
    while(i2cIsBusBusy(i2cREG2) == true);
    /* Wait until Stop is detected */
    while(i2cIsStopDetected(i2cREG2) == 0);
    /* Clear the Stop condition */
    i2cClearSCD(i2cREG2);
    }