CompileHello

1. Add hello apps to uClinux-dist (recommended)

 

Please follow the doc to add your user apps,

nios2-linux/uClinux-dist/Documentation/Adding-User-Apps-HOWTO

But change foo to hello.

 

cd nios2-linux/uClinux-dist

mkdir user/hello

Edit user/Makefile,

add a line in the sorting order.

   dir_$(CONFIG_USER_HELLO_HELLO)            += hello

Edit user/Kconfig

after menu "Miscellaneous Applications".

config USER_HELLO_HELLO
    bool "hello"
    help
      This program prints hello world.

Create user/hello/Makefile

EXEC = hello
OBJS = hello.o

all: $(EXEC)

$(EXEC): $(OBJS)
     $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

romfs:
     $(ROMFSINST)    /bin/$(EXEC)

clean:
     -rm -f $(EXEC) *.elf *.gdb *.o

 

Create user/hello/hello.c.

 

#include <stdio.h>
int main(void)
{
printf("hello world\n");
}

Then make menuconfig and select [*] hello. Now you can make. Boot nios2 and run "hello".


 2. Compile without adding to uClinux-dist (not recommended)

To compile a simple program for uClinux, just add -elf2flt to link flag

Create a file, hello.c

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. 

If using pthreads, only the main thread will have this 16KiB size.   Subsequent threads will have their stack at default size, and you will want to change that size at thread creation.  Otherwise, you're in for some stack-blowing fun, which hard to debug.

 

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 .

You will need those include headers, too.
The order of libraries is important, the linker will search only one pass by default.

example apps to use button pio.




staging directory

We built the uClibc in the toolchain. The headers and libs of uClibc will be included automatically. So you don't need "-I" and "-L" for uClibc.

When you need other lib, eg libssl, the uClinux-dist/staging directory will be created for libraries installation. This is NOT the staging_dir when we build the toochain.

Please look at the Makefile of uClinux-dist/lib/libssl. We defined "INSTALL_PREFIX=$(STAGEDIR)".

This is the destination of library installation. We would install all libraries there.

The header files of libraries will be installed in nios2-linux/uClinux-dist/staging/usr/include .
The libraries objects will be installed in nios2-linux/uClinux-dist/staging/usr/lib .

If you want to compile a program outside uClinux-dist, you will need to include the header and lib paths. Like this,

nios2-linux-uclibc-gcc abc.c -o abc -elf2flt -I/home/hippo/nios2-linux/uClinux-dist/staging/usr/include -L/home/hippo/nios2-linux/uClinux-dist/staging/usr/lib ....

If you add apps to uClinux-dist, these rules are already defined in the CFLAGS and LDFLAGS in the file uClinux-dist/vendors/Altera/nios2/config.arch .

Sometimes, the application executable will be installed in nios2-linux/uClinux-dist/staging/usr/bin. There will be romfs target in apps Makefile to copy the executable to uClinux-dist/romfs/bin or usr/bin.




Implicit Rules of Makefile


Please check info of "make".


Compiling C programs
     `N.o' is made automatically from `N.c' with a command of the form
     `$(CC) -c $(CPPFLAGS) $(CFLAGS)'.

Compiling C++ programs
     `N.o' is made automatically from `N.cc', `N.cpp', or `N.C' with a
     command of the form `$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)'.  We
     encourage you to use the suffix `.cc' for C++ source files instead
     of `.C'.

Assembling and preprocessing assembler programs
     `N.o' is made automatically from `N.s' by running the assembler,
     `as'.  The precise command is `$(AS) $(ASFLAGS)'.

     `N.s' is made automatically from `N.S' by running the C
     preprocessor, `cpp'.  The precise command is `$(CPP) $(CPPFLAGS)'.

Linking a single object file
     `N' is made automatically from `N.o' by running the linker
     (usually called `ld') via the C compiler.  The precise command
     used is `$(CC) $(LDFLAGS) N.o $(LOADLIBES) $(LDLIBS)'.







IO programming in user space.

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.

To do decent I/O programming you should either create an appropriate Kernel driver, map the I/O addresses into your application with portable means like mmap() (see Accessing hardware registers from user space programs ) or use uio.

If you really want to work around the ways of the OS and create non-portable code, you should consider the following instructions (while of course the Build System providesdecent  I/O access functions for Kernel drivers out of the box).

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)

 

Tag page

Files 1

FileSizeDateAttached by 
 nios2-commandline-io.c
No description
9.05 kB01:16, 8 Dec 2008AdminActions
Viewing 1 of 1 comments: view all
Nice story about this.
Great Nice Pictures
causes back pain knee high shoes lizlange maternity fishing boats sale flu incubation period remedies sore throat cost breast implants muscle weight gain surgery breast reduction girls party ideas women laptop bags car brake parts continuing nursing education cost breast augmentation protein whey powder baby slings carriers bed bugs pictures bedding duvet covers phone pay as you go outdoor swing sets discount dinnerware sets coffee espresso machines salton yogurt maker hair color pictures pictures of haircuts asvab practice test herpes photos easy cooking recipes toy dog breeds herpes simplex 1 personalized dog tags stress fracture foot occupational therapy schools frontline plus cats fleas on humans physical therapy salary scabies pictures pictures of shingles what is blood pressure ibuprofen side effects pictures of ringworm gendongan bayi kain cukin ring sling pouch sling perlengkapan bayi selendang bayi nursing cover selendang baby perlengkapan bayi baru lahir bayi bayi lucu foto bayi parish vintage ebooks download tv series download manga free download make money online health information cell phone review disorders anxiety psat practice test blood preasure smart water filter filter pur water weight loss effects blood pressure side effects socks over knee socks dress Hot News Today Jual Beli Make Money Celebrity Gossip Kesehatan Alternatif Gosip Selebritis celebrity photos celebrity news hot celebrities celebrity blog kesehatan info kesehatan kesehatan kerja pelayanan kesehatan indonesia artis foto artis gambar artis selebriti iklan iklan gratis jual online handphone daily news breaking news local news online news food dehydrator tray massage chair ijoy homedics massage shiatsu evening gown dress evening wedding dress dry face skin gold toe sock window coverings blinds blinds for windows lip gloss cosmetics folding table chairs women high heels stockings high heels treatment of pain chlorine generation makeup lighted mirror pain management specialist lawn pest control garden pest control joint pain causes
Posted 16:09, 20 Jul 2010
Viewing 1 of 1 comments: view all
You must login to post a comment.
SourceForge.net