Introduced ec_slave_online_state_t, ec_slave_set_online_state(), renamed
authorFlorian Pose <fp@igh-essen.com>
Mon, 05 Mar 2007 09:55:39 +0000
changeset 610 29489a43ecee
parent 609 611d738a1392
child 611 16498ed938f4
Introduced ec_slave_online_state_t, ec_slave_set_online_state(), renamed
slave->online to ec_online_state and introduced
ec_slave_set_app_state().
master/ethernet.c
master/fsm_master.c
master/master.c
master/slave.c
master/slave.h
--- a/master/ethernet.c	Mon Mar 05 09:10:02 2007 +0000
+++ b/master/ethernet.c	Mon Mar 05 09:55:39 2007 +0000
@@ -317,7 +317,8 @@
 
 void ec_eoe_state_rx_start(ec_eoe_t *eoe /**< EoE handler */)
 {
-    if (!eoe->slave->online || !eoe->slave->master->main_device.link_state)
+    if (eoe->slave->online_state == EC_SLAVE_OFFLINE ||
+            !eoe->slave->master->main_device.link_state)
         return;
 
     ec_slave_mbox_prepare_check(eoe->slave, &eoe->datagram);
@@ -518,7 +519,8 @@
     unsigned int wakeup = 0;
 #endif
 
-    if (!eoe->slave->online || !eoe->slave->master->main_device.link_state)
+    if (eoe->slave->online_state == EC_SLAVE_OFFLINE ||
+            !eoe->slave->master->main_device.link_state)
         return;
 
     spin_lock_bh(&eoe->tx_queue_lock);
--- a/master/fsm_master.c	Mon Mar 05 09:10:02 2007 +0000
+++ b/master/fsm_master.c	Mon Mar 05 09:55:39 2007 +0000
@@ -185,7 +185,7 @@
         // link is down
         fsm->slaves_responding = 0;
         list_for_each_entry(slave, &master->slaves, list) {
-            slave->online = 0;
+            ec_slave_set_online_state(slave, EC_SLAVE_OFFLINE);
         }
         fsm->state = ec_fsm_master_state_error;
         return;
@@ -302,7 +302,7 @@
         up(&master->eeprom_sem);
 
         slave = request->slave;
-        if (!slave->online || slave->error_flag) {
+        if (slave->online_state == EC_SLAVE_OFFLINE || slave->error_flag) {
             EC_ERR("Discarding EEPROM data, slave %i not ready.\n",
                     slave->ring_position);
             request->state = EC_EEPROM_REQ_ERROR;
@@ -344,7 +344,7 @@
     // check if any slaves are not in the state, they're supposed to be
     list_for_each_entry(slave, &master->slaves, list) {
         if (slave->error_flag
-            || !slave->online
+            || slave->online_state == EC_SLAVE_OFFLINE
             || slave->requested_state == EC_SLAVE_STATE_UNKNOWN
             || (slave->current_state == slave->requested_state
                 && slave->self_configured)) continue;
@@ -380,7 +380,7 @@
                 EC_DBG("Processing SDO request...\n");
             slave = master->sdo_request->sdo->slave;
             if (slave->current_state == EC_SLAVE_STATE_INIT
-                || !slave->online) {
+                || slave->online_state == EC_SLAVE_OFFLINE) {
                 EC_ERR("Failed to process SDO request, slave %i not ready.\n",
                        slave->ring_position);
                 master->sdo_request->return_code = -1;
@@ -403,7 +403,7 @@
                 || slave->sdo_dictionary_fetched
                 || slave->current_state == EC_SLAVE_STATE_INIT
                 || jiffies - slave->jiffies_preop < EC_WAIT_SDO_DICT * HZ
-                || !slave->online
+                || slave->online_state == EC_SLAVE_OFFLINE
                 || slave->error_flag) continue;
 
             if (master->debug_level) {
@@ -459,7 +459,7 @@
     if (fsm->validate) {
         fsm->validate = 0;
         list_for_each_entry(slave, &master->slaves, list) {
-            if (slave->online) continue;
+            if (slave->online_state == EC_SLAVE_ONLINE) continue;
 
             // At least one slave is offline. validate!
             EC_INFO("Validating bus.\n");
@@ -485,7 +485,6 @@
 {
     ec_slave_t *slave = fsm->slave;
     ec_datagram_t *datagram = fsm->datagram;
-    uint8_t new_state;
 
     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) {
         ec_master_queue_datagram(fsm->master, fsm->datagram);
@@ -501,39 +500,14 @@
 
     // did the slave not respond to its station address?
     if (datagram->working_counter != 1) {
-        if (slave->online) {
-            slave->online = 0;
-            if (slave->master->debug_level)
-                EC_DBG("Slave %i: offline.\n", slave->ring_position);
-        }
+        ec_slave_set_online_state(slave, EC_SLAVE_OFFLINE);
         ec_fsm_master_action_next_slave_state(fsm);
         return;
     }
 
     // slave responded
-    new_state = EC_READ_U8(datagram->data);
-    if (!slave->online) { // slave was offline before
-        slave->online = 1;
-        slave->error_flag = 0; // clear error flag
-        slave->current_state = new_state;
-        if (slave->master->debug_level) {
-            char cur_state[EC_STATE_STRING_SIZE];
-            ec_state_string(slave->current_state, cur_state);
-            EC_DBG("Slave %i: online (%s).\n",
-                   slave->ring_position, cur_state);
-        }
-    }
-    else if (new_state != slave->current_state) {
-        if (slave->master->debug_level) {
-            char old_state[EC_STATE_STRING_SIZE],
-                cur_state[EC_STATE_STRING_SIZE];
-            ec_state_string(slave->current_state, old_state);
-            ec_state_string(new_state, cur_state);
-            EC_DBG("Slave %i: %s -> %s.\n",
-                   slave->ring_position, old_state, cur_state);
-        }
-        slave->current_state = new_state;
-    }
+    ec_slave_set_state(slave, EC_READ_U8(datagram->data)); // set app state first
+    ec_slave_set_online_state(slave, EC_SLAVE_ONLINE);
 
     // check, if new slave state has to be acknowledged
     if (slave->current_state & EC_SLAVE_STATE_ACK_ERR && !slave->error_flag) {
@@ -614,7 +588,7 @@
 {
     ec_datagram_t *datagram = fsm->datagram;
 
-    while (fsm->slave->online) {
+    while (fsm->slave->online_state == EC_SLAVE_ONLINE) {
         if (fsm->slave->list.next == &fsm->master->slaves) { // last slave?
             fsm->state = ec_fsm_master_state_start;
             fsm->state(fsm); // execute immediately
--- a/master/master.c	Mon Mar 05 09:10:02 2007 +0000
+++ b/master/master.c	Mon Mar 05 09:55:39 2007 +0000
@@ -528,7 +528,7 @@
 
         // don't try to set PREOP for slaves that don't respond,
         // because of 3 second timeout.
-        if (!slave->online) {
+        if (slave->online_state == EC_SLAVE_OFFLINE) {
             if (master->debug_level)
                 EC_DBG("Skipping to configure offline slave %i.\n",
                         slave->ring_position);
--- a/master/slave.c	Mon Mar 05 09:10:02 2007 +0000
+++ b/master/slave.c	Mon Mar 05 09:55:39 2007 +0000
@@ -116,7 +116,7 @@
     slave->current_state = EC_SLAVE_STATE_UNKNOWN;
     slave->self_configured = 0;
     slave->error_flag = 0;
-    slave->online = 1;
+    slave->online_state = EC_SLAVE_ONLINE;
     slave->fmmu_count = 0;
 
     slave->coupler_index = 0;
@@ -329,6 +329,58 @@
 /*****************************************************************************/
 
 /**
+ * Sets the application state of a slave.
+ */
+
+void ec_slave_set_state(ec_slave_t *slave, /**< EtherCAT slave */
+        ec_slave_state_t new_state /**< new application state */
+        )
+{
+    if (new_state != slave->current_state) {
+        if (slave->master->debug_level) {
+            char old_state[EC_STATE_STRING_SIZE],
+                cur_state[EC_STATE_STRING_SIZE];
+            ec_state_string(slave->current_state, old_state);
+            ec_state_string(new_state, cur_state);
+            EC_DBG("Slave %i: %s -> %s.\n",
+                   slave->ring_position, old_state, cur_state);
+        }
+        slave->current_state = new_state;
+    }
+}
+
+/*****************************************************************************/
+
+/**
+ * Sets the online state of a slave.
+ */
+
+void ec_slave_set_online_state(ec_slave_t *slave, /**< EtherCAT slave */
+        ec_slave_online_state_t new_state /**< new online state */
+        )
+{
+    if (new_state == EC_SLAVE_OFFLINE &&
+            slave->online_state == EC_SLAVE_ONLINE) {
+        if (slave->master->debug_level)
+            EC_DBG("Slave %i: offline.\n", slave->ring_position);
+    }
+    else if (new_state == EC_SLAVE_ONLINE &&
+            slave->online_state == EC_SLAVE_OFFLINE) {
+        slave->error_flag = 0; // clear error flag
+        if (slave->master->debug_level) {
+            char cur_state[EC_STATE_STRING_SIZE];
+            ec_state_string(slave->current_state, cur_state);
+            EC_DBG("Slave %i: online (%s).\n",
+                   slave->ring_position, cur_state);
+        }
+    }
+
+    slave->online_state = new_state;
+}
+
+/*****************************************************************************/
+
+/**
  */
 
 void ec_slave_request_state(ec_slave_t *slave, /**< ETherCAT slave */
@@ -636,8 +688,8 @@
     off += sprintf(buffer + off, " (");
     off += ec_state_string(slave->requested_state, buffer + off);
     off += sprintf(buffer + off, ")\nFlags: %s, %s\n",
-                   slave->online ? "online" : "OFFLINE",
-                   slave->error_flag ? "ERROR" : "ok");
+            slave->online_state == EC_SLAVE_ONLINE ? "online" : "OFFLINE",
+            slave->error_flag ? "ERROR" : "ok");
     off += sprintf(buffer + off, "Ring position: %i\n",
                    slave->ring_position);
     off += sprintf(buffer + off, "Advanced position: %i:%i\n",
--- a/master/slave.h	Mon Mar 05 09:10:02 2007 +0000
+++ b/master/slave.h	Mon Mar 05 09:55:39 2007 +0000
@@ -72,6 +72,12 @@
 }
 ec_slave_state_t;
 
+typedef enum {
+    EC_SLAVE_OFFLINE,
+    EC_SLAVE_ONLINE
+}
+ec_slave_online_state_t;
+
 /*****************************************************************************/
 
 /**
@@ -195,11 +201,11 @@
     struct kobject kobj; /**< kobject */
     ec_master_t *master; /**< master owning the slave */
 
-    ec_slave_state_t requested_state; /**< requested slave state */
-    ec_slave_state_t current_state; /**< current slave state */
+    ec_slave_state_t requested_state; /**< requested application state */
+    ec_slave_state_t current_state; /**< current application state */
     unsigned int self_configured; /**< slave was configured by this master */
     unsigned int error_flag; /**< stop processing after an error */
-    unsigned int online; /**< non-zero, if the slave responds. */
+    unsigned int online_state; /**< online state */
 
     // addresses
     uint16_t ring_position; /**< ring position */
@@ -266,6 +272,8 @@
                           const ec_sii_sync_t *);
 
 void ec_slave_request_state(ec_slave_t *, ec_slave_state_t);
+void ec_slave_set_state(ec_slave_t *, ec_slave_state_t);
+void ec_slave_set_online_state(ec_slave_t *, ec_slave_online_state_t);
 
 // SII categories
 int ec_slave_fetch_strings(ec_slave_t *, const uint8_t *);