做过一个有关RTLinux的项目,时间一长,差不多忘光了,现在尽量把原来做过的东西总结一下,以备后用,同时正在做类似项目的一个借鉴
平台
主机:redhat 8.0
目标机:PC104模块、ISA总线脉冲输出、实时串口通信
linux-2.4.18.tar.bz2 +rtlinux-3.2-pre1.tar.bz2
简述
Linux是典型的分时应用系统,对于实时性要求很高的应用,必须对内核本身动手术。而RTLinux则采取了一种比较聪明也比较折中的办法:他们实现一个最底层的精简的调度器,用于调度实时线程,原来的内核本身则成为实时调度器的一个优先级最低的任务。这样,当有实时任务时,普通内核已经建立于其上的普通进程被强制中断,实时线程被强制执行;只有当若有实时线程都让出cpu之后,普通内核才被运行,再由普通内核去调度执行普通的应用程序……
实例
- #include <rtl_fifo.h>
- #include <rtl.h>
- #include <rtl_sched.h>
- #include <time.h>
- #include <pthread.h>
- #include <rtl_time.h>
- #include <signal.h>
- #include "rt_com.h"
- #include <linux/kernel.h>
- #include <linux/module.h>
- MODULE_LICENSE("GPL v2");
- MODULE_AUTHOR("Wind-Son");
- MODULE_DESCRIPTION("Pulse-Control system");
- typedef unsigned short __u16;
- void io_bit_on(__u16 port, unsigned int pos, __u16 *status)
- {
- __asm__ __volatile__(
- "movl %1,%?x\n\t"
- "movl %0,%?x\n\t"
- "btsl %2,(%?x)\n\t"
- "mov (%?x),%%al\n\t"
- "out %%al,(%%dx)\n\t"
- "out %%al,$0x80\n\t"
- :
- :"m"(status), "rm"(port), "Ir"(pos)
- );
- }
- void io_bit_off(__u16 port, unsigned int pos, __u16 *status)
- {
- __asm__ __volatile__(
- "movl %1,%?x\n\t"
- "movl %0,%?x\n\t"
- "btrl %2,(%?x)\n\t"
- "mov (%?x),%%al\n\t"
- "out %%al,(%%dx)\n\t"
- "out %%al,$0x80\n\t"
- :
- :"m"(status), "rm"(port), "Ir"(pos)
- );
- }
- #define dbg_print rtl_printf
- #define MIN_TIME 5000
- static void get_time_interval(void)
- {
- }
- void* pulse_generate_thread(void *arg)
- {
- static __u16 io_status = 0;
- struct sched_param p;
- hrtime_t current_time;
- REAL_TIME_GET_ENABLE;
- int intrrupt_sched_period = 180000;
- p.sched_priority = 1;
- struct timespec resolution;
- rtl_setclockmode(CLOCK_REALTIME, RTL_CLOCK_MODE_PERIODIC,
- intrrupt_sched_period);
- clock_getres(rtl_getschedclock(), &resolution);
- intrrupt_sched_period = timespec_to_ns(&resolution);
- pthread_make_periodic_np(pthread_self(), clock_gethrtime(rtl_getschedclock()),
- intrrupt_sched_period);
- pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);
- for (;;) {
- dbg_print("debug entry\n");
- while (!ready)
- pthread_wait_np();
- dbg_print("debug exit\n");
- if (!init_rt_clock) {
- init_rt_clock = 1;
- pthread_wait_np();
- current_time = clock_gethrtime(CLOCK_REALTIME);
- } else {
- if (intrrupt_sched_period < MIN_TIME)
- intrrupt_sched_period = MIN_TIME;
- current_time += intrrupt_sched_period;
- clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, hrt2ts(current_time), NULL);
- }
- io_bit_on(IO_PORT_OUT, XPULSE, &io_status);
- intrrupt_sched_period = get_time_interval();
- }
- return 0;
- }
- static void init_for_rt_mm(void)
- {
- }
- static void rt_alloc_mm(void)
- {
- thread_wait_np();
- buf = kmalloc(size, GFP_ATOMIC);
- }
- static int kmalloc_thread(void * kthread_arg)
- {
- unsigned long timeout = HZ;
- init_for_rt_mm();
- for (;;) {
- while (!get_flag(MM_ALLOC_FLAG)) {
- if( signal_pending(current))
- return 0;
- timeout = interruptible_sleep_on_timeout(&wq, timeout);
- }
- rt_alloc_mm();
- clear_flag(MM_ALLOC_FLAG);
- }
- return -1;
- }
- wait_queue_head_t wq;
- static pid_t kmalloc_kthread_id;
- static int kmalloc_kthread_state = 1;
- static int pulse_generate_thread_created = 0;
- static int main_ctrl_thread_created = 0;
- static pthread_t pulse_generate_pthread;
- static pthread_t main_ctrl_pthread;
- static pthread_mutex_t cache_mutex;
- void rt_mm_request(void)
- {
- set_flag(MM_ALLOC_FLAG);
- while(get_flag(MM_ALLOC_FLAG))
- pthread_wait_np();
- }
- void* main_ctrl_thread(void *arg)
- {
- int work_sched_period = 160000;
- struct timespec resolution;
- int ret1 = rtl_setclockmode(rtl_getschedclock(), RTL_CLOCK_MODE_PERIODIC,
- work_sched_period);
- if (ret1) {
- dbg_print("seting periodic mode failed\n");
- clear_flag(WORK_SCHED_MODE);
- }
- clock_getres(rtl_getschedclock(), &resolution);
- work_sched_period = timespec_to_ns(&resolution);
- pthread_make_periodic_np(pthread_self(), clock_gethrtime(rtl_getschedclock()),
- work_sched_period);
- init_task();
- for (;;) {
- if (work) {
- dbg_print("work\n");
- rt_mm_request();
- calc_time_interval();
- if (exit)
- break;
- } else
- pthread_wait_np();
- }
- exit_task();
- return 0;
- }
- int init_module(void)
- {
- pthread_attr_t attr;
- struct sched_param p;
- int ret;
- rtf_destroy(0);
- rtf_destroy(1);
- rt_com_clr_in(0);
- rt_com_clr_out(0);
- int fifo_status = rtf_create(0,100);
- if(fifo_status)
- dbg_print("FIFO Create failed!");
- fifo_status = rtf_create(1, 4000);
- if(fifo_status)
- dbg_print("FIFO Create failed!");
- rt_com_setup(0, 9600, RT_COM_PARITY_NONE, 1, 8);
- hrtime_t now = gethrtime();
- pthread_attr_init(&attr);
- pthread_mutex_init(&cache_mutex, NULL);
- pthread_attr_setfp_np(&attr, 1);
- ret = pthread_create(&pulse_generate_pthread, &attr,
- pulse_generate_thread, (void *)0);
- if (!ret)
- pulse_generate_thread_created = 1;
- pthread_make_periodic_np (pulse_generate_pthread, now + 2 * 240000, 80000);
- p . sched_priority = 1;
- pthread_setschedparam (pulse_generate_pthread, SCHED_FIFO, &p);
- ret = pthread_create(&main_ctrl_pthread, &attr, main_ctrl_thread, (void *)1);
- if (!ret)
- main_ctrl_thread_created=1;
- pthread_make_periodic_np (main_ctrl_pthread, now + 2 * 160000, 30000);
- p . sched_priority = 2;
- pthread_setschedparam (main_ctrl_pthread, SCHED_FIFO, &p);
- init_waitqueue_head(&wq);
- kmalloc_kthread_id = kernel_thread(kmalloc_thread, NULL, 0);
- if (kmalloc_kthread_id < 0) {
- printk(KERN_ERR "fork failed, errno %d\n", -kmalloc_kthread_id);
- return kmalloc_kthread_id;
- }
- return ret;
- }
- void cleanup_module(void)
- {
- int ret = kill_proc(kmalloc_kthread_id, SIGKILL, 1);
- if (!ret) {
- int count = 10 * HZ;
- while (kmalloc_kthread_state && --count) {
- current->state = TASK_INTERRUPTIBLE;
- schedule_timeout(1);
- }
- }
- if (main_ctrl_thread_created) {
- pthread_cancel(main_ctrl_pthread);
- pthread_join(main_ctrl_pthread, NULL);
- pthread_delete_np(main_ctrl_pthread);
- }
- if (pulse_generate_thread_created) {
- pthread_cancel(pulse_generate_pthread);
- pthread_join(pulse_generate_pthread, NULL);
- pthread_delete_np(pulse_generate_pthread);
- }
- rt_com_setup(0, -1, 0, 0, 0);
- rtf_destroy(0);
- rtf_destroy(1);
- pthread_mutex_destroy (&cache_mutex);
- }
复制代码
其实现在有关Linux实时应用的原理和应用方面的介绍已经不少,所以我主要是想从自己的亲身实践中的经验教训出发总结一下。
我遇到的主要问题主要有以下几个:
1、硬实时调度精度不够的问题。刚开始产生脉冲驱动的线程我按照例子程序采样如下方式
pthread_make_periodic_np(); //设置调度方式和周期等参数
pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);
pthread_wait_np(); //让出cpu进入睡眠
可实际情况总是不理想,输出波形不够稳定,离预想的效果也很远。试着将调度策略SCHED_FIFO改其他几种方式,也一样。最后尝试用clock_nanosleep()才达到了比较理想的效果。理论上clock_nanosleep()应该达到ns级别的精度,当然实际精度还要取决于硬件。
2、实时线程所能到达的实时效果和精度极限也就是定时器本身的精度了。有过在51上做开发经验的都有这样一个意识:定时器中断处理例程里尽量只做最简单、最必须的工作,但毕竟还是有开销的。如果你对精度还有更高的要求,可在main_ctrl_thread()即负责计算脉冲间隔时间的例程中加入补偿值,以抵消脉冲输出例程中的时间开销。
3、实时线程中频繁的动态申请内存时常宕机。后来经过实验摸索才采取了上面代码中所述的拐弯抹角的办法。如果谁碰到过类似问题有更好的办法,还望指出。
4、应用程序直接向串口输出时总出错。
开始方法是system("/bin/ls ./data/ >> /dev/ttyS0";在没有实时线程的影响的情况下,这样是没有问题。开启实时线程后就老出错。
后改成如下方式就好了:由实时模块通过实时调用rt_com_write()和rt_com_read()读写串口;再通过实时管道rtl_fifo转发到应用程序
另外,纯粹经验谈,实时线程如果不主动让出cpu,任何用户程序无法运行,包括你的键盘响应!如果你的某个环节可能陷入循环,你能做的就只有poweroff了;被迫重启后在ext2文件系统上随之而来的是漫长的fscheck……所以我在调试阶段,基本上是只要有循环的的方,就加上pthread_wait_np();以后再慢慢把不必要的去掉。