歡迎您光臨本站 註冊首頁

把自己的應用程序載入到uClinux中運行

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

昨天終於順利完成了在SkyEye上面模擬運行uClinux的工作,今天決心乘勝追擊,把一個hello,world程序載入到uClinux中去運行。經過差不多一天的摸索,到晚飯時分終於成功!下面把開發過程總結一下。

首先是應用程序的存放位置。在uClinux的安裝目錄(我的機器上是/home/user/uClinux-dist)下有個user目錄,裡面存放的就是系統的應用程序,一般規律是一個應用程序一個目錄。

在Debian Linux系統中打開一個終端,默認就在用戶主目錄下(我的機器就是/home/user),然後在uClinux的user目錄下面建立了一個名為test的目錄:

cd uClinux/user/

mkdir test

然後在test目錄下建立一個名為hello.c的C程序,源代碼如下:

#include
int main(void)
{
printf("Hello,uClinux! ");
return(0);
}
然後使用交叉編譯器編譯hello.c源程序,生成名為hello的可執行文件:

arm-elf-gcc -Wall -elf2flt -o hello hello.c

再將該可執行文件複製到uClinux映像文件目錄(/home/user/uClinux/romfs/bin)中:

cp ./hello ../../romfs/bin/

然後在test目錄下建立一個Makefile文件,內容如下(其實是從user下的其他目錄中複製過來稍加修改得到的):

EXEC = hello
OBJS = hello.o

all: $(EXEC)

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

$(OBJS): hello.c

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

clean:
-rm -f $(EXEC) *.elf *.gdb *.o
將來大家可以照著這個模板來修改其中的關聯關係即可,其餘的嘛,可以不動。

接下來要對uClinux的一些配置文件進行修改,告訴uClinux系統添加了一個User Application。

編輯uClinux目錄下的文件user/Makefile,增加下面這行文字:
dir_$(CONFIG_USER_TEST_HELLO) += test

在這裡注意兩個問題:一是增加的位置,打開文件就會發現這個Makefile裡面有很多類似的語句,只要按照字母順序,把上面這行放到合適的位置即可;二是注意上面這行文字的顏色變化,黑色部分其實跟其他行是一樣的,紅色部分則是我們在user中添加的那個用戶程序目錄,注意前面出現是全大寫,後面出現是要與用戶目錄一致的,而藍色部分則是用戶程序編譯之後生成的那個可執行文件的名字,要全大寫。

編輯uClinux目錄下的文件config/Configure.help,在文件末尾追加下面這段文字:
CONFIG_USER_TEST_HELLO
This is an example C program in uClinux OS, and it will print "Hello, uClinux!" on screen.
注意:第一行要頂頭寫,第二行則要以2個空格開頭,總長度小於70個字元。

編輯uClinux目錄下的文件config/config.in,在文件末尾追加下面這段文字:
#########################
mainmenu_option next_comment
comment 'Test'

bool 'hello' CONFIG_USER_TEST_HELLO
endmenu
#########################
然後返回uClinux的根目錄下,重新編譯內核,不過這個過程就不用像第一遍編譯內核那麼複雜了,可以只編譯連接最後需要生成的那個鏡像文件即可:

genromfs -v -V "ROMdisk" -f ./images/romfs.img -d ./romfs

make image

make

前面兩步操作會給出一個內存映像表,在裡面可以發現有一個名為hello的應用程序。如果沒有錯誤的話會很快完成,最後一步make就要花些時間了,去食堂吃飯打個來回貌似還是沒問題的,呵呵~~

等最後的make完成,那麼新的內核即linux-2.4.x/linux文件已經生成了,使用SkyEye模擬運行新的內核:

skyeye -e ./linux-2.4.x/linux

等uClinux啟動完成出現「>」提示符時,輸入hello並回車,系統將會顯示什麼呢?看看下面的記錄吧,呵呵~~

$ skyeye -e ./linux-2.4.x/linux
big_endian is false.
arch: arm
cpu info: armv3, arm7tdmi, 41007700, fff8ff00, 0
mach info: name at91, mach_init addr 0x8062ea0
can't find device module: (name:lcd, type:(null))
uart_mod:0, desc_in:, desc_out:, converter:
SKYEYE: use arm7100 mmu ops
Loaded ROM images/romfs.img
exec file "./linux-2.4.x/linux"'s format is elf32-little.
load section .init: addr = 0x01000000 size = 0x0000a000.
load section .text: addr = 0x0100a000 size = 0x000e4d00.
load section .data: addr = 0x010f0000 size = 0x0000a6d8.
not load section .bss: addr = 0x010fa6e0 size = 0x00022700 .
not load section .debug_abbrev: addr = 0x00000000 size = 0x0003ec44 .
not load section .debug_info: addr = 0x00000000 size = 0x019d8a71 .
not load section .debug_line: addr = 0x00000000 size = 0x00304781 .
not load section .debug_pubnames: addr = 0x00000000 size = 0x0000c669 .
not load section .debug_aranges: addr = 0x00000000 size = 0x000024d8 .
start addr is set to 0x01000000 by exec file.
Linux version 2.4.32-uc0 (user@yxhcs11) (gcc version 2.95.3 20010315 (release)(ColdFire patches - 20010318 from http://fiddes.net/coldfire/)(uClinux XIP and shared lib patches from http://www.snapgear.com/)) #6 2007年 11月 16日 星期五 17:03:24 CST
Processor: Atmel AT91M40xxx revision 0
Architecture: EB01
On node 0 totalpages: 1024
zone(0): 0 pages.
zone(1): 1024 pages.
zone(2): 0 pages.
Kernel command line: root=/dev/rom0
Calibrating delay loop... 15.82 BogoMIPS
Memory: 4MB = 4MB total
Memory: 2900KB available (915K code, 184K data, 40K init)
Dentry cache hash table entries: 512 (order: 0, 4096 bytes)
Inode cache hash table entries: 512 (order: 0, 4096 bytes)
Mount cache hash table entries: 512 (order: 0, 4096 bytes)
Buffer cache hash table entries: 1024 (order: 0, 4096 bytes)
Page-cache hash table entries: 1024 (order: 0, 4096 bytes)
POSIX conformance testing by UNIFIX
Linux NET4.0 for Linux 2.4
Based upon Swansea University Computer Society NET3.039
Initializing RT netlink socket
Starting kswapd
Atmel USART driver version 0.99
ttyS0 at 0xfffd0000 (irq = 2) is a builtin Atmel APB USART
ttyS1 at 0xfffcc000 (irq = 3) is a builtin Atmel APB USART
Blkmem copyright 1998,1999 D. Jeff Dionne
Blkmem copyright 1998 Kenneth Albanowski
Blkmem 1 disk images:
0: 1400000-151EFFF [VIRTUAL 1400000-151EFFF] (RO)
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
SkyEye NE2k Ethernet driver version 0.2 (2003-04-27)
sene2k dev name: eth0: <6>NET4: Linux TCP/IP 1.0 for NET4.0
IP Protocols: ICMP, UDP, TCP
IP: routing cache hash table of 512 buckets, 4Kbytes
TCP: Hash tables configured (established 512 bind 512)
NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.
VFS: Mounted root (romfs filesystem) readonly.
Shell invoked to run file: /etc/rc
Command: hostname GDB-ARMulator
Command: /bin/expand /etc/ramfs.img /dev/ram0
Command: mount -t proc proc /proc
mount: /etc/mtab: Read-only file system
Command: mount -t ext2 /dev/ram0 /var
mount: /etc/mtab: Read-only file system
Command: mkdir /var/tmp
Command: mkdir /var/log
Command: mkdir /var/run
Command: mkdir /var/lock
Command: mkdir /var/empty
Command: cat /etc/motd
Welcome to
____ _ _
/ __| ||_|
_ _| | | | _ ____ _ _ _ _
| | | | | | || | _ | | | | / /
| |_| | |__| || | | | | |_| |/
| _______|_||_|_| |_|____|_/_/
| |
|_|

GDB/ARMulator support by <davidm@snapgear.com>
For further information check:
http://www.uclinux.org/

Command: /bin/ifconfig eth0 up 10.0.0.2
Execution Finished, Exiting

Sash command shell (version 1.1.1)
/> hello
Hello,uClinux!
/>
往後,只要我們繼續擴充這個hello.c,或者繼續在test增加其他的文件比如說.h或者.c文件都可以的,不過記得要修改Makefile啊。

[火星人 ] 把自己的應用程序載入到uClinux中運行已經有1089次圍觀

http://coctec.com/docs/linux/show-post-187783.html