void *CommandClock_task(void *args) { RT_TASK *mytask; unsigned long command; char R = 'R'; int ackn = 0; RT_TASK *get = (RT_TASK *)0, *put = (RT_TASK *)0, *task; if (!(mytask = rt_thread_init(nam2num("CLKTSK"), 1, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT TASK CommandClock_task\n"); exit(1); } printf("INIT TASK CommandClock_task %p.\n", mytask); mlockall(MCL_CURRENT | MCL_FUTURE); Clockstatus = stopped; while (ackn != ('a' + 'b')) { task = rt_receive((RT_TASK *)0, &command); switch (command) { case 'b': get = task; ackn += command; break; case 'a': put = task; ackn += command; break; } } rt_return(put, command); rt_return(get, command); while(1) { switch (Clockstatus) { case stopped: rt_receive(put, &command); if (command == 'R') { Clockstatus = running; } break; case running: if (rt_receive_if(put, &command)) { if (command == 'T') { Clockstatus = stopped; } } else { command = R; } break; } rt_send(get, command); if (command == 'F') { goto end; } } end: rt_task_delete(mytask); printf("END TASK CommandClock_task %p.\n", mytask); return 0; }
static void fun(long none) { volatile double s, a[MAXDIM], b[MAXDIM]; long msg; RTIME period, wait_delay, sync_time, aim_time; *worst_lat = -2000000000; wait_delay = nano2count(WAIT_DELAY); period = nano2count(PERIOD); for(msg = 0; msg < MAXDIM; msg++) { a[msg] = b[msg] = 3.141592; } rtai_cli(); aim_time = rt_get_time(); sync_time = aim_time + wait_delay; aim_time += period; while (!rt_receive_if(NULL, &msg)) { WAIT_AIM_TIME(); sync_time = rt_get_time(); msg = abs((long)(sync_time - aim_time)); sync_time = aim_time + wait_delay; aim_time += period; if (msg > *worst_lat) { *worst_lat = msg; } s = dot(a,b, MAXDIM); rt_busy_sleep(WORKING_TIME); rt_sleep_until(sync_time); } rtai_sti(); }
static void CommandChrono_task(long t) { RTIME fiveSeconds = nano2count(FIVE_SECONDS); unsigned long command; unsigned int buffered = 0; unsigned int C = 'C'; unsigned int R = 'R'; int ackn = 0; RT_TASK *get = (RT_TASK *)0, *put = (RT_TASK *)0, *task; Chronostatus = stoppedInitial; while (ackn != ('c' + 'd')) { task = rt_receive((RT_TASK *)0, &command); switch (command) { case 'd': get = task; ackn += command; break; case 'c': put = task; ackn += command; break; } } rt_return(put, command); rt_return(get, command); while(1) { cpu_used[hard_cpu_id()]++; switch (Chronostatus) { case stoppedInitial: if (buffered) { command = buffered; buffered = 0; } else { rt_receive(put, &command); } Chronostatus = running; break; case running: if (rt_receive_if(put, &command)) { if (command == 'E') { Chronostatus = stoppedFinal; } } else { command = C; } break; case stoppedFinal: Chronostatus = stoppedInitial; if (rt_receive_timed(put, &command, fiveSeconds) > 0) { buffered = command; } command = R; break; } rt_send(get, command); } }
int main(void) { RT_TASK *spktsk, *plrtsk; RTIME period; MBX *mbx; char data, temp; unsigned int msg, i; // ioperm(PORT_ADR, 1, 1); iopl(3); if (!(spktsk = rt_task_init_schmod(nam2num("SPKTSK"), 1, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT SPEAKER TASK\n"); exit(1); } mbx = rt_mbx_init(nam2num("SNDMBX"), 4000); printf("\nSPEAKER TASK RUNNING\n"); rt_set_oneshot_mode(); start_rt_timer(0); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); period = nano2count(PERIOD); rt_task_make_periodic(spktsk, rt_get_time() + 5*period, period); for (i = 0; i < 100; i++) { plrtsk = rt_receive(0, &msg); rt_return(plrtsk, msg); } while(1) { if (!rt_mbx_receive_if(mbx, &data, 1)) { data = filter(data); temp = inb(PORT_ADR); temp &= 0xfd; temp |= (data & 1) << 1; outb(temp, PORT_ADR); } rt_task_wait_period(); if ((plrtsk = rt_receive_if(0, &msg))) { rt_return(plrtsk, msg); break; } } rt_sleep(nano2count(100000000)); rt_make_soft_real_time(); rt_mbx_delete(mbx); stop_rt_timer(); rt_task_delete(spktsk); printf("\nSPEAKER TASK STOPS\n"); return 0; }
static void *intr_handler(void *args) { RT_TASK *mytask, *master; RTIME period; MBX *mbx; char data = 'G'; char temp; unsigned int msg; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // rt_allow_nonroot_hrt(); ioperm(PORT_ADR, 1, 1); if (!(mytask = rt_task_init_schmod(nam2num("SOUND"), 1, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT SOUND TASK\n"); exit(1); } mbx = rt_get_adr(nam2num("SNDMBX")); mlockall(MCL_CURRENT | MCL_FUTURE); printf("\nINIT SOUND TASK %p\n", mytask); rt_make_hard_real_time(); period = nano2count(PERIOD); rt_mbx_send(mbx, &data, 1); rt_task_make_periodic(mytask, rt_get_time() + 100*period, period); while(1) { if (!rt_mbx_receive_if(mbx, &data, 1)) { data = filter(data); temp = inb(PORT_ADR); temp &= 0xfd; temp |= (data & 1) << 1; outb(temp, PORT_ADR); } rt_task_wait_period(); if ((master = rt_receive_if(0, &msg))) { rt_return(master, msg); break; } } rt_make_soft_real_time(); rt_task_delete(mytask); printf("\nEND SOUND TASK %p\n", mytask); return 0; }
pid_t rt_Creceive(pid_t pid, void *msg, size_t maxsize, size_t *msglen, RTIME delay) { RT_TASK *task; MSGCB *cb; task = pid ? pid2rttask(pid) : 0; if (delay) { task = rt_receive_timed(task, (unsigned int *)&cb, delay); } else { task = rt_receive_if(task, (unsigned int *)&cb); } if (task) { if ((pid = rttask2pid(task))) { *msglen = maxsize <= cb->sbytes ? maxsize : cb->sbytes; if (*msglen) { memcpy(msg, cb->sbuf, *msglen); } return pid; } return 0; } return 0; }
int main(int argc, char *argv[]) { int diff; int sample; long average; int min_diff; int max_diff; int period; int i; RTIME t, svt; RTIME expected, exectime[3]; MBX *mbx; RT_TASK *task, *latchk; struct sample { long long min; long long max; int index, ovrn; } samp; double s = 0.0, sref; long long max = -1000000000, min = 1000000000; signal(SIGINT, endme); signal(SIGKILL, endme); signal(SIGTERM, endme); if (!(mbx = rt_mbx_init(nam2num("LATMBX"), 20*sizeof(samp)))) { printf("CANNOT CREATE MAILBOX\n"); exit(1); } if (!(task = rt_task_init_schmod(nam2num("LATCAL"), 0, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT MASTER LATENCY TASK\n"); exit(1); } printf("\n## RTAI latency calibration tool ##\n"); printf("# period = %i (ns) \n", PERIOD); printf("# average time = %i (s)\n", (int)AVRGTIME); printf("# use the FPU\n"); printf("#%sstart the timer\n", argc == 1 ? " " : " do not "); printf("# timer_mode is %s\n", TIMER_MODE ? "periodic" : "oneshot"); printf("\n"); if (!(hard_timer_running = rt_is_hard_timer_running())) { if (TIMER_MODE) { rt_set_periodic_mode(); } else { rt_set_oneshot_mode(); } period = start_rt_timer(nano2count(PERIOD)); } else { period = nano2count(PERIOD); } for(i = 0; i < MAXDIM; i++) { a[i] = b[i] = 3.141592; } sref = dot(a, b, MAXDIM); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); expected = rt_get_time() + 200*period; rt_task_make_periodic(task, expected, period); svt = rt_get_cpu_time_ns(); i = 0; samp.ovrn = 0; while (!end) { min_diff = 1000000000; max_diff = -1000000000; average = 0; for (sample = 0; sample < SMPLSXAVRG && !end; sample++) { expected += period; if (!rt_task_wait_period()) { if (TIMER_MODE) { diff = (int) ((t = rt_get_cpu_time_ns()) - svt - PERIOD); svt = t; } else { diff = (int) count2nano(rt_get_time() - expected); } } else { samp.ovrn++; diff = 0; if (TIMER_MODE) { svt = rt_get_cpu_time_ns(); } } outb(i = 1 - i, 0x378); if (diff < min_diff) { min_diff = diff; } if (diff > max_diff) { max_diff = diff; } average += diff; s = dot(a, b, MAXDIM); if (fabs((s - sref)/sref) > 1.0e-15) { printf("\nDOT PRODUCT RESULT = %20.16e %20.16e %20.16e\n", s, sref, fabs((s - sref)/sref)); return 0; } } samp.min = min_diff; samp.max = max_diff; samp.index = average/SMPLSXAVRG; #if SOLO if (max < samp.max) max = samp.max; if (min > samp.min) min = samp.min; rt_printk("SOLO * min: %lld/%lld, max: %lld/%lld average: %d (%d) <Hit [RETURN] to stop> *\n", samp.min, min, samp.max, max, samp.index, samp.ovrn); #else rt_mbx_send_if(mbx, &samp, sizeof(samp)); if ((latchk = rt_get_adr(nam2num("LATCHK"))) && (rt_receive_if(latchk, &average) || end)) { rt_return(latchk, average); break; } #endif } while (rt_get_adr(nam2num("LATCHK"))) { rt_sleep(nano2count(1000000)); } rt_make_soft_real_time(); if (!hard_timer_running) { stop_rt_timer(); } rt_get_exectime(task, exectime); if (exectime[1] && exectime[2]) { printf("\n>>> S = %g, EXECTIME = %G\n", s, (double)exectime[0]/(double)(exectime[2] - exectime[1])); } rt_task_delete(task); rt_mbx_delete(mbx); return 0; }
int main(int argc, char *argv[]) { int diff; int skip; long average; int min_diff; int max_diff; int period; int i; RTIME t, svt; RTIME expected, exectime[3]; MBX *mbx; RT_TASK *task, *latchk; struct sample { long long min; long long max; int index, ovrn; } samp; double s; signal(SIGHUP, endme); signal(SIGKILL, endme); signal(SIGTERM, endme); signal(SIGALRM, endme); if (!(mbx = rt_mbx_init(nam2num("LATMBX"), 20*sizeof(samp)))) { printf("CANNOT CREATE MAILBOX\n"); exit(1); } if (!(task = rt_task_init_schmod(nam2num("LATCAL"), 0, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT MASTER TASK\n"); exit(1); } printf("\n## RTAI latency calibration tool ##\n"); printf("# period = %i (ns) \n", PERIOD); printf("# average time = %i (s)\n", (int)AVRGTIME); printf("# use the FPU\n"); printf("#%sstart the timer\n", argc == 1 ? " " : " do not "); printf("# timer_mode is %s\n", TIMER_MODE ? "periodic" : "oneshot"); printf("\n"); if (argc == 1) { if (TIMER_MODE) { rt_set_periodic_mode(); } else { rt_set_oneshot_mode(); } period = start_rt_timer(nano2count(PERIOD)); } else { period = nano2count(PERIOD); } for(i = 0; i < MAXDIM; i++) { a[i] = b[i] = 3.141592; } s = dot(a, b, MAXDIM); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); rt_task_make_periodic(task, expected = rt_get_tscnt() + 10*period, period); svt = rt_get_cpu_time_ns(); samp.ovrn = i = 0; while (!end) { min_diff = 1000000000; max_diff = -1000000000; average = 0; for (skip = 0; skip < SKIP && !end; skip++) { expected += period; if (!rt_task_wait_period()) { if (TIMER_MODE) { diff = (int) ((t = rt_get_cpu_time_ns()) - svt - PERIOD); svt = t; } else { diff = (int) count2nano(rt_get_tscnt() - expected); } } else { samp.ovrn++; diff = 0; if (TIMER_MODE) { svt = rt_get_cpu_time_ns(); } } if (diff < min_diff) { min_diff = diff; } if (diff > max_diff) { max_diff = diff; } average += diff; s = dot(a, b, MAXDIM); } samp.min = min_diff; samp.max = max_diff; samp.index = average/SKIP; rt_mbx_send_if(mbx, &samp, sizeof(samp)); if ((latchk = rt_get_adr(nam2num("LATCHK"))) && (rt_receive_if(latchk, (unsigned long *)&average) || end)) { rt_return(latchk, (unsigned long)average); break; } } while (rt_get_adr(nam2num("LATCHK"))) { rt_sleep(nano2count(1000000)); } rt_make_soft_real_time(); if (argc == 1) { stop_rt_timer(); } rt_get_exectime(task, exectime); if (exectime[1] && exectime[2]) { printf("\n>>> S = %g, EXECTIME = %G\n", s, (double)exectime[0]/(double)(exectime[2] - exectime[1])); } rt_task_delete(task); rt_mbx_delete(mbx); return 0; }
int main(int argc, char *argv[]) { int diff; int skip; int average; int min_diff; int max_diff; int period; int i; RTIME expected, ts; MBX *mbx; RT_TASK *task; struct sample { long long min; long long max; int index; double s; int ts; } samp; double s; if (!(mbx = rt_mbx_init(nam2num("LATMBX"), 20*sizeof(samp)))) { printf("CANNOT CREATE MAILBOX\n"); exit(1); } if (!(task = rt_task_init_schmod(nam2num("LATCAL"), 0, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT MASTER TASK\n"); exit(1); } if (argc == 1) { rt_set_oneshot_mode(); start_rt_timer(0); } for(i = 0; i < MAXDIM; i++) { a[i] = b[i] = 3.141592; } vdot(a, b, MAXDIM); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); period = nano2count(PERIOD); rt_task_make_periodic(task, expected = rt_get_time() + 5*period, period); #ifdef OVERALL min_diff = 1000000000; max_diff = -1000000000; #endif while (1) { #ifndef OVERALL min_diff = 1000000000; max_diff = -1000000000; #endif average = 0; for (skip = 0; skip < SKIP; skip++) { expected += period; rt_task_wait_period(); diff = (int)count2nano(rt_get_time() - expected); if (diff < min_diff) { min_diff = diff; } if (diff > max_diff) { max_diff = diff; } average += diff; ts = rt_get_time(); s = vdot(a, b, MAXDIM); ts = rt_get_time() - expected; } samp.min = min_diff; samp.max = max_diff; samp.index = average/SKIP; samp.s = (double)s; samp.ts = (int)count2nano(ts); rt_mbx_send_if(mbx, &samp, sizeof(samp)); if (rt_receive_if(rt_get_adr(nam2num("LATCHK")), (unsigned int *)&average)) { rt_return(rt_get_adr(nam2num("LATCHK")), (unsigned int)average); break; } } while (rt_get_adr(nam2num("LATCHK"))) { rt_sleep(nano2count(1000000)); } rt_make_soft_real_time(); if (argc == 1) { stop_rt_timer(); } rt_task_delete(task); rt_mbx_delete(mbx); return 0; }