Activate, Send and received; cyclic timer with setitimer().
authorFlorian Pose <fp@igh-essen.com>
Thu, 02 Oct 2008 13:40:23 +0000
changeset 1247 5f1f1a3e6636
parent 1246 4042bda8c980
child 1248 3cc16b60a571
Activate, Send and received; cyclic timer with setitimer().
examples/user/main.c
lib/master.c
master/cdev.c
master/ioctl.h
--- a/examples/user/main.c	Thu Oct 02 10:45:40 2008 +0000
+++ b/examples/user/main.c	Thu Oct 02 13:40:23 2008 +0000
@@ -4,15 +4,39 @@
  *
  ****************************************************************************/
 
+#include <unistd.h>
+#include <sys/time.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <signal.h>
+
 #include "ecrt.h"
 
 /****************************************************************************/
 
+static unsigned int sig_alarms = 0;
+static unsigned int user_alarms = 0;
+
+/****************************************************************************/
+
+void signal_handler(int signum) {
+    switch (signum) {
+        case SIGALRM:
+            sig_alarms++;
+            break;
+    }
+}
+
+/****************************************************************************/
+
 int main(int argc, char **argv)
 {
 	ec_master_t *master;
 	ec_domain_t *domain;
 	ec_slave_config_t *sc;
+    struct sigaction sa;
+    struct itimerval tv;
     
     master = ecrt_request_master(0);
 	if (!master)
@@ -26,8 +50,41 @@
     if (!sc)
         return -1;
 
+    if (ecrt_master_activate(master))
+        return -1;
+
+    sa.sa_handler = signal_handler;
+    sigemptyset(&sa.sa_mask);
+    sa.sa_flags = 0;
+    if (sigaction(SIGALRM, &sa, 0)) {
+        fprintf(stderr, "Failed to install signal handler!\n");
+        return -1;
+    }
+
+    tv.it_interval.tv_sec = 0;
+    tv.it_interval.tv_usec = 10000;
+    tv.it_value.tv_sec = 0;
+    tv.it_value.tv_usec = 1;
+    if (setitimer(ITIMER_REAL, &tv, NULL)) {
+        fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
+        return 1;
+    }
+
 	while (1) {
-		sleep(1);
+        sleep(1);
+
+        while (sig_alarms != user_alarms) {
+
+#if 0
+            struct timeval t;
+            gettimeofday(&t, NULL);
+            printf("%u %u\n", t.tv_sec, t.tv_usec);
+#endif
+            ecrt_master_receive(master);
+            ecrt_master_send(master);
+
+            user_alarms++;
+        }
 	}
 
 	return 0;
--- a/lib/master.c	Thu Oct 02 10:45:40 2008 +0000
+++ b/lib/master.c	Thu Oct 02 13:40:23 2008 +0000
@@ -102,6 +102,12 @@
 
 int ecrt_master_activate(ec_master_t *master)
 {
+    if (ioctl(master->fd, EC_IOCTL_ACTIVATE, NULL) == -1) {
+        fprintf(stderr, "Failed to activate master: %s\n",
+                strerror(errno));
+        return -1; 
+    }
+
     return 0;
 }
 
@@ -109,12 +115,18 @@
 
 void ecrt_master_send(ec_master_t *master)
 {
+    if (ioctl(master->fd, EC_IOCTL_SEND, NULL) == -1) {
+        fprintf(stderr, "Failed to send: %s\n", strerror(errno));
+    }
 }
 
 /*****************************************************************************/
 
 void ecrt_master_receive(ec_master_t *master)
 {
+    if (ioctl(master->fd, EC_IOCTL_RECEIVE, NULL) == -1) {
+        fprintf(stderr, "Failed to receive: %s\n", strerror(errno));
+    }
 }
 
 /*****************************************************************************/
@@ -123,5 +135,4 @@
 {
 }
 
-
 /*****************************************************************************/
--- a/master/cdev.c	Thu Oct 02 10:45:40 2008 +0000
+++ b/master/cdev.c	Thu Oct 02 13:40:23 2008 +0000
@@ -1452,6 +1452,63 @@
     return 0;
 }
 
+/*****************************************************************************/
+
+/** Activates the master.
+ */
+int ec_cdev_ioctl_activate(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+	if (unlikely(!priv->requested))
+		return -EPERM;
+
+    if (ecrt_master_activate(master))
+        return -EIO;
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+/** Send frames.
+ */
+int ec_cdev_ioctl_send(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+	if (unlikely(!priv->requested))
+		return -EPERM;
+
+    spin_lock_bh(&master->internal_lock);
+    ecrt_master_send(master);
+    spin_unlock_bh(&master->internal_lock);
+    return 0;
+}
+
+/*****************************************************************************/
+
+/** Receive frames.
+ */
+int ec_cdev_ioctl_receive(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+	if (unlikely(!priv->requested))
+		return -EPERM;
+
+    spin_lock_bh(&master->internal_lock);
+    ecrt_master_receive(master);
+    spin_unlock_bh(&master->internal_lock);
+    return 0;
+}
+
 /******************************************************************************
  * File operations
  *****************************************************************************/
@@ -1577,6 +1634,18 @@
             if (!(filp->f_mode & FMODE_WRITE))
 				return -EPERM;
 			return ec_cdev_ioctl_create_slave_config(master, arg, priv);
+        case EC_IOCTL_ACTIVATE:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_activate(master, arg, priv);
+        case EC_IOCTL_SEND:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_send(master, arg, priv);
+        case EC_IOCTL_RECEIVE:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_receive(master, arg, priv);
         default:
             return -ENOTTY;
     }
--- a/master/ioctl.h	Thu Oct 02 10:45:40 2008 +0000
+++ b/master/ioctl.h	Thu Oct 02 13:40:23 2008 +0000
@@ -82,6 +82,9 @@
 #define EC_IOCTL_REQUEST                EC_IO(0x16)
 #define EC_IOCTL_CREATE_DOMAIN          EC_IO(0x17)
 #define EC_IOCTL_CREATE_SLAVE_CONFIG  EC_IOWR(0x18, ec_ioctl_config_t)
+#define EC_IOCTL_ACTIVATE               EC_IO(0x19)
+#define EC_IOCTL_SEND                   EC_IO(0x1a)
+#define EC_IOCTL_RECEIVE                EC_IO(0x1b)
 
 #define EC_IOCTL_STRING_SIZE 64