Version 23, changed by hippo5329@yahoo.com.tw. 08/25/2008. Show version history
To compile a simple program, just add -elf2flt to link flag
Create a file, hello.c
#include <stdio.h>
int main(void)
{
printf("hello world\\\\n");
}nios2-linux-uclibc-gcc hello.c -o hello -elf2flt
The compiled object format is FLAT.
You may check it with,
nios2-linux-uclibc-flthdr hello
hello
Magic: bFLT
Rev: 4
Build Date: Mon Jun 5 21:49:44 2006
Entry: 0x40
Data Start: 0x4a8c
Data End: 0x5c48
BSS End: 0x7ca8
Stack Size: 0x1000
Reloc Start: 0x5c48
Reloc Count: 0x11e
Flags: 0x1 ( Load-to-Ram )
Then copy hello to the romfs/bin dir. Rebuild the kernel image for initramfs.
cp hello ~/nios2-linux/uClinux-dist/romfs/bin
cd ~/nios2-linux/uClinux-dist
make
Boot nios2 uClinux, and run
hello
The default stack size of application is 4KB, you can change it with -elf2flt="-s <new stack size>" option, eg ,
nios2-linux-uclibc-gcc hello.c -o hello -elf2flt="-s 16000"
will have stack size as 16KiB.
The default include dir search path is /opt/nios2/include (or staging_dir/include if you use buildroot)
The default library search path is /opt/nios2/lib (or staging_dir/lib if you use buildroot)
The default apps library is uClibc's libc, so you don't need -lc .
If you use math, you need -lm .
If you use pthread, you need -lpthread .
If you use crypt, you need -lcrypt .
example apps to use button pio.
Note, you are doing in user space, with uclibc and included from /opt/nios2/include. It is not kernel space. You can not use interrupt. You can not use nios2 HAL, either.
You should know about the cache in Nios II. (BadOman:?) The first 2GB of address space is cached. The second 2GB is non-cached.
These are not two seperate memory spaces or anything so there is a
total of 2GB of address space (mirrored memory). This only applies for Nios II Full version with Data Cache. Nios II Standard version is uncached, so there should be no problems.
In other words address 0x00000000 (cachable) maps to address 0x8000000 (non-cachable)
" " " " " " 0x00000001 " " " " " " " " 0x8000001 (and so on .....)
So use the first 2GB for non-peripheral access (local memory), and the second 2GB for peripherals
You can define memory pointer access, and you can make it uncached by setting address bit 31.
eg, 0x00004000 -> 0x80004000
Or use port address defined in nios2_system.h, eg na_button_pio.
#include </home/hippo/uClinux-dist/linux-2.6.x/include/nios2_system.h>
#define inw(port) (*(volatile unsigned *)(port)) # for io read,
#define outw(d,port) (*(volatile unsigned *)(port))=(d) # for io write,
As an alternative, you can use these two defines (which are always uncached), they are similar to the Nios2-IDE functions :
#define IORD(address,offset) (*(volatile unsigned *)(((address)|0x80000000)+4*(offset)))
#define IOWR(address,offset,value) (*(volatile unsigned *)(((address)|0x80000000)+4*(offset)))=(value)