Nios® II Software Developer Handbook

ID 683525
Date 8/28/2023
Public
Document Table of Contents

3.9. Creating a Software Package

This section shows how you can build a custom library into a BSP as a software package. The software package can be linked to any BSP through the BSP Editor.

This section contains an example illustrating the steps necessary to include any software package into a Nios II BSP.

To create and exercise the example software package, perform the following steps:

  1. Locate the ip directory in your Intel FPGA Complete Design Suite installation. For example, if the Intel FPGA Complete Design Suite version 14.1 is installed on the Windows operating system, the directory might be c:\altera\14.1\ip. Under the ip directory, create a directory for the software package. For simplicity, this section refers to this directory as <example package>.
  2. In <example package>, create a subdirectory named EXAMPLE_SW_PACKAGE. In <example package>/EXAMPLE_SW_PACKAGE, create two subdirectories named inc and lib.
  3. In <example package>/EXAMPLE_SW_PACKAGE/inc, create a new header file named example_sw_package.h containing the following code:
    /* Example Software Package */
    void example_sw_package(void);
  4. In <example package>/EXAMPLE_SW_PACKAGE/lib, create a new C source file named example_sw_package.c containing the following code:
    /* Example Software Package */
    #include <stdio.h>
    #include "..\inc\example_sw_package.h"
    
    void example_sw_package(void)
    {
       printf ("Example Software Package. \n");
    }
  5. In <example package>, create a new Tcl script file named example_sw_package_sw.tcl containing the following code:
    #
    # example_sw_package_sw.tcl
    #
    
    # Create a software package known as "example_sw_package"
    create_sw_package example_sw_package
    
    # The version of this software
    set_sw_property version 14.1
    
    # Location in generated BSP that sources should be copied into
    set_sw_property bsp_subdirectory Example_SW_Package
    
    #
    # Source file listings...
    #
    
    # C/C++ source files
    #add_sw_property c_source EXAMPLE_SW_PACKAGE/src/my_source.c
    
    # Include files
    add_sw_property include_source
    EXAMPLE_SW_PACKAGE/inc/example_sw_package.h
    
    # Lib files
    add_sw_property lib_source
    EXAMPLE_SW_PACKAGE/lib/libexample_sw_package_library.a
    
    # Include paths for headers which define the APIs for this package
    # to share w/ app & bsp
    # Include paths are relative to the location of this software
    # package tcl file
    
    add_sw_property include_directory EXAMPLE_SW_PACKAGE/inc
    
    # This driver supports HAL & UCOSII BSP (OS) types
    add_sw_property supported_bsp_type HAL
    add_sw_property supported_bsp_type UCOSII
    
    # Add example software package system.h setting to the BSP:
    add_sw_setting quoted_string system_h_define \
      example_sw_package_system_value EXAMPLE_SW_PACKAGE_SYSTEM_VALUE 1 \
      "Example software package system value"
    # End of file
  6. In the SBT for Eclipse, create a Nios II application and BSP project based on the Hello World template. Set the application project name to hello_example_sw_package.
  7. Create a new C file named hello_example_sw_package.c in the new application project containing the following code:
    /*
     * "Hello World" example.
     *
     * This example prints 'Hello from Nios II' to the STDOUT stream. It also 
     * tests inclusion of a user software package.
     */
    
    #include <stdio.h>
    #include "example_sw_package.h"
    
    int main()
    {
      printf("Hello from Nios II!\n");
      example_sw_package();
      return 0;
    }
  8. Delete hello_world.c from the hello_example_sw_package application project.
  9. In the File menu, point to New and click Nios II Library
  10. Set the project name to example_sw_package_library.
  11. For Location, browse to <example package>\EXAMPLE_SW_PACKAGE\lib
    Note: Building the library here is required, because the resulting .a is referenced here by example_sw_package_sw.tcl.
  12. Click Finish.
  13. Build the example_sw_package_library project to create the libexample_sw_package_library.a library archive file.
  14. Right-click the BSP project, point to Nios II, and click BSP Editor to open the BSP Editor.
  15. In the BSP Software Packages tab, find example_sw_package in the software package table, and enable it.
    If there are any errors in a software package's *_sw.tcl file, such as an incorrect path that causes a file to not be found, the software package does not appear in the BSP Editor.
  16. Click the Generate button to regenerate the BSP. On the File menu, click Save to save your changes to settings.bsp.
  17. In the File menu, click Exit to exit the BSP Editor.
  18. Build the hello_example_sw_package_bsp BSP project.
  19. Build the hello_example_sw_package application project.

    hello_example_sw_package.elf is ready to download and execute.