CANopen SDO read implementiert.
authorFlorian Pose <fp@igh-essen.com>
Fri, 24 Feb 2006 13:14:51 +0000
changeset 80 8cd08d73b560
parent 79 319a97c1f0f9
child 81 28b1defdcc88
CANopen SDO read implementiert.
include/EtherCAT_rt.h
master/canopen.c
master/canopen.h
rt/msr_module.c
--- a/include/EtherCAT_rt.h	Fri Feb 24 13:14:21 2006 +0000
+++ b/include/EtherCAT_rt.h	Fri Feb 24 13:14:51 2006 +0000
@@ -92,6 +92,24 @@
                                   uint32_t value,
                                   size_t size);
 
+int EtherCAT_rt_canopen_sdo_read(ec_slave_t *slave,
+                                 uint16_t sdo_index,
+                                 uint8_t sdo_subindex,
+                                 uint32_t *value);
+
+int EtherCAT_rt_canopen_sdo_addr_write(ec_master_t *master,
+                                       const char *addr,
+                                       uint16_t sdo_index,
+                                       uint8_t sdo_subindex,
+                                       uint32_t value,
+                                       size_t size);
+
+int EtherCAT_rt_canopen_sdo_addr_read(ec_master_t *master,
+                                      const char *addr,
+                                      uint16_t sdo_index,
+                                      uint8_t sdo_subindex,
+                                      uint32_t *value);
+
 /*****************************************************************************/
 
 #endif
--- a/master/canopen.c	Fri Feb 24 13:14:21 2006 +0000
+++ b/master/canopen.c	Fri Feb 24 13:14:51 2006 +0000
@@ -9,16 +9,14 @@
  *****************************************************************************/
 
 #include <linux/delay.h>
+#include <linux/slab.h>
+#include <linux/module.h>
 
 #include "../include/EtherCAT_si.h"
 #include "master.h"
 
 /*****************************************************************************/
 
-// Prototypen
-
-/*****************************************************************************/
-
 /**
    Schreibt ein CANopen-SDO (service data object).
  */
@@ -48,7 +46,7 @@
     EC_WRITE_U16(data + 2,  slave->station_address); // Station address
     EC_WRITE_U8 (data + 4,  0x00); // Channel & priority
     EC_WRITE_U8 (data + 5,  0x03); // CANopen over EtherCAT
-    EC_WRITE_U16(data + 6,  0x2000); // Number & Service
+    EC_WRITE_U16(data + 6,  0x2000); // Number (0), Service (SDO request)
     EC_WRITE_U8 (data + 8,  0x13 | ((4 - size) << 2)); // Spec., exp., init.
     EC_WRITE_U16(data + 9,  sdo_index);
     EC_WRITE_U8 (data + 11, sdo_subindex);
@@ -124,7 +122,146 @@
 
 /*****************************************************************************/
 
+/**
+   Schreibt ein CANopen-SDO (service data object).
+ */
+
+int EtherCAT_rt_canopen_sdo_read(ec_slave_t *slave, /**< EtherCAT-Slave */
+                                 uint16_t sdo_index, /**< SDO-Index */
+                                 uint8_t sdo_subindex, /**< SDO-Subindex */
+                                 uint32_t *value /**< Speicher für gel. Wert */
+                                 )
+{
+    unsigned char data[0xF6];
+    ec_frame_t frame;
+    unsigned int tries_left;
+    ec_master_t *master;
+
+    memset(data, 0x00, 0xF6);
+
+    master = slave->master;
+
+    EC_WRITE_U16(data,      0x0006); // Length of the Mailbox service data
+    EC_WRITE_U16(data + 2,  slave->station_address); // Station address
+    EC_WRITE_U8 (data + 4,  0x00); // Channel & priority
+    EC_WRITE_U8 (data + 5,  0x03); // CANopen over EtherCAT
+    EC_WRITE_U16(data + 6,  0x2000); // Number (0), Service (SDO request)
+    EC_WRITE_U8 (data + 8,  0x1 << 1 | 0x2 << 5); // Exp., Upload request
+    EC_WRITE_U16(data + 9,  sdo_index);
+    EC_WRITE_U8 (data + 11, sdo_subindex);
+
+    ec_frame_init_npwr(&frame, master, slave->station_address, 0x1800, 0xF6,
+                       data);
+
+    if (unlikely(ec_frame_send_receive(&frame) < 0)) return -1;
+
+    if (unlikely(frame.working_counter != 1)) {
+        printk(KERN_ERR "EtherCAT: Mailbox send - Slave %i did not respond!\n",
+               slave->ring_position);
+        return -1;
+    }
+
+    // Read "written bit" of Sync-Manager
+
+    tries_left = 10;
+    while (tries_left)
+    {
+        ec_frame_init_nprd(&frame, master, slave->station_address, 0x808, 8);
+
+        if (unlikely(ec_frame_send_receive(&frame) < 0)) return -1;
+
+        if (unlikely(frame.working_counter != 1)) {
+            printk(KERN_ERR "EtherCAT: Mailbox check - Slave %i did not"
+                   " respond!\n", slave->ring_position);
+            return -1;
+        }
+
+        if (EC_READ_U8(frame.data + 5) & 8) { // Written bit is high
+            break;
+        }
+
+        udelay(1000);
+        tries_left--;
+    }
+
+    if (!tries_left) {
+        printk(KERN_ERR "EtherCAT: Mailbox check - Slave %i timed out.\n",
+               slave->ring_position);
+        return -1;
+    }
+
+    ec_frame_init_nprd(&frame, master, slave->station_address, 0x18F6, 0xF6);
+
+    if (unlikely(ec_frame_send_receive(&frame) < 0)) return -1;
+
+    if (unlikely(frame.working_counter != 1)) {
+        printk(KERN_ERR "EtherCAT: Mailbox receive - Slave %i did not"
+               " respond!\n", slave->ring_position);
+        return -1;
+    }
+
+    if (EC_READ_U8 (frame.data + 5) != 0x03 || // COE
+        EC_READ_U16(frame.data + 6) != 0x3000 || // SDO response
+        EC_READ_U8 (frame.data + 8) >> 5 != 0x02 || // Upload response
+        EC_READ_U16(frame.data + 9) != sdo_index || // Index
+        EC_READ_U8 (frame.data + 11) != sdo_subindex) // Subindex
+    {
+        printk(KERN_ERR "EtherCAT: Illegal mailbox response at slave %i!\n",
+               slave->ring_position);
+        return -1;
+    }
+
+    *value = EC_READ_U32(frame.data + 12);
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+int EtherCAT_rt_canopen_sdo_addr_write(ec_master_t *master,
+                                       /**< EtherCAT-Master */
+                                       const char *addr,
+                                       /**< Addresse, siehe ec_address() */
+                                       uint16_t index,
+                                       /**< SDO-Index */
+                                       uint8_t subindex,
+                                       /**< SDO-Subindex */
+                                       uint32_t value,
+                                       /**< Neuer Wert */
+                                       size_t size
+                                       /**< Größe des Datenfeldes */
+                                       )
+{
+    ec_slave_t *slave;
+    if (!(slave = ec_address(master, addr))) return -1;
+    return EtherCAT_rt_canopen_sdo_write(slave, index, subindex, value, size);
+}
+
+/*****************************************************************************/
+
+int EtherCAT_rt_canopen_sdo_addr_read(ec_master_t *master,
+                                      /**< EtherCAT-Slave */
+                                      const char *addr,
+                                      /**< Addresse, siehe ec_address() */
+                                      uint16_t index,
+                                      /**< SDO-Index */
+                                      uint8_t subindex,
+                                      /**< SDO-Subindex */
+                                      uint32_t *value
+                                      /**< Speicher für gel. Wert */
+                                      )
+{
+    ec_slave_t *slave;
+    if (!(slave = ec_address(master, addr))) return -1;
+    return EtherCAT_rt_canopen_sdo_read(slave, index, subindex, value);
+}
+
+/*****************************************************************************/
+
 EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_write);
+EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_read);
+EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_addr_write);
+EXPORT_SYMBOL(EtherCAT_rt_canopen_sdo_addr_read);
 
 /*****************************************************************************/
 
--- a/master/canopen.h	Fri Feb 24 13:14:21 2006 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-/******************************************************************************
- *
- *  c a n o p e n . h
- *
- *  CANopen over EtherCAT
- *
- *  $Id$
- *
- *****************************************************************************/
-
-#ifndef _CANOPEN_H_
-#define _CANOPEN_H_
-
-/*****************************************************************************/
-
-
-/*****************************************************************************/
-
-#endif
-
-/* Emacs-Konfiguration
-;;; Local Variables: ***
-;;; c-basic-offset:4 ***
-;;; End: ***
-*/
--- a/rt/msr_module.c	Fri Feb 24 13:14:21 2006 +0000
+++ b/rt/msr_module.c	Fri Feb 24 13:14:51 2006 +0000
@@ -118,6 +118,7 @@
 {
     struct ipipe_domain_attr attr; //ipipe
     const ec_field_init_t *field;
+    uint32_t version;
 
     // Als allererstes die RT-lib initialisieren
     if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_globals_register) < 0) {
@@ -152,7 +153,7 @@
                                               field->field_type,
                                               field->field_index,
                                               field->field_count)) {
-            printk(KERN_ERR "EtherCAT: Could not register field!\n");
+            printk(KERN_ERR "Could not register field!\n");
             goto out_release_master;
         }
     }
@@ -160,21 +161,18 @@
     printk(KERN_INFO "Activating master...\n");
 
     if (EtherCAT_rt_master_activate(master)) {
-        printk(KERN_ERR "EtherCAT: Could not activate master!\n");
+        printk(KERN_ERR "Could not activate master!\n");
         goto out_release_master;
     }
 
-#if 0
-    if (EtherCAT_rt_canopen_sdo_write(master, "0:4", 0x4067, 0, 1, 2)) {
-        printk(KERN_ERR "EtherCAT: Could not set SSI baud rate!\n");
+    EtherCAT_rt_master_debug(master, 2);
+    if (EtherCAT_rt_canopen_sdo_addr_read(master, "0:3", 0x100A, 1,
+                                          &version)) {
+        printk(KERN_ERR "Could not read SSI version!\n");
         goto out_release_master;
     }
-
-    if (EtherCAT_rt_canopen_sdo_write(master, "0:4", 0x4061, 4, 1, 1)) {
-        printk(KERN_ERR "EtherCAT: Could not set SSI feature bit!\n");
-        goto out_release_master;
-    }
-#endif
+    printk(KERN_INFO "Klemme 3 Software-version: %u\n", version);
+    EtherCAT_rt_master_debug(master, 0);
 
     ipipe_init_attr(&attr);
     attr.name = "IPIPE-MSR-MODULE";