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/CC1350: cc1350 pca9658 communication through i2c not working.

Part Number: CC1350

Tool/software: TI-RTOS

hi,

i am trying to connect pca9685 with cc1350 using i2c. 

writing from cc1350 to pca9685 seems to be working fine but the code gets stuck in i2c_transfer function while reading. looks like pca is not responding (my best guess).

but the same pca9685 is working just fine with arduino.

all my connections seem to be right. 

i have tried both with and without pullups on sda scl lines

can you please help me find out whats going wrong.

#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
#include <inttypes.h>

#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>

#include "Board.h"

#define TASKSTACKSIZE       640

#define PCA9685_MODE1 0x0
#define PCA9685_PRESCALE 0xFE

unsigned int    i=4095;
uint8_t         txBuffer[5];
uint8_t         rxBuffer[2];
I2C_Handle      i2c;
I2C_Transaction i2cTransaction;

uint8_t read8(uint8_t addr)
{
    txBuffer[0] = addr;
    i2cTransaction.slaveAddress = 0x40;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 1;
    if (I2C_transfer(i2c, &i2cTransaction)) <<-------------------------------------------------- ADDED A BREAKPOINT HERE ---- AFTER I HIT RUN AGAIN ---- NEVER COMES OUT OF THIS
    {
        printf("\n=====\nsuccess at pca reading \n=====\n"); <<------------------------------------- NEVER EXECUTES
        //printf("\n=====\nsuccess at pca reading %\n=====\n", PRIu8, addr);
        return rxBuffer[0];
    }
    else
    {
        printf("\n=====\nfailure at pca reading %\n=====\n", PRIu8, addr);
        while(1);
        return 0;
    }
}

uint8_t write8(uint8_t addr, uint8_t data)
{
    txBuffer[0] = addr;
    txBuffer[1] = data;
    i2cTransaction.slaveAddress = 0x40;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 1;
    //sleep(2);
    if (I2C_transfer(i2c, &i2cTransaction))
    {
        printf("\n=====\nsuccess at pca writing \n=====\n");
        //printf("\n=====\nsuccess at pca writing %\n=====\n", PRIu8, addr);
        return 1;
    }
    else
    {
        printf("\n=====\nfailure at pca writing %\n=====\n", PRIu8, addr);
        while(1);
        return 0;
    }
}

void reset(void)
{
    txBuffer[0] = 0x00;
    txBuffer[1] = 0x80;
    i2cTransaction.slaveAddress = 0x40;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 1;
    sleep(2);
    if (I2C_transfer(i2c, &i2cTransaction))
    {
        printf("\n=====\npca reset command sent\n=====\n");
    }
    else
    {
        printf("\n=====\nfailure at pca reset\n=====\n");
        while(1);
    }
}

void setPwmFreq(float freq)
{
    printf("attempting to set freq: %f Hz\n=====\nsetting pre-scale: 3\n=====\n", freq);

    uint8_t oldmode = read8(PCA9685_MODE1);
    uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
    write8(PCA9685_MODE1, newmode); // go to sleep
    write8(PCA9685_PRESCALE, 3); // set the prescaler
    write8(PCA9685_MODE1, oldmode);
    sleep(5);
    write8(PCA9685_MODE1, oldmode | 0xa0);  //  This sets the MODE1 register to turn on auto increment.

    printf("new mode set to: %\n=====\n", PRIu8, read8(PCA9685_MODE1));

}

void *mainThread(void *arg0)
{
    I2C_Params      i2cParams;

    GPIO_init();
    I2C_init();

    GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    GPIO_setConfig(Board_GPIO_LED1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz;
    i2c = I2C_open(Board_I2C_TMP, &i2cParams);
    if (i2c == NULL) {
        while (1);
    }
    else {
    }

    reset();
    setPwmFreq(1600);

    txBuffer[0] = 0X00;
    txBuffer[1] = 0X00;
    txBuffer[2] = 0X00;
    txBuffer[3] = 0X00;
    txBuffer[4] = 0X00;
    i2cTransaction.slaveAddress = 0X40;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 5;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 1;
    sleep(2);
    GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_ON);

    while(1)
    {   i-=8;
        if(i==1)
            i=4095;
        txBuffer[0] = 0X06;
        txBuffer[1] = 0X00;
        txBuffer[2] = 0X00;
        txBuffer[3] = i;
        txBuffer[4] = i>>8;
        if (I2C_transfer(i2c, &i2cTransaction))
        {
            //printf("i2c transac\n");
            //printf(rxBuffer[0]);
            GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);
            GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_ON);
        }
        else
        {
            //printf("i2c NOT transac\n");
            GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
            GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_OFF);
        }

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

    I2C_close(i2c);

    return (NULL);
}