关于中断事件EVENT

liaohuchen   2008-7-3 10:09 楼主
在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;
}

回复评论 (8)

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()不误收到事件。。。。
点赞  2008-7-3 11:17
??????
您发的这个明确说明:
Auto-reset event objects automatically change from signaled to nonsignaled after a single waiting thread is released
点赞  2008-7-3 14:28
顶一下
点赞  2008-7-3 16:05
??
点赞  2008-7-3 23:24
你用的是哪个版本的WINCE?用法是不是有点问题。
我的中断处理过程是:

  1. DWORD idInt = 0; //用实际中断号替换0
  2. HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //创建事件对象
  3. InterruptInitialize(idInt, hEvent, NULL, 0);  //把中断号和事件对象关联。
  4. while(1)
  5. {
  6. WaitForSingleObject(hEvent, INFINITE);//等待中断。发生中断时会Disable对应的中断信号。

  7. //处理中断

  8. InterruptDone(idInt); //完成中断处理,会Enable中断,之后就可以继续接受到中断。
  9. }
点赞  2008-7-4 00:41
谁说的一定要加?
点赞  2008-7-4 08:54
fengwx说的对,只要
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //创建事件对象
InterruptInitialize(idInt, hEvent, NULL, 0);  //把中断号和事件对象关联。
while(1)
{
WaitForSingleObject(hEvent, INFINITE);//等待中断。发生中断时会Disable对应的中断信号。

//处理中断

InterruptDone(idInt); //完成中断处理,会Enable中断,之后就可以继续接受到中断。
}
就行,不用reset.
点赞  2008-7-4 10:09
thank u!
点赞  2008-7-4 15:48
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复