• wince编程随笔关于看门狗


    本程序基于TQ2440原有的看门狗驱动修改(vs2005+wince6.0测试通过):

    “watchdog.h”

    #ifndef __WDG_H_
    #define __WDG_H_
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    #define IO_CTL_WDG_OPEN 0x01
    #define IO_CTL_WDG_FEED 0x02
    
    DWORD WDG_Init(DWORD dwContext);
    DWORD WDG_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode);
    DWORD WDG_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes);
    
    DWORD WDG_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count);
    DWORD WDG_Seek(DWORD hOpenContext, long Amount, DWORD Type);
    
    BOOL WDG_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut);
    BOOL WDG_Close(DWORD hOpenContext);
    BOOL WDG_Deinit(DWORD hDeviceContext);
    void WDG_PowerUp(DWORD hDeviceContext);
    void WDG_PowerDown(DWORD hDeviceContext);
    
    #ifdef __cplusplus
    }
    #endif
    
    
    #endif

    “watchdog.cpp”

    #include <windows.h>
    #include <nkintr.h>
    #include <pm.h>
    #include "pmplatform.h"
    #include "Pkfuncs.h"
    #include "BSP.h"
    
    #include "watchdog.h"
    
    
    volatile S3C2440A_WATCHDOG_REG *v_pWDGregs ;                    // Virtual allocation
    bool InitializeAddresses(VOID)
    {
        bool    RetValue = TRUE;
    
        /* WatchDog Register Allocation */
        v_pWDGregs = (volatile S3C2440A_WATCHDOG_REG *)VirtualAlloc(0, sizeof(S3C2440A_WATCHDOG_REG), MEM_RESERVE, PAGE_NOACCESS);
        if (v_pWDGregs == NULL) 
        {
            ERRORMSG(1,(TEXT("For IOPregs : VirtualAlloc faiGPIO!\r\n")));
            RetValue = FALSE;
        }
        else 
        {
            if (!VirtualCopy((PVOID)v_pWDGregs, (PVOID)(S3C2440A_BASE_REG_PA_WATCHDOG >> 8), sizeof(S3C2440A_WATCHDOG_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE)) 
            {
                ERRORMSG(1,(TEXT("For IOPregs: VirtualCopy faiGPIO!\r\n")));
                RetValue = FALSE;
            }
        }
    
        if (!RetValue) 
        {
            if (v_pWDGregs) 
            {
                VirtualFree((PVOID) v_pWDGregs, 0, MEM_RELEASE);
            }
    
            v_pWDGregs = NULL;
        }
    
        return(RetValue);
    
    }
    
    static BOOL FeedIt()
    {
        //v_pWDGregs->WTCON = 0x8021; // Init Value
        //v_pWDGregs->WTDAT = 0xFEA9;
        //v_pWDGregs->WTCNT = 0xFEA9;
        //v_pWDGregs->WTCON = ( 1 << 0) | (0x03 << 3 ) | (1 << 5) | (0x5A << 8);
    
    
        return TRUE;
    }
    
    BOOL WINAPI  
    DllEntry(HANDLE    hinstDLL, DWORD dwReason, LPVOID  Reserved/* lpvReserved */)
    {
        switch(dwReason)
        {
        case DLL_PROCESS_ATTACH:
            DEBUGREGISTER((HINSTANCE)hinstDLL);
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            break;
        case DLL_PROCESS_DETACH:
            break;
    #ifdef UNDER_CE
        case DLL_PROCESS_EXITING:
            break;
        case DLL_SYSTEM_STARTED:
            break;
    #endif
        }
    
        return TRUE;
    }
    
    
    BOOL WDG_Deinit(DWORD hDeviceContext)
    {
        return TRUE;
    }
    
    
    BOOL WDG_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut)
    {
        switch(dwCode)
        {
        case IO_CTL_WDG_OPEN:
            v_pWDGregs->WTCON = 0x8021; // Init Value
            v_pWDGregs->WTDAT = 0xFEA9;
            v_pWDGregs->WTCON = ( 1 << 0) | (0x03 << 3 ) | (1 << 5) | (0x5A << 8);
            RETAILMSG(1,(TEXT("WDG: Open\r\n")));
            break;
        case IO_CTL_WDG_FEED:
            v_pWDGregs->WTCNT = 0xFEA9;
            RETAILMSG(1,(TEXT("WDG: Feed\r\n")));
            break;
        default:
            break;
        }
        return TRUE;
    }
    
    void WDG_PowerDown(DWORD hDeviceContext)
    {
    }
    
    void WDG_PowerUp(DWORD hDeviceContext)
    {
    }
    
    DWORD WDG_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
    {
        return 0;
    }
    
    DWORD WDG_Seek(DWORD hOpenContext, long Amount, DWORD Type)
    {
        return 0;
    }
    
    DWORD WDG_Init(DWORD dwContext)
    {
        if (!InitializeAddresses())
            return (FALSE);
        RETAILMSG(1,(TEXT("WDG: Init\r\n")));
        return TRUE;
    }
    
    BOOL WDG_Close(DWORD hOpenContext)
    {
        RETAILMSG(1,(TEXT("WDG: Close\r\n")));
        return TRUE;
    }
    
    // Beaf is here
    
    DWORD WDG_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
    {
        
        //FeedIt();
        return TRUE;
    }
    
    DWORD WDG_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
    {
        RETAILMSG(1,(TEXT("WDG: Write\r\n")));
        //FeedIt();
        return TRUE;
    }

    使用关键字调用即可,例如:

     先头文件里声明控制字:

    #define IO_CTL_WDG_OPEN 0x01
    #define IO_CTL_WDG_FEED 0x02

    源文件里调用:

    创建

    HANDLE     wdgdriver= CreateFile(L"WDG1:",GENERIC_READ | GENERIC_WRITE,
            0,NULL,OPEN_EXISTING,0,NULL);

    打开看门狗

    DeviceIoControl(wdgdriver,IO_CTL_WDG_OPEN,NULL,0,NULL,0,NULL,NULL);

    喂狗

    DeviceIoControl(wdgdriver,IO_CTL_WDG_FEED,NULL,0,NULL,0,NULL,NULL);

    至于流驱动完成制作流程我不做赘述了

    值得一提是的,我用的TQ2440开发板,想要看到调试信息,修改“bsp_cfgt.h”

    如下:

    //*********************        //use for eboot
    #define eboot_nouse_UART    0
    #define eboot_use_UART0        1
    #define eboot_use_UART1        2
    #define eboot_UARTn            eboot_use_UART0
    
    //*********************        //use for NK.bin
    #define BSP_nouse_UART        (0)
    #define BSP_use_UART0        (1)
    #define BSP_use_UART1        (2)
    #define BSP_UARTn            BSP_use_UART0
    

    BSP_UARTn 原来的值是  (0)  改为 (1)  即可。

  • 相关阅读:
    UML与ER图
    动态规划法求背包问题
    回溯法求解n皇后和迷宫问题
    优先队列和二叉堆
    快排,归并和Shell排序
    关系型数据库设计范式
    Python使用Redis数据库
    Python urllib简单使用
    当SQL UPDATE遇到EXISTS(SELECT ...)时
    关于使用mybatis的一个惨痛教训
  • 原文地址:https://www.cnblogs.com/swack/p/by_swack.html
Copyright © 2020-2023  润新知