Embedded Design Handbook

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

4.2.3.2.3. HAL Initialization

As for any other C program, the first part of the HAL's initialization is implemented by the Nios® II processor's crt0.S routine. For more information, see “crt0 Initialization”. After crt0.S completes the C run-time initialization, it calls the HAL alt_main() function, which initializes the HAL BSP run-time library and subsystems.

The HAL alt_main() function performs the following steps:

  1. Initializes interrupts—Sets up interrupt support for the Nios® II processor (with the alt_irq_init() function).
  2. Starts MicroC/OS-II—Starts the MicroC/OS-II RTOS, if this RTOS is configured to run (with the ALT_OS_INIT and ALT_SEM_CREATE functions). For additional information about MicroC/OS-II use and initialization, refer to “Selecting the Operating System (HAL versus MicroC/OS-II RTOS)”.
  3. Initializes device drivers—Initializes device drivers (with the alt_sys_init() function). The Nios® II Software Build Tools automatically find all peripherals supported by the HAL, and automatically insert a call to a device configuration function for each peripheral in the alt_sys_init() code. To override this behavior, you can disable a device driver with the Nios® II BSP Editor, in the Drivers tab.

    For information about enabling and disabling device drivers, refer to “Using the BSP Editor” in the Getting Started with the Graphical User Interface chapter of the Nios® II Gen2 Software Developer's Handbook.

    To disable a driver from the Nios® II Command Shell, use the following option to the nios2-bsp script:

    --cmd set_driver <peripheral_name> none

    For information about removing a device configuration function, and other methods of reducing the BSP library size, refer to Table 54.

  4. Configures stdio functions—Initializes stdio services for stdin, stderr, and stdout. These services enable the application to use the GNU newlib stdio functions and maps the file pointers to supported character devices.For more information about configuring the stdio services, refer to “Character Mode Devices”.
  5. Initializes C++ CTORS and DTORS—Handles initialization of C++ constructor and destructor functions. These function calls are necessary if your application is written in the C++ programming language. By default, the HAL configuration mechanism enables support for the C++ programming language. Disabling this feature reduces your application's code footprint, as noted in “Software Application Optimization”.

    The Nios® II C++ language support depends on the GCC tool chain. The Nios® II GCC 4 C++ tool chain supports polymorphism, friendship and inheritance, multiple inheritance, virtual base classes, run-time type information (typeid), the mutable type qualifier, namespaces, templates, new-and-delete style dynamic memory allocation, operator overloading, and the Standard Template Library (STL). Exceptions and new-style dynamic casts are not supported.

  6. Calls main()—Calls function main(), or application program. Most applications are constructed using a main() function declaration, and begin execution at this function.

    If you use a BSP that is not based on the HAL and need to initialize it after the crt0.S routine runs, define your own alt_main() function. For an example, see the main() and alt_main() functions in the hello_alt_main.c file at < Nios® II EDS install dir> \examples\software\hello_alt_main.

    After you generate your BSP project, the alt_main.c source file is located in the HAL/src directory.