uCLinux 在 NiosⅡ上的移植
摘要:SOPC 必将成为未来 IC 发展的趋势,实时操作系统 uClinux 在软核 Nios
Ⅱ上的成功移植对于嵌入式产品的开发有重要的意义。
本文首先介绍了 NiosⅡ和 uClinux,然后论述了在 DE2 开发板上利用 Quartus
Ⅱ、SOPC Builder 等软件工具来构建一个以 NiosⅡ处理器为核心的硬件平台,
并且在这个平台上移植了 uClinux。
关键词:uClinux;NiosⅡ;SOPC
1、 NiosⅡ和 uClinux 的介绍
(1) 什么是 NiosⅡ
在二十世纪九十年度末,可编程逻辑器件(PLD)的复杂度已经能够在单个
可编程器件内实现整个系统。完整的单芯片系统(SOC)概念是指在一个芯片中
实现用户定义的系统,它通常暗指包括片内存储器和外设的微处理器。最初宣称
真正的 SOC――或可编程单芯片系统(SOPC)――能够提供基于 PLD 的处理器。
在 2000 年,Altera 发布了 NiosⅠ处理器,这是 Altera Excalibur 嵌入处理器计划
中第一个产品,它成为业界第一款为可编程逻辑优化的可配置处理器。
NiosⅡ是建立在前一代基础之上,能以少量的资源提供更多性能的 32 位
CPU。
NiosⅡ系列包括三种软 CPU 核,一种是最大系统性能的高速版(Fast),一种
是为最少逻辑使用量优化的经济版(Economy),还有一种是前两种 CPU 的平衡的
标准版(Standard)。所有核都是 100%代码兼容,让设计者根据系统需求变化改变
CPU,而不会影响现有的软件投入。另外,NiosⅡ处理器具有完善的软件开发套
件,包括编译器、集成开发环境(IDE)、JTAG 调试器、实时操作系统(RTOS)和
TCP/IP 协议栈[1]。
(2) 什么是 uClinux
Linux 是一种很受欢迎的操作系统,它与 UNIX 系统兼容,开放源代码。它
原本被设计为桌面系统,现在广泛应用于服务器领域。而更大的影响在于它正逐
渐的应用于嵌入式设备。uClinux 正是在这种氛围下产生的。在 uClinux 这个英
文单词中 u 表示 Micro,小的意思,C 表示 Control,控制的意思,所以 uClinux
就是 Micro-Control-Linux,字面上的理解就是“针对微控制领域而设计的 Linux
系统”。
uClinux 以其优异的性能、免费开放的代码等优点,博得众多嵌入式开发者
的青睐。和过去基于简单 RTOS 甚至没有使用任何操作系统的嵌入式程序设计相
比,基于 uClinux 这样的成熟的、高效的、健壮的、可靠的、模块化的、易于配
置的操作系统来开发自己的应用程序,无疑能进一步提高效率,并具有很好的可
移植性。
2、 编译环境搭建
1. 在 VMware 上安装 Ubuntu9.04。安装库 ncurses, ncurses-devel, bison, gawk。
bison gawk
install ncurses-base
libncurses5-dev
>
sudo aptitude
build-essential
2. 通过下面的命令下载编译好的 BinaryToolchain。
wget http://www.niosftp.com/pub/gnutools/nios2gcc-20080203.tar.bz2
3. 解压下载的 BinaryToolchain。
可以将 nios2gcc-20080203.tar.bz2 放在一个单独的位置,便于系统文件干净整
洁!在这里,我是用 root 进入 Ubuntu 的,所以我将 nios2gcc-20080203.tar.bz2 放
在/usr/local/src 里面。
打开终端,下面命令进入/usr/local/src.
cd /usr/local/src
解压 nios2gcc-20080203.tar.bz2
tar jxvf nios2gcc-20080203.tar.bz2
4. 设置交叉 gcc 编译的路径
于 Ubuntu 的用户来说,开启终端是要重新加载 profile 这个文件,所以我们
需要更改这个文件。打开 profile 文件,还是在 use/local/src 路径下
gedit ~/.profile
加上 cross gcc 的路径,在最后面加上如下语句
export PATH=$PATH:/usr/local/src/opt/nios2/bin
当你更改 profile 文件之后,需要重新 logout 以及 login,在这里,我们可以
用下面一个命令,让 profile 文件生效。
source ~/.profile
5. 检查 cross gcc 路径是否设置正确
echo $PATH
出现下面的路径
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:use/local/src/
opt/nios2/bin
6. 检查 cross gcc 安装成功
nios2-linux-uclibc-gcc -v
如果安装成功的话,就会出现下面的结果
3、 编译 uClinux 内核以及生成 uClinux 镜像
1、下载 uClinux 内核代码
使用命令:
wget http://www.niosftp.com/pub/linux/nios2-linux-20090929.tar
2、解压 nios2-linux-20100621.tar
还是在 use/local/src 这个路径下,执行下面的命令解压 nios2-linux-20100621.tar.
tar -C /usr/local/src -xvf nios2-linux-20100621.tar
这样,就 use/local/src 下生成一个目录为 nios2-linux 的目录。
3、配置内核
进入内核目录。
cd uClinux-dist
执行下面的命令,配置内核。
make menuconfig
4、根据 sopc 生成的 ptf 文件设置硬件 makefile
将 sopc 生成的 ptf 文件拷贝到当前的/use/local/src 目录下
执行一下命令,设置硬件的 makefile
make vendor_hwselect SYSPTF=/use/local/src/system_0.ptf
5、编译内核
执行下面的命令
cd uClinux-dist
make
这步需要等待一下,有一定的时间。至此,内核就编译成功。
最终在/use/local/src/nios2-linux/uClinux-dist/linux-2.6.x/arch/nios2/boot 下生成镜像文件
zImage。
4、 下载硬件 sof 文件和镜像文件
1、下载硬件到 DE2
nios2-configure-sof (--cable x) DE2_NIOS.sof
2、下载镜像文件到 DE2 中
nios2-download -g (--cable x) zImage
3、观察 uClinux 启动
nios2-terminal (--cable x)
至此,uClinux 在 FPGA 上的移植就成功,从 nios uart 中传出一下 uClinux 启动信息:
Using cable "USB-Blaster [USB 2-1.2]", device 1, instance 0x00
Pausing target processor: OK
Initializing CPU cache (if present)
OK
Downloaded 1272KB in 7.7s (165.1KB/s)
Verified OK
Starting processor at address 0x00500000
# nios2-terminal
nios2-terminal: connected to hardware target using JTAG UART on cable
nios2-terminal: "USB-Blaster [USB 2-1.2]", device 1, instance 0
nios2-terminal: (Use the IDE stop button or Ctrl-C to terminate)
Uncompressing Linux... Ok, booting the kernel.
Linux version 2.6.26-rc6 (root@legant-desktop) (gcc version 3.4.6) #23 PREEMPT Fri Oct 14
18:27:24 ULAT 2011
uClinux/Nios II
Built 1 zonelists in Zone order, mobility grouping off. Total pages: 8128
Kernel command line:
PID hash table entries: 128 (order: 7, 512 bytes)
Dentry cache hash table entries: 4096 (order: 2, 16384 bytes)
Inode-cache hash table entries: 2048 (order: 1, 8192 bytes)
Memory available: 30136k/2333k RAM, 0k/0k ROM (1552k kernel code, 781k data)
Mount-cache hash table entries: 512
net_namespace: 180 bytes
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: 1, 8192 bytes)
TCP bind hash table entries: 1024 (order: 0, 4096 bytes)
TCP: Hash tables configured (established 1024 bind 1024)
TCP reno registered
NET: Registered protocol family 1
io scheduler noop registered
io scheduler deadline registered (default)
ttyJ0 at MMIO 0x8009340 (irq = 8) is a Altera JTAG UART
console [ttyJ0] enabled
ttyS0 at MMIO 0x8008e00 (irq = 7) is a Altera UART
dm9000 Ethernet Driver, V1.30
TCP cubic registered
NET: Registered protocol family 17
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
Freeing unused kernel memory: 572k freed (0x1b8000 - 0x246000)
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)
/>
5、 总结
整个 uClinux 的移植过程包括硬件平台的建立、测试,uClinux 的移植以及驱动的编写、
应用程序的开发三个部分。这三个环节是紧密相连的,后续步骤都是建在前一步骤的基础之
上的。本文侧重的是 uClinux 的移植,对于其他的两个方向都只是简单的介绍。本文从移植,
uClinux 内核结构等概念入手介绍了相关的理论知识,为后文详细移植过程和操作做了充分
的理论准备。
参考文献
[1] 任爱锋,初秀琴,常存,等.基于 FPGA 的嵌入式系统设计.西安:西安电子
科 技大学出版社,2004 年
[2] Altera Corporation.DE2使用手册.www.terasic.com.2005.08.12
[3] Hyok S.Choi.uClinux2.6入门.www.linuxsir.org.2004.06.30
[4] 11Wyouken.移植uClinux到Nios.www.icwin.net.2006.2.26
姜黎.n2cpu_nii51010 Tutorial.USA.Altera Corporation.2005.P12~P54