/** * @file * Analogy for Linux, input command test program * * @note Copyright (C) 1997-2000 David A. Schleef * @note Copyright (C) 2008 Alexis Berlemont * * Xenomai is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Xenomai is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Xenomai; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include #include #include #include #include #include #define NB_SCAN 100000 static RT_TASK rt_task_desc; int main(int argc, char *argv[]) { int err; unsigned short buf[10000]; RTIME dt[NB_SCAN], t1, t2; int i; a4l_desc_t dsc = { .sbdata = NULL }; unsigned int chans[] = { // Maximum 16 channels in a scanlist! #define P(i) PACK(i, 0, AREF_GROUND) P(0), P(1), P(2), P(3), P(4), P(5), P(6), P(7), #undef P #define P(i) PACK(i, 1, AREF_GROUND) P(0), P(1), P(2), P(3), P(4), P(5), P(6), P(7), #undef P #define P(i) PACK(i, 2, AREF_GROUND) // P(0), P(1), P(2), P(3), P(4), }; a4l_cmd_t cmd = { .idx_subd = 0, .flags = TRIG_WAKE_EOS, .start_src = TRIG_NOW, .start_arg = 0, .scan_begin_src = TRIG_TIMER, .scan_begin_arg = 350000, /* in ns */ .convert_src = TRIG_TIMER, .convert_arg = 4000, /* in ns */ .scan_end_src = TRIG_COUNT, .scan_end_arg = sizeof(chans)/sizeof(chans[0]), .stop_src = TRIG_COUNT, .stop_arg = NB_SCAN, // .stop_arg = 1, .nb_chan = sizeof(chans)/sizeof(chans[0]), .chan_descs = chans, }; RT_MUTEX mutex; int N = 0; err = mlockall(MCL_CURRENT | MCL_FUTURE); if (err < 0) { fprintf(stderr, "mlockall failed (%d)\n", errno); err = errno; goto out_main; } err = rt_task_shadow(&rt_task_desc, NULL, 90, 0); if (err < 0) { fprintf(stderr, "rt_task_shadow failed (%d)\n", err); goto out_main; } err = rt_mutex_create(&mutex, NULL); if (err < 0) { fprintf(stderr, "rt_mutex_create failed (%d)\n", err); goto out_main; } err = a4l_open(&dsc, "analogy0"); if (err < 0) { fprintf(stderr, "a4l_open failed (%d)\n", err); goto out_mutex; } a4l_snd_cancel(&dsc, cmd.idx_subd); i = 0; err = a4l_snd_command(&dsc, &cmd); if (err < 0) { fprintf(stderr, "a4l_snd_command failed (%d)\n", err); goto out_close; } err = rt_mutex_acquire(&mutex, 0); if (err < 0) { fprintf(stderr, "rt_mutex_acquire failed (%d)\n", err); goto out_close; } rt_mutex_release(&mutex); t1 = rt_timer_read(); for (i = 0 ; i < NB_SCAN ; i++) { xntrace_special('A',i); err = a4l_async_read(&dsc, buf, sizeof(chans)/sizeof(chans[0])*2, A4L_INFINITE); xntrace_special('B',i); if (err < 0) { fprintf(stderr, "a4l_read failed (%d)\n", err); goto out_report; } t2 = rt_timer_read(); xntrace_special('C',i); dt[i] = t2 - t1; t1 = t2; N++; if (i > 200 && dt[i] < cmd.scan_begin_arg - (cmd.scan_begin_arg >> 1) && dt[i-1] > cmd.scan_begin_arg - (cmd.scan_begin_arg >> 1) && dt[i-2] > cmd.scan_begin_arg - (cmd.scan_begin_arg >> 1) && dt[i-3] > cmd.scan_begin_arg - (cmd.scan_begin_arg >> 1)) { xntrace_user_freeze(31414, 1); printf("N=%d\n", N); goto out_report; } } out_report: for (i = 0 ; i < sizeof(chans)/sizeof(chans[0]) ; i++) { printf("0x%4x ", buf[i]); } printf("\n"); for (i = 1 ; i < N ; i++) { if (dt[i] < cmd.scan_begin_arg - (cmd.scan_begin_arg >> 1) || cmd.scan_begin_arg + (cmd.scan_begin_arg >> 1) < dt[i]) { printf("%d (%Ld, %Ld)\n", i, dt[i-1], dt[i]); } } printf("\n"); out_close: a4l_close(&dsc); out_mutex: rt_mutex_delete(&mutex); out_main: return err; }