Nios® II Software Developer's Handbook

Transcript

1 ® Nios II Software Developer's Handbook ® ® Quartus Updated for Intel Prime Design Suite: 19.1 Subscribe NII-SDH | 2019.04.30 Send Feedback Latest document on the web: PDF | HTML

2 Contents Contents ® II Software Developer's Handbook Revision History...12 1. Nios ® 1.1. Overview of Nios II Embedded Development Revision History... 13 1.2. Getting Started with the Graphical User Interface Revision History...13 1.3. Getting Started from the Command Line Revision History...13 1.4. Nios II Software Build Tools Revision History...14 1.5. Overview of the Hardware Abstraction Layer Revision History... 14 1.6. Developing Programs Using the Hardware Abstraction Layer Revision History...14 1.7. Developing Device Drivers for the Hardware Abstraction Layer Revision History...15 1.8. Exception Handling Revision History... 15 1.9. Cache and Tightly-Coupled Memory Revision History...15 1.10. MicroC/OS-II Real-Time Operating System Revision History... 15 1.11. Ethernet and the NicheStack TCP/IP Stack - Nios II Edition Revision History...16 1.12. Read-Only Zip File System Revision History... 16 1.13. Publishing Component Information to Embedded Software Revision History... 16 1.14. HAL API Reference Revision History...17 1.15. Nios II Software Build Tools Reference Revision History...17 2. Overview of Nios II Embedded Development...18 2.1. Prerequisites for Understanding the Nios II Embedded Design Suite...18 2.2. Finding Nios II EDS Files...18 2.3. Nios II Software Development Environment...19 2.4. Nios II EDS Development Flows...19 2.4.1. Nios II SBT Development Flow...19 2.5. Nios II Programs...20 2.5.1. Makefiles and the SBT... 20 2.5.2. Nios II Software Project Types...21 2.6. Intel FPGA Software Packages for Embedded Systems... 22 2.7. Nios II Embedded Design Examples...22 2.7.1. Hardware Examples... 22 2.7.2. Software Examples... 23 2.8. Third-Party Embedded Tools Support...23 2.9. Additional Nios II Information...23 3. Getting Started with the Graphical User Interface... 25 3.1. Installing Eclipse IDE into Nios II EDS...25 3.2. Getting Started with Nios II Software in Eclipse... 26 3.2.1. The Nios II SBT for Eclipse Workbench...26 3.2.2. Creating a Project...27 3.2.3. Navigating the Project... 29 3.2.4. Building the Project...29 3.2.5. Configuring the FPGA... 30 3.2.6. Running the Project on Nios II Hardware...30 3.2.7. Debugging the Project on Nios II Hardware... 31 3.2.8. Creating a Simple BSP...37 3.3. Makefiles and the Nios II SBT for Eclipse...37 3.3.1. Eclipse Source Management...38 3.3.2. User Source Management... 39 3.3.3. BSP Source Management... 40 ® Nios II Software Developer's Handbook Send Feedback 2

3 Contents 3.4. Using the BSP Editor... 40 3.4.1. Tcl Scripting and the Nios II BSP Editor...41 3.4.2. Starting the Nios II BSP Editor...41 3.4.3. The Nios II BSP Editor Screen Layout...41 3.4.4. The Command Area...41 3.4.5. The Console Area...46 3.4.6. Exporting a Tcl Script...46 3.4.7. Creating a New BSP... 47 3.4.8. BSP Validation Errors...47 3.5. Run Configurations in the SBT for Eclipse...48 3.5.1. Opening the Run Configuration Dialog Box...48 3.5.2. The Project Tab...48 3.5.3. The Target Connection Tab... 49 3.5.4. The Debugger Tab...49 3.6. Optimizing Project Build Time...49 3.7. Importing a Command-Line Project...49 3.7.1. Nios II Command-Line Projects...50 3.7.2. Importing through the Import Wizard...50 3.7.3. Road Map... 50 3.7.4. Import a Command-Line C/C++ Application... 51 3.7.5. Import a Supporting Project...51 3.7.6. User-Managed Source Files... 52 3.8. Packaging a Library for Reuse...52 3.8.1. Creating the User Library...52 3.8.2. Using the Library... 53 3.9. Creating a Software Package...53 3.10. Programming Flash in Intel FPGA Embedded Systems... 56 3.10.1. Starting the Flash Programmer... 56 3.10.2. Creating a Flash Programmer Settings File...57 3.10.3. The Flash Programmer Screen Layout... 57 3.10.4. The Command Area...57 3.10.5. The Console Area...58 3.10.6. Saving a Flash Programmer Settings File...58 3.10.7. Flash Programmer Options... 58 3.11. Creating Memory Initialization Files... 59 3.11.1. Generate Memory Initialization Files... 59 3.11.2. Generate Memory Initialization Files by the Legacy Method...60 3.11.3. Memory Initialization Files for User-Defined Memories...60 3.12. Running a Nios II System with ModelSim...62 3.12.1. Using ModelSim with an SOPC Builder-Generated System...62 3.12.2. Using ModelSim with a Platform Designer-Generated System...62 3.13. Eclipse Usage Notes... 65 3.13.1. Configuring Application and Library Properties...65 3.13.2. Configuring BSP Properties...65 3.13.3. Exclude from Build Not Supported... 66 3.13.4. Selecting the Correct Launch Configuration Type... 66 3.13.5. Target Connection Options...66 3.13.6. Renaming Nios II Projects... 66 3.13.7. Running Shell Scripts from the SBT for Eclipse... 67 3.13.8. Must Use Nios II Build Configuration...67 3.13.9. CDT Limitations... 68 ® Nios II Software Developer's Handbook Send Feedback 3

4 Contents 3.13.10. Enhancements for Build Configurations in SBT and SBT for Eclipse... 69 4. Getting Started from the Command Line...72 4.1. Advantages of Command-Line Software Development...72 4.2. Outline of the Nios II SBT Command-Line Interface...72 4.2.1. Utilities...73 4.2.2. Scripts...73 4.2.3. Tcl Commands...76 4.2.4. Tcl Scripts...76 4.2.5. The Nios II Command Shell...76 4.3. Getting Started in the SBT Command Line...77 4.3.1. Prerequisites...77 4.3.2. Creating Hello_World for an Intel FPGA Development Board...78 4.3.3. Running Hello_World on an Intel FPGA Development Board...78 4.3.4. Debugging hello_world... 79 4.4. Software Build Tools Scripting Basics...80 4.4.1. Creating a BSP with a Script...81 4.4.2. Creating an Application Project with a Script...84 4.5. Running make...84 4.5.1. Creating Memory Initialization Files...85 5. Nios II Software Build Tools... 86 5.1. Road Map for the SBT...87 5.1.1. What the Build Tools Create...87 5.1.2. Comparing the Command Line with Eclipse...87 5.2. Makefiles...87 5.2.1. Modifying Makefiles...88 5.2.2. Makefile Targets...88 5.3. Nios II Embedded Software Projects...89 5.3.1. Applications and Libraries...89 5.3.2. Board Support Packages... 90 5.3.3. Software Build Process... 92 5.4. Common BSP Tasks...93 5.4.1. Adding the Nios II SBT to Your Tool Flow...93 5.4.2. Linking and Locating...95 5.4.3. Other BSP Tasks...101 5.5. Details of BSP Creation...105 5.5.1. BSP Settings File Creation... 107 5.5.2. Generated and Copied Files... 107 5.5.3. HAL BSP Files and Folders... 108 5.5.4. Linker Map Validation...112 5.6. Tcl Scripts for BSP Settings...112 5.6.1. Calling a Custom BSP Tcl Script...112 5.7. Revising Your BSP...115 5.7.1. Rebuilding Your BSP... 115 5.7.2. Regenerating Your BSP... 116 5.7.3. Updating Your BSP... 118 5.7.4. Recreating Your BSP...119 5.8. Specifying BSP Defaults...121 5.8.1. Top Level Tcl Script for BSP Defaults... 122 5.8.2. Specifying the Default stdio Device...123 ® Nios II Software Developer's Handbook Send Feedback 4

5 Contents 5.8.3. Specifying the Default System Timer...123 5.8.4. Specifying the Default Memory Map...124 5.8.5. Specifying Default Bootloader Parameters...124 5.8.6. Using Individual Default Tcl Procedures... 125 5.9. Device Drivers and Software Packages...126 5.10. Boot Configurations for Intel FPGA Embedded Software...126 5.10.1. Memory Types...126 5.10.2. Boot from Flash Configuration...127 5.10.3. Boot from Monitor Configuration...127 5.10.4. Run from Initialized Memory Configuration...127 5.10.5. Run-time Configurable Reset Configuration... 128 5.11. Intel FPGA-Provided Embedded Development Tools...128 5.11.1. Nios II Software Build Tool GUIs...128 5.11.2. The Nios II Command Shell...131 5.11.3. The Nios II Command-Line Commands...131 5.12. Restrictions... 151 6. Overview of the Hardware Abstraction Layer...152 6.1. Getting Started with the Hardware Abstraction Layer... 152 6.2. HAL Architecture for Embedded Software Systems...153 6.2.1. Services...153 6.2.2. Layers of a HAL-Based System...153 6.2.3. Applications versus Drivers...154 6.2.4. Generic Device Models...154 6.2.5. C Standard Library—newlib...155 6.3. Embedded Hardware Supported by the HAL...155 6.3.1. Nios II Processor Core Support... 155 6.3.2. Supported Peripherals...155 6.3.3. MPU Support... 157 6.3.4. MMU Support...157 7. Developing Programs Using the Hardware Abstraction Layer... 158 7.1. HAL BSP Settings...158 7.2. The Nios II Embedded Project Structure...159 7.3. The system.h System Description File...160 7.4. Data Widths and the HAL Type Definitions... 161 7.5. UNIX-Style Interface...162 7.6. File System...162 7.7. Using Character-Mode Devices...164 7.7.1. Standard Input, Standard Output and Standard Error...164 7.7.2. General Access to Character Mode Devices... 164 7.7.3. C++ Streams... 165 7.7.4. /dev/null...165 7.7.5. Lightweight Character-Mode I/O...165 7.7.6. Intel FPGA Logging Functions...165 7.8. Using File Subsystems...170 7.8.1. Host-Based File System...171 7.9. Using Timer Devices...171 7.9.1. System Clock Driver...172 7.9.2. Alarms...172 7.9.3. Timestamp Driver... 173 ® Nios II Software Developer's Handbook Send Feedback 5

6 Contents 7.10. Using Flash Devices...174 7.10.1. Simple Flash Access...175 7.10.2. Block Erasure or Corruption... 176 7.10.3. Fine-Grained Flash Access... 177 7.11. Using DMA Devices...181 7.11.1. DMA Transmit Channels...182 7.11.2. DMA Receive Channels...183 7.12. Using Interrupt Controllers... 185 7.13. Reducing Code Footprint in Embedded Systems...186 7.13.1. Enable Compiler Optimizations...186 7.13.2. Use Reduced Device Drivers...187 7.13.3. Reduce the File Descriptor Pool...187 7.13.4. Use /dev/null...187 7.13.5. Use a Smaller File I/O Library...188 7.13.6. Use the Lightweight Device Driver API...195 7.13.7. Use the Minimal Character-Mode API...196 7.13.8. Eliminate Unused Device Drivers... 197 7.13.9. Eliminate Unneeded Exit Code...198 7.13.10. Turn off C++ Support...198 7.14. Boot Sequence and Entry Point...198 7.14.1. Hosted Versus Free-Standing Applications...198 7.14.2. Boot Sequence for HAL-Based Programs...199 7.14.3. Customizing the Boot Sequence... 200 7.15. Memory Usage...201 7.15.1. Memory Sections...201 7.15.2. Assigning Code and Data to Memory Partitions...202 7.15.3. Placement of the Heap and Stack... 204 7.15.4. Global Pointer Register...205 7.15.5. Boot Modes... 206 7.16. Working with HAL Source Files... 207 7.16.1. Finding HAL Files...207 7.16.2. Overriding HAL Functions... 207 8. Developing Device Drivers for the Hardware Abstraction Layer...209 8.1. Driver Integration in the HAL API... 209 8.2. The HAL Peripheral-Specific API... 210 8.3. Preparing for HAL Driver Development...210 8.4. Development Flow for Creating Device Drivers...210 8.5. Nios II Hardware Design Concepts...211 8.5.1. The Relationship Between the .sopcinfo File and system.h... 211 8.5.2. Using the System Generation Tool to Optimize Hardware...211 8.5.3. Components, Devices, and Peripherals...211 8.6. Accessing Hardware... 212 8.7. Creating Embedded Drivers for HAL Device Classes... 214 8.7.1. Character-Mode Device Drivers... 214 8.7.2. File Subsystem Drivers... 216 8.7.3. Timer Device Drivers... 217 8.7.4. Flash Device Drivers...218 8.7.5. DMA Device Drivers...219 8.7.6. Ethernet Device Drivers... 221 8.8. Integrating a Device Driver in the HAL...225 ® Nios II Software Developer's Handbook Send Feedback 6

7 Contents 8.8.1. Overview... 225 8.8.2. Assumptions and Requirements...226 8.8.3. The Nios II BSP Generator...227 8.8.4. File Names and Locations...228 8.8.5. Driver and Software Package Tcl Script Creation...229 8.9. Creating a Custom Device Driver for the HAL...238 8.9.1. Header Files and alt_sys_init.c...238 8.9.2. Device Driver Source Code...240 8.10. Reducing Code Footprint in HAL Embedded Drivers... 240 8.10.1. Provide Reduced Footprint Drivers... 240 8.10.2. Support the Lightweight Device Driver API...241 8.11. HAL Namespace Allocation...242 8.12. Overriding the HAL Default Device Drivers...242 9. Exception Handling...244 9.1. Nios II Exception Handling Overview...244 9.1.1. Exception Handling Terminology...244 9.1.2. Interrupt Controllers...246 9.1.3. Latency and Response Time...248 9.2. Nios II Interrupt Service Routines...250 9.2.1. HAL APIs for Hardware Interrupts...251 9.2.2. HAL ISR Restrictions...255 9.2.3. Writing an ISR...255 9.2.4. Registering an ISR with the Enhanced Interrupt API...258 9.2.5. Enabling and Disabling Interrupts...259 9.2.6. Configuring an External Interrupt Controller...260 9.2.7. C Example...260 9.2.8. Upgrading to the Enhanced HAL Interrupt API...261 9.3. Improving Nios II ISR Performance...262 9.3.1. Software Performance Improvements... 262 9.3.2. Hardware Performance Improvements... 268 9.4. Debugging Nios II ISRs...270 9.5. HAL Exception Handling System Implementation...271 9.5.1. Exception Handling System Structure...271 9.5.2. General Exception Funnel...272 9.5.3. Hardware Interrupt Funnel... 273 9.5.4. Software Exception Funnel... 275 9.5.5. Invalid Instructions...279 9.6. The Nios II Instruction-Related Exception Handler... 279 9.6.1. Writing an Instruction-Related Exception Handler... 280 9.6.2. Registering an Instruction-Related Exception Handler...281 9.6.3. Removing an Instruction-Related Exception Handler...282 10. Cache and Tightly-Coupled Memory... 283 10.1. Nios II Cache Implementation...283 10.1.1. Defining Cache Properties...284 10.2. HAL API Functions for Managing Cache...284 10.3. Initializing the Nios II Cache after Reset...284 10.3.1. Assembly Code to Initialize the Instruction Cache... 285 10.3.2. Assembly Code to Initialize the Data Cache...285 10.3.3. HAL Behavior for Initializing the Nios II Cache after Reset...285 ® Nios II Software Developer's Handbook Send Feedback 7

8 Contents 10.4. Nios II Device Driver Cache Considerations...286 10.4.1. HAL Behavior for Nios II Device Driver Cache Considerations...286 10.5. Cache Considerations for Writing Program Loaders...287 10.5.1. HAL Behavior for Cache Considerations for Writing Program Loaders...287 10.6. Managing Cache in Multi-Master and Multi-Processor Systems... 287 10.6.1. Cache Implementation...288 10.6.2. Bit-31 Cache Bypass...288 10.6.3. HAL Behavior for Managing Cache in Multi-Master and Multi-Processor Systems...288 10.7. Nios II Tightly-Coupled Memory... 289 11. MicroC/OS-II Real-Time Operating System...290 11.1. Overview of the MicroC/OS-II RTOS...290 11.1.1. Licensing...291 11.2. Other RTOS Providers... 291 11.3. The Nios II Implementation of MicroC/OS-II...291 11.3.1. MicroC/OS-II Architecture...292 11.3.2. MicroC/OS-II Device Drivers... 292 11.3.3. Thread-Safe HAL Drivers...293 11.3.4. The newlib ANSI C Standard Library...294 11.3.5. Interrupt Service Routines for MicroC/OS-II...295 11.4. Implementing MicroC/OS-II Projects for the Nios II Processor... 295 12. Ethernet and the NicheStack TCP/IP Stack...296 12.1. Prerequisites for Understanding the NicheStack TCP/IP Stack...296 12.2. Introduction to the NicheStack TCP/IP Stack - Nios II Edition... 297 12.2.1. The NicheStack TCP/IP Stack Files and Directories... 297 12.2.2. Support and Licensing...298 12.3. Other TCP/IP Stack Providers for the Nios II Processor... 298 12.4. Using the NicheStack TCP/IP Stack - Nios II Edition... 298 12.4.1. Nios II System Requirements...298 12.4.2. The NicheStack TCP/IP Stack Tasks...299 12.4.3. Initializing the Stack...299 12.4.4. Calling the Sockets Interface...302 12.5. Configuring the NicheStack TCP/IP Stack in a Nios II Program...303 12.5.1. NicheStack TCP/IP Stack General Settings... 304 12.5.2. IP Options...304 12.5.3. TCP Options...304 12.6. Further Information...305 12.7. Known Limitations...305 13. Read-Only Zip File System... 306 13.1. Using the Read-Only Zip File System in a Project...306 13.1.1. Preparing the Zip File...307 13.1.2. Programming the Zip File to Flash...307 14. Publishing Component Information to Embedded Software... 308 14.1. Embedded Component Information Flow...308 14.1.1. Embedded Component Information Flow Diagram...308 14.1.2. Tcl Assignment Statements...309 14.2. Embedded Software Assignments...309 14.2.1. C Macro Namespace... 309 ® Nios II Software Developer's Handbook Send Feedback 8

9 Contents 14.2.2. Configuration Namespace...310 14.2.3. Memory Initialization Namespace...313 15. HAL API Reference...315 15.1. HAL API Functions...315 15.1.1. _exit()...315 15.1.2. _rename()...316 15.1.3. alt_dcache_flush()...316 15.1.4. alt_dcache_flush_all()...317 15.1.5. alt_dcache_flush_no_writeback()...318 15.1.6. alt_uncached_malloc()...319 15.1.7. alt_uncached_free()... 319 15.1.8. alt_remap_uncached()...320 15.1.9. alt_remap_cached()...321 15.1.10. alt_icache_flush_all()...322 15.1.11. alt_icache_flush()...323 15.1.12. alt_alarm_start()...323 15.1.13. alt_alarm_stop()... 325 15.1.14. alt_dma_rxchan_depth()... 325 15.1.15. alt_dma_rxchan_close()...326 15.1.16. alt_dev_reg()...327 15.1.17. alt_dma_rxchan_open()... 328 15.1.18. alt_dma_rxchan_prepare()... 329 15.1.19. alt_dma_rxchan_reg()...330 15.1.20. alt_dma_txchan_close()... 331 15.1.21. alt_dma_txchan_ioctl()...332 15.1.22. alt_dma_txchan_open()... 333 15.1.23. alt_dma_txchan_reg()...334 15.1.24. alt_flash_close_dev()...335 15.1.25. alt_exception_cause_generated_bad_addr()...336 15.1.26. alt_erase_flash_block()... 336 15.1.27. alt_dma_rxchan_ioctl()... 337 15.1.28. alt_dma_txchan_space()... 339 15.1.29. alt_dma_txchan_send()...340 15.1.30. alt_flash_open_dev()... 341 15.1.31. alt_fs_reg()... 341 15.1.32. alt_get_flash_info()... 342 15.1.33. alt_ic_irq_disable()...343 15.1.34. alt_ic_irq_enabled()...344 15.1.35. alt_ic_isr_register()... 345 15.1.36. alt_ic_irq_enable()...347 15.1.37. alt_instruction_exception_register()...348 15.1.38. alt_irq_disable()...349 15.1.39. alt_irq_cpu_enable_interrupts ()...350 15.1.40. alt_irq_disable_all()...351 15.1.41. alt_irq_enable()... 352 15.1.42. alt_irq_enable_all()...352 15.1.43. alt_irq_enabled()...353 15.1.44. alt_irq_init()... 354 15.1.45. alt_irq_pending ()...355 15.1.46. alt_irq_register()...356 ® Nios II Software Developer's Handbook Send Feedback 9

10 Contents 15.1.47. alt_llist_insert()...357 15.1.48. alt_llist_remove()...358 15.1.49. alt_load_section()...359 15.1.50. alt_nticks()...360 15.1.51. alt_read_flash()...360 15.1.52. alt_tick()...361 15.1.53. alt_ticks_per_second()...362 15.1.54. alt_timestamp()... 363 15.1.55. alt_timestamp_freq()...364 15.1.56. alt_timestamp_start()...364 15.1.57. alt_write_flash()...365 15.1.58. alt_write_flash_block()...366 15.1.59. close()... 367 15.1.60. fstat()...368 15.1.61. fork()...369 15.1.62. fcntl()... 369 15.1.63. execve()...371 15.1.64. getpid()...371 15.1.65. kill()... 372 15.1.66. stat()...373 15.1.67. settimeofday()... 374 15.1.68. wait()...374 15.1.69. unlink()...375 15.1.70. sbrk()... 376 15.1.71. link()... 376 15.1.72. lseek()...377 15.1.73. alt_sysclk_init()...378 15.1.74. open()... 379 15.1.75. times()...380 15.1.76. read()...381 15.1.77. write()... 382 15.1.78. usleep()... 383 15.1.79. alt_lock_flash()...384 15.1.80. gettimeofday()...385 15.1.81. ioctl()...386 15.1.82. isatty()...387 15.2. HAL Standard Types...388 15.2.1. alt_getchar()... 389 15.2.2. alt_putstr()...389 15.2.3. alt_putchar()...390 15.2.4. alt_printf()... 391 15.3. ADC HAL Device Driver... 391 15.3.1. adc_stop...391 15.3.2. adc_start...392 15.3.3. adc_set_mode_run_once...392 15.3.4. adc_set_mode_run_continuously...392 15.3.5. adc_recalibrate... 393 15.3.6. adc_interrupt_enable...393 15.3.7. adc_interrupt_disable... 394 15.3.8. adc_clear_interrupt_status... 394 15.3.9. adc_wait_for_interrupt - ADC Sample Storage Status Register...394 ® Nios II Software Developer's Handbook Send Feedback 10

11 Contents 15.3.10. adc_interrupt_asserted...395 15.3.11. adc_wait_for interrupt - IRQ Status Register... 395 15.3.12. alt_adc_word_read... 395 16. Nios II Software Build Tools Reference...396 16.1. Nios II Software Build Tools Utilities...396 16.1.1. Logging Levels...396 16.1.2. Setting Values...397 16.1.3. Utility and Script Summary...397 16.1.4. nios2-app-generate-makefile... 398 16.1.5. nios2-bsp-create-settings...400 16.1.6. nios2-bsp-generate-files...402 16.1.7. nios2-bsp-query-settings...403 16.1.8. nios2-bsp-update-settings...404 16.1.9. nios2-lib-generate-makefile... 405 16.1.10. nios2-bsp-editor...407 16.1.11. nios2-app-update-makefile... 407 16.1.12. nios2-lib-update-makefile...410 16.1.13. nios2-swexample-create...412 16.1.14. nios2-elf-insert...413 16.1.15. nios2-elf-query...414 16.1.16. nios2-flash-programmer-generate...415 16.1.17. nios2-bsp... 417 16.1.18. nios2-bsp-console...419 16.1.19. alt-file-convert (BETA)...420 16.2. Nios II Design Example Scripts...421 16.2.1. create-this-bsp... 421 16.2.2. create-this-app... 421 16.2.3. Finding create-this-app and create-this-bsp... 422 16.3. Settings Managed by the Software Build Tools...423 16.3.1. Overview of BSP Settings... 424 16.3.2. Overview of Component and Driver Settings... 425 16.3.3. Settings Reference... 426 16.4. Application and User Library Makefile Variables...462 16.4.1. Application Makefile Variables...462 16.4.2. User Library Makefile Variables...464 16.4.3. Standard Build Flag Variables...465 16.5. Software Build Tools Tcl Commands... 465 16.5.1. Tcl Command Environments...465 16.5.2. Tcl Commands for BSP Settings...466 16.5.3. Tcl Commands for BSP Generation Callbacks... 491 16.5.4. Tcl Commands for Drivers and Packages...500 16.6. Software Build Tools Path Names...511 16.6.1. Command Arguments... 511 16.6.2. Object File Directory Tree... 512 ® Nios II Software Developer's Handbook Send Feedback 11

12 NII-SDH | 2019.04.30 Send Feedback ® 1. Nios II Software Developer's Handbook Revision History ® II Software Developer's Handbook Revision History Summary Table 1. Nios Date of Last Update Chapter ® II Embedded Development Revision Overview of Nios October 29, 2018 History on page 13 April 30, 2019 Getting Started with the Graphical User Interface Revision History on page 13 October 29, 2018 on Getting Started from the Command Line Revision History page 13 October 29, 2018 Nios II Software Build Tools Revision History on page 14 October 29, 2018 Overview of the Hardware Abstraction Layer Revision History on page 14 October 29, 2018 Developing Programs Using the Hardware Abstraction Layer on page 14 Revision History Developing Device Drivers for the Hardware Abstraction October 29, 2018 on page 15 Layer Revision History October 29, 2018 Exception Handling Revision History on page 15 on Cache and Tightly-Coupled Memory Revision History October 29, 2018 page 15 MicroC/OS-II Real-Time Operating System Revision History October 29, 2018 on page 15 Ethernet and the NicheStack TCP/IP Stack - Nios II Edition October 29, 2018 on page 16 Revision History Read-Only Zip File System Revision History on page 16 October 29, 2018 October 29, 2018 Publishing Component Information to Embedded Software on page 16 Revision History October 29, 2018 HAL API Reference Revision History on page 17 on Nios II Software Build Tools Reference Revision History October 29, 2018 page 17 Intel Corporation. All rights reserved. Agilex, Altera, Arria, Cyclone, Enpirion, Intel, the Intel logo, MAX, Nios, Quartus and Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in the U.S. and/or ISO other countries. Intel warrants performance of its FPGA and semiconductor products to current specifications in 9001:2015 accordance with Intel's standard warranty, but reserves the right to make changes to any products and services Registered at any time without notice. Intel assumes no responsibility or liability arising out of the application or use of any information, product, or service described herein except as expressly agreed to in writing by Intel. Intel customers are advised to obtain the latest version of device specifications before relying on any published information and before placing orders for products or services. *Other names and brands may be claimed as the property of others.

13 ® II Software Developer's Handbook Revision History 1. Nios NII-SDH | 2019.04.30 ® 1.1. Overview of Nios II Embedded Development Revision History ® ® Quartus Intel Changes Document Version Prime Version 2018.10.29 18.1 Editorial changes: Rebranding. 2017.05.08 Maintenance release. 17.0 2015.05.14 Initial release. 15.0 Related Information on page 18 Overview of Nios II Embedded Development 1.2. Getting Started with the Graphical User Interface Revision History Document Version Changes Intel Quartus Prime Version . 2019.04.30 19.1 Added section: Installing Eclipse IDE into Nios II EDS 2018.10.29 . Nios II Hardware v2 (beta) • Removed section: 18.1 • Editorial changes: Rebranding. 2017.05.08 • Added information about when to use the Nios II Hardware v2 (beta) 17.0 section. • Added information about using Intel Quartus Prime Programmer when Programming Flash in using the Intel Quartus Prime Pro Edition in the section. Intel FPGA Embedded Systems 15.1 Removed references of the SOPC Builder. 2015.12.14 Corrected the available versions of the GCC. 2015.10.30 15.1 15.0 Initial release. 2015.05.14 Related Information on page 25 Getting Started with the Graphical User Interface 1.3. Getting Started from the Command Line Revision History Changes Intel Quartus Document Version Prime Version 18.1 Editorial changes: Rebranding. 2018.10.29 17.0 2017.05.08 Maintenance release. Initial release. 15.0 2015.05.14 Related Information on page 72 Getting Started from the Command Line ® Nios II Software Developer's Handbook Send Feedback 13

14 ® II Software Developer's Handbook Revision History 1. Nios NII-SDH | 2019.04.30 1.4. Nios II Software Build Tools Revision History Document Version Changes Intel Quartus Prime Version Editorial changes: Rebranding. 2018.10.29 18.1 2017.05.08 17.0 section. MacroC/OS-II Thread-Aware Debugging Removed the 15.0 Initial release. 2015.05.14 Related Information • Nios II Software Build Tools on page 86 • GNU Website 1.5. Overview of the Hardware Abstraction Layer Revision History Intel Quartus Document Version Changes Prime Version 2018.10.29 18.1 Editorial changes: Rebranding. 2017.05.08 Maintenance release. 17.0 Initial release. 15.0 2015.05.14 Related Information on page 152 Overview of the Hardware Abstraction Layer 1.6. Developing Programs Using the Hardware Abstraction Layer Revision History Changes Intel Quartus Document Version Prime Version 18.1 Editorial changes: Rebranding. 2018.10.29 • In section , replaced instances of "txt" Advanced_Placement_Options 17.1 2017.10.09 with "text" Boot Modes • In section , updated the description for alt_load_section() 17.0 Maintenance release. 2017.05.08 2015.05.14 Initial release. 15.0 Related Information Developing Programs Using the Hardware Abstraction Layer on page 158 ® Nios II Software Developer's Handbook Send Feedback 14

15 ® 1. Nios II Software Developer's Handbook Revision History NII-SDH | 2019.04.30 1.7. Developing Device Drivers for the Hardware Abstraction Layer Revision History Changes Intel Quartus Document Version Prime Version 18.1 2018.10.29 Editorial changes: Rebranding. 2017.05.08 17.0 Maintenance release. 15.0 Initial release. 2015.05.14 Related Information Developing Device Drivers for the Hardware Abstraction Layer on page 209 1.8. Exception Handling Revision History Changes Intel Quartus Document Version Prime Version 2018.10.29 18.1 Editorial changes: Rebranding. Maintenance release. 2017.05.08 17.0 2015.05.14 Initial release. 15.0 Related Information Exception Handling on page 244 1.9. Cache and Tightly-Coupled Memory Revision History Intel Quartus Changes Document Version Prime Version Editorial changes: Rebranding. 18.1 2018.10.29 17.0 Maintenance release. 2017.05.08 Initial release. 2015.05.14 15.0 Related Information Cache and Tightly-Coupled Memory on page 283 1.10. MicroC/OS-II Real-Time Operating System Revision History Changes Intel Quartus Document Version Prime Version 18.1 Editorial changes: Rebranding. 2018.10.29 2017.05.08 Maintenance release. 17.0 15.0 2015.05.14 Initial release. ® Nios II Software Developer's Handbook Send Feedback 15

16 ® II Software Developer's Handbook Revision History 1. Nios NII-SDH | 2019.04.30 Related Information MicroC/OS-II Real-Time Operating System on page 290 1.11. Ethernet and the NicheStack TCP/IP Stack - Nios II Edition Revision History Document Version Intel Quartus Changes Prime Version 2018.10.29 18.1 Editorial changes: Rebranding. 2017.05.08 Maintenance release. 17.0 2015.05.14 Initial release. 15.0 Related Information • Appendix A: Using the Nios II Integrated Development Environment • Ethernet and the NicheStack TCP/IP Stack on page 296 1.12. Read-Only Zip File System Revision History Document Version Intel Quartus Changes Prime Version Editorial changes: Rebranding. 18.1 2018.10.29 Maintenance release. 2017.05.08 17.0 15.0 Initial release. 2015.05.14 Related Information on page 306 Read-Only Zip File System 1.13. Publishing Component Information to Embedded Software Revision History Intel Quartus Changes Document Version Prime Version 18.1 Editorial changes: Rebranding. 2018.10.29 17.0 Maintenance release. 2017.05.08 15.0 Initial release. 2015.05.14 Related Information on page 308 Publishing Component Information to Embedded Software ® Nios II Software Developer's Handbook Send Feedback 16

17 ® II Software Developer's Handbook Revision History 1. Nios NII-SDH | 2019.04.30 1.14. HAL API Reference Revision History Document Version Changes Intel Quartus Prime Version 2018.10.29 Editorial changes: Rebranding. 18.1 2017.10.09 17.1 . alt_load_section() Updated the description for Added the ADC HAL Device Driver APIs. 2017.05.08 17.0 15.0 2015.05.14 Initial release. Related Information on page 315 HAL API Reference 1.15. Nios II Software Build Tools Reference Revision History Document Version Intel Quartus Changes Prime Version 2018.10.29 18.1 • Clarified the big endian support in setting. hal.make.ignore_system_derived.big_endian • Editorial changes: Rebranding. 2017.05.08 Maintenance release. 17.0 15.0 2015.05.14 Initial release. Related Information Nios II Software Build Tools Reference on page 396 ® Nios II Software Developer's Handbook Send Feedback 17

18 NII-SDH | 2019.04.30 Send Feedback 2. Overview of Nios II Embedded Development There are two variation of : Nios II Software Developer's Handbook • The Nios II Classic Software Developer's Handbook describes embedded software development tools for the Nios II Classic cores; and there are no future updates planned. describes embedded software Nios II Software Developer's Handbook • The development tools for the Nios II. It does not describe IP cores. This handbook describes the basic information needed to develop embedded software for the Intel FPGA Nios II processor. The Intel FPGA Nios II processor contains new features added after the Intel Quartus Prime 14.0 version. The chapters in this handbook describes the Nios II software development environment, the Nios II Embedded Design Suite (EDS) tools that are available to you, and the process for developing software. Related Information on page 13 Overview of Nios II Embedded Development Revision History For details on the document revision history of this chapter 2.1. Prerequisites for Understanding the Nios II Embedded Design Suite The Nios II Software Developer’s Handbook assumes you have a basic familiarity with embedded processor concepts. You do not need to be familiar with any specific Intel FPGA technology or with Intel FPGA development tools. Familiarity with Intel FPGA hardware development tools can give you a deeper understanding of the reasoning behind the Nios II software development environment. However, software developers can create and debug applications without further knowledge of Intel FPGA technology. 2.2. Finding Nios II EDS Files When you install the Nios II EDS, you specify a root directory for the EDS file structure. This root directory must be adjacent to the Quartus II installation. When you install the latest release of the Nios II EDS on the Windows operating system, choose a local root folder that identifies the content, for example: c:\altera \ . \nios2eds . For simplicity, this handbook refers to this directory as Note: Intel Corporation. All rights reserved. Agilex, Altera, Arria, Cyclone, Enpirion, Intel, the Intel logo, MAX, Nios, Quartus and Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in the U.S. and/or ISO other countries. Intel warrants performance of its FPGA and semiconductor products to current specifications in 9001:2015 accordance with Intel's standard warranty, but reserves the right to make changes to any products and services Registered at any time without notice. Intel assumes no responsibility or liability arising out of the application or use of any information, product, or service described herein except as expressly agreed to in writing by Intel. Intel customers are advised to obtain the latest version of device specifications before relying on any published information and before placing orders for products or services. *Other names and brands may be claimed as the property of others.

19 2. Overview of Nios II Embedded Development NII-SDH | 2019.04.30 2.3. Nios II Software Development Environment The Nios II EDS provides a consistent software development environment that works for all Nios II processor systems. With the Nios II EDS running on a host computer, an ™ Intel FPGA FPGA, and a JTAG download cable (such as an Intel FPGA USB-Blaster download cable), you can write programs for and communicate with any Nios II processor system. The Nios II processor’s JTAG debug module provides a single, consistent method to connect to the processor using a JTAG download cable. Accessing the processor is the same, regardless of whether a device implements only a Nios II processor system, or whether the Nios II processor is embedded deeply in a complex multiprocessor system. Therefore, you do not need to spend time manually creating interface mechanisms for the embedded processor. The Nios II EDS includes proprietary and open-source tools (such as the GNU C/C++ tool chain) for creating Nios II programs. The Nios II EDS automates board support package (BSP) creation for Nios II processor-based systems, eliminating the need to spend time manually creating BSPs. The BSP provides a C/C++ runtime environment, insulating you from the hardware in your embedded system. Intel FPGA BSPs contain the Intel FPGA hardware abstraction layer (HAL), an optional RTOS, and device drivers. 2.4. Nios II EDS Development Flows A development flow is a way of using a set of development tools together to create a software project. The Nios II EDS provides the following development flows for creating Nios II programs: • The Nios II Software Build Tools (SBT), which provides two user interfaces: — The Nios II SBT command line — The Nios II SBT for Eclipse 2.4.1. Nios II SBT Development Flow The Nios II SBT allows you to create Nios II software projects, with detailed control over the software build process. The same Nios II SBT utilities, scripts and Tcl commands are available from both the command line and the Nios II SBT for Eclipse graphical user interface (GUI). The SBT allows you to create and manage single-threaded programs as well as complex applications based on an RTOS and middleware libraries available from Intel FPGA and third-party vendors. The SBT provides powerful Tcl scripting capabilities. In a Tcl script, you can query project settings, specify project settings conditionally, and incorporate the software project creation process in a scripted software development flow. Tcl scripting is supported both in Eclipse and at the command line. Related Information on page 86 Nios II Software Build Tools For more information about Tcl scripting. ® Nios II Software Developer's Handbook Send Feedback 19

20 2. Overview of Nios II Embedded Development NII-SDH | 2019.04.30 2.4.1.1. Nios II SBT for Eclipse The Nios II SBT for Eclipse is a thin GUI layer that runs the Nios II SBT utilities and scripts behind the scenes, presenting a unified development environment. The SBT for Eclipse provides a consistent development platform that works for all Nios II processor systems. You can accomplish all software development tasks within Eclipse, including creating, editing, building, running, debugging, and profiling programs. The Nios II SBT for Eclipse is based on the popular Eclipse framework and the Eclipse C/C++ development toolkit (CDT) plugins. The Nios II SBT creates your project makefiles for you, and Eclipse provides extensive capabilities for interactive debugging and management of source files. The SBT for Eclipse also allows you to import and debug projects you created in the Nios II Command Shell. Related Information on page 25 • Getting Started with the Graphical User Interface For more information about the Nios II SBT for Eclipse. • Eclipse Foundation For more information about Eclipse, visit the Eclipse Foundation website. 2.4.1.2. Nios II SBT Command Line In the Nios II SBT command line development flow, you create, modify, build, and run Nios II programs with Nios II SBT commands typed at a command line or embedded in a script. You run the Nios II SBT commands from the Nios II Command Shell. Note: To debug your command-line program, import your SBT projects to Eclipse. You can further edit, rebuild, run, and debug your imported project in Eclipse. Related Information on page 72 Getting Started from the Command Line For more information about the Nios II SBT in command-line mode 2.5. Nios II Programs Each Nios II program you develop consists of an application project, optional user library projects, and a BSP project. You build your Nios II program to create an Executable and Linking Format File ( .elf) which runs on a Nios II processor. The Nios II SBT creates software projects for you. Each project is based on a makefile. 2.5.1. Makefiles and the SBT The makefile is the central component of a Nios II software project, whether the project is created with the Nios II SBT for Eclipse, or on the command line. The makefile describes all the components of a software project and how they are compiled and linked. With a makefile and a complete set of C/C++ source files, your Nios II software project is fully defined. ® Nios II Software Developer's Handbook Send Feedback 20

21 2. Overview of Nios II Embedded Development NII-SDH | 2019.04.30 As a key part of creating a software project, the SBT creates a makefile for you. Nios II projects are sometimes called "user-managed," because you, the user, are responsible for the content of the project makefile. You use the Nios II SBT to control what goes in the makefile. Related Information on page 396 Nios II Software Build Tools Reference For more information about creating makefiles. 2.5.2. Nios II Software Project Types 2.5.2.1. Application Project A Nios II C/C++ application project consists of a collection of source code, plus a makefile. A typical characteristic of an application is that one of the source files contains function main() . An application includes code that calls functions in libraries and BSPs. The makefile compiles the source code and links it with a BSP and one or elf more optional libraries, to create one . file. 2.5.2.2. User Library Project A user library project is a collection of source code compiled to create a single library archive file ( ). Libraries often contain reusable, general purpose functions that .a multiple application projects can share. A collection of common arithmetical functions main() is one example. A user library does not contain a function. 2.5.2.3. BSP Project A Nios II BSP project is a specialized library containing system-specific support code. A BSP provides a software runtime environment customized for one processor in a Nios II hardware system. The Nios II EDS provides tools to modify settings that control the behavior of the BSP. A BSP contains the following elements: • Hardware abstraction layer (1) • Optional custom newlib C standard library • Device drivers • Optional software packages • Optional real-time operating system Related Information • Intel FPGA Software Packages for Embedded Systems on page 22 on page 152 • Overview of the Hardware Abstraction Layer • Nios II Software Build Tools Reference on page 396 For more information, refer to the "Nios II Embedded Software Projects" chapter. on page 22 • Intel FPGA Software Packages for Embedded Systems (1) The complete HTML documentation for newlib resides in the Nios II EDS directory. ® Nios II Software Developer's Handbook Send Feedback 21

22 2. Overview of Nios II Embedded Development NII-SDH | 2019.04.30 • MicroC/OS-II Real-Time Operating System on page 290 2.6. Intel FPGA Software Packages for Embedded Systems The Nios II EDS includes software packages to extend the capabilities of your software. You can include these software packages in your BSP. Table 2. Intel FPGA Nios II Software Packages Distributed with the Nios II EDS Name Description Refer to the "Ethernet and the NicheStack TCP/IP Stack - NicheStack TCP/IP Stack - Nios II Edition Nios II Edition" chapter. Read-only zip file system Refer to the "Read-Only Zip File System" chapter. Host file system Refer to the "Developing Programs Using the Hardware Abstraction Layer" chapter. Related Information • Read-Only Zip File System on page 306 • Developing Programs Using the Hardware Abstraction Layer on page 158 on page 296 • Ethernet and the NicheStack TCP/IP Stack • Embedded Software For more information about a complete list of the additional software packages available from Intel FPGA’s partners 2.7. Nios II Embedded Design Examples The Nios II EDS includes documented software examples to demonstrate all prominent features of the Nios II processor and the development environment. The examples can help you start the development of your custom design. They provide a stable starting point for exploring design options. Also, they demonstrate many commonly used features of the Nios II EDS. Note: The hardware design examples are available on the Embedded Processor Design Examples web page. Related Information Embedded Processor Design Examples 2.7.1. Hardware Examples You can run Nios II hardware designs on many Intel development boards. The hardware examples for each Intel development board are available on the Design Examples web page.. is intended to demonstrate Linux. This design does Note: The Nios II with MMU design not work with the SBT, because the SBT does not support the Nios II MMU. Related Information • Nios II Ethernet Standard Design Example • Nios II Processor with Memory Management Unit Design Example ® Nios II Software Developer's Handbook Send Feedback 22

23 2. Overview of Nios II Embedded Development NII-SDH | 2019.04.30 • Intel FPGA Development Kits • Design Examples 2.7.2. Software Examples You can run Nios II software examples that run on many of the hardware design examples described in the previous section. The Nios II software examples directory contains the following files: • Source file (.c) • Header file (.h) • readme.txt • template.xml Figure 1. Software Design Example Directory Structure Software Examples .c readme.txt template.xml .h Related Information Getting Started from the Command Line on page 72 For more information about using these scripts to create software projects. 2.8. Third-Party Embedded Tools Support Several third-party vendors support the Nios II processor, providing products such as design services, operating systems, stacks, other software libraries, and development tools. Related Information Nios II Processor For more information about the most up-to-date information about third-party support for the Nios II processor 2.9. Additional Nios II Information This handbook is one part of the complete Nios II processor documentation suite. Consult the following references for further Nios II information: ® Nios II Software Developer's Handbook Send Feedback 23

24 2. Overview of Nios II Embedded Development NII-SDH | 2019.04.30 • The Nios II Processor Reference Guide defines the processor hardware architecture and features, including the instruction set architecture. provides a reference for the peripherals • The Embedded Peripherals IP User Guide distributed with the Nios II processor. This handbook describes the hardware structure and Nios II software drivers for each peripheral. describes how to use the software development Embedded Design Handbook • The tools effectively, and recommends design styles and practices for developing, debugging, and optimizing embedded systems. • The Intel FPGA Knowledge Database is an Internet resource that offers solutions to frequently asked questions with an easy-to-use search engine. • Application notes and tutorials offer step-by-step instructions on using the Nios II processor for a specific application or purpose. These documents are available on the Intel FPGA Nios II Processor Documentation web page. • The Nios II EDS documentation launchpad. The launchpad is an HTML page installed with the Nios II EDS, which provides links to Nios II documentation, examples, and other resources. The way you open the launchpad depends on your software platform. — In the Windows operating system, on the Start menu, point to Programs > Nios II EDS , and click Nios II Documentation . Intel FPGA > /documents/ — In the Linux operating system, open index.html in a web browser. Related Information • Nios II Processor Reference Guide For more information on hardware architecture and features, including the instruction set architecture • Embedded Peripherals IP User Guide For more information on hardware structure and Nios II software drivers for each peripheral • Embedded Design Handbook For more information on design styles and practices for developing, debugging, and optimizing embedded systems • Intel FPGA Knowledge Database For more information, refer to the Knowledge Database page of the Intel FPGA website. • Intel FPGA Nios II Processor Documentation ® Nios II Software Developer's Handbook Send Feedback 24

25 NII-SDH | 2019.04.30 Send Feedback 3. Getting Started with the Graphical User Interface The Nios II Software Build Tools (SBT) for Eclipse is a set of plugins based on the Eclipse framework and the Eclipse C/C++ development toolkit (CDT) plugins. The Nios II SBT for Eclipse provides a consistent development platform that works for all Nios II embedded processor systems. You can accomplish all Nios II software development tasks within Eclipse, including creating, editing, building, running, debugging, and profiling programs. Related Information on page 13 Getting Started with the Graphical User Interface Revision History For details on the document revision history of this chapter 3.1. Installing Eclipse IDE into Nios II EDS Starting with Nios II EDS v19.1, the Nios II EDS requires the Eclipse IDE component to be manually installed. The procedure for installing Eclipse IDE: 1. Download CDT 8.8.1 which is Eclipse C/C++ IDE for Mars.2. https://www.eclipse.org/downloads/download.php?file=/ a. Windows: technology/epp/downloads/release/mars/2/eclipse-cpp-mars-2-win32- x86_64.zip b. Linux: https://www.eclipse.org/downloads/download.php?file=/ technology/epp/downloads/release/mars/2/eclipse-cpp-mars-2-linux-gtk- x86_64.tar.gz 2. /nios2eds/bin . / You should see the nios2eds/bin/eclipse folder after extraction is done. 3. Rename the / nios2eds/bin/eclipse folder to /nios2eds/bin/eclipse_nios2 . https://www.eclipse.org/downloads/download.php?file=/ 4. Extract the technology/epp/downloads/release/mars/2/eclipse-cpp-mars-2-win32-x86_64.zip https://www.eclipse.org/downloads/download.php?file=/ for Windows or technology/epp/downloads/release/mars/2/eclipse-cpp-mars-2-linux-gtk- /nios2eds/bin . Intel Corporation. All rights reserved. Agilex, Altera, Arria, Cyclone, Enpirion, Intel, the Intel logo, MAX, Nios, Quartus and Stratix words and logos are trademarks of Intel Corporation or its subsidiaries in the U.S. and/or ISO other countries. Intel warrants performance of its FPGA and semiconductor products to current specifications in 9001:2015 accordance with Intel's standard warranty, but reserves the right to make changes to any products and services Registered at any time without notice. Intel assumes no responsibility or liability arising out of the application or use of any information, product, or service described herein except as expressly agreed to in writing by Intel. Intel customers are advised to obtain the latest version of device specifications before relying on any published information and before placing orders for products or services. *Other names and brands may be claimed as the property of others.

26 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 The extraction overrides files in /nios2eds/bin/eclipse_nios2 5. Verify the extraction is done correctly by making sure you see the /nios2eds/bin/ file. eclipse_nios2/plugin_customization.ini 6. You can now launch Nios II SBT for Eclipse using . eclipse-nios2.exe Note: /nios2eds/bin/README 3.2. Getting Started with Nios II Software in Eclipse Writing software for the Nios II processor is similar to writing software for any other microcontroller family. The easiest way to start designing effectively is to purchase a development kit from Intel FPGA that includes documentation, a ready-made evaluation board, a getting-started reference design, and all the development tools necessary to write Nios II programs. Modifying existing code is a common, easy way to learn to start writing software in a new environment. The Nios II Embedded Design Suite (EDS) provides many example software designs that you can examine, modify, and use in your own programs. The provided examples range from a simple "Hello world" program, to a working RTOS example, to a full TCP/IP stack running a web server. Each example is documented and ready to compile. This section guides you through the most fundamental operations in the Nios II SBT for Eclipse in a tutorial-like fashion. It shows how to create an application project for the Nios II processor, along with the board support package (BSP) project required to interface with your hardware. It also shows how to build the application and BSP projects in Eclipse, and how to run the software on an Intel FPGA development board. 3.2.1. The Nios II SBT for Eclipse Workbench The term 'workbench' refers to the Nios II SBT for Eclipse desktop development environment. The workbench is where you edit, compile and debug your programs in Eclipse. 3.2.1.1. Perspectives, Editors, and Views Each workbench window contains one or more perspectives. Each perspective provides a set of capabilities for accomplishing a specific type of task. Most perspectives in the workbench comprise an editor area and one or more views. An editor allows you to open and edit a project resource (i.e., a file, folder, or project). Views support editors, and provide alternative presentations and ways to navigate the information in your workbench. Any number of editors can be open at once, but only one can be active at a time. The main menu bar and toolbar for the workbench window contain operations that are applicable to the active editor. Tabs in the editor area indicate the names of resources that are currently open for editing. An asterisk (*) indicates that an editor has unsaved changes. Views can also provide their own menus and toolbars, which, if ® Nios II Software Developer's Handbook Send Feedback 26

27 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 present, appear along the top edge of the view. To open the menu for a view, click the drop-down arrow icon at the right of the view's toolbar or right-click in the view. A view might appear on its own, or stacked with other views in a tabbed notebook. For detailed information about the Eclipse workbench, perspectives, and views, refer to the Eclipse help system. Before you create a Nios II project, you must ensure that the Nios II perspective is visible. To open the Nios II perspective, on the Window menu, point to Open , then . Other , and click Perspective Nios II 3.2.1.2. The Intel FPGA Bytestream Console The workbench in Eclipse for Nios II includes a bytestream console, available through the Eclipse Console view. The Intel FPGA bytestream console enables you to see stderr stdout stdin output from the processor's and devices, and send input to its device. Related Information on page 33 Using the Intel FPGA Bytestream Console For more information about the Altera bytestream console. 3.2.2. Creating a Project In the Nios II perspective, on the File menu, point to Nios II Application and BSP Nios II Application and BSP from Template wizard appears. from Template . The This wizard provides a quick way to create an application and BSP at the same time. Alternatively, you can create separate application, BSP and user library projects. 3.2.2.1. Specifying the Application Nios II Application and BSP from Template In the first page of the wizard, you specify a hardware platform, a project name, and a project template. You optionally override the default location for the application project, and specify a processor name if you are targeting a multiprocessor hardware platform. You specify a BSP in the second page of the wizard. 3.2.2.1.1. Specifying the Hardware Platform You specify the target hardware design by selecting a SOPC Information File ) in the SOPC Information File name box. ( .sopcinfo 3.2.2.1.2. Specifying the Project Name Select a descriptive name for your project. The SBT creates a folder with this name to contain the application project files. Letters, numbers, and the underscore (_) symbol are the only valid project name characters. Project names cannot contain spaces or special characters. The first character in the project name must be a letter or underscore. The maximum filename length is 250 characters. ® Nios II Software Developer's Handbook Send Feedback 27

28 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Related Information Specifying the BSP on page 28 For more information about how the SBT also creates a folder to contain BSP project files. 3.2.2.1.3. Specifying the Project Template Project templates are ready-made, working software projects that serve as examples to show you how to structure your own Nios II projects. It is often easier to start with a working project than to start a blank project from scratch. You select the project template from the list. Templates The hello_world template provides an easy way to create your first Nios II project and verify that it builds and runs correctly. 3.2.2.1.4. Specifying the Project Location The project location is the parent directory in which the SBT creates the project folder. By default, the project location is under the directory containing the . sopcinfo file, in software a folder named . To place your application project in a different folder, turn off , Use default location box. Project location and specify the path in the 3.2.2.1.5. Specifying the Processor CPU name contains a If your target hardware contains multiple Nios II processors, list of all available processors in your design. Select the processor on which your software is intended to run. 3.2.2.2. Specifying the BSP Nios When you have finished specifying the application project in the first page of the wizard, you proceed to the second page by II Application and BSP from Template Next . clicking On the second page, you specify the BSP to link with your application. You can create a new BSP for your application, or select an existing BSP. Creating a new BSP is often the simplest way to get a project running the first time. You optionally specify the name and location of the BSP. 3.2.2.2.1. Specifying the BSP Project Name >, the BSP is named project By default, if your application project name is < >_bsp . You can type in a different name if you prefer. The SBT creates a < project directory with this name, to contain the BSP project files. Related Information on page 27 Specifying the Project Name For more information about how the BSP project names are subject to the same restrictions as application project names. ® Nios II Software Developer's Handbook Send Feedback 28

29 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.2.2.2.2. Specifying the BSP Project Location The BSP project location is the parent directory in which the SBT creates the folder. The default project location is the same as the default location for an application project. To place your BSP in a different folder, turn off , and Use default location specify the BSP location in the Project location box. 3.2.2.2.3. Selecting an Existing BSP As an alternative to creating a BSP automatically from a template, you can associate Select an existing BSP your application project with a pre-existing BSP. Select Import , and select a BSP in the list. The Create project from your workspace and buttons to the right of the existing BSP list provide convenient ways to add BSPs to the list. 3.2.2.3. Creating the Projects to create the projects. When you have specified your BSP, you click Finish The SBT copies required source files to your project directories, and creates makefiles command on your make clean and other generated files. Finally, the SBT executes a BSP. Related Information on page 86 Nios II Software Build Tools For more information about the folders and files in a Nios II BSP. 3.2.3. Navigating the Project view, When you have created a Nios II project, it appears in the Project Explorer which is typically displayed at the left side of the Nios II perspective. You can expand each project to examine its folders and files. Related Information on page 86 Nios II Software Build Tools For more information about what happens when Nios II projects are created, refer to "Nios II Software Projects". For more information about the make clean command, refer to "Makefiles". 3.2.4. Building the Project To build a Nios II project in the Nios II SBT for Eclipse, right-click the project name Build Project and click . A progress bar shows you the build status. The build process can take a minute or two for a simple project, depending on the speed of the host machine. Building a complex project takes longer. During the build process, you view the build commands and command-line output in view. the Eclipse Console Console When the build process is complete, the following message appears in the title: view, under the C-Build [] build complete] [ ® Nios II Software Developer's Handbook Send Feedback 29

30 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 If the project has a dependency on another project, such as a BSP or a user library, the SBT builds the dependency project first. This feature allows you to build an application and its BSP with a single command. Related Information on page 396 Nios II Software Build Tools Reference 3.2.5. Configuring the FPGA Before you can run your software, you must ensure that the correct hardware design is running on the FPGA. To configure the FPGA, you use the Intel Quartus Prime Programmer. In the Windows operating system, you start the Intel Quartus Prime Programmer from the Nios II SBT for Eclipse, through the Nios II menu. In the Linux operating system, you start Intel Quartus Prime Programmer from the Intel Quartus Prime software. The project directory for your hardware design contains an SRAM Object File ( .sof ) along with the . file contains the hardware design to be sopcinfo file. The . sof programmed in the FPGA. Related Information Intel Quartus Prime Programmer For more information about programming an FPGA with Intel Quartus Prime Programmer. 3.2.6. Running the Project on Nios II Hardware This section describes how to run a Nios II program using the Nios II SBT for Eclipse on Nios II hardware, such as an Intel FPGA development board. If your project was created with version 10.1 or earlier of the Nios II SBT, you must Note: re-import it to create the Nios II launch configuration correctly. , To run a software project, right-click the application project name, point to Run As . To run a software project as a ModelSim simulation, Nios II Hardware and click Nios II Run As , and click right-click the application project name, point to . ModelSim This command carries out the following actions: • Creates a Nios II run configuration. • Builds the project executable. If all target files are up to date, nothing is built. • Establishes communications with the target, and verifies that the FPGA is configured with the correct hardware design. • ) to the target memory Downloads the Executable and Linking Format File ( .elf • elf entry point. Starts execution at the . Program output appears in the Nios II Console view. The Nios II Console view maintains a terminal I/O connection with a communication device connected to the Nios II processor in the hardware system, such as a JTAG UART. When the Nios II ® Nios II Software Developer's Handbook Send Feedback 30

31 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 stderr , the Nios II Console view displays the text. The program writes to stdout or Nios II Console view can also accept character input from the host keyboard, which is . sent to the processor and read as stdin icon in the Nios II To disconnect the terminal from the target, click the Terminate Console view. Terminating only disconnects the host from the target. The target processor continues executing the program. Related Information on page 48 • Run Configurations in the SBT for Eclipse For more information about about run configurations. • Lauterbach GmbH Website For more information about the Nios II instruction set. 3.2.7. Debugging the Project on Nios II Hardware This section describes how to debug a Nios II program using the Nios II SBT for Eclipse on Nios II hardware, such as an Intel FPGA development board. Debug To debug a software project, right-click the application project name, point to As , and click . This command carries out the following actions: Nios II Hardware • Creates a Nios II run configuration. • Builds the project executable. If all target files are up to date, nothing is built. • If debugging on hardware, establishes communications with the target, and verifies that the FPGA is configured with the correct hardware design. elf • Downloads the . to the target memory. • . Sets a breakpoint at the top of main() elf entry point. • Starts execution at the . The Eclipse debugger with the Nios II plugins provides a Nios II perspective, allowing you to perform many common debugging tasks. Debugging a Nios II program with the Nios II plugins is generally the same as debugging any other C/C++ program with Eclipse and the CDT plugins. For information about debugging with Eclipse and the CDT plugins, refer to the Eclipse help system. Related Information on page 48 Run Configurations in the SBT for Eclipse For more information about about run configurations. 3.2.7.1. List of Debugging Tasks with the Nios II SBT for Eclipse The debugging tasks you can perform with the Nios II SBT for Eclipse include the following tasks: ® Nios II Software Developer's Handbook Send Feedback 31

32 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 • Controlling program execution with commands such as: — Suspend (pause) — Resume — Terminate — Step Into — Step Over — Step Return • Setting breakpoints and watchpoints • Viewing disassembly • Instruction stepping mode • Displaying and changing the values of local and global variables in the following formats: — Binary — Decimal — Hexadecimal • Displaying watch expressions • Viewing and editing registers in the following formats: — Binary — Decimal — Hexadecimal • Viewing and editing memory in the following formats: — Hexadecimal — ASCII — Signed integer — Unsigned integer view • Viewing stack frames in the Debug 3.2.7.1.1. Console View Just as when running a program, Eclipse displays program output in the Console view of Eclipse. The Console view maintains a terminal I/O connection with a communication device connected to the Nios II processor in the hardware system, or stderr, the such as a JTAG UART. When the Nios II program writes to stdout Console view displays the text. The Console view can also accept character input from the host keyboard, which is sent to the processor and read as . stdin 3.2.7.1.2. Disconnecting the Terminal from the Target icon in the Console To disconnect the terminal from the target, click the Terminate view. Terminating only disconnects the host from the target. The target processor continues executing the program. If your project was created with version 10.1 or earlier of the Nios II SBT, you must Note: re-import it to create the Nios II launch configuration correctly. ® Nios II Software Developer's Handbook Send Feedback 32

33 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.2.7.2. Using the Intel FPGA Bytestream Console The Intel FPGA bytestream console enables you to see output from the processor's stderr device. The function of the and devices, and send input to its stdout stdin Intel FPGA bytestream console is similar to the nios2-terminal command-line utility. Console Open the Intel FPGA bytestream console in the Eclipse view the same way as any other Eclipse console, by clicking the Open Console button. When you open the Intel FPGA bytestream console, the Bytestream Console dialog box shows you a list of available bytestreams. This is the same set of Selection bytestreams recognized by System Console. Select the bytestream connected to the processor you are debugging. You can send characters to the processor's stdin device by typing in the bytestream console. Be aware that console input in buffered on a line-by-line basis. Therefore, the processor does not receive any characters until you press the Enter key. A bytestream device can support only one connection at a time. You must close the Note: Intel FPGA bytestream console before attempting to connect to the processor with the nios2-terminal utility, and vice versa. Related Information Analyzing and Debugging Designs with the System Console For more information about how System Console recognizes bytestreams. 3.2.7.3. Run Time Stack Checking And Exception Debugging To enable extra exception information, navigate to Nios II Advanced Features Exception Checking ➤ MegaWizard ➤ Extra Information ; and recompile the HW project and regenerate the BSP in the Nios II SBT for Register Eclipse. 1. Enable the Run Time Stack Checking in the BSP project from NIOS II SBT for Eclipce Nios II BSP Editor. From the BSP project, right-click and navigate to Nios ➤ ➤ Advanced ➤ hal ➤ II BSP editor Settings enable_run_time_stack_checking . 2. Rebuild BSP and software. 3. Ensure that the FPGA is configured. 4. ➤ Nios II Hardware . Debug As Start the Debug Session by navigating to 5. Run the Software. 3.2.7.3.1. Nios II Exception Debugging To allow easier debugging of Nios II exceptions, first enable the extra exception information in the Nios II. Note: This is already enabled if you have an MMU. Also you can navigate to Nios II MegaWizard ➤ Advanced Features Exception ➤ Extra Information Register . Checking There are other options you can choose, like unimplemented instructions. Note: ® Nios II Software Developer's Handbook Send Feedback 33

34 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 When an exception is hit, the cause value in the Nios II Exception Register can be decoded using the Nios II Exceptions (In Decreasing Priority Order) table from the Nios II Processor Reference Handbook . Note: This table only provides the general cause. Related Information • Nios II Classic Processor Reference Handbook For more information about the Exception Register Decode Table, refer to the "Exception Overview" chapter in the "Programming Model" section. • Nios II Processor Reference Handbook For more information about the Exception Register Decode Table, refer to the "Exception Overview" chapter in the "Programming Model" section. • Nios II Classic Processor Reference Handbook For more information about the Exception Register Description, refer to the "The exception Register" chapter in the "Programming Model" section. • Nios II Processor Reference Handbook For more information about the Exception Register Description, refer to the "The exception Register" chapter in the "Programming Model" section. 3.2.7.3.2. Stack Overflow Settings Stack Checking To enable , go to the BSP Editor and click on the tab, click enable_runtime_stack_checking , and then click on . When the Advanced, hal is enabled, extra code is added at the start of each function call to: Stack Checking • Check the current value of the stack pointer , which is stored in the Exception Temp (ET) max stack size • Compare this to the Register If the stack pointed to is outside of the valid range, the software branches and calls a instruction. This is seen by the Debug Control module. “break 3” and can detect heap exhaustion, as well. With stack checking on, new() malloc() Note: Example 1. Example of function with stack checking code ___vfprintf_internal_r: 000002ec: addi sp,sp,-1308 000002f0: bgeu sp,et,0x2f8 <___vfprintf_internal_r+12> 000002f4: break 3 and break 3 lines are what is added for the stack overflow checking. If the bgeu The is called. stack pointer has grown beyond its limits the break Related Information Embedded Design Handbook For more information, refer to the "Stack Overflow" chapter of the Embedded Design Handbook. Recognizing and Debugging a Stack Overflow When a stack overflow occurs, having registered an instruction-related exception handler helps you identify it by its behavior. Default Instruction-Related Exception Handler ® Nios II Software Developer's Handbook Send Feedback 34

35 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 The default value for an instruction-related exception handler is when it is not registered. “break 3” If you don’t register an instruction-related exception handler, the instruction is picked up by the software trap logic and a break is passed to the debugger. You must roll back through the history in the debugger to find the memory operation that triggered the stack checking break. malloc() and can detect heap exhaustion. With stack checking on, Note: new() How to Isolate the Cause of a Sigtrap sigtrap How to isolate the cause of a seen in the debugger with no instruction-related exception handle? sigtrap : The Debugger breaks with 1. Use the thread view in the debug window and select the last state. This is the highest number. The last thread will be the actual call than overflowed. 2. Switch to instruction stepping mode in the debugger by pressing the i-> button in the debug window, which opens the memory disassembly view. If there has been a stack overflow the disassembly view should show execution after the stack check: pointing to a break3 ___vfprintf_internal_r: 000002ec: addi sp,sp,-1308 000002f0: bgeu sp,et,0x2f8 <___vfprintf_internal_r+12> 000002f4: break 3 sp and which holds the max stack side in the Nios II register 3. Check the value of et view. vs et . 4. Move to the prior state in the debug window and re-check sp ® Nios II Software Developer's Handbook Send Feedback 35

36 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Figure 2. Nios II Debug window Custom Instruction-Related Exception Handler For use outside the debugger, you can register your own instruction-related exception handler which is called when the break (or any exception) is seen. On an exception, including overflow, the HAL calls the instruction-related exception handler, passing in the cause field from the exception register, and the address which caused the exception. At this point, it is up to you to decide what do. For more information about how to register an instruction-related exception, refer to Related Information on page 244 • Exception Handling This chapter provides more information about the details on how to register an instruction-related exception. on page 280 • Writing an Instruction-Related Exception Handler This chapter provides more information about the details on how to register an instruction-related exception. on page 281 • Registering an Instruction-Related Exception Handler This chapter provides more information about the details on how to register an instruction-related exception. • Programming Model Nios II For more information, refer to the "Programming Model" chapter of the . Processor Reference Handbook ® Nios II Software Developer's Handbook Send Feedback 36

37 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.2.8. Creating a Simple BSP You create a BSP with default settings using the Nios II Board Support Package and click Nios II Board wizard. To start the wizard, on the menu, point to File New Support Package . The Nios II Board Support Package wizard enables you to specify the following BSP parameters: • The name • The underlying hardware design • The location • The operating system and version You can select the operating system only at the time you create the BSP. To change operating systems, you must create a new BSP by using the Additional arguments to nios2-bsp the script. ™ If you intend to run the project in the Nios II ModelSim simulation environment, use the Additional arguments parameter to specify the location of the test bench ). The file is located in the Intel Quartus .spd simulation package descriptor file ( .spd --set Prime project directory. Specify the path as follows: . QUARTUS_PROJECT_DIR= Note: Intel FPGA recommends that you use a relative path name, to ensure that the location of your project is independent of the installation directory. After you have created the BSP, you have the following options for GUI-based BSP editing: • To access and modify basic BSP properties, right-click the BSP project, click ➤ Nios II BSP Properties . Properties • To modify parameters and settings in detail using the Nios II BSP Editor, refer to . Using the BSP Editor Related Information on page 40 • Using the BSP Editor For more information on how to modify parameters and settings in detail using the Nios II BSP Editor. • Nios II Software Build Tools For more information about nios2-bsp command arguments. 3.3. Makefiles and the Nios II SBT for Eclipse The Nios II SBT for Eclipse creates and manages the makefiles for Nios II software projects. When you create a project, the Nios II SBT creates a makefile based on the source content you specify and the parameters and settings you select. When you modify the project in Eclipse, the Nios II SBT updates the makefile to match. Details of how each makefile is created and maintained vary depending on the project type, and on project options that you control. The authoritative specification of project contents is always the makefile, regardless how it is created or updated. ® Nios II Software Developer's Handbook Send Feedback 37

38 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 By default, the Nios II SBT manages the list of source files in your makefile, based on actions you take in Eclipse. However, in the case of applications and libraries, you have the option to manage sources manually. Both styles of source management are discussed in the following sections. 3.3.1. Eclipse Source Management Nios II application and user library makefiles are based on source files and properties that you specify directly. Eclipse source management allows you to add and remove source files with standard Eclipse actions, such as dragging a source file into and out of the Project Explorer view and adding a new source file through the File menu. You can examine and modify many makefile properties in the Nios II Application Properties Nios II Library Properties dialog box. To open the dialog box, right- or ➤ Nios II Application Properties or Properties click the project, click Properties . ➤ Nios II Library Properties 3.3.1.1. Modifying a Makefile with Eclipse Source Management Table 3. GUI Actions that Modify an Application or Makefile with Eclipse Source Management Where Modified Modification Specifying the application or user library name Nios II Application Properties or Nios II Library Properties dialog box. For more information, refer to the Eclipse help system. Adding or removing source files dialog box. Specifying a path to an associated BSP Project References dialog box. Project References Specifying a path to an associated user library Nios II Library Properties Nios II Application Properties or Enabling, disabling or modifying compiler options dialog box. After the SBT has created a makefile, you can modify the makefile in the following ways: • With the Nios II SBT for Eclipse. • With Nios II SBT commands from the Nios II Command Shell. When modifying a makefile, the SBT preserves any previous nonconflicting modifications, regardless how those modifications were made. After you modify a makefile with the Nios II Command Shell, in Eclipse you must Update linked resource to keep the Eclipse project right-click the project and click view in step with the makefile. When the Nios II SBT for Eclipse modifies a makefile, it locks the makefile to prevent corruption by other processes. You cannot edit the makefile from the command line until the SBT has removed the lock. If you want to exclude a resource (a file or a folder) from the Nios II makefile Remove from Nios temporarily, without deleting it from the project, you can use the II Build command. Right-click the resource and click Remove from Nios II Build . When a resource is excluded from the build, it does not appear in the makefile, and ® Nios II Software Developer's Handbook Send Feedback 38

39 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Eclipse ignores it. However, it is still visible in the Project Explorer, with a modified icon. To add the resource back into the build, right-click the resource and click Add to Nios II Build . Do not use the Eclipse Note: Exclude from build command. With Nios II software projects, you must use the and Add to Nios II Build commands Remove from Nios II Build instead. 3.3.1.2. Absolute Source Paths and Linked Resources By default, the source files for an Eclipse project are stored under the project directory. If your project must incorporate source files outside the project directory, you can add them as linked resources. An Eclipse linked resource can be either a file or a folder. With a linked folder, all source files in the folder and its subfolders are included in the build. When you add a linked resource (file or folder) to your project, the SBT for Eclipse adds the file or folder to your makefile with an absolute path name. You might use a linked resource to refer to common source files in a fixed location. In this situation, you can move the project to a different directory without disturbing the common source file references. A linked resource appears with a modified icon (green dot) in the Project Explorer, to distinguish it from source files and folders that are part of the project. You can use the Eclipse debugger to step into a linked source file, exactly as if it were part of the project. You can reconfigure your project to refer to any linked resource either as an individual file, or through its parent folder. Right-click the linked resource and click Update . Linked Resource and Add to Nios II Build commands You can use the Remove from Nios II Build with linked resources. When a linked resource is excluded from the build, its icon is modified with a white dot. You can use Eclipse to create a path variable, defining the location of a linked resource. A path variable makes it easy to modify the location of one or more files in your project. For information about working with path variables and creating linked resources, refer to the Eclipse help system. 3.3.2. User Source Management You can remove a makefile from source management control through the Nios II dialog box. Application Properties Nios II Library Properties or to convert the makefile to user source Simply turn off Enable source management management. When is off, you must update your Enable source management makefile manually to add or remove source files to or from the project. The SBT for Eclipse makes no changes to the list of source files, but continues to manage all other project parameters and settings in the makefile. ® Nios II Software Developer's Handbook Send Feedback 39

40 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.3.2.1. Modifying a Makefile with User Source Management Editing a makefile manually is an advanced technique. Intel FPGA recommends that you avoid manual editing. The SBT provides extensive capabilities for manipulating makefiles while ensuring makefile correctness. In a makefile with user-managed sources, you can refer to source files with an absolute path. You might use an absolute path to refer to common source files in a fixed location. In this situation, you can move the project to a different directory without disturbing the common source file references. Projects with user-managed sources do not support the following features: • Linked resources Add to Nios II Build command • The • The Remove from Nios II Build command GUI Actions that Modify an Application or a Makefile with User Source Table 4. Management Modification Where Modified Nios II Library Properties Nios II Application Properties Specifying the application or user library name or dialog box Project References Specifying a path to an associated BSP dialog box Specifying a path to an associated user library Project References dialog box or Nios II Library Properties Enabling, disabling or modifying compiler options Nios II Application Properties dialog box With user source management, the source files shown in the Eclipse Project Explorer Note: view do not necessarily reflect the sources built by the makefile. To update the Project Explorer view to match the makefile, right-click the project and click Sync from Nios II Build . 3.3.3. BSP Source Management Nios II BSP makefiles are handled differently from application and user library makefiles. BSP makefiles are based on the operating system, BSP settings, selected software packages, and selected drivers. You do not specify BSP source files directly. BSP makefiles must be managed by the SBT, either through the BSP Editor or through the SBT command-line utilities. Related Information Using the BSP Editor on page 40 For more information about specifying BSPs 3.4. Using the BSP Editor Typically, you create a BSP with the Nios II SBT for Eclipse. The Nios II plugins provide the basic tools and settings for defining your BSP. For more advanced BSP editing, use the Nios II BSP Editor. The BSP Editor provides all the tools you need to create even the most complex BSPs. ® Nios II Software Developer's Handbook Send Feedback 40

41 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.4.1. Tcl Scripting and the Nios II BSP Editor The Nios II BSP Editor provides support for Tcl scripting. When you create a BSP in the BSP Editor, the editor can run a Tcl script that you specify to supply BSP settings. You can also export a Tcl script from the BSP Editor, containing all the settings in an existing BSP. By studying such a script, you can learn about how BSP Tcl scripts are constructed. 3.4.2. Starting the Nios II BSP Editor You start the Nios II BSP Editor in one of the following ways: Nios II , and click BSP Editor . The editor • Right-click an existing project, point to .bsp loads the BSP Settings File ( ) associated with your project, and is ready to update it. . The editor starts without loading • On the Nios II menu, click Nios II BSP Editor bsp file. a . Properties dialog Properties • Right-click an existing BSP project and click . In the BSP Editor . The editor loads your . bsp box, select Nios II BSP Properties ➤ file for update. 3.4.3. The Nios II BSP Editor Screen Layout The Nios II BSP Editor screen is divided into two areas. The top area is the command area, and the bottom is the console area. The details of the Nios II BSP Editor screen areas are described in this section. Below the console area is the Generate button. This button is enabled when the BSP settings are valid. It generates the BSP target files, as shown in the Target BSP tab. Directory 3.4.4. The Command Area In the command area, you specify settings and other parameters defining the BSP. The command area contains several tabs: • The tab Main • The tab Software Packages tab • The Drivers Linker Script • The tab tab Enable File Generation • The • The Target BSP Directory tab , along with Each tab allows you to view and edit a particular aspect of the . bsp relevant command line parameters and Tcl scripts. Main Software Packages and Drivers tabs are the , The settings that appear on the same as the settings you manipulate on the command line. Related Information Nios II Software Build Tools Reference on page 396 ® Nios II Software Developer's Handbook Send Feedback 41

42 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.4.4.1. The Main Tab The tab presents general settings and parameters, and operating system Main settings, for the BSP. The BSP includes the following settings and parameters: • The path to the . sopcinfo file specifying the target hardware • The processor name • The operating system and version Note: You cannot change the operating system in an existing BSP. You must create a new BSP based on the desired operating system. • The BSP target directory—the destination for files that the SBT copies and creates for your BSP. • BSP settings Common BSP settings appear in a tree structure. Settings are organized into and Advanced categories. Settings are further organized into functional groups. The available settings depend on the operating system. When you select a group of settings, the controls for those settings appear in the pane to the right of the tree. When you select a single setting, the pane shows the setting control, the full setting name, and the setting description. Related Information • The Software Packages Tab on page 42 • The Drivers Tab on page 43 For more information about how the software package and driver settings are presented separately. 3.4.4.2. The Software Packages Tab Software Packages The tab allows you to insert and remove software packages in your BSP, and control software package settings. tab is the software package table, listing each Software Packages At the top of the available software package. The table allows you to select the software package version, and enable or disable the software package. The operating system determines which software packages are available. Many software packages define settings that you can control in your BSP. When you enable a software package, the available settings appear in a tree structure, organized and Advanced settings. into Common When you select a group of settings, the controls for those settings appear in the pane to the right of the tree. When you select a single setting, the pane shows the setting control, the full setting name, and the setting description. Enabling and disabling software packages and editing software package settings can have a profound impact on BSP behavior. Refer to the documentation for the specific software package for details. ® Nios II Software Developer's Handbook Send Feedback 42

43 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Related Information • The Drivers Tab on page 43 For more information about how the software package and driver settings are presented separately. on page 42 • The Main Tab • Read-Only Zip File System on page 306 For more information about the read-only zip file system. on page 296 • Ethernet and the NicheStack TCP/IP Stack 3.4.4.3. The Drivers Tab The Drivers tab allows you to select, enable, and disable drivers for devices in your system, and control driver settings. Drivers At the top of the tab is the driver table, mapping components in the hardware system to drivers. The driver table shows components with driver support. Each component has a module name, module version, module class name, driver name, and driver version, determined by the contents of the hardware system. The table allows you to select the driver by name and version, as well as to enable or disable each driver. When you select a driver version, all instances of that driver in the BSP are set to the version you select. Only one version of a given driver can be used in an individual BSP. Many drivers define settings that you can control in your BSP. Available driver settings Common and appear in a tree structure below the driver table, organized into Advanced settings. When you select a group of settings, the controls for those settings appear in the pane to the right of the tree. When you select a single setting, the pane shows the setting control, the full setting name, and the setting description. Enabling and disabling device drivers, changing drivers and driver versions, and editing driver settings, can have a profound impact on BSP behavior. Refer to the relevant component documentation and driver information for details. Related Information on page 42 • The Software Packages Tab on page 42 • The Main Tab • Embedded Peripherals IP User Guide For more information about Intel FPGA components. 3.4.4.4. The Linker Script Tab tab allows you to view available memory in your hardware system, The Linker Script and examine and modify the arrangement and usage of linker regions in memory. When you make a change to the memory configuration, the SBT validates your change. Rearranging linker regions and linker section mappings can have a very significant Note: impact on BSP behavior. ® Nios II Software Developer's Handbook Send Feedback 43

44 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Related Information The Problems Tab on page 46 tab in the console area. If there is a problem, a message appears in the Problems 3.4.4.4.1. Linker Section Mappings Linker Section Mappings table shows the tab, the Linker Script At the top of the mapping from linker sections to linker regions. You can edit the BSP linker section mappings using the following buttons located next to the linker section table: button Add • Add —Adds a linker section mapping to an existing linker region. The opens the Add Section Mapping dialog box, where you specify a new section name and an existing linker region. —Removes a mapping from a linker section to a linker region. • Remove • Restore Defaults —Restores the section mappings to the default configuration set up at the time of BSP creation. 3.4.4.4.2. Linker Regions Linker Memory Regions At the bottom of the Linker Script tab, the table shows all defined linker regions. Each row of the table shows one linker region, with its address range, memory device name, size, and offset into the selected memory device. You reassign a defined linker region to a different memory device by selecting a column. The Size and Offset different device name in the Memory Device Name columns are editable. You can also edit the list of linker regions using the following buttons located next to the linker region table: Add • Add —Adds a linker region in unused space on any existing device. The dialog box, where you specify the button opens the Add Memory Region memory device, the new memory region name, the region size, and the region's offset from the device base address. • Remove —Removes a linker region definition. Removing a region frees the region's memory space to be used for other regions. —Creates a linker region representing a memory device that • Add Memory Device is outside the hardware system. The button launches the Add Memory Device dialog box, where you can specify the device name, memory size and base address. After you add the device, it appears in the linker region table, the Memory Map dialog box. This Memory Device Usage Table dialog box, and the functionality is equivalent to the add_memory_device Tcl command. Note: Ensure that you specify the correct base address and memory size. If the base address or size of an external memory changes, you must edit the BSP manually to match. The SBT does not automatically detect changes in external memory devices, even if you update the BSP by creating a new settings file. ® Nios II Software Developer's Handbook Send Feedback 44

45 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 • Restore Defaults —restores the memory regions to the default configuration set up at the time of BSP creation. —Opens the Memory Memory Device Usage Table • Memory Usage . The allows you to view memory device usage by defined Device Usage Table memory region. As memory regions are added, removed, and adjusted, each device's free memory, used memory, and percentage of available memory are updated. The rightmost column is a graphical representation of the device’s usage, according to the memory regions assigned to it. dialog box. The memory map allows you Memory Map —Opens the • Memory Map Device to view a map of system memory in the processor address space. The table is a read-only reference showing memories in the hardware system that are mastered by the selected processor. Devices are listed in memory address order. Device To the right of the table is a graphical representation of the processor's memory space, showing the locations of devices in the table. Gaps indicate unmapped address space. This representation is not to scale. Note: Related Information on page 396 Nios II Software Build Tools Reference 3.4.4.5. Enable File Generation Tab tab allows you to take ownership of specific BSP files Enable File Generation The that are normally generated by the SBT. When you take ownership of a BSP file, you Enable can modify it, and prevent the SBT from overwriting your modifications. The File Generation tab shows a tree view of all target files to be generated or copied when the BSP is generated. To disable generation of a specific file, expand the software component containing the file, expand any internal directory folders, select the file, and right-click. Each disabled file appears in a list at the bottom of the tab. This functionality is equivalent to the Tcl command. set_ignore_file Note: If you take ownership of a BSP file, the SBT can no longer update it to reflect future changes in the underlying hardware. If you change the hardware, be sure to update the file manually. Related Information on page 396 Nios II Software Build Tools Reference 3.4.4.6. Target BSP Directory Tab tab is a read-only reference showing you what output to The Target BSP Directory expect when the BSP is generated. It does not depict the actual file system, but rather the files and directories to be created or copied when the BSP is generated. Each software component, including the operating system, drivers, and software packages, specifies source code to be copied into the BSP target directory. The files are generated in the directory specified on the Main tab. When you generate the BSP, existing BSP files are overwritten, unless you disable tab. generation of the file in the Enable File Generation ® Nios II Software Developer's Handbook Send Feedback 45

46 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.4.5. The Console Area The console area shows results of settings and commands that you select in the command area. The console area consists of the following tabs: • The tab Information • The Problems tab Processing tab • The 3.4.5.1. The Information Tab Information tab shows a running list of high-level changes you make to your The BSP, such as adding a software package or changing a setting value. 3.4.5.2. The Problems Tab The tab shows warnings and errors that impact or prohibit BSP creation. Problems For example, if you inadvertently specify an invalid linker section mapping, a message Problems appears in the tab. 3.4.5.3. The Processing Tab tab shows files and folders created and Processing When you generate your BSP, the copied in the BSP target directory. 3.4.6. Exporting a Tcl Script When you have configured your BSP to your satisfaction, you can export the BSP settings as a Tcl script. This feature allows you to perform the following tasks: • Regenerate the BSP from the command line • Recreate the BSP as a starting point for a new BSP • Recreate the BSP on a different hardware platform • Examine the Tcl script to improve your understanding of Tcl command usage The exported Tcl script captures all BSP settings that you have changed since the previous time the BSP settings file was saved. If you export a Tcl script after creating a new BSP, the script captures all nondefault settings in the BSP. If you export a Tcl script after editing a pre-existing BSP, the script captures your changes from the current editing session. To export a Tcl script, in the Tools menu, click Export Tcl Script , and specify a . tcl filename and destination path. The file extension is . You can later run your exported script as a part of creating a new BSP. Related Information • Using a Tcl Script in BSP Creation on page 47 For more information about how to run a Tcl script during BSP creation. on page 115 • Revising Your BSP For more information about default BSP settings and recreating and regenerating BSPs. ® Nios II Software Developer's Handbook Send Feedback 46

47 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.4.7. Creating a New BSP To create a BSP in the Nios II BSP Editor, use the command in the File New BSP New BSP menu to open the dialog box. This dialog box controls the creation of a new BSP settings file. The BSP Editor loads this new BSP after the file is created. In this dialog box, you specify the following parameters: • sopcinfo file defining the hardware platform. The . • The CPU name of the targeted processor. • The BSP type and version. Note: You can select the operating system only at the time you create the BSP. To change operating systems, you must create a new BSP. • The operating system version. • bsp The name of the BSP settings file. It is created with file extension . . • Absolute or relative path names in the BSP settings file. By default, relative paths are enabled for filenames in the BSP settings file. • An optional Tcl script that you can run to supply additional settings. Normally, you specify the path to your . file relative to the BSP directory. sopcinfo This enables you to move, copy and archive the hardware and software files together. file, or specify an absolute path, the Nios II BSP If you browse to the . sopcinfo Editor offers to convert your path to the relative form. 3.4.7.1. Using a Tcl Script in BSP Creation When you create a BSP, the New BSP Settings File dialog box allows you to specify the path and filename of a Tcl script. The Nios II BSP Editor runs this script after all other BSP creation steps are done, to modify BSP settings. This feature allows you to perform the following tasks: • Recreate an existing BSP as a starting point for a new BSP • Recreate a BSP on a different hardware platform • Include custom settings common to a group of BSPs The Tcl script can be created by hand or exported from another BSP. Related Information on page 46 • Exporting a Tcl Script For more information about how to create a Tcl script from an existing BSP. • Nios II Software Build Tools on page 86 For more information about Tcl scripts and BSP settings, refer to "Tcl Scripts for BSP Settings". 3.4.8. BSP Validation Errors If you modify a hardware system after basing a BSP on it, some BSP settings might no longer be valid. This is a very common cause of BSP validation errors. Eliminating these errors usually requires correcting a large number of interrelated settings. ® Nios II Software Developer's Handbook Send Feedback 47

48 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 If your modifications to the underlying hardware design result in BSP validation errors, the best practice is to update or recreate the BSP. Updating and recreating BSPs is very easy with the BSP Editor. If you recreate your BSP, you might find it helpful to capture your old BSP settings by exporting them to a Tcl script. You can edit the Tcl script to remove any settings that are incompatible with the new hardware design. Related Information • Using a Tcl Script in BSP Creation on page 47 For more information about how to run a Tcl script during BSP creation. on page 46 • Exporting a Tcl Script For more information about how to create a Tcl script from an existing BSP. • Nios II Software Build Tools on page 86 For more information about Tcl scripts and BSP settings, refer to "Tcl Scripts for BSP Settings". 3.5. Run Configurations in the SBT for Eclipse Eclipse uses run configurations to control how it runs and debugs programs. Run configurations in the Nios II SBT for Eclipse have several features that help you debug Nios II software running on FPGA platforms. 3.5.1. Opening the Run Configuration Dialog Box You can open the run configuration dialog box two ways: Run As , and click • You can right-click an application, point to Run Configurations . , and click Debug • You can right-click an application, point to Debug As . Configurations Depending on which way you opened the run configuration dialog box, the title is Debug Configuration Run Configuration . However, both views show the or either same run configurations. If your project was created with version 10.1 or earlier of the Nios II SBT, you must Note: re-import it to create the Nios II launch configuration correctly. Each run configuration is presented on several tabs. This section describes each tab. 3.5.2. The Project Tab button opens On this tab, you specify the application project to run. The Advanced dialog box. In this dialog box, you can control Nios II ELF Section Properties the the runtime parameters in the following ways: ® Nios II Software Developer's Handbook Send Feedback 48

49 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 • Specify the processor on which to execute the program (if the hardware design provides multiple processors) • Specify the device to use for standard I/O • Specify the expected location, timestamp and value of the system ID • Specify the path to the Intel Quartus Prime JTAG Debugging Information File .jdi ) ( • Enable or disable profiling The Nios II SBT for Eclipse sets these parameters to reasonable defaults. Do not modify them unless you have a clear understanding of their effects. 3.5.3. The Target Connection Tab This tab allows you to control the connection between the host machine and the target hardware in the following ways: • Select the cable, if more than one cable is available • Allow software to run despite a system ID value or timestamp that differs from the hardware • Reset the processor when the software is downloaded The System ID Properties button allows you to examine the system ID and timestamp in both the . elf file and the hardware. This can be helpful when you need to analyze the cause of a system ID or timestamp mismatch. 3.5.4. The Debugger Tab In this tab, you optionally enable the debugger to halt at a specified entry point. 3.6. Optimizing Project Build Time When you build a Nios II project, the project makefile builds any components that are unbuilt or out of date. For this reason, the first time you build a project is normally the slowest. Subsequent builds are fast, only rebuilding sources that have changed. ® host compile and build time performance has With Nios II Gen 2, the Windows improved. For example, it is now three times faster to build the example. webserver 3.7. Importing a Command-Line Project If you have software projects that were created with the Nios II SBT command line, you can import the projects into the Nios II SBT for Eclipse for debugging and further development. This section discusses the import process. Your command-line C/C++ application, and its associated BSP, is created on the command line. Any Nios II SBT command-line project is ready to import into the Nios II SBT for Eclipse. No additional preparation is necessary. ® Nios II Software Developer's Handbook Send Feedback 49

50 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.7.1. Nios II Command-Line Projects The Nios II SBT for Eclipse imports the following kinds of Nios II command-line projects: • Command-line C/C++ application project • Command-line BSP project • Command-line user library project You can edit, build, debug, and manage the settings of an imported project exactly the same way you edit, build, debug, and manage the settings of a project created in Nios II SBT for Eclipse. 3.7.2. Importing through the Import Wizard The Nios II SBT for Eclipse imports each type of project through the wizard. Import Import The wizard determines the kind of project you are importing, and configures it appropriately. You can continue to develop project code in your SBT project after importing the project into Eclipse. You can edit source files and rebuild the project, using the SBT either in Eclipse or on the command line. Related Information Getting Started from the Command Line on page 72 For more information about creating projects with the command line. 3.7.3. Road Map Importing and debugging a project typically involves several of the following tasks. You do not need to perform these tasks in this order, and you can repeat or omit some tasks, depending on your needs. • Import a command-line C/C++ application • Import a supporting project • Debug a command-line C/C++ application • Edit command-line C/C++ application code When importing a project, the SBT for Eclipse might make some minor changes to your makefile. If the makefile refers to a source file located outside the project directory tree, the SBT for Eclipse treats that file as a linked resource. However, it does not add or remove any source files to or from your makefile. When you import an application or user library project, the Nios II SBT for Eclipse allows you to choose Eclipse source management or user source management. Unless your project has an unusual directory structure, choose Eclipse source management, to allow the SBT for Eclipse to automatically maintain your list of source files. You debug and edit an imported project exactly the same way you debug and edit a project created in Eclipse. ® Nios II Software Developer's Handbook Send Feedback 50

51 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.7.4. Import a Command-Line C/C++ Application To import a command-line C/C++ application, perform the following steps: 1. Start the Nios II SBT for Eclipse. dialog box appears. 2. On the File menu, click . The Import Import Import folder, and select Nios II Software Build Tools Project 3. Expand the Nios II Software Build Tools Project . wizard appears. Next . The 4. Click File Import 5. Click Browse and locate the directory containing the C/C++ application project to import. . The wizard fills in the project path. 6. Click OK 7. Specify the project name in the Project name box. Note: project file at: You might see a warning saying "There is already a . path < >". This warning indicates that the directory already contains an Eclipse project. Either it is an Eclipse project, or it is a command-line project that is already imported into Eclipse. If the project is already in your workspace, do not re-import it. . The wizard imports the application project. Finish 8. Click After you complete these steps, the Nios II SBT for Eclipse can build, debug, and run the complete program, including the BSP and any libraries. The Nios II SBT for Eclipse builds the project using the SBT makefiles in your imported C/C++ application project. Eclipse displays and steps through application source code exactly as if the project were created in the Nios II SBT for Eclipse. However, Eclipse does not have direct information about where BSP or user library code resides. If you need to view, debug or step through BSP or user library source code, you need to import the BSP or user library. Related Information on page 51 Import a Supporting Project For more information about the process of importing supporting projects, such as BSPs and libraries. 3.7.4.1. Importing a Project with Absolute Source Paths If your project uses an absolute path to refer to a source file, the SBT for Eclipse imports that source file as a linked resource. In this case, the import wizard provides a page where you can manage how Eclipse refers to the source: as a file, or through a parent directory. Related Information Absolute Source Paths and Linked Resources on page 39 For more information about managing linked resources. 3.7.5. Import a Supporting Project While debugging a C/C++ application, you might need to view, debug or step through source code in a supporting project, such as a BSP or user library. To make supporting project source code visible in the Eclipse debug perspective, you need to import the supporting project. ® Nios II Software Developer's Handbook Send Feedback 51

52 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 If you do not need BSP or user library source code visible in the debugger, you can skip this task, and proceed to debug your project exactly as if you had created it in Eclipse. If you have several C/C++ applications based on one BSP or user library, import the BSP or user library once, and then import each application that is based on the BSP or user library. Each application's makefile contains the information needed to find and build any associated BSP or libraries. Related Information on page 51 Import a Command-Line C/C++ Application For more information about the steps for importing a supporting project. 3.7.6. User-Managed Source Files When you import a Nios II application or user library project, the Nios II SBT for Eclipse offers the option of user source management. User source management is helpful if you prefer to update your makefile manually to reflect source files added to or removed from the project. With user source management, Eclipse never makes any changes to the list of source files in your makefile. However, the SBT for Eclipse manages all other project parameters and settings, just as with any other Nios II software project. If your makefile refers to a source file with an absolute path, when you import with user source management, the absolute path is untouched, like any other source path. You might use an absolute path to refer to common source files in a fixed location. In this situation, you can move the project to a different directory without disturbing the common source file references. User source management is not available with BSP projects. BSP makefiles are based on the operating system, BSP settings, selected software packages, and selected drivers. You do not specify BSP source files directly. Related Information User Source Management on page 39 For more information about how the SBT for Eclipse handles makefiles with user- managed sources. 3.8. Packaging a Library for Reuse This section shows how to create and use a library archive file ( .a ) in the Nios II Software Build Tools for Eclipse. This technique enables you to provide a library to another engineer or organization without providing the C source files. This process entails two tasks: 1. Create a Nios II user library 2. Create a Nios II application project based on the user library 3.8.1. Creating the User Library To create a user library, perform the following steps: ® Nios II Software Developer's Handbook Send Feedback 52

53 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Nios II Library . 1. In the File menu, point to New and click test_lib 2. Type a project name, for example . 3. For .c Location , browse to the directory containing your library source files ( ). .h and Finish . 4. Click libtest_lib.a ). 5. Build the project to create the .a file (in this case 3.8.2. Using the Library To use the library in a Nios II application project, perform the following steps: 1. Create your Nios II application project. 2. To set the library path in the application project, right-click the project, and click Properties . Nios II Application Properties Nios II Application Paths , next to . In 3. Expand and browse to the directory , click Application include directories Add containing your library header files. , click Add and browse to the directory 4. Next to Application library directories file. .a containing your Add Library name , click and type the library project name you selected 5. Next to when you created your user library. 6. Click OK . 7. Build your application. source files are not required to build the application As this example shows, the .c project. To hand off the library to another engineer or organization for reuse, you provide the following files: • ) Nios II library archive file ( .a • .h ) Software header files ( Related Information Creating a Project on page 27 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. ® Nios II Software Developer's Handbook Send Feedback 53

54 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 To create and exercise the example software package, perform the following steps: 1. directory in your Intel FPGA Complete Design Suite installation. For ip Locate the 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 directory, create a directory for the software package. For simplicity, this ip section refers to this directory as . 2. . In In , create a subdirectory named EXAMPLE_SW_PACKAGE , create two subdirectories named < example package >/EXAMPLE_SW_PACKAGE . and inc lib 3. example package , create a new header file >/EXAMPLE_SW_PACKAGE/inc In < named example_sw_package.h containing the following code: /* Example Software Package */ void example_sw_package(void); 4. example package >/EXAMPLE_SW_PACKAGE/lib , create a new C source file In < containing the following code: example_sw_package.c named /* Example Software Package */ #include #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 ® Nios II Software Developer's Handbook Send Feedback 54

55 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 # 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. hello_example_sw_package.c Create a new C file named 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 #include "example_sw_package.h" int main() { printf("Hello from Nios II!\n"); example_sw_package(); return 0; } 8. hello_world.c Delete from the hello_example_sw_package application project. Nios II Library 9. In the File menu, point to New and click 10. Set the project name to example_sw_package_library . 11. \EXAMPLE_SW_PACKAGE\lib , browse to For Location Note: is referenced Building the library here is required, because the resulting .a example_sw_package_sw.tcl . here by Finish . 12. Click 13. Build the example_sw_package_library project to create the library archive file. libexample_sw_package_library.a ® Nios II Software Developer's Handbook Send Feedback 55

56 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 BSP Editor to open the 14. Right-click the BSP project, point to Nios II , and click BSP Editor. 15. in the software tab, find Software Packages In the example_sw_package package table, and enable it. file, such as an incorrect *_sw.tcl If there are any errors in a software package's path that causes a file to not be found, the software package does not appear in the BSP Editor. to Generate button to regenerate the BSP. On the File menu, click 16. Click the Save save your changes to settings.bsp . Exit 17. In the File menu, click to exit the BSP Editor. 18. Build the hello_example_sw_package_bsp BSP project. 19. hello_example_sw_package application project. Build the is ready to download and execute. hello_example_sw_package.elf 3.10. Programming Flash in Intel FPGA Embedded Systems Many Nios II processor systems use external flash memory to store one or more of the following items: • Program code • Program data • FPGA configuration data • File systems Flash programmer tools allow you to program software, FPGA configuration, and application-specific binary data into flash memory devices. The tools support combining these different types of data so that they can be stored in a single flash device. In Intel Quartus Prime Standard Edition, the Nios II SBT for Eclipse provides the Nios II flash programmer GUI-based tool and associated utilities, to help you manage and program the contents of flash memory. The sections below describe how to use these tools. In Intel Quartus Prime Pro Edition, the Nios II Flash programmer has been deprecated and the Intel Quartus Prime Professional programmer should be used instead. You can access the Intel Quartus Prime Professional Programmer by performing the following steps: 1. Start the Intel Quartus Prime software. The Intel Quartus Prime Pro Edition window appears. 2. ➤ Programmer . The Programmer window appears. Select Tools . 3.10.1. Starting the Flash Programmer in the Nios II menu. You start the flash programmer by clicking Flash Programmer When you first open the flash programmer, no controls are available until you open or .flash-settings ). create a Flash Programmer Settings File ( ® Nios II Software Developer's Handbook Send Feedback 56

57 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.10.2. Creating a Flash Programmer Settings File The . file describes how you set up the flash programmer GUI to flash-settings program flash. This information includes the files to be programmed to flash, .sopcinfo file describing the hardware configuration, and the file programming a locations. You must create or open a flash programmer settings file before you can program flash. You create a flash programmer settings file through the File menu. When you click New , the New Flash Programmer Settings File dialog box appears. 3.10.2.1. Specifying the Hardware Configuration file. You can locate You specify the hardware configuration by opening a . sopcinfo sopcinfo file in either of two ways: the . • Browse to a BSP settings file. The flash programmer finds the . sopcinfo file associated with the BSP. • file. Browse directly to a . sopcinfo Once you have identified a hardware configuration, details about the target hardware appear at the top of the Nios II flash programmer screen. Hardware Connections Also at the top of the Nios II flash programmer screen is the dialog box. This dialog box allows button, which opens the Hardware Connections you to select a download cable, and control system ID behavior. Related Information The Target Connection Tab on page 49 3.10.3. The Flash Programmer Screen Layout The flash programmer screen is divided into two areas. The top area is the command area, and the bottom is the console area. The details of the flash programmer screen areas are described in this section. button. This button is enabled when the flash Below the console area is the Start programmer parameters are valid. It starts the process of programming flash. 3.10.4. The Command Area In the command area, you specify settings and other parameters defining the flash programmer settings file. The command area contains one or more tabs. Each tab represents a flash memory component available in the target hardware. Each tab allows you to view the parameters of the memory component, and view and edit the list of files to be programmed in the component. Remove buttons allow you to create and edit the list of files to be and Add The programmed in the flash memory component. box shows the commands used to generate the The File generation command .flash ) used to program flash memory. Motorola S-record Files ( File programming command box shows the commands used to program The files to flash memory. flash the . ® Nios II Software Developer's Handbook Send Feedback 57

58 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Properties dialog box, which allows you to view The Properties button opens the Properties elf and modify information about an individual file. In the case of a . , the button provides access to the project reset address, the flash base and end addresses, and the boot loader file (if any). The flash programmer determines whether a boot loader is required based on the load section. You can use the Properties text and run locations of the . dialog box to override the default boot loader configuration. 3.10.5. The Console Area The console area shows results of settings and commands that you select in the command area. The console area consists of the following tabs: tab • The Information • The tab Problems tab Processing • The 3.10.5.1. The Information Tab Information tab shows the high-level changes you make to your flash The programmer settings file. 3.10.5.2. The Problems Tab tab shows warnings and error messages about the process of flash The Problems programmer settings file creation. 3.10.5.3. The Processing Tab When you program flash, the Processing tab shows the individual programming actions as they take place. 3.10.6. Saving a Flash Programmer Settings File When you have finished configuring the input files, locations, and other settings for programming your project to flash, you can save the settings in a . flash-settings file. With a . flash-settings file, you can program the project again without flash-settings reconfiguring the settings. You save a . file through the File menu. 3.10.7. Flash Programmer Options Through the Options menu, you can control several global aspects of flash programmer behavior, as described in this section. Related Information Nios II Flash Programmer User’s Guide For more information about these features. 3.10.7.1. Staging Directories Staging Directories dialog box, you control where the flash Through the files. flash-settings programmer creates its script and . ® Nios II Software Developer's Handbook Send Feedback 58

59 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.10.7.2. Generate Files If you disable this option, the flash programmer does not generate programming files, but programs files already present in the directory. You might use this feature to reprogram a set of files that you have previously created. 3.10.7.3. Program Files If you disable this option, the flash programmer generates the programming files and the script, but does not program flash. You can use the files later to program flash by option. turning off the Generate Files 3.10.7.4. Erase Flash Before Programming When enabled, this option erases flash memory before programming. 3.10.7.5. Run From Reset After Programming When enabled, this option resets and starts the Nios II processor after programming flash. 3.11. Creating Memory Initialization Files Sometimes it is useful to generate memory initialization files. For example, to program your FPGA with a complete, running Nios II system, you must include the memory contents in your .sof file. In this configuration, the processor can boot directly from internal memory without downloading. Creating a Hexadecimal (Intel-Format) File ( ) is a necessary intermediate step in .hex creating such a .sof .hex f iles and other file. The Nios II SBT for Eclipse can create memory initialization formats. To generate correct memory initialization files, the Nios II SBT needs details about the physical memory configuration and the types of files required. Typically, this information is specified when the hardware system is generated. If your system contains a user-defined memory, you must specify these details Note: manually. Related Information Generate Memory Initialization Files by the Legacy Method on page 60 3.11.1. Generate Memory Initialization Files To generate memory initialization files, perform the following steps: 1. Right-click the application project. and click Build to open the Make Targets dialog box. 2. Point to Make targets . mem_init_generate 3. Select ® Nios II Software Developer's Handbook Send Feedback 59

60 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 4. Click Build . The makefile generates a separate file (or files) for each memory .qip .qip device. It also generates a Intel Quartus Prime IP File ( file tells ). The the Intel Quartus Prime software where to find the initialization files. 5. file to your Intel Quartus Prime project. .qip Add the 6. Recompile your Intel Quartus Prime project. 3.11.2. Generate Memory Initialization Files by the Legacy Method To generate memory initialization files by the legacy method, perform the following steps: 1. Right-click the application project. Build to open the Make Targets 2. Point to Make targets and click dialog box. . 3. Select mem_init_install 4. Click Build . The makefile generates a separate file (or files) for each memory device. The makefile inserts the memory initialization files directly in the Intel Quartus Prime project directory for you. 5. Recompile your Intel Quartus Prime project. Related Information Hardware Reference For information about working in the stand-alone flow. 3.11.3. Memory Initialization Files for User-Defined Memories Generating memory initialization files requires detailed information about the physical memory devices, such as device names and data widths. Normally, the Nios II SBT file. However, in the case of a user- .sopcinfo extracts this information from the .sopcinfo file does not contain information about the data defined memory, the memory, which is outside the system. Therefore, you must provide this information manually. You specify memory device information when you add the user-defined memory device to your BSP. The device information persists in the BSP settings file, allowing you to regenerate memory initialization files at any time, exactly as if the memory device were part of the hardware system. Add Memory Advanced Specify the memory device information in the tab of the mem_init.mk . On dialog box. Settings in this tab control makefile variables in Device tab, you can control the following memory characteristics: the Advanced • The physical memory width. The device’s name in the hardware system. • The memory initialization file parameter name. Every memory device can have an HDL parameter specifying the name of the initialization file. The Nios II ModelSim launch configuration overrides the HDL parameter to specify the memory initialization filename. When available, this method is preferred for setting the memory initialization filename. • The Mem init filename parameter can be used in Nios II systems as an alternative method of specifying the memory initialization filename. The Mem init filename parameter directly overrides any filename specified in the HDL. ® Nios II Software Developer's Handbook Send Feedback 60

61 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 • Connectivity to processor master ports. These parameters are used when creating the linker script. • The memory type: volatile, CFI flash or EPCS flash. • Byte lanes. • You can also enable and disable generation of the following memory initialization file types: — .hex file — .dat and .sym files — .flash file Related Information Publishing Component Information to Embedded Software on page 308 For more information about this parameter, refer to "Embedded Software Assignments". 3.11.3.1. Specifying the Memory Device Information in the Advanced Tab Add Memory Specify the memory device information in the Advanced tab of the . dialog box. Settings in this tab control makefile variables in Device mem_init.mk On the tab, you can control the following memory characteristics: Advanced • The physical memory width. • The device's name in the hardware system. • The memory initialization file parameter name. Every memory device can have an HDL parameter specifying the name of the initialization file. The Nios II ModelSim launch configuration overrides the HDL parameter to specify the memory initialization filename. When available, this method is preferred for setting the memory initialization filename. parameter can be used in Nios II systems as an • The Mem init filename Mem init alternative method of specifying the memory initialization filename. The filename parameter directly overrides any filename specified in the HDL. • Connectivity to processor master ports. These parameters are used when creating the linker script. • The memory type: volatile, CFI flash or EPCS flash. • Byte lanes. • You can also enable and disable generation of the following memory initialization file types: — .hex file — .sym files .dat and — .flash file Related Information on page 308 Publishing Component Information to Embedded Software For more information about this parameter, refer to "Embedded Software Assignments". ® Nios II Software Developer's Handbook Send Feedback 61

62 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.12. Running a Nios II System with ModelSim You can run a Nios II program on Nios II hardware, such as an Intel FPGA development board, or you can run it in the Nios II ModelSim simulation environment. Note: If your project was created with version 10.1 or earlier of the Nios II SBT, you must re-import it to create the Nios II launch configuration correctly. 3.12.1. Using ModelSim with an SOPC Builder-Generated System If your hardware system was generated by SOPC Builder, running a software project in ModelSim is very similar to running it on Nios II hardware. To run a Nios II software project in ModelSim, right-click on the application project name, point to Run As , and click Nios II ModelSim . To debug a software project in ModelSim, right-click on the application project name, Nios II ModelSim . point to Debug As , and click Related Information Running the Project on Nios II Hardware on page 30 3.12.2. Using ModelSim with a Platform Designer-Generated System To run a Platform Designer-generated Nios II system with ModelSim, you must first create a simulation model and test bench, and specify memory initialization files. You create your Nios II simulation model and test bench using the steps that apply to any Platform Designer design. Intel Quartus Prime Standard Edition Handbook For more information, refer to the Handbook. Volume 1: Design and Synthesis Related Information Quartus Prime Standard Edition Handbook Volume 1: Design and Synthesis 3.12.2.1. Preparing your Software for ModelSim Creating the software projects is nearly the same as when you run the project on hardware. To prepare your software for ModelSim simulation, perform the following steps: 1. Create your software project. If you need to initialize a user-defined memory, you must take special steps to create memory initialization files correctly. 2. Build your software project. 3. Create a ModelSim launch configuration with the following steps: a. Right-click the application project name, point to , and click Run Run As dialog box, select Configurations . In the Run Configurations Nios II New button. , and click the ModelSim b. Main tab, ensure that the correct software project name and . file In the elf are selected. Apply to save the launch configuration. c. Click ® Nios II Software Developer's Handbook Send Feedback 62

63 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 d. Click Close to close the dialog box. If you are simulating multiple processors, create a launch configuration for each processor, and create a launch group. Run . The Nios II SBT for 4. Open the run configuration you previously created. Click Eclipse performs a command to create memory make mem_init_generate initialization files, and launches ModelSim. 5. ldr . At the ModelSim command prompt, type Related Information • Creating a Project on page 27 on page 29 • Building the Project • Generate Memory Initialization Files by the Legacy Method on page 60 on page 37 • Creating a Simple BSP 3.12.2.2. Potential Error Message When you create the launch configuration, you might see the following error message: SEVERE: The Intel Quartus Prime project location has not been set in the ELF section. You can manually override this setting in the launch configuration's ELF file 'Advanced' properties page. Related Information on page 37 Creating a Simple BSP 3.12.2.3. Nios II GCC Tool Chain The Nios II EDS version 14.0 is the first version of the Nios II cores. After 14.0, Nios II cores are used. The Nios II GNU tool chain has been upgraded, as well. The following GCC versions are available in the following Nios II EDS versions: • GCC 4.8.3 in 14.1 • GCC 4.9.1 in 15.0 When upgrading to a new tool chain, there will be Nios II-specific changes and GCC Note: changes and enhancements. Related Information • Porting to GCC 4.8 For more information about how GNU also provides a porting guide to GCC 4.8 to document common issues. • Porting to GCC 4.9 For more information about how GNU also provides a porting guide to GCC 4.9 to document common issues. • GCC Releases For more information about full GCC release notes. ® Nios II Software Developer's Handbook Send Feedback 63

64 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.12.2.3.1. Nios II Specific Changes • instead of #pragma to reliably -mcustom-* __buildin_custom_* Use or generate Nios II Floating Point Custom Instructions (FPCI), independent of compiler optimization level and command line flags. • -mcustom-* #pragma for Nios II Floating Point Custom Instructions To use or (FPCI): — flag must be used to generate fmins and fmax -ffinite-math-only The FPCI — The optimization (non -O0 flag) must be used to generate fsqrts FPCI • Users implementing transcendental functions in hardware must use the - flag to generate the FPCI for the transcendental funsafe-math-optimizations fcoss() fsins() ftans() , fatans() , fexps() , flogs() and functions , , corresponding double-precision functions. • #pragma custom_fadds 253 The Pragma format has changed from eg. to and function attributes provide #pragma GCC target("custom-fadds=253") an alternative format . __attribute__((target("custom-fadds=253"))) • flags instead of -EL/-EB for endian settings. Software Build Use the -mel/-meb Tool for Eclipse (SBTE) users must regenerate the BSP for this setting to take effect. • The flag and reverse_bitfields pragma are no longer -mreverse-bitfields supported. • -mstack-check to enable The flag must be used instead of -fstack-check stack checking. 3.12.2.3.2. GCC Changes and Enhancements • nios2-elf-gcc GCC 4.7.3 supports function calls -Wa,-relax-all The flag in and programs exceeding the 256 MB limit. • When used with optimization, inline assembly code with the asm operator needs to declare values imported from C and exported back to C, using the mechanisms described on the "Exteded Asm - Assembler Instructions with C Expression Operands" page. • Pre-standard C++ headers are not supported in GCC 4.7.3. Replace pre-standard with C++ with standard C++ eg. #include , cout, endl , respectively. #include , std::cout and std::endl • -Wl,--defsym foo=bar The compile flag where bar is an undefined symbol, will generate error at the linker level in GCC 4.7.3. GCC 4.1.2 does not include this check. Related Information on page 86 • Nios II Software Build Tools For more information about the GCC toolchains. • Getting Started from the Command Line on page 72 • Extended Asm - Assembler Instructions with C Expression Operands • Intel FPGA Software Installation and Licensing Manual For more information about installing the Quartus Prime software. ® Nios II Software Developer's Handbook Send Feedback 64

65 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.13. Eclipse Usage Notes The behavior of certain Eclipse and CDT features is modified by the Nios II SBT for Eclipse. If you attempt to use these features the same way you would with a non-Nios II project, you might have problems configuring or building your project. This section discusses such features. If you launch the Nios II Software Build Tools for Eclipse from the Nios II command shell, you cannot pause execution of the Nios II application when debugging the application. Running the program in this way closes the GDB connection to the target, leaving the processor running. This is caused by running Eclipse from the Cygwin environment. To ensure that the pause button works, launch the Nios II Software Build Tool for Eclipse either from Platform Designer or directly from your operating system's Start menu. 3.13.1. Configuring Application and Library Properties To configure project properties specific to Nios II SBT application and library projects, use the tabs of the Nios II Application Properties and Nios II Library Properties Properties dialog box. To open the appropriate properties tab, right-click the application or library project and Nios II Application Properties or click . Depending on the project type, Properties tab appears in the list of tabs. Click the appropriate Nios II Library Properties Properties tab to open it. 3.13.1.1. Comparing the Nios II Application Properties and Nios II Library Properties tabs Nios II Application Properties tabs are nearly The and Nios II Library Properties identical. These tabs allow you to control the following project properties: • elf file (application project only) The name of the target . • The library name (library project only) • A list of symbols to be defined in the makefile • A list of symbols to be undefined in the makefile • A list of assembler flags • Warning level flags • A list of user flags • Generation of debug symbols • Compiler optimization level • Generation of object dump file (application project only) • Source file management • Path to associated BSP (required for application, optional for library) 3.13.2. Configuring BSP Properties To configure BSP settings and properties, use the Nios II BSP Editor. ® Nios II Software Developer's Handbook Send Feedback 65

66 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 Related Information • Using the BSP Editor on page 40 For more information about the BSP Editor. on page 40 • Using the BSP Editor For more information about the BSP Editor. 3.13.3. Exclude from Build Not Supported command is not supported. You must use the Remove from Exclude from Build The and Add to Nios II Build commands instead. Nios II Build This behavior differs from the behavior of the Nios II SBT for Eclipse in version 9.1. 3.13.4. Selecting the Correct Launch Configuration Type If you try to debug a Nios II software project as a CDT Local C/C++ Application launch configuration type, you see an error message, and the Nios II Debug perspective fails to open. This is expected CDT behavior in the Eclipse platform. Local C/C++ Application is the launch configuration type for a standard CDT project. To invoke the Nios II plugins, you must use a Nios II launch configuration type. If your project was created with version 10.1 or earlier of the Nios II SBT, you must Note: re-import it to create the Nios II launch configuration correctly. 3.13.5. Target Connection Options The Nios II launch configurations offer the following Nios II-specific options in the Target Connection tab: • Disable 'Nios II Console' view • Ignore mismatched system ID • Ignore mismatched system timestamp • Download ELF to selected target system • Start processor • Reset the selected target system 3.13.6. Renaming Nios II Projects To rename a project in the Nios II SBT for Eclipse, perform the following steps: 1. Right-click the project and click Rename . 2. Type the new project name. . Refresh 3. Right-click the project and click If you neglect to refresh the project, you might see the following error message when you attempt to build it: is out of sync with the Resource system ® Nios II Software Developer's Handbook Send Feedback 66

67 3. Getting Started with the Graphical User Interface NII-SDH | 2019.04.30 3.13.7. Running Shell Scripts from the SBT for Eclipse Many SBT utilities are implemented as shell scripts. You can use Eclipse external tools configurations to run shell scripts. However, you must ensure that the shell environment is set up correctly. To run shell scripts from the SBT for Eclipse, execute the following steps: 1. Start the Nios II Command Shell. 2. Start the Nios II SBT for Eclipse by typing the following command: eclipse-nios2 You must start the SBT for Eclipse from the command line in both the Linux and Windows operating systems, to set up the correct shell environment. 3. From the Eclipse Run menu, select to External Tools ➤ External Tools . Configurations 4. Create a new tools configuration, or open an existing tools configuration. Location 5. On the Argument . Main tab, set and Table 5. Location and Argument to Run Shell Script from Eclipse Platform Argument Location Windows -c "