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/TM4C1294NCPDT: Reading 2 bytes from I2C slave device with Tiva I2C1 configured as master

Part Number: TM4C1294NCPDT

Tool/software: TI-RTOS

I have set a sensor LTC4151 at address 0xD0 tied to I2C1 interface of Tiva as master. I am attaching a simple code where I send the address of the register I wish to read from the LTC4151 to the slave LTC4151 device 

I see that the code I used has been tried by others but I am not able to read data

#define SLAVE_ADDRESS 0xD0 // Address appears from bits 7:1 D0 shifted 1 place to right is 68
#define NUM_OF_I2CBYTES 2

//*****************************************************************************
//
// Main Program to Configure and Use the I2C Master I2C1
//
//*****************************************************************************
int
main(void)
{

// Setup System Clock for 120MHz
//
ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ |
SYSCTL_CFG_VCO_480), 120000000);

// Enable GPIO for Configuring the I2C Interface Pins
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);

// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOG));

//
// Configure Pins for I2C1 Master Interface
//
GPIOPinConfigure(GPIO_PG0_I2C1SCL);
GPIOPinConfigure(GPIO_PG1_I2C1SDA);

GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_1);
GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_0);

//
// Stop the Clock, Reset and Enable I2C Module
// in Master Function
//
SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C1);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);


//
// Wait for the Peripheral to be ready for programming
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C1));

// Initialize and Configure the Master Module
//
I2CMasterInitExpClk(I2C1_BASE, ui32SysClock, true);

// --------------------------To Write before read-----------------------

I2CMasterSlaveAddrSet (I2C1_BASE, SLAVE_ADDRESS, false); //set slave address and initiate with write

I2CMasterControl (I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND); //Send STOP bit

I2CMasterDataPut (I2C1_BASE, 0x00); //Put 0x00 -- send address of register to be read from SLAVE

while (!(I2CMasterBusy(I2C1_BASE))); //Wait till end of transaction
// while ((I2CMasterBusy(I2C1_BASE))); //Wait till end of transaction

I2CMasterSlaveAddrSet (I2C1_BASE, SLAVE_ADDRESS, true); //set slave address and initiate with read

I2CMasterControl (I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); //Read the 1st Byte

pui32DataRx[0] = I2CMasterDataGet(I2C1_BASE);

while (!(I2CMasterBusy(I2C1_BASE))); //Wait till end of transaction
// while ((I2CMasterBusy(I2C1_BASE))); //Wait till end of transaction


I2CMasterControl (I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); //Read the 2nd Byte

pui32DataRx[1] = I2CMasterDataGet(I2C1_BASE);

while (!(I2CMasterBusy(I2C1_BASE))); //Wait till end of transaction
// while ((I2CMasterBusy(I2C1_BASE))); //Wait till end of transaction

while (1)
{
}
} //end main

  • Before someone tries to debug your code, have you looked at the I2C lines with an oscilloscope? What does your code do? Does it get all of the way to the while(1) loop at the end.
  • The master bus is stuck after I send the address of the register in the slave I want to read. I2C_MCS is 0x1!

    I have a TI RTOS program which reads 20 samples from register 0x00 at the set SLAVE ADDRESS. Here the I2C)MCS is 0x20!

    I will put a scope and analyze on Monday.

    In the mean time I attach both projects (TIRTOS which reads values and the non TI RTOS code which I pasted yesterday in the forum) for your information.

    Regards

    Pavitra

  • This is the driver file for TI RTOS Program which seems to read from my LTC4151 devcice

    i2ctmp006.c
    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    /*
    * Copyright (c) 2015, Texas Instruments Incorporated
    * All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    *
    * * Redistributions of source code must retain the above copyright
    * notice, this list of conditions and the following disclaimer.
    *
    * * Redistributions in binary form must reproduce the above copyright
    * notice, this list of conditions and the following disclaimer in the
    * documentation and/or other materials provided with the distribution.
    *
    * * Neither the name of Texas Instruments Incorporated nor the names of
    * its contributors may be used to endorse or promote products derived
    * from this software without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */
    /*
    * ======== i2ctmp006.c ========
    */
    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/runtime/System.h>
    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    /* TI-RTOS Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/I2C.h>
    /* Example/Board Header files */
    #include "Board.h"
    /**************************Global Variables ***********************************/
    uint32_t voltage;
    uint8_t txBuffer[1];
    uint8_t rxBuffer[2];
    #define TASKSTACKSIZE 640
    Task_Struct task0Struct;
    Char task0Stack[TASKSTACKSIZE];
    /*
    * ======== taskFxn ========
    * Task for this function is created statically. See the project's .cfg file.
    */
    Void taskFxn(UArg arg0, UArg arg1)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    This is the non TI RTOS (Tivaware Program) which exhibits bus Busy behavior after the Tiva Master attempts to send the register (0x00) to access information.

    4478.main.c
    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    /*
    * main.c
    *
    * Created on: Apr 26, 2018
    * Author: Pramanujam
    */
    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_types.h"
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "utils/random.h"
    #include "utils/uartstdio.h"
    #define SLAVE_ADDRESS 0x68 // Address appears from bits 7:1 D0 shifted 1 place to right is 68
    #define NUM_OF_I2CBYTES 2
    /*****************************************************************************
    //
    // Global variables
    //
    //*****************************************************************************/
    uint32_t pui32DataRx[NUM_OF_I2CBYTES];
    void I2C1IntHandler(void)
    {
    }
    //*****************************************************************************
    //
    // This function sets up UART0 to be used for a console to display information
    // as the example is running.
    //
    //*****************************************************************************
    void
    InitConsole(void)
    {
    //
    // Enable GPIO port A which is used for UART0 pins.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    //
    // Configure the pin muxing for UART0 functions on port A0 and A1.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Were you able to resolve this issue after getting scope pictures of the I2C bus?
  • I have not been able to get around to that task.

    It does look a timing issue and analogous TI RTOS program is working but this is not.

    Will post next week.

    Thanks

  • Actually the SDA and SCL lines were flipped while configuring in my code. Thats why it did not work previously.

    Now its working! Thanks

    //
    GPIOPinConfigure(GPIO_PG0_I2C1SCL);
    GPIOPinConfigure(GPIO_PG1_I2C1SDA);

    GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_1); // configure SDA
    GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_0); // Configure SCL

    In the last two lines I had the pin numbers flipped.