📄 drvswiic.c
字号:
//SWIIC_Delay();
}
if (bAck)
{
// acknowledge
SWIIC_SDA_Chk(I2C_ACKNOWLEDGE); //SWIIC_SDA_Chk
}
else
{
// non-acknowledge
SWIIC_SDA_Chk(I2C_NON_ACKNOWLEDGE);
}
SWIIC_Delay();
SWIIC_SCL_Chk(_HIGH); //SWIIC_SCL_Chk
SWIIC_Delay();
SWIIC_SCL(_LOW); //SWIIC_SCL
SWIIC_Delay();
SWIIC_Delay(); //jaly test
SWIIC_Delay();
SWIIC_Delay();
SWIIC_Delay(); //jaly test
SWIIC_Delay();
SWIIC_Delay();
return ucReceive;
}
/******************************************************************************/
///Write bytes, be able to write 1 byte or several bytes to several register offsets in same slave address.
///@param u8SlaveID \b IN: Slave ID (Address)
///@param u8addrcount \b IN: register NO to write, this parameter is the NO of register offsets in pu8addr buffer,
///it should be 0 when *pu8addr = NULL.
///@param *pu8addr \b IN: pointer to a buffer containing target register offsets to write
///@param u16size \b IN: Data length (in byte) to write
///@param *pu8data \b IN: pointer to the data buffer for write
///@return BOOLEAN:
///- TRUE: Success
///- FALSE: Fail
/******************************************************************************/
BOOLEAN MDrv_IIC_WriteBytes(U8 u8SlaveID, U8 AddrCnt, U8* pu8addr, U16 u16size, U8* pBuf)
{
BYTE ucDummy; // loop dummy
ucDummy = I2C_ACCESS_DUMMY_TIME;// HH
while (ucDummy--)
{
if (IIC_AccessStart(u8SlaveID, SWIIC_WRITE) == FALSE)
{
continue;
}
while( AddrCnt )
{
AddrCnt--;
if ( IIC_SendByte( *pu8addr ) == FALSE )
return FALSE;
pu8addr++;
}
while (u16size ) // loop of writting data
{
u16size-- ;
//IIC_SendByte(*pBuf); // send byte
if ( IIC_SendByte( *pBuf ) == FALSE )
return FALSE;
pBuf++; // next byte pointer
}
break;
}
IIC_Stop();
return TRUE;
}
/******************************************************************************/
///Read bytes, be able to read 1 byte or several bytes from several register offsets in same slave address.
///@param u8SlaveID \b IN: Slave ID (Address)
///@param u8AddrNum \b IN: register NO to read, this parameter is the NO of register offsets in pu8addr buffer,
///it should be 0 when *paddr = NULL.
///@param *paddr \b IN: pointer to a buffer containing target register offsets to read
///@param u16size \b IN: Data length (in byte) to read
///@param *pu8data \b IN: pointer to retun data buffer.
///@return BOOLEAN:
///- TRUE: Success
///- FALSE: Fail
/******************************************************************************/
BOOLEAN MDrv_IIC_ReadBytes(U8 ucSlaveAdr, U8 ucSubAdr, U8* paddr, U16 ucBufLen, U8* pBuf)
{
BYTE ucDummy; // loop dummy
ucDummy = I2C_ACCESS_DUMMY_TIME;
while (ucDummy--)
{
if (IIC_AccessStart(ucSlaveAdr, SWIIC_WRITE) == FALSE)
{
continue;
}
while( ucSubAdr )
{
ucSubAdr--;
if (IIC_SendByte( *paddr ) == FALSE) return FALSE;
paddr++;
}
if (IIC_AccessStart(ucSlaveAdr, SWIIC_READ) == FALSE)
{
continue;
}
while (ucBufLen--) // loop to burst read
{
*pBuf = IIC_GetByte(ucBufLen); // receive byte
pBuf++; // next byte pointer
}
break;
}
IIC_Stop();
return TRUE;
}
/******************************************************************************/
///Read 1 byte through IIC
///@param u8SlaveID \b IN: Slave ID
///@param u8RegAddr \b IN: Target register offset to read
///@param *pu8Data \b IN: pointer to 1 byte return data.
///@return BOOLEAN:
///- TRUE: Success
///- FALSE: Fail
/******************************************************************************/
BOOLEAN MDrv_IIC_ReadByte ( U8 u8SlaveID, U8 u8RegAddr, U8 *pu8Data )
{
BOOLEAN Result;
Result=MDrv_IIC_ReadBytes(u8SlaveID,1, &u8RegAddr,1, pu8Data);
return Result;
}
/******************************************************************************/
///Write 1 byte through IIC
///@param u8SlaveID \b IN: Slave ID
///@param u8RegAddr \b IN: Target register offset to write
///@param u8Data \b IN: 1 byte data to write
///@return BOOLEAN:
///- TRUE: Success
///- FALSE: Fail
/******************************************************************************/
BOOLEAN MDrv_IIC_WriteByte ( U8 u8SlaveID, U8 u8RegAddr, U8 u8Data )
{
return( MDrv_IIC_WriteBytes(u8SlaveID,1, &u8RegAddr, 1, &u8Data) );
}
//------------------------------------------------------------------------
BOOLEAN MDrv_IIC_Write2Bytes(U8 u8SlaveID, U8 u8addr, U16 u16data)
{
U8 u8Data[2];
u8Data[0] = (u16data>>8) & 0xFF;
u8Data[1] = (u16data) & 0xFF;
return (MDrv_IIC_WriteBytes(u8SlaveID, 1, &u8addr, 2, u8Data));
}
U16 MDrv_IIC_Read2Bytes(U8 u8SlaveID, U8 u8addr)
{
U8 u8Data[2];
MDrv_IIC_ReadBytes(u8SlaveID, 1, &u8addr, 2, u8Data);
return ( (((U16)u8Data[0])<<8)|u8Data[1] );
}
BOOLEAN MDrv_IIC_WriteByteDirectly(U8 u8SlaveID, U8 u8Data)
{
IIC_Start();
if (IIC_SendByte(u8SlaveID & 0xFE)==FALSE)
return FALSE;
if (IIC_SendByte(u8Data)==FALSE)
return FALSE;
IIC_Stop();
return TRUE;
}
U8 MDrv_IIC_ReadByteDirectly(U8 u8SlaveID)
{
U8 pu8Data;
IIC_Start();
if(IIC_SendByte(u8SlaveID|0x1)==FALSE)
return FALSE;
//IIC_NoAck();
//if(IIC_GetByte(&pu8Data)==FALSE)
// return FALSE;
pu8Data=IIC_GetByte(0); //get non_ack
IIC_Stop();
return pu8Data;
}
BOOLEAN MDrv_IIC_Write4Bytes(U8 u8SlaveID, U32 u32Data, U8 u8EndData)
{
IIC_Start();
if(IIC_SendByte(u8SlaveID&0xFE)==FALSE)
return FALSE;
if(IIC_SendByte( (U8)(((U32)u32Data)>>24) ) == FALSE )
return FALSE;
if(IIC_SendByte( (U8)(((U32)u32Data)>>16) ) == FALSE )
return FALSE;
if(IIC_SendByte( (U8)(((U32)u32Data)>>8) ) == FALSE )
return FALSE;
if(IIC_SendByte( (U8)(((U32)u32Data)>>0) ) == FALSE )
return FALSE;
if(IIC_SendByte(u8EndData)==FALSE)
return FALSE;
IIC_Stop();
}
BOOLEAN MDrv_IIC_WriteGroupBytes(U8 u8SlaveID, U8 u8SubGroup, U16 u16Addr, U16 u16Data)
{
IIC_Start();
if(IIC_SendByte(u8SlaveID&0xFE)==FALSE)
return FALSE;
if(IIC_SendByte(u8SubGroup)==FALSE)
return FALSE;
if(IIC_SendByte((u16Addr>>8)&0xFF)==FALSE)
return FALSE;
if(IIC_SendByte(u16Addr&0xFF)==FALSE)
return FALSE;
if(IIC_SendByte((u16Data>>8)&0xFF)==FALSE)
return FALSE;
if(IIC_SendByte(u16Data&0xFF)==FALSE)
return FALSE;
IIC_Stop();
}
U16 MDrv_IIC_ReadGroupBytes(U8 u8SlaveID, U8 u8SubGroup, U16 u16Addr)
{
U16 u16Data;
U8 u8Address[3];
u8Address[0] = u8SubGroup;
u8Address[1] = (u16Addr>>8)&0xFF;
u8Address[2] = u16Addr&0xFF;
MDrv_IIC_ReadBytes(u8SlaveID, 3, u8Address, 2, (U8 *)&u16Data);
return u16Data;
}
//------------------------------------------------------------------------
//HW IIC test loop
//------------------------------------------------------------------------
#if 0
#define EE_TEST_SLAVE_ADDR 0xA4
#define EE_512_SLAVE_ADDR 0xA0
#define TUNER_SLAVE_ADR 0x1e
#define INFORM(x) x
void HwI2cAccessTest(void)
{
U8 TunerAddr=0x50;
U8 i; U8 x;
U8 ReadIn[8];
code U8 IicTestData[8]={0x12,0x34,0x56,0x78,0x99,0xbc,0xde,0xf0};
code U8 IicTestData2[8]={0x11,0x22,0x33,0x66,0x55,0x66,0x77,0x88};
U8 Addr[2];
//-----------------------------------
#if 0
x=MDrv_IIC_WriteByte( TUNER_SLAVE_ADR, 0x50,0x55 );
if ( x == FALSE ) IIC_Stop();
printf("start read.........\r\n");
i=0;
x=MDrv_IIC_ReadByte ( TUNER_SLAVE_ADR, 0x50,&i );
if ( x == FALSE ) IIC_Stop();
printf("i=%02bx\n",i);
i=10;
while( i < 50 )
{
i=10;
}
#endif
// MDrv_IIC_WriteBytes(TUNER_SLAVE_ADR,1,&TunerAddr,5,IicTestData);
// MDrv_IIC_ReadBytes(TUNER_SLAVE_ADR,1,&TunerAddr,5,ReadIn);
// for ( i=0;i<8;i++ )
// printf("%02bx, ",ReadIn[i]);
// printf("\r\n");
#if 0
//test 0
x=MDrv_IIC_WriteByte( EE_TEST_SLAVE_ADDR, 0x20,0x55 );
if ( x == FALSE ) IIC_Stop();
printf("start read.........\r\n");
i=0;
x=MDrv_IIC_ReadByte ( EE_TEST_SLAVE_ADDR, 0x20,&i );
if ( x == FALSE ) IIC_Stop();
if ( i == 0x55 )
INFORM(printf("Pass 1 byte read/write[%02bx]\r\n",i));
else
INFORM(printf("Fail 1 byte read/write[%02bx]\r\n",i));
//-----------------------------------
//test 1
MDrv_IIC_WriteByte( EE_TEST_SLAVE_ADDR, 0x20,0xAA );
INFORM(printf("start read 0xAA.........\r\n"));
i=0;
MDrv_IIC_ReadByte ( EE_TEST_SLAVE_ADDR, 0x20,&i );
if ( i == 0xAA )
INFORM(printf("Pass 1 byte read/write[%02bx]\r\n",i));
else
INFORM(printf("Fail 1 byte read/write[%02bx]\r\n",i));
//-----------------------------------
//test 2
MDrv_IIC_WriteBytes(EE_TEST_SLAVE_ADDR, 1, 0x10, 8, IicTestData);
INFORM(printf("start read data array.........\r\n"));
MDrv_IIC_ReadBytes( EE_TEST_SLAVE_ADDR, 1, 0x10, 8, ReadIn);
for ( i=0;i<8;i++ )
{
if ( IicTestData[i]!= ReadIn[i] )
{
INFORM(printf("Iic write series bytes error[%02bx]=%02bx\n",i,ReadIn[i]));
break;
}
}
if ( i == 8 )
INFORM(printf("Read write BYTES Pass\n",i));
#endif
#if 1
//-----------------------------------
//test 3
Addr[0]=0xc8;
Addr[1]=0x03;
MDrv_IIC_WriteBytes(EE_512_SLAVE_ADDR,2,Addr,8,IicTestData);
//delay loop;
INFORM(printf("start read data 24c32.........\r\n"));
MDrv_IIC_ReadBytes(EE_512_SLAVE_ADDR,2,Addr,8,ReadIn);
for ( i=0;i<8;i++ )
printf("%02bx, ",ReadIn[i]);
printf("\r\n\n");
Addr[0]=0xc9;
Addr[1]=0x03;
MDrv_IIC_WriteBytes(EE_512_SLAVE_ADDR,2,Addr,8,IicTestData2);
//delay loop;
INFORM(printf("start read data 24c32.........\r\n"));
MDrv_IIC_ReadBytes(EE_512_SLAVE_ADDR,2,Addr,8,ReadIn);
for ( i=0;i<8;i++ )
printf("%02bx, ",ReadIn[i]);
printf("\r\n\n");
#endif
//iiC test loop end
}//*/
#endif
/******************************************************************************/
///I2C Speed: set I2C Speed, u8Speed = 1 ~ 9, Speed up IIC by decrease u8Speed.
/******************************************************************************/
void MDrv_IIC_SetSpeed(U8 u8Speed)
{
_u8Delay = u8Speed;
/*
if(u8Speed >= 1 && u8Speed <= 9)
{
if(u8Speed == 1)
{
_u8Delay = 15;
}
else
{
_u8Delay = 25 * u8Speed;
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -