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

📄 acl_lib.cpp

📁 一个基于windriver开发的
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
// File - acl_lib.c
//
// Library for accessing the ACL card.
// Code was generated by DriverWizard v6.02 - http://www.jungo.com.
// The library accesses the hardware via WinDriver functions.
// 
// Copyright (c) 2003 Jungo Ltd.  http://www.jungo.com
// 
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "acl_lib.h"
#include "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
CHAR ACL_ErrorString[1024];

// internal data structures
typedef struct ACL_STRUCT
{
    HANDLE hWD;
    WD_CARD_REGISTER cardReg;
} ACL_STRUCT;

// internal function used by ACL_Open()
void ACL_SetCardElements(ACL_HANDLE hACL);

// Function: ACL_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:
//   phACL [out] returns the handle to the opened card.
// Return Value:
//   TRUE if the card was opened successfully. FALSE if there were any errors.
BOOL ACL_Open (ACL_HANDLE *phACL)
{
    ACL_HANDLE hACL = (ACL_HANDLE)malloc(sizeof(ACL_STRUCT));

    WD_VERSION ver;
    DWORD dwStatus;

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

    ACL_RegisterWinDriver();

    hACL->hWD = WD_Open();

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

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

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

    // ACL_Open() was successful
    *phACL = hACL;
    return TRUE;

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

// Function: ACL_Close()
//   Unregister an opened card.
// Parameters:
//   hACL [in] handle to the card as received from ACL_Open().
// Return Value:
//   None.
void ACL_Close(ACL_HANDLE hACL)
{
    // unregister card
    if (hACL->cardReg.hCard) 
        WD_CardUnregister(hACL->hWD, &hACL->cardReg);

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

    free (hACL);
}

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

    hWD = WD_Open();
    if (hWD!=INVALID_HANDLE_VALUE)
    {
         strcpy(lic.cLicense, "6C3CC2BFF76637EC556BF9D50D98F518FD244A93.0a7bfb0bb428");
         WD_License(hWD, &lic);
         WD_Close(hWD);
    }
}

void ACL_SetCardElements(ACL_HANDLE hACL)
{
    WD_ITEMS* pItem;
    hACL->cardReg.Card.dwItems = ACL_TOTAL_ITEMS;
    pItem = &hACL->cardReg.Card.Item[0];

    // 8111
    pItem[ACL_IORange1].item = ITEM_IO;
    pItem[ACL_IORange1].fNotSharable = TRUE;
    pItem[ACL_IORange1].I.IO.dwAddr = ACL_IORange1_ADDR;
    pItem[ACL_IORange1].I.IO.dwBytes = ACL_IORange1_BYTES;

}

// General read/write functions

// Function: ACL_ReadWriteBlock()
//   Read/Write data from/to the card's memory/IO into/from a given buffer.
// Parameters:
//   hACL [in] handle to the card as received from ACL_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 ACL_ReadWriteBlock(ACL_HANDLE hACL, ACL_ADDR addrSpace,
    DWORD dwOffset, BOOL fRead, PVOID buf, DWORD dwBytes, ACL_MODE mode)
{
    WD_TRANSFER trans;
    BOOL fMem = hACL->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY;
    BZERO(trans);
    if (fRead)
    {
        if (mode==ACL_MODE_BYTE) trans.cmdTrans = fMem ? RM_SBYTE : RP_SBYTE;
        else if (mode==ACL_MODE_WORD) trans.cmdTrans = fMem ? RM_SWORD : RP_SWORD;
        else if (mode==ACL_MODE_DWORD) trans.cmdTrans = fMem ? RM_SDWORD : RP_SDWORD;
    }
    else
    {
        if (mode==ACL_MODE_BYTE) trans.cmdTrans = fMem ? WM_SBYTE : WP_SBYTE;
        else if (mode==ACL_MODE_WORD) trans.cmdTrans = fMem ? WM_SWORD : WP_SWORD;
        else if (mode==ACL_MODE_DWORD) trans.cmdTrans = fMem ? WM_SDWORD : WP_SDWORD;
    }
    if (fMem)
        trans.dwPort = hACL->cardReg.Card.Item[addrSpace].I.Mem.dwTransAddr;
    else trans.dwPort = hACL->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 (hACL->hWD, &trans);
}

// Function: ACL_ReadByte()
//   Read a Byte from the card's memory/IO.
// Parameters:
//   hACL [in] handle to the card as received from ACL_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 ACL_ReadByte (ACL_HANDLE hACL, ACL_ADDR addrSpace, DWORD dwOffset)
{
    BYTE data;
    if (hACL->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        PBYTE pData = (PBYTE) (hACL->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        data = *pData; // read directly from the memory mapped range
    }
    else ACL_ReadWriteBlock(hACL, addrSpace, dwOffset, TRUE, &data, sizeof(BYTE), ACL_MODE_BYTE);
    return data;
}

// Function: ACL_ReadWord()
//   Read a Word from the card's memory/IO.
// Parameters:
//   hACL [in] handle to the card as received from ACL_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 ACL_ReadWord (ACL_HANDLE hACL, ACL_ADDR addrSpace, DWORD dwOffset)
{
    WORD data;
    if (hACL->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        PWORD pData = (PWORD) (hACL->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        data = *pData; // read directly from the memory mapped range
    }
    else ACL_ReadWriteBlock(hACL, addrSpace, dwOffset, TRUE, &data, sizeof(WORD), ACL_MODE_WORD);
    return data;
}

// Function: ACL_ReadDword()
//   Read a Dword from the card's memory/IO.
// Parameters:
//   hACL [in] handle to the card as received from ACL_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.
DWORD ACL_ReadDword (ACL_HANDLE hACL, ACL_ADDR addrSpace, DWORD dwOffset)
{
    DWORD data;
    if (hACL->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        PDWORD pData = (PDWORD) (hACL->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        data = *pData; // read directly from the memory mapped range
    }
    else ACL_ReadWriteBlock(hACL, addrSpace, dwOffset, TRUE, &data, sizeof(DWORD), ACL_MODE_DWORD);
    return data;
}

// Function: ACL_WriteByte()
//   Write a Byte to the card's memory/IO.
// Parameters:
//   hACL [in] handle to the card as received from ACL_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 ACL_WriteByte (ACL_HANDLE hACL, ACL_ADDR addrSpace, DWORD dwOffset, BYTE data)
{
    if (hACL->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        PBYTE pData = (PBYTE) (hACL->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        *pData = data; // write directly to the memory mapped range
    }
    else ACL_ReadWriteBlock(hACL, addrSpace, dwOffset, FALSE, &data, sizeof(BYTE), ACL_MODE_BYTE);
}

// Function: ACL_WriteWord()
//   Write a Word to the card's memory/IO.
// Parameters:
//   hACL [in] handle to the card as received from ACL_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 ACL_WriteWord (ACL_HANDLE hACL, ACL_ADDR addrSpace, DWORD dwOffset, WORD data)
{
    if (hACL->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        PWORD pData = (PWORD) (hACL->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        *pData = data; // write directly to the memory mapped range
    }
    else ACL_ReadWriteBlock(hACL, addrSpace, dwOffset, FALSE, &data, sizeof(WORD), ACL_MODE_WORD);
}

// Function: ACL_WriteDword()
//   Write a Dword to the card's memory/IO.
// Parameters:
//   hACL [in] handle to the card as received from ACL_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 ACL_WriteDword (ACL_HANDLE hACL, ACL_ADDR addrSpace, DWORD dwOffset, DWORD data)
{
    if (hACL->cardReg.Card.Item[addrSpace].item==ITEM_MEMORY)
    {
        PDWORD pData = (PDWORD) (hACL->cardReg.Card.Item[addrSpace].I.Mem.dwUserDirectAddr + dwOffset);
        *pData = data; // write directly to the memory mapped range
    }
    else ACL_ReadWriteBlock(hACL, addrSpace, dwOffset, FALSE, &data, sizeof(DWORD), ACL_MODE_DWORD);
}

⌨️ 快捷键说明

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