zoukankan      html  css  js  c++  java
  • 使用VisualStudio读写NI FPGA板卡实例(基于FPGA Interface C API Generator)

    实验平台说明:安装了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上位机软件。

    实验步骤:

    1. 在LabVIEW中新建FPGA工程,命名为FPGA.vi

    此工程中,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;

    其中NiFpga_FPGA.h包含了应用程序中函数调用需要的常量,和上述读写结构的地址信息

    NiFpga.c和NiFpga.h对所有工程都是一样的,包含了调用NI FPGA的各种系统函数。

           3.新建visual studio控制台应用程序,添加上述几个文件

      4.NiFpga.c设置编译属性,选择No Precompile,如下。(如果不这样设置,编译的时候会因C/C++编译问题而报错)

                5.编译上位机程序,主要是打开FPGA(完成下载bitfile到FPGA的任务),按照地址读写FPGA中的接口,关闭FPGA等操作。测试程序如下:

     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...
    ");
    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...
    ");
    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...
    ");
    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...
    ");
    54             NiFpga_MergeStatus(&status, NiFpga_Close(session, 0));
    55         }
    56         /* must be called after all other calls */
    57         printf("Finalizing...
    ");
    58         NiFpga_MergeStatus(&status, NiFpga_Finalize());
    59     }
    60     /* check if anything went wrong */
    61     if (NiFpga_IsError(status))
    62     {
    63         printf("Error %d!
    ", status);
    64         printf("Press <Enter> to quit...
    ");
    65         getchar();
    66     }
    67 
    68     return status;
    69 }

    参考文献:

    • 官网给出的R系列FPGA的C API用法说明http://www.ni.com/tutorial/8638/en/
    • 电脑上给出的FPGA Interface C API Generator的example

     

  • 相关阅读:
    洛谷 P1990 覆盖墙壁
    洛谷 P1033 自由落体
    洛谷 P2049 魔术棋子
    洛谷 P2183 巧克力
    poj_1743_Musical Theme(后缀数组)
    Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset
    Codeforces Round #367 (Div. 2) C. Hard problem
    hdu_5831_Rikka with Parenthesis II(模拟)
    hdu_5826_physics(物理题)
    hdu_5821_Ball(贪心)
  • 原文地址:https://www.cnblogs.com/yuesheng/p/7189496.html
Copyright © 2011-2022 走看看