当前位置: 代码迷 >> WinCE >> waitForSingleObject(gI2cEvent,I2C_RW_TIMEOUT)有关问题
  详细解决方案

waitForSingleObject(gI2cEvent,I2C_RW_TIMEOUT)有关问题

热度:98   发布时间:2016-04-28 13:20:42.0
waitForSingleObject(gI2cEvent,I2C_RW_TIMEOUT)问题
在IIC驱动中
IIC_Read和IIC_Write函数中
都有ret = WaitForSingleObject(gI2CEvent, I2C_RW_TIMEOUT);
其中I2C_RW_TIMEOUT定义为: #define I2C_RW_TIMEOUT 2000
具体函数:
DWORD IIC_Read(DWORD Handle, LPVOID pBuffer, DWORD dwNumBytes)
{
UINT32 count, ret;
uchar *pReadBuffer;

if ((pBuffer == NULL) || (dwNumBytes <= 0))
return 0;

pReadBuffer = MapPtrToProcess(pBuffer, GetCallerProcess());

// 设置从机地址及当前状态
I2cCurSla = I2cSla | 0x01;
I2cStatus = I2C_STATUS_SETADDR;
StartI2C(I2cCurSla);

ret = WaitForSingleObject(gI2CEvent, I2C_RW_TIMEOUT); /* 挂起当前线程,直到IIC中断的产生 */  
ResetEvent(gI2CEvent);
if ((IICError != I2C_ERROR_NO_ERR) || (ret != WAIT_OBJECT_0))
{
RETAILMSG(1, (TEXT("ERROR: IIC_Read: Send Slave Address fail! \r\n")));
return 0;
}

I2cStatus = I2C_STATUS_RECEIVE; // 进入接收状态
for (count = 0; count < dwNumBytes; count++)
{
if (count == (dwNumBytes - 1))
IIC_StartRecByteNA();
else
IIC_StartRecByteA();

/* 挂起当前线程,直到IIC中断的产生 */ 
ret = WaitForSingleObject(gI2CEvent, I2C_RW_TIMEOUT); ResetEvent(gI2CEvent);
if (ret != WAIT_OBJECT_0)
{
if (ret == WAIT_TIMEOUT)
RETAILMSG(1, (TEXT("ERROR: IIC read data time out! \r\n")));
else
RETAILMSG(1, (TEXT("ERROR: IIC read data fail! \r\n")));

return count;
}

*pReadBuffer = IIC_RecByte();
pReadBuffer++;

if (IICError != I2C_ERROR_NO_ERR)
{
RETAILMSG(1, (TEXT("ERROR: IIC_Read: Receive data fail! \r\n")));
break;
}
}

StopI2C(0);

return count;
}

------解决方案--------------------
等待gI2CEvent事件的发生,如果2秒后事件没有发生,就超时处理
如果gI2CEvent事件发生了,就马上wait返回,ret == WAIT_OBJECT_0