RTLinux编程总结

简介: <div class="bct fc05 fc11 nbw-blog ztag"><p>做过一个有关RTLinux的项目,时间一长,差不多忘光了,现在尽量把原来做过的东西总结一下,以备后用,同时正在做类似项目的一个借鉴<br>平台<br>主机:redhat 8.0<br>目标机:PC104模块、ISA总线脉冲输出、实时串口通信<br>         linux-2.4.18.tar.bz2

做过一个有关RTLinux的项目,时间一长,差不多忘光了,现在尽量把原来做过的东西总结一下,以备后用,同时正在做类似项目的一个借鉴
平台
主机:redhat 8.0
目标机:PC104模块、ISA总线脉冲输出、实时串口通信
         linux-2.4.18.tar.bz2 +rtlinux-3.2-pre1.tar.bz2
简述
Linux是典型的分时应用系统,对于实时性要求很高的应用,必须对内核本身动手术。而RTLinux则采取了一种比较聪明也比较折中的办法:他们实现一个最底层的精简的调度器,用于调度实时线程,原来的内核本身则成为实时调度器的一个优先级最低的任务。这样,当有实时任务时,普通内核已经建立于其上的普通进程被强制中断,实时线程被强制执行;只有当若有实时线程都让出cpu之后,普通内核才被运行,再由普通内核去调度执行普通的应用程序……
实例

  1. #include <rtl_fifo.h>
  2. #include <rtl.h>
  3. #include <rtl_sched.h>
  4. #include <time.h>
  5. #include <pthread.h>
  6. #include <rtl_time.h>
  7. #include <signal.h>
  8. #include "rt_com.h"
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. MODULE_LICENSE("GPL v2");
  12. MODULE_AUTHOR("Wind-Son");
  13. MODULE_DESCRIPTION("Pulse-Control system");
  14. typedef unsigned short __u16;
  15. void io_bit_on(__u16 port, unsigned int pos, __u16 *status)
  16. {
  17.         __asm__ __volatile__(
  18.                 "movl %1,%?x\n\t"
  19.                 "movl %0,%?x\n\t"
  20.                 "btsl %2,(%?x)\n\t"
  21.                 "mov (%?x),%%al\n\t"
  22.         "out %%al,(%%dx)\n\t"
  23.         "out %%al,$0x80\n\t"
  24.                 :
  25.                 :"m"(status), "rm"(port), "Ir"(pos)
  26.         );
  27. }
  28. void io_bit_off(__u16 port, unsigned int pos, __u16 *status)
  29. {
  30.         __asm__ __volatile__(      
  31.                 "movl %1,%?x\n\t"
  32.                 "movl %0,%?x\n\t"
  33.                 "btrl %2,(%?x)\n\t"
  34.                 "mov (%?x),%%al\n\t"
  35.         "out %%al,(%%dx)\n\t"
  36.         "out %%al,$0x80\n\t"
  37.                 :
  38.                 :"m"(status), "rm"(port), "Ir"(pos)
  39.         );
  40. }
  41. #define dbg_print rtl_printf
  42. #define MIN_TIME              5000
  43. static void get_time_interval(void)
  44. {
  45. }
  46. void* pulse_generate_thread(void *arg)
  47. {
  48.         static __u16 io_status = 0;
  49.         struct sched_param p;
  50.         hrtime_t current_time;
  51.         REAL_TIME_GET_ENABLE;
  52.         int intrrupt_sched_period = 180000;
  53.         p.sched_priority = 1;
  54.         struct timespec resolution;
  55.        
  56.         rtl_setclockmode(CLOCK_REALTIME, RTL_CLOCK_MODE_PERIODIC,
  57.                         intrrupt_sched_period);
  58.         clock_getres(rtl_getschedclock(), &resolution);
  59.         intrrupt_sched_period = timespec_to_ns(&resolution);
  60.        
  61.         pthread_make_periodic_np(pthread_self(), clock_gethrtime(rtl_getschedclock()),
  62.                 intrrupt_sched_period);
  63.         pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);
  64.         for (;;) {
  65.                 dbg_print("debug entry\n");
  66.                 while (!ready)
  67.                     pthread_wait_np();
  68.                 dbg_print("debug exit\n");
  69.                 if (!init_rt_clock) {
  70.                        
  71.                         init_rt_clock = 1;
  72.                         pthread_wait_np();
  73.                         current_time = clock_gethrtime(CLOCK_REALTIME);
  74.                 } else {
  75.                     if (intrrupt_sched_period < MIN_TIME)
  76.                                 intrrupt_sched_period = MIN_TIME;
  77.                     current_time += intrrupt_sched_period;
  78.                        
  79.                     clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, hrt2ts(current_time), NULL);
  80.                 }
  81.                
  82.                 io_bit_on(IO_PORT_OUT, XPULSE, &io_status);
  83.                
  84.             intrrupt_sched_period = get_time_interval();
  85.         }
  86.         return 0;
  87. }
  88. static void init_for_rt_mm(void)
  89. {
  90. }
  91. static void rt_alloc_mm(void)
  92. {
  93.         thread_wait_np();
  94.         buf = kmalloc(size, GFP_ATOMIC);
  95. }
  96. static int kmalloc_thread(void * kthread_arg)
  97. {
  98.         unsigned long timeout = HZ;
  99.         init_for_rt_mm();
  100.         for (;;) {
  101.                 while (!get_flag(MM_ALLOC_FLAG)) {
  102.                        
  103.                         if( signal_pending(current))
  104.                                 return 0;
  105.                         timeout = interruptible_sleep_on_timeout(&wq, timeout);
  106.                 }
  107.                 rt_alloc_mm();
  108.                 clear_flag(MM_ALLOC_FLAG);
  109.         }
  110.         return -1;
  111. }
  112. wait_queue_head_t wq;
  113. static pid_t kmalloc_kthread_id;
  114. static int kmalloc_kthread_state = 1;
  115. static int pulse_generate_thread_created = 0;
  116. static int main_ctrl_thread_created = 0;
  117. static pthread_t pulse_generate_pthread;      
  118. static pthread_t main_ctrl_pthread;
  119. static pthread_mutex_t cache_mutex;
  120. void rt_mm_request(void)
  121. {
  122.         set_flag(MM_ALLOC_FLAG);
  123.        
  124.         while(get_flag(MM_ALLOC_FLAG))      
  125.                 pthread_wait_np();
  126. }
  127. void* main_ctrl_thread(void *arg)
  128. {
  129.         int work_sched_period = 160000;
  130.         struct timespec resolution;
  131.         int ret1 = rtl_setclockmode(rtl_getschedclock(), RTL_CLOCK_MODE_PERIODIC,
  132.                 work_sched_period);              
  133.         if (ret1) {
  134.                 dbg_print("seting periodic mode failed\n");
  135.                 clear_flag(WORK_SCHED_MODE);
  136.         }
  137.         clock_getres(rtl_getschedclock(), &resolution);
  138.         work_sched_period = timespec_to_ns(&resolution);
  139.         pthread_make_periodic_np(pthread_self(), clock_gethrtime(rtl_getschedclock()),
  140.             work_sched_period);
  141.         init_task();      
  142.         for (;;) {
  143.                 if (work) {
  144.                         dbg_print("work\n");
  145.                         rt_mm_request();
  146.                         calc_time_interval();
  147.                         if (exit)
  148.                             break;
  149.                 } else
  150.                         pthread_wait_np();
  151.         }
  152.         exit_task();
  153.     return 0;
  154. }
  155. int init_module(void)
  156. {
  157.         pthread_attr_t attr;
  158.         struct sched_param p;
  159.         int ret;
  160.         rtf_destroy(0);
  161.         rtf_destroy(1);
  162.         rt_com_clr_in(0);
  163.         rt_com_clr_out(0);
  164.        
  165.         int fifo_status = rtf_create(0,100);
  166.         if(fifo_status)
  167.                 dbg_print("FIFO Create failed!");
  168.         fifo_status = rtf_create(1, 4000);
  169.         if(fifo_status)
  170.                 dbg_print("FIFO Create failed!");
  171.        
  172.         rt_com_setup(0, 9600, RT_COM_PARITY_NONE, 1, 8);
  173.         hrtime_t now = gethrtime();
  174.         pthread_attr_init(&attr);
  175.         pthread_mutex_init(&cache_mutex, NULL);
  176.         pthread_attr_setfp_np(&attr, 1);
  177.        
  178.         ret = pthread_create(&pulse_generate_pthread, &attr,
  179.                 pulse_generate_thread, (void *)0);
  180.         if (!ret)
  181.                 pulse_generate_thread_created = 1;
  182.         pthread_make_periodic_np (pulse_generate_pthread, now + 2 * 240000, 80000);
  183.         p . sched_priority = 1;
  184.         pthread_setschedparam (pulse_generate_pthread, SCHED_FIFO, &p);
  185.        
  186.         ret = pthread_create(&main_ctrl_pthread, &attr, main_ctrl_thread, (void *)1);
  187.         if (!ret)
  188.                 main_ctrl_thread_created=1;
  189.         pthread_make_periodic_np (main_ctrl_pthread, now + 2 * 160000, 30000);
  190.         p . sched_priority = 2;
  191.         pthread_setschedparam (main_ctrl_pthread, SCHED_FIFO, &p);
  192.         init_waitqueue_head(&wq);
  193.         kmalloc_kthread_id = kernel_thread(kmalloc_thread, NULL, 0);
  194.         if (kmalloc_kthread_id < 0) {
  195.                 printk(KERN_ERR "fork failed, errno %d\n", -kmalloc_kthread_id);
  196.                 return kmalloc_kthread_id;
  197.         }
  198.         return ret;
  199. }
  200. void cleanup_module(void)
  201. {
  202.        
  203.         int ret = kill_proc(kmalloc_kthread_id, SIGKILL, 1);
  204.         if (!ret) {
  205.                 int count = 10 * HZ;
  206.                
  207.                 while (kmalloc_kthread_state && --count) {
  208.                         current->state = TASK_INTERRUPTIBLE;
  209.                         schedule_timeout(1);
  210.                 }
  211.         }
  212.         if (main_ctrl_thread_created) {
  213.                 pthread_cancel(main_ctrl_pthread);
  214.                 pthread_join(main_ctrl_pthread, NULL);
  215.                 pthread_delete_np(main_ctrl_pthread);
  216.         }
  217.         if (pulse_generate_thread_created) {
  218.                 pthread_cancel(pulse_generate_pthread);
  219.                 pthread_join(pulse_generate_pthread, NULL);
  220.                 pthread_delete_np(pulse_generate_pthread);
  221.         }
  222.         rt_com_setup(0, -1, 0, 0, 0);
  223.         rtf_destroy(0);
  224.         rtf_destroy(1);
  225.         pthread_mutex_destroy (&cache_mutex);
  226. }

复制代码

其实现在有关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();以后再慢慢把不必要的去掉。

目录
打赏
0
0
0
0
47
分享
相关文章
|
10天前
|
linux命令详细说明以及案例
本文介绍了常用的 Linux 命令及其详细说明和示例,包括:`ls`(列出目录内容)、`cd`(更改目录)、`rm` 和 `mv`(删除与移动文件)、`grep`(搜索文本)、`cat`(显示文件内容)以及 `chmod`(更改文件权限)。每个命令均配有功能描述、选项说明及实际案例,帮助用户更好地掌握 Linux 命令的使用方法。
97 56
|
13天前
|
Linux基础:文件和目录类命令分析。
总的来说,这些基础命令,像是Linux中藏匿的小矮人,每一次我们使用他们,他们就把我们的指令准确的传递给Linux,让我们的指令变为现实。所以,现在就开始你的Linux之旅,挥动你的命令之剑,探索这个充满神秘而又奇妙的世界吧!
59 19
|
27天前
|
Linux 常用文件查看命令
`cat` 命令用于连接文件并打印到标准输出,适用于快速查看和合并文本文件内容。常用示例包括:`cat file1.txt` 查看单个文件,`cat file1.txt file2.txt` 合并多个文件,`cat &gt; filename` 创建新文件,`cat &gt;&gt; filename` 追加内容。`more` 和 `less` 命令用于分页查看文件,`tail` 命令则用于查看文件末尾内容,支持实时追踪日志更新,如 `tail -f file.log`。
50 5
Linux 常用文件查看命令
|
10天前
|
linux常用命令详细说明以及案例
本文介绍了Linux中几个常用的命令及其用法,包括:`ls`(列出目录内容)、`cd`(切换目录)、`mkdir`(创建目录)、`rm -p`(删除目录及内容)和`mv`(移动或重命名文件/目录)。每个命令都配有详细说明、语法格式、常见选项及实用案例,帮助用户更好地理解和使用这些基础命令。内容源自[linux常用命令详细说明以及案例](https://linux.ciilii.com/show/news-285.html)。
|
2月前
|
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
111 24
Linux系统之whereis命令的基本使用
|
1月前
|
Linux od命令
本文详细介绍了Linux中的 `od`命令,包括其基本语法、常用选项和示例。通过这些内容,你可以灵活地使用 `od`命令查看文件内容,提高分析和调试效率。确保理解每一个选项和示例的实现细节,应用到实际工作中时能有效地处理各种文件查看需求。
60 19
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
191 25
|
2月前
|
Linux查看内存命令
1. free free命令是最常用的查看内存使用情况的命令。它显示系统的总内存、已使用内存、空闲内存和交换内存的总量。 free -h • -h 选项:以易读的格式(如GB、MB)显示内存大小。 输出示例: total used free shared buff/cache available Mem: 15Gi 4.7Gi 4.1Gi 288Mi 6.6Gi 9.9Gi Swap: 2.0Gi 0B 2.0Gi • to
57 2
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
83 11
|
4月前
|
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
151 14
Linux 10 个“who”命令示例
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等