Nios II Software Developer’s Handbook
Contents
Chapter Revision Dates
About this Handbook
How to Contact Altera
Typographic Conventions
Section I. Nios II Software Development
1. Overview
Introduction
What’s New
Getting Started
Nios II Software Development Environment
Nios II Programs
Application Project
Library Project
BSP Project
Hardware Abstraction Layer (HAL)
Newlib C Standard Library
Device Drivers
Optional Software Packages
Optional Real-Time Operating System (RTOS)
Design Flows for Creating Nios II Programs
The Nios II IDE Design Flow
The Nios II Software Build Tools Design Flow
Design Flow Tools
Additional EDS Support
GNU Tool Chain
Instruction Set Simulator
Example Designs
Third-Party Support
Migrating from the First- Generation Nios Processor
Further Nios II Information
Referenced Documents
Document Revision History
2. Nios II Integrated Development Environment
Introduction
The Nios II IDE Workbench
Perspectives, Editors, and Views
EDS Design Flows and the IDE
IDE-Managed Projects and Makefiles
User-Managed Projects and Makefiles
Creating a New IDE-Managed Project
Building and Managing Projects
Running and Debugging Programs
Importing User-Managed Projects
Road Map
Import a User-Managed C/C++ Application
Import a Supporting Project
Importing a User-Managed BSP
Importing a User-Managed Library
Importing a C/C++ Source Project
Debug a User-Managed C/C++ Application
Edit User-Managed C/C++ Application Code
Programming Flash
Help System
Referenced Documents
Document Revision History
3. Introduction to the Nios II Software Build Tools
Introduction
Advantages of the Nios II Software Build Tools
Outline of the Nios II Software Build Tools
The Parts of the Software Build Tools
What the Build Tools Create
Makefiles and the Software Build Tools
Getting Started
What You Need
Creating hello_world for a Nios Development Board
Running hello_world on a Nios Development Board
Debugging hello_world
Import the hello_world Application
Set Up a Debug Configuration
Download Executable Code and Start the Debugger
Next Steps
Creating a Script
Scripting Basics
Nios II Scripting Examples
Referenced Documents
Document Revision History
4. Using the Nios II Software Build Tools
Introduction
Advantages of the Software Build Tools Design Flow
Road Map to the Nios II Software Build Tools
Software Build Process
Generators, Utilities, and Scripts
Generators
Utilities
Scripts
Using Nios II Example Design Scripts
create-this-bsp
create-this-app
Finding create-this-app and create-this-bsp
User-Managed Makefiles
Makefile Targets
Nios II C2H Makefiles
Applications and Libraries
Board Support Packages
Overview of BSP Creation
Generated and Copied Files
Coordinating with Hardware Changes
Altera HAL BSP
HAL BSP Files and Folders
settings.bsp
summary.html
Makefile
public.mk
mem_init.mk
alt_sys_init.c
system.h
linker.h
linker.x
memory.gdb
HAL Directory
drivers Directory
obj Directory
libhal_bsp.a Library
Micrium MicroC/OS-II BSP
Common BSP Tasks
Adding the Nios II Software Build Tools to Your Tool Flow
Using Version Control
Copying, Moving, or Renaming a BSP
Handing Off a BSP
Running a Nios II System with ModelSim
Linking and Locating
Creating Memory Initialization Files
Modifying Linker Memory Regions
Creating a Custom Linker Section
Dividing a Linker Region to Create a New Region and Section
Changing the Default Linker Memory Region
Changing a Linker Section Mapping
Other BSP Tasks
Creating a BSP for a Nios Development Board
Querying Settings
Managing Device Drivers
Creating a Custom Version of newlib
Controlling the stdio Device
Porting Nios II IDE Projects
Applications
System Libraries
User Libraries
Using the Nios II C2H Compiler
Details of BSP Creation
Tcl Scripts for Board Support Package Settings
BSP Settings File Creation
Modifying the BSP
Recreate the BSP Using a Tcl Script
Run nios2-bsp
Run nios2-bsp-update-settings and nios2-bsp-generate-files
Coordinating with SOPC Builder System Changes
Specifying BSP Defaults
Top Level Script for BSP Defaults
Specifying the Default stdio Device
Specifying the Default System Timer
Specifying the Default Memory Map
Specifying Default Bootloader Parameters
Invoking Procedures in the Default Tcl Script
Device Drivers and Software Packages
Assumptions and Requirements
The Nios II BSP Generator Flow
File Names and Locations
Example
Driver and Software Package Tcl Script Creation
Tcl Command Walkthrough for a Typical Driver or Software Package
Creating and Naming the Driver or Package
Identifying the Hardware Component Class
Setting the BSP Type
Specifying an Operating System
Specifying Source Files
Specifying a Subdirectory
Enabling Software Initialization
Adding Include Paths
Version Compatibility
Creating Settings for Device Drivers and Software Packages
Data Types
Setting Destination Files
Setting Display Name
Setting Generation Name
Setting Default Value
Setting Description
Setting Creation Example
Porting Advanced Nios II IDE Projects
Custom Component Device Drivers
Precompiled Libraries
Non-HAL Device Drivers
Boot Configurations
Boot from Flash Configuration
Boot from Monitor Configuration
Run from Initialized Memory Configuration
Run-time Configurable Reset Configuration
Advanced Exceptions
Software Exception Handler
Instruction-Related Exceptions
The Instruction- Related Exception Handler
Writing an Instruction-Related Exception Handler
Registering an Instruction-Related Exception Handler
Removing an Instruction-Related Exception Handler
Restrictions
Referenced Documents
Document Revision History
Section II. The Hardware Abstraction Layer
5. Overview of the Hardware Abstraction Layer
Introduction
Getting Started
HAL Architecture
Services
Applications vs. Drivers
Generic Device Models
Device Model Classes
Benefits to Application Developers
Benefits to Device Driver Developers
C Standard Library-newlib
Supported Peripherals
Referenced Documents
Document Revision History
6. Developing Programs Using the Hardware Abstraction Layer
Introduction
Nios II Design Flows
HAL BSP Settings
The Nios II Project Structure
The system.h System Description File
Data Widths and the HAL Type Definitions
UNIX-Style Interface
File System
Using Character- Mode Devices
Standard Input, Standard Output and Standard Error
General Access to Character Mode Devices
C++ Streams
/dev/null
Lightweight Character-Mode I/O
Using File Subsystems
Using Timer Devices
System Clock Driver
Alarms
Timestamp Driver
Using Flash Devices
Simple Flash Access
Block Erasure or Corruption
Fine-Grained Flash Access
Using DMA Devices
DMA Transmit Channels
DMA Receive Channels
Memory-to-Memory DMA Transactions
Reducing Code Footprint
Enable Compiler Optimizations
Use Reduced Device Drivers
Reduce the File Descriptor Pool
Use /dev/null
Use a Smaller File I/O Library
Use the Small newlib C Library
Use UNIX-Style File I/O
Emulate ANSI C Functions
Use the Lightweight Device Driver API
Use the Minimal Character-Mode API
alt_printf()
alt_putchar()
alt_putstr()
alt_getchar()
Eliminate Unused Device Drivers
Eliminate Unneeded Exit Code
Eliminate Clean Exit
Eliminate All Exit Code
Turn off C++ Support
Boot Sequence and Entry Point
Hosted vs. Free-Standing Applications
Boot Sequence for HAL-Based Programs
Customizing the Boot Sequence
Memory Usage
Memory Sections
Assigning Code and Data to Memory Partitions
Simple Placement Options
Advanced Placement Options
Placement of the Heap and Stack
Global Pointer Register
Boot Modes
Paths to HAL Files
IDE-Managed Projects
Finding HAL Files
Overriding HAL Functions
User-Managed Projects
Finding HAL Files
Overriding HAL Functions
Referenced Documents
Document Revision History
7. Developing Device Drivers for the Hardware Abstraction Layer
Introduction
Integration into the HAL API
Peripheral-Specific API
Before You Begin
Development Flow for Creating Device Drivers
SOPC Builder Concepts
The Relationship between system.h and SOPC Builder
Using SOPC Builder for Optimal Hardware Configuration
Components, Devices and Peripherals
Accessing Hardware
Creating Drivers for HAL Device Classes
Character-Mode Device Drivers
Create a Device Instance
Register a Character Device
File Subsystem Drivers
Create a Device Instance
Register a File Subsystem Device
Timer Device Drivers
System Clock Driver
Timestamp Driver
Create a Flash Driver
Register a Flash Device
DMA Device Drivers
DMA Transmit Channel
DMA Receive Channel
Ethernet Device Drivers
Provide the NicheStack Hardware Interface Routines
Provide *INSTANCE and *INIT Macros
Provide a Software Initialization Function
Integrating a Device Driver into the HAL
Design Flows
Directory Structure for HAL Devices
Device Driver Files for the HAL
A Device’s HAL Header File and alt_sys_init.c
Device Driver Source Code
Source Code Discovery in the IDE Design Flow
Source Code Discovery in the Build Tools Design Flow
Reducing Code Footprint
Provide Reduced Footprint Drivers
Support the Lightweight Device Driver API
Namespace Allocation
Overriding the Default Device Drivers
Referenced Documents
Document Revision History
Section III. Advanced Programming Topics
8. Exception Handling
Introduction
Nios II Exceptions Overview
Exception Handling Concepts
How the Hardware Works
ISRs
HAL API for ISRs
Writing an ISR
Restricted Environment
Registering an ISR
Enabling and Disabling ISRs
C Example
Example: An ISR to Service a Button PIO IRQ
Example: Registering the Button PIO ISR with the HAL
ISR Performance Data
Improving ISR Performance
Software Performance Improvements
Move Lengthy Processing to Application Context
Move Lengthy Processes to Hardware
Increase Buffer Size
Use Double Buffering
Keep Interrupts Enabled
Use Fast Memory
Use Nested ISRs
Use Compiler Optimization
Hardware Performance Improvements
Add Fast Memory
Add a DMA Controller
Place the Exception Handler Address in Fast Memory
Use a Fast Nios II Core
Select Interrupt Priorities
Use the Interrupt Vector Custom Instruction
Debugging ISRs
Summary of Guidelines for Writing ISRs
HAL Exception Handler Implementation
Exception Handler Structure
Top-Level Exception Handler
Hardware Interrupt Handler
Software Exception Handler
Unimplemented Instructions
When to Use the Unimplemented Instruction Handler
Using the Unimplemented Instruction Handler
Software Trap Handling
Other Exceptions
Invalid Instructions
HAL Exception Handler Files
Referenced Documents
Document Revision History
9. Cache and Tightly-Coupled Memory
Introduction
Nios II Cache Implementation
Example: An excerpt from system.h that defines the Cache Structure
HAL API Functions for Managing Cache
Further Information
Initializing Cache after Reset
Example: Assembly code to initialize the instruction cache
Example: Assembly code to initialize the data cache
For HAL System Library Users
Writing Device Drivers
For HAL System Library Users
Writing Program Loaders or Self- Modifying Code
Example: Assembly Code That Writes a New Instruction to Memory
For Users of the HAL System Library
Managing Cache in Multi- Master/Multi- CPU Systems
Bit-31 Cache Bypass
For HAL System Library Users
Tightly-Coupled Memory
Referenced Documents
Document Revision History
10. MicroC/OS-II Real-Time Operating System
Introduction
Overview
Further Information
Licensing
Other RTOS Providers
The Nios II Implementation of MicroC/OS-II
MicroC/OS-II Architecture
MicroC/OS-II Thread-Aware Debugging
MicroC/OS-II Device Drivers
Thread-Safe HAL Drivers
The newlib ANSI C Standard Library
Interrupt Service Routines for MicroC/OS-II
Implementing MicroC/OS-II Projects for the Nios II Processor
MicroC/OS-II General Options
Event Flags Settings
Mutex Settings
Semaphores Settings
Mailboxes Settings
Queues Settings
Memory Management Settings
Miscellaneous Settings
Task Management Settings
Time Management Settings
Referenced Documents
Document Revision History
11. Ethernet and the NicheStack TCP/IP Stack - Nios II Edition
Overview
Prerequisites
Introduction
The NicheStack TCP/IP Stack Files and Directories
Licensing
Other TCP/IP Stack Providers
Using the NicheStack TCP/ IP Stack
Nios II System Requirements
The NicheStack TCP/IP Stack Tasks
Initializing the Stack
alt_iniche_init()
netmain()
iniche_net_ready
get_mac_addr() and get_ip_addr()
Calling the Sockets Interface
Configuring the NicheStack TCP/ IP Stack in the Nios II IDE
NicheStack TCP/IP Stack General Settings
IP Options
TCP Options
Further Information
Known Limitations
Referenced Documents
Document Revision History
Section IV. Appendices
12. HAL API Reference
Introduction
HAL API Functions
_exit()
_rename()
alt_alarm_start()
alt_alarm_stop()
alt_dcache_flush()
alt_dcache_flush_all()
alt_dev_reg()
alt_dma_rxchan_close()
alt_dma_rxchan_depth()
alt_dma_rxchan_ioctl()
alt_dma_rxchan_open()
alt_dma_rxchan_prepare()
alt_dma_rxchan_reg()
alt_dma_txchan_close()
alt_dma_txchan_ioctl()
alt_dma_txchan_open()
alt_dma_txchan_reg()
alt_dma_txchan_send()
alt_dma_txchan_space()
alt_erase_flash_block()
alt_exception_cause_generated_bad_addr()
alt_flash_close_dev()
alt_flash_open_dev()
alt_fs_reg()
alt_get_flash_info()
alt_icache_flush()
alt_icache_flush_all()
alt_instruction_exception_register()
alt_irq_disable()
alt_irq_disable_all()
alt_irq_enable()
alt_irq_enable_all()
alt_irq_enabled()
alt_irq_register()
alt_llist_insert()
alt_llist_remove()
alt_load_section()
alt_nticks()
alt_read_flash()
alt_remap_cached()
alt_remap_uncached()
alt_sysclk_init()
alt_tick()
alt_ticks_per_second()
alt_timestamp()
alt_timestamp_freq()
alt_timestamp_start()
alt_uncached_free()
alt_uncached_malloc()
alt_write_flash()
alt_write_flash_block()
close()
execve()
fcntl()
fork()
fstat()
getpid()
gettimeofday()
ioctl()
isatty()
kill()
link()
lseek()
open()
read()
sbrk()
settimeofday()
stat()
times()
unlink()
usleep()
wait()
write()
Standard Types
Referenced Documents
Document Revision History
13. Altera-Provided Development Tools
Introduction
The Nios II IDE Tools
Altera Nios II Build Tools
Nios II Software Build Tools
File Format Conversion Tools
Other Command-Line Tools
Nios II IDE Command-Line Tools
GNU Compiler Tool Chain
GNU Tool Chain
Libraries and Embedded Software Packages
Example Designs
Referenced Documents
Document Revision History
14. Nios II Software Build Tools Reference
Introduction
Nios II Software Build Tools Utilities
Logging Levels
Setting Formats
Utility Summary
nios2-app-generate-makefile
Usage
Options
Description
nios2-bsp-create-settings
Usage
Options
Description
Example
nios2-bsp-generate-files
Usage
Options
Description
nios2-bsp-query-settings
Usage
Options
Description
nios2-bsp-update-settings
Usage
Options
Description
nios2-lib-generate-makefile
Usage
Options
Description
nios2-c2h-generate-makefile
Usage
Options
Description
Example
nios2-bsp
Usage
Options
Description
Settings
Overview of BSP Settings
Overview of Component and Driver Settings
Altera Host-Based File System Settings
Altera Read-Only Zip File System Settings
Altera NicheStack® TCP/IP - Nios II Edition Stack Settings
Altera Avalon-MM JTAG UART Driver Settings
Altera Avalon-MM UART Driver Settings
Settings Reference
hal.sys_clk_timer
hal.timestamp_timer
hal.max_file_descriptors
ucosii.os_max_tasks
ucosii.os_lowest_prio
ucosii.os_thread_safe_newlib
ucosii.miscellaneous.os_arg_chk_en
ucosii.miscellaneous.os_cpu_hooks_en
ucosii.miscellaneous.os_debug_en
ucosii.miscellaneous.os_sched_lock_en
ucosii.miscellaneous.os_task_stat_en
ucosii.miscellaneous.os_task_stat_stk_chk_en
ucosii.miscellaneous.os_tick_step_en
ucosii.miscellaneous.os_event_name_size
ucosii.miscellaneous.os_max_events
ucosii.miscellaneous.os_task_idle_stk_size
ucosii.miscellaneous.os_task_stat_stk_size
ucosii.task.os_task_change_prio_en
ucosii.task.os_task_create_en
ucosii.task.os_task_create_ext_en
ucosii.task.os_task_del_en
ucosii.task.os_task_name_size
ucosii.task.os_task_profile_en
ucosii.task.os_task_query_en
ucosii.task.os_task_suspend_en
ucosii.task.os_task_sw_hook_en
ucosii.time.os_time_tick_hook_en
ucosii.time.os_time_dly_resume_en
ucosii.time.os_time_dly_hmsm_en
ucosii.time.os_time_get_set_en
ucosii.os_flag_en
ucosii.event_flag.os_flag_wait_clr_en
ucosii.event_flag.os_flag_accept_en
ucosii.event_flag.os_flag_del_en
ucosii.event_flag.os_flag_query_en
ucosii.event_flag.os_flag_name_size
ucosii.event_flag.os_flags_nbits
ucosii.event_flag.os_max_flags
ucosii.os_mutex_en
ucosii.mutex.os_mutex_accept_en
ucosii.mutex.os_mutex_del_en
ucosii.mutex.os_mutex_query_en
ucosii.os_sem_en
ucosii.semaphore.os_sem_accept_en
ucosii.semaphore.os_sem_set_en
ucosii.semaphore.os_sem_del_en
ucosii.semaphore.os_sem_query_en
ucosii.os_mbox_en
ucosii.mailbox.os_mbox_accept_en
ucosii.mailbox.os_mbox_del_en
ucosii.mailbox.os_mbox_post_en
ucosii.mailbox.os_mbox_post_opt_en
ucosii.mailbox.os_mbox_query_en
ucosii.os_q_en
ucosii.queue.os_q_accept_en
ucosii.queue.os_q_del_en
ucosii.queue.os_q_flush_en
ucosii.queue.os_q_post_en
ucosii.queue.os_q_post_front_en
ucosii.queue.os_q_post_opt_en
ucosii.queue.os_q_query_en
ucosii.queue.os_max_qs
ucosii.os_mem_en
ucosii.memory.os_mem_query_en
ucosii.memory.os_mem_name_size
ucosii.memory.os_max_mem_part
ucosii.os_tmr_en
ucosii.timer.os_task_tmr_stk_size
ucosii.timer.os_task_tmr_prio
ucosii.timer.os_tmr_cfg_max
ucosii.timer.os_tmr_cfg_name_size
ucosii.timer.os_tmr_cfg_ticks_per_sec
ucosii.timer.os_tmr_cfg_wheel_size
altera_avalon_jtag_uart_driver.enable_small_driver
altera_avalon_uart_driver.enable_small_driver
altera_avalon_uart_driver.enable_ioctl
altera_iniche.iniche_default_if
altera_iniche.enable_dhcp_client
altera_iniche.enable_ip_fragments
altera_iniche.enable_include_tcp
altera_iniche.enable_tcp_zerocopy
altera_iniche.enable_net_stats
altera_ro_zipfs.ro_zipfs_name
altera_ro_zipfs.ro_zipfs_offset
altera_ro_zipfs.ro_zipfs_base
altera_hostfs.hostfs_name
hal.linker.exception_stack_memory_region_name
hal.linker.allow_code_at_reset
hal.linker.enable_alt_load
hal.linker.enable_alt_load_copy_rwdata
hal.linker.enable_alt_load_copy_rodata
hal.linker.enable_alt_load_copy_exceptions
hal.linker.enable_exception_stack
hal.linker.exception_stack_size
hal.make.build_pre_process
hal.make.ar_pre_process
hal.make.bsp_cflags_defined_symbols
hal.make.ar_post_process
hal.make.as
hal.make.build_post_process
hal.make.bsp_cflags_debug
hal.make.ar
hal.make.rm
hal.make.cxx_pre_process
hal.make.bsp_cflags_warnings
hal.make.bsp_arflags
hal.make.bsp_cflags_optimization
hal.make.as_post_process
hal.make.cc_pre_process
hal.make.bsp_asflags
hal.make.as_pre_process
hal.make.bsp_cflags_undefined_symbols
hal.make.cc_post_process
hal.make.cxx_post_process
hal.make.cc
hal.make.bsp_cxx_flags
hal.make.bsp_inc_dirs
hal.make.cxx
hal.make.bsp_cflags_user_flags
hal.enable_exit
hal.enable_small_c_library
hal.enable_clean_exit
hal.enable_runtime_stack_checking
hal.enable_gprof
hal.enable_c_plus_plus
hal.enable_reduced_device_drivers
hal.enable_lightweight_device_driver_api
hal.enable_instruction_related_exceptions_api
hal.enable_mul_div_emulation
hal.enable_sim_optimize
hal.enable_sopc_sysid_check
hal.custom_newlib_flags
hal.stdin
hal.stdout
hal.stderr
hal.log_port
Tcl Commands for BSP Settings
add_memory_region
Usage
Options
Description
Example
add_section_mapping
Usage
Options
Description
Example
are_same_resource
Usage
Options
Description
delete_memory_region
Usage
Options
Description
delete_section_mapping
Usage
Options
Description
Example
disable_sw_package
Usage
Options
Description
enable_sw_package
Usage
Options
Description
Example
get_addr_span
Usage
Options
Description
Example
get_available_drivers
Usage
Options
Description
Example
get_available_sw_packages
Usage
Options
Description
Example
get_base_addr
Usage
Options
Description
Example
get_current_memory_regions
Usage
Options
Description
Example
get_current_section_mappings
Usage
Options
Description
Example
get_default_memory_regions
Usage
Options
Description
Example
get_driver
Usage
Options
Description
Example
get_enabled_sw_packages
Usage
Options
Description
Example
get_exception_offset
Usage
Options
Description
Example
get_exception_slave_desc
Usage
Options
Description
Example
get_fast_tlb_miss_exception_offset
Usage
Options
Description
Example
get_fast_tlb_miss_exception_slave_desc
Usage
Options
Description
Example
get_irq_number
Usage
Options
Description
get_memory_region
Usage
Options
Description
Example
get_module_class_name
Usage
Options
Description
Example
get_module_name
Usage
Options
Description
Example
get_module_parameter_value
Usage
Options
Description
get_reset_offset
Usage
Options
Description
Example
get_reset_slave_desc
Usage
Options
Description
Example
get_section_mapping
Usage
Options
Description
Example
get_setting
Usage
Options
Description
Example
get_setting_desc
Usage
Options
Description
Example
get_slave_descs
Usage
Options
Description
Example
is_char_device
Usage
Options
Description
Example
is_connected_to_data_master
Usage
Options
Description
is_connected_to_instruction_master
Usage
Options
Description
is_flash
Usage
Options
Description
is_memory_device
Usage
Options
Description
Example
is_non_volatile_storage
Usage
Options
Description
log_debug
Usage
Options
Description
log_default
Usage
Options
Description
Example
log_error
Usage
Options
Description
log_verbose
Usage
Options
Description
set_driver
Usage
Options
Description
Example
set_setting
Usage
Options
Description
Example
Tcl Commands for Drivers and Packages
add_sw_property
Usage
Options
Description
add_sw_setting
Usage
Options
Description
create_driver
Usage
Options
Description
create_sw_package
Usage
Options
Description
set_sw_property
Usage
Options
Description
Path Names
Command Arguments
Object File Directory Tree
Referenced Documents
Document Revision History
15. Read-Only Zip File System
Introduction
Using the Zip File System in a Project
Preparing the Zip File
Programming the Zip File to Flash
Referenced Documents
Document Revision History