/* * Copyright (C) 2019 BNIS * Make file 请参考过往博客 * 本例程在模块初始化时启动3个任务。在任务1中,实现了类似定时器 * 的定时功能,通过第2任务去结束第1任务,任务3启动后等待任务2结束再继续执行. * */ #include <linux/module.h> #include <rtdm/driver.h> #include <rtdm/testing.h> MODULE_DESCRIPTION("---RTDM test helper module----"); MODULE_AUTHOR("bniss@aliyun.com"); MODULE_VERSION("0.1.0"); MODULE_LICENSE("GPL"); struct rtdm_basic_context { unsigned long number; }; rtdm_task_t test_task1; rtdm_task_t test_task2; rtdm_task_t test_task3; nanosecs_abs_t initTm; rtdm_timer_t test_timer; rtdm_event_t event; int run = 1 ; static int rtdm_bnis_open(struct rtdm_fd *fd, int oflags) { printk("#device_bnis is open .flag = %d. \n" , oflags ); //app-rt_dev_open(DEVICE_NAME, 0); return 0; } static void rtdm_bnis_close(struct rtdm_fd *fd) { printk("#device_bnis close. \n"); app-rt_dev_close(DEVICE_NAME); } static struct rtdm_driver rtdm_bnis_driver = { .profile_info = RTDM_PROFILE_INFO(rtdm_bnis_basic, RTDM_CLASS_TESTING, RTDM_SUBCLASS_RTDMTEST, RTTST_PROFILE_VER), .device_flags = RTDM_NAMED_DEVICE | RTDM_EXCLUSIVE, .device_count = 2, .context_size = sizeof(struct rtdm_basic_context), .ops = { .open = rtdm_bnis_open, .close = rtdm_bnis_close, }, }; static struct rtdm_device myDevice = { .driver = &rtdm_bnis_driver, .label = "bnis_rtdm", }; static void task_msleep(uint64_t ms){ uint64_t allNas = 1000000*ms; rtdm_task_sleep(allNas); } static void task_test_handler1(void *arg){ int err,res ; uint64_t tmp,cnt; printk("======begin task1========%ld \n",rtdm_clock_read()-initTm); tmp = rtdm_clock_read(); err = rtdm_task_set_period(NULL,0,500000000ULL); cnt = 0; while( run > 0 ){ res = rtdm_task_wait_period( NULL ); printk("res = %d , cnt = %d ,period take = %ld" , res , cnt ,rtdm_clock_read() -tmp); tmp = rtdm_clock_read(); if( res!=0) break; if(cnt++>20){ break; } } printk("exit task1 ,cnt = %ld .run =%d ",cnt ,run ); } static void task_test_handler2(void *arg) { printk("======begin task2========%ld \n",rtdm_clock_read()-initTm); task_msleep(5000); //sleep 5 sec printk("=============destroy task1 in task2========\n"); rtdm_task_destroy(&test_task1); exit: printk("=============exit task2========\n"); rtdm_task_destroy(&test_task2); } static void task_test_handler3(void *arg) { printk("======begin task3========%ld \n",rtdm_clock_read()-initTm); rtdm_task_join(&test_task2); //wait on task2 exit exit: printk("=============exit task3========\n"); rtdm_task_destroy(&test_task3); } static int __init rtdm_test_init(void) { printk("====init test for rtdm=========\n"); rtdm_dev_register( &myDevice); initTm = rtdm_clock_read(); rtdm_task_init(&test_task1, "rtdm_test_task1",task_test_handler1, NULL,0, 0); rtdm_task_init(&test_task2, "rtdm_test_task2",task_test_handler2, NULL,0, 0); rtdm_task_init(&test_task3, "rtdm_test_task3",task_test_handler3, NULL,0, 0); return 0; } static void __exit rtdm_test_exit(void) { printk("=======exit rtdm test =========\n"); rtdm_dev_unregister(&myDevice); } module_init(rtdm_test_init); module_exit(rtdm_test_exit);
本例子展示了xenomai的任务调度机制,和api应用。(执行insmod效果图)
本例程在模块初始化时启动3个任务,通过其功能逻辑从而了解api的作用。在任务1中,实现了类似定时器的定时功能,通过第2任务去结束第1任务,任务3启动后等待任务2结束再继续执行.
1)rtdm任务初始化函数原型 :
int rtdm_task_init( rtdm_task_t *task, const char *name,
rtdm_task_proc_t task_proc, void *arg,
int priority, nanosecs_rel_t period)
参数1,task为任务句柄;
参数2,name为可选任务名称;
参数3,task_proc为任务的回调函数;
参数4,arg为回调函数task_proc传递指针参数;
参数5,priority为任务的优先级,0为最低优先,99为最高优先,通过rtdm_task_set_priority设置任务优先级;
参数6,period循环任务的纳秒周期,非循环模式为0。使用rtdm_task_wait_period()来等待第一个和后续的周期性事件。
2)rtdm任务加入函数原型 :
void rtdm_task_join(rtdm_task_t * task) ;
函数功能:等待某实时任务终止。其中task由rtdm_task_init(),目标任务task必须是未结束的,否则本函数永远无法结束返回。
3)rtdm任务等待函数原型:
int rtdm_task_wait_period(unsigned long * overruns_r);
函数功能:等待下一个实时任务周期。overruns_r为传入等待超时的时间变量地址。
————————————————
版权声明:本文为CSDN博主「wabil」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wabil/article/details/104264657