用定时器1做的简单输出,友善micro2440的板子
请帮我分析下,搞了好长时间,还是出不来
流驱动:#include "options.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include "time.h" //定义IOCTL_TIM_START 1 IOCTL_TIM_STOP 2
#define PUBLIC
#define PRIVATE static
PRIVATE DWORD g_OpenCount = 0;
static DWORD INTIntr = SYSINTR_UNDEFINED;
static DWORD INTIrq = IRQ_TIMER1;
PRIVATE volatile S3C2440A_IOPORT_REG * v_pIOPregs;
volatile S3C2440A_PWM_REG * v_PWMregs;
volatile S3C2440A_INTR_REG * v_pINTRregs;
#define BUFSIZE 256
static HANDLE Timer_Event; //事件
static HANDLE Timer_Thread; //线程
int i1 = 0;
int dat = 1;
WCHAR achBuffer[BUFSIZE];
BOOL bTimerThreadRun;
BOOL TimerINTInit()
{
RETAILMSG(1,(TEXT("timer1::::**********KeyInt_Setting***********\r\n")));
//GPIO
v_pIOPregs->GPBCON = (v_pIOPregs->GPBCON &~(3 << 10)) | (1<< 10); // GPB5 == OUTPUT.控制LED
v_pIOPregs->GPBCON = (v_pIOPregs->GPBCON &~(3 << 12)) | (1<< 12); // GPB6 == OUTPUT.
v_pIOPregs->GPBCON = (v_pIOPregs->GPBCON &~(3 << 14)) | (1<< 14); // GPB7 == OUTPUT.
v_pIOPregs->GPBCON = (v_pIOPregs->GPBCON &~(3 << 16)) | (1<< 16); // GPB8 == OUTPUT.控制LED
v_pIOPregs->GPBCON = (v_pIOPregs->GPFCON &~(3 << 0)) | (1<< 0); // GPF0 == OUTPUT.输出F端口
v_pIOPregs->GPFCON = (v_pIOPregs->GPFCON &~(3 << 2)) | (1<< 2); // GPF1 == OUTPUT.
//INTR
v_pINTRregs->INTMSK &=~(0x00000001<<11); //0 打开timer1 中断
v_pINTRregs->INTMOD &=~(0x00000001<<11); //0 timer1 irq中断
//Timer1
v_PWMregs->TCNTB1 = 0x00004e20;
v_PWMregs->TCMPB1 = 0x00000000;
// v_pPWMregs->TCFG0
v_PWMregs->TCFG1 &= 0xff0fff0f; //timer1 2分频
v_PWMregs->TCON |=(0x00000001<<9); //手动更新
v_PWMregs->TCON |=(0x00000001<<11); //自动装载
v_PWMregs->TCON |=~(0x00000001<<8); //停止
v_PWMregs->TCON &=~(0x00000001<<9); //清手动更新
return TRUE;
}
void StartTimer()
{
v_PWMregs->TCON |= 0x01 << 9; //manual update on, load the counter
Sleep(1);
//set start bit and clear manual update bit
v_PWMregs->TCON |= 0x01 << 8;
v_PWMregs->TCON &= ~(0x01 << 9);
//启动线程
bTimerThreadRun = TRUE;
ResumeThread(Timer_Thread);
}
void StopTimer()
{
v_PWMregs->TCON &= ~(0x01 << 8);
bTimerThreadRun = FALSE;
}
static DWORD Timer1_Thread_Fun()
{
while(bTimerThreadRun)
{
WaitForSingleObject(Timer_Event,INFINITE);
RETAILMSG(1,(TEXT("timer1::::Thread id running :times == %d\r\n"),i1));
i1++;
if(dat==1)
{
v_pIOPregs->GPFDAT=v_pIOPregs->GPFDAT|(0x3<<0); //F端口11
v_pIOPregs->GPBDAT=v_pIOPregs->GPBDAT&~(0xF<<5); //LED
dat = 0;
}
else
{
v_pIOPregs->GPFDAT=v_pIOPregs->GPFDAT&~(0x3<<0); //00
v_pIOPregs->GPBDAT=v_pIOPregs->GPBDAT|(0xF<<5); //LED
dat =1;
}
InterruptDone(INTIntr);
}
return 0;
}
PRIVATE BOOL
TIM_InitializeAddresses(VOID)
{
BOOL RetValue = TRUE;
// RETAILMSG(1, (TEXT(">>> PBT_initalization address..set..\r\n")));
/* IO Register Allocation */
v_pIOPregs = (volatile S3C2440A_IOPORT_REG *)VirtualAlloc(0, sizeof(S3C2440A_IOPORT_REG), MEM_RESERVE,
PAGE_NOACCESS);
if (v_pIOPregs == NULL)
{
ERRORMSG(1,(TEXT("For IOPregs : VirtualAlloc failed!\r\n")));
RetValue = FALSE;
}
else
{
if (!VirtualCopy((PVOID)v_pIOPregs, (PVOID)(S3C2440A_BASE_REG_PA_IOPORT >> 8), sizeof
(S3C2440A_IOPORT_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(TEXT("For IOPregs: VirtualCopy failed!\r\n")));
RetValue = FALSE;
}
}
if (!RetValue)
{
if (v_pIOPregs)
{
VirtualFree((PVOID) v_pIOPregs, 0, MEM_RELEASE);
}
v_pIOPregs = NULL;
}
/* INTR Register Allocation */
v_pINTRregs = (volatile S3C2440A_INTR_REG *)VirtualAlloc(0, sizeof(S3C2440A_INTR_REG), MEM_RESERVE,
PAGE_NOACCESS);
if (v_pINTRregs == NULL)
{
ERRORMSG(1,(TEXT("For INTRregs : VirtualAlloc failed!\r\n")));
RetValue = FALSE;
}
else
{
if (!VirtualCopy((PVOID)v_pINTRregs, (PVOID)(S3C2440A_BASE_REG_PA_INTR >> 8), sizeof(S3C2440A_INTR_REG),
PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(TEXT("For INTRregs: VirtualCopy failed!\r\n")));
RetValue = FALSE;
}
}
if (!RetValue)
{
RETAILMSG (1, (TEXT("::: PBT_InitializeAddresses - Fail!!\r\n") ));
if (v_pINTRregs)
{
VirtualFree((PVOID) v_pINTRregs, 0, MEM_RELEASE);
}
v_pINTRregs = NULL;
}
v_PWMregs = (volatile S3C2440A_PWM_REG *)VirtualAlloc(0, sizeof(S3C2440A_PWM_REG), MEM_RESERVE, PAGE_NOACCESS);
if (v_PWMregs == NULL)
{
ERRORMSG(1,(TEXT("For IOPregs : VirtualAlloc failed!\r\n")));
RetValue = FALSE;
}
else
{
if (!VirtualCopy((PVOID)v_PWMregs, (PVOID)(S3C2440A_BASE_REG_PA_PWM >> 8), sizeof(S3C2440A_PWM_REG),
PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(TEXT("For IOPregs: VirtualCopy failed!\r\n")));
RetValue = FALSE;
}
}
if (!RetValue)
{
if (v_PWMregs)
{
VirtualFree((PVOID) v_PWMregs, 0, MEM_RELEASE);
}
v_PWMregs = NULL;
}
return(RetValue);
}
DWORD TIM_Init(DWORD dwContext)
{ // if you drive a idiographic device,add oter code ep: int,hardwareinit and so on
DWORD IDINTThread;
RETAILMSG(1,(TEXT("timer:::::*********InitKey_Init***********\n")));
TIM_InitializeAddresses();
RETAILMSG(1,(TEXT("timer1::::before CreateThread \r\n")));
Timer_Thread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)Timer1_Thread_Fun,0,0,&IDINTThread);
if(!Timer_Thread)
{
RETAILMSG(1,(TEXT("timer1::::CreateThread faile \r\n")));
return FALSE;
}
else
{
RETAILMSG(1,(TEXT("timer1::::CreateThread OK \r\n")));
}
Timer_Event = CreateEvent(NULL,false,false,NULL);
if(!Timer_Event)
{
RETAILMSG(1,(TEXT("timer1::::CreateEvent faile \r\n")));
return FALSE;
}
else
{
RETAILMSG(1,(TEXT("timer1::::CreateEvent OK \r\n")));
}
if(!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR,&INTIrq,sizeof(INTIrq),&INTIntr,sizeof(INTIntr),NULL))
{
RETAILMSG(1,(TEXT("timer1::::ERROR:kerneliocontrol.\r\n")));
return(FALSE);
}
else
{
RETAILMSG(1,(TEXT("timer1::::KernelioControl OK\r\n")));
}
if(!InterruptInitialize(INTIntr,Timer_Event,NULL,0))
{
RETAILMSG(1,(TEXT("timer1::::Interruptinitialize failed \r\n")));
return FALSE;
}
else
{
RETAILMSG(1,(TEXT("timer1::::Interruptinitialize OK \r\n")));
}
return TRUE;
}
PUBLIC BOOL WINAPI
DllEntry(HANDLE hInstDll, DWORD dwReason, LPVOID lpvReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
RETAILMSG(1,(TEXT("timer:::::*******DLL_PROCESS_ATTACH******\n")));
break;
case DLL_THREAD_ATTACH:
RETAILMSG(1,(TEXT("timer:::::******DLL_THREAD_ATTACH*******\n")));
break;
case DLL_THREAD_DETACH:
RETAILMSG(1,(TEXT("timer:::::******DLL_THREAD_DETACH******\n")));
break;
case DLL_PROCESS_DETACH:
RETAILMSG(1,(TEXT("timer:::::******DLL_PROCESS_DETACH******\n")));
break;
}
return true;
}
BOOL TIM_Close(DWORD Handle)
{
return TRUE;
} // TIM_Close
BOOL TIM_Deinit(DWORD dwContext)
{ CloseHandle(Timer_Thread);
if (Timer_Event)
CloseHandle(Timer_Event);
// 释放申请的虚拟空间
if (v_pIOPregs)
VirtualFree((PVOID) v_pIOPregs, 0, MEM_RELEASE);
if (v_PWMregs)
VirtualFree((PVOID) v_PWMregs, 0, MEM_RELEASE);
InterruptDisable(INTIrq);
KernelIoControl(IOCTL_HAL_RELEASE_SYSINTR, &INTIrq, sizeof(UINT32), NULL ,0 ,NULL);
return TRUE;
} // PWM_Deinit
DWORD TIM_Open(DWORD dwData, DWORD dwAccess, DWORD dwShareMode)
{
DWORD dwRet=0;
RETAILMSG(1,(TEXT("MYSTRINGS:TIM_Open\n")));
dwRet=1;
return dwRet;
} // TIM_Open
BOOL
TIM_IOControl(
DWORD Handle,
DWORD dwIoControlCode,
PBYTE pInBuf,
DWORD nInBufSize,
PBYTE pOutBuf,
DWORD nOutBufSize,
PDWORD pBytesReturned
)
{ TimerINTInit();
switch(dwIoControlCode){
case IOCTL_TIM_START:
StartTimer();
break;
case IOCTL_TIM_STOP:
StopTimer();
break;}
RETAILMSG(1,(TEXT("MYSTRINGS:TIM_IOControl\n")));
return TRUE;
} // TIM_IOControl
DWORD TIM_Read(DWORD Handle, LPVOID pBuffer, DWORD dwNumBytes)
{
return 0;
}
DWORD TIM_Write(DWORD Handle, LPCVOID pBuffer, DWORD dwNumBytes)
{
return 0;
}
DWORD TIM_Seek(DWORD Handle, long lDistance, DWORD dwMoveMethod)
{
return 0;
}
void TIM_PowerUp(void)
{
return;
}
void TIM_PowerDown(void)
{
return;
}
应用程序
#define IOCTL_TIM_START 1
#define IOCTL_TIM_STOP 2
BOOL CTimeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
。。。。
hFile=CreateFile(TEXT("TIM1:"),GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL );
if(hFile==INVALID_HANDLE_VALUE ){
MessageBox(TEXT("Cannot open TIM1!"),TEXT("Error"),MB_OK);
return TRUE;}
。。。。。
}
void CTimeDlg::OnBnClickedStart() //开始按钮
{
// TODO: Add your control notification handler code here
BOOL ret;
ret=::DeviceIoControl(hFile,IOCTL_TIM_START,NULL,0,NULL,0,NULL,NULL);
if(ret!=TRUE)
{MessageBox(_T("启动失败"));
return;
}
}
void CTimeDlg::OnBnClickedClose() //关闭按钮
{
// TODO: Add your control notification handler code here
BOOL ret;
ret=::DeviceIoControl(hFile,IOCTL_TIM_STOP,NULL,0,NULL,0,NULL,NULL);
if(ret!=TRUE)
{MessageBox(_T("设置分频失败"));
return;
}
}