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: I2C infinite loop problem

Part Number: INA226


Tool/software: Code Composer Studio

Hello, I am trying to create device drivers for the INA226. When I try to send or receive data over the I2C interface, my code gets stuck in an infinite loop. I am using a Hercules Launchpad TMS570LC43x microcontroller. It does not seem like the data is transferring. Below is the code. 

#include "HL_sys_common.h"
/* USER CODE BEGIN (1) */
/* Include FreeRTOS scheduler files */
#include "FreeRTOS.h"
#include "os_task.h"
/* Include HET header file - types, definitions and function declarations for system driver */
#include "HL_het.h"
#include "HL_gio.h"
#include "HL_i2c.h"

void main(void)
{
/* USER CODE BEGIN (3) */
//Set slave address uint32 SLAVE_ADDR = 1000000; uint32 slave_add = SLAVE_ADDR << 1; slave_add |= 1;
//Following Halcogen example code i2cInit(); i2cSetSlaveAdd(i2cREG1, slave_add); i2cSetDirection(i2cREG1,I2C_RECEIVER); i2cSetMode(i2cREG1,I2C_MASTER); i2cSetStop(i2cREG1); i2cClearSCD(i2cREG1); i2cSetStart(i2cREG1); i2cSendByte(i2cREG1,slave_add); uint8 test1 = i2cReceiveByte(i2cREG1); // Code gets stuck here while(i2cIsBusBusy(i2cREG1) == true); /* Wait until Stop is detected */ while(i2cIsStopDetected(i2cREG1) == 0); i2cClearSCD(i2cREG1); return; /* USER CODE END */ }


The code gets stuck in the i2cReceiveByte() function. Below is the while loop where it is getting stuck.

uint8 i2cReceiveByte(i2cBASE_t *i2c)
{
    /*SAFETYMCUSW 28 D MR:NA <APPROVED> "Potentially infinite loop found - Hardware Status check for execution sequence" */


    while ((i2c->STR & (uint32)I2C_RX_INT) == 0U) //code gets stuck here
    {
    } /* Wait */


/* USER CODE BEGIN (25) */
/* USER CODE END */    return ((uint8)i2c->DRR);
}

Does anyone know how I can stop getting stuck in this loop?

  • Hello Vince,

    You don't need to send slave address separately. 

    To read data from INA226, you need to write the register address first. Otherwise, the data in last register pointer location will be read.

    You need to set the data count to read (2 bytes):     i2cSetCount(i2cREG1, 2);

    The slave address in your code is not correct. The slave address listed in Table 2 is binary number (base 2). 1000000b -->  0x40.

    When write slave address to SAR register, you don't need to left shift the address by 1. I2CSAR=0x40 

  • Thank you for replying to my question.

    There are still a few things that I'm unsure about. 

    1. Is there anything, in particular, I should write to the register that I wish to read, or is write function strictly to set the register pointer? 


    2. When you say I don't have to send the slave address separately what do you mean by that? Does it mean that I don't need to send the slave address using the i2cSendByte() function, or do I just send the slave address and the register pointer as a single uint16 value? Is the address phase automatically done when I write to the SAR register, meaning the first byte I send with the i2cSendByte() function is the register pointer?

    3. It seems that the Transmit data ready interrupt flag (TXRDY) flag in the i2c status register (STR) is never set, which leads to my code getting stuck in the infinite loop. 

    Below are some screenshots of my code and where my code gets stuck and the register values at that point.

  • Thank you for replying to my question.

    There are still a few things that I'm unsure about. 

    1. Is there anything, in particular, I should write to the register that I wish to read, or is write function strictly to set the register pointer? 


    2. When you say I don't have to send the slave address separately what do you mean by that? Does it mean that I don't need to send the slave address using the i2cSendByte() function, or do I just send the slave address and the register pointer as a single uint16 value? Is the address phase automatically done when I write to the SAR register, meaning the first byte I send with the i2cSendByte() function is the register pointer?

    3. It seems that the Transmit data ready interrupt flag (TXRDY) flag in the i2c status register (STR) is never set, which leads to my code getting stuck in the infinite loop. 

    Below are some screenshots of my code and where my code gets stuck and the register values at that point.

  • Hello Vince,

    Have you resolved the I2C problem?

  • Hello,


    No, I am still getting stuck in this loop.