RTX下IK220計數卡的PCI示例程式碼,自己分析用

bbs598598發表於2012-08-23
//////////////////////////////////////////////////////////////////
//
// 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, &reg_word)) return FALSE;
    if(!OutputW(IK220_CONTROLREGISTER, IK220_RUNMODE)) return FALSE;
    do
    {
        RtSleepFt(&SleepTime);
        if(!InputW(IK220_FLAG_1_REGISTER, &reg_word)) return FALSE;
    }while(!(reg_word & IK220_G28SEM_10));
    if(!InputW(IK220_CLEAR_FLAG_1_REGISTER, &reg_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;
}

相關文章