我在调试一个I2C驱动,用IO口模拟的I2C总线,目前ADS调试裸机通过,但是加载到系统里面,总线时序就不对了,总收不到从设备发出的应答信号,即使我发送设备地址也不到应答。请问高手帮帮忙呀!我调试快两周了,没搞定呀!
下面是我的写字节函数,延时已经使用了精确延时,目前是20us(因为在ADS下调试通过),1us-60us延时都试过。
[code=C/C++][/code]
//----------------------------------------------------------------------------------
unsigned char I2c_WriteByte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
int error=0, i1=0, i2=0;
// unsigned char ub;
for(i2 = 0; i2 < 8; i2++ )
{ // setup data onto SDA
if(value & 0x80){
SDA_Set; //SetSDA( 1 );
}
else{
SDA_Clear; //SetSDA( 0 );
}
Delay_us(1); //I2C timing: data setup time > 200ns
// issue a clock
SCL_Set; //SetSCL( 1 );
Delay_us(1); //I2C timing: SCL high time > 0.6us
SCL_Clear; //SetSCL( 0 );
Delay_us(2); //I2C timing: SCL low time > 1.3us
value= value<< 1;
}
//check ACK(Low state) from I2C device
SDA_OutDisable; //set SDA as input
Delay_us(1); //I2C timing: data(from I2C device) setup time > 200ns
SCL_Set; //SetSCL( 1 ); // issue a clock
Delay_us(1); //I2C timing: SCL high time > 0.6us
if(s2440IOP->rGPFDAT>>1) //read ACK_BIT issued from I2C device
{
// NO ACK, so setup STOP condition
SCL_Set; //SetSCL( 1 );
Delay_us(1); //I2C timing: stop setup timing > 0.6us
SDA_Set; //SetSDA( 1 ); SDA is in input status!
return -(i1+1); // no acknowledgement -> failed
}
SCL_Clear; //SetSCL( 0 );
Delay_us(2); //I2C timing: SCL low time > 1.3us
//check ACK passed, config SDA as output for further outputing
SDA_OutEnable;
SDA_Set;
return 0;;
} |