The following code istheimplementation of theI2Ccode forC6455.
CSL (Chip Support Library)was used.
main ()isthe i2c codefor reading the I2C-to-UART Device registry.
I2ccode is executed,thedatamust be sentas follows.
slaveAddress(WRITE) + Ack + Reg + Ack + slaveAddress (READ) + Ack + readData + Ack
Buti2cReadByte ()alsodo not knowthe codeis correct.
Theerror occursin 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 (), pleasereview 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);
// Theerror occursin 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;
}