定时器中断驱动问题
micro2440定时器中断,用定时器1,对应端口没有输出
以下是程序段
//中断IST
DWORD TIMER_IST(LPVOID lpParameter)
{DWORD dwRes = 0;
BOOL bRes = 1;
while(1)
{dwRes = WaitForSingleObject(hIntpar,INFINITE);
if (dwRes != WAIT_OBJECT_0)
{
MessageBox(NULL,_T("Wait timer event failed."), NULL, MB_OK);
return 0;
}
else
{
//MessageBox(NULL,_T("Wait timer event ok."), NULL, MB_OK);
if (bRes)
{
GpioUp();
}
else
{
GpioDown();
}
bRes = !bRes;
}
InterruptDone(dwSysIntr);
}
return 0;
}
//中断
VOID TimerInterrupt(void)
{
HANDLE hIntpar; //事件
HANDLE hThreadIST;//线程
DWORD dwSysIntr;
DWORD dwIrq=IRQ_TIMER1;
BOOL bRes;
//BOOL bTimerThreadRun;
//获取逻辑中断号
bRes = KernelIoControl( IOCTL_HAL_REQUEST_SYSINTR,
&dwIrq,
sizeof( dwIrq ),
&dwSysIntr,
sizeof( dwSysIntr ),
NULL );
if (!bRes)
{
RETAILMSG(1,(TEXT("Get system interrupt failed. \n")));
// return FALSE;
}
//创建事件
hIntpar=CreateEvent(NULL,FALSE,FALSE,NULL);
//创建线程
DWORD dwThreadId;
hThreadIST=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TIMER_IST, NULL, CREATE_SUSPENDED, &dwThreadId);
CeSetThreadPriority(hThreadIST,5);
//注册中断事件
if(InterruptInitialize(dwSysIntr,hIntpar,NULL,NULL)){
RETAILMSG(1,(TEXT("Init interrupt ok! \n")));
}
else{ RETAILMSG(1,(TEXT("Init interrupt failed! \n")));
}
//bTimerThreadRun=TRUE;
//恢复线程等待
ResumeThread(hThreadIST);
}
/*******************************************************************************************
函数名称: TIM_Init
描 述: 驱动程序初始化函数
输入参数: DWORD dwContext: 设备管理器传递给本驱动的参数, 通常为流接口驱动在注册表内的位置
输出参数: 无
返 回: 驱动程序句柄
*******************************************************************************************/
PUBLIC DWORD TIM_Init(DWORD dwContext)
{TIM_InitializeAddresses();
TimerInterrupt();
#if 0
RETAILMSG(TRUE, (TEXT("v_pIOPregs->GPBCON is 0x%x).\r\n"), v_pIOPregs->GPBCON));
RETAILMSG(TRUE, (TEXT("v_PWMregs->TCFG0 is 0x%x).\r\n"), v_PWMregs->TCFG0));
RETAILMSG(TRUE, (TEXT("v_PWMregs->TCFG1 is 0x%x).\r\n"), v_PWMregs->TCFG1));
RETAILMSG(TRUE, (TEXT("v_PWMregs->TCMPB0 is 0x%x).\r\n"), v_PWMregs->TCMPB0));
RETAILMSG(TRUE, (TEXT("v_PWMregs->TCNTB0 is 0x%x).\r\n"), v_PWMregs->TCNTB0));
RETAILMSG(TRUE, (TEXT("v_PWMregs->TCON is 0x%x).\r\n"), v_PWMregs->TCON));
#endif
RETAILMSG(1, (TEXT("::: PWM_Init Sucessfully. \r\n")));
return (DWORD)1;
}
/*******************************************************************************************
函数名称: TIM_IOControl
描 述: 驱动程序 I/O 请求
输入参数:
输出参数:
返 回: TRUE: 成功 FALSE: 失败
*******************************************************************************************/
BOOL
TIM_IOControl(
DWORD Handle,
DWORD dwIoControlCode,
PBYTE pInBuf,
DWORD nInBufSize,
PBYTE pOutBuf,
DWORD nOutBufSize,
PDWORD pBytesReturned
)
{
BOOL bErr = FALSE;
switch (dwIoControlCode)
{
case IOCTL_TIM_SET_PRESCALER: /* 设置预分频值 */
if (nInBufSize >= 2) /* 需要2个字节缓冲区 */
{
TIM_SetPrescaler(pInBuf[0], pInBuf[1]);
bErr = TRUE;
}
break;
case IOCTL_TIM_SET_DIVIDER:
if (nInBufSize >= 2) /* 需要2个字节缓冲区 */
bErr = TIM_SetDivider(pInBuf[0], pInBuf[1]);
// 设置分频值 (定时器编号, 分频值)
break;
case IOCTL_TIM_START:
if (nInBufSize >= 3) /* 需要3个字缓冲区 */
{
DWORD t_num = *((DWORD *)pInBuf);
DWORD cycle = *((DWORD *)(pInBuf + 4));
DWORD duty = *((DWORD *)(pInBuf + 8));
#if 0
RETAILMSG(TRUE, (TEXT("t_num is 0x%x).\r\n"), t_num));
RETAILMSG(TRUE, (TEXT("cycle is 0x%x).\r\n"), cycle));
RETAILMSG(TRUE, (TEXT("duty is 0x%x).\r\n"), duty));
#endif
bErr = TIM_StartPWM(t_num, cycle, duty);
}
break;
case IOCTL_TIM_GET_FREQUENCY:
if ((nInBufSize >= 0) && (nOutBufSize > 0))
{
DWORD t_num = *((DWORD *)pInBuf);
bErr = TIM_GetFrequency(t_num, (DWORD *)pOutBuf);
*pBytesReturned = 1;
}
break;
default:
break;
}
return bErr;
} // TIM_IOControl
在应用程序中
void CTimeDlg::OnBnClickedStart()
{
// TODO: Add your control notification handler code here
BOOL ret;
BYTE prescale[2]={1,97};
BYTE divider[2]={1,2};
UpdateData(TRUE);
ret=::DeviceIoControl(hFile,IOCTL_TIM_SET_PRESCALER,prescale,2,NULL,0,NULL,NULL);
if(ret!=TRUE)
{MessageBox(_T("设置预分频失败"));
return;
}
ret=::DeviceIoControl(hFile,IOCTL_TIM_SET_DIVIDER,divider,2,NULL,0,NULL,NULL);
if(ret!=TRUE)
{MessageBox(_T("设置分频失败"));
return;
}
DWORD buff[3]={1,FREQ_VALUE,0};
ret=::DeviceIoControl(hFile,IOCTL_TIM_START,buff,3,NULL,0,NULL,NULL);
if(ret!=TRUE)
{MessageBox(_T("启动定时器失败"));
return;}
DWORD timer=1,curfreq,actlen;
ret=::DeviceIoControl(hFile,IOCTL_TIM_GET_FREQUENCY,&timer,1,&curfreq,1,&actlen,NULL);
if(ret!=TRUE)
{MessageBox(_T("获取频率失败"));
return;
}
m_srtTIMfreq.Format(_T("%d"),curfreq);
UpdateData(FALSE);
}
用定时器0,TOUT0有输出。。。但用定时器1,中断,设置的端口没有反应,求助
[解决办法]
你没有对定时器1做设置和start吧。 你的TIM_setXXX函数原型看不到。
不知你怎么做的。
TimerInterrupt 最好不要放到init中做,也放在iocontrol中做,因为你会改变timerIrq