Embedded Systems

Altera NIOS II Development: Part I

Electrical & Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-1

Overview

• NIOS II programs and associated projects – Applications – User libraries – Board support packages (BSPs) • Programming a NIOS II system – User programs – C standard library – Hardware (HAL) and HAL layers – Device drivers • HAL Architecture and Services • Developing Programs Using the HAL

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-2

1 NIOS II Programs

• Each NIOS II program consists of: – an application project, – optional user library projects, and – a (BSP) project • The build creates an Executable and Linking Format File (.elf) which runs on a NIOS II processor.

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-3

NIOS II Software Project Types: 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 contains commands to: – compile the source code – link it with a BSP and one or more optional libraries – create one .elf file

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-4

2 NIOS II Software Project Types: User Library Project • A user library project is a collection of source code compiled to create a single library archive file (.a) – Libraries often contain reusable, general purpose functions that multiple application projects can share – A collection of common arithmetical functions is one example • A user library does not contain a main() function

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-5

NIOS II Software Project Types: 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 an SOPC Builder system • The NIOS II EDS provides tools to modify settings that control the behavior of the BSP • A BSP contains the following elements: – layer – C standard library – Device drivers – Optional software packages – Optional real-time

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-6

3 Overview of the Hardware Abstraction Layer

• The HAL provides a simple interface for programs to connect to the underlying hardware • The HAL application program interface (API) is integrated with the ANSI C standard library • The HAL API allows access to devices and files using familiar C library functions – printf(), fopen(), fwrite(), etc. • The HAL serves as a device driver package for NIOS II processor systems – provides a consistent programming interface to the peripherals in a NIOS II system

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-7

HAL Architecture and Services

• Integration with the newlib ANSI C standard library – Provides the familiar C standard library functions • Device drivers – Provides access to each device in the system • The HAL API – Provides a consistent, standard interface to HAL services, such as device access, handling, and alarm facilities • System initialization – Performs initialization tasks for the processor and the runtime environment before main() • Device initialization – Initializes each device in the system before main() runs

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-8

4 Layers of a HAL-Based System

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-9

HAL Generic Device Models

• The HAL provides generic device models for classes of peripherals found in embedded systems – timers, Ethernet MAC/PHY chips, and I/O peripherals that transmit character data, etc. • Generic device models allow programs to use a consistent API, regardless of the underlying hardware

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-10

5 HAL Device Model Classes

• Character-mode devices – Hardware peripherals that send and/or receive characters serially, such as a UART. • Timer devices – Hardware peripherals that count clock ticks and can generate periodic interrupt requests. • File subsystems – A mechanism for accessing files stored in physical device(s) • Ethernet devices – Devices that provide access to an Ethernet connection for a networking stack • Direct memory access (DMA) devices – Peripherals that perform bulk data transactions from a data source to a destination • devices – Nonvolatile memory devices that use a special programming protocol to store data

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-11

HAL Peripherals

• All peripherals must have a header file that defines the peripheral’s low-level interface to hardware • Therefore, all peripherals support the HAL to some extent • However, some peripherals might not provide device drivers • If drivers are not available, use only the definitions provided in the header files to access the hardware • Some peripherals provide functions that are not based on the HAL generic device models – For example, Altera provides a general-purpose parallel I/O (PIO) core for use with the NIOS II processor system – The PIO peripheral does not fit in any class of generic device models provided by the HAL, and so it provides a header file and a few dedicated functions only – User pushbuttons and LEDs are examples of these

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-12

6 Developing Programs Using The HAL

• NIOS II Development Flows – The NIOS II Embedded Design Suite (EDS) provides two distinct development flows for creating NIOS II programs • NIOS II Software Build Tools (SBT for Eclipse or command line) • NIOS II Integrated Development Environment (IDE) • HAL BSP Settings – Every NIOS II board support package (BSP) has settings that determine the BSP’s characteristics – For example, HAL BSPs have settings to identify the hardware components associated with standard devices such as stdout • Defining and manipulating BSP settings is an important part of NIOS II project creation • BSP settings can be modified with the NIOS II BSP Editor, with command-line options, or with Tcl scripts

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-13

The NIOS II HAL Project Structure

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-14

7 The NIOS II HAL Project Structure

• Every HAL-based NIOS II program consists of two NIOS II projects – Your application-specific code is contained in one project (the user application project), and – it depends on a separate BSP project (the HAL BSP). • The application project contains all the code you develop • The executable image for your program ultimately results from building both projects • With the NIOS II Software Build Tools for Eclipse, the tools create the HAL BSP project when you create your application project • The BSP project depends on the SOPC Builder system, defined by a SOPC Information File (.sopcinfo) – The SOPC file is created using the SPOC Builder tool within the Altera Quartus software

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-15

The system.h System Description File

• The system.h file provides a complete software description of the NIOS II system hardware • The system.h file describes each peripheral in the system and provides: – The hardware configuration of the peripheral – The base address – Interrupt request (IRQ) information (if any) – A symbolic name for the peripheral

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-16

8 Excerpts from a system.h File

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-17

Data Widths and the HAL Type Definitions

• For embedded processors such as the NIOS II processor, it is often important to know the exact width and precision of data • Because the ANSI C data types do not explicitly define data width, the HAL uses a set of standard type definitions instead • The ANSI C types are supported, but their data widths are dependent on the ’s convention • The header file alt_types.h defines the HAL type definitions

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-18

9 The HAL and GNU Toolchain Type Definitions and Data Widths

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-19

UNIX-Style Interface

• The HAL API provides a number of -style functions – The UNIX-style functions provide a familiar development environment for new NIOS II programmers – Can ease the task of porting existing code to run in the HAL environment • The HAL uses these functions primarily to provide the system interface for the ANSI C standard library – For example, the functions perform device access required by the C library functions defined in stdio.h.

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-20

10 Available UNIX-style Functions

• _exit() • open() • close() • read() • fstat() • sbrk() • getpid() • settimeofday() • gettimeofday() • stat() • ioctl() • usleep() • isatty() • wait() • kill() • write() • lseek()

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-21

File System Functions

• The HAL provides infrastructure for UNIX-style file access • You can access files in a HAL-based by using – The C standard library file I/O functions in the C library (for example fopen(), fclose(), and fread()), or – The UNIX-style file I/O provided by the HAL • HAL-provided functions – close() – fstat() – ioctl() – isatty() – lseek() – open() – read() – stat() – write()

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-22

11 Example “Hello World” Application

• Example Goal: Create a user “Hello World” application and associated BSP application using the NIOS II Software Build Tools for Eclipse with a precompiled (i.e. prebuilt) NIOS II system • Will be built in c:\temp directory • Copy: – cycloneIII_3c25_NIOSII_standard.sof from C:\altera\91\kits\cycloneIII_3c25_NIOSII\examples\standard into c:\temp • SOF file to be downloaded to FPGA – cycloneIII_3c25_NIOSII_standard_sopc.sopcinfo from C:\altera\91\kits\cycloneIII_3c25_NIOSII\examples\standard into c:\temp • Contains all information about the NIOS II processor (and system) necessary to build the BSP project (and ultimately the user executable)

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-23

NIOS II System Built Using SOPC Builder

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-24

12 Starting the NIOS SBT for Eclipse

• From the Start Menu, select Altera->NIOS II EDS 9.1->NIOS II 9.1 Software Build Tools for Eclipse

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-25

NIOS II SBT for Eclipse

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-26

13 NIOS II SBT for Eclipse

• Select File->New->NIOS II Application and BSP from Template

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-27

NIOS II SBT for Eclipse

• Select File->New->NIOS II Application and BSP from Template

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-28

14 NIOS II SBT for Eclipse

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-29

NIOS II SBT for Eclipse

• Build the BSP – Right click the hello_world_bsp project – Select “Build Project” • Build the user application – Right click hello_world project – Select “Build Project” • Configure the NEEK board with the processor design – Select NIOS II->Quartus II Programmer – Download the cycloneIII_3c25_NIOSII_standard.sof file to the NEEK • Run the executable – Select Run->Run As->NIOS II Hardware

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-30

15 NIOS II SBT for Eclipse

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-31

Alternate output using Character Output to a Device

#include #include int main (void) { char* msg = "hello world"; FILE* fp; fp = fopen ("/dev/jtag_uart", "w"); if (fp!=NULL) { fprintf(fp, "%s",msg); fclose (fp); } return 0; }

Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 5-32

16