uClinux Tutorial Altera NIOS2 Archive

(Author: Manfred Schmid)
Last update: 23 Jul 2008


Abstract

Find here topics that have been taken out from the main document, because they are deprecated.

Conventions

Commands are written with a fixed size font with an Cmd: indicator symbolizing the Command prompt.
Lines in a file are diplayed in blue color.
Paths and files are displayed in italic.
Text to be entered in edit boxes is displayed in red.

Contents

Nios2 uClinux

Download

Nios2 uClinux

IDE

The Microtronix uClinux is very convenient to use because it works together with the Altera IDE as Eclipse Plug-In.
The Kernel is installed simply by running the Windows installer of the Nios II Linux Developer CD.
The CD is available from Microtronix for $ 199 and ships in approx 1 week or by download from Nios-Forum.
The Developement is done with the Altera Developement tools, which use Cygwin under Windows XP.

The Microtronix IDE was made for the Altera 5.0 tools, therefore some minor things are not working with 7.2.
The fix is to use the Commandline for Upload and the configure.bat file for the Kernel Menuconfig.
Be careful with the installation of Cygwin. No Cygwin other than Altera's is allowed on the system.
Make sure that the path variable points to the right locations. Check the path variable in Environment variables.
Furthermore check the variables: QUARTUS_ROOTDIR, SOPC_BUILDER_PATH and SOPC_KIT_NIOS2, they all have to point to the 7.2 version.
Fix the path in configure.bat (for example: c:\altera\72\nios2eds\bin\eclipse\workspace\<projectname>\configure.bat)

Follow the Quick Start Guide of Microtronix to create a Kernel Project and a Filesystem Project and if desired a Application Project.
Configure the kernel by starting configure.bat.
You should see a DOS-Box with the Kernel configuration menu.
In order to compile for the first time choose:

Turn on: ===> Processor type and features ===> Platform (Altera Cyclone Development board support) ===> Altera Cyclone Development board support
Turn on: ===> Device Drivers ===> Ethernet (10 or 100MBit) ===> Davicom DM9000 10_100 Ethernet chip support

Turn off: ===> Device Drivers ===> ATA/ATAPI/MFM/RLL support ===> ATA/ATAPI/MFM/RLL support
Turn off: ===> Processor type and features ===> Enable leds, seven segment display

Now run Build Project from the Nios2 IDE.
The result is a file called vmlinux.bin in the <Project>/build directory.
The size of the vmlinux.bin should not exceed 2MB because this will be the size for our Kernel Partition.
To convert the bin file into a S19 record for uploading type following command on the commandline.
Note: All commandline options need to be executed in the "Nios II SDK shell"
Cmd: bin2flash --input=vmlinux.bin --output=vmlinux.flash --location=0x0

The filesystem can be build in a similar way.
Just add or remove packages in the IDE. Additionally you can add or remove packages or files in the <project>/target directory.
Now run Build Project from the Nios2 IDE with the filesystem selected as your current project.
The result is a file called romfs.bin in the <Project> directory.
The size of the romfs.bin should not exceed 2MB because this will be the size for our Filesystem Partition.
To convert the bin file into a S19 record for uploading type following command on the commandline.
Note: All commandline options need to be executed in the "Nios II SDK shell"
Cmd: bin2flash --input=romfs.bin --output=romfs.flash --location=0x200000

Upload the Kernel to the CFI Flash with following commands:
Cmd: nios2-flash-programmer --base=0x2000000 --debug --erase=0x0+0x200000
Cmd: nios2-flash-programmer --base=0x2000000 --debug vmlinux.flash

Upload the Filesystem to the CFI Flash with following commands:
Cmd: nios2-flash-programmer --base=0x2000000 --debug --erase=0x200000+0x200000
Cmd: nios2-flash-programmer --base=0x2000000 --debug romfs.flash

Open a terminal with:
Cmd: nios2-terminal

After pressing the reset button Key1 on the DE2 Board you should see the Linux boot.

Back

A second option for compiling kernel and filesystem is to work under Linux instead of Windows and Cygwin.
The advantage is that some makefiles need only slight modification from the original.
If you want to use initramfs, than you must use Linux because bugs in Cygwin make it impossible to get a working filesystem.

Most people in the Nios Forum use the Buildroot environment which runs on a real Linux OS rather than Cygwin.
Support on compiling applications is often offered for Buildroot only and not for the Altera IDE.
It is very difficult to port the Makefiles to the Altera IDE sometimes, so therefore its easier to work on Linux.
So in order to compile some applications like the OpenSSL libraries I set up a Linux machine.
I suggest to use either Fedora Core 7 (FC7).
To set up the Linux machine select >Server packages<. Missing packages can be installed easily in the KDE enviroment later on..
Hippo from Nios Forum makes this process painless. He provides a script that does all the magic.
I will mirror this script and explain the setup briefly. For the long description go to the Nios Forum ===> Downloads ===> Post Your Own IP ===> buildroot to build toolchain on Linux.
First we need to change the compiler to use to gcc3, gcc4 has problems compiling binutils-2.15

FC4 and FC5:
Cmd: su - # your root passwd
Cmd: yum install compat-gcc-32
Cmd: cd /usr/bin
Cmd: mv gcc gcc4
Cmd: ln -s gcc32 gcc
Cmd: exit

FC7:
The script for the compatability compiler does no longer work on Fedora Core 7.
It is necessary to install version 3.4 for the compiler
Cmd: su - # your root passwd
Cmd: rpm -i compat-gcc-34-3.4.6-7.i386.rpm
Cmd: cd /usr/bin
Cmd: mv gcc gcc4
Cmd: ln -s gcc34 gcc
Cmd: exit

Create a nios2 user, for example
Cmd: su - # your root passwd
Cmd: adduser nios2
Cmd
: exit

Login as nios2 user and create dir download
Cmd: mkdir download

Now download the build script from the download section into the download directory and start it.
Cmd: cd ~/download
Cmd: unzip build.zip
Cmd: ./build

This builds the whole toolchain. The script uses wget to download the necessary packages.
It might be that some of these links will not be present in the future and you might edit the script to correct the links.
In my case all links worked, but there was an error with patching some config.sub files. I just ignored those errors, it didn't hurt later.

Add the path for the cross compiler to the PATH in ~/.bash_profile
PATH=$PATH:$HOME/bin:$HOME/buildroot/build_nios2/staging_dir/bin

To compile and configure the kernel do following:
Cmd: cd ~/linux-2.6.x
Cmd: make ARCH=nios2nommu CROSS_COMPILE=nios2-linux-uclibc- hwselect SYSPTF=your_system.ptf

For the DE2 board the your_system.ptf = nios_0.ptf from above
For the Altera CycloneII NIOS dev board your_system.ptf = std_2C35.ptf

Configure the kernel:
Cmd: make ARCH=nios2nommu CROSS_COMPILE=nios2-linux-uclibc- menuconfig

Select the board and drivers needed.

Compile the kernel now:
Cmd: make ARCH=nios2nommu CROSS_COMPILE=nios2-linux-uclibc- zImage

As output you get zImage, the compressed kernel image, also you get vmlinux.bin the uncompressed version.
vmlinux.bin is what you also get using the altera IDE, so uploading the kernel could be done with the tools from the Altera IDE using bin2flash on vmlinux.bin

The Linux buildroot uses initramfs, which is a ram filesystem supported by the kernel. For embedded solutions no other filesystem is needed.
How initramfs works is explained in its own section.

Back

To keep up with latest kernels it is recommended to use the uClinux-dist approach.
Current kernel is 2.6.17.
A kernel update was needed to be able to use the drivers for ISP1362 and ISP1761.
A detailed description can be found on Nios-Wiki.
Download the binary toolchain and put it in ~/download.
Install it:
Cmd: su
Cmd: cd ~/download
Cmd: tar jxf nios2gcc.tar.bz2 -C ~
Cmd: exit
The cross compiler tools will be in ~/opt/nios.
Update the path in ~/.bash_profile:
PATH=$PATH:$HOME/bin:$HOME/opt/nios/bin

Download uClinux-dist source uClinux-dist-20060803 from uClinux.org (embarrassingly slow) or mirror and put it in your home directory ~/.
Install it:
Cmd: tar jxf uClinux-dist-20060803.tar.bz2

Download the patch for Nios2 and put it in ~/uClinux-dist
Install it:
Cmd: cd ~/uClinux-dist
Cmd: zcat uClinux-dist-20060803-nios2-02.diff.gz | patch -p0

Now you can follow the Nios-Wiki or follow my approach:
I have setup 2 users: 1 for the buildroot approach lets say user buildroot and one for the uClinux-dist approach lets say uclinuxdist.
Because I like the buildroot environment more I removed the linux-2.6.x directory from user buildroot and replaced it with the linux-2.6.x directory from user uclinuxdist.
Then the environment is like described in buildroot but with latest kernel version.

If you are new the best approach is to follow exactly the tutorials in Nios-Wiki.

Back

Compiling Packages

A lot of Linux tools are available. The tools are available as sourcecode packages. The difficult task is now to compile them for the Nios2 processor.
The sourcecode usually does not need to be changed. It is the Makefile that needs some tweeking.
There are mainly 3 places where to find applications.

1) Microtronix. Applications can be found in c:\altera\72\nios2eds\examples\software\linux

2) Buildroot. These packages come in form of a tar.gz and are downloaded with wget from some place in the internet. Additionally packages, not available in the buildroot environment, can be downloaded aswell.
The procedure for changing the makefiles is than the same as described for buildroot.

3) uClinux-dist. Many applications can be found in the directory ~/uClinux-dist/user

The task is to change the Makefile. In general following needs to be added or changed:
- Compiler. The makefiles use the host compiler but the Nios needs the cross compiler.
- CFLAGS. Special flags are needed to compile for embedded.
- ELF. The output format needs to be changed to FLAT binary format.

Applications can be found in c:\altera\72\nios2eds\examples\software\linux.
For compiling you need to start the Nios2 IDE.
Create a new project and say Linux-application.
For the projectname just choose the directory name of the application.
The Rules.mak and Settings.mak file will be autogenerated.
Right click the project and say build project.
The application has the extension .exe.
Remove the exe and copy the application into your rootfs/bin directory.
Dont forget to change the permissions to executable.

Back

To change the makefiles for buildroot is the most complicated, because there are many flavours and it can get quite complicated.
Therefore I can only describe the case that I could manage.

The sourcecode comes in form of a tar.gz file.
In case the package has a config.sub file and a configure script we are lucky.
In order to compile for the Nios2 we need to adjust the config.sub file first and then run configure with some options to generate the Makefile.
In config.sub add the nios2 in the supported architectures:
find the line:
case $basic_machine in
and add nios2 to the list.
Replace:
| z8k )
with
| z8k \
| nios2 )

Now run the configure script with following options
Cmd: ./configure --host=nios2-linux --with-cc=nios2-linux-gcc --with-cflags="-D__uClinux__ -DUSE_VFORK -Dfork=vfork" --disable-shared --enable-static --with-endianness=little --with-ldflags="-elf2flt=-s16000"

The executable can be build with make now.
Cmd: make

Back

The applications can be found in the directory uClinux-dist/user.
There is already a Makefile in the directory of the application you would like to build.
If you just run it, it will generate a executable using the host compiler with output format ELF.
If it compiles without errors we can change the Makefiles now.
Add the following lines after EXEC:
CROSS := nios2-linux-uclibc-
CC := $(CROSS)gcc
ELFFLAGS := -s 8192
CFLAGS += -D__uClinux__ -DUSE_VFORK -Dfork=vfork

After $ (CC) in the $(EXEC) section add:
-elf2flt="$(ELFFLAGS)

That's it. There is a good chance it can be compiled and linked for the Nios2 now.
See the example for the telnet application ported from the uClinux-dist.

Back

Minimum Kernel Config

2.6.17

The question of what to enable and what not in menuconfig is a little tricky.
Here is a list of features that need to be enabled for the smallest possible kernel.
The list was found by experiment and covers the case of a system with initramfs (means no mtd).
Only the items that need to be enabled are mentioned here, all others have to be left unchecked:

General setup ===>

(~/rootfs ~/download/rootfs_list) Initramfs source file(s)
[X] Configure standard kernel features (for small systems) ===>

[X] Enable support for printk
[X] BUG() support

Processor type and features ===>

CPU (NIOS2) ===>

[X] NIOS2

Platform (Altera Cyclone Developement board support) ===>

[X] Altera Cyclone Developement board support

Nios II Hardware Multiply Support (Enable mul instruction) ===>

[X] Enable mul instruction

(0x0500000) Link address offset for booting

Exectutable file formats ===>

[X] Kernel support for flat binaries

Device Drivers ===>

Input device support ===>

Hardware I/O ports ===>

[X] Serial I/O support

Character devices ===>

Serial drivers ===>

[X] Altera JTAG UART support (for the nios2-terminal)
OR:
[X] Nios serial support
[X] Support for console on Nios UART (for Hyperterminal)

File systems ===>

Pseudo filesystems ===>

[X] /proc file system support
[X] sysfs file system support

Back

DM9000A Network Driver

2.6.17

Device Drivers ===>

Networking support ===>

[X] Networking Support
Networking options ===>

[X] Packet socket
[X] TCP/IP networking
[X] IP: kernel level autoconfiguration
[X] IP: DHCP support

[X] Network device support
Ethernet (10 or 100MBit) ===>

[X] Ethernet (10 or 100MBit)
[X] Davicom DM9000 10_100 Ethernet chip support

Note:
The kernel 2.6.17 has a newer version of the driver which also works. I tested both dm9000x and dm9000 on the DE2 board.
[X] DM9000 support

Back

USB

2.6.11 and 2.6.17

This is a more complicated story.
Because the driver is not part of Microtronix Linux Kernel and not part of the official uClinux Linux Kernel it has to be ported from the ISP116x.
There are sources allready ported but they are for Kernel version 2.6.16. These sources do not compile on 2.6.11 because some generic driver architecture has changed in the mean time.
I have backported the sources to the Microtronix Linux with Kernel version 2.6.11.
So far only the host controller (HCD) is supported.

Because there is a couple of files that need to be augmented it is best to start with a patch to patch the sources to be compatible to 2.6.16.
Copy the file isp1362.diff from the download section into the directory c:\altera\kits\nios2_51\bin\eclipse\plugins\com.microtronix.nios2linux.kernel_1.4.0\linux-2.6.x
Then apply the patch.
Cmd: patch -p0 < isp1362.diff
Because the patch has a bug in the usb makefile, fix the line for the ISP1362 in file: c:\altera\kits\nios2_51\bin\eclipse\plugins\com.microtronix.nios2linux.kernel_1.4.0\linux-2.6.x\drivers\usb\Makefile
Fix for line:
obj-$(CONFIG_USB_ISP1362_HCD) += host/
just below the line of the ISP116x.

After this replace following files:
Replace the isp1362-hcd.c with the backported isp1362-hcd.c from the download section.
Replace the isp1362.h with the backported isp1362.h from the download section.
The sources are complete now.

There is 2 technologies for blockdevices:

When USB Block device is choosen then it overrules the SCSI settings, that means your driver will use /dev/uba instead of /dev/sda.

Kernel 2.6.17:

In Menuconfig following needs to be enabled for SCSI and UBA:

Device Drivers ===>

USB support ===>

[X] Support for Host-side USB
[X] USB device filesystem
[X] ISP1362 HCD support
[X] USB Mass Storage support
[X] USB Human Interface Device (full HID) support
[X] HID input layer support
[X] /dev/hiddev raw HID device support

File Systems ===>

Native Language Support ===>

[X] Codepage 437 (United States, Canada)
[X] NLS ISO 8859-1 (Latin 1; Western European Languages)

DOS/FAT/NT Filesystems --->

[X] VFAT (Windows-95) fs support

In Menuconfig following needs to be enabled for USB Block Device:

Device Drivers ===>

Block devices ===>

[X] Low Performance USB Block driver

In Menuconfig following needs to be enabled for SCSI Device:

Turn off:

Device Drivers ===>

Block devices ===>

[ ] Low Performance USB Block driver

Turn on:

Device Drivers ===>

SCSI device support ===>

[X] SCSI disk support

Back

Busybox

To create the busybox executable go into the directory ~/buildroot/build_nios2/busybox and run
Cmd: make menuconfig
Now choose the packages you want to have build into busybox.

Before you can run make it is very important to fix the Rules.mak file:
Add the line
CFLAGS+=-elf2flt="-s16000"
after the line
CFLAGS+=-I$(top_builddir)/include -I$(top_srcdir)/include -I$(srcdir)
in order to get executables that run on Nios.

Now compile:
Cmd: make

Note: Busybox saves Flash memory space but demands a lot of RAM. On DE2 with only 8MB of RAM busybox is not recommended.

Back

Boa Webserver

If it is necessary to handle files with a filesize > 1MB, then following needs to be enabled in menuconfig:

Processor type and features ===>

[X] Allow allocating large blocks (> 1MB) of memory

General setup ===>

[X] Configure standard kernel features (for small systems) ===>

[X] Use full SLAB allocator

If the filesize is always smaller than 1 MB, both kernel features can be omitted. SLOB will than be used instaed of SLAB.

Troubleshooting:
1) mm/nommu.c(120): kernel BUG!
This happens if "Allow allocating large blocks" is enabled but "Use full SLAB allocator" is not.
The problem appears if a large file (>1MB) is created and than removed. From now on the filesystem is corrupted.

Back

Download

description version kernel dist package/file
Buildroot (Nios2 Toolchain)   2.6   build.zip
ISP1362-HCD   2.6.11   isp1362.zip
dm9000a 2.03a 2.6.17   dm9000a.zip
nand512w3a   2.6.17   nand.zip
Telnet   2.6.x uClinux-dist-20060803 telnet.diff
Boa 0.93.15 2.6.x uClinux-dist-20060803 boa.zip
dhcpcd v.1.3.22-pl4 2.6.x uClinux-dist-20060803 dhcpcd.zip