在IIC_INIT中建立事件gI2CEvent = CreateEvent(NULL, FALSE, FALSE, NULL);(为自动重置)
在中断线程I2C_IntrThread中SetEvent(gI2CEvent)后
进入IIC_READ函数的ret = WaitForSingleObject(gI2CEvent, I2C_RW_TIMEOUT);
为什么还需要ResetEvent(gI2CEvent)???
我看书中介绍操作系统会自动设置为无信号状态。
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;
}
The state of an event object remains nonsignaled until it is explicitly set to signaled by the SetEvent or PulseEvent function. This nonsignaled state blocks the execution of any threads that have specified the event object in a call to one of the wait functions.
ResetEvent sets the event to the nonsignaled state even if the event was signaled multiple times before being reset.
The ResetEvent function is used primarily for manual-reset event objects, which must be set explicitly to the nonsignaled state. Auto-reset event objects automatically change from signaled to nonsignaled after a single waiting thread is released
就是为了让WaitForSingleObject()不误收到事件。。。。
??????
您发的这个明确说明:
Auto-reset event objects automatically change from signaled to nonsignaled after a single waiting thread is released
你用的是哪个版本的WINCE?用法是不是有点问题。
我的中断处理过程是:
- DWORD idInt = 0; //用实际中断号替换0
- HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //创建事件对象
- InterruptInitialize(idInt, hEvent, NULL, 0); //把中断号和事件对象关联。
- while(1)
- {
- WaitForSingleObject(hEvent, INFINITE);//等待中断。发生中断时会Disable对应的中断信号。
- //处理中断
- InterruptDone(idInt); //完成中断处理,会Enable中断,之后就可以继续接受到中断。
- }
fengwx说的对,只要
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //创建事件对象
InterruptInitialize(idInt, hEvent, NULL, 0); //把中断号和事件对象关联。
while(1)
{
WaitForSingleObject(hEvent, INFINITE);//等待中断。发生中断时会Disable对应的中断信号。
//处理中断
InterruptDone(idInt); //完成中断处理,会Enable中断,之后就可以继续接受到中断。
}
就行,不用reset.