#include #include #include #include #include #include #include MODULE_DESCRIPTION("Simple SHM demo"); MODULE_AUTHOR("Y. Kobayashi"); MODULE_LICENSE("GPL"); #define STACK_SIZE 4000 #include "adc.h" #include "period.h" static RT_TASK thread; static float *data; static struct my_msg_struct * msg; static RTIME tick_period; static void fun(long t_dummy) { RTIME s_time; float t, t0; int count, count_record = 0, flag = 0; while (1) { if (msg->command == START_TASK) { msg->command = STOP_TASK; flag = 1; count = 0; count_record = 0; s_time = rt_get_time(); t0 = 0; rt_task_make_periodic(&thread, s_time, tick_period); } if(flag == 1){ t = count2nano(rt_get_time() - s_time)*1.0e-9; count++; if(count == 2){ da_conv(1.0, 0); count = 0; }else{ da_conv(0, 0); } if(count_record < BUF_LEN * RECORDING_SAMPLES){ data[count_record++] = t; // 現在時刻 data[count_record++] = t - t0; // 前回からの差分 t0 = t; }else{ flag = 0; rt_task_make_periodic(&thread, rt_get_time(), tick_period*PERIOD_COUNT); } } rt_task_wait_period(); } } int init_module(void) { // RTIME tick_period; RTIME now; if((data = (float *)rtai_kmalloc(nam2num("data"), BUF_LEN * RECORDING_SAMPLES * sizeof(float))) == NULL){ return -ENOMEM; } if((msg = (struct my_msg_struct *)rtai_kmalloc(nam2num("msg"), sizeof(struct my_msg_struct))) == NULL){ return -ENOMEM; } msg->command = STOP_TASK; rt_task_init(&thread, fun, 0, STACK_SIZE, 0, 1, 0); // 後ろから 2 番地目の 1 = use_fpu を有効にしておかないと、data[] の一部に nan が入る tick_period = start_rt_timer(nano2count(SAMPLING_PERIOD)); rt_linux_use_fpu(1); now = rt_get_time(); rt_task_make_periodic(&thread, now + tick_period, tick_period*PERIOD_COUNT); init_dac(); return 0; } void cleanup_module(void) { stop_rt_timer(); rt_busy_sleep(10000000); rt_task_delete(&thread); rtai_kfree(nam2num("data")); rtai_kfree(nam2num("msg")); cleanup_dac(); }