實驗平臺說明:安裝了NI LabVIEW 2015 32bit版本,安裝了NI FPGA Interface C API Generator,安裝了硬件PCIe-7842R;安裝了Visual Studio 2015(下載的C API Generator說明是針對VS2013,不過實驗測試發現vs2015能夠使用);按照官網給的例子進行實驗http://www.ni.com/tutorial/8638/en/,只測試了FPGA板卡模擬輸入功能,沒有寫模擬輸出功能。官網例子是採用NI公司的LabWindows/CVI上位機軟件。session
實驗步驟:app
此工程中,FPGA和上位機有四個接口:3個參數輸入接口(Samples等等),1個數據讀取接口(FIFO),接口通訊經過PCIe進行。編譯該工程,獲得bitfile文件函數
2.打開FPGA Interface C API Generator,導入以前用LabVIEW編譯生成的FPGA bitfile,點擊generate,生成了幾個個文件:1.FPGA bitfile; 2.NiFpga.c;3.NiFpga.h;4.NiFpga_FPGA.h;oop
其中NiFpga_FPGA.h包含了應用程序中函數調用須要的常量,和上述讀寫結構的地址信息測試
NiFpga.c和NiFpga.h對全部工程都是同樣的,包含了調用NI FPGA的各類系統函數。ui
3.新建visual studio控制檯應用程序,添加上述幾個文件spa
4.NiFpga.c設置編譯屬性,選擇No Precompile,以下。(若是不這樣設置,編譯的時候會因C/C++編譯問題而報錯)code
5.編譯上位機程序,主要是打開FPGA(完成下載bitfile到FPGA的任務),按照地址讀寫FPGA中的接口,關閉FPGA等操做。測試程序以下:blog
1 // ConsoleApplication1.cpp : Defines the entry point for the console application. 2 // 3 // NIFPGATest.cpp : Defines the entry point for the console application. 4 // 5 #include "stdafx.h" 6 #include "NiFpga_FPGA.h" 7 #include <malloc.h> 8 9 int main() 10 { 11 /* must be called before any other calls */ 12 printf("Initializing...\n"); 13 NiFpga_Status status = NiFpga_Initialize(); 14 if (NiFpga_IsNotError(status)) 15 { 16 NiFpga_Session session; 17 /* opens a session, downloads the bitstream, and runs the FPGA */ 18 printf("Opening a session...\n"); 19 /* opens a session, downloads the bitstream, but does not run the FPGA */ 20 NiFpga_MergeStatus(&status, NiFpga_Open(NiFpga_FPGA_Bitfile, NiFpga_FPGA_Signature, 21 "RIO0", NiFpga_OpenAttribute_NoRun, &session)); 22 if (NiFpga_IsNotError(status)) 23 { 24 /* declare variables for output and input */ 25 double numSamples, aorate, airate; 26 uint16_t threshold = 0; 27 uint32_t r, timeout = 10000/* 10 seconds */; 28 NiFpga_Bool overLimit; 29 int16_t *data = NULL; 30 numSamples = 10; 31 airate = 1000; 32 /* allocate size for the samples to read */ 33 data = (int16_t*)malloc(sizeof(int16_t) * numSamples); 34 35 /* write the number of samples and loop rate to the FPGA VI */ 36 NiFpga_MergeStatus(&status, NiFpga_WriteI32(session, NiFpga_FPGA_ControlI32_Samples, numSamples)); 37 NiFpga_MergeStatus(&status, NiFpga_WriteU32(session, NiFpga_FPGA_ControlU32_LoopPeriod, airate)); 38 NiFpga_MergeStatus(&status, NiFpga_WriteI16(session, NiFpga_FPGA_ControlI16_Threshold, threshold)); 39 40 /* run the FPGA application */ 41 printf("Running the FPGA...\n"); 42 NiFpga_MergeStatus(&status, NiFpga_Run(session, 0)); 43 44 /* read the DMA FIFO */ 45 NiFpga_MergeStatus(&status, NiFpga_ReadFifoI16(session, NiFpga_FPGA_TargetToHostFifoI16_FIFO, 46 data, numSamples, timeout, &r)); 47 48 /* read the Over Limit? boolean */ 49 NiFpga_MergeStatus(&status, NiFpga_ReadBool(session, NiFpga_FPGA_IndicatorBool_OverLimit, 50 &overLimit)); 51 52 /* close the session now that we're done */ 53 printf("Closing the session...\n"); 54 NiFpga_MergeStatus(&status, NiFpga_Close(session, 0)); 55 } 56 /* must be called after all other calls */ 57 printf("Finalizing...\n"); 58 NiFpga_MergeStatus(&status, NiFpga_Finalize()); 59 } 60 /* check if anything went wrong */ 61 if (NiFpga_IsError(status)) 62 { 63 printf("Error %d!\n", status); 64 printf("Press <Enter> to quit...\n"); 65 getchar(); 66 } 67 68 return status; 69 }
參考文獻:接口