(原創) 如何在μClinux開發第一支Hello World程式? (IC Design) (DE2) (Nios II) (OS) (Linux) (μClinux) (C/C++) (gcc)

Abstract
μClinux順利的在DE2上執行是不夠的,重要的是如何將我們寫的程式放到μClinux上執行。

Introduction
(原創) 如何在DE2上安裝μClinux作業系統? (IC Design) (DE2) (Nios II) (OS) (Linux) (μClinux)中,我們已經將μClinux順利的跑在DE2上,這樣就結束了嗎?開發人員關心的當然是如何將自己寫的程式跑在μClinux上。

這牽涉到兩個問題要解決:
1.如何使用nios2-linux-uclibc-gcc這個cross compiler?
2.如何將自己的執行檔放到image上?

我們試著看看如何讓Hello World順利的執行在μClinux上。

如何使用nios2-linux-uclibc-gcc這個cross compiler?
Step 1:
首先在/usr/local/src下用vi寫一個hello_world.c

1 #include <stdio.h>
2 
3 int main() {
4   printf("Hello World\n");
5 }


Step 2:
使用gcc編譯Linux版的hello_world.c

[root@localhost src]# gcc hello_world.c -o hello_world_linux


Step 3:
執行Linux版的hello_world_linux

[root@localhost src]# ./hello_world_linux


執行結果

Hello World


這樣確定了我們的hello_world.c程式碼完全正確,可以在Linux順利執行。

Step 4:
使用nios2-linux-uclibc-gcc cross compiler編譯μClinux版hello_world.c

[root@localhost src]# nios2-linux-uclibc-gcc -W1 -elf2flt hello_world.c -o hello_world_uclinux


nios2-linux-uclibc-gcc是一個Nios II平台的cross compiler,安裝的方法請參閱(原創) 如何在DE2上安裝μClinux作業系統? (IC Design) (DE2) (Nios II) (OS) (Linux) (μClinux)

-Wl代表的是linker的參數,-elf2flt是因為Nios II平台為elf格式,但μClinux為flt格式,所以要做轉換。

Step 5:
嘗試執行hello_world_uclinux

[root@localhost src]# ./hello_world_uclinux
-bash : ./hello_world_uclinux: cannot execute binary file


一如我們預期無法正常執行,這表示cross compile成功。

如何將自己的執行檔放到image上?
所有要打包成image的檔案,都會放在/usr/local/src/uClinux-dist/romfs下,也就是說,我們只要將執行檔、函式庫、圖片....放在這個目錄下,將來就會打包成zImage下載到DE2的SDRAM內。

Step 1:
將hello_world_uclinux放到/usr/local/src/uClinux-dist/romfs/usr//bin下

[root@localhost src]# cp hello_world_uclinux /usr/local/src/uClinux-dist/romfs/usr/bin


Step 2:
打包成image

[root@localhost src]# cd uClinux-dist;make linux image


最後會在/usr/local/src/uClinux-dist/linux-2.6.x/arch/nios2nommu/boot/下找到zImage

Step 3:
將zImage複製到Windows的C:\altera\72\nios2eds\examples\下

將軟硬體燒入到DE2
Step 1:
將DE2_NIOS2.sof複製到C:\altera\72\nios2eds\examples\下

Step 2:
啟動Nios II Command Shell
開始 -> 程式集 -> Altera -> Nios II EDS 7.2 -> Nios II 7.2 Command Shell

Step 3:
將硬體燒入到DE2

[SOPC Builder]$ nios2-configure-sof DE2_NIOS.sof


執行結果

Searching for SOF file:
in .
  DE2_NIOS.sof

Info: *******************************************************************
Info: Running Quartus II Programmer
Info: Command: quartus_pgm --no_banner --mode
=jtag -o p;DE2_NIOS.sof
Info: Using programming cable "USB-Blaster [USB-0]"
Info: Started Programmer operation at Mon Jun 
09 03:01:45 2008
Info: Configuring device index 
1
Info: Device 
1 contains JTAG ID code 0x020B40DD
Info: Configuration succeeded -- 
1 device(s) configured
Info: Successfully performed operation(s)
Info: Ended Programmer operation at Mon Jun 
09 03:01:46 2008
Info: Quartus II Programmer was successful. 
0 errors, 0 warnings
    Info: Allocated 
54 megabytes of memory during processing
    Info: Processing ended: Mon Jun 
09 03:01:46 2008
    Info: Elapsed time: 
00:00:02


Step 4:
將μClinux的zImage下載到SDRAM

[SOPC Builder]$ nios2-download -g zImage


執行結果

Using cable "USB-Blaster [USB-0]", device 1, instance 0x00
Pausing target processor: OK
Initializing CPU cache (if present)
OK
Downloaded 1197KB in 
14.4s (83.1KB/s)
Verified OK
Starting processor at address 0x00D00000


Step 5:
啟動μClinux

[SOPC Builder]$ nios2-terminal


執行結果

Uncompressing Linux Ok, booting the kernel.
Linux version 
2.6.19-uc1 (root@localhost.localdomain) (gcc version 3.4.6) #2 PR
EMPT Sun Jun 
8 23:28:30 CST 2008


uClinux/Nios II
Altera Nios II support (C) 
2004 Microtronix Datacom Ltd.
Built 
1 zonelists.  Total pages: 2032
Kernel command line:
PID hash table entries: 
32 (order: 5, 128 bytes)
Dentry cache hash table entries: 
1024 (order: 0, 4096 bytes)
Inode-cache hash table entries: 
1024 (order: 0, 4096 bytes)
Memory available: 5956k/8192k RAM
, 0k/0k ROM (1465k kernel code, 680k data)
Mount-cache hash table entries: 
512
NET: Registered protocol family 
16
NET: Registered protocol family 
2
IP route cache hash table entries: 
1024 (order: 0, 4096 bytes)
TCP established hash table entries: 
1024 (order: 0, 4096 bytes)
TCP bind hash table entries: 
1024 (order: 0, 4096 bytes)
TCP: Hash tables configured (established 
1024 bind 1024)
TCP reno registered
io scheduler noop registered
io scheduler deadline registered (default)
Serial: JTAG UART driver $Revision: 
1.3 $
ttyJ0 at MMIO 0x806810f0 (irq 
= 1) is a jtag_uart
TCP cubic registered
NET: Registered protocol family 
1
NET: Registered protocol family 
17
Freeing unused kernel memory: 572k freed (0x97a000 - 0xa08000)
Shell invoked to run file: /etc/rc
Command: hostname uClinux
Command: mount -t proc proc /proc
Command: mount -t sysfs sysfs /sys
Command: mount -t usbfs none /proc/bus/usb
mount: Mounting none on /proc/bus/usb failed: No such file or directory
Command: mkdir /var/tmp
Command: mkdir /var/log
Command: mkdir /var/run
Command: mkdir /var/lock
Command: mkdir /var/empty
Command: ifconfig lo 
127.0.0.1
Command: route add -net 
127.0.0.0 netmask 255.0.0.0 lo
Command: cat /etc/motd
Welcome to
          ____ _  _
         /  __| ||_|
    _   _| |  | | _ ____  _   _  _  _
   | | | | |  | || |  _ \| | | |\ \/ /
   | |_| | |__| || | | | | |_| |/    \
   |  ___\____|_||_|_| |_|\____|\_/\_/
   | |
   |_|

For further information check:
http://www.uclinux.org/

Execution Finished
, Exiting

Sash command shell (version 
1.1.1)
/>


若看到這個畫面,表示μClinux已經順利執行在DE2上。

Step 6:
執行μClinux版的hello_world_uclinux

/> cd usr/bin
/usr/bin> hello_world_uclinux
Hello World
/usr/bin>


Conclusion
到目前為止,我們已經會使用gcc做cross compiler和打包image。事實上,無論什麼開發版,遷入式系統大抵都是這種開發方式,這和Windows Programming與Web Programming有相當大的差別。

See Also
(原創) 如何在DE2上安裝μClinux作業系統? (IC Design) (DE2) (Nios II) (OS) (Linux) (μClinux)

posted on 2008-06-09 11:28  真 OO无双  阅读(7866)  评论(20编辑  收藏  举报

导航