RTX下IK220計數卡的PCI示例程式碼,自己分析用
//////////////////////////////////////////////////////////////////
//
// IK220_RS485_APP.c - C file
//
//
//////////////////////////////////////////////////////////////////
#include "IK220_RS485_APP.h"
#include <math.h>
/************************************************************************/
//主程式
/************************************************************************/
u16_t Status[10] = {0};
const double Ts = 0.0002;
const LONGLONG SENDCYCLE = 2000; //設定中斷間隔為2000*100納秒
const double pi =3.1415926535;
//const double mutfre = 0.038;
LARGE_INTEGER liPeriod;
u8_t i;
u8_t modeFlag = 0;
u8_t sig = 0;
long tsc = 0;
FILE* fp=NULL;
FILE* fp1=NULL;
FILE* fp2=NULL;
HANDLE hTimer = NULL;
double STARTPOINT = 0.0;
//double STARTPOINT = 0;
double Scurvey[100000] = {0};
double Scurvev[100000] = {0};
double Scurvea[100000] = {0};
double Disturbr[50] = {0};
double fpSave[100000] = {0};
double fvSave[100000] = {0};
double conpSave[100000] = {0};
double convSave[100000] = {0};
double ioutSave[100000] = {0};
double rub[10000] = {0};
double rub1[10000] = {0};
void _cdecl wmain( int argc, wchar_t **argv, wchar_t **envp )//主函式
{
u32_t temp;
LARGE_INTEGER x;
if ( RtSetThreadPriority( GetCurrentThread(), RT_PRIORITY_MAX) == FALSE )
//設定當前執行緒優先順序
{
RtWprintf(L"設定優先順序錯誤:%d\n", GetLastError() );
ExitProcess( 1 );
}
SearchIK220Card();//查詢IK220卡;下邊檔案IK220FUNCS.c中呼叫
Sleep(3);
SearchCP132Card();//查詢ICP132卡;
Sleep(3);
/*設定定時器時間間隔 百納秒*/
liPeriod.QuadPart = SENDCYCLE;// 10000;
//建立定時器核心執行緒
if (!(hTimer = RtCreateTimer(NULL,0,TimerHandler,NULL,RT_PRIORITY_MAX,
CLOCK_FASTEST)))
{
RtWprintf(L"建立定時器錯誤:%d\n", GetLastError() );
ExitProcess( 1 );
}
IK220Get32(&temp);
while(!IK220Get32(&temp))
{
;
}
//週期中斷
if (!RtSetTimerRelative( hTimer,&liPeriod,&liPeriod ) )
{
RtWprintf(L"掛接定時器失敗:%d\n", GetLastError() );
ExitProcess( 1 );
}
RtWprintf(L"定時器初始化完成!\n");
while(!sig)
{
Sleep(200);
}
RtCancelTimer( hTimer, &x);
RtDeleteTimer( hTimer);
ExitProcess(0);
}
void RTFCNDCL TimerHandler(void *nContext)
{
switch(modeFlag)
{
case 0: findZero(); break;
case 1: stickZero(); break;
case 2: freeOut(); break;
case 3: startScurve(); break;
default: zeroOut(); sig = 1;SuspendThread( GetCurrentThread ( ) ); break;
}
return;
}
//以下在IK220FUNCS.c 中。cp132的類似,在CP132Funcs中
void SearchIK220Card(void)
{
//定義IK220卡PCI配置引數儲存變數
UCHAR IK220_PCI_CR_Buffer[PCI_COMMON_HDR_LENGTH];
PPCI_COMMON_CONFIG IK220PciData = ( PPCI_COMMON_CONFIG ) IK220_PCI_CR_Buffer;
static PCI_SLOT_NUMBER IK220SlotNumber;
u32_t IK220BusNumber;
//查詢IK220計數卡
IK220BusNumber = DeviceSearch( IK220_VENDOR_ID, IK220_DEVICE_ID,
IK220_Subvendor_ID,IK220_Subdevice_ID, &IK220SlotNumber, IK220PciData );
RtWprintf ( L"已搜尋到IK220計數卡!\n");
//初始化IK220計數卡
if( !IK220Init(IK220BusNumber, &IK220SlotNumber, IK220PciData) )
{
IK220Cleanup();
MsgAndExit( L"Device initialization failed." );
}
RtWprintf ( L"IK220計數卡初始化完畢!\n");
}
BOOL IK220Init(
int busNumber, // 輸入:匯流排號
PCI_SLOT_NUMBER *pSlotNumber, // 輸入:裝置號和功能號
PPCI_COMMON_CONFIG PciData // 輸入:PCI配置暫存器的內容
)
{
u16_t reg_word;
LARGE_INTEGER liLocalConfigMem;
LARGE_INTEGER liPhysAddr0;
LARGE_INTEGER liPhysAddr1;
ULONG AddressSpaceType;
ULONG bytesWritten;
LARGE_INTEGER tranMemAddr;
LARGE_INTEGER SleepTime;
SleepTime.QuadPart = 10;
RtWprintf ( L"1");
liLocalConfigMem.QuadPart = PciData->u.type0.BaseAddresses[0];
AddressSpaceType = 0x0; //memory address
RtTranslateBusAddress (
PCIBus, // bus interface type
busNumber, // bus number (zero based)
liLocalConfigMem, // bus-relative address
&AddressSpaceType, // specifies port number or a memory address
&tranMemAddr // pointer to the translated address
);
pLocalConfigMem = (PUSHORT)RtMapMemory(tranMemAddr,0x54,0);
RtWprintf ( L"2");
liPhysAddr0.QuadPart = PciData->u.type0.BaseAddresses[2];
AddressSpaceType = 0x0; //memory address
RtTranslateBusAddress (
PCIBus, // bus interface type
busNumber, // bus number (zero based)
liPhysAddr0, // bus-relative address
&AddressSpaceType, // specifies port number or a memory address
&tranMemAddr // pointer to the translated address
);
pPhysAddr0 = (PUSHORT)RtMapMemory(tranMemAddr,0x20,0);
RtWprintf ( L"3\n");
liPhysAddr1.QuadPart = PciData->u.type0.BaseAddresses[3];
AddressSpaceType = 0x0; //memory address
RtTranslateBusAddress (
PCIBus, // bus interface type
busNumber, // bus number (zero based)
liPhysAddr1, // bus-relative address
&AddressSpaceType, // specifies port number or a memory address
&tranMemAddr // pointer to the translated address
);
pPhysAddr1 = (PUSHORT)RtMapMemory(tranMemAddr,0x20,0);
RtWprintf ( L"4\n");
PciData->Command = ( PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE |
PCI_ENABLE_BUS_MASTER | PCI_ENABLE_WRITE_AND_INVALIDATE );
bytesWritten = RtSetBusDataByOffset (
PCIConfiguration, // type of bus data to be set
busNumber, // bus number (zero based)
pSlotNumber->u.AsULONG, // logical slot number
PciData, // pointer buffer containing configuration information
0, // byte offset in the buffer
PCI_COMMON_HDR_LENGTH // number of bytes in the buffer
);
if( bytesWritten == 0 )
return FALSE;
RtWprintf ( L"5");
//IK220Reset();
//Sleep(50);
//IK220Stop();
//Sleep(50);
//IK220StartRef();
//Sleep(50);
if(!DownLoad(Pgm220, sizeof(Pgm220))) return FALSE;
RtWprintf ( L"6\n");
if(!InputW(IK220_CLEAR_FLAG_1_REGISTER, ®_word)) return FALSE;
if(!OutputW(IK220_CONTROLREGISTER, IK220_RUNMODE)) return FALSE;
do
{
RtSleepFt(&SleepTime);
if(!InputW(IK220_FLAG_1_REGISTER, ®_word)) return FALSE;
}while(!(reg_word & IK220_G28SEM_10));
if(!InputW(IK220_CLEAR_FLAG_1_REGISTER, ®_word)) return FALSE;
Sleep(50);
IK220WritePar(1,0);
Sleep(50);
IK220WritePar(2,1);
Sleep(50);
IK220WritePar(11,19);
Sleep(50);
IK220WritePar(8,1);
Sleep(50);
IK220WritePar(9,1);
Sleep(50);
IK220WritePar(10,8);
Sleep(50);
IK220Reset();
IK220Stop();
Sleep(50);
IK220StartRef();
Sleep(50);
IK220WritePar(14,2);
Sleep(50);
RtWprintf ( L"Init done!\n");
return TRUE;
}
//
// IK220_RS485_APP.c - C file
//
//
//////////////////////////////////////////////////////////////////
#include "IK220_RS485_APP.h"
#include <math.h>
/************************************************************************/
//主程式
/************************************************************************/
u16_t Status[10] = {0};
const double Ts = 0.0002;
const LONGLONG SENDCYCLE = 2000; //設定中斷間隔為2000*100納秒
const double pi =3.1415926535;
//const double mutfre = 0.038;
LARGE_INTEGER liPeriod;
u8_t i;
u8_t modeFlag = 0;
u8_t sig = 0;
long tsc = 0;
FILE* fp=NULL;
FILE* fp1=NULL;
FILE* fp2=NULL;
HANDLE hTimer = NULL;
double STARTPOINT = 0.0;
//double STARTPOINT = 0;
double Scurvey[100000] = {0};
double Scurvev[100000] = {0};
double Scurvea[100000] = {0};
double Disturbr[50] = {0};
double fpSave[100000] = {0};
double fvSave[100000] = {0};
double conpSave[100000] = {0};
double convSave[100000] = {0};
double ioutSave[100000] = {0};
double rub[10000] = {0};
double rub1[10000] = {0};
void _cdecl wmain( int argc, wchar_t **argv, wchar_t **envp )//主函式
{
u32_t temp;
LARGE_INTEGER x;
if ( RtSetThreadPriority( GetCurrentThread(), RT_PRIORITY_MAX) == FALSE )
//設定當前執行緒優先順序
{
RtWprintf(L"設定優先順序錯誤:%d\n", GetLastError() );
ExitProcess( 1 );
}
SearchIK220Card();//查詢IK220卡;下邊檔案IK220FUNCS.c中呼叫
Sleep(3);
SearchCP132Card();//查詢ICP132卡;
Sleep(3);
/*設定定時器時間間隔 百納秒*/
liPeriod.QuadPart = SENDCYCLE;// 10000;
//建立定時器核心執行緒
if (!(hTimer = RtCreateTimer(NULL,0,TimerHandler,NULL,RT_PRIORITY_MAX,
CLOCK_FASTEST)))
{
RtWprintf(L"建立定時器錯誤:%d\n", GetLastError() );
ExitProcess( 1 );
}
IK220Get32(&temp);
while(!IK220Get32(&temp))
{
;
}
//週期中斷
if (!RtSetTimerRelative( hTimer,&liPeriod,&liPeriod ) )
{
RtWprintf(L"掛接定時器失敗:%d\n", GetLastError() );
ExitProcess( 1 );
}
RtWprintf(L"定時器初始化完成!\n");
while(!sig)
{
Sleep(200);
}
RtCancelTimer( hTimer, &x);
RtDeleteTimer( hTimer);
ExitProcess(0);
}
void RTFCNDCL TimerHandler(void *nContext)
{
switch(modeFlag)
{
case 0: findZero(); break;
case 1: stickZero(); break;
case 2: freeOut(); break;
case 3: startScurve(); break;
default: zeroOut(); sig = 1;SuspendThread( GetCurrentThread ( ) ); break;
}
return;
}
//以下在IK220FUNCS.c 中。cp132的類似,在CP132Funcs中
void SearchIK220Card(void)
{
//定義IK220卡PCI配置引數儲存變數
UCHAR IK220_PCI_CR_Buffer[PCI_COMMON_HDR_LENGTH];
PPCI_COMMON_CONFIG IK220PciData = ( PPCI_COMMON_CONFIG ) IK220_PCI_CR_Buffer;
static PCI_SLOT_NUMBER IK220SlotNumber;
u32_t IK220BusNumber;
//查詢IK220計數卡
IK220BusNumber = DeviceSearch( IK220_VENDOR_ID, IK220_DEVICE_ID,
IK220_Subvendor_ID,IK220_Subdevice_ID, &IK220SlotNumber, IK220PciData );
RtWprintf ( L"已搜尋到IK220計數卡!\n");
//初始化IK220計數卡
if( !IK220Init(IK220BusNumber, &IK220SlotNumber, IK220PciData) )
{
IK220Cleanup();
MsgAndExit( L"Device initialization failed." );
}
RtWprintf ( L"IK220計數卡初始化完畢!\n");
}
BOOL IK220Init(
int busNumber, // 輸入:匯流排號
PCI_SLOT_NUMBER *pSlotNumber, // 輸入:裝置號和功能號
PPCI_COMMON_CONFIG PciData // 輸入:PCI配置暫存器的內容
)
{
u16_t reg_word;
LARGE_INTEGER liLocalConfigMem;
LARGE_INTEGER liPhysAddr0;
LARGE_INTEGER liPhysAddr1;
ULONG AddressSpaceType;
ULONG bytesWritten;
LARGE_INTEGER tranMemAddr;
LARGE_INTEGER SleepTime;
SleepTime.QuadPart = 10;
RtWprintf ( L"1");
liLocalConfigMem.QuadPart = PciData->u.type0.BaseAddresses[0];
AddressSpaceType = 0x0; //memory address
RtTranslateBusAddress (
PCIBus, // bus interface type
busNumber, // bus number (zero based)
liLocalConfigMem, // bus-relative address
&AddressSpaceType, // specifies port number or a memory address
&tranMemAddr // pointer to the translated address
);
pLocalConfigMem = (PUSHORT)RtMapMemory(tranMemAddr,0x54,0);
RtWprintf ( L"2");
liPhysAddr0.QuadPart = PciData->u.type0.BaseAddresses[2];
AddressSpaceType = 0x0; //memory address
RtTranslateBusAddress (
PCIBus, // bus interface type
busNumber, // bus number (zero based)
liPhysAddr0, // bus-relative address
&AddressSpaceType, // specifies port number or a memory address
&tranMemAddr // pointer to the translated address
);
pPhysAddr0 = (PUSHORT)RtMapMemory(tranMemAddr,0x20,0);
RtWprintf ( L"3\n");
liPhysAddr1.QuadPart = PciData->u.type0.BaseAddresses[3];
AddressSpaceType = 0x0; //memory address
RtTranslateBusAddress (
PCIBus, // bus interface type
busNumber, // bus number (zero based)
liPhysAddr1, // bus-relative address
&AddressSpaceType, // specifies port number or a memory address
&tranMemAddr // pointer to the translated address
);
pPhysAddr1 = (PUSHORT)RtMapMemory(tranMemAddr,0x20,0);
RtWprintf ( L"4\n");
PciData->Command = ( PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE |
PCI_ENABLE_BUS_MASTER | PCI_ENABLE_WRITE_AND_INVALIDATE );
bytesWritten = RtSetBusDataByOffset (
PCIConfiguration, // type of bus data to be set
busNumber, // bus number (zero based)
pSlotNumber->u.AsULONG, // logical slot number
PciData, // pointer buffer containing configuration information
0, // byte offset in the buffer
PCI_COMMON_HDR_LENGTH // number of bytes in the buffer
);
if( bytesWritten == 0 )
return FALSE;
RtWprintf ( L"5");
//IK220Reset();
//Sleep(50);
//IK220Stop();
//Sleep(50);
//IK220StartRef();
//Sleep(50);
if(!DownLoad(Pgm220, sizeof(Pgm220))) return FALSE;
RtWprintf ( L"6\n");
if(!InputW(IK220_CLEAR_FLAG_1_REGISTER, ®_word)) return FALSE;
if(!OutputW(IK220_CONTROLREGISTER, IK220_RUNMODE)) return FALSE;
do
{
RtSleepFt(&SleepTime);
if(!InputW(IK220_FLAG_1_REGISTER, ®_word)) return FALSE;
}while(!(reg_word & IK220_G28SEM_10));
if(!InputW(IK220_CLEAR_FLAG_1_REGISTER, ®_word)) return FALSE;
Sleep(50);
IK220WritePar(1,0);
Sleep(50);
IK220WritePar(2,1);
Sleep(50);
IK220WritePar(11,19);
Sleep(50);
IK220WritePar(8,1);
Sleep(50);
IK220WritePar(9,1);
Sleep(50);
IK220WritePar(10,8);
Sleep(50);
IK220Reset();
IK220Stop();
Sleep(50);
IK220StartRef();
Sleep(50);
IK220WritePar(14,2);
Sleep(50);
RtWprintf ( L"Init done!\n");
return TRUE;
}
相關文章
- 符合PCI的Web應用開發指南:PCI是線上支付必過關卡Web
- 水晶報表引數程式設計示例程式碼程式設計
- Linux下PCI轉串列埠卡驅動安裝方法Linux串列埠
- Linux下PCI轉串列埠卡及USB轉串列埠Linux串列埠
- IBM P670更換PCI卡IBM
- 深入講解Flutter應用模板原始碼:計數器示例Flutter原始碼
- 自己寫的部分實用的程式碼
- Delphi之三匯模擬語音卡(SHT-8B/PCI/FAX)可複用原始碼原始碼
- 實現保留指定數目小數的示例程式碼
- MT碳數藏系統程式設計開發技術(程式碼示例)程式設計
- .NET用使用儲存過程獲取輸出引數的程式碼示例!儲存過程
- werkzeug原始碼分析——從官網的示例程式碼開始原始碼
- 運用關卡結構圖分析《曠野之息》的關卡設計
- Android HAL 層框架分析以及程式碼示例Android框架
- 影片聊天原始碼,同步、非同步示例程式碼分析原始碼非同步
- 卡馬克:用C++進行函數語言程式設計C++函數程式設計
- Ubuntu下統計專案的程式碼行數-cloc工具Ubuntu
- xargs 的示例程式碼.
- 《Sencha應用程式的UI測試 》一文的示例分析 薦UI
- RabbitMQ 程式碼示例MQ
- lisp 程式碼示例Lisp
- 排序程式碼示例排序
- 函數語言程式設計實用介紹(下)函數程式設計
- 安卓應用安全指南 5.6.1 密碼學 示例程式碼安卓密碼學
- 程式設計師的燈下黑:能認識自己嗎?程式設計師
- 使用SQL實現車流量的計算的示例程式碼SQL
- 英偉達CES開年釋出:新卡RTX 2060下週2400元開售,17款RTX筆記本同發筆記
- Java NIO 程式碼示例Java
- java SPI 程式碼示例Java
- linux下音效卡程式設計試探(轉)Linux程式設計
- WIN下的普通備份指令碼示例指令碼
- 常用設計模式之.Net示例程式碼合集設計模式
- 物件導向設計介紹和程式碼示例物件
- 記一次髮卡網程式碼審計
- 統計程式碼行數
- 精簡自己 20% 的程式碼
- 【原創】Linux PCI驅動框架分析(一)Linux框架
- 【原創】Linux PCI驅動框架分析(二)Linux框架