Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Oct 17, 2006


Feedback


Yes No

Related Categories

Related Links - Developer Zone

Related Links - Products and Services

Using Clusters and Arrays in LabVIEW FPGA

4 ratings | 3.50 out of 5
Print

Overview

LabVIEW FPGA supports clusters, and arrays of fixed size. Although these data types provide many benefits, under certain conditions their use can result in reduced performance or increased FPGA gate usage.

Clusters and Arrays in the Front Panel

When you place a cluster or array on the front panel of your main LabVIEW FPGA VI, the resulting LabVIEW implementation includes support for the FPGA Interface. The FPGA Interface enables your host VI to read or write the cluster/array.

For clusters and arrays, the FPGA Interface must implement the following behind the scenes:
  • Flatten/Unflatten: Since the LabVIEW FPGA target and the host LabVIEW target may use different conventions to store clusters and arrays, the data must be flattened before transfer, and unflattened after transfer. This flatten/unflatten process is similar to the mechanism LabVIEW uses for file I/O. In LabVIEW 8.0 or later, refer to the How LabVIEW Stores Data in Memory  topic in the LabVIEW Help  (linked below). In LabVIEW 7.1 or earlier, refer to the LabVIEW Data Storage application note in the LabVIEW Bookshelf for more information.
  • Access Protection: All elements of the cluster or array must be accessed as a unit. For example, if the host VI reads a cluster, and at the same time LabVIEW FPGA is writing that cluster, the host VI must wait for LabVIEW FPGA to complete its write. If this sort of protection did not exist, the host VI might read some cluster elements as old data (before the LabVIEW FPGA write), and the remaining elements as new data.
These implementation issues are simple for data types such as integer, but for clusters and arrays you may see some adverse effects. On the LabVIEW FPGA side, code for clusters/arrays can add a significant number of FPGA gates, and can result in reduced performance. On the host side, code to flatten/unflatten consumes processor time, which can result in reduced performance.

In addition to the front panel of your main LabVIEW FPGA VI, these conditions also apply to using clusters or arrays as global variables within LabVIEW FPGA.

Clusters and Arrays in the Diagram


Within the diagram of your LabVIEW FPGA VI, access to arrays and clusters is very efficient. Operations such as indexing an array or unbundling a cluster translate to relatively few FPGA gates. For example, you can use most array and cluster operations within a Single-Cycle Timed Loop.

Clusters and arrays are also efficient when used to pass data in or out of a subVI. Since the front panel of the subVI is not exposed to the host FPGA Interface, the implementation does not use the extra FPGA gates required for flatten/unflatten and access protection.

Handshaking with Integers


Although using clusters and arrays on your LabVIEW FPGA front panel is convenient, for some applications the resulting performance and gate usage may not meet your requirements. For such applications you may consider handshaking your cluster/array data one integer value at a time.

As an example, assume you have an array of 24 integer values in LabVIEW FPGA, and you want to transfer this data to your host VI as efficiently as possible. On the front panel of your LabVIEW FPGA VI, you define a single integer indicator (Data), and a single boolean indicator (Ready). Within LabVIEW FPGA, you use a For loop to iterate through the array. Within the For loop, you wire each element to Data, then set Ready true, then wait for Ready to change to false (element read by host). Within the host VI, you use a similar For loop to build an array of 24 integer values. Within the For loop, you use the FPGA Interface to read Data into the array, then set Ready false.

By using this handshaking technique, you break up the cluster/array into its component elements, and then rebuild those elements on the other side. This avoids flatten/unflatten of the cluster/array, and it enables you to protect the access yourself. In most cases, the result will be more efficient than using a cluster/array on the front panel.

Another benefit of handshaking with integers is that you can use FIFOs to store data in either LabVIEW FPGA or the host VI. Use of FIFOs often helps to isolate each side from latencies on the other side. This enables your application to avoid data loss, and in some cases it improves performance.

Examples: FPGA I/O for CAN


The Controller Area Network (CAN) is a real-time communications protocol used in embedded systems (such as automotive electronics) and industrial control systems. The NI 9853 is a CAN module for the CompactRIO Reconfigurable Embedded System. Access to the CAN ports of the NI 9853 is provided through the LabVIEW FPGA I/O functions.

The Properties dialog of the CAN output and input functions allows you to select the data type that you wire in/out as either cluster or array. If the cluster data type is selected, the I/O function uses a cluster in which each element represents a field of the CAN frame as defined by the CAN standard. If the array data type is selected, the I/O function uses an array of six U32 integers, which facilitates handshaking to/from the host VI.

Example 1 shows a basic LabVIEW FPGA VI for CAN. Clusters on the front panel are used to transmit a CAN frame, and to show the most recent CAN frame received (Figure 1). The diagram shows that using clusters with the CAN I/O functions enables a very simple implementation (Figure 2).

[+] Enlarge Image
Figure 1: LabVIEW FPGA Front Panel for Example 1
 

[+] Enlarge Image
Figure 2: LabVIEW FPGA Block Diagram for Example 1
 
Example 2 shows how you can use clusters efficiently within the LabVIEW FPGA diagram. This diagram reads a CAN frame as a cluster, and then compares the CAN identifier and data bytes to check for a trigger condition (Figure 3). Since this code avoids using the cluster on the front panel, it uses very few FPGA gates.

[+] Enlarge Image
Figure 3: LabVIEW FPGA Block Diagram for Example 2
 
Example 3 implements handshaking to transfer CAN frames from FPGA to the host VI. The LabVIEW FPGA front panel contains Data and Ready indicators to handshake CAN frames to the host VI (Figure 4).

The top loop of the LabVIEW FPGA diagram reads the CAN frame as an array of integers, then stores those integers in a FIFO (Figure 5). The CAN input waits for a new CAN frame prior to returning the data. The bottom loop of the LabVIEW FPGA diagram handshakes each CAN frame to the host VI one integer at a time. The two loops run independently, the top driven by CAN traffic (the CAN input waits for a received frame), and the bottom driven by handshaking with the host VI.

The diagram for the host VI uses the FPGA Interface to handshake integers for each CAN frame (Figure 6). Once a single CAN frame (6 integers) is received, it is written to a logfile. The contents of the logfile can be displayed at later time, such as waveforms of physical units (channels) using NI-CAN features.

Since example 3 handshakes with integers rather than using clusters, it is very efficient and fast. Although the LabVIEW FPGA diagram of example 3 is more complex than example 1, it uses fewer FPGA gates.

Figure 4 LabVIEW FPGA Front Panel for Example 3
 

[+] Enlarge Image
Figure 5 LabVIEW FPGA Block Diagram for Example 3
 

[+] Enlarge Image
Figure 6 LabVIEW host VI Block Diagram for Example 3
 

Conclusion

Each technique for using clusters/arrays in LabVIEW FPGA has potential benefits. Your choice of technique will depend on the requirements of your application.
Benefits of using clusters and arrays:
  • Simpler diagram (easy to use)
  • If you limit use to the diagram only (not front panel), fast and efficient
Benefits of handshaking with integers:
  • Faster for transfer to/from host VI
  • Fewer FPGA gates

Related Links

How LabVIEW Stores Data in Memory
4 ratings | 3.50 out of 5
Print

Reader Comments | Submit a comment »

I wish there was example code..
In this document there is a section entitled "Clusters and Arrays in the Front Panel". I am trying to implement the method suggested here and I wish there was an inclusion of example code.
- Jerry Aldridge, Automation Foundation. jerry.aldridge@automationfoundation.com - Nov 20, 2008

please add clustertoarray arraytocluster VIs to LVGPA palette for CAN applications
You may also add these VIs to the app.note on the web
- Davide Palandella, National Instruments. davide.palandella@ni.com - Mar 12, 2006

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).