?? iic.c
字號:
/***********************************************************/
/* Copyright 2008 by BTECH Incorporated. */
/* All rights reserved. Property of BTECH Incorporated. */
/* */
/***********************************************************/
#include "iic.h"
#define I2CDELAY(iterations) { \
volatile Int j; \
for(j = 0; j < iterations; j ++); \
}
#define DELAY_TIME 0x4000
/*BTECHDM642的IIC的設置結構*/
static I2C_Config EVM642VIDEOIIC_WriteByteConfig = {
0, /* master mode, i2coar;采用主模式 */
0, /* no interrupt, i2cimr;只寫,不讀,采用無中斷方式*/
(20-5), /* scl low time, i2cclkl; */
(20-5), /* scl high time,i2cclkh; */
2, /* configure later, i2ccnt;*/
0, /* configure later, i2csar;*/
0x4620, /* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* no repeat mode */
(75-1), /* 2MHz clock, i2cpsc */
};
static I2C_Config EVM642VIDEOIIC_ReadByteConfig = {
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, /* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* no repeat mode */
(75-1), /* 2MHz clock, i2cpsc */
};
static const I2C_Config EVM642VIDEOIIC_WriteConfig = {
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, /* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* repeat mode */
(75-1), /* 4MHz clock, i2cpsc */
};
static const I2C_Config EVM642VIDEOIIC_ReadConfig = {
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;*/
0x44a0, /* master rx mode, */
/* i2c runs free, */
/* 8-bit data + ACK */
/* repeat mode */
(75-1), /* 4MHz clock, i2cpsc */
};
/*
* ======== _IIC_write ========
* This function performs write operation via I2C bus.
*/
/* Spin in a delay loop for delay iterations */
void EVMDM642_wait(Uint32 delay)
{
volatile Uint32 i, n;
n = 0;
for (i = 0; i < delay; i++)
{
n = n + 1;
}
}
/* Spin in a delay loop for delay microseconds */
void EVMDM642_waitusec(Uint32 delay)
{
EVMDM642_wait(delay * 21);
}
/*by hj*/
void _IIC_write_byte(I2C_Handle hI2C,
Uint8 devAddress,
Uint32 subAddress,
Uint8 data
)
{
I2C_Config prevI2CCfg;
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* Save old settings */
I2C_getConfig(hI2C, &prevI2CCfg);
/* Restore settings for AIC23 */
EVM642VIDEOIIC_WriteByteConfig.i2csar = devAddress;
I2C_config(hI2C, &EVM642VIDEOIIC_WriteByteConfig);
/* Submit the MSB for transmit */
I2C_RSETH(hI2C, I2CDXR, (subAddress) & 0xff);
/* Generate start condition, starts transmission */
I2C_start(hI2C);
/* Wait until MSB transmit is done */
while(!I2C_xrdy(hI2C));
/* Submit the LSB for transmit */
I2C_RSETH(hI2C, I2CDXR,data);
EVMDM642_waitusec(350);
/* Generate stop condition */
I2C_sendStop(hI2C);
/* Wait until bus is free */
while (I2C_bb(hI2C));
EVMDM642_waitusec(350);
/* Reconfigure I2C with old settings */
I2C_config(hI2C, &prevI2CCfg);
}
/*
* ======== _IIC_read ========
* This function performs read from operation via I2C bus.
*/
/*by hj*/
void _IIC_read_byte(I2C_Handle hI2C,
Uint8 devAddress,
Uint32 subAddress,
Uint8 *data
)
{
I2C_Config prevI2CCfg;
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* Save old settings */
I2C_getConfig(hI2C, &prevI2CCfg);
/* Restore settings for AIC23 */
EVM642VIDEOIIC_WriteByteConfig.i2csar = devAddress;
I2C_config(hI2C, &EVM642VIDEOIIC_WriteByteConfig);
/* Submit the MSB for transmit */
I2C_RSETH(hI2C, I2CDXR, (subAddress) & 0xff);
/* Generate start condition, starts transmission */
I2C_start(hI2C);
/* Wait until MSB transmit is done */
while(!I2C_xrdy(hI2C));
/* Generate stop condition */
I2C_sendStop(hI2C);
EVMDM642_waitusec(20);
/* Reconfigure I2C with old settings */
I2C_config(hI2C, &prevI2CCfg);
I2C_getConfig(hI2C, &prevI2CCfg);
/*從發送到接收需一段時間轉換*/
EVMDM642_waitusec(0x200);
/* Restore settings for AIC23 */
EVM642VIDEOIIC_ReadByteConfig.i2csar = devAddress;
I2C_config(hI2C, &EVM642VIDEOIIC_ReadByteConfig);
/* 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 */
*data = I2C_RGETH(hI2C, I2CDRR);
/* Generate stop condition */
I2C_sendStop(hI2C);
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* Short delay for AIC23 to accept command */
EVMDM642_waitusec(20);
/* Reconfigure I2C with old settings */
I2C_config(hI2C, &prevI2CCfg);
}
/*
* ======== _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_WriteConfig.i2cmdr);
/* set I2C imr register */
I2C_RSETH(hI2C, I2CIMR, EVM642VIDEOIIC_WriteConfig.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_writeByte(hI2C, 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 ++);
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);
}
void _IIC_read(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_WriteConfig.i2cmdr);
/* set I2C imr register */
I2C_RSETH(hI2C, I2CIMR, EVM642VIDEOIIC_WriteConfig.i2cimr);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
/* set I2C count register */
I2C_RSETH(hI2C, I2CCNT, 1);
/* write the sub address */
I2C_writeByte(hI2C, subAddress);
/* Generate start condition */
I2C_start(hI2C);
I2CDELAY(DELAY_TIME);
/* waiting for sub-address to be transmitted */
while(!I2C_xrdy(hI2C));
/* now enter the master receiver mode */
I2C_RSETH(hI2C, I2CMDR, EVM642VIDEOIIC_ReadConfig.i2cmdr);
/* set I2C count register */
I2C_RSETH(hI2C, I2CCNT, numBytes);
/* clear the DRR register */
I2C_readByte(hI2C);
/* Generate start condition */
I2C_start(hI2C);
I2CDELAY(DELAY_TIME);
/* read the data */
for(i = 0; i < numBytes; i ++) {
while(!I2C_rrdy(hI2C));
*data++ = I2C_readByte(hI2C);
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);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -