fp@238: /****************************************************************************** fp@238: * fp@238: * $Id$ fp@238: * fp@238: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@238: * fp@238: * This file is part of the IgH EtherCAT Master. fp@238: * fp@238: * The IgH EtherCAT Master is free software; you can redistribute it fp@238: * and/or modify it under the terms of the GNU General Public License fp@246: * as published by the Free Software Foundation; either version 2 of the fp@246: * License, or (at your option) any later version. fp@238: * fp@238: * The IgH EtherCAT Master is distributed in the hope that it will be fp@238: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@238: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@238: * GNU General Public License for more details. fp@238: * fp@238: * You should have received a copy of the GNU General Public License fp@238: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@238: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@238: * fp@246: * The right to use EtherCAT Technology is granted and comes free of fp@246: * charge under condition of compatibility of product made by fp@246: * Licensee. People intending to distribute/sell products based on the fp@246: * code, have to sign an agreement to guarantee that products using fp@246: * software based on IgH EtherCAT master stay compatible with the actual fp@246: * EtherCAT specification (which are released themselves as an open fp@246: * standard) as the (only) precondition to have the right to use EtherCAT fp@246: * Technology, IP and trade marks. fp@246: * fp@238: *****************************************************************************/ fp@238: fp@907: /** \file fp@907: * EtherCAT master state machine. fp@907: */ fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: #include "globals.h" fp@238: #include "master.h" fp@329: #include "mailbox.h" fp@858: #include "slave_config.h" fp@715: #ifdef EC_EOE fp@661: #include "ethernet.h" fp@715: #endif fp@849: fp@528: #include "fsm_master.h" fp@528: fp@528: /*****************************************************************************/ fp@528: fp@528: void ec_fsm_master_state_start(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_broadcast(ec_fsm_master_t *); fp@907: void ec_fsm_master_state_read_state(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_acknowledge(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_configure_slave(ec_fsm_master_t *); fp@717: void ec_fsm_master_state_clear_addresses(ec_fsm_master_t *); fp@907: void ec_fsm_master_state_scan_slave(ec_fsm_master_t *); fp@872: void ec_fsm_master_state_write_sii(ec_fsm_master_t *); fp@905: void ec_fsm_master_state_sdo_dictionary(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_sdo_request(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_end(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_error(ec_fsm_master_t *); fp@251: fp@238: /*****************************************************************************/ fp@238: fp@907: /** Constructor. fp@907: */ fp@907: void ec_fsm_master_init( fp@907: ec_fsm_master_t *fsm, /**< Master state machine. */ fp@907: ec_master_t *master, /**< EtherCAT master. */ fp@907: ec_datagram_t *datagram /**< Datagram object to use. */ fp@528: ) fp@238: { fp@238: fsm->master = master; fp@528: fsm->datagram = datagram; fp@528: fsm->state = ec_fsm_master_state_start; fp@650: fsm->idle = 0; fp@528: fsm->slaves_responding = 0; fp@553: fsm->topology_change_pending = 0; fp@528: fsm->slave_states = EC_SLAVE_STATE_UNKNOWN; fp@238: fp@436: // init sub-state-machines fp@830: ec_fsm_slave_config_init(&fsm->fsm_slave_config, fsm->datagram); fp@831: ec_fsm_slave_scan_init(&fsm->fsm_slave_scan, fsm->datagram, fp@831: &fsm->fsm_slave_config, &fsm->fsm_coe_map); fp@528: ec_fsm_sii_init(&fsm->fsm_sii, fsm->datagram); fp@528: ec_fsm_change_init(&fsm->fsm_change, fsm->datagram); fp@528: ec_fsm_coe_init(&fsm->fsm_coe, fsm->datagram); fp@741: ec_fsm_coe_map_init(&fsm->fsm_coe_map, &fsm->fsm_coe); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@907: /** Destructor. fp@907: */ fp@907: void ec_fsm_master_clear( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@238: { fp@436: // clear sub-state machines fp@830: ec_fsm_slave_config_clear(&fsm->fsm_slave_config); fp@830: ec_fsm_slave_scan_clear(&fsm->fsm_slave_scan); fp@433: ec_fsm_sii_clear(&fsm->fsm_sii); fp@434: ec_fsm_change_clear(&fsm->fsm_change); fp@436: ec_fsm_coe_clear(&fsm->fsm_coe); fp@741: ec_fsm_coe_map_clear(&fsm->fsm_coe_map); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@907: /** Executes the current state of the state machine. fp@907: * fp@907: * If the state machine's datagram is not sent or received yet, the execution fp@907: * of the state machine is delayed to the next cycle. fp@907: * fp@907: * \return false, if state machine has terminated fp@907: */ fp@907: int ec_fsm_master_exec( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@528: { fp@528: if (fsm->datagram->state == EC_DATAGRAM_SENT fp@528: || fsm->datagram->state == EC_DATAGRAM_QUEUED) { fp@505: // datagram was not sent or received yet. fp@528: return ec_fsm_master_running(fsm); fp@528: } fp@528: fp@528: fsm->state(fsm); fp@528: return ec_fsm_master_running(fsm); fp@456: } fp@456: fp@456: /*****************************************************************************/ fp@456: fp@456: /** fp@650: * \return false, if state machine has terminated fp@650: */ fp@650: int ec_fsm_master_running( fp@907: const ec_fsm_master_t *fsm /**< Master state machine. */ fp@650: ) fp@528: { fp@528: return fsm->state != ec_fsm_master_state_end fp@528: && fsm->state != ec_fsm_master_state_error; fp@446: } fp@446: fp@650: /*****************************************************************************/ fp@650: fp@650: /** fp@650: * \return true, if the state machine is in an idle phase fp@650: */ fp@650: int ec_fsm_master_idle( fp@907: const ec_fsm_master_t *fsm /**< Master state machine. */ fp@650: ) fp@650: { fp@650: return fsm->idle; fp@650: } fp@650: fp@238: /****************************************************************************** fp@907: * Master state machine fp@238: *****************************************************************************/ fp@238: fp@907: /** Master state: START. fp@907: * fp@907: * Starts with getting slave count and slave states. fp@907: */ fp@907: void ec_fsm_master_state_start( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@528: { fp@650: fsm->idle = 1; fp@528: ec_datagram_brd(fsm->datagram, 0x0130, 2); fp@528: fsm->state = ec_fsm_master_state_broadcast; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@907: /** Master state: BROADCAST. fp@907: * fp@907: * Processes the broadcast read slave count and slaves states. fp@907: */ fp@907: void ec_fsm_master_state_broadcast( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@528: { fp@528: ec_datagram_t *datagram = fsm->datagram; fp@553: unsigned int i; fp@238: ec_slave_t *slave; fp@260: ec_master_t *master = fsm->master; fp@238: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT) fp@637: return; // always retry fp@505: fp@553: // bus topology change? fp@553: if (datagram->working_counter != fsm->slaves_responding) { fp@553: fsm->topology_change_pending = 1; fp@553: fsm->slaves_responding = datagram->working_counter; fp@906: EC_INFO("%u slave(s) responding.\n", fsm->slaves_responding); fp@906: } fp@906: fp@906: if (datagram->state != EC_DATAGRAM_RECEIVED) { // link is down fp@906: fsm->state = ec_fsm_master_state_error; fp@906: return; fp@260: } fp@260: fp@553: // slave states changed? fp@553: if (EC_READ_U8(datagram->data) != fsm->slave_states) { fp@403: char states[EC_STATE_STRING_SIZE]; fp@553: fsm->slave_states = EC_READ_U8(datagram->data); fp@528: ec_state_string(fsm->slave_states, states); fp@325: EC_INFO("Slave states: %s.\n", states); fp@260: } fp@260: fp@656: if (fsm->topology_change_pending) { fp@656: down(&master->scan_sem); fp@656: if (!master->allow_scan) { fp@656: up(&master->scan_sem); fp@900: } else { fp@900: master->scan_busy = 1; fp@656: up(&master->scan_sem); fp@656: fp@656: // topology change when scan is allowed: fp@656: // clear all slaves and scan the bus fp@656: fsm->topology_change_pending = 0; fp@656: fsm->idle = 0; fp@656: fsm->scan_jiffies = jiffies; fp@656: fp@715: #ifdef EC_EOE fp@656: ec_master_eoe_stop(master); fp@661: ec_master_clear_eoe_handlers(master); fp@715: #endif fp@656: ec_master_destroy_slaves(master); fp@656: fp@656: master->slave_count = datagram->working_counter; fp@656: fp@656: if (!master->slave_count) { fp@656: // no slaves present -> finish state machine. fp@900: master->scan_busy = 0; fp@656: wake_up_interruptible(&master->scan_queue); fp@656: fsm->state = ec_fsm_master_state_end; fp@260: return; fp@260: } fp@260: fp@656: // init slaves fp@656: for (i = 0; i < master->slave_count; i++) { fp@656: if (!(slave = (ec_slave_t *) kmalloc(sizeof(ec_slave_t), fp@656: GFP_ATOMIC))) { fp@656: EC_ERR("Failed to allocate slave %i!\n", i); fp@656: ec_master_destroy_slaves(master); fp@900: master->scan_busy = 0; fp@656: wake_up_interruptible(&master->scan_queue); fp@656: fsm->state = ec_fsm_master_state_error; fp@656: return; fp@656: } fp@656: fp@656: if (ec_slave_init(slave, master, i, i + 1)) { fp@656: // freeing of "slave" already done fp@656: ec_master_destroy_slaves(master); fp@900: master->scan_busy = 0; fp@656: wake_up_interruptible(&master->scan_queue); fp@656: fsm->state = ec_fsm_master_state_error; fp@656: return; fp@656: } fp@656: fp@908: // do not force reconfiguration in operation mode to avoid fp@908: // unnecesssary process data interruptions fp@908: if (master->mode != EC_MASTER_MODE_OPERATION) fp@908: slave->force_config = 1; fp@908: fp@656: list_add_tail(&slave->list, &master->slaves); fp@260: } fp@260: fp@904: // broadcast clear all station addresses fp@717: ec_datagram_bwr(datagram, 0x0010, 2); fp@717: EC_WRITE_U16(datagram->data, 0x0000); fp@717: fsm->retries = EC_FSM_RETRIES; fp@717: fsm->state = ec_fsm_master_state_clear_addresses; fp@656: return; fp@656: } fp@260: } fp@260: fp@853: if (list_empty(&master->slaves)) { fp@853: fsm->state = ec_fsm_master_state_end; fp@853: } else { fp@907: // fetch state from first slave fp@853: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@907: ec_datagram_fprd(fsm->datagram, fsm->slave->station_address, fp@907: 0x0130, 2); fp@853: fsm->retries = EC_FSM_RETRIES; fp@907: fsm->state = ec_fsm_master_state_read_state; fp@907: } fp@907: } fp@907: fp@907: /*****************************************************************************/ fp@907: fp@907: /** Check for pending SII write requests and process one. fp@907: * fp@872: * \return non-zero, if an SII write request is processed. fp@601: */ fp@872: int ec_fsm_master_action_process_sii( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@601: ) fp@601: { fp@601: ec_master_t *master = fsm->master; fp@872: ec_sii_write_request_t *request; fp@601: fp@604: // search the first request to be processed fp@604: while (1) { fp@872: down(&master->sii_sem); fp@872: if (list_empty(&master->sii_requests)) { fp@872: up(&master->sii_sem); fp@604: break; fp@604: } fp@604: // get first request fp@872: request = list_entry(master->sii_requests.next, fp@872: ec_sii_write_request_t, list); fp@601: list_del_init(&request->list); // dequeue fp@861: request->state = EC_REQUEST_BUSY; fp@872: up(&master->sii_sem); fp@601: fp@872: // found pending SII write operation. execute it! fp@607: if (master->debug_level) fp@906: EC_DBG("Writing SII data to slave %u...\n", fp@906: request->slave->ring_position); fp@872: fsm->sii_request = request; fp@872: fsm->sii_index = 0; fp@754: ec_fsm_sii_write(&fsm->fsm_sii, request->slave, request->word_offset, fp@815: request->data, EC_FSM_SII_USE_CONFIGURED_ADDRESS); fp@872: fsm->state = ec_fsm_master_state_write_sii; fp@601: fsm->state(fsm); // execute immediately fp@601: return 1; fp@601: } fp@601: fp@601: return 0; fp@601: } fp@601: fp@601: /*****************************************************************************/ fp@601: fp@907: /** Check for pending Sdo requests and process one. fp@907: * fp@814: * \return non-zero, if an Sdo request is processed. fp@646: */ fp@646: int ec_fsm_master_action_process_sdo( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@646: ) fp@646: { fp@646: ec_master_t *master = fsm->master; fp@849: ec_master_sdo_request_t *request; fp@858: ec_sdo_request_t *req; fp@646: ec_slave_t *slave; fp@646: fp@858: // search for internal requests to be processed fp@858: list_for_each_entry(slave, &master->slaves, list) { fp@858: if (!slave->config) fp@858: continue; fp@858: list_for_each_entry(req, &slave->config->sdo_requests, list) { fp@858: if (req->state == EC_REQUEST_QUEUED) { fp@880: fp@880: if (ec_sdo_request_timed_out(req)) { fp@880: req->state = EC_REQUEST_FAILURE; fp@880: if (master->debug_level) fp@880: EC_DBG("Sdo request for slave %u timed out...\n", fp@880: slave->ring_position); fp@880: continue; fp@880: } fp@858: fp@858: if (slave->current_state == EC_SLAVE_STATE_INIT || fp@858: slave->error_flag) { fp@858: req->state = EC_REQUEST_FAILURE; fp@858: continue; fp@858: } fp@858: fp@880: req->state = EC_REQUEST_BUSY; fp@858: if (master->debug_level) fp@858: EC_DBG("Processing Sdo request for slave %u...\n", fp@858: slave->ring_position); fp@858: fp@858: fsm->idle = 0; fp@858: fsm->sdo_request = req; fp@858: fsm->slave = slave; fp@858: fsm->state = ec_fsm_master_state_sdo_request; fp@859: ec_fsm_coe_transfer(&fsm->fsm_coe, slave, req); fp@858: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@858: return 1; fp@858: } fp@858: } fp@858: } fp@858: fp@858: // search the first external request to be processed fp@646: while (1) { fp@646: down(&master->sdo_sem); fp@849: if (list_empty(&master->slave_sdo_requests)) { fp@646: up(&master->sdo_sem); fp@646: break; fp@646: } fp@646: // get first request fp@849: request = list_entry(master->slave_sdo_requests.next, fp@849: ec_master_sdo_request_t, list); fp@646: list_del_init(&request->list); // dequeue fp@861: request->req.state = EC_REQUEST_BUSY; fp@646: up(&master->sdo_sem); fp@646: fp@831: slave = request->slave; fp@646: if (slave->current_state == EC_SLAVE_STATE_INIT || fp@646: slave->error_flag) { fp@849: EC_ERR("Discarding Sdo request, slave %u not ready.\n", fp@646: slave->ring_position); fp@849: request->req.state = EC_REQUEST_FAILURE; fp@646: wake_up(&master->sdo_queue); fp@646: continue; fp@646: } fp@646: fp@849: // Found pending Sdo request. Execute it! fp@646: if (master->debug_level) fp@906: EC_DBG("Processing Sdo request for slave %u...\n", fp@646: slave->ring_position); fp@646: fp@849: // Start uploading Sdo fp@650: fsm->idle = 0; fp@858: fsm->sdo_request = &request->req; fp@858: fsm->slave = slave; fp@646: fsm->state = ec_fsm_master_state_sdo_request; fp@859: ec_fsm_coe_transfer(&fsm->fsm_coe, slave, &request->req); fp@646: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@646: return 1; fp@646: } fp@646: fp@646: return 0; fp@646: } fp@646: fp@646: /*****************************************************************************/ fp@646: fp@907: /** Master action: IDLE. fp@907: * fp@907: * Does secondary work. fp@907: */ fp@907: void ec_fsm_master_action_idle( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@907: { fp@907: ec_master_t *master = fsm->master; fp@656: ec_slave_t *slave; fp@304: fp@858: // Check for pending Sdo requests fp@646: if (ec_fsm_master_action_process_sdo(fsm)) fp@646: return; fp@646: fp@442: if (master->mode == EC_MASTER_MODE_IDLE) { fp@442: fp@814: // check, if slaves have an Sdo dictionary to read out. fp@442: list_for_each_entry(slave, &master->slaves, list) { fp@834: if (!(slave->sii.mailbox_protocols & EC_MBOX_COE) fp@442: || slave->sdo_dictionary_fetched fp@442: || slave->current_state == EC_SLAVE_STATE_INIT fp@442: || jiffies - slave->jiffies_preop < EC_WAIT_SDO_DICT * HZ fp@442: || slave->error_flag) continue; fp@442: fp@442: if (master->debug_level) { fp@905: EC_DBG("Fetching Sdo dictionary from slave %u.\n", fp@442: slave->ring_position); fp@442: } fp@442: fp@442: slave->sdo_dictionary_fetched = 1; fp@442: fp@814: // start fetching Sdo dictionary fp@650: fsm->idle = 0; fp@441: fsm->slave = slave; fp@905: fsm->state = ec_fsm_master_state_sdo_dictionary; fp@442: ec_fsm_coe_dictionary(&fsm->fsm_coe, slave); fp@441: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@441: return; fp@441: } fp@442: fp@872: // check for pending SII write operations. fp@872: if (ec_fsm_master_action_process_sii(fsm)) fp@872: return; // SII write request found fp@304: } fp@304: fp@528: fsm->state = ec_fsm_master_state_end; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@907: /** Master action: Get state of next slave. fp@907: */ fp@907: void ec_fsm_master_action_next_slave_state( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@260: { fp@260: ec_master_t *master = fsm->master; fp@260: ec_slave_t *slave = fsm->slave; fp@260: fp@304: // is there another slave to query? fp@292: if (slave->list.next != &master->slaves) { fp@907: // fetch state from next slave fp@650: fsm->idle = 1; fp@637: fsm->slave = list_entry(slave->list.next, ec_slave_t, list); fp@815: ec_datagram_fprd(fsm->datagram, fsm->slave->station_address, fp@293: 0x0130, 2); fp@505: fsm->retries = EC_FSM_RETRIES; fp@907: fsm->state = ec_fsm_master_state_read_state; fp@907: return; fp@907: } fp@907: fp@907: // all slaves processed fp@907: ec_fsm_master_action_idle(fsm); fp@907: } fp@907: fp@907: /*****************************************************************************/ fp@907: fp@907: /** Master action: Configure. fp@907: */ fp@907: void ec_fsm_master_action_configure( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@907: { fp@907: ec_master_t *master = fsm->master; fp@907: ec_slave_t *slave = fsm->slave; fp@907: fp@907: // Does the slave have to be configured? fp@907: if (!slave->error_flag fp@907: && (slave->current_state != slave->requested_state fp@908: || slave->force_config)) { fp@907: // Start slave configuration, if it is allowed. fp@907: down(&master->config_sem); fp@907: if (!master->allow_config) { fp@907: up(&master->config_sem); fp@907: } else { fp@907: master->config_busy = 1; fp@907: up(&master->config_sem); fp@907: fp@907: if (master->debug_level) { fp@908: char old_state[EC_STATE_STRING_SIZE], fp@908: new_state[EC_STATE_STRING_SIZE]; fp@907: ec_state_string(slave->current_state, old_state); fp@908: ec_state_string(slave->requested_state, new_state); fp@908: EC_DBG("Changing state of slave %u from %s to %s%s.\n", fp@908: slave->ring_position, old_state, new_state, fp@908: slave->force_config ? " (forced)" : ""); fp@907: } fp@907: fp@908: // configuration will be done immediately; therefore reset the fp@908: // force flag fp@908: slave->force_config = 0; fp@908: fp@907: fsm->idle = 0; fp@907: fsm->state = ec_fsm_master_state_configure_slave; fp@907: ec_fsm_slave_config_start(&fsm->fsm_slave_config, slave); fp@907: fsm->state(fsm); // execute immediately fp@907: return; fp@907: } fp@907: } fp@907: fp@907: // slave has error flag set; process next one fp@907: ec_fsm_master_action_next_slave_state(fsm); fp@907: } fp@907: fp@907: /*****************************************************************************/ fp@907: fp@907: /** Master action: Acknowledge. fp@907: */ fp@907: void ec_fsm_master_action_acknowledge( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@907: { fp@907: ec_slave_t *slave = fsm->slave; fp@907: fp@907: if (!slave->error_flag) { fp@907: // Check, if new slave state has to be acknowledged fp@907: if (slave->current_state & EC_SLAVE_STATE_ACK_ERR) { fp@907: fsm->idle = 0; fp@907: fsm->state = ec_fsm_master_state_acknowledge; fp@907: ec_fsm_change_ack(&fsm->fsm_change, slave); fp@907: fsm->state(fsm); // execute immediately fp@907: return; fp@907: } fp@907: fp@907: // No acknowlegde necessary; check for configuration fp@907: ec_fsm_master_action_configure(fsm); fp@907: return; fp@907: } fp@907: fp@907: // slave has error flag set; process next one fp@907: ec_fsm_master_action_next_slave_state(fsm); fp@907: } fp@907: fp@907: /*****************************************************************************/ fp@907: fp@907: /** Master state: READ STATE. fp@907: * fp@907: * Fetches the AL state of a slave. fp@907: */ fp@907: void ec_fsm_master_state_read_state( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@528: ec_datagram_t *datagram = fsm->datagram; fp@260: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@907: EC_ERR("Failed to receive AL state datagram for slave %u" fp@713: " (datagram state %i)\n", fp@713: slave->ring_position, datagram->state); fp@528: fsm->state = ec_fsm_master_state_error; fp@238: return; fp@238: } fp@238: fp@260: // did the slave not respond to its station address? fp@906: if (datagram->working_counter != 1) { fp@906: if (!slave->error_flag) { fp@906: slave->error_flag = 1; fp@906: if (fsm->master->debug_level) fp@906: EC_DBG("Slave %u did not respond to state query.\n", fp@906: fsm->slave->ring_position); fp@906: } fp@906: fsm->topology_change_pending = 1; fp@906: fsm->state = ec_fsm_master_state_error; fp@906: return; fp@906: } fp@906: fp@907: // A single slave responded fp@907: ec_slave_set_state(slave, EC_READ_U8(datagram->data)); fp@907: ec_fsm_master_action_acknowledge(fsm); fp@907: } fp@907: fp@907: /*****************************************************************************/ fp@907: fp@907: /** Master state: ACKNOWLEDGE. fp@907: */ fp@907: void ec_fsm_master_state_acknowledge( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@454: { fp@454: ec_slave_t *slave = fsm->slave; fp@454: fp@907: if (ec_fsm_change_exec(&fsm->fsm_change)) fp@907: return; fp@454: fp@454: if (!ec_fsm_change_success(&fsm->fsm_change)) { fp@454: fsm->slave->error_flag = 1; fp@907: EC_ERR("Failed to acknowledge state change on slave %u.\n", fp@454: slave->ring_position); fp@907: } fp@907: fp@907: ec_fsm_master_action_configure(fsm); fp@907: } fp@907: fp@907: /*****************************************************************************/ fp@907: fp@907: /** Master state: CLEAR ADDRESSES. fp@907: */ fp@717: void ec_fsm_master_state_clear_addresses( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@717: ) fp@717: { fp@717: ec_master_t *master = fsm->master; fp@717: ec_datagram_t *datagram = fsm->datagram; fp@717: fp@717: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@717: return; fp@717: fp@717: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@717: EC_ERR("Failed to receive address clearing datagram (state %i).\n", fp@717: datagram->state); fp@900: master->scan_busy = 0; fp@794: wake_up_interruptible(&master->scan_queue); fp@717: fsm->state = ec_fsm_master_state_error; fp@717: return; fp@717: } fp@717: fp@717: if (datagram->working_counter != master->slave_count) { fp@717: EC_WARN("Failed to clear all station addresses: Cleared %u of %u", fp@717: datagram->working_counter, master->slave_count); fp@717: } fp@717: fp@717: EC_INFO("Scanning bus.\n"); fp@717: fp@717: // begin scanning of slaves fp@717: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@907: fsm->state = ec_fsm_master_state_scan_slave; fp@830: ec_fsm_slave_scan_start(&fsm->fsm_slave_scan, fsm->slave); fp@830: ec_fsm_slave_scan_exec(&fsm->fsm_slave_scan); // execute immediately fp@717: } fp@717: fp@717: /*****************************************************************************/ fp@717: fp@907: /** Master state: SCAN SLAVE. fp@907: * fp@652: * Executes the sub-statemachine for the scanning of a slave. fp@652: */ fp@907: void ec_fsm_master_state_scan_slave( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@652: ) fp@238: { fp@238: ec_master_t *master = fsm->master; fp@637: ec_slave_t *slave = fsm->slave; fp@325: fp@907: if (ec_fsm_slave_scan_exec(&fsm->fsm_slave_scan)) fp@528: return; fp@325: fp@715: #ifdef EC_EOE fp@834: if (slave->sii.mailbox_protocols & EC_MBOX_EOE) { fp@661: // create EoE handler for this slave fp@661: ec_eoe_t *eoe; fp@661: if (!(eoe = kmalloc(sizeof(ec_eoe_t), GFP_KERNEL))) { fp@661: EC_ERR("Failed to allocate EoE handler memory for slave %u!\n", fp@661: slave->ring_position); fp@900: } else if (ec_eoe_init(eoe, slave)) { fp@661: EC_ERR("Failed to init EoE handler for slave %u!\n", fp@661: slave->ring_position); fp@661: kfree(eoe); fp@900: } else { fp@661: list_add_tail(&eoe->list, &master->eoe_handlers); fp@661: } fp@661: } fp@715: #endif fp@661: fp@325: // another slave to fetch? fp@637: if (slave->list.next != &master->slaves) { fp@637: fsm->slave = list_entry(slave->list.next, ec_slave_t, list); fp@830: ec_fsm_slave_scan_start(&fsm->fsm_slave_scan, fsm->slave); fp@830: ec_fsm_slave_scan_exec(&fsm->fsm_slave_scan); // execute immediately fp@325: return; fp@325: } fp@325: fp@652: EC_INFO("Bus scanning completed in %u ms.\n", fp@652: (u32) (jiffies - fsm->scan_jiffies) * 1000 / HZ); fp@661: fp@900: master->scan_busy = 0; fp@900: wake_up_interruptible(&master->scan_queue); fp@900: fp@792: // Attach slave configurations fp@792: ec_master_attach_slave_configs(master); fp@792: fp@716: #ifdef EC_EOE fp@661: // check if EoE processing has to be started fp@661: ec_master_eoe_start(master); fp@715: #endif fp@661: fp@528: fsm->state = ec_fsm_master_state_end; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@907: /** Master state: CONFIGURE SLAVE. fp@907: * fp@907: * Starts configuring a slave. fp@907: */ fp@907: void ec_fsm_master_state_configure_slave( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@907: { fp@907: ec_master_t *master = fsm->master; fp@907: fp@907: if (ec_fsm_slave_config_exec(&fsm->fsm_slave_config)) fp@907: return; fp@907: fp@907: // configuration finished fp@907: master->config_busy = 0; fp@907: wake_up_interruptible(&master->config_queue); fp@830: fp@900: if (!ec_fsm_slave_config_success(&fsm->fsm_slave_config)) { fp@900: // TODO: mark slave_config as failed. fp@900: } fp@656: fp@907: fsm->idle = 1; fp@907: ec_fsm_master_action_next_slave_state(fsm); fp@907: } fp@907: fp@907: /*****************************************************************************/ fp@907: fp@907: /** Master state: WRITE SII. fp@907: */ fp@872: void ec_fsm_master_state_write_sii( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@269: { fp@601: ec_master_t *master = fsm->master; fp@872: ec_sii_write_request_t *request = fsm->sii_request; fp@601: ec_slave_t *slave = request->slave; fp@269: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@601: slave->error_flag = 1; fp@872: EC_ERR("Failed to write SII data to slave %i.\n", fp@607: slave->ring_position); fp@649: request->state = EC_REQUEST_FAILURE; fp@872: wake_up(&master->sii_queue); fp@872: fsm->state = ec_fsm_master_state_error; fp@872: return; fp@872: } fp@872: fp@872: fsm->sii_index++; fp@872: if (fsm->sii_index < request->word_size) { fp@601: ec_fsm_sii_write(&fsm->fsm_sii, slave, fp@872: request->word_offset + fsm->sii_index, fp@872: request->data + fsm->sii_index * 2, fp@815: EC_FSM_SII_USE_CONFIGURED_ADDRESS); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@269: return; fp@269: } fp@269: fp@872: // finished writing SII fp@607: if (master->debug_level) fp@872: EC_DBG("Finished writing %u words of SII data to slave %u.\n", fp@754: request->word_size, slave->ring_position); fp@861: request->state = EC_REQUEST_SUCCESS; fp@872: wake_up(&master->sii_queue); fp@872: fp@872: // TODO: Evaluate new SII contents! fp@872: fp@872: // check for another SII write request fp@872: if (ec_fsm_master_action_process_sii(fsm)) fp@601: return; // processing another request fp@601: fp@637: fsm->state = ec_fsm_master_state_end; fp@419: } fp@419: fp@419: /*****************************************************************************/ fp@419: fp@907: /** Master state: SDO DICTIONARY. fp@907: */ fp@905: void ec_fsm_master_state_sdo_dictionary( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@905: ) fp@419: { fp@419: ec_slave_t *slave = fsm->slave; fp@419: ec_master_t *master = fsm->master; fp@419: fp@436: if (ec_fsm_coe_exec(&fsm->fsm_coe)) return; fp@436: fp@436: if (!ec_fsm_coe_success(&fsm->fsm_coe)) { fp@528: fsm->state = ec_fsm_master_state_error; fp@419: return; fp@419: } fp@419: fp@814: // Sdo dictionary fetching finished fp@419: fp@419: if (master->debug_level) { fp@423: unsigned int sdo_count, entry_count; fp@423: ec_slave_sdo_dict_info(slave, &sdo_count, &entry_count); fp@814: EC_DBG("Fetched %i Sdos and %i entries from slave %i.\n", fp@423: sdo_count, entry_count, slave->ring_position); fp@419: } fp@419: fp@637: fsm->state = ec_fsm_master_state_end; fp@269: } fp@269: fp@430: /*****************************************************************************/ fp@430: fp@907: /** Master state: SDO REQUEST. fp@907: */ fp@907: void ec_fsm_master_state_sdo_request( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@430: { fp@430: ec_master_t *master = fsm->master; fp@858: ec_sdo_request_t *request = fsm->sdo_request; fp@436: fp@436: if (ec_fsm_coe_exec(&fsm->fsm_coe)) return; fp@436: fp@436: if (!ec_fsm_coe_success(&fsm->fsm_coe)) { fp@858: EC_DBG("Failed to process Sdo request for slave %u.\n", fp@646: fsm->slave->ring_position); fp@858: request->state = EC_REQUEST_FAILURE; fp@646: wake_up(&master->sdo_queue); fp@646: fsm->state = ec_fsm_master_state_error; fp@646: return; fp@646: } fp@646: fp@814: // Sdo request finished fp@861: request->state = EC_REQUEST_SUCCESS; fp@646: wake_up(&master->sdo_queue); fp@646: fp@646: if (master->debug_level) fp@858: EC_DBG("Finished Sdo request for slave %u.\n", fp@646: fsm->slave->ring_position); fp@646: fp@814: // check for another Sdo request fp@646: if (ec_fsm_master_action_process_sdo(fsm)) fp@646: return; // processing another request fp@430: fp@637: fsm->state = ec_fsm_master_state_end; fp@430: } fp@430: fp@446: /*****************************************************************************/ fp@446: fp@907: /** State: ERROR. fp@907: */ fp@528: void ec_fsm_master_state_error( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@528: ) fp@528: { fp@528: fsm->state = ec_fsm_master_state_start; fp@446: } fp@446: fp@446: /*****************************************************************************/ fp@446: fp@907: /** State: END. fp@907: */ fp@907: void ec_fsm_master_state_end( fp@907: ec_fsm_master_t *fsm /**< Master state machine. */ fp@907: ) fp@528: { fp@528: fsm->state = ec_fsm_master_state_start; fp@528: } fp@528: fp@528: /*****************************************************************************/