Permalink
Browse files

Updated RTDM test program + PCI/README.txt

  • Loading branch information...
1 parent bdf51cf commit 63c6c035919087853d34273e98ba22f65dd0070d @pficheux committed Nov 14, 2011
Showing with 178 additions and 132 deletions.
  1. +10 −8 Dragon_PCI/PCI/README.txt
  2. +168 −124 Dragon_PCI/PCI/RTDM/posix/xenomai_pci.c
View
@@ -180,14 +180,16 @@ PCI IO RTDM driver loading
pci_io_pci_probe: found 100:0
pci_io_pci_probe: IO resource base address : ec00
-# xenomai_square_gpio -p 5000000 -r pci_io0
-RTDM mode on "pci_io0" !
-Period= 5000000 ns
-GPIO device pci_io0 opened!
-Loop= 400 sec= 1313661580 nsec= 357121091 delta= 4999835 ns jitter= -165 ns
-Loop= 800 sec= 1313661581 nsec= 357120852 delta= 4999836 ns jitter= -164 ns
-Loop= 1200 sec= 1313661582 nsec= 357121450 delta= 5000674 ns jitter= 674 ns
-Loop= 1600 sec= 1313661583 nsec= 357121210 delta= 4999835 ns jitter= -165 ns
+# xenomai_pci -p 1000000 -k
+Using RTDK
+pci_io_open: opening device 0
+== Period: 1000 us
+DOMAIN SWITCH !!
+Loop= 2000 dt= 0 999759 (-241 ns)
+Loop= 4000 dt= 0 999759 (-241 ns)
+Loop= 6000 dt= 0 999759 (-241 ns)
+Loop= 8000 dt= 0 999759 (-241 ns)
+Loop= 10000 dt= 0 1000597 (597 ns)
...
@@ -1,150 +1,194 @@
-/*
+/*
* Xenomai version of SQUARE example, POSIX skin + RTDM driver
*/
-#include <err.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <sys/io.h>
+#include <unistd.h>
+#include <stdlib.h>
#include <math.h>
-#include <pthread.h>
-#include <rtdm/rtdm.h>
-#include <signal.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
-#include <sys/io.h>
-#include <sys/mman.h>
-#include <sys/time.h>
+#include <signal.h>
+#include <getopt.h>
#include <time.h>
-#include <unistd.h>
-
-#include <pci_io_rtdm.h>
+#include <pthread.h>
+#include <rtdm/rtdm.h>
+#include <native/task.h>
+#include <rtdk.h>
-#define __unused __attribute__((__unused__))
+pthread_t thid_square;
+int use_rtdk;
-pthread_t th_led_id;
+#define PERIOD 50000000LL /* 50 ms = 50 x 1000000 ns */
+int led_mask=0x01;
int fd;
-unsigned long long int period_ns;
+long period_ns = 0;
+int loop_prt = 100; /* print every 100 loops: 5 s */
int test_loops = 0; /* outer loop count */
-void *th_led_callback(void *args __unused)
+/* Thread function*/
+void *thread_square (void *dummy)
{
- int error;
- struct timespec start, period, t, told;
- int led_val = 0, loop_prt;
-
- // Display every 2 sec
- loop_prt = 2000000000 / period_ns;
-
- /* Start a periodic task in 1s */
- clock_gettime(CLOCK_REALTIME, &start);
- start.tv_sec += 1;
- period.tv_sec = 0;
- period.tv_nsec = period_ns;
-
- // Make task periodic
- if ((error = pthread_make_periodic_np(pthread_self(), &start, &period)) != 0)
- errx(EXIT_FAILURE, "failed to set periodic (code = %d)", error);
-
- /* Main loop */
- for (;;) {
- unsigned long overruns = 0;
-
- test_loops++;
-
- /* Wait scheduler */
- error = pthread_wait_np(&overruns);
-
- if (error || overruns)
- errx(EXIT_FAILURE, "wait_period failed"
- " (err = %d) (overruns = %lu)", error, overruns);
-
- /* Write value with RTDM ioctl or write */
-// if (rt_dev_ioctl(fd, PCI_IO_IOCTL_WRITE, led_val) < 0)
-// errx(EXIT_FAILURE, "ioctl error");
- if (rt_dev_write(fd, &led_val, sizeof(int)) < 0)
- errx(EXIT_FAILURE, "write error");
-
- led_val = ~led_val;
-
- /* Read value */
- // rt_dev_ioctl(fd, PCI_IO_IOCTL_READ, &val);
-
- /* old_time <-- current_time */
- told.tv_sec = t.tv_sec;
- told.tv_nsec = t.tv_nsec;
-
- /* Get current time */
- clock_gettime(CLOCK_REALTIME, &t);
-
- /* Print if necessary */
- if ((test_loops % loop_prt) == 0)
- printf("Loop= %d sec= %ld nsec=%ld jitter= %lld ns\n", test_loops, t.tv_sec - told.tv_sec, t.tv_nsec - told.tv_nsec, t.tv_nsec - told.tv_nsec - period_ns);
- }
+ int err;
+ struct timespec start, period, t, told;
+
+ // Display every 2 sec
+ loop_prt = 2000000000 / period_ns;
+
+ /* Start a periodic task in 1s */
+ clock_gettime(CLOCK_REALTIME, &start);
+ start.tv_sec += 1;
+ period.tv_sec = 0;
+ period.tv_nsec = period_ns;
+
+ // Warn switch
+ rt_task_set_mode(0, T_WARNSW, NULL);
+
+ // Make task periodic
+ err = pthread_make_periodic_np(pthread_self(), &start, &period);
+
+ if (err)
+ {
+ fprintf(stderr,"square: failed to set periodic, code %d\n",err);
+ exit(EXIT_FAILURE);
+ }
+
+ /* Main loop */
+ for (;;)
+ {
+ unsigned long overruns = 0;
+
+ test_loops++;
+
+ /* Wait scheduler */
+ err = pthread_wait_np(&overruns);
+
+ if (err || overruns) {
+ fprintf(stderr,"wait_period failed: err %d, overruns: %lu\n", err, overruns);
+ exit(EXIT_FAILURE);
+ }
+
+ /* Write value with RTDM ioctl or write */
+ // if (rt_dev_ioctl(fd, PCI_IO_IOCTL_WRITE, led_val) < 0)
+ // errx(EXIT_FAILURE, "ioctl error");
+
+ /* Write to // port */
+ rt_dev_write(fd, (void *)&led_mask, 1);
+ led_mask = ~led_mask;
+
+ /* Read value */
+ // rt_dev_ioctl(fd, PCI_IO_IOCTL_READ, &val);
+
+ /* old_time <-- current_time */
+ told.tv_sec = t.tv_sec;
+ told.tv_nsec = t.tv_nsec;
+
+ /* Get current time */
+ clock_gettime (CLOCK_REALTIME, &t);
+
+ /* Print if necessary */
+ if ((test_loops % loop_prt) == 0) {
+ if (use_rtdk)
+ rt_printf ("Loop= %d dt= %ld %ld (%ld ns)\n", test_loops, t.tv_sec - told.tv_sec, t.tv_nsec - told.tv_nsec, t.tv_nsec - told.tv_nsec - period_ns);
+ else
+ printf ("Loop= %d dt= %ld %ld (%ld ns)\n", test_loops, t.tv_sec - told.tv_sec, t.tv_nsec - told.tv_nsec, t.tv_nsec - told.tv_nsec - period_ns);
+ }
+ }
}
-void cleanup_upon_sig(int sig __unused)
+void cleanup_upon_sig(int sig __attribute__((unused)))
{
- pthread_cancel(th_led_id);
- pthread_join(th_led_id, NULL);
- rt_dev_close(fd);
+ pthread_cancel (thid_square);
+ pthread_join (thid_square, NULL);
+ rt_dev_close (fd);
- exit(EXIT_SUCCESS);
+ exit(0);
}
-void usage()
+void warn_upon_switch(int sig __attribute__((unused)))
{
- extern char * __progname;
-
- fprintf(stderr, "usage: %s dev period ", __progname);
+ printf ("DOMAIN SWITCH !!\n");
}
-int main (int argc, char **argv)
+void usage (char *s)
{
- int error;
- char *device;
- struct sched_param th_led_param = { .sched_priority = 99 };
- pthread_attr_t th_led_attr;
-
- if (argc < 3) {
- usage();
- return EXIT_FAILURE;
- }
-
- device = argv[1];
- period_ns = atoi(argv[2]); /* ns */
-
- if (period_ns == 0)
- errx(EXIT_FAILURE, "invalid period");
-
- signal(SIGINT, cleanup_upon_sig);
- signal(SIGTERM, cleanup_upon_sig);
- signal(SIGHUP, cleanup_upon_sig);
-
- printf("Period: %lld us\n", period_ns / 1000);
-
- // Avoid paging: MANDATORY for TR !!
- mlockall(MCL_CURRENT | MCL_FUTURE);
-
- // Open RTDM driver
- if ((fd = rt_dev_open(device, 0)) < 0)
- err(EXIT_FAILURE, "rt_open");
-
- // Thread attributes
- pthread_attr_init(&th_led_attr);
-
- // Joinable
- pthread_attr_setdetachstate(&th_led_attr, PTHREAD_CREATE_JOINABLE);
-
- // Priority, set priority to 99
- pthread_attr_setinheritsched(&th_led_attr, PTHREAD_EXPLICIT_SCHED);
- pthread_attr_setschedpolicy(&th_led_attr, SCHED_FIFO);
- pthread_attr_setschedparam(&th_led_attr, &th_led_param);
-
- // Create thread
- if ((error = pthread_create(&th_led_id, &th_led_attr, &th_led_callback, NULL)) != 0)
- errx(EXIT_FAILURE, "failed to create thread (code = %d)", error);
-
- pause();
+ fprintf (stderr, "Usage: %s [-p period] [-k]\n", s);
+ exit (1);
+}
- return EXIT_SUCCESS;
+int main (int ac, char **av)
+{
+ int err;
+ struct sched_param param_square = {.sched_priority = 99 };
+ pthread_attr_t thattr_square;
+ char *cp, *progname = (char*)basename(av[0]);
+
+ period_ns = PERIOD; /* ns */
+
+ while (--ac) {
+ if ((cp = *++av) == NULL)
+ break;
+ if (*cp == '-' && *++cp) {
+ switch(*cp) {
+ case 'k' :
+ use_rtdk = 1;
+ rt_print_auto_init(1);
+ printf ("Using RTDK\n");
+ break;
+
+ case 'p' :
+ period_ns = (unsigned long)atoi(*++av); break;
+
+ default:
+ usage(progname);
+ break;
+ }
+ }
+ else
+ break;
+ }
+
+ signal(SIGINT, cleanup_upon_sig);
+ signal(SIGTERM, cleanup_upon_sig);
+ signal(SIGHUP, cleanup_upon_sig);
+ signal(SIGALRM, cleanup_upon_sig);
+ signal(SIGXCPU, warn_upon_switch);
+
+ printf("== Period: %ld us\n", period_ns / 1000);
+
+ // Avoid paging: MANDATORY for RT !!
+ mlockall(MCL_CURRENT|MCL_FUTURE);
+
+ // Open RTDM driver
+ if ((fd = rt_dev_open("pci_io0", 0)) < 0) {
+ perror("rt_open");
+ exit(EXIT_FAILURE);
+ }
+
+ // Thread attributes
+ pthread_attr_init(&thattr_square);
+
+ // Joinable
+ pthread_attr_setdetachstate(&thattr_square, PTHREAD_CREATE_JOINABLE);
+
+ // Priority, set priority to 99
+ pthread_attr_setinheritsched(&thattr_square, PTHREAD_EXPLICIT_SCHED);
+ pthread_attr_setschedpolicy(&thattr_square, SCHED_FIFO);
+ pthread_attr_setschedparam(&thattr_square, &param_square);
+
+ // Create thread
+ err = pthread_create(&thid_square, &thattr_square, &thread_square, NULL);
+
+ if (err)
+ {
+ fprintf(stderr,"square: failed to create square thread, code %d\n",err);
+ return 0;
+ }
+
+ pause();
+
+ return 0;
}

0 comments on commit 63c6c03

Please sign in to comment.