//////////////////////////////////////////////////////////////////
//
// RTX_PCI1723Funcs.c - C file
//
// This file was generated using the RTX 8.1.0 Application Wizard.
//
//////////////////////////////////////////////////////////////////
#include "Rtx_PCI1723.h"
/************************************************************************/
/*PCI-1723驱动代码 */
/************************************************************************/
DWORD Rtx_PCI1723_Initial(const UINT CardIndex,unsigned short DeviceID,unsigned short VendorID)
{
ULONG uDeviceNum = 0; //设备号
ULONG uFunNum = 0; //功能号
ULONG uBusNum = 0; //总线号
ULONG uOffset = 0; //偏置,用于检索插槽是否为空,空则为0,相邻插槽关联常数为256。
ULONG uNothingToWrite = 0; //获取PCI配置空间失败.
ULONG uBytesWritten = 0; //获得PCI配置空间信息。一般为64字节。
BOOL bFlag = 0;
// UINT uIndex = 0; //当系统中存在多块相同PCI卡时检索之用。
DWORD _base_addr = 0; //返回的基地址
//================================================================================================
PCI_SLOT_NUMBER SlotNumber;
PPCI_COMMON_CONFIG PciData;
UCHAR chBuffer[PCI_COMMON_HDR_LENGTH];
PciData = (PPCI_COMMON_CONFIG)chBuffer;
SlotNumber.u.bits.Reserved = 0;
bFlag = TRUE;
for(uBusNum = 0; bFlag ;uBusNum++)
{
for(uDeviceNum = 0;uDeviceNum < PCI_MAX_DEVICES && bFlag ;uDeviceNum++)
{
SlotNumber.u.bits.DeviceNumber = uDeviceNum;
for(uFunNum = 0; uFunNum < PCI_MAX_FUNCTION; uFunNum++)
{
SlotNumber.u.bits.FunctionNumber = uFunNum;
uBytesWritten = RtGetBusDataByOffset(PCIConfiguration,uBusNum,SlotNumber.u.AsULONG,PciData,uOffset,PCI_COMMON_HDR_LENGTH);
if(uBytesWritten == uNothingToWrite)
{
RtWprintf(L"NothingWritten!");
RtWprintf(L"Error ByteWritten = %d\n",uBytesWritten);
uDeviceNum = PCI_MAX_DEVICES;
bFlag = FALSE;
break;
}
if(PciData->VendorID == PCI_INVALID_VENDORID) //如果VendorID等于0xFFFF,表示PCI卡错误,进行下一个循环
{
break;
}
if((PciData->DeviceID == DeviceID) && (PciData->VendorID == VendorID) && uDeviceNum == CardIndex)
{
//设置命令参数用于访问PCI设备的控制寄存器
PciData->Command = (PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE | PCI_ENABLE_BUS_MASTER | PCI_ENABLE_WRITE_AND_INVALIDATE);
RtSetBusDataByOffset(PCIConfiguration,uBusNum,SlotNumber.u.AsULONG,PciData,0,PCI_COMMON_HDR_LENGTH);
_base_addr = PciData->u.type0.BaseAddresses[2] & 0xfffffffe;
return _base_addr;
}
}
}
}
return 0;
}
//
void Rtx_PCI1723_DAOutPut(DWORD dwAddr,int nChannel,double dValue)
{
if(dwAddr == 0)
{
return;
}
if(dValue < -9.9999999)
{
dValue = -9.9999999;
}
else if(dValue > 9.9999999)
{
dValue = 9.9999999;
}
RtWritePortUshort((PUSHORT)(dwAddr + 2*nChannel),(USHORT)(0x8000 + dValue * 32767/10.0));
}
USHORT Rtx_PCI1723_DAReadLatch(DWORD dwAddr,int nChannel)
{
if(dwAddr == 0)
{
return -1;
}
return (RtReadPortUshort((PUSHORT)(dwAddr + 2*nChannel)));
}
int Rtx_PCI1723_GetBoardID(DWORD dwAddr)
{
int nID = 0;
if(dwAddr == 0)
{
return -1;
}
nID = RtReadPortUshort((PUSHORT)(dwAddr + 0x10)) & 0x000f;
return (nID);
}
USHORT Rtx_PCI1723_GetCurSynchStatus(DWORD dwAddr)
{
if(dwAddr == 0)
{
return -1;
}
return (RtReadPortUshort((PUSHORT)(dwAddr + 0x12)) & 0x0001);
}
void Rtx_PCI1723_DASetSynchronize(DWORD dwAddr,BOOL bSynchro)
{
USHORT uCurStatus = 0;
if(dwAddr == 0)
{
return;
}
uCurStatus = Rtx_PCI1723_GetCurSynchStatus(dwAddr);
if(bSynchro)
{
RtWritePortUshort((PUSHORT)(dwAddr + 0x12),(USHORT)(uCurStatus | 0x0001));
}
else
{
RtWritePortUshort((PUSHORT)(dwAddr + 0x12),(USHORT)(uCurStatus & 0xfffe));
}
}
void Rtx_PCI1723_DIOSet(DWORD dwAddr,USHORT uStyle)
{
if(dwAddr == 0)
{
return;
}
RtWritePortUshort((PUSHORT)(dwAddr + 0x1A),uStyle);
}
USHORT Rtx_PCI1723_DIReadPort(DWORD dwAddr)
{
USHORT uValue = 0;
if(dwAddr == 0)
{
return -1;
}
uValue = RtReadPortUshort((PUSHORT)(dwAddr + 0x1c));
return (uValue);
}
void Rtx_PCI1723_DOWritePort(DWORD dwAddr,USHORT nValue)
{
if(dwAddr == 0)
{
return;
}
RtWritePortUshort((PUSHORT)(dwAddr + 0x1c),(USHORT)nValue);
}
BOOL Rtx_PCI1723_ReleaseCard(DWORD dwAddr)
{
if(dwAddr != 0)
{
dwAddr = 0;
return TRUE;
}
else
{
return FALSE;
}
}