int I2C_Read(unsigned char msSlaveAddr,unsigned char* writeDataP,
int wrDataLen, unsigned char* readDataP,int rdDataLen)
{
int retval;
hw_i2c.DevAddr = msSlaveAddr;
RTOS_MutexLock(&i2c_mutex);
if(wrDataLen==0)
retval=HWI2C_Read_Addr(&hw_i2c, 0,readDataP,rdDataLen,0);
else if(wrDataLen==1) //support restart mode
retval=HWI2C_Read_Addr(&hw_i2c, *writeDataP, readDataP,rdDataLen,1);
else{
retval=HWI2C_Write_Addr(&hw_i2c, 0,writeDataP, wrDataLen,0);
retval|=HWI2C_Read_Addr(&hw_i2c,0, readDataP,rdDataLen,0);
}
RTOS_MutexUnLock(&i2c_mutex);
return retval==RM_OK ? 0 : -1;
}
int I2C_Write(unsigned char msSlaveAddr,unsigned char subaddr,unsigned char* dataP,int dataLen)
{
int retval;
hw_i2c.DevAddr = msSlaveAddr;
RTOS_MutexLock(&i2c_mutex);
retval=HWI2C_Write_Addr(&hw_i2c,subaddr,dataP, dataLen,1);
RTOS_MutexUnLock(&i2c_mutex);
return retval==RM_OK ? 0 : -1;
}
int I2C_Write_NoSubAddr(unsigned char msSlaveAddr,unsigned char* dataP,int dataLen)
{
int retval;
hw_i2c.DevAddr = msSlaveAddr;
RTOS_MutexLock(&i2c_mutex);
retval=HWI2C_Write_Addr(&hw_i2c, 0, dataP, dataLen, 0);
RTOS_MutexUnLock(&i2c_mutex);
return retval==RM_OK ? 0 : -1;
}
这个是驱动里的i2c代码
我要填充的函数是:
/*****************************************************************************
**
** Name: Auvitek_WriteReg
**
** Description: Write values to device .
**
** Parameters:
** nAddr - the register address of AU85xx to be written
** bData - the data to be written to the register
** Global variable bMainAddr - the device address (IIC address) of AU85xx. 0x8E or 0x80
** For Auvitek module or the module built following Auvitek
** reference design, it is 0x8E. And the default value of
** this variable in our code is 0x8E.
**
**
** Returns: status:
** STATUS_OK - No errors
** user-defined
**
** Notes: You MUST provide code for this function
**
**
** Revision History:
**
**
*****************************************************************************/
UINT16 Auvitek_WriteReg( UINT16 nAddr, UINT8 bData)
{
/*UINT16 status=STATUS_OK;
INT8 n;
unsigned char add;
add =
n=I2C_Write(0x8E,add,&bData,1);
if(n==0) return status;
else return 0x0005;*/
UINT8 buf[3];
UINT16 n;
buf[0] = 0x80|(nAddr>>8);
buf[1] = nAddr&0xFF;
buf[2] = bData;
n=I2C_Write_NoSubAddr(0x8E,buf,3);
if(n==0) return 0x0001;
else return 0x0005;
/*
** Todo:add code to implement a write operation
** For IIC accessing, please read our application note. Here is just a brief.
** device address (bMainAddr), control code (0x80), register address of AU85xx (nAddr) and data (bData)
** will be put in 4 bytes and sent through IIC one by one. The following is a brief description of writting steps
** 1. Start
** 2. Device address with the last bit is 0
** 3. ACK
** 4. Control code
** 5. ACK
** 6. Register address
** 7. ACK
** 8. Data
** 9. ACK
** 10. Stop
**
** Please pay more attention on control code and register address. Be noticed that the high 3 bits of register address should be put in low 3 bits
** of control code
*/
}
i2c不认得什么寄存器地址,原理是第一个过来的是器件地址,以后的每个器件自己规定。而我们的规范是器件地址,控制代码,寄存器地址,数据。而我们可以用的接口有I2C_Write(unsigned char msSlaveAddr,unsigned char subaddr,unsigned char* dataP,int dataLen),I2C_Write_NoSubAddr(unsigned char msSlaveAddr,unsigned char* dataP,int dataLen)。由于我们要发送控制代码所以选择I2C_Write_NoSubAddr。
/*****************************************************************************
**
** Name: Auvitek_ReadReg
**
** Description: Read values from the register .
**
** Parameters:
** nAddr - the register address to be read
** pData - pointer to the Data to receive the value from the register
**
** Global variable bMainAddr - the device address (IIC address) of AU85xx. 0x8E or 0x80
** For Auvitek module or the module built following Auvitek
** reference design, it is 0x8E. And the default value of
** this variable in our code is 0x8E.
** Returns: status:
** STATUS_OK - No errors
** user-defined
**
** Notes: You MUST provide code for this function
**
** Revision History:
**
*****************************************************************************/
UINT16 Auvitek_ReadReg(UINT16 nAddr, UINT8 *pData)
{
/* UINT16 status=STATUS_OK;
INT8 n;//wangyi
unsigned char aa;
aa=(unsigned char)nAddr;
n=I2C_Read(0x8E,&aa,1,pData,1);//wangyi
if(n==0) return status;//wangyi
else return 0x0005;//wangyi*/
UINT8 buf[3];
UINT16 status;
buf[0] = 0x40|(nAddr>>8);
buf[1] = nAddr&0xFF;
buf[2] = 0x00;
status = I2C_Write_NoSubAddr(0x8E,buf,3);
status |= I2C_Read(0x8E,NULL,0,pData,1);
if(status==0) return 0x0001;
else return 0x0005;
/*
** Todo: add code to implement a read operation
** For IIC accessing, please read our application note. Here is just a brief.
** device address (bMainAddr), control code (0x40), register address of AU85xx (nAddr) and 1 dummy byte
** will be put in 4 bytes and sent through IIC one by one. The following is a brief description of writting steps
** 1. Start
** 2. Device address with the last bit is 0
** 3. ACK
** 4. Control code
** 5. ACK
** 6. Register address
** 7. ACK
** 8. 1 dummy byte
** 9. ACK
** 10. Stop
** 11. Start
** 12. device address with the last bit is 1
** 13. ACK
** 14. value of a register
** 15. ACK
** 16. Stop
** Please pay more attention on control code and register address. Be noticed that the high 3 bits of register address should be put in low 3 bits
** of control code
*/
/* return status; */
}
这个上面讲的很清楚了,先写后读,先写设备地址在控制代码,在寄存器地址,空字节。后读,设备地址,取出寄存器的值缩放的位置。
如果器件没有定义自己的规范,那我们就直接利用驱动中所提供的函数,把设备地址,寄存器地址,读写的值等参数放好就行。如果他有自己的规范则按照他的规范来做