The myRIO C library
The T1 target computer, the NI myRIO, has an associated C library that is included in the D1 VM distribution workspace under the C Support for myRIO directory. This contains several helpful functions and data types in files under source for interacting with myRIO I/O. For speed, nearly all of the I/O is handled by the FPGA. The FPGA comes preconfigured with a “personality” (i.e., a bitstream configuration file) that defines how C programs running on the microprocessor (i.e., the programs we write) can access the I/O. Interfacing with the I/O through the FPGA comprises the majority of the myRIO’s C library.
Of special importance is the header file MyRio.h, which we will include in each program
written for the myRIO: #include "MyRio.h"
.
This header file includes MyRio1900.h, which
in turn includes NiFpga.h, which declares the
primary I/O data types and functions. Whenever we want to open a myRIO
FPGA session, we call
; // declare status type
NiFpga_Status status= MyRio_Open(); // open myRIO FPGA session status
The NiFpga_Status
type is defined
in the NiFpga.h header file with
typedef int32_t NiFpga_Status;
The C facility typedef
creates a
new data type (e.g., int
, double
) in terms of
some other data type. In this sense, it is more of an alias than a
proper new data type. In the typedef
code given
previously, NiFpga_Status
is defined
to be synonymous with the built-in C type int32_t
, a
fixed-width integer of width 32 bits.1 A value of 0
indicates proper
status, negative integers correspond to error codes, and positive
integers are warnings.
The MyRio_Open()
function’s prototype is declared in MyRio.h as
follows:
(); NiFpga_Status MyRio_Open
Its function is to open a communication session between the microprocessor and the FPGA. This must be called before any other FPGA function.
As we have already mentioned, an NiFpga_Status
of 0
indicates the
successful opening of a session. The constant NiFpga_Status_Success
is the name for this.
There are several other NiFpga_Status
es defined in NiFpga.h, like the error status NiFpga_Status_MemoryFull
, which has integer
value -52000
.
It is important to test the status after calling MyRio_Open()
. In
MyRio.h
, the function MyRio_IsNotSuccess()
is defined. It accepts an argument of type NiFpga_Status
and returns another custom
type, NiFpga_Bool
, defined as
typedef uint8_t NiFpga_Bool;
This is the C unsigned eight-bit integer type and can take on the
value NiFpga_False
(0
) or NiFpga_True
(1
). Assigning names
to logical true and false is common practice in C, which contents itself
with 0
and
1
to serve
this purpose. Testing for the successful opening of an FPGA session,
then, is a matter of
= MyRio_Open(); // open myRIO FPGA session
status if (MyRio_IsNotSuccess(status)) { // test if sess. opened successfully
return status; // if not, return error/warning code
}
Placing this in a main()
function
attempts to open an FPGA session, but if it is not successful, the
execution will terminate and return an error code. Given that I/O is
necessary for virtually all interesting programming of the myRIO, these
statements should appear in all main()
programs that
we write.
An open FPGA session should be closed before the end of main()
. The session
can be closed with
= MyRio_Close(); // close myRIO FPGA session status
Again, a nonzero returned status is an error code or warning.
Of course, after we open a session and before we close it, we will want to actually interact with the myRIO I/O through the FPGA. In the lab exercises that follow, we will do just that!
csupport6, fpgaPersonality6
A fixed-width integer differs from an
int
in that the width ofint
depends on the compiler.↩︎
Online Resources for Section 0.5
No online resources.