Linux+RTAI实时内核编译
原文地址:http://qiuhanstar.blog.163.com/blog/static/30854695201041023731757/
这个博客上写的问题,我基本上都遇到了,也按照其所说的方法都修改通过了。
由于在编译内核的过程中,碰到了comedi组件问题,老是编译不通过,所以就查找了一下这个是什么东西,发现其实在使用中并没有用到这个部分,所以就没有将其编译进去。这样就绕过了这个问题,以下是comedi官网对他的介绍。
Comedi linux control and measurement device interface
Introduction
The Comedi project develops open-sourcedrivers, tools, and libraries for data acquisition.
Comedi is a collection of drivers for avariety of common data acquisition plug-in boards. The drivers are implementedas a core Linux kernel module providing common functionality and individuallow-level driver modules.
Comedilib is a user-space library thatprovides a developer-friendly interface to Comedi devices. Included in theComedilib distribution is documentation, configuration and calibrationutilities, and demonstration programs.
Kcomedilib is a Linux kernel module(distributed with Comedi) that provides the same interface as Comedilib inkernel space, suitable for real-time tasks. It is effectively a "kernellibrary" for using Comedi from real-time tasks.
step 17之前的几个步骤都是和matlab有关的,我选择忽略不看。the step17 是一个例子,可以看一下
由于最新的RTAI3.8实时包支持的版本有:hal-linux-2.6.24-x86-2.0-07.patch hal-linux-2.6.30.5-x86-2.4-05.patch hal-linux-2.6.25-x86-2.0-09.patch hal-linux-2.6.31.8-x86-2.4-09.patch hal-linux-2.6.28.9-x86-2.2-07.patch hal-linux-2.6.32.2-x86-2.5-00.patch hal-linux-2.6.29.5-x86-2.4-03.patch
在尝试2.6.32.8版本内核失败后,选择比现用内核(2.6.31.17)低的2.6.30.5版本内核。
win@win-desktop:~$ sudo su
root@win-desktop:/home/win#
root@win-desktop:/usr/src# tar -jxf linux-2.6.30.5.tar.bz2
root@win-desktop:/usr/src# tar -jxf rtai-3.8.tar.bz2
解压完后进入linux-2.6.30.5
root@win-desktop:/usr/src# cd linux-2.6.30.5
将内核打上刚解压的实时包rtai-3.8
root@win-desktop:/usr/src/linux-2.6.30.5# patch -p1 -b ../rtai-3.8/base/arch/x86/patches/hal-linux-2.6.30.5-x86-2.4-05.patch
root@win-desktop:/usr/src/linux-2.6.30.5# make mrproper
进行内核配置,可以使用make menuconfig 也可用make xconfig,两者的区别在于前者是只能使用键盘操作,而且不够直观。
在默认都基础上必须选上:Processor type and features -->Processor fanmily选择cpu为Opteron/Athlon64/Hammer/K8
须去掉的选项有:Processor type and features -->Symmetric multi-processing support(因为cpu是单核)
Processor type and features -->High Memory Support-->off
root@win-desktop:/usr/src/linux-2.6.30.5# make xconfig
编译,安装内核
root@win-desktop:/usr/src/linux-2.6.30.5# make
root@win-desktop:/usr/src/linux-2.6.30.5# make install
root@win-desktop:/usr/src/linux-2.6.30.5# make modules
root@win-desktop:/usr/src/linux-2.6.30.5# make modules_install
root@win-desktop:/usr/src/linux-2.6.30.5# cd /
root@win-desktop:/# /usr/sbin/mkinitramfs -o /boot/initrd.img-2.6.30.5 2.6.30.5
root@win-desktop:/# update-grub
root@win-desktop:/# reboot
root@win-desktop:/# cd /usr/src/rtai-3.8
root@win-desktop:/# make xconfig
root@win-desktop:/# make
root@win-desktop:/# make install
root@win-desktop:/usr/realtime/testsuite/user/latency# ./run
**
* Type ^C to stop this application.
**
## RTAI latency calibration tool ##
# period = 100000 (ns)
# use the FPU
# timer_mode is oneshot
RTH| lat min| ovl min| lat avg| lat max| ovl max| overruns
RTD| -1284| -1284| 131| 9317| 9317| 0
RTD| -1094| -1284| 153| 8668| 9317| 0
RTD| -1679| -1720| -52| 8974| 9317| 0
RTD| -1656| -1720| -92| 7299| 9317| 0
RTD| -1570| -1720| -70| 7319| 9317| 0
RTD| -1745| -1745| -28| 103025| 103025| 1
RTD| -1616| -1745| -102| 3701| 103025| 1
RTD| -1654| -1745| -77| 7446| 103025| 1
RTD| -1636| -1745| -86| 7425| 103025| 1
RTD| -1733| -1745| 58| 9216| 103025| 1
RTD| -1271| -1745| 174| 8213| 103025| 1
RTD| -1672| -1745| 192| 204544| 204544| 3
RTD| -1602| -1745| 381| 31605| 204544| 3
RTD| -1511| -1745| 454| 15495| 204544| 3
RTD| -1284| -1745| 176| 8175| 204544| 3
RTD| -1243| -1745| 245| 76948| 204544| 3
RTD| -1264| -1745| 175| 8867| 204544| 3
RTD| -1295| -1745| 185| 8658| 204544| 3
RTD| -1197| -1745| 162| 8238| 204544| 3
RTH| lat min| ovl min| lat avg| lat max| ovl max| overruns
RTD| -1689| -1745| 34| 9489| 204544| 3
RTD| -1345| -1745| -216| 9827| 204544| 3
RTD| -1692| -1745| -231| 9413| 204544| 3
RTD| -1679| -1745| -300| 7189| 204544| 3
RTD| -1686| -1745| -62| 82956| 204544| 3
RTD| -1719| -1745| -173| 8709| 204544| 3
RTD| -1692| -1745| -51| 8688| 204544| 3
RTD| -1618| -1745| -69| 43582| 204544| 3
RTD| -1684| -1745| -210| 8340| 204544| 3
RTD| -1709| -1745| -318| 8200| 204544| 3
RTD| -1691| -1745| -165| 9502| 204544| 3
RTD| -1709| -1745| 605| 404965| 404965| 7
RTD| -1420| -1745| 810| 115960| 404965| 8
RTD| -1699| -1745| -132| 8931| 404965| 8
RTD| -1666| -1745| -181| 32339| 404965| 8
RTD| -1712| -1745| -62| 21061| 404965| 8
RTH| lat min| ovl min| lat avg| lat max| ovl max| overruns
RTD| -1682| -1745| -235| 8146| 404965| 8
RTD| -1422| -1745| -241| 8893| 404965| 8
RTD| -1689| -1745| -269| 7908| 404965| 8
RTD| -1724| -1745| -410| 13455| 404965| 8
RTD| -1689| -1745| -321| 7894| 404965| 8
^CRTD| -1689| -1745| -321| 7894| 404965| 8
>>> S = 98.696, EXECTIME = 0.0488363
以上可以看出随着进程的增加,overruns数目明显增加。
经过6次的编译,最终成功编译内核,并加入实时包RTAI.在编译过程中也遇到了不少困难,走了很多弯路,以下是几次编译中遇到的问题和解决的方法。
第一次编译:在网上查看一些资料后,下载最新的内核2.6.32.8根据自己的判断,在配置中删除很多不相干的选项,其中把文件系统Ext4类型文件系统去 掉。因为Ubuntu系统已经安装了一段时间,误以为还是属于Ext3类型文件系统。没有打实时包补丁,然后在Ubuntu环境下编译,并安装。在最后更 新开机列表时,网上找的资料都说在/boot/grub/menu.lst文件中添加新编译的内核,可是,在Ubuntu系统下根本不存在这个文件,后来 才找到是grub.cfg文件,但此文件是只读文件,是由grub-mkconfig命令所产生的。在产生列表之前先用update-grub命令更新列 表。但最终还是因为找不到合适的文件系统而无法启动。(Error:cannot find the Ext4 fs !)。
第二次编译:由于编译新内核后,不会自动删除原来的内核。因此仍然通过原来的内核系统登录,将原来内核配置的基础上,选Ext4文件系统,并重新编译。最终成功启动,并正常使用。
第 三次编译:将最新的内核(2.6.32.8)打上实时补丁包RTAI3.8,由于补丁包支持到2.6.32.2版本,所以内核版本与补丁版本不完全一致。 按照原来配置的方法,进行配置也是删除了很多无关项,但最终失败,系统在进入登录界面后就卡住,应该是在系统条用initrd.img-2.6.32.8 文件时出现错误,由于没有任何提示,所以还是不能完全确定什么原因,只是初步判断是实时包与系统内核不兼容。
第 四次编译,重新下载内核,这次选择实时包可以完全支持的2.6.30.5版本,由于内核与之前不同,因此.config文件的选项也会有一些差别。重新裁 剪配置,并按照有关资料说法,去掉所有APCI选项,编译后还是有问题,在登录时提示:Warring:unable to find a suitable fs in /proc/mounts,is it mounted?
Mount of root filesystem failed.
系统无法登录,但还是不明白具体所出的问题。经过的一翻寻找后发现,使用dmesg命令显示具体信息时最后一行为:
Filesystem with huge files cannot be mounted read-write without(ONFIG-LBD)
根 据这个信息才明白是在Enable the block layer 的Support for large block devices and files必须选上!由于版本的不同,在2.6.32.8版本内核的.config配置文件已经默认选上,但在2.6.30.5版本内核的.config 却没有选上。
第 五次编译:在原来的基础上只增选Support for large block devices and files并重新编译,最终可以成功启动,却不能找到有效的声卡配置,也不能检测到USB,而且新编译的系统运行速度明显比原内核系统慢,甚至不能正常关 机:可以终止所有的进程以及关闭cpu电源,但主板电源仍然没有关断!
第 六次编译:根据前面配置的经验,这次配置尽量少低裁剪,并选上必须选择的功能,最后实现正常启动,并运行。由此可以得出结论,linux内核编译的难点在 于内核配置方面,对系统硬件的不熟悉是造成编译有难度的主要原因。经过多次编译发现,要想编译一个普通冗余的系统不难,尽量照搬原来的内核配置即可,但要 裁剪到一个非常精简的内核却是一件很难度的事情。
https://www.rtai.org/RTAILAB/RTAI-TARGET-HOWTO.txt
STEP BY STEP RTAI TARGET FOR BEGINNERS
(c) 2006 Giampiero Campa campa at cemr dot wvu dot edu
STEP 1 - Install Linux
----------------------
Any recent distribution is supposed to be ok, however it is
extremely common to run into problems related to the particular
machine+kernel+compiler+distribution combination that you will be using.
I would suggest using a distribution that was released before the
last rtai version. I am currently using Fedora 3, since i have
personally found slightly less problems with the RedHat-Fedora line.
Be sure to install both the 'x' and 'qt' developement packages.
Also, keep in mind that older distributions based on 2.4 or 2.2 kernels
require slightly different procedures for most of the points below.
In what follows, the shell prompt sign is indicated with the symbol "$".
STEP 2 - Download and unpack RTAI
---------------------------------
Go to http://www.rtai.org/ and look for the newest version
(3.3 or greater).
You should unpack your RTAI source in /usr/src, (standard /usr/src use),
so AS ROOT, download the rtai-x.x.tar.bz2 file into /usr/src
and then unpack it using the following commands in a terminal window :
$ cd /usr/src
$ tar --bzip2 -xvf rtai-x.x.tar.bz2
Create a symbolic link to the new rtai directory,
since it will be useful later:
$ rm -f rtai
$ ln -fs rtai-x.x rtai
After that, have a look in the directory
/usr/src/rtai/base/arch/i386/patches/
to locate the patches for the supported kernels.
You can have a look at the README.INSTALL file in the
rtai root folder for advanced installation options.
STEP 3 - Download and unpack a new Kernel
-----------------------------------------
Never use the kernel that comes with the distribution,
always download a new kernel from http://www.kernel.org/
Make sure that the kernel is actually supported by RTAI
that is make sure that a patch exist for that kernel.
The first three numbers in the kernel have to match the
first three numbers in the patch. If more than one patch
exist with the same first three numbers pick the last one.
If more than one kernel exist with the same first three numbers
it is safer to pick the base version (i.e. the one that does not
have the fourth number).
For example, the following patches exist for the 2.6.15 kernels
in the /usr/src/rtai/base/arch/i386/patches/ directory:
hal-linux-2.6.15-i386-1.1-03.patch and hal-linux-2.6.15-i386-1.2-00.patch
so the last patch should be used, with the base kernel 2.6.15, although
it should work fine with kernels until 2.6.15.7 too.
Again, source code traditionally go in /usr/src, so, as root,
download the kernel (linux-x.x.xx.tar.bz2) into /usr/src
and do the following:
$ cd /usr/src
$ tar --bzip2 -xvf linux-x.x.xx.tar.bz2
You need to set a symbolic link to the new linux root folder
since it's needed later:
$ rm -f linux
$ ln -fs linux-x.x.xx linux
STEP 4 - Patch your Kernel
--------------------------
Again, as root (assuming an x86 architecture):
$ cd /usr/src/linux
$ patch -p1 < /usr/src/rtai/base/arch/i386/patches/hal-linux-x.x.xx_rx.patch
STEP 5 - Configure your Kernel
------------------------------
Again, as root:
$ cd /usr/src/linux
It is a good idea to copy the distribution .config file into the linux folder,
so one could start configuring something that already works:
$ cp -f /boot/config-x.x.xx-x.xxx .config
However, that is not striclty necessary, so you can safely skip the previous step.
Then, to configure the kernel do the following:
$ make config (or make menuconfig or make xconfig)
In the code maturity level options, set the "prompt for development and/or
incomplete code/drivers" option to 'yes'. In the "loadable module support" section,
make sure that the "Enable loadable module support" is set to 'yes', and set the
"Module Versioning support" is to 'no'. In the "processor type and features" section,
set the "Preemptible kernel" and the "Use register arguments" options to 'no',
and make sure to set the "interrupt pipeline" option, (aka IPIPE) to 'yes'.
Finally, make sure that the "/proc file system support", under the
"Pseudo filesystems" subsection of the "File systems" section is set to yes.
Remember to save the kernel configuration on exit.
STEP 6 - Compile and install your Kernel
----------------------------------------
Again, as root :
$ cd /usr/src/linux
then compile the kernel:
$ make clean && make && make modules_install && make install
if everything goes fine (i.e. no errors encountered),
the new kernel image, together with its system map file, should
appear in the boot directory, and the boot loader configuration file
(/etc/grub.conf if you are using fedora), will be changed to allow
booting with the new kernel.
Now reboot:
$ /sbin/reboot
(make sure to reboot into your new rtai-patched-kernel).
If everything works fine, it is a good idea to copy the
configuration file for future reference, so as root:
$ cd /usr/src/linux
$ cp -f .config config-my-rtlx-yy.txt
It is also a good idea to change the name of the kernel image,
ramdisk image, and system.map so that they won't be overwritten
the next time you compile a new kernel, so as root:
$ cd /boot
$ cp -f vmlinuz-x.x.xx my-rtlx-yy
$ cp -f initrd-x.x.xx.img my-inrd-yy.img
$ cp -f System.map-x.x.xx System.map-my-rtlx-yy
Of course you have to edit accordingly the boot configuration file,
for example under Fedora Core 3, assuming grub is your boot loader,
you can copy the automatically added entry, relative to the new kernel,
which is usually the first one, to a new entry that won't be overwritten
when you compile and install a new kernel.
For example this is a velid entry for the grub bootloader in /etc/grub.conf :
title my-rtlx-xx (2.6.9-adeos)
root (hd0,0)
kernel /my-rtlx-xx ro root=/dev/VolGroup00/LogVol00 rhgb quiet
initrd /my-inrd-xx.img
reboot again chosing the entry you just added to make sure everything
works as it should.
STEP 7 - Download and install the MESA library
----------------------------------------------
At this point, if your system already has some pre-installed
version of the mesa library, the thing that makes most sense
is to try to skip this step, and only later on, if step 8
fails due to a Mesa library that's too new for efltk to compile,
return here and install MesaLib-6.2.tar.gz, in any case,
whatever you do, do not uninstall the preinstalled MesaLib.
Download MesaLib-6.2.tar.gz (from www.mesa3d.org)
in a temporary directory (/tmp), then, as root :
$ cd /tmp
$ tar xvzf MesaLib-6.2.tar.gz
$ cd Mesa-6.2
$ make linux-x86-static
$ make install
Accept the default locations for the header and library files.
After the installation reboot and make sure everything works.
STEP 8 - Download and install the EFLTK package
-----------------------------------------------
As root, download EFLTK from CVS in a temporary directory (/tmp)
$ cd /tmp
$ cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/ede login
(press ENTER when CVS asks for password)
$ cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/ede co efltk
$ cd efltk
$ ./efltk-config.in --prefix=/usr/local --multithread
$ ./emake
$ ./emake install
add the line /usr/local/lib/ to the file /etc/ld.so.conf
the entries in the file should look more or less like this:
/usr/local/lib
include ld.so.conf.d/*.conf
/usr/X11R6/lib
/usr/lib/wine
then run ldconfig to update the library database:
$ /sbin/ldconfig
Again, reboot and make sure everything works fine.
STEP 9 - Configure and install RTAI
------------------------------------
Again, as root :
$ cd /usr/src/rtai
$ make config (or make menuconfig of make xconfig)
In the Machine Menu adjust the number of processors.
Say yes to rtai lab, and leave the efltk folder to /usr/local
Do not include comedi support at this time even if you plan to use it later.
Do not change any other default choice unless you know what you are doing.
$ make
$ make install
reboot :
$ /usr/bin/reboot
STEP 10 - Test the RTAI installation
------------------------------------
Again, as root, make sure you are running the patched kernel, then :
$ cd /usr/realtime/testsuite/user/latency
$ ./run (use ctrl-c to stop it)
$ cd /usr/realtime/testsuite/user/preempt
$ ./run (use ctrl-c to stop it)
$ cd /usr/realtime/testsuite/user/switches
$ ./run (use ctrl-c to stop it)
$ cd /usr/realtime/testsuite/kern/latency
$ ./run (use ctrl-c to stop it)
$ cd /usr/realtime/testsuite/kern/preempt
$ ./run (use ctrl-c to stop it)
$ cd /usr/realtime/testsuite/kern/switches
$ ./run (use ctrl-c to stop it)
make sure that there are no errors.
Somtimes there could be errors opening FIFOs due to the fact that entries
like /dev/rtf/0 are not persistent, so they get deleted after a reboot.
If that's the case, you should include code to recreate those entries in
/etc/rc.d/rc.local to make sure that they are recreated everytime,
see http://www.captain.at/rtai-error-opening-dev-rtf.php for example.
STEP 11 - Download, unpack and install Comedi and Comedilib
----------------------------------------------------------
If you are new to Linux and RTAI i strongly suggest
that you SKIP this step since you do not need
comedi or comedilib to use or learn RTAI.
However, if you plan to use comedi, this is the right
time to download and install comedi and comedilib.
The reference web site is http://www.comedi.org/
You should unpack the newest source in /usr/src,
so AS ROOT, dowload both comedi-x.x.xx.tar.gz and
comedilib-x.x.xx.tar.gz files into /usr/src
and then unpack them using the following commands (still as root):
$ cd /usr/src
$ tar -xzvf comedi-x.x.xx.tar.gz
$ tar -xzvf comedilib-x.x.xx.tar.gz
Create a symbolic link to comedi since it will be useful later:
$ rm -f comedi
$ ln -fs comedi-x.x-xx comedi
If your comedi version is 0.7.70 (or older) then you should manually
edit the /usr/src/linux/.config file and add the line:
CONFIG_RTHAL=Y
At this point you are ready to configure and install
both comedilib and comedi:
$ cd /usr/src/comedilib-x.x.xx
$ ./configure --sysconfdir=/etc
$ make
$ make install
$ make dev
$ cd /usr/src/comedi
$ ./configure --with-linuxdir=/usr/src/linux --with-rtaidir=/usr/realtime
$ make
$ make install
$ cp include/linux/comedi.h /usr/include/
$ cp include/linux/comedilib.h /usr/include/
$ mkdir /usr/local/include/linux
$ ln -s /usr/include/comedi.h /usr/local/include/linux/comedi.h
$ ln -s /usr/include/comedilib.h /usr/local/include/linux/comedilib.h
Reboot and make sure everything works fine.
At this point, you have to add comedi to the rtai configuration,
so, as root:
$ cd /usr/src/rtai
$ make config (or make menuconfig of make xconfig)
In the Add-ons Menu select Comedi support over LXRT,
and specify the comedi installation directory,
(typically /usr/local, in any case it should contain
lib/libcomedi.a include/linux/comedilib.h and include/linux/comedi.h
Then recompile and install rtai:
$ make
$ make install
Reboot and re-test rtai (Step 11) to make sure everything is fine.
STEP 12 - Copy the rtai-lab folder into the current Matlab installation
-----------------------------------------------------------------------
It is assumed that you have a working Matlab installation that includes
simulink and Real Time Workshop (i currently use version 7.0.1).
A few extra step are required if your matlab installation is not on
the same system where you installed rtai. Typically people have Matlab
installed under a Windows system. A Windows version of Matlab installed
under Wine, counts as an installation on a different system.
The thing to do at this point is to copy the entire content of the
folder (typically) /usr/src/rtai-3.3/rtai-lab/matlab into Matlab_Root\rtw\c\rtai
(typically the Matlab_Root is something like C:\Matlab701 for
Windows and /usr/local/matlab for Unix).
STEP 13 - Run setup.m and create mex files
------------------------------------------
After you copied the rtai-lab folder you should open matlab
and run the Setup.m file that is included in the rtai-lab folder.
This basically adds the new folder to the matlab path.
After you have run setup.m you should be able to access the
simulink library RTAI Devices from the Simulink browser.
Now you need to create mex (Matlab Executable) files for every
s-function in Matlab_Root\rtw\c\rtai, since it will be needed later:
From the matlab window, go to Matlab_Root\rtw\c\rtai\devices,
make sure the mex support is working (mex -setup) and then
issue the following commands:
mex sfun_rtai_led.c
mex sfun_rtai_log.c
mex sfun_rtai_meter.c
mex sfun_rtai_scope.c
mex sfun_rtai_synchronoscope.c
mex sfun_comedi_dio_write.c
mex sfun_comedi_dio_read.c
mex sfun_comedi_data_write.c
mex sfun_comedi_data_read.c
Note that in older rtai versions you have to modify
several files at this point for things to work fine,
so, yet another reason to use rtai-3.2 or greater.
STEP 14 - Edit rtai.tmf
-----------------------
If you are using a Matlab installation (for Unix) on the same Linux
system in which you installed RTAI, then you can skip the following
and jump to the next step, otherwise you have to modify the rtai.tmf file
in the rtai-lab folder, specifically, you must modify the following lines :
MATLAB_ROOT = |>MATLAB_ROOT<|
---------------------------------
Here you have to replace |>MATLAB_ROOT<| with the path of a Linux
folder in which you will later copy some matlab related files.
Typically you will replace |>MATLAB_ROOT<| with /usr/local/matlab,
and only later you will actually create the /usr/local/matlab folder
in the Linux_with_RTAI filesystem and copy stuff within it (Step 16).
COMPUTER = |>COMPUTER<|
------------------------------
Here you have to replace |>COMPUTER<| with GLNX86
(assuming Linux is innstalled on a x86 architecture).
You also have to modify the entries LINUX_HOME,
RTAIDIR and COMEDI_HOME, to make sure they refer
to the paths where Linux, rtai, and comedi are installed,
(typically they are /usr/src/linux, /usr/realtime
and /usr/src/comedi respectively).
Finally, into the "Rules" section delete the following lines:
|>START_EXPAND_RULES<|%.o : |>EXPAND_DIR_NAME<|/%.c
gcc -c $(CFLAGS) $<
|>END_EXPAND_RULES<|
otherwise, later on, the generated make files (*.mk)
will contain the (windows) relative paths of the custom
s-functions used in the scheme, therefore causing the make
process to fail miserably.
STEP 15 - Generating code from a simple example
-----------------------------------------------
Copy the file Matlab_Root\rtw\c\rtai\examples\test.mdl
somewhere else, for example in Matlab_Root\work,
then within matlab, move to that folder,
open the file with simulink and run it.
It should run without problems.
At this point you are ready to generate code from it.
Open the simulation menu, simulation parameters,
Real-Time Workshop, and make sure that the RTAI Target
is selected, (otherwise browse and select it).
Then click on Build, to generate code from the scheme.
At this point you should have the folder test_rtai
under your working directory. This folder contains
the code generated from the test simulink scheme.
If your simulink scheme uses some custom s-functions,
(which is not specifically the case of test.mdl but
it is indeed the normal case), then it is very important
at this point to remember to copy the C code of every
custom s-functions used in the scheme inside the
generated scheme_rtai folder.
Another important thing to remember about s-functions
is to always USE LOWERCASE characters in the file name,
this is because the file names of the s-functions
will be converted to lowercase within the make file (*.mk),
which does not make any difference under windows but
causes missing file errors under unix if the original
name contained uppercase characters.
STEP 16 - Copy the extern, rtw, and Simulink folders to Linux
-------------------------------------------------------------
If you are using a Matlab installation (for Unix) on the same Linux
system in which you installed RTAI, then you can skip this step.
In your Linux filesystem, create a folder that
corresponds to the entry MATLAB_ROOT in your rtai.tmf file,
(typically /usr/local/matlab), then copy within it the three folders
Matlab_Root\extern, Matlab_Root\rtw and Matlab_Root\Simulink
from the current Matlab installation.
Make sure all the c files in the folder Matlab_Root\rtw\c\libsrc
have been transfered, since they all will be needed later,
and for mysterious reasons some of them have a tendency to get lost.
STEP 17 - Copy, compile and run the generated code
--------------------------------------------------
Copy the test_rtai folder somewhere in the linux filesystem,
(e.g. usr/local/src/tests/test_rtai) and then,
from within the test_rtai folder, as root :
make -f test.mk
at this point, if the compilation proceeds without errors you
should get the executable test in the parent folder, so move
to the parent folder, insert the required modules:
sync
insmod /usr/realtime/modules/rtai_hal.ko
insmod /usr/realtime/modules/rtai_lxrt.ko
insmod /usr/realtime/modules/rtai_fifos.ko
insmod /usr/realtime/modules/rtai_sem.ko
insmod /usr/realtime/modules/rtai_mbx.ko
insmod /usr/realtime/modules/rtai_msg.ko
insmod /usr/realtime/modules/rtai_netrpc.ko ThisNode="127.0.0.1"
sync
at this point run the test:
./test -v -f 5
the process should run for 5 seconds.
Then to test also xrtailab:
./test &
starts the process in wait mode,
/usr/realtime/bin/xrtailab &
starts the xrtailab window, from that window you can then
actually start the real time process, log the data,
see the plots from the scopes in the simulink schemes as the
process runs, and then stop the process.
CREATION OF A MINIMAL STAND ALONE RTAI SYSTEM
STEP 1 - Configure and compile a reduced version of your Kernel
---------------------------------------------------------------
The first thing to do is to compile the kernel that fits the target system,
as far as processor type, bus and peripherals are concerned.
As root:
$ cd /usr/src/linux
$ make config (or menuconfig or xconfig)
Since this must be a reduced version of the kernel, i.e. the
final size should be less than 700K, you have to disable virtually
everything except the loadable module support, the IPIPE option,
the ISA bus, and the IDE, floppy, ext2, and /proc support.
Also, at this point, if you are really working with a floppy disk,
and you need an unattended reboot, you should manually edit the file
/usr/src/linux/init/do_mounts.c, search for the "press ENTER" string
and, a few lines below, change the instruction
sys_read(fd, &c, 1); with the instruction sys_read(fd, &c, 0);
Once you have done the above, you have to compile the kernel:
$ make clean && make
copy the configuration file for future reference :
$ cp -f .config config-floppy-rtlx-yy.txt
STEP 2 - Download, unpack and install Busybox
---------------------------------------------
Go to http://busybox.net/downloads/ and look for the newest stable
version, (i use version 1.01).
As root, download the busybox-x.xx.tar.bz2 file into /usr/src
ans unpack it using the following commands :
$ cd /usr/src
$ tar --bzip2 -xvf busybox-x.xx.tar.bz2
Set a symbolic link for later use:
$ rm -f busybox
$ ln -fs busybox-x.xx busybox
then, configure and install busybox:
$ cd /usr/src/busybox
$ make config (or make menuconfig)
In the Build Options, select "Build Busybox as a static binary".
Be sure to include at least the init, insmod, rmmod, and mount,
in general, select to only what you need, if unsure, use the default option.
$ make dep && make busybox
finally, save the config file for future reference:
$ cp -f .config my-busybox-config-yy.txt
STEP 3 - Download, unpack and install LILO
---------------------------------------------
Go to http://freshmeat.net/projects/bin86/ and look for the
latest version of the bin86 assembler and loader, (i use 0.16.17).
As root, download the bin86-x.xx.xx.tar.gz file into /usr/src
ans unpack it using the following commands
$ cd /usr/src
$ tar -zxvf bin86-x.xx.xx.tar.gz
Then compile the bin86 assembler:
$ cd /usr/src/bin86-x.xx.xx
$ mkdir /usr/local/man/man1
$ make && make install
If everything goes fine, then it's time to download LILO:
go to http://freshmeat.net/projects/lilo/ and look for the
newest stable version of the lilo, (i use version 22.7.1).
As root, download the lilo-xx.x.x.src.tar.gz file into /usr/src
ans unpack it using the following commands :
$ cd /usr/src
$ tar -zxvf lilo-xx.x.x.src.tar.gz
Set a symbolic link for later use:
$ rm -f lilo
$ ln -fs lilo-xx.x.x lilo
then, build lilo (you don't have to install it on the system as a
boot loader, you just need to have the /usr/src/lilo/lilo binary)
$ make
Then reboot and make sure everything is fine.
STEP 4 - Prepare Floppy
----------------------------------------------------------
In what follows, it is assumed that you want to create a
stand alone RTAI system on a floppy. This is rarely useful,
since you can't normally include anything else other than
the bare essential, however, if you learn how to squeeze RTAI
on a floppy then you can later put it wherever you want,
using exactly the same procedure with just a few changes.
Insert a floppy disk, then, as root,
unmount the floppy :
$ umount /dev/fd0
$ sync
zero out the content of the floppy block by block:
$ dd if=/dev/zero of=/dev/fd0 bs=1k count=1440
create an ext2 file system on the floppy:
$ /sbin/mke2fs -F -m 0 /dev/fd0
create a temporary folder a if it does not exist,
and remove everything if it exists
$ mkdir -p /mnt/a
$ rm -drf /mnt/a/*
mount the floppy on /mnt/a :
$ mount -t ext2 /dev/fd0 /mnt/a
$ sync
STEP 5 - Populate the root
--------------------------
Again as root, create the classic unix directories :
$ cd /mnt/a
$ mkdir boot
$ mkdir initrd
$ mkdir dev
$ mkdir proc
$ mkdir etc
$ mkdir sbin
$ mkdir bin
$ mkdir lib
$ mkdir mnt
$ mkdir usr
$ mkdir tmp
$ mkdir lib/modules
$ mkdir -p var/{log,run}
$ touch var/run/utmp
$ sync
STEP 6 - Populate /dev
----------------------
$ cp -dpR /dev/fd[0-1] dev
$ cp -dpR /dev/hda[0-3] dev
$ cp -dpR /dev/tty[0-6] dev
$ cp -dpR /dev/ttyS[0-6] dev
$ cp -dpR /dev/ram[0-6] dev
$ cp -dpR /dev/console dev
$ cp -dpR /dev/zero dev
$ cp -dpR /dev/null dev
$ cp -dpR /dev/kmem dev
$ cp -dpR /dev/mem dev
$ mkdir dev/rtf
$ cp -dpR /dev/rtf/[0-9] dev/rtf
$ cp -dpR /dev/rtf[0-9] dev
$ cp -dpR /dev/rtai_shm dev
$ sync
STEP 7 - Create the configuration files and populate /etc
---------------------------------------------------------
I suggest that you create the folder /etc/rtai-fd0
to store the configuration files for your system.
There are 2 very important configuration files that the
system needs to find during the boot process, that is
/etc/fstab and /etc/init.d/rcS :
/etc/fstab
----------
This is the file that tells the filesystems to mount
and where they are located, usually just a few lines are required:
/dev/fd0 / ext2 defaults 1 1
none /proc proc defaults 0 0
If the kernel does not support the /proc filesystem then
just the first line is necessary.
/etc/init.d/rcS
---------------
This file (rcS stands for Run Commands Shell) contains
command to be executed by the shell as soon as it starts.
here is an example:
#!/bin/sh
/bin/mount -av
sync
insmod /lib/modules/rtai_hal.ko
insmod /lib/modules/rtai_lxrt.ko
insmod /lib/modules/rtai_fifos.ko
insmod /lib/modules/rtai_sem.ko
insmod /lib/modules/rtai_mbx.ko
insmod /lib/modules/rtai_msg.ko
sync
# insert your calls here
poweroff
The lilo configuration file it is not needed during the
boot process but it is needed later to properly write the
floppy boot sector:
lilo.conf
---------
Here is an example of lilo.conf file for a stand alone
system with both kernel and filesystem on the same floppy :
boot =/dev/fd0
map =/boot/map
timeout =00
read-write
backup =/dev/null
compact
image =/boot/bzImage
label =RTFly
root =/dev/fd0
It is important to store also the configuration files
for both the kernel and busybox:
$ cp -f /usr/src/linux/.config /etc/rtai-fd0/config-linux.txt
$ cp -f /usr/src/busybox/.config /etc/rtai-fd0/config-busybox.txt
At this point we can move the files into the mnt/a/etc folder:
$ cd /mnt/a
$ cp /etc/rtai-fd0/* etc
It is always better to make sure that rc is executable:
$ chmod -R 777 etc/*
$ sync
Finally, if you modified the code of either the linux kernel
or busybox, it is a good idea to include it here in this folder.
STEP 8 - Populate /bin /sbin and /usr using busybox
---------------------------------------------------
As root, do the following :
$ cd /usr/src/busybox
$ make PREFIX=/mnt/a install
$ sync
STEP 9 - Copy the kernel and make the disk bootable
---------------------------------------------------
The kernel image has to be compiled for the target processor.
$ cp /usr/src/linux/arch/i386/boot/bzImage /mnt/a/boot/bzImage
$ sync
$ /usr/src/lilo/lilo -v -C /etc/lilo.conf -r /mnt/a
$ sync
STEP 10 - Copy rtai related stuff
--------------------------------
If you are really working with a standard floppy disk then
at this point there will probably be only about 50K left
on your floppy disk so, there will be no space left to
copy any modules or executables, except maybe for a few ones,
so, in that case you can skip this step.
All the rtai modules and the executables that need to run
on the target system, have to be compiled for the target system,
therefore make sure that rtai has been compiled and installed
using the same linux .config file that was used to compile the
kernel for the target processor in step 1.
If you are not sure, then, as root :
$ cd /usr/src/rtai
$ make clean && make && make install
Then,
$ cd /mnt/a
$ cp /usr/realtime/modules/rtai_hal.ko lib/modules
$ cp /usr/realtime/modules/rtai_lxrt.ko lib/modules
$ cp /usr/realtime/modules/rtai_fifos.ko lib/modules
$ cp /usr/realtime/modules/rtai_sem.ko lib/modules
$ cp /usr/realtime/modules/rtai_mbx.ko lib/modules
$ cp /usr/realtime/modules/rtai_msg.ko lib/modules
$ sync
Also, allow the tmp directory to be accessible,
if you want to copy some file over there.
chmod -R 777 /mnt/flash/tmp
At this point, you should recompile the executables
that need to run on the target system, and copy them
on the tmp directory along with any other needed file.
STEP 11 - Check size, unmount and reboot
----------------------------------------
$ df -h /dev/fd0
$ sync
$ sync
$ umount /dev/fd0
now take your device, insert it into the target system,
reboot and cross your fingers really hard :-)
原文地址:http://blog.csdn.net/lu_embedded/article/details/44627291
如今Linux系统正被广泛用于各种嵌入式实时系统中,但由于Linux系统本身不支持硬实时扩展,故限制了其发展。由于Linux + RTAI的双内核方式构成的实时系统方案具有良好性能,本文将简述如何构建这样一个Linux + RTAI的实时系统。
一、准备工作
首先选择一款RTAI的版本,我们这里以RTAI-3.7为例,从官网【www.rtai.org】下载该RTAI压缩包。进入RTAI-3.7的/rtai-3.7/base/arch/i386/patches,查看此版本RTAI支持的内核版本。我们这里选择Linux-2.6.20.21,于是从官网【www.kernel.org】下载该Linux内核压缩包。
登录已安装好的Ubuntu系统,切换到超级用户权限(或直接用超级用户登录)。将前面下载好的Linux内核以及RTAI压缩包复制到/usr/src目录中。
进入/usr/src目录,解压文件。
# tar –jxvf linux-2.6.20.21.tar.bz2
# tar –jxvf rtai-3.7.tar.bz2
解压完成后在/usr/src目录下出现两个文件夹linux-2.6.20.21和rtai-3.7。
注意,如果你想在新安装的Ubuntu系统上进行Linux内核编译与RTAI安装,请连接网络并进行以下操作:
# apt-get update
# apt-get install build-essential
# apt-get install kernel-package
# apt-get install gcc
# apt-get install libncurses5
# apt-get install libncurses5-dev
# apt-get install libqt3-mt-dev
二、打上RTAI补丁
进入Linux源码目录,接下来给内核打上相应的RTAI补丁(首先确保你的Ubuntu系统上已安装patch工具)。
# cd linux-2.6.20.21
# patch –p1 –b < /usr/src/rtai-3.7/base/arch/i386/patches/hal-linux-2.6.20.21-i386-1.12-03.patch
三、配置Linux内核
先执行以下命令,用以清除目录下所有配置文件和以前生成内核时所产生的中间文件。
# make mrproper
在Ubuntu下有多种配置菜单的方式,推荐使用图形化的配置菜单。
# make menuconfig
(方便起见,可以将/usr/src/linux-headers-`uname -r`/.config拷贝到本目录下。)
下面介绍配置菜单中需要修改的一些选项:
(1) General setup àLocal version – append to kernel release = -rtai-3.7
(2) General setup àCreate deprecated sysfs files (NEW) = no
(3) Enable loadable module support àModule versioning support = no
说明:不同的内核在使用同一模块时区别于它原有的模块,此处不选。
(4) Enable loadable module support àModule unloading
说明:这个选项可以卸载不再使用的模块,如果不选将不能卸载任何模块。
(5) Processor type and features àSymmetric multi – processing support(对称多处理器支持)= no
说明:如果有多个CPU或者使用的是多核CPU就选上,此时“EnhancedReal Time Clock Support”选项必须开启,“Advanced Power Management”选项必须关闭。
(6) Processor type and features àInterrupt pipeline = yes
(7) Processor type and features àSubarchitecture Type(子类型架构)= PC – compatible(标准的PC或兼容机)
(8) Processor type and features àProcessor family(处理器系列)= core2 / newer xeon(根据实际CPU选择,例如酷睿双核)
(9) Processor type and features àGeneric x86 support(通用x86支持)= no(若CPU能在上述“Processor family”中找到,则可不选)
(10) Processor type and features àMulti – core scheduler support(针对多核CPU进行调度策略优化)=yes
(11) Processor type and features àPreemption Model(内核抢占模式)àPreemptible Kernel (Low – Latency Desktop)(适用运行实时程序的主动内核抢占)= yes
(12) Power management options (ACPI,APM) àPower Management support(电源管理选项)= no
说明:电源管理有APM和ACPI两种标准且不能同时使用,即使关闭该选项,X86上运行的Linux也会在空闲时发出HLT指令将CPU进入睡眠状态。
(13) Power management options (ACPI,APM) àCPU Frequency scaling(CPU变频控制)àCPU Frequency scaling Power management options = no
说明:CPU变频控制允许在运行中改变CPU主频,达到省电和降温的目的,但是这个驱动不会自动改变CPU的时钟速度,选择no。
四、编译及安装Linux内核
# make-kpkg clean
# make-kpkg –initrd kernel_image kernel_headers
当编译结束后,会在/usr/src目录下生成两个文件,分别是镜像文件和头文件。进入目录,安装镜像文件和头文件。
# dpkg –I linux-headers-2.6.20.21-rtai-3.7_2.6.20.21-rtai-3.7-10.00.Custom_i386.deb
# dpkg –Ilinux-image-2.6.20.21-rtai-3.7_2.6.20.21-rtai-3.7-10.00.Custom_i386.deb
安装后将得到三个非常重要的文件,分别是System.map,config和vmlinuz,可在/boot/目录下找到。接着重启系统,进入新配置的内核环境。
# shutdown –r now
五、编译及安装RTAI
进入RTAI源码目录,进行简单的配置后即可编译和安装。
# cd /usr/src/rtai-3.7
# make menuconfig
General àLinux source trr = /usr/src/linux-2.6.20.21(也可直接创建软链接 #ln –s linux-2.6.20.21 linux)
Machine(x86) àNumber of CPUs (SMP - only) = 2(CPU核数)
# make
# make install
六、测试
# cd /usr/realtime/testsuite/kern/latency
# ./run
本文详细介绍了如何构建Linux+RTAI实时系统,包括准备工作、配置Linux内核、安装RTAI、测试等内容。

868

被折叠的 条评论
为什么被折叠?



