📄 hi_i2c.c
字号:
#include <config.h>#if defined(CONFIG_HI3560_LOGO)#include <common.h>#include "hi_i2c.h"static int i2cSendByte(unsigned char u8Value){ unsigned short usData = 0x00FF; unsigned short status; unsigned int i = 0; usData = u8Value & usData; /*set write CMD*/ IIC_REG_READ(IIC_TXFIFO_FLAG, status); /*check TX fifo status*/ while(status >= 8) /*TX FIFO full and wait for a while*/ { udelay(1000); if(++i>TIMECOUNT) { return 1; } IIC_REG_READ(IIC_TXFIFO_FLAG, status); } IIC_REG_WRITE(IIC_DATA_CMD,usData); /*write one byte to TX fifo*/ return 0; }/*! \brief Receives a character over I2C * * \return Character received * */static unsigned char i2cReceiveByte(void){ unsigned char ucValue; unsigned short status; unsigned int i = 0; IIC_REG_WRITE(IIC_DATA_CMD,0x0100); /*read Rx FIFO*/ IIC_REG_READ(IIC_RXFIFO_FLAG, status); while(status == 0) /*RX FIFO is empty and wait for a while*/ { udelay(1000); if(++i>TIMECOUNT) { return 1; } IIC_REG_READ(IIC_RXFIFO_FLAG, status); } IIC_REG_READ(IIC_DATA_CMD,ucValue); return(ucValue);}int i2cCheckSendResult(void){ unsigned short status; unsigned int i = 0; IIC_REG_READ(IIC_TXFIFO_FLAG, status); while(status != 0) /*TX FIFO is not empty and wait for a while*/ { udelay(1000); if(++i>TIMECOUNT) { return 1; } IIC_REG_READ(IIC_TXFIFO_FLAG, status); } return 0;}static void i2cSetAddress( unsigned char u8address){ IIC_REG_WRITE(IIC_ENABLE, 0); IIC_REG_WRITE(IIC_TAR, u8address>>1); IIC_REG_WRITE(IIC_ENABLE, 1);}/*! \brief Reads data from a client on the I2C bus * * \param client Device to read data from * \param address Address of data within device * * \return data received * */unsigned char I2C_Read( struct i2c_client *pstClient, unsigned char u8Address){ return I2C_SerialRead((unsigned char)(pstClient->addr), (unsigned char)u8Address );}/*for digital camera iic , added by wangaijun*/unsigned char SccbSerialRead( unsigned char u8DevAddress, unsigned char u8Address){ int rxdata = 0; return rxdata;}/*! \brief Writes data to a client on the I2C bus * * \param client Device to write data from * \param address Address of data within device * \param data Data to write to device * */int I2C_Write( struct i2c_client *client, unsigned char u8Address, unsigned char u8Data){ return I2C_SerialWrite(client->addr, u8Address, u8Data);}unsigned char I2C_SerialRead( unsigned char u8DevAddress, unsigned char u8Address){ int rxdata; i2cSetAddress((unsigned char)(u8DevAddress)); i2cSendByte(u8Address); rxdata = i2cReceiveByte(); return rxdata;}/*! \brief Writes data to a device on the I2C bus * * \param client Device to write data from * \param address Address of data within device * \param data Data to write to device * */int I2C_SerialWrite( int devAddress, int address, int data){ int ret; i2cSetAddress((unsigned char)(devAddress)); i2cSendByte(address); i2cSendByte(data); ret = i2cCheckSendResult(); return ret;}static int i2cInitialized = 0;void I2C_Init(void){ unsigned int cnt, hcnt, lcnt; if(i2cInitialized == 0) { IIC_REG_WRITE(IIC_ENABLE, 0); cnt = I2C_CLK/I2C_RATE; hcnt = (cnt/9)*4; lcnt = (cnt/9)*5; IIC_REG_WRITE(IIC_CON, CONTROL_VALUE); IIC_REG_WRITE(IIC_HCNT, hcnt); IIC_REG_WRITE(IIC_LCNT, lcnt); IIC_REG_WRITE(IIC_INTR_MASK, 0x0000); i2cInitialized = 1; return; } else { return; }}/* end of file hi_i2c.c */#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -