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.

C6455 i2cReadByte () error using CSL(Chip Support Library)

Other Parts Discussed in Thread: SPRC234

The following code is the implementation of the I2C code for C6455.
CSL (Chip Support Library) was used.

main () is the i2c code for reading the I2C-to-UART Device registry.
I2c code is executed, the data must be sent as follows.

slaveAddress(WRITE) + Ack + Reg + Ack + slaveAddress (READ) + Ack + readData + Ack

But i2cReadByte () also do not know the code is correct.
The error occurs in the code below.


/ * If Recieve Data Ready, then read the data * /
do {
     CSL_i2cGetHwStatus (hI2c, CSL_I2C_QUERY_RX_RDY,
    & response);
} While (response! = 1);


i2cReadByte (), please review the code.

 

/***************** C6455 I2C Code *********************/

CSL_I2cHandle    hI2c;
CSL_I2cObj       i2cObj;
 
void I2C_open(void)
{
 Bool  i2cEn;
 CSL_Status   status = CSL_SOK;
 
    /* Unlock the control register */
    CSL_FINST(((CSL_DevRegs*)CSL_DEV_REGS)->PERLOCK, DEV_PERLOCK_LOCKVAL,
               UNLOCK);
              
    /* Enable the I2C */
    CSL_FINST(((CSL_DevRegs*)CSL_DEV_REGS)->PERCFG0, DEV_PERCFG0_I2CCTL,
               ENABLE);

    do {
        i2cEn = (Bool) CSL_FEXT(((CSL_DevRegs*)CSL_DEV_REGS)->PERSTAT0,
                                  DEV_PERSTAT0_I2CSTAT);
    } while (i2cEn != TRUE);

    //printf("Powersaver clock for I2C is enabled\n");
     
      
    /* Clear local data structures */
    memset(&i2cObj, 0, sizeof(CSL_I2cObj));
        
    /* Initialize I2C module */
    status = CSL_i2cInit(NULL);

    if (status != CSL_SOK) {
        printf("I2C: Initialization error.\n");
        printf("\tReason: CSL_i2cInit [status = 0x%x].\n", status);
        //i2cFailCnt++;
        return;
    }
   
    /* open i2c */
    hI2c = CSL_i2cOpen(&i2cObj, CSL_I2C, NULL, &status);

    if ((status != CSL_SOK) || (hI2c == NULL)) {
        printf("I2C: Error opening the instance. \
               [status = 0x%x, hI2c = 0x%x]\n", status, hI2c);
        return;
    }
}

void I2C_HwSetup(Uint32 uSlaveAddress, Uint32 RW)
{
 CSL_I2cHwSetup  hwSetup;
 CSL_Status   status = CSL_SOK;

    hwSetup.mode           = CSL_I2C_MODE_MASTER;   // master mode(1),     
    hwSetup.dir            = RW;           // tx mode(1),      rx mode(0)
    hwSetup.addrMode       = CSL_I2C_ADDRSZ_SEVEN;  // 7-bit addressing mode(0)
    hwSetup.sttbyteen      = CSL_I2C_STB_DISABLE;   // normal mode(0)
    hwSetup.ownaddr        = uSlaveAddress;      // own address
    hwSetup.ackMode        = CSL_I2C_ACK_ENABLE;   // ACK mode(0)
    hwSetup.repeatMode     = CSL_I2C_REPEAT_MODE_DISABLE; // no repeat mode(0)
 
 hwSetup.runMode        = CSL_I2C_FREE_MODE_ENABLE;
 hwSetup.loopBackMode   = CSL_I2C_DLB_DISABLE;
 hwSetup.freeDataFormat = CSL_I2C_FDF_DISABLE;    // use 7/10bit addressing mode

    hwSetup.resetMode      = CSL_I2C_IRS_ENABLE;
    hwSetup.bcm            = CSL_I2C_BCM_DISABLE;   // disable back compatibility
    hwSetup.inten          = 0x00;  
    hwSetup.clksetup       = &clksetup;

    // i2c hwsetup
    status = CSL_i2cHwSetup(hI2c, &hwSetup);

    if (status != CSL_SOK) {
        printf("I2C: Error in I2C Hw Setup. [status = 0x%x]\n",status);
        return;
    }
}

Uint8 i2cReadByte(Uint8 uchAddress, Uint32 uSlaveAddress)
{
 Uint32 response;
 Uint32 datalen = 1;
 Uint32 BbResponse;
 Uint32 cmd_arg;
 CSL_Status status = CSL_SOK;
 Uint8 recv;
 
 Uint8 xmtBuf = REG_ADDR(uchAddress, CHAN_B);
 
 /* transmitter mode **************************************************/
 
  /* i2c hwsetup */
  I2C_HwSetup(uSlaveAddress, CSL_I2C_DIR_TRANSMIT);
  
  /* i2c out of reset */
  CSL_i2cHwControl(hI2c, CSL_I2C_CMD_OUTOFRESET, NULL); 
  
  /* set data count */
  CSL_i2cHwControl(hI2c, CSL_I2C_CMD_SET_DATA_COUNT, &datalen);
  
  // slave address
  CSL_i2cHwControl(hI2c,CSL_I2C_CMD_SET_SLAVE_ADDR,&uSlaveAddress);
  
  /* Enable I2C */
  status = CSL_i2cHwControl(hI2c, CSL_I2C_CMD_ENABLE, NULL);
  if (status != CSL_SOK) {
   printf("I2C: Error while enabling I2C. [status = 0x%x]\n", status);
   return 0;
  }
  
  /* Start I2C */
  status = CSL_i2cHwControl(hI2c, CSL_I2C_CMD_START, NULL);
  if (status != CSL_SOK) {
   printf("I2C: Error while starting I2C. [status = 0x%x]\n", status);
   return 0;
  } 
  
  /* Wait for the transfer to start */
  do {
   CSL_i2cGetBusBusy(hI2c,&BbResponse);
  } while(BbResponse != 1);
  
  /* If Transmit Data Ready interrupt flag bit is set */
  do {
   CSL_i2cGetHwStatus(hI2c, CSL_I2C_QUERY_TX_RDY, &response);
  } while(response != 1 );
  
  /* Write the data into Data Transmit register*/
  CSL_i2cWrite(hI2c, &xmtBuf);
  
  /* Clear the flag */
  cmd_arg  = CSL_I2C_CLEAR_XRDY;// | CSL_I2C_CLEAR_ARDY  |CSL_I2C_CLEAR_RRDY;
  if (CSL_i2cHwControl(hI2c,CSL_I2C_CMD_CLEAR_STATUS,&cmd_arg) != CSL_SOK)
   return;
   
  // Stop the transmission
  if (CSL_i2cHwControl(hI2c, CSL_I2C_CMD_STOP ,NULL) != CSL_SOK) {
   printf("err\r\n");
      return 0;
  }

 

 /* receiver mode ***********************************************************/
 
  I2C_HwSetup(uSlaveAddress, CSL_I2C_DIR_RECEIVE);
  
  CSL_i2cHwControl(hI2c, CSL_I2C_CMD_OUTOFRESET, NULL); 
  
  CSL_i2cHwControl(hI2c, CSL_I2C_CMD_SET_DATA_COUNT, &datalen);
  
  // slave address
  CSL_i2cHwControl(hI2c,CSL_I2C_CMD_SET_SLAVE_ADDR,&uSlaveAddress);
  
  // Enable I2C
  status = CSL_i2cHwControl(hI2c, CSL_I2C_CMD_ENABLE, NULL);
  if (status != CSL_SOK) {
    printf("I2C: Error while enabling I2C. [status = 0x%x]\n", status);
    return 0;
  }
  
  // Start I2C
  status = CSL_i2cHwControl(hI2c, CSL_I2C_CMD_START, NULL);
  if (status != CSL_SOK) {
   printf("I2C: Error while starting I2C. [status = 0x%x]\n", status);
   return 0;
  } 
  
  // Wait for the transfer to start
  do {
    CSL_i2cGetBusBusy(hI2c,&BbResponse);
  } while(BbResponse != 1);
  

// The error occurs in the code below.
  /* If Recieve Data Ready , then read the data */
  do {         
      CSL_i2cGetHwStatus(hI2c, CSL_I2C_QUERY_RX_RDY, &response);
  } while (response != 1);
        
  /* Copy the data from Data Recieve register */
  CSL_i2cRead(hI2c, &recv);
  
  /* Clear the receive ready flag */
  cmd_arg  = CSL_I2C_CLEAR_RRDY;
  if (CSL_i2cHwControl(hI2c,CSL_I2C_CMD_CLEAR_STATUS,&cmd_arg) != CSL_SOK)
  return 0;
  
  /* Stop the transmission */
  if (CSL_i2cHwControl(hI2c, CSL_I2C_CMD_STOP ,NULL) != CSL_SOK) {
  printf("err\r\n");
    return 0;
  }
 
 return recv;
}

void main(void)
{
 Uint32 read_register = 0x07;
 Uint32 slaveAddress = 0x48;
 Uint8 recv;
 
 I2C_open();
 recv = i2cReadByte(read_register, slaveAddress);
 
 printf(recv : %x\r\n", recv);
 
 return;
}

  • Hi Jeong Sng,

    Is this Source code supplied by TI ? If yes, Do you modify the supplied code for your project requirment? Would you please mention the details like name of the CSL package and version e.t.c

    Did you uncover the code errors in the CSL package ( which encloses the I2C example project ) of C6455 ? or in your own code?

    If it is your own code, my recommendation is to first try-out the working (I2C) examples on C6455. It is always good to start with something which already works. Go to the product folder of C6455 and download the "C6455 Chip Support Library (CSL) - SPRC234" under software support. You will find the entire set of examples with ready-made CCS project build files.

    Regards,

    Shankari

    -------------------------------------------------------------------------------------------------------

    Please click the Verify Answer button on this post if it answers your question.
    --------------------------------------------------------------------------------------------------------

     

  • CSL version 03.00.10.02 was used.
    csl example is loopback code. is run well.
    read / write function code that I have implemented.

    sc16is752 (i2c-to-uart) device to read and write the data.

    write function is not a problem.
        slaveAddr (W) + Ack + Register + Ack + Data + Ack.

    error occurs during the execution of a read function.
        slaveAddr (W) + Ack + Register + Ack + slaveAddr (R) + Ack, and read
  • Hi,

    there is little that I can do to help you. In my case, I used TI's code as is and it worked. check http://e2e.ti.com/support/dsp/c6000_multi-core_dsps/f/439/t/256786.aspx

    "You can download a working example here http://linux-c6x.org/files/releases/ibl-1.0/ , the file iss ibl-1.0-src.tar.gz File is under \src\util\i2cRead"

    Khaled.

  • Thank you.
    Sources and forums that I've seen before.

    I would like to use CSL.

    i2cReadByte () function, an error occurs.
    Please code review.