iv Altera Corporation
Preliminary
Contents Nios II Software Developer’s Handbook
Getting Started ....................................................................................................................................... 3–1
HAL Architecture .................................................................................................................................. 3–2
Services .............................................................................................................................................. 3–2
Applications vs. Drivers .................................................................................................................. 3–3
Generic Device Models .................................................................................................................... 3–3
C Standard Library—Newlib ......................................................................................................... 3–4
Supported Peripherals .......................................................................................................................... 3–5
Chapter 4. Developing Programs using the HAL
Introduction ............................................................................................................................................ 4–1
The Nios II IDE Project Structure ........................................................................................................ 4–1
The system.h System Description File ................................................................................................ 4–2
Data Widths & the HAL Type Definitions ......................................................................................... 4–3
UNIX-Style Interface ............................................................................................................................. 4–4
File System .............................................................................................................................................. 4–5
Using Character-Mode Devices ........................................................................................................... 4–6
Standard Input, Standard Output & Standard Error .................................................................. 4–7
General Access to Character Mode Devices ................................................................................. 4–7
C++ Streams ...................................................................................................................................... 4–8
/dev/null .......................................................................................................................................... 4–8
Using File Subsystems .......................................................................................................................... 4–8
Using Timer Devices ............................................................................................................................. 4–8
The HAL System Clock ................................................................................................................... 4–9
Alarms ................................................................................................................................................ 4–9
High Resolution Time Measurement .......................................................................................... 4–11
Using Flash Devices ............................................................................................................................ 4–12
Simple Flash Access ....................................................................................................................... 4–12
Block Erasure or Corruption ......................................................................................................... 4–14
Fine-Grained Flash Access ............................................................................................................ 4–15
Using DMA Devices ............................................................................................................................ 4–18
DMA Transmit Channels .............................................................................................................. 4–19
DMA Receive Channels ................................................................................................................. 4–20
Memory-to-Memory DMA Transactions .................................................................................... 4–21
Reducing Code Footprint ................................................................................................................... 4–23
Enable Compiler Optimizations ...................................................................................................4–23
Use Small Footprint Device Drivers ............................................................................................ 4–23
Reduce the File Descriptor Pool ................................................................................................... 4–24
Use /dev/null ................................................................................................................................. 4–24
Use UNIX not ANSI C File I/O ....................................................................................................4–24
Use the Small Newlib C Library ..................................................................................................4–25
Eliminate Unused Device Drivers ............................................................................................... 4–27
Use _exit() for No Clean Exit ........................................................................................................ 4–27
Disable Instruction Emulation ...................................................................................................... 4–27
Boot Sequence and Entry Point ......................................................................................................... 4–28
Hosted vs. Free-Standing Applications ...................................................................................... 4–28
Boot Sequence for HAL-Based Programs ................................................................................... 4–29
Customizing the Boot Sequence ..................................................................................................
.4–30