请问各位高手:
这段在IIC驱动中的中断线程的作用是什么啊?
能否详细解释一下里面的重要代码??
谢谢各位啦!
DWORD I2C_IntrThread(PVOID pArg)
{
DWORD ret;
// ???¨I2C????????????
gI2CIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// ?????? I2C ????: ×??á????????, ???í I2C ????
if (!(InterruptInitialize(g_I2CSysIntr, gI2CIntrEvent, 0, 0)))
{
CloseHandle(gI2CIntrEvent);
RETAILMSG(1, (TEXT("ERROR: I2C Bus: Interrupt initialize failed.\r\n")));
return 0;
}
// IIC ????????????????
while (1)
{
ret = WaitForSingleObject(gI2CIntrEvent, INFINITE);
if ((ret != WAIT_OBJECT_0) || (g_bKillIST == TRUE)) /* ?????????ò?í?ó·??ú */
{
CloseHandle(gI2CIntrEvent);
RETAILMSG(1, (TEXT("INFO: I2C Bus Driver DeInit or Error Occur. \r\n")));
return 0; /* ???????? */
}
switch(I2cStatus)
{
// Start IIC Status
case I2C_STATUS_SETADDR:
if((v_pIICPregs->IICSTAT & 0x09) == 0) // ·??????·????
{
SetEvent(gI2CEvent);
IICError = I2C_ERROR_NO_ERR;
}
else
{
if (I2cCurSla & I2C_READ)
v_pIICPregs->IICSTAT = (2 << 6) | (0<<5) | (1<<4); // ??????
else
v_pIICPregs->IICSTAT = (3 << 6) | (0<<5) | (1<<4); // ??????
IICCON_DACK(v_pIICPregs->IICCON);
Sleep(1); // ?????á???????ú?ú?ê±?
SetEvent(gI2CEvent);
IICError = I2C_ERROR_SETADDR;
}
break;
// Send Bytes Status
case I2C_STATUS_SEND:
if((v_pIICPregs->IICSTAT & 0x09) == 0) // ????????·???
{
IICError = I2C_ERROR_NO_ERR;
SetEvent(gI2CEvent);
}
else
{
v_pIICPregs->IICSTAT = (3 << 6) | (0 << 5) | (1 << 4);
IICCON_DACK(v_pIICPregs->IICCON);
Sleep(1); // ?????á???????ú?ú?ê±?
IICError = I2C_ERROR_SEND;
SetEvent(gI2CEvent);
}
break;
// Receive Bytes
case I2C_STATUS_RECEIVE:
if((v_pIICPregs->IICSTAT & 0x08) == 0)
{
IICError = I2C_ERROR_NO_ERR;
SetEvent(gI2CEvent);
}
else
{
// ·????á??????
v_pIICPregs->IICSTAT = (2 << 6) | (0 << 5) | (1 << 4);
IICCON_DACK(v_pIICPregs->IICCON);
Sleep(1); // ?????á???????ú?ú?ê±?
IICError = I2C_ERROR_RECEIVE;
SetEvent(gI2CEvent);
}
break;
default:
break;
}
InterruptDone(g_I2CSysIntr);
}
}
这个是带注解的:
DWORD I2C_IntrThread(PVOID pArg)
{
DWORD ret;
// 创建I2C中断中断事件
gI2CIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// 初始化 I2C 中断: 注册中断事件, 允许 I2C 中断
if (!(InterruptInitialize(g_I2CSysIntr, gI2CIntrEvent, 0, 0)))
{
CloseHandle(gI2CIntrEvent);
RETAILMSG(1, (TEXT("ERROR: I2C Bus: Interrupt initialize failed.\r\n")));
return 0;
}
// IIC 中断线程开始运行
while (1)
{
ret = WaitForSingleObject(gI2CIntrEvent, INFINITE);
if ((ret != WAIT_OBJECT_0) || (g_bKillIST == TRUE)) /* 驱动卸载或错误发生 */
{
CloseHandle(gI2CIntrEvent);
RETAILMSG(1, (TEXT("INFO: I2C Bus Driver DeInit or Error Occur. \r\n")));
return 0; /* 线程退出 */
}
switch(I2cStatus)
{
// Start IIC Status
case I2C_STATUS_SETADDR:
if((v_pIICPregs->IICSTAT & 0x09) == 0) // 发送地址成功
{
SetEvent(gI2CEvent);
IICError = I2C_ERROR_NO_ERR;
}
else
{
if (I2cCurSla & I2C_READ)
v_pIICPregs->IICSTAT = (2 << 6) | (0<<5) | (1<<4); // 读终止
else
v_pIICPregs->IICSTAT = (3 << 6) | (0<<5) | (1<<4); // 写终止
IICCON_DACK(v_pIICPregs->IICCON);
Sleep(1); // 等待结束信号产生完毕
SetEvent(gI2CEvent);
IICError = I2C_ERROR_SETADDR;
}
break;
// Send Bytes Status
case I2C_STATUS_SEND:
if((v_pIICPregs->IICSTAT & 0x09) == 0) // 数据成功发送
{
IICError = I2C_ERROR_NO_ERR;
SetEvent(gI2CEvent);
}
else
{
v_pIICPregs->IICSTAT = (3 << 6) | (0 << 5) | (1 << 4);
IICCON_DACK(v_pIICPregs->IICCON);
Sleep(1); // 等待结束信号产生完毕
IICError = I2C_ERROR_SEND;
SetEvent(gI2CEvent);
}
break;
// Receive Bytes
case I2C_STATUS_RECEIVE:
if((v_pIICPregs->IICSTAT & 0x08) == 0)
{
IICError = I2C_ERROR_NO_ERR;
SetEvent(gI2CEvent);
}
else
{
// 发送结束信号
v_pIICPregs->IICSTAT = (2 << 6) | (0 << 5) | (1 << 4);
IICCON_DACK(v_pIICPregs->IICCON);
Sleep(1); // 等待结束信号产生完毕
IICError = I2C_ERROR_RECEIVE;
SetEvent(gI2CEvent);
}
break;
default:
break;
}
InterruptDone(g_I2CSysIntr);
}
}
主要是CASE语句这块
请了解IIC驱动的高手结合IIC总线协议帮我分析一下
谢谢大家啦!
这个地方就要你对照datasheet中关于i2c寄存器的 说明部分了, 并且上面得主是很详细, 了解协议先
这里中断线程在整个程序中的主要作用是什么呢?
它是怎么影响整个程序的啊?不解啊
谢谢啦