Using the LabVIEW C Generator to Develop Algorithms for the Texas Instruments MSP430 Microprocessor

Publish Date: Aug 31, 2010 | 3 Ratings | 3.67 out of 5 |  PDF

Overview

With the LabVIEW C Generator, you can create optimized C code from your LabVIEW VIs. The C Generator combines C code generation with intuitive graphical programming so you can develop and prototype algorithms quickly and efficiently in LabVIEW before you use them in a C application. This tutorial guides you through developing a VI for C code generation, configuring C code generation settings, generating optimized C code, and deploying that C code to a Texas Instruments MSP430 Ultra-Low-Power Microcontroller.

Table of Contents

  1. Introduction
  2. Creating a C Code Generation Project
  3. Creating a VI for C Code Generation
  4. Setting C Code Generation Options
  5. Generating Optimized C Code
  6. Using Code Composer Studio to Implement LabVIEW Generated C Code.
  7. Related Links

1. Introduction

In this tutorial, you develop a VI from which you generate C code, then use Texas Instruments Code Composer Studio™ to develop and deploy the generated C code to a TI MSP430 Ultra-Low-Power Microcontroller.

The VI in this tutorial manipulates an image of the Texas Instruments logo, which is represented as a 1D array in the VI. After you deploy the generated C code, the MSP430 microcontroller displays the altered image on an LCD screen attached to the microcontroller. The following figure shows the array, the image it represents, and the effects that the VI performs on the image.

Required Software and Hardware

You need the following software and hardware to complete this tutorial:

  • LabVIEW 2010 Full or Professional Development System
  • LabVIEW 2010 C Generator
  • Code Composer Studio v4
  • TI's MSP430F5438 Experimenter Board (MSP-EXP430F5438)
  • A computer that meets the minimum system requirements of LabVIEW and Code Composer Studio.

Note: Refer to the LabVIEW C Generator Readme and LabVIEW Release Notes for information about system requirements and installation.

Back to Top

2. Creating a C Code Generation Project

Before you create a VI from which to generate C code, you create a LabVIEW project and a C Code Generation build specification in LabVIEW. Creating a C Code Generation build specification before you create the VI enables syntax checking while you develop in LabVIEW. Syntax checking ensures that the C Generator can create generic C code based on the design of the VI.

Complete the following steps to create a project and build specification.

  1. Launch LabVIEW.
  2. Select File»New Project to create a new LabVIEW project (*.lvproj) in LabVIEW.
  3. Right-click the untitled project in the Project Explorer window and select Save from the shortcut
    menu. Name and save the project.
  4. In the Project Explorer window, right-click Build Specifications under My Computer and select
    New»C Code Generation from the shortcut menu to create a C code generation build specification, as shown in the following figure. LabVIEW displays the C Code Generation Properties dialog box.

Figure 1: Creating a C Code Generation Project in LabVIEW

You use a C code generation build specification to specify how the C Generator generates C code for a VI.

  1. On the Information page of the C Code Generation Properties dialog box, rename the build specification.
  2. In the Destination directory text box, specify a location to save the generated C files.
  3. Click the OK button to close the build specification.
  4. LabVIEW displays a dialog box to notify you that you have not selected a VI to export. Click the Yes button to close the dialog box. You will add the VIs in a later step.
  5. Save the project.

Back to Top

3. Creating a VI for C Code Generation

After you create the project and C Code Generation build specification, you can develop a VI for C code generation. 

Tip: If you are new to LabVIEW, refer to the Getting Started with LabVIEW Web site for additional resources and example VIs.

In this tutorial, the top-level VI applies effects to an image represented by a 1D array. The top-level VI contains an enumerated input with which you can specify which effect to apply to the image. Three subVIs implement the effects—one subVI mirrors the image, a second subVI flips the image, and a third subVI negates the image. The top-level VI calls each subVI, as shown in the following figures.

Figure 2: Top-level VI and SubVIs

 Figure 3: Top-Level VI, ImageEffects.vi

 Figure 4: SubVI, MirrorImage.vi

Complete the following steps to add the VIs to the project.

  1. Download ImageEffects.vi, MirrorImage.vi, InvertImage.vi, and FlipImage.vi. This tutorial includes these VIs as attachments.
  2. Save the VIs to the same location you saved the LabVIEW project file (*.lvproj).
  3. Open the project in LabVIEW.
  4. In the Project Explorer window, right-click My Computer and select Add»File from the shortcut
    menu. You must include the VI you want to generate C code for under My Computer.
    You cannot generate C code from any other targets in the Project Explorer window.
  5. In the Select a File to Insert dialog box, navigate to the location you saved the VIs. Add all of the VIs to the project.
  6. Save the project.

Setting Up the Connector Pane

Figure 3 shows the inputs and outputs of the block diagram of the top-level VI. The C function parameters match these inputs and outputs. When you create a VI you want to export for C code generation, you must select a
pattern to use for the connector pane and assign all front panel controls and indicators to the connector pane terminals. If you do not set up the connector pane for the exported VI, you cannot configure parameters for the function prototype.

Figure 4: Icon vs. Connector Pane

Complete the following steps to set up the connector pane for the top-level VI.

  1. Open ImageEffects.vi.
  2. Right-click the icon in the upper right corner of the front panel window and select Show Connector from the shortcut menu to display the connector pane. The connector pane replaces the icon. 
  3. Click a terminal of the connector pane. The tool automatically changes to the Wiring tool, and the terminal turns black.
  4. Click the front panel control or indicator you want to assign to the terminal. A marquee highlights the object.
  5. Click an open space in the front panel window. The marquee disappears, and the terminal changes to the data type color of the control to indicate that you connected the terminal.
  6. Repeat steps 3 through 5 for each front panel control or indicator. 

Special Considerations

While this tutorial includes completed VIs, you should keep the following considerations in mind when you develop VIs for C code generation.

  • The C Generator cannot generate C code if the exported VI hierarchy contains unsupported data types, VIs, functions, or structures. As you develop the VI, account for differences in VIs developed to run as LabVIEW applications and VIs you develop for C code generation. Exported VIs and all VIs in their hierarchies support some block diagram objects differently from VIs running as LabVIEW applications. Refer to Developing VIs for C Code Generation topic in the LabVIEW Help. For a complete list of limitations and unsupported LabVIEW features. In LabVIEW, select Help»LabVIEW to display the LabVIEW Help.

Figure 5: Unsupported Features

  • Depending on the target on which you plan to deploy the generated C code, memory limitations might be a significant issue. For mobile devices such as smartphones, memory limitations are usually not an issue with the generated C code. However for embedded targets such as the MSP430 microcontroller, the amount of available memory might be limited. For example, the image effect VIs created for this tutorial were optimized for use with the MSP430 microcontroller. Figures 6 further demonstrates differences in memory usage. Figure 6 shows the block diagrams of two VIs that complete the same task. The first block diagram includes code that functions correctly on a PC. The second block diagram includes code that is optimize for memory usage.
The following block diagram mirrors a 2D array of Booleans. Operations on a 1D array that ultimately reflects a 2D object are easy to conceptualize when converted to a 2D object. However, these operations require more memory than is available on the MSP430 microcontroller.
The following block diagram uses a 1D array of integers to mirror a 2D array of Booleans. This method requires three For Loops instead of one For Loop and uses the Data Manipulation VIs. While this code is more complex, it does not exceed memory limitations for the MSP430 microcontroller.

 Figure 6: Optimized vs. Non-Optimized Code

Defining the Function Prototype

Use the build specification to specify which VI you want to export for C code generation and to define the function prototype that the C Generator generates for that VI.


Complete the following steps to define the function prototype.

  1. Double-click the C Code Generation build specification in the Project Explorer window to display the C Code Generation Properties dialog box.
  2. Select Source Files from the Category list in the C Code Generation Properties dialog box to display the Source Files page.
  3. From the Project Files tree, select the ImageEffects.vi and click the Add Item arrow button next to the Exported VI listbox to specify the VI you want to export for C code generation.
  4. LabVIEW automatically displays the Define VI Prototype dialog box when you specify a VI in the Exported VI listbox. Use the Define VI Prototype dialog box, as shown in Figure 7, to define the parameters of the function prototype that the C Generator creates from the exported VI. Notice that the text displayed in the Function Name text box matches the name of the VI you selected for export. Also notice that the parameters displayed in the Parameters list match the inputs and outputs of the exported VI.

Figure 7: Define VI Prototype Dialog Box

  1. Review the function prototype. After you define the function prototype, the Function Prototype text box displays a preview of the function prototype that the C Generator generates based on the current settings in the dialog box:

    void ImageEffects(uInt16 ImageArray[], int32 Columns, int32 Rows, Enum16 ImageOperation, uInt16 **ChangedArray, int32 len, int32 *len2)
  2. Click the OK button to close the Define VI Prototype dialog box. Do not close the C Code Generation Properties dialog box.

 

Back to Top

4. Setting C Code Generation Options

From a LabVIEW block diagram, the C Generator creates generic C code that you can compile on many targets. You must specify platform-specific settings for the target on which you want to use the generatedC code. You use the C Code Generation Properties dialog box to define these settings. 

Information Page

Use the Information page of the C Code Generation Properties dialog box to name the build specification, select the location to save the generated C files, and provide a description for the build specification.

Figure 8: Information Page

Source Files Page

You already used the Source Files page of the C Code Generation Properties dialog box to specify the VI for which you want to generate C code. You also can use the Always Included listbox to specify additional VIs that your VIs dynamically reference.

Figure 9: Source Files Page

For this tutorial, you should see the top-level VI and all subVIs listed on this page.

C Code Generation Settings Page

Use the C Code Generation Settings page of the C Code Generation Properties dialog box to change the C code generation settings and optimization settings for all VIs in the hierarchy of the exported VI.

Figure 11: C Code Generation Settings Page

You also can set endianess and alignment and enable various optimizations. The Endian format option specifies the byte order of data in the generated C code. Big endian specifies that the most-significant byte occupies the lowest memory address while little endian specifies that the least-significant byte occupies the lowest memory address. The Alignment option specifies the memory padding value to which the C Generator forces internal data structures. This value must match the alignment value for the double data type on the processor.

For this tutorial, set Endian format to Little endian format and Alignment to 4.

Source File Settings Page

Use the Source File Settings page of the C Code Generation Properties dialog box to set individual C code generation options for each VI in the hierarchy of the exported VI. You must select a VI from the Project Files tree to see the C code generation options. This page is useful if you have some VIs for which you want to enable features, such as Guard Code, and you do not want to enable this feature for all of your VIs.

Figure 12: Source File Settings

If you select From project for a property listed in the VI Settings textbox, the VI uses the setting you specified on the C Code Generation Settings page.

For this tutorial, use the default value of From project for all properties. 

Pre/Post Build Actions Page

Use the Pre/Post Build Actions page of the C Code Generation Properties dialog box to specify VIs for LabVIEW to execute before or after the build.

Figure 13: Pre/Post Build Actions

For this tutorial, use the default values on this page.

Back to Top

5. Generating Optimized C Code

After you configure C code generation options, you can generate C code for the VI. 

Complete the following steps to generate C code from the exported VI.

  1. Expand Build Specifications in the Project Explorer window.
  2. Right-click the build specification you created and select Build from the shortcut
    menu.
    Tip: You can check the syntax of the exported VI before you build by right-clicking the build specification and selecting Check Syntax from the shortcut menu.

    LabVIEW displays the Build status dialog box while it generates C code for the exported VI.
  1. Click the Explore button in the Build status dialog box to open the directory containing the generated C code. The C Generator generates the C code to the location you specified in the Destination directory option on the Information page of the C Code Generation Properties dialog box.
    Tip: To open the directory containing the generated C files, you also can right-click the C Code Generation build specification in the Project Explorer window and select Explore from the shortcut menu.

The C Generator generates the following files:

  • ImageEffects.c file, which contains the exported function of the top-level VI you selected for C code generation.
  • * .h file, which contains the library interface that specifies the function you defined in the build specification.
  •  *.c files, which contain an internal function that implements a subVI of the top-level VI.
  • LVForms.h, which contains declarations for the entry point functions for each VI.
  • LVFuncsUsed.h, which contains usage information to support source-level dead-code stripping.
  • LVGlobs.h, which contains declarations for global initialization functions and data type information used in an external application.
  • Makefile is an example makefile. After the C Generator generates Makefile, the C Generator does not overwrite this file if you regenerate the C code. If you want to generate a new example Makefile, you must delete the file before regenerating the C code.
  • dependencies.mk is the secondary makefile. The C Generator includes all LabVIEW settings, such the build specification name and C code generation option values, that are required to generate C code.

Refer to the LabVIEW Help for more information about generated files.

Back to Top

6. Using Code Composer Studio to Implement LabVIEW Generated C Code.

Code Composer Studio is a tool suite that you can use to develop and debug embedded applications. Code Composer Studio includes compilers for different Texas Instruments device families, a source code editor, a project build environment, a debugger, and many other features.

Creating a Code Composer Studio Project

After you generate C code in LabVIEW, you must create a Code Composer Studio (CCS) project to implement the exported functions.

Complete the following steps to create a new CCS project.

  1. Launch Code Composer Studio.
  2. Select File»New»CCS Project. In the new project dialog box, shown in Figure 14, name the project and select a location to save the project.

Figure 14: Name and Specify a Location for the Project.

  1. Click the Next button to configure the CCS project type. Select MSP430 as the project type and choose if the CCS project is configured for debug, release, or both modes, as shown in Figure 15.

Figure 15: Select Project Type

  1. Click the Next button to specify additional project settings. Specify if you want to reference existing CCS projects in the new CCS project, as shown in Figure 16.

Figure 16: Reference Other Projects

  1. Click the Next button to configure additional CCS project settings. Specify the project output type, the device variant you want to build for, the code generation tools to use, and any additional settings. For the purposes if this tutorial, use the settings shown in the Figure 17.

Figure 17: Configure Project Settings

File Inclusions

After you create the CCS project, you must configure it so that it links to and includes the necessary files.

Complete the following steps to configure file inclusions.

  1. Right-click the CCS project in the C/C++ Projects window and select Build Properties from the shortcut menu. Figure 18 shows the project build properties.

Figure 18: Project Build Properties

  1. Configure project macros. Select the Macros tab. 
  2. On the Macros tab, create two directory macros, one for the LabVIEW file path and one for the C code generation library source, as shown in Figure 19. Use the following paths:
    • LIBSRCPATH: C:\Program Files\National Instruments\LabVIEW 2010\CCodeGen\libsrc
    • LVPATH: C:\Program Files\National Instruments\LabVIEW 2010

Figure 19: Project Macros

  1. Setup the project includes. Select the Tool Setting tab.
  2. Select Include Options from the MSP430 Compiler tree.
  3. Add new directories to the #include search path, as shown in Figure 20. After you add the directories to the search path, they appear in the project view. Ensure you include the following search paths:
    • $(LVPATH)\CCodeGen\include\blockdiagram
    • $(LVPATH)\CCodeGen\include\comms
    • $(LVPATH)\CCodeGen\include\frontpanel
    • $(LVPATH)\CCodeGen\analysis\development\include
    • $(LVPATH)\CCodeGen\analysis\LV\source\include
    • $(LVPATH)\CCodeGen\include\platform\MSP430

Figure 20: MSP430 Compiler Include Options

Compiler/Linker Settings

After you configure the CCS project and ensure you include all the correct files, you can configure other settings, such as compiler optimizations and diagnostic options. Due to the memory intensive nature of array operations, you must change the heap and stack size. Select the Basic Options of the MSP430 Linker properties in the CCS project properties, as shown in Figure 21. Set the stack size to 400 and the heap size to 5000. These settings proved to provide enough memory to allow the array operations to run successfully.

Figure 21: Basic Linker Options

Setting Up the MSP430 Platform

If you try to browse to the labview\CCodeGen\include\platform\MSP430 directory you specified in a previous step, the directory does not exist. The platform directory contains subdirectories for different platforms for which you can compile. 

Complete the following steps to create and add files to the MSP430 directory.

  1. Create a new MSP430 directory as a subdirectory of labview\CCodeGen\include\platform directory.
  2. Copy the contents of the labview\CCodeGen\include\platform\win directory into the MSP430 directory. 

The MSP430 directory now contains two important files—LVDefs_Plat.h and LVSysIncludes.h.

LVDefs_plat.h contains basic constants and macros that the C Generator needs to generate C code. This file provides the mapping between generic C function calls that the C Generator generates and platform-specific, run-time functions. LVDefs_plat.h also contains platform definitions of data types for the compiler. The LVDefs_plat.h file copied from the win directory defines the data types and function calls correctly for a Win32 DLL created with the Microsoft Visual Studio compiler.

LVSysIncludes.h contains platform-specific header files, toolchain-specific header files, and standard C header files. Most files in the labview\CCodeGen directory include LVSysIncludes.h.

To build code for the MSP430, you must properly configure these files to align the data types and function calls that the compiler uses with the LabVIEW data types. To define data types, you must know the compiler definitions of those data types. To define the generic function calls, you must know how the target SDK implements these function calls.

After you setup the #include files, you must complete the following steps to add the source code to the CCS project. 

  1. Create a subdirectory in the CCS project directory to contain the .c files. 
  2. Open the Makefile that the C Generator created when you generated C code in LabVIEW.
  3. Copy all of the .c files referenced in the Makefile from the labview\CCodeGen\libsrc directory to the new  subdirectory in the CCS project.
  4. Add the generated C code to the CCS project. Create another subdirectory in the CCS project directory and copy all of the generated C files into the new subdirectory.

Developing C Code

After you setup the CCS project and include all necessary files, you must integrate the generated C code into an existing C program or create a new C program. Figure 22 shows the CCS project with the generated C code included and the exported function implemented in a .c file.

Figure 22: Project View

Building the Project and Deploying to the MSP430

After configuring the CCS project, you can build it and deploy it to the MSP430. Right-click the CCS project in the C/C++ Project window and select Build Project from the shortcut menu. After you build the CCS project, you can launch a new debugging session by clicking the Debug button in the Project Toolbar and selecting Debug Active Project, as shown in Figure 23.

Figure 23: Project Toolbar


Figure 24: Launching Debug Session


Figure 25: Loading the Program to the MSP430

After the debug session launches and the code loads to the MSP430, click the Run Mode button in the Debug Toolbar and select Run, as shown in Figure 26.

Figure 26: Debug Toolbar

Selecting Run starts the code on the MSP430. Figure 27 shows the original image and the manipulated image displayed on the LCD screen.

Figure 27: Image Operations

Note: Other product and company names mentioned herein are trademarks or trade names of their respective companies.

Back to Top

7. Related Links

Products and Services: LabVIEW

Products and Services: NI LabVIEW C Generator

Code Composer Studio IDE Overview from Texas Instruments

Getting Started with the NI LabVIEW C Generator

Getting Started with LabVIEW

Back to Top

Bookmark & Share



Ratings

Rate this document

Answered Your Question?
Yes No

Submit