📄 iic.c
字号:
#include <csl.h>
#include <csl_i2c.h>
#include <TDS6713EVM.h>
#include <IIC.h>
// Set I2C registers. */
I2C_Config MyI2CCfgT = {
I2C_FMKS(I2COAR,A,OF(0x00)), //Not used if master
I2C_FMKS(I2CIMR,ICXRDY,MSK) |
I2C_FMKS(I2CIMR,ICRRDY,MSK) |
I2C_FMKS(I2CIMR,ARDY,MSK) |
I2C_FMKS(I2CIMR,NACK,MSK) |
I2C_FMKS(I2CIMR,AL,MSK),
//Master clock frequency is 200kHz(SYSCLK2 is 150MHz)
I2C_FMKS(I2CCLKL,ICCL,OF(6)),
I2C_FMKS(I2CCLKH,ICCH,OF(6)),
I2C_FMKS(I2CCNT,ICDC,OF(2)),
I2C_FMKS(I2CSAR,A,OF(26)),
I2C_FMKS(I2CMDR,FREE,RFREE) |
I2C_FMKS(I2CMDR,STT,START) |
I2C_FMKS(I2CMDR,MST,MASTER) |
I2C_FMKS(I2CMDR,TRX,XMT) |
I2C_FMKS(I2CMDR,RM,REPEAD) |
I2C_FMKS(I2CMDR,IRS,NRST) |
I2C_FMKS(I2CMDR,STB,NONE),
I2C_FMKS(I2CPSC,IPSC,OF(15-1)) // 10MHz
};
I2C_Config MyI2CCfgR = {
I2C_FMKS(I2COAR,A,OF(0x00)),
I2C_FMKS(I2CIMR,ICXRDY,MSK) |
I2C_FMKS(I2CIMR,ICRRDY,MSK) |
I2C_FMKS(I2CIMR,ARDY,MSK) |
I2C_FMKS(I2CIMR,NACK,MSK) |
I2C_FMKS(I2CIMR,AL,MSK),
// Master clock frequency is 200kHz(SYSCLK2 is 150MHz)
I2C_FMKS(I2CCLKL,ICCL,OF(6)),
I2C_FMKS(I2CCLKH,ICCH,OF(6)),
I2C_FMKS(I2CCNT,ICDC,OF(1)),
I2C_FMKS(I2CSAR,A,OF(0)),
I2C_FMKS(I2CMDR,FREE,RFREE) |
I2C_FMKS(I2CMDR,MST,MASTER) |
I2C_FMKS(I2CMDR,TRX,RCV) |
I2C_FMKS(I2CMDR,IRS,NRST) |
I2C_FMKS(I2CMDR,STB,NONE),
I2C_FMKS(I2CPSC,IPSC,OF(15-1)) // 10MHz
};
// I2C_write16() Write 16bits data to AIC23 registers. Configure AIC23 control register
// Parameters: hI2C(I2C id) regnum(The related register) regval(Register value to be written)
//Return: True or False.
Uint16 I2C_write16(I2C_Handle hI2C,Uint8 regnum,Uint8 regval)
{
Uint8 tempdata = 0;
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
//Configure I2C
I2C_config(hI2C,&MyI2CCfgT);
//Write the low data into Data Transmit register
tempdata = regnum;
I2C_writeByte(hI2C, tempdata&0xFF);
//To invoke the start condition
I2C_start(hI2C);
// Wait until MSB transmit is done
while(!I2C_xrdy(hI2C));
//Write the high data into Data Transmit register
tempdata = regval;
I2C_writeByte(hI2C, tempdata&0xFF);
//Generate Stop condition
I2C_sendStop(hI2C);
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
return TRUE;
}
// I2C_read16() Write 16bits data to AIC23 registers. Configure AIC23 control register.
// Parameters: hI2C(I2C id) regnum(The related register) regval(Register value to be written)
// Return: True or False
Uint16 I2C_read16(I2C_Handle hI2C,Uint8 regnum,Uint8 regval)
{
Uint8 tempdata = 0;
/* Send address. */
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
I2C_config(hI2C,&MyI2CCfgT);
//Write the low data into Data Transmit register
tempdata = regnum;
I2C_writeByte(hI2C, tempdata&0xFF);
//To invoke the start condition
I2C_start(hI2C);
//Wait until MSB transmit is done
while(!I2C_xrdy(hI2C));
//Write the high data into Data Transmit register.
tempdata = regval;
I2C_writeByte(hI2C, tempdata&0xFF);
//Generate Stop condition
I2C_sendStop(hI2C);
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
//Wait for the time from transmit to receive
TDS6713EVM_wait(0x200);
//Receive data
I2C_config(hI2C,&MyI2CCfgR);
//Generate start condition, starts transmission
I2C_start(hI2C);
while(I2C_FGETH(hI2C,I2CSTR,ARDY));
//Wait until MSB transmit is done
while(!I2C_rrdy(hI2C));
//Submit the MSB for transmit
regval = I2C_RGETH(hI2C, I2CDRR);
//Generate stop condition
I2C_sendStop(hI2C);
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
return TRUE;
}
//This function waits until the I2C bus busy bit is reset
waitForBusFree(I2C_Handle hI2C)
{
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
while(I2C_bb(hI2C));
}
//This function waits till the I2C bus busy bit gets set
waitForBusBusy(I2C_Handle hI2C)
{
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to set
while(!I2C_bb(hI2C));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -