目前用IO模拟的方式读写EEPROM是正常的,说明硬件已经没问题了。但是硬件I2C读EEPROM有问题,写没问题。读的代码如下:
while (((I2Cx->STATUS_1.STATUS_1_Byte & 0x28) != I2C_STATUS_1_READY_MASK) && (timeout < I2C_READY_STATUS_TIMEOUT))
{
timeout ++;
}
if (timeout >= I2C_READY_STATUS_TIMEOUT)
{
errorStatus |= I2C_ERROR_START_NO_BUSY_FLAG;
}
errorStatus |= I2C_Start(I2Cx);
errorStatus |= I2C_WriteOneByte(I2Cx, slaveAddr);
if(addrbit == 16)
errorStatus |= I2C_WriteOneByte(I2Cx, (addr >> 8));
errorStatus |= I2C_WriteOneByte(I2Cx, (addr & 0xff));
errorStatus |= I2C_Start(I2Cx);
errorStatus |= I2C_WriteOneByte(I2Cx, slaveAddr | 0x01);
while(len > 1 && (errorStatus == I2C_ERROR_NULL))
{
errorStatus |= I2C_ReadOneByte(I2Cx, pdata, I2C_SEND_ACK);
pdata++;
len--;
}
errorStatus |= I2C_ReadOneByte(I2Cx, pdata, I2C_SEND_NACK);
errorStatus |= I2C_Stop(I2Cx);
if(errorStatus == I2C_ERROR_NULL)
return TRUE;
else
{
Printf("I2C Read Fail:%x, len:%d\n", errorStatus, len);
return FALSE;
}
errorStatus没有错误,返回都是I2C_ERROR_NULL,读回来的数据错了一个字节。
然后我在errorStatus |= I2C_WriteOneByte(I2Cx, slaveAddr | 0x01);后面加一句空读就正常了。
errorStatus |= I2C_ReadOneByte(I2Cx, pdata, I2C_SEND_ACK);//dummy read?
这个是必须的吗?没看到文档有说明这部分的内容。
|