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

來源:互聯網
上載者:User

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>

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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.