RTAI的用户空间编程(四)——代码基础框架

前面先准备三个文件:
1. Makefile

TARGET = periodic_task  
SRCS = periodic_task.c 
prefix := $(shell rtai-config --prefix)
ifeq ($(prefix),)
$(error Please add /bin to your PATH variable)
endif
OBJECTS = $(SRCS:.c=.o)
CC = $(shell rtai-config --cc)
LXRT_CFLAGS = $(shell rtai-config --lxrt-cflags)
LXRT_LDFLAGS = $(shell rtai-config --lxrt-ldflags
all: $(TARGET)
%.o: %.c
    $(CC) -c  $(LXRT_CFLAGS) $<
$(TARGET) :  $(OBJECTS)
    $(CC)  -o $(TARGET) $(LXRT_LDFLAGS) -llxrt $(OBJECTS)
clean:
    rm -f *.o  *~ core.* $(TARGET)
.PHONY: clean

2. run
实际上是调用rtai-load来运行的.

${DESTDIR}/usr/realtime/bin/rtai-load

3. .runinfo
隐藏文件rtai-load根据这个来运行

prog_name:lxrt+sem+mbx+msg+fifos:!./prog_name; popall:control_c

(2 、3步骤可以简化成另一种方式,直接进入RTAI安装目录,然后insmod rtai_hal.ko insmod rtai_sched.ko insmod rtai_lxrt.ko……,然后运行程序./prog_name 就行了)

4. 代码框架

  • 单触发模式
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
//#include 
#include 
#include 
#include 
//#include 
//#include 

static int thread0;
static void *fun0(void *arg)
{
    RT_TASK *task;
    task = rt_task_init_schmod(nam2num("TASK0"), 0, 0, 0, SCHED_FIFO, 0xF);
     mlockall(MCL_CURRENT | MCL_FUTURE); 
    //设置优先级和调度算法 这里他用SCHED_FIFO,而SCHED_OTHER(基于时间片)优先会低些,这个Name就是实时任务的标识  nam2num 是把名字转为name id 以免命名冲突

    // 进入硬实时模式,无此语句时,默认的是软实时
    //rt_make_hard_real_time();

    //此处添加代码,如下语句
    //rt_printk("Hello World!\n");

    //将实时任务或者线程返回到标准linux的状态
    rt_make_soft_real_time();

    //删除任务
    rt_task_delete(task);
    return 0;
}

int main(void)
{
    RT_TASK *task;

    // make main thread LXRT soft realtime
    task = rt_task_init_schmod(nam2num("MYTASK"), 9, 0, 0, SCHED_FIFO, 0xF);
    mlockall(MCL_CURRENT | MCL_FUTURE);

    // start realtime timer and scheduler
    rt_set_oneshot_mode();
    start_rt_timer(0);

    // create a linux thread 
    thread0 = rt_thread_create(fun0, NULL, 10000);

    // wait for end of program
    printf("TYPE  TO TERMINATE\n");
    getchar();

    // cleanup stuff
    stop_rt_timer();
    return 0;
}

- 周期模式

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define TICK_TIME 1000000
#define CPUMAP 0x1

static RT_TASK *main_Task;
static RT_TASK *loop_Task;
int keep_on_running = 1;
static pthread_t main_thread;
static RTIME expected;
static RTIME sampling_interval;

static void *main_loop()
{
    if (!(loop_Task = rt_task_init_schmod(nam2num("RTAI01"), 2, 0, 0, SCHED_FIFO, CPUMAP))) 
    {
        printf("CANNOT INIT PERIODIC TASK\n");
        exit(1); 
    } 

    //mlockall(MCL_CURRENT | MCL_FUTURE);(源代码没有,…?)
    expected = rt_get_time() + 100*sampling_interval;
    rt_task_make_periodic(loop_Task, expected, sampling_interval); 

    rt_make_hard_real_time();

    while (keep_on_running)
    {
        //insert your main periodic loop here
        rt_task_wait_period();//
        //set keep_on_running to 0 if you want to exit
    } 

    rt_task_delete(loop_Task);
    return 0;
}

int main(void)
{
    RT_TASK *Main_Task;
    if (!(Main_Task = rt_task_init_schmod(nam2num("MNTSK"), 0, 0, 0, SCHED_FIFO, 0xF))) 
    {
        printf("CANNOT INIT MAIN TASK\n");
        exit(1);
    } 

    //mlockall(MCL_CURRENT | MCL_FUTURE); 
    if ((hard_timer_running = rt_is_hard_timer_running()))
    {
        printf("Skip hard real_timer setting...\n");
        sampling_interval = nano2count(TICK_TIME);
    }
    else
    {
        printf("Starting real time timer...\n");
        rt_set_oneshot_mode();
        start_rt_timer(0);// 启动计时器 记得用完要关掉
    } 

    sampling_interval = nano2count(TICK_TIME);
    pthread_create(&main_thread, NULL, main_loop, NULL);
    while (keep_on_running)
        sampling_interval = sampling_interval; //do nothing!

    rt_task_delete(Main_Task);
    return 0;
}

你可能感兴趣的:(Linux,RTAI)