📄 i2c.c
字号:
#include "iic.h"
//#define DELAY_TIME 1000
#define DELAY_TIME 200
#define I2CDELAY(iterations) { \
volatile Int j; \
for(j = 0; j < iterations; j ++); \
}
static I2C_Config EVM642VIDEOIIC_repeat_wr_Config = {
0, /* master mode, i2coar; */
0, /* no interrupt, i2cimr; */
(20-5), /* scl low time, i2cclkl; */
(20-5), /* scl high time,i2cclkh; */
1, /* configure later, i2ccnt;*/
0, /* configure later, i2csar;*/
0x46a0, /* i2cmdr, */
/* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* repeat mode, i2ccnt is not used */
(75-1), /* 4MHz clock, i2cpsc */
};
static const I2C_Config EVM642VIDEOIIC_Config = {
0, /* master mode, i2coar; */
0, /* no interrupt, i2cimr; */
(20-5), /* scl low time, i2cclkl; */
(20-5), /* scl high time,i2cclkh; */
1, /* configure later, i2ccnt;*/
0, /* configure later, i2csar;*/
0x4620, /* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* no repeat mode */
(75-1), /* 4MHz clock, i2cpsc */
};
static I2C_Config EVM642VIDEOIIC_repeat_rd_Config = {
0, /* master mode, i2coar; */
0, /* no interrupt, i2cimr; */
(20-5), /* scl low time, i2cclkl; */
(20-5), /* scl high time,i2cclkh; */
1, /* configure later, i2ccnt;*/
0, /* configure later, i2csar;*/
0x4420, /* i2cmdr, */
/* master rx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* repeat mode, i2ccnt is not used */
(75-1), /* 4MHz clock, i2cpsc */
};
Uint8 IIC_read8(I2C_Handle hI2C,
Uint8 devAddress,
Uint32 subAddress
)
{
Uint8 read_data;
int i_read_data;
I2C_Config prevIICConfig;
/* make sure handle is valid */
if(hI2C == INV) {
return;
}
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* save old settings */
I2C_getConfig(hI2C, &prevIICConfig);
I2C_config(hI2C, &EVM642VIDEOIIC_repeat_wr_Config);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
/* Generate start condition */
I2C_start(hI2C);
I2CDELAY(DELAY_TIME);
read_data=(subAddress & 0xff);/* subaddress */
while(!I2C_xrdy(hI2C));
// I2C_writeByte(hI2C, read_data); CAUSE ERROR IN BIG-ENDIAN
*((Uint32 *)hI2C->i2cdxrAddr) = read_data;
I2CDELAY(DELAY_TIME);
/* Generate stop condition */
I2C_sendStop(hI2C);
I2CDELAY(DELAY_TIME);
/* Wait until bus is free */
while (I2C_bb(hI2C));
I2CDELAY(DELAY_TIME);
I2CDELAY(DELAY_TIME); /*It has to be added by double delay.Otherwise it refuses to work.*/
/* set I2C mode register */
I2C_config(hI2C, &EVM642VIDEOIIC_repeat_rd_Config);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
I2CDELAY(DELAY_TIME);
/* Generate start condition */
I2C_start(hI2C);
/* read the data */
while(!I2C_rrdy(hI2C));
// read_data = I2C_readByte(hI2C);
read_data = (Uint8) *((Uint32 *)hI2C->i2cdrrAddr);
I2CDELAY(DELAY_TIME);
/* Generate stop condition */
I2C_sendStop(hI2C);
I2CDELAY(DELAY_TIME);
/* Wait until bus is free */
while (I2C_bb(hI2C));
I2CDELAY(DELAY_TIME);
/* now restore the previous I2C settings */
I2C_config(hI2C, &prevIICConfig);
I2CDELAY(DELAY_TIME);
return read_data;
}
Uint16 IIC_read16(I2C_Handle hI2C,
Uint8 devAddress,
Uint32 subAddress
)
{
Uint16 read_data16;
Uint8 read1_data8;
Uint8 read2_data8;
Uint8 read_add;
int i_read_data;
I2C_Config prevIICConfig;
/* make sure handle is valid */
if(hI2C == INV) {
return;
}
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* save old settings */
I2C_getConfig(hI2C, &prevIICConfig);
I2C_config(hI2C, &EVM642VIDEOIIC_repeat_wr_Config);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
/* Generate start condition */
I2C_start(hI2C);
I2CDELAY(DELAY_TIME);
read_add=(subAddress & 0xff);/* subaddress */
while(!I2C_xrdy(hI2C));
// I2C_writeByte(hI2C, read_data); CAUSE ERROR IN BIG-ENDIAN
*((Uint32 *)hI2C->i2cdxrAddr) = read_add;
I2CDELAY(DELAY_TIME);
/* Generate stop condition */
I2C_sendStop(hI2C);
I2CDELAY(DELAY_TIME);
/* Wait until bus is free */
while (I2C_bb(hI2C));
I2CDELAY(DELAY_TIME);
I2CDELAY(DELAY_TIME); /*It has to be added by double delay.Otherwise it refuses to work.*/
/* set I2C mode register */
I2C_config(hI2C, &EVM642VIDEOIIC_repeat_rd_Config);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
I2CDELAY(DELAY_TIME);
/* Generate start condition */
I2C_start(hI2C);
/* read the data */
while(!I2C_rrdy(hI2C));
read1_data8 = I2C_readByte(hI2C);
DM642_wait(10);
read2_data8 = I2C_readByte(hI2C);
//read1_data8 = (Uint8) *((Uint32 *)hI2C->i2cdrrAddr);
//read2_data8 = (Uint8) *((Uint32 *)hI2C->i2cdrrAddr +1);
I2CDELAY(DELAY_TIME);
/* Generate stop condition */
I2C_sendStop(hI2C);
I2CDELAY(DELAY_TIME);
/* Wait until bus is free */
while (I2C_bb(hI2C));
I2CDELAY(DELAY_TIME);
/* now restore the previous I2C settings */
I2C_config(hI2C, &prevIICConfig);
I2CDELAY(DELAY_TIME);
read_data16 = (read1_data8 | (read1_data8<<8));
return read_data16;
}
/**********************************/
/*
* ======== _IIC_write ========
* This function performs write operation via I2C bus.
*/
void IIC_write(I2C_Handle hI2C,
Uint8 devAddress,
Uint32 subAddress,
Uint8 *data,
Uint16 numBytes
)
{
Int i;
I2C_Config prevIICConfig;
/* make sure handle is valid */
if(hI2C == INV) {
return;
}
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* save old settings */
I2C_getConfig(hI2C, &prevIICConfig);
/* set I2C mode register */
I2C_RSETH(hI2C, I2CMDR, EVM642VIDEOIIC_Config.i2cmdr);
/* set I2C imr register */
I2C_RSETH(hI2C, I2CIMR, EVM642VIDEOIIC_Config.i2cimr);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
/* set I2C count register */
I2C_RSETH(hI2C, I2CCNT, numBytes + 1);
/* write the sub address */
I2C_RSETH(hI2C, I2CDXR, subAddress);
/* Generate start condition */
I2C_start(hI2C);
I2CDELAY(DELAY_TIME);
/* write the data */
for(i = 0; i < numBytes; i ++) {
while(!I2C_xrdy(hI2C));
// I2C_writeByte(hI2C, *data ++); CAUSE ERROR IN BIG-ENDIAN
*((Uint32 *)hI2C->i2cdxrAddr) = *data ++;
I2CDELAY(DELAY_TIME);
}
/* Generate stop condition */
I2C_sendStop(hI2C);
I2CDELAY(DELAY_TIME);
/* Wait until bus is free */
while (I2C_bb(hI2C));
I2CDELAY(DELAY_TIME);
I2CDELAY(DELAY_TIME);
/* now restore the previous I2C settings */
/* set I2C mode register */
I2C_RSETH(hI2C, I2CMDR, prevIICConfig.i2cmdr);
/* set I2C imr register */
I2C_RSETH(hI2C, I2CIMR, prevIICConfig.i2cimr);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, prevIICConfig.i2csar);
/* set I2C count register */
I2C_RSETH(hI2C, I2CCNT, prevIICConfig.i2ccnt);
I2CDELAY(DELAY_TIME);
I2CDELAY(DELAY_TIME);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -