NI Linux Real-Time Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

FPGA Interface C API Example for NI Linux Real-Time and Eclipse

This document contains an FPGA Interface C API example for use on NI Linux Real-Time targets. It was developed in the C/C++ Development Tools for NI Linux Real-Time, Eclipse Edition.

The purpose of this example is to demonstrate the various host to FPGA communication mechanisms available in the FPGA Interface C API, all in one application, as a supplement to the existing FPGA Interface C API examples. The application is relatively simple, and is intended to be run from the command line in NI Linux Real-Time. The example uses the following Host-to-FPGA communication methods:

  • DMA FIFO (FPGA to Host and Host to FPGA)
  • Basic Read/Write controls
  • IRQ for Host-FPGA synchronization

To use this example, please install and configure the Eclipse tools to ensure that you have the ability to cross compile and deploy to a Linux Real-Time target. To do so, follow this tutorial: Getting Started with C/C++ Development Tools for NI Linux Real-Time, Eclipse Edition.

You will then need to import the C project into your workspace in Eclipse.

  1. Download and unzip the attached files. Then, open Eclipse and go to File >> Import and select General >> Existing Projects into Workspace.
  2. Choose the "C Code" folder from the attached zip file.
  3. Ensure "Copy Projects into workspace" is enabled
  4. Click Finish.

Note: You will also need to link against libdl in order to use any RIO driver functions. To do this, open up the Properties dialog for your project. Then expand the C/C++ Build section and select Settings. Under the tab Tool Settings, expand Cross G++ Linker and then select Libraries. In that category, add "dl" under the Libraries (-l) header.

The attached zip file contains LabVIEW code (LabVIEW FPGA and a LabVIEW Real-Time test VI) as well as C code. The example was built for a cRIO-9068, so if you are using that target then you do not need to open or recompile the LabVIEW FPGA code, as long as your NI-RIO version is still compatible. If you do need to recompile the FPGA code, download the FPGA Interface C API. Then follow this tutorial to learn how to use it:

Building a R Series FPGA Interface Host Application in C.

Before running the application, ensure that you have transferred the FPGA bit file ("NiFpga_FPGA.lvbitx") to your target to the same directory that you deployed your built C application.

Please note: This code is still in beta and may contain minor errors


Colden
Download All
Comments
Member
Member
on

HI All

Thanks for the tutorial. I'm just getting to grips with the cRio and hope this is the correct place to post a question like this. I've deployed a few very simple applications successfully and am now trying to run this example but have run into difficulty. Every time I run application I get the following error:

(safemode) admin@cRioKE:~# echo $PWD'>'

/home/admin>

(safemode) admin@cRioKE:~#

(safemode) admin@cRioKE:~# /home/admin/cRioKE/FPGA_General_Communication;exit

Initializing...

Error opening libNiFpga.so: libNiFpga.so: cannot open shared object file: No such file or directory

Opening a Session...

Error! Exiting program. LabVIEW error code: -52006

logout

I suspect this is something to do with transferring the FPGA bit file: it seems to be in the same directory as my built C application on the cRio but maybe there's a trick I'm missing somewhere? Thanks for any help.

Regards

eclipse_NI_screenshot.jpg

Active Participant
Active Participant
on

Note that the controller is in safemode, and within safemode you cannot access the FPGA. Install LabVIEW Real-Time and NI-RIO/CompactRIO support to the target through MAX. (note that the console promt tells you that you are in safemode)

From the screenshot, it seems that you've transferred the bitfile over fine.

Member
Member
on

Thanks for the help. My bad:- installed Real-Time properly through max, however I'm now getting the following error which also has me stumped: any ideas? I've searched for the error code but so far no luck.

admin@cRioKE:~# echo $PWD'>'

/home/admin>

admin@cRioKE:~#

admin@cRioKE:~# /home/admin/cRioKE/FPGA_General_Communication;exit

Initializing...

Opening a Session...

Error! Exiting program. LabVIEW error code: -63101

logout

NI Employee
on

Hello Macster,

That error code corresponds to:

------------------------------------------------------------------------------

Error -63101 occurred at an unidentified location

Possible reason(s):

NI-RIO:  A valid .lvbitx bitfile is required. If you are using a vaild .lvbitx bitfile, the bitfile may not be compatible with the software you are using. Determine which version of LabVIEW was used to make the bitfile, update your software to that version or later, and try again.

------------------------------------------------------------------------------

Basically it means your code isn't pointing to your bit file correctly, or your bit file was built in the wrong version of LabVIEW. The second option is less likely, so make sure you have your file paths correct in your C code, especially when you call FPGA Open.

If your file paths are correct, what version of LabVIEW did you load onto the target, and what version of LabVIEW FPGA did you use to build the bit file?

Colden
Member
Member
on

I loaded LabView Real Time 13.0.0 onto the cRio. Because I'm using a cRIO-9068 I didn't build the bitfile myself, just used the one that was included in the project.

Being relatively new to C/C++ programming as far as I can tell

#define NiFpga_FPGA_Bitfile "NiFpga_FPGA.lvbitx"

defines the file  name which is the same as the file copied to the cRio  folder containing the application as shown in the first post. But you're right, removing the bitfile form the folder means the exact same error comes up, so somewhere along the line, that file isn't being found.

So I'm a bit stumped. Guess it's worth trying to build the bitfile myself? MAX screenshot included below just incase there's a glaring error in software somewhere. Thanks again for the help.

Cheers

Jamie

max_NI_screenshot.jpg

NI Employee
on

Hello macster,

You shouldn't need to rebuild the bit file - the one included with the project should be compatible with the 9068 and NI-RIO 13.0.

My guess is that it has to do with the way you're calling the program in Linux. If the code is executing in the folder where the bit file is, you're fine. It could be executing in a different folder, in which case you need to give it a full static path to the bit file.

Colden
Member
Member
on

Hi All

Sorry to be a pain about this but I'm not getting anywhere here.

I definately have the bitfile in the same location as my application on the cRIO. Just to try I've changed the bitfile field in the application to:

static const char* const NiFpga_FPGA_Bitfile = "/home/admin/cRioKE/NiFpga_FPGA.lvbitx";

Now i get the error code is -61201. (Any way to find out what these are without LabView installed?) In terms of how I'm running the code, I've set things up based on the c/c++ eclipse tutorial but obviously with different project paths etc. Thanks for the help.

Member
Member
on

Looks like you need to uninstall NI-RIO IO Scan.  Your error is:

Error -61201 occurred at an unidentified location

Possible reason(s):

LabVIEW FPGA:  The chassis is in Scan Interface programming mode. In order to run FPGA VIs, you must go to the chassis properties page, select FPGA programming mode, and deploy the settings.

Active Participant
Active Participant
on

From the C/C++ interface, it's a little more difficult, not sure if there's a way to automate this but there are the error files at /usr/local/natinst/labview/errors on the device itself. From lvfpga.err, -61201 means that the device's FPGA is currently in Scan Interface mode:

...

<nierror code="-61201">

LabVIEW FPGA:  The chassis is in Scan Interface programming mode. In order to run FPGA VIs, you must go to the chassis properties page, select FPGA programming mode, and deploy the settings.

</nierror>

...

NI Employee
on

You can also see a list of error codes for the C Interface here:

http://zone.ni.com/reference/en-XX/help/372928G-01/capi/errors/

That information should also be available locally on your dev machine if you have the FPGA Interface C API installed, in the help documentation.

But yes, in this case DAQjr is right - you need to uninstall NI-RIO IO Scan. Your controller will boot up in scan mode y default if you have it installed, unless you deploy the chassis in FPGA mode. There isn't an easy way to deploy the chassis in FPGA mode without LabVIEW.

Colden
Member
Member
on

Ok, thanks all. It's now working. For future reference for those who are as new to this system as I am:

1) Took the device out of safe mode and made sure Real-Time and NI-RIO/CompactRIO was installed properly.

2) I had to uninstall, NI-RIO IO Scan, something I installed without really thinking about what it might do.

3) In NiFpga_FPGA.h I changed the line

#define NiFpga_FPGA_Bitfile "NiFpga_FPGA.lvbitx"

to

static const char* const NiFpga_FPGA_Bitfile = "/home/admin/cRioKE/NiFpga_FPGA.lvbitx";

thereby referencing exactly where my bitfile is on my cRIO. I am still unsure as to why the original line does not work as both application and bitfilre are in the same folder.

Thanks for all the help.


Active Participant
Active Participant
on

It all depends on where you run the resulting binary from.

For example, if the binary was at

/a/b/c/d/binary

and it includes a reference to a file "foo", also at

/a/b/c/d/foo

and you run the binary from another folder,

/a/b $ ./c/d/binary < running binary using a relative path ".(the current directory)/c/d/binary

^in folder /a/b

the program will use "." or the current working directory (/a/b) when building a full path given a relative path name (in this case, the program's relative referrence to "foo" would be constructed as "/a/b/foo"). In UNIX-like systems, "." is shorthand for "the present working directory". Likewise, ".." is shorthand for "one directory up from the current path".

Starting at /a/b,

"cd .." will bring you to /a

"cd ." will keep you at /a

"cd .././a/b/./.." will move you to /, keep you there, back to /a, then to /a/b, keep you at /a/b, then back to /a

So, bringing that back to the program that you're using, if you make sure that you ran the program from the directory that the bitfile and the program actually exist at (in other words, make sure that the present working directory is the directory that houses both the bitfile and the binary), you shouldn't need to change the path to be an absolute, full path.

Member
Member
on

Dear officer,

Hi! I have met the same problem as macster met,as when I run the program from Ecilpse,it always comes up with the error -63101,I am a liitle bit confused about the path that point to the bitfile in C code,because I do not know how to point to it in the software.

Really thanks for your help!

Sincerely,

Liu DiQQ图片20140618121825.jpgQQ图片20140618121947.jpg

Active Participant
Active Participant
on

Here are a couple of things to check. First, just a basic sanity check; is the lvbitx file actually on your target? Where? You can use the Remote System Explorer to see. Next, check the header that exists in your project (either in NiFpga.h or, more likely, in $NAME_OF_YOUR_BITFILE.h), it will list a name for the bitfile that it is attempting to open. As I recall, it is a relative path that assumes that the binary is run from the same directory that houses the bitfile. If in doubt, you could modify your program to print out what it sees as the current working directory is, and that can tell you why it is unable to find the bitfile.

Hope this helps, and note that there are many, many good resources out there online on how to work with files on Linux systems, specifically within C/C++.

Member
Member
on

Dear officer,

Hi! When I modify my file path as macster did,I print out the error -61203.As at first I did not find my bitfile in the remote system explorer,I drag it into the path /home/admin.Is there any soulution for this?

Thanks for your help!

Sincerely,

Liu Di

Active Participant
Active Participant
on

-61203 corresponds to the FPGA being busy in so-called "interactive mode". Are you running the FPGA VI interactively from within LabVIEW (e.g. opened a project, a LabVIEW FPGA's front-panel, and are running it)?

http://zone.ni.com/reference/en-XX/help/372928G-01/capi/errors/

Member
Member
on

Yes I am running the Labview project and also the front panel.A strange thing is that I just want to use a FIFO to complete the mission that when I load some data from C code,and the Labview project will read them and calculate their average.When I am running the Labview project,it appears the error -61203,but when I stop the project and run the C code,I can get the average of them.What is that supposed?

Sorry for bothering so much,thanks for your help!

Sincerely,

Liu Di

Active Participant
Active Participant
on

The primary issue is that only one session (type) is allowed when communicating with the LabVIEW FPGA VI. This is intended behavior. Anytime that you are running an FPGA VI's front panel to change/view values, that prevents the other means of  interacting with the FPGA VI.

Contributors