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

📄 pm5002k_lib.c

📁 pm518采集卡驱动源码
💻 C
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////
// File - pm5002k_lib.c
//
// Library for accessing the PM5002K card.
// Code was generated by DriverWizard v6.20 - http://www.jungo.com.
// The library accesses the hardware via WinDriver functions.
// 
// Copyright (c) 2004 Jungo Ltd.  http://www.jungo.com
// 
//////////////////////////////////////////////////////////////////////

#include "pm5002k_lib.h"
#include "c:/windriver/include/windrvr_int_thread.h"
#include "c:/windriver/include/status_strings.h"

#ifndef __KERNEL__
	#include <stdio.h>
#endif

// If an error occurs, this string will be set to contain a relevant error message
//这里又要定义,往int5里加东西
CHAR PM5002K_ErrorString[1024];
LONG m_sdata[4096];
LONG m_inttime;
// internal data structures
typedef struct
{
    WD_INTERRUPT Int;
    HANDLE hThread;
    PM5002K_Interrupt5_HANDLER funcIntHandler;
} PM5002K_Interrupt5_INTERRUPT;

typedef struct
{
    WD_INTERRUPT Int;
    HANDLE hThread;
    PM5002K_Interrupt7_HANDLER funcIntHandler;
} PM5002K_Interrupt7_INTERRUPT;

typedef struct
{
    WD_INTERRUPT Int;
    HANDLE hThread;
    PM5002K_Interrupt10_HANDLER funcIntHandler;
} PM5002K_Interrupt10_INTERRUPT;

typedef struct
{
    WD_INTERRUPT Int;
    HANDLE hThread;
    PM5002K_Interrupt11_HANDLER funcIntHandler;
} PM5002K_Interrupt11_INTERRUPT;

typedef struct
{
    WD_INTERRUPT Int;
    HANDLE hThread;
    PM5002K_Interrupt12_HANDLER funcIntHandler;
} PM5002K_Interrupt12_INTERRUPT;

typedef struct PM5002K_STRUCT
{
    HANDLE hWD;
    PM5002K_Interrupt5_INTERRUPT Interrupt5;
    PM5002K_Interrupt7_INTERRUPT Interrupt7;
    PM5002K_Interrupt10_INTERRUPT Interrupt10;
    PM5002K_Interrupt11_INTERRUPT Interrupt11;
    PM5002K_Interrupt12_INTERRUPT Interrupt12;

//	PM5002K_DMA_HANDLE DMA;

    WD_CARD_REGISTER cardReg;
} PM5002K_STRUCT;

// internal function used by PM5002K_Open()
void PM5002K_SetCardElements(PM5002K_HANDLE hPM5002K);

// Function: PM5002K_Open()
//   Register an ISA card to enable working with it.
//   The handle returned from this function is used by most of the functions in this file.
// Parameters:
//   phPM5002K [out] returns the handle to the opened card.
// Return Value:
//   TRUE if the card was opened successfully. FALSE if there were any errors.
BOOL PM5002K_Open (PM5002K_HANDLE *phPM5002K)
{
    PM5002K_HANDLE hPM5002K = (PM5002K_HANDLE)malloc(sizeof(PM5002K_STRUCT));

    WD_VERSION ver;
    DWORD dwStatus;

    *phPM5002K = NULL;
    PM5002K_ErrorString[0] = '\0';
    if (!hPM5002K)
    {
        sprintf(PM5002K_ErrorString, "Failed allocating memory\n");
        return FALSE;
    }
    BZERO(*hPM5002K);

    dwStatus = PM5002K_RegisterWinDriver();
    if (dwStatus)
    {
        sprintf(PM5002K_ErrorString, "Failed registering WinDriver license\n");
        goto Exit;
    }

    hPM5002K->hWD = WD_Open();

    // Verify that the handle is valid and that the version number is correct
    if (hPM5002K->hWD==INVALID_HANDLE_VALUE)
    {
        sprintf(PM5002K_ErrorString, "Failed opening WinDriver device\n");
        goto Exit;
    }

    BZERO(ver);
    WD_Version(hPM5002K->hWD,&ver);
    if (ver.dwVer<WD_VER)
    {
        sprintf(PM5002K_ErrorString, "Incorrect WinDriver version. Expected %d.%02d, got %d.%02d\n",
             WD_VER/100, WD_VER, ver.dwVer/100, ver.dwVer);
        goto Exit;
    }

    PM5002K_SetCardElements(hPM5002K);
    hPM5002K->cardReg.fCheckLockOnly = FALSE;
    dwStatus = WD_CardRegister(hPM5002K->hWD, &hPM5002K->cardReg);
    if (dwStatus)
    {
        sprintf(PM5002K_ErrorString, "Failed locking device. Status 0x%lx - %s\n",
            dwStatus, Stat2Str(dwStatus));
        goto Exit;
    }

    BZERO (hPM5002K->Interrupt5);
    hPM5002K->Interrupt5.Int.hInterrupt = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt5].I.Int.hInterrupt;
    hPM5002K->Interrupt5.Int.dwOptions = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt5].I.Int.dwOptions;

    BZERO (hPM5002K->Interrupt7);
    hPM5002K->Interrupt7.Int.hInterrupt = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt7].I.Int.hInterrupt;
    hPM5002K->Interrupt7.Int.dwOptions = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt7].I.Int.dwOptions;

    BZERO (hPM5002K->Interrupt10);
    hPM5002K->Interrupt10.Int.hInterrupt = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt10].I.Int.hInterrupt;
    hPM5002K->Interrupt10.Int.dwOptions = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt10].I.Int.dwOptions;

    BZERO (hPM5002K->Interrupt11);
    hPM5002K->Interrupt11.Int.hInterrupt = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt11].I.Int.hInterrupt;
    hPM5002K->Interrupt11.Int.dwOptions = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt11].I.Int.dwOptions;

    BZERO (hPM5002K->Interrupt12);
    hPM5002K->Interrupt12.Int.hInterrupt = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt12].I.Int.hInterrupt;
    hPM5002K->Interrupt12.Int.dwOptions = hPM5002K->cardReg.Card.Item[PM5002K_Interrupt12].I.Int.dwOptions;

    // PM5002K_Open() was successful
    *phPM5002K = hPM5002K;
    return TRUE;

Exit:
    // An error occured during the execution of PM5002K_Open()
    if (hPM5002K->cardReg.hCard) 
        WD_CardUnregister(hPM5002K->hWD, &hPM5002K->cardReg);
    if (hPM5002K->hWD!=INVALID_HANDLE_VALUE)
        WD_Close(hPM5002K->hWD);
    free (hPM5002K);
    return FALSE;
}

// Function: PM5002K_Close()
//   Unregister an opened card.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
// Return Value:
//   None.
void PM5002K_Close(PM5002K_HANDLE hPM5002K)
{
    // disable interrupts
    if (PM5002K_Interrupt5IsEnabled(hPM5002K))
        PM5002K_Interrupt5Disable(hPM5002K);
    if (PM5002K_Interrupt7IsEnabled(hPM5002K))
        PM5002K_Interrupt7Disable(hPM5002K);
    if (PM5002K_Interrupt10IsEnabled(hPM5002K))
        PM5002K_Interrupt10Disable(hPM5002K);
    if (PM5002K_Interrupt11IsEnabled(hPM5002K))
        PM5002K_Interrupt11Disable(hPM5002K);
    if (PM5002K_Interrupt12IsEnabled(hPM5002K))
        PM5002K_Interrupt12Disable(hPM5002K);

    // unregister card
    if (hPM5002K->cardReg.hCard) 
        WD_CardUnregister(hPM5002K->hWD, &hPM5002K->cardReg);

    // close WinDriver
    WD_Close(hPM5002K->hWD);

    free (hPM5002K);
}

// Function: PM5002K_RegisterWinDriver()
//   Enter a license string into WinDriver module.
// Parameters:
//   None.
// Return Value:
//   WinDriver status code.
DWORD PM5002K_RegisterWinDriver(void)
{
    HANDLE hWD;
    WD_LICENSE lic;
    DWORD dwStatus;

    hWD = WD_Open();
    if (hWD==INVALID_HANDLE_VALUE)
        dwStatus = WD_INVALID_HANDLE;
    else
    {
        strcpy(lic.cLicense, "6C3CC2FF76DF316EECA8011E8C7C6D27D725D515.zhangming");
        dwStatus = WD_License(hWD, &lic);
        WD_Close(hWD);
    }
    return dwStatus;
}

void PM5002K_SetCardElements(PM5002K_HANDLE hPM5002K)
{
    WD_ITEMS* pItem;
    hPM5002K->cardReg.Card.dwItems = PM5002K_TOTAL_ITEMS;
    pItem = &hPM5002K->cardReg.Card.Item[0];

    pItem[PM5002K_IORange0].item = ITEM_IO;
    pItem[PM5002K_IORange0].fNotSharable = FALSE;
    pItem[PM5002K_IORange0].I.IO.dwAddr = PM5002K_IORange0_ADDR;
    pItem[PM5002K_IORange0].I.IO.dwBytes = PM5002K_IORange0_BYTES;

    pItem[PM5002K_Interrupt5].item = ITEM_INTERRUPT;
    pItem[PM5002K_Interrupt5].fNotSharable = FALSE;
    pItem[PM5002K_Interrupt5].I.Int.dwInterrupt = PM5002K_Interrupt5_IRQ;
    pItem[PM5002K_Interrupt5].I.Int.dwOptions = PM5002K_Interrupt5_OPTIONS;

    pItem[PM5002K_Interrupt7].item = ITEM_INTERRUPT;
    pItem[PM5002K_Interrupt7].fNotSharable = FALSE;
    pItem[PM5002K_Interrupt7].I.Int.dwInterrupt = PM5002K_Interrupt7_IRQ;
    pItem[PM5002K_Interrupt7].I.Int.dwOptions = PM5002K_Interrupt7_OPTIONS;

    pItem[PM5002K_Interrupt10].item = ITEM_INTERRUPT;
    pItem[PM5002K_Interrupt10].fNotSharable = FALSE;
    pItem[PM5002K_Interrupt10].I.Int.dwInterrupt = PM5002K_Interrupt10_IRQ;
    pItem[PM5002K_Interrupt10].I.Int.dwOptions = PM5002K_Interrupt10_OPTIONS;

    pItem[PM5002K_Interrupt11].item = ITEM_INTERRUPT;
    pItem[PM5002K_Interrupt11].fNotSharable = FALSE;
    pItem[PM5002K_Interrupt11].I.Int.dwInterrupt = PM5002K_Interrupt11_IRQ;
    pItem[PM5002K_Interrupt11].I.Int.dwOptions = PM5002K_Interrupt11_OPTIONS;

    pItem[PM5002K_Interrupt12].item = ITEM_INTERRUPT;
    pItem[PM5002K_Interrupt12].fNotSharable = FALSE;
    pItem[PM5002K_Interrupt12].I.Int.dwInterrupt = PM5002K_Interrupt12_IRQ;
    pItem[PM5002K_Interrupt12].I.Int.dwOptions = PM5002K_Interrupt12_OPTIONS;

}

// General read/write functions

// Function: PM5002K_ReadWriteBlock()
//   Read/Write data from/to the card's memory/IO into/from a given buffer.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
//   addrSpace [in] the address space of the card to access.
//   dwOffset [in] offset relative to the beginning of the address space to access.
//   fRead [in] direction of operation:
//     TRUE indicates read from the card's memory/IO into the given buffer.
//     FALSE indicates write from the given buffer to the card's memory/IO.
//   buf [in/out] a caller allocated buffer to read/write to/from.
//   dwBytes [in] the number of bytes to read/write. The allocated buffer should be at least dwBytes long.
//   mode [in] perform the data transfer byte by byte / word by word / dword by dword.
// Return Value:
//   None.
void PM5002K_ReadWriteBlock(PM5002K_HANDLE hPM5002K, PM5002K_ADDR addrSpace,
    DWORD dwOffset, BOOL fRead, PVOID buf, DWORD dwBytes, PM5002K_MODE mode)
{
    WD_TRANSFER trans;
    BOOL fMem = hPM5002K->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY;
    BZERO(trans);
    if (fRead)
    {
        if (mode==PM5002K_MODE_BYTE) trans.cmdTrans = fMem ? RM_SBYTE : RP_SBYTE;
        else if (mode==PM5002K_MODE_WORD) trans.cmdTrans = fMem ? RM_SWORD : RP_SWORD;
        else if (mode==PM5002K_MODE_DWORD) trans.cmdTrans = fMem ? RM_SDWORD : RP_SDWORD;
    }
    else
    {
        if (mode==PM5002K_MODE_BYTE) trans.cmdTrans = fMem ? WM_SBYTE : WP_SBYTE;
        else if (mode==PM5002K_MODE_WORD) trans.cmdTrans = fMem ? WM_SWORD : WP_SWORD;
        else if (mode==PM5002K_MODE_DWORD) trans.cmdTrans = fMem ? WM_SDWORD : WP_SDWORD;
    }
    if (fMem)
        trans.dwPort = hPM5002K->cardReg.Card.Item[addrSpace].I.Mem.dwTransAddr;
    else trans.dwPort = hPM5002K->cardReg.Card.Item[addrSpace].I.IO.dwAddr;
    trans.dwPort += dwOffset;

    trans.fAutoinc = TRUE;
    trans.dwBytes = dwBytes;
    trans.dwOptions = 0;
    trans.Data.pBuffer = buf;
    WD_Transfer (hPM5002K->hWD, &trans);
}

// Function: PM5002K_ReadByte()
//   Read a Byte from the card's memory/IO.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
//   addrSpace [in] the address space of the card to access.
//   dwOffset [in] offset relative to the beginning of the address space to access.
// Return Value:
//   The value read from the card's memory/IO.
BYTE PM5002K_ReadByte (PM5002K_HANDLE hPM5002K, PM5002K_ADDR addrSpace, DWORD dwOffset)
{
    BYTE data;
    if (hPM5002K->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        BYTE *pData = (BYTE *) (hPM5002K->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        data = *pData; // read directly from the memory mapped range
    }
    else PM5002K_ReadWriteBlock(hPM5002K, addrSpace, dwOffset, TRUE, &data, sizeof(BYTE), PM5002K_MODE_BYTE);
    return data;
}

// Function: PM5002K_ReadWord()
//   Read a Word from the card's memory/IO.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
//   addrSpace [in] the address space of the card to access.
//   dwOffset [in] offset relative to the beginning of the address space to access.
// Return Value:
//   The value read from the card's memory/IO.
WORD PM5002K_ReadWord (PM5002K_HANDLE hPM5002K, PM5002K_ADDR addrSpace, DWORD dwOffset)
{
    WORD data;
    if (hPM5002K->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        WORD *pData = (WORD *) (hPM5002K->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        data = *pData; // read directly from the memory mapped range
    }
    else PM5002K_ReadWriteBlock(hPM5002K, addrSpace, dwOffset, TRUE, &data, sizeof(WORD), PM5002K_MODE_WORD);
    return data;
}

// Function: PM5002K_ReadDword()
//   Read a Dword from the card's memory/IO.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
//   addrSpace [in] the address space of the card to access.
//   dwOffset [in] offset relative to the beginning of the address space to access.
// Return Value:
//   The value read from the card's memory/IO.
UINT32 PM5002K_ReadDword (PM5002K_HANDLE hPM5002K, PM5002K_ADDR addrSpace, DWORD dwOffset)
{
    UINT32 data;
    if (hPM5002K->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        UINT32 *pData = (UINT32 *) (hPM5002K->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        data = *pData; // read directly from the memory mapped range
    }
    else PM5002K_ReadWriteBlock(hPM5002K, addrSpace, dwOffset, TRUE, &data, sizeof(UINT32), PM5002K_MODE_DWORD);
    return data;
}

// Function: PM5002K_WriteByte()
//   Write a Byte to the card's memory/IO.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
//   addrSpace [in] the address space of the card to access.
//   dwOffset [in] offset relative to the beginning of the address space to access.
//   data [in] the data to write to the card's memory/IO.
// Return Value:
//   None.
void PM5002K_WriteByte (PM5002K_HANDLE hPM5002K, PM5002K_ADDR addrSpace, DWORD dwOffset, BYTE data)
{
    if (hPM5002K->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        BYTE *pData = (BYTE *) (hPM5002K->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        *pData = data; // write directly to the memory mapped range
    }
    else PM5002K_ReadWriteBlock(hPM5002K, addrSpace, dwOffset, FALSE, &data, sizeof(BYTE), PM5002K_MODE_BYTE);
}

// Function: PM5002K_WriteWord()
//   Write a Word to the card's memory/IO.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
//   addrSpace [in] the address space of the card to access.
//   dwOffset [in] offset relative to the beginning of the address space to access.
//   data [in] the data to write to the card's memory/IO.
// Return Value:
//   None.
void PM5002K_WriteWord (PM5002K_HANDLE hPM5002K, PM5002K_ADDR addrSpace, DWORD dwOffset, WORD data)
{
    if (hPM5002K->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        WORD *pData = (WORD *) (hPM5002K->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        *pData = data; // write directly to the memory mapped range
    }
    else PM5002K_ReadWriteBlock(hPM5002K, addrSpace, dwOffset, FALSE, &data, sizeof(WORD), PM5002K_MODE_WORD);
}

// Function: PM5002K_WriteDword()
//   Write a Dword to the card's memory/IO.
// Parameters:
//   hPM5002K [in] handle to the card as received from PM5002K_Open().
//   addrSpace [in] the address space of the card to access.
//   dwOffset [in] offset relative to the beginning of the address space to access.
//   data [in] the data to write to the card's memory/IO.
// Return Value:

⌨️ 快捷键说明

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