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.

RTOS/TM4C123GH6PM: INA219 I2C Communication Problem with TIVA C EK-TM4C123GXL

Part Number: TM4C123GH6PM
Other Parts Discussed in Thread: INA219, EK-TM4C123GXL

Tool/software: TI-RTOS

Hi Team,

I'm using INA219 for current measurement.

Tried to communicate with the microntroller using I2C. But unfortunately, Transfer is failing.

I'm using TI-RTOS 2.16.00.08,CCSv8.1.0 to my project.

Please find the attached project folder and correct me where I'm going wrong.

Thanks in Advance,

Yashwanth Kumar Gandeti,ina219 Datasheet.pdfina219.zip

  • Hello Yashwanth,

    You are going to have to give us more information than just saying 'transfer is failing' and posting your project. We don't do in depth code reviews of an entire project.

    Please offer some more details about what has and hasn't worked, and just post relevant code like your I2C initialization, and what commands are being sent.

    Furthermore for I2C, make sure pull-up resistors have been installed on SDA and SCL lines, and please inform what value the pull-up resistors have.

    If you have an oscilloscope, I would recommend scoping the I2C lines to see what communication is occurring on them.
  • Hi Ralph Jacobi,

    For I2C Communication I have used I2C1Channel. Initialization as follows:

        SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
    
        /* Configure the appropriate pins to be I2C instead of GPIO. */
        GPIOPinConfigure(GPIO_PA6_I2C1SCL);
        GPIOPinConfigure(GPIO_PA7_I2C1SDA);
        GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
        GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);

    I have configured void currentTask() as task. In this task when I tried to do I2cwrite using writeRegister()

    void writeRegister(I2C_Handle handle, uint16_t regAddr, uint16_t value)
    {
        uint8_t             txBuffer[4];
        I2C_Transaction     i2cTransaction;
    
        i2cTransaction.slaveAddress = CURR_I2C_ADDR;
    
         //Write to a 16-bit status register
        i2cTransaction.writeBuf = txBuffer;
        i2cTransaction.writeCount = 3;
        i2cTransaction.readCount = 0;
    
        txBuffer[0] = regAddr & 0xFF; // 8- Bit address
        txBuffer[1] = value >> 8; // HB Addr
        txBuffer[2] = value & 0xFF; // LB Addr
    
        if (!I2C_transfer(handle, &i2cTransaction)) {
            GPIO_write(Board_LED2, Board_LED_ON);
            System_abort("Bad I2C transfer!");
        }
    }

    for writing a calibration value to the register of address 0x05 using

    writeRegister(i2c,0x05, 8192);

    In this function after executing 

    I2C_transfer(handle, &i2cTransaction)

    (while debugging....)

    control is not executing next line of code. when i tried to pause the debug. control is in Load_IdleFxn() in Load.c,some times in Idle_loop() in Idle.c.

    I haven't used any external pull-up resistors for SCL and SDA lines.(I'm using TIVA C EK-TM4C123GXL launchpad. Do i need to add external pull - up resistors?) 

    and my read function as follows (haven't executed this function as I can't come out of the I2C_transfer() of write register):

    void readRegister(I2C_Handle handle,
                      uint16_t regAddr,
                      uint16_t *data,
                      size_t   length)
    {
        uint8_t             txBuffer[2];
        I2C_Transaction     i2cTransaction;
    
        i2cTransaction.slaveAddress = CURR_I2C_ADDR;
    
        /* Write to a 16-bit status register */
        i2cTransaction.writeBuf = txBuffer;
        i2cTransaction.readBuf = data;
        i2cTransaction.writeCount = 1;
        i2cTransaction.readCount = length;
    
        txBuffer[0] = regAddr & 0xFF; //LB Addr
    
        if (!I2C_transfer(handle, &i2cTransaction)) {
            GPIO_write(Board_LED2, Board_LED_ON);
            System_abort("Bad I2C transfer!");
        }
    }
    

    Please help me, where I'm going wrong.

    I don't have an oscilloscope to see what communication going on them.

    Regards,

    Yashwanth.

  • Hello Yashwanth,

    The EK-TM4C123GXL LaunchPad does not come with pull-up resistors for I2C. If the board you are using to connect to the INA219 sensor does not have pull-up's either, then you need to install those. Please check as I2C communication is not possible without the pull-up's.
  • Hi Ralphi Jacobi,

    Just now I came to know that 10k pull up resistors were installed on SCL and SDA lines. I'm using Adafruit's breakout board for INA219.

    Regards,
    Yashwanth
  • Hello Yashwanth,

    I'm sorry but I really don't understand the issue.

    If you pause a running system, it's common to have the PC in Load_IdleFxn since this is called in the Idle task (which runs when nothing else is happening).

    Please use Tools->ROV to help figure out the state of your system.

    Todd
  • Hi Todd,

    I'm sorry I don't that PC in Load_IdleFxn since this is called in the Idle task .
    Ya.. What you said is true . currentTask is in blocked state,Idle task is in running state always.

    My query is when debugging step by step after executing
    I2C_transfer(handle, &i2cTransaction)
    in writeregister() PC is not coming to the next line of code. I think something went wrong while transaction. But unable to figure out what the problem exactly.

    As per Datasheet, INA219 is of 16-Bit register type and I'm trying to write in the way mentioned above.

    Can you help and correct me is the correct way of writing to INA219.

    Regards,
    Yashwanth
  • The debugging with single step gets a little difficult with optimization on. You can try redcing the compiler optimization. That does not address the driver library code though. For that, you can either rebuild the library with no optimization or for a short-term experiment, add the I2C.c and I2CTiva.c files from the product into your project (and rebuild). Your project will pick up the local functions (which are compiled with the reduced optimization).

    Todd
  • Hi Todd,

    As you told I added I2C.c and I2CTiva.c files.

    While debugging in step by step mode . After executing

            /*
             * Wait for the transfer to complete here.
             * It's OK to block from here because the I2C's Hwi will unblock
             * upon errors
             */
            Semaphore_pend(Semaphore_handle(&(object->transferComplete)),
                                            BIOS_WAIT_FOREVER);

    in bool I2CTiva_transfer(I2C_Handle handle, I2C_Transaction *transaction) function in I2CTiva.c file

    PC didn't came out of this line.

    This means i'm doing something wrong in writing slave address,register address and value ?

    Regards,

    Yashwanth Kumar Gandeti.

  • I'd avoid the single stepping with this one. Just put a breakpoint after the I2C_transfer and at the I2C ISR (I2CTiva_hwiFxn). Does the ISR ever run? If not, it sound like you either have a configuration or hardware issue. A scope would come in handy here to determine if anything is going at the I2C.

    If the ISR is running, does the data make sense (content and quantity).

    Todd
  • Dear Todd,

    As you suggested, kept breakpoint after the I2C_transfer and at the I2C ISR (I2CTiva_hwiFxn).
    ISR haven't run ever and didn't stop after the I2C_transfer breakpoint also.

    Configuration issue you mean while configuring I2C channel?
    I think hardware is fine as i'm able to use same I2c Pins to test with the other sensor (MPU - 6050) using I2c Communication.

    Regards,
    Yashwanth.
  • Ok, so the task is blocked on the semaphore (so the breakpoint after I2C_transfer is not getting hit). The ISR is not running (which would have called the Semphore_post).

    Configuration could be the settings in the "Board".c file (e.g. EK_TM4C123GXL.c) or the I2C address. If that same I2C is working with a different peripheral, it sounds like it is ok. Do you have correct I2C address and using a valid bitrate. This is starting to sound like you really need a scope to see what is going on with the wires.

    Todd
  • Hi Todd,

    I2C Address is valid (I'm using 0x40 as my slave address.)
    Bit rate as 100 KHz.

    I'll try to get a scope and tap the wires to know whats going on.
    I'll get back to you soon.

    Thanks a lot for your continous support in debugging my problem.

    Regards,
    Yashwanth Kumar.
  • Hi Todd,

    Now PC is hitting the breakpoint after the line I2C_transfer.
    As I2C_transfer() is returning false. System is aborting as per my code in the above mentioned.
    I think my structuring like reg address and value before transfer is wrong.
    Can you help in that?

    Regards,
    Yashwanth
  • Did you get the scope to see what is happening on the lines?
  • Hello Todd,

    Yeah I tapped the lines using Oscilloscope. The address of registers are not correct. I'll send the screenshot of oscilloscope.

    I'm trying to correct the register address structuring.

    Regards,

    Yashwanth

  • Hello Todd,

    INA219 using 5V supply and EK-TM4C123GXL is with 3.3V Will this be a problem? Should i use any level shifter to make 5V of INA219 to 3.3V ?

    Regards,
    Yashwanth
  • Hello Yashwanth,

    While the pins on the EK-TM4C123GXL are 5V tolerant, the analog specifications for functionality are only tolerant to VDD - i.e. 3.3V. You indeed will need to use level translators between the iNA219 signals and the EK-TM4C123GXL.
  • Hi Todd,

    Sorry its being long time to reply as I'm out of work.

    I found that INA219 is responding to my write and read operations when I created a seperate project. When I tried to include in my project configuring in TASK context it's not working . It's behaving like a old one (not coming out of the I2C_Transfer ()).
    What might be the problem do you think? I can attach both the codes if you agree to look into .

    Regards,
    Yashwanth
  • Please attach both projects. Make sure to denote which one is working.
  • HI Todd,

    Please find the attached projects denoted :

    Working_Project - Working .

    NotWorking_Project - not Working .

    Regards,

    Yashwanth

    Edit: Projects removed due to privacy issues of project

  • Dear Todd,

    Thank a lot for your continuous guidance in finding my problem.
    Finally I found the problem i.e., pin configuration problem I made I2C pins as GPIO after configuring them as I2C. Sorry for my mistake I troubled you lot. But In this process of debugging i came to know what all might be problems. It's really a good learning for me.
    Hope your support will be there in future to guide me if I stuck (Hoping not to) some where.
    Thank you all who support in this process and finally thanks to TI and TI Forum by creating such a wonderful platform we are able to discuss problems and solve.
    Hatsoff TI !!!

    Thanks and Regards,
    Yashwanth Kumar
  • Glad to hear you found the issue!