⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wait.cpp

📁 wince开发的xbase270开发板实现电机的控制。可控制步进电机和直流电机的运转时间
💻 CPP
字号:
// Wait.cpp: implementation of the CWait class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Motor.h"
#include "Wait.h"
#include "Motordef.h"
#include <pkfuncs.h> 

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define PAGE_SIZE 0x1000
#define ALIGNMENT_MASK (PAGE_SIZE-1)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

static volatile OST_REGS *ost;

CWait::CWait()
{

}

CWait::~CWait()
{

}


PVOID CWait::VirtualAllocCopy(unsigned size,char *str,PVOID pVirtualAddress)
{
    PVOID ptr;
    unsigned offset;

    offset = (unsigned)pVirtualAddress & ALIGNMENT_MASK;

    size +=offset ? PAGE_SIZE : 0;
    ptr = VirtualAlloc(0,size,MEM_RESERVE,PAGE_NOACCESS);
    if (ptr == NULL)
    {
        ERRORMSG(1,(TEXT("VirtualAlloc failed! %s : size=0x%x, (0x%x)\r\n"),str,size,GetLastError()));
        return(0);
    }

    if (!VirtualCopy((PVOID)ptr,(PVOID)((unsigned)pVirtualAddress - offset),size,PAGE_READWRITE|PAGE_NOCACHE))
    {
        ERRORMSG(1,(TEXT("VirtualCopy failed! %s : addr=0x%x, offset=0x%x(0x%x)\r\n"),str,(unsigned)pVirtualAddress,offset,GetLastError()));
        return(0);
    }
    return((PVOID)((PBYTE)ptr+offset));
}


PVOID CWait::VirtualAllocCopyPhysical(unsigned size, char *str, PVOID pPhysicalAddress)
{
    PVOID ptr;
    unsigned offset;

    offset = (unsigned)pPhysicalAddress & ALIGNMENT_MASK;

    size +=offset ? PAGE_SIZE : 0;
    ptr = VirtualAlloc(0, size, MEM_RESERVE, PAGE_NOACCESS);

    if (ptr == NULL)
    {
        ERRORMSG(1,(TEXT("VirtualAllocCopyPhysical failed! %s : size=0x%x, (0x%x)\r\n"),str,size,GetLastError()));
        return(0);
    }

    if (!VirtualCopy((PVOID)ptr, (PVOID)(((unsigned)pPhysicalAddress - offset) >> 8), size, PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))
    {
        ERRORMSG(1,(TEXT("VirtualCopyCopyPhysical failed! %s : addr=0x%x, offset=0x%x(0x%x)\r\n"),str,(unsigned)pPhysicalAddress,offset,GetLastError()));
        return(0);
    }
    return((PVOID)((PBYTE)ptr+offset));
}

void CWait::usWait(unsigned int usVal)
{
    unsigned Start;
    static unsigned firstCall=1;

    if (firstCall)
    {
        ost = (volatile OST_REGS*)VirtualAllocCopyPhysical(0x80,"Wait OST",(PVOID)(OST_BASE_PHYSICAL));
        if (!ost)
        {
            RETAILMSG(TRUE, (TEXT("usWait: Map OST register failed\r\n")));
            return;
        }
        firstCall=0;
    }

    if (usVal != 0)
    {
        Start = ost->oscr;
        while ((ost->oscr - Start) < usVal * TIMERTICK)
        {
            // do nothing
        }
    }
}

void CWait::msWait(unsigned msVal) 
{
    usWait(msVal*1000);
}

void CWait::sWait(unsigned sVal)
{
    msWait(sVal*1000);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -