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/CC2640R2F: Trying to get accel value from mpu6050 with i2c example

Part Number: CC2640R2F
Other Parts Discussed in Thread: CC2640

Tool/software: Code Composer Studio

Hi again.

I'm trying to use mpu6050 with cc2640 launchpad.

I started i2c with tidriver example i2ctmp007 and trying editing the example a little bit.

Here's a part of code

/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
printf("Starting the i2ctmp007 example\n");

/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C_TMP, &i2cParams);
if (i2c == NULL) {
printf("Error Initializing I2C\n");
while (1);
}
else {
printf("I2C Initialized!\n");
printf("let's go!");
}

I'm using printf to check where error occurs, and there's strange thing happening.

Console print "I2C Initialized!" but not "let's go!".

I have no idea what the problem is.

Anybody knows..?

Thank you!

  • Hello Changmin,

    Try adding "\n" at the end of "let's go!" like you did for the other printf to flush the buffer.

    Thanks
    ki
  • Wow, it totally works! Thanks a lot!
  • Sorry, another problem has occured.
    Here's the code

    for (i = 0; i < 20; i++) {
    printf("I'm in the loop!\n");
    if (I2C_transfer(i2c, &i2cTransaction)) {
    printf("what? \n");
    txBuffer[0] = 0x3B;
    i2cTransaction.slaveAddress = Board_TMP_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;
    temperature = (rxBuffer[0] << 8) | (rxBuffer[1] );
    printf("please!\n");

    temperature /= 1000;

    printf("Sample %u: %d (C)\n", i, temperature);
    }
    else {
    printf("I2C Bus fault\n");
    }

    /* Sleep for 1 second */
    sleep(1);
    }

    and now it prints "I'm in the loop!", but it doesn't print neither "What?" nor "I2C Bus fault".
    Out of the frying pan into the fire.
  • Changmin Lee said:
    and now it prints "I'm in the loop!", but it doesn't print neither "What?" nor "I2C Bus fault".

    Maybe the I2C_transfer() function hasn't returned.

    Is the i2cTransaction variable initialized before the first call to I2C_transfer(i2c, &i2cTransaction) ?

  • Yes I did. I'm using tidriver i2ctmp007 example,


    txBuffer[0] = TMP007_OBJ_TEMP;
    i2cTransaction.slaveAddress = Board_TMP_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;

    for (i = 0; i < 20; i++) {
    if (I2C_transfer(i2c, &i2cTransaction)) {

    temperature = (rxBuffer[0] << 6) | (rxBuffer[1] >> 2);

    if (rxBuffer[0] & 0x80) {
    temperature |= 0xF000;
    }

    temperature /= 32;

    printf("Sample %u: %d (C)\n", i, temperature);
    }
    else {
    printf("I2C Bus fault\n");
    }


    This is the part receiving data from tmp sensor. Referring this part, I tried to set up mpu6050 as following

    uint8_t setupBuffer[2];
    setupBuffer[0] = 0x6B;
    setupBuffer[1] = 0;
    i2cTransaction.slaveAddress = Board_TMP_ADDR;
    i2cTransaction.writeBuf = setupBuffer;
    i2cTransaction.writeCount = 2;
    I2C_transfer(i2c, &i2cTransaction);
    printf("setup complete!\n");
    All I changed from other parts of example is Board_TMP_ADDR. I changed it to 0x68, which is the address of mpu6050,
    and trying to write value 0 to register 0x6B.
    It doesn't print "setup complete!"

    Transaction variable initializing part is as following, same as the example.

    uint16_t temperature;
    uint8_t txBuffer[1];
    uint8_t rxBuffer[2];
    I2C_Handle i2c;
    I2C_Params i2cParams;
    I2C_Transaction i2cTransaction;
  • Changmin Lee said:
    and now it prints "I'm in the loop!", but it doesn't print neither "What?" nor "I2C Bus fault".

    Can you actually single step over those printf statements, not see no C I/O output?

  • Umm.. Sorry, I don't understand.
    Do you mean I should not use printf..?
    It doesn't work without printf either.
  • Actually I am asking if the printf statement is being reached by your application. One way to tell is if you can actually single step over it with the debugger
  • Actually, this is the first time using CCS (even using ARM) so I'm not familiar with debugger.
    Do you have link I can get information about it?

    And just in case it can help, here's the full code. 
    The address of mpu6050 is 0x68, and I'm trying to wirte 0 to register 0x6B.

    It prints

    "Starting the i2ctmp007 example"
    "I2C Initialized!"
    "let's go!"
    "leggo"

    Thank you sincerly for your concern.

    
    
    /*
     *    ======== i2ctmp007.c ========
     */
    #include <stdint.h>
    #include <stddef.h>
    #include <unistd.h>
    #include <stdio.h>
    
    
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/I2C.h>
    
    
    /* Example/Board Header files */
    #include "Board.h"
    
    #define TASKSTACKSIZE       640
    
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        unsigned int    i;
        uint16_t        temperature;
        uint8_t         txBuffer[1];
        uint8_t         rxBuffer[2];
        I2C_Handle      i2c;
        I2C_Params      i2cParams;
        I2C_Transaction i2cTransaction;
    
    
        /* Call driver init functions */
    
        GPIO_init();
        I2C_init();
    
    
        /* Turn on user LED */
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
        printf("Starting the i2ctmp007 example\n");
    
        /* Create I2C for usage */
        I2C_Params_init(&i2cParams);
        i2cParams.bitRate = I2C_400kHz;
        i2c = I2C_open(Board_I2C_TMP, &i2cParams);
        if (i2c == NULL) {
            printf("Error Initializing I2C\n");
            while (1);
        }
        else {
            printf("I2C Initialized!\n");
            printf("let's go!\n");
        }
        printf("leggo\n");
    
    
    // setup mpu6050
        uint8_t          setupBuffer[2];
        setupBuffer[0] = 0x6B;
        setupBuffer[1] = 0;
        i2cTransaction.slaveAddress = 0x68;
        i2cTransaction.writeBuf = setupBuffer;
        i2cTransaction.writeCount = 2;
        i2cTransaction.readBuf = NULL;
        i2cTransaction.readCount = 0;
        I2C_transfer(i2c, &i2cTransaction);
        printf("setup complete!\n");
    
    
    
        /* Take 20 samples and print them out onto the console */
        for (i = 0; i < 20; i++) {
            printf("I'm in the loop!\n");
    
            txBuffer[0] = 0x3B;
            i2cTransaction.slaveAddress = 0x68;
            i2cTransaction.writeBuf = txBuffer;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = rxBuffer;
            i2cTransaction.readCount = 2;
    
    
            if (I2C_transfer(i2c, &i2cTransaction)) {
    
    
                temperature = (rxBuffer[0] << 8) | (rxBuffer[1] );
    
                temperature /= 1000;
    
                printf("Sample %u: %d (C)\n", i, temperature);
            }
            else {
                printf("I2C Bus fault\n");
            }
    
            /* Sleep for 1 second */
            sleep(1);
        }
    
        /* Deinitialized I2C */
        I2C_close(i2c);
        printf("I2C closed!\n");
    
        return (NULL);
    }
    


    // setup mpu6050
    uint8_t setupBuffer[2];
    setupBuffer[0] = 0x6B;
    setupBuffer[1] = 0;
    i2cTransaction.slaveAddress = 0x68;
    i2cTransaction.writeBuf = setupBuffer;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = NULL;
    i2cTransaction.readCount = 0;
    I2C_transfer(i2c, &i2cTransaction);
    printf("setup complete!\n");


    /* Take 20 samples and print them out onto the console */
    for (i = 0; i < 20; i++) {
    printf("I'm in the loop!\n");

    txBuffer[0] = 0x3B;
    i2cTransaction.slaveAddress = 0x68;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction)) {


    temperature = (rxBuffer[0] << 8) | (rxBuffer[1] );

    temperature /= 1000;

    printf("Sample %u: %d (C)\n", i, temperature);
    }
    else {
    printf("I2C Bus fault\n");
    }

    /* Sleep for 1 second */
    sleep(1);
    }

    /* Deinitialized I2C */
    I2C_close(i2c);
    printf("I2C closed!\n");

    return (NULL);
    }




  • Changmin Lee said:
    Actually, this is the first time using CCS (even using ARM) so I'm not familiar with debugger.
    Do you have link I can get information about it?

    I would start with the below link:

    http://dev.ti.com/tirex/#/?link=Development%20Tools%2FIntegrated%20Development%20Environments%2FCode%20Composer%20Studio%2FDebug%2FDocuments%2FUser%27s%20Guides%2FDebug%20Handbook

    Also the below link is recommended:

    http://processors.wiki.ti.com/index.php/GSG:Debugging_projects_v5#Launching_the_debugger

    Note Fig 6 in the above link. It is a graphic of the debugging toolbar. The yellow arrows are source stepping buttons.

    Hope this helps

    ki

  • Oh now I see. Thanks

    And It stops right after I step over

    I2C_transfer(i2c, &i2cTransaction);

     , just before printing "setup complete!".

  • I got it.
    Now I know where the problem is, but still don't know what to do.
    Maybe I should post another question.
    You are helping me a lot. Thanks ki!
  • Yes, it is a different issue, so please post in a new thread. Also, sine it is not a CCS tools related question, please post in the "Bluetooth Low Energy' Forum. The experts there can help you best.

    Thanks
    ki