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@238: /** fp@238: \file fp@238: EtherCAT finite state machines. fp@238: */ fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: #include "globals.h" fp@238: #include "fsm.h" fp@238: #include "master.h" fp@329: #include "mailbox.h" fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: void ec_fsm_master_start(ec_fsm_t *); fp@260: void ec_fsm_master_broadcast(ec_fsm_t *); fp@304: void ec_fsm_master_read_states(ec_fsm_t *); fp@454: void ec_fsm_master_acknowledge(ec_fsm_t *); fp@260: void ec_fsm_master_validate_vendor(ec_fsm_t *); fp@260: void ec_fsm_master_validate_product(ec_fsm_t *); fp@304: void ec_fsm_master_rewrite_addresses(ec_fsm_t *); fp@304: void ec_fsm_master_configure_slave(ec_fsm_t *); fp@304: void ec_fsm_master_scan_slaves(ec_fsm_t *); fp@304: void ec_fsm_master_write_eeprom(ec_fsm_t *); fp@419: void ec_fsm_master_sdodict(ec_fsm_t *); fp@430: void ec_fsm_master_sdo_request(ec_fsm_t *); fp@446: void ec_fsm_master_end(ec_fsm_t *); fp@446: void ec_fsm_master_error(ec_fsm_t *); fp@325: fp@308: void ec_fsm_slavescan_start(ec_fsm_t *); fp@310: void ec_fsm_slavescan_address(ec_fsm_t *); fp@308: void ec_fsm_slavescan_state(ec_fsm_t *); fp@308: void ec_fsm_slavescan_base(ec_fsm_t *); fp@308: void ec_fsm_slavescan_datalink(ec_fsm_t *); fp@308: void ec_fsm_slavescan_eeprom_size(ec_fsm_t *); fp@308: void ec_fsm_slavescan_eeprom_data(ec_fsm_t *); fp@308: fp@462: void ec_fsm_slaveconf_state_start(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_init(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_clear_fmmus(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_sync(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_preop(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_fmmu(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_sdoconf(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_saveop(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_state_op(ec_fsm_t *); fp@462: fp@462: void ec_fsm_slaveconf_enter_sync(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_enter_preop(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_enter_sdoconf(ec_fsm_t *); fp@462: void ec_fsm_slaveconf_enter_saveop(ec_fsm_t *); fp@462: fp@462: void ec_fsm_slave_state_end(ec_fsm_t *); fp@462: void ec_fsm_slave_state_error(ec_fsm_t *); fp@251: fp@238: /*****************************************************************************/ fp@238: fp@260: /** fp@260: Constructor. fp@260: */ fp@260: fp@260: int ec_fsm_init(ec_fsm_t *fsm, /**< finite state machine */ fp@260: ec_master_t *master /**< EtherCAT master */ fp@325: ) fp@238: { fp@238: fsm->master = master; fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_slaves_responding = 0; fp@238: fsm->master_slave_states = EC_SLAVE_STATE_UNKNOWN; fp@260: fsm->master_validation = 0; fp@238: fp@293: ec_datagram_init(&fsm->datagram); fp@293: if (ec_datagram_prealloc(&fsm->datagram, EC_MAX_DATA_SIZE)) { fp@293: EC_ERR("Failed to allocate FSM datagram.\n"); fp@238: return -1; fp@238: } fp@238: fp@436: // init sub-state-machines fp@433: ec_fsm_sii_init(&fsm->fsm_sii, &fsm->datagram); fp@434: ec_fsm_change_init(&fsm->fsm_change, &fsm->datagram); fp@436: ec_fsm_coe_init(&fsm->fsm_coe, &fsm->datagram); fp@433: fp@238: return 0; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@260: /** fp@260: Destructor. fp@260: */ fp@260: fp@260: void ec_fsm_clear(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@436: // clear sub-state machines 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@433: fp@293: ec_datagram_clear(&fsm->datagram); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@260: /** fp@260: Executes the current state of the state machine. fp@435: \return false, if state machine has terminated fp@435: */ fp@435: fp@435: int ec_fsm_exec(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@238: fsm->master_state(fsm); fp@435: fp@456: return ec_fsm_running(fsm); fp@456: } fp@456: fp@456: /*****************************************************************************/ fp@456: fp@456: /** fp@456: \return false, if state machine has terminated fp@456: */ fp@456: fp@456: int ec_fsm_running(ec_fsm_t *fsm /**< finite state machine */) fp@456: { fp@446: return fsm->master_state != ec_fsm_master_end fp@446: && fsm->master_state != ec_fsm_master_error; fp@446: } fp@446: fp@446: /*****************************************************************************/ fp@446: fp@446: /** fp@446: \return true, if the master state machine terminated gracefully fp@446: */ fp@446: fp@446: int ec_fsm_success(ec_fsm_t *fsm /**< finite state machine */) fp@446: { fp@446: return fsm->master_state == ec_fsm_master_end; fp@325: } fp@325: fp@238: /****************************************************************************** fp@446: * operation/idle state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@260: Master state: START. fp@238: Starts with getting slave count and slave states. fp@238: */ fp@238: fp@238: void ec_fsm_master_start(ec_fsm_t *fsm) fp@238: { fp@293: ec_datagram_brd(&fsm->datagram, 0x0130, 2); fp@293: ec_master_queue_datagram(fsm->master, &fsm->datagram); fp@260: fsm->master_state = ec_fsm_master_broadcast; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: BROADCAST. fp@260: Processes the broadcast read slave count and slaves states. fp@260: */ fp@260: fp@260: void ec_fsm_master_broadcast(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@260: unsigned int topology_change, states_change, i; fp@238: ec_slave_t *slave; fp@260: ec_master_t *master = fsm->master; fp@238: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@260: if (!master->device->link_state) { fp@254: fsm->master_slaves_responding = 0; fp@260: list_for_each_entry(slave, &master->slaves, list) { fp@260: slave->online = 0; fp@260: } fp@260: } fp@446: else { fp@446: EC_ERR("Failed to receive broadcast datagram.\n"); fp@446: } fp@446: fsm->master_state = ec_fsm_master_error; fp@238: return; fp@238: } fp@238: fp@293: topology_change = (datagram->working_counter != fp@260: fsm->master_slaves_responding); fp@293: states_change = (EC_READ_U8(datagram->data) != fsm->master_slave_states); fp@293: fp@293: fsm->master_slave_states = EC_READ_U8(datagram->data); fp@293: fsm->master_slaves_responding = datagram->working_counter; fp@260: fp@260: if (topology_change) { fp@262: EC_INFO("%i slave%s responding.\n", fp@262: fsm->master_slaves_responding, fp@262: fsm->master_slaves_responding == 1 ? "" : "s"); fp@262: fp@325: if (master->mode == EC_MASTER_MODE_OPERATION) { fp@262: if (fsm->master_slaves_responding == master->slave_count) { fp@262: fsm->master_validation = 1; // start validation later fp@262: } fp@262: else { fp@262: EC_WARN("Invalid slave count. Bus in tainted state.\n"); fp@262: } fp@260: } fp@260: } fp@260: fp@260: if (states_change) { fp@403: char states[EC_STATE_STRING_SIZE]; fp@325: ec_state_string(fsm->master_slave_states, states); fp@325: EC_INFO("Slave states: %s.\n", states); fp@260: } fp@260: fp@306: // topology change in idle mode: clear all slaves and scan the bus fp@306: if (topology_change && master->mode == EC_MASTER_MODE_IDLE) { fp@260: fp@260: ec_master_eoe_stop(master); fp@448: ec_master_destroy_slaves(master); fp@260: fp@325: master->slave_count = datagram->working_counter; fp@325: fp@325: if (!master->slave_count) { fp@260: // no slaves present -> finish state machine. fp@446: fsm->master_state = ec_fsm_master_end; fp@251: return; fp@251: } fp@251: fp@260: // init slaves fp@325: for (i = 0; i < master->slave_count; i++) { fp@260: if (!(slave = (ec_slave_t *) kmalloc(sizeof(ec_slave_t), fp@260: GFP_ATOMIC))) { fp@260: EC_ERR("Failed to allocate slave %i!\n", i); fp@448: ec_master_destroy_slaves(master); fp@446: fsm->master_state = ec_fsm_master_error; fp@260: return; fp@260: } fp@260: fp@260: if (ec_slave_init(slave, master, i, i + 1)) { fp@325: // freeing of "slave" already done fp@448: ec_master_destroy_slaves(master); fp@446: fsm->master_state = ec_fsm_master_error; fp@260: return; fp@260: } fp@260: fp@260: if (kobject_add(&slave->kobj)) { fp@260: EC_ERR("Failed to add kobject.\n"); fp@260: kobject_put(&slave->kobj); // free fp@448: ec_master_destroy_slaves(master); fp@446: fsm->master_state = ec_fsm_master_error; fp@260: return; fp@260: } fp@260: fp@260: list_add_tail(&slave->list, &master->slaves); fp@260: } fp@260: fp@446: EC_INFO("Scanning bus.\n"); fp@446: fp@260: // begin scanning of slaves fp@260: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@308: fsm->slave_state = ec_fsm_slavescan_start; fp@304: fsm->master_state = ec_fsm_master_scan_slaves; fp@260: fsm->master_state(fsm); // execute immediately fp@260: return; fp@260: } fp@260: fp@260: // fetch state from each slave fp@260: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@293: ec_datagram_nprd(&fsm->datagram, fsm->slave->station_address, 0x0130, 2); fp@293: ec_master_queue_datagram(master, &fsm->datagram); fp@304: fsm->master_state = ec_fsm_master_read_states; fp@304: } fp@304: fp@304: /*****************************************************************************/ fp@304: fp@304: /** fp@304: Master action: PROC_STATES. fp@304: Processes the slave states. fp@304: */ fp@304: fp@304: void ec_fsm_master_action_process_states(ec_fsm_t *fsm fp@304: /**< finite state machine */ fp@304: ) fp@304: { fp@304: ec_master_t *master = fsm->master; fp@304: ec_slave_t *slave; fp@403: char old_state[EC_STATE_STRING_SIZE], new_state[EC_STATE_STRING_SIZE]; fp@304: fp@304: // check if any slaves are not in the state, they're supposed to be fp@304: list_for_each_entry(slave, &master->slaves, list) { fp@421: if (slave->error_flag fp@421: || !slave->online fp@421: || slave->requested_state == EC_SLAVE_STATE_UNKNOWN fp@421: || (slave->current_state == slave->requested_state fp@456: && slave->configured)) continue; fp@421: fp@446: if (master->debug_level) { fp@446: ec_state_string(slave->current_state, old_state); fp@456: if (slave->current_state != slave->requested_state) { fp@456: ec_state_string(slave->requested_state, new_state); fp@456: EC_DBG("Changing state of slave %i (%s -> %s).\n", fp@456: slave->ring_position, old_state, new_state); fp@446: } fp@456: else if (!slave->configured) { fp@456: EC_DBG("Reconfiguring slave %i (%s).\n", fp@456: slave->ring_position, old_state); fp@446: } fp@421: } fp@304: fp@456: fsm->master_state = ec_fsm_master_configure_slave; fp@304: fsm->slave = slave; fp@462: fsm->slave_state = ec_fsm_slaveconf_state_start; fp@456: fsm->slave_state(fsm); // execute immediately fp@304: return; fp@304: } fp@304: fp@325: // Check, if EoE processing has to be started fp@325: ec_master_eoe_start(master); fp@325: fp@442: if (master->mode == EC_MASTER_MODE_IDLE) { fp@442: fp@442: // Check for a pending SDO request fp@442: if (master->sdo_seq_master != master->sdo_seq_user) { fp@442: if (master->debug_level) fp@442: EC_DBG("Processing SDO request...\n"); fp@442: slave = master->sdo_request->sdo->slave; fp@442: if (slave->current_state == EC_SLAVE_STATE_INIT fp@475: || !slave->online) { fp@442: EC_ERR("Failed to process SDO request, slave %i not ready.\n", fp@442: slave->ring_position); fp@442: master->sdo_request->return_code = -1; fp@442: master->sdo_seq_master++; fp@442: } fp@442: else { fp@442: // start uploading SDO fp@442: fsm->slave = slave; fp@442: fsm->master_state = ec_fsm_master_sdo_request; fp@442: fsm->sdo_request = master->sdo_request; fp@442: ec_fsm_coe_upload(&fsm->fsm_coe, slave, fsm->sdo_request); fp@442: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@442: return; fp@442: } fp@442: } fp@442: fp@442: // check, if slaves have an SDO dictionary to read out. fp@442: list_for_each_entry(slave, &master->slaves, list) { fp@442: 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->online fp@442: || slave->error_flag) continue; fp@442: fp@442: if (master->debug_level) { fp@442: EC_DBG("Fetching SDO dictionary from slave %i.\n", fp@442: slave->ring_position); fp@442: } fp@442: fp@442: if (kobject_add(&slave->sdo_kobj)) { fp@442: EC_ERR("Failed to add SDO kobj of slave %i.\n", fp@442: slave->ring_position); fp@442: slave->error_flag = 1; fp@446: fsm->master_state = ec_fsm_master_error; fp@442: return; fp@442: } fp@442: fp@442: slave->sdo_dictionary_fetched = 1; fp@442: fp@442: // start fetching SDO dictionary fp@441: fsm->slave = slave; fp@442: fsm->master_state = ec_fsm_master_sdodict; 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@442: // check for pending EEPROM write operations. fp@304: list_for_each_entry(slave, &master->slaves, list) { fp@304: if (!slave->new_eeprom_data) continue; fp@304: fp@304: if (!slave->online || slave->error_flag) { fp@304: kfree(slave->new_eeprom_data); fp@304: slave->new_eeprom_data = NULL; fp@304: EC_ERR("Discarding EEPROM data, slave %i not ready.\n", fp@304: slave->ring_position); fp@304: continue; fp@304: } fp@304: fp@304: // found pending EEPROM write operation. execute it! fp@304: EC_INFO("Writing EEPROM of slave %i...\n", slave->ring_position); fp@470: fsm->slave = slave; fp@304: fsm->sii_offset = 0x0000; fp@433: ec_fsm_sii_write(&fsm->fsm_sii, slave, fsm->sii_offset, fp@433: slave->new_eeprom_data, EC_FSM_SII_NODE); fp@304: fsm->master_state = ec_fsm_master_write_eeprom; fp@304: fsm->master_state(fsm); // execute immediately fp@304: return; fp@304: } fp@304: } fp@304: fp@446: fsm->master_state = ec_fsm_master_end; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master action: Get state of next slave. fp@260: */ fp@260: fp@260: void ec_fsm_master_action_next_slave_state(ec_fsm_t *fsm fp@260: /**< finite state machine */) 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@292: // process next slave fp@292: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@293: ec_datagram_nprd(&fsm->datagram, fsm->slave->station_address, fp@293: 0x0130, 2); fp@293: ec_master_queue_datagram(master, &fsm->datagram); fp@304: fsm->master_state = ec_fsm_master_read_states; fp@304: return; fp@304: } fp@304: fp@304: // all slave states read fp@304: fp@304: // check, if a bus validation has to be done fp@292: if (fsm->master_validation) { fp@292: fsm->master_validation = 0; fp@292: list_for_each_entry(slave, &master->slaves, list) { fp@292: if (slave->online) continue; fp@292: fp@292: // At least one slave is offline. validate! fp@292: EC_INFO("Validating bus.\n"); fp@292: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@292: fsm->master_state = ec_fsm_master_validate_vendor; fp@433: ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x0008, EC_FSM_SII_POSITION); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@292: return; fp@251: } fp@292: } fp@292: fp@304: ec_fsm_master_action_process_states(fsm); fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@409: Master state: READ STATES. fp@260: Fetches the AL- and online state of a slave. fp@260: */ fp@260: fp@304: void ec_fsm_master_read_states(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@260: uint8_t new_state; fp@260: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@446: EC_ERR("Failed to receive AL state datagram for slave %i!\n", fp@446: slave->ring_position); fp@446: fsm->master_state = ec_fsm_master_error; fp@238: return; fp@238: } fp@238: fp@260: // did the slave not respond to its station address? fp@293: if (datagram->working_counter != 1) { fp@260: if (slave->online) { fp@260: slave->online = 0; fp@461: if (slave->master->debug_level) fp@459: EC_DBG("Slave %i: offline.\n", slave->ring_position); fp@260: } fp@260: ec_fsm_master_action_next_slave_state(fsm); fp@260: return; fp@260: } fp@260: fp@260: // slave responded fp@293: new_state = EC_READ_U8(datagram->data); fp@260: if (!slave->online) { // slave was offline before fp@260: slave->online = 1; fp@291: slave->error_flag = 0; // clear error flag fp@260: slave->current_state = new_state; fp@461: if (slave->master->debug_level) { fp@459: char cur_state[EC_STATE_STRING_SIZE]; fp@459: ec_state_string(slave->current_state, cur_state); fp@459: EC_DBG("Slave %i: online (%s).\n", fp@459: slave->ring_position, cur_state); fp@459: } fp@260: } fp@260: else if (new_state != slave->current_state) { fp@461: if (slave->master->debug_level) { fp@459: char old_state[EC_STATE_STRING_SIZE], fp@459: cur_state[EC_STATE_STRING_SIZE]; fp@459: ec_state_string(slave->current_state, old_state); fp@459: ec_state_string(new_state, cur_state); fp@459: EC_DBG("Slave %i: %s -> %s.\n", fp@459: slave->ring_position, old_state, cur_state); fp@459: } fp@260: slave->current_state = new_state; fp@260: } fp@260: fp@454: // check, if new slave state has to be acknowledged fp@454: if (slave->current_state & EC_SLAVE_STATE_ACK_ERR && !slave->error_flag) { fp@454: ec_fsm_change_ack(&fsm->fsm_change, slave); fp@454: ec_fsm_change_exec(&fsm->fsm_change); fp@454: fsm->master_state = ec_fsm_master_acknowledge; fp@454: return; fp@454: } fp@454: fp@454: ec_fsm_master_action_next_slave_state(fsm); fp@454: } fp@454: fp@454: /*****************************************************************************/ fp@454: fp@454: /** fp@454: Master state: ACKNOWLEDGE fp@454: */ fp@454: fp@454: void ec_fsm_master_acknowledge(ec_fsm_t *fsm /**< finite state machine */) fp@454: { fp@454: ec_slave_t *slave = fsm->slave; fp@454: fp@454: if (ec_fsm_change_exec(&fsm->fsm_change)) return; fp@454: fp@454: if (!ec_fsm_change_success(&fsm->fsm_change)) { fp@454: fsm->slave->error_flag = 1; fp@454: EC_ERR("Failed to acknowledge state change on slave %i.\n", fp@454: slave->ring_position); fp@454: fsm->master_state = ec_fsm_master_error; fp@454: return; fp@454: } fp@454: fp@260: ec_fsm_master_action_next_slave_state(fsm); fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: VALIDATE_VENDOR. fp@260: Validates the vendor ID of a slave. fp@260: */ fp@260: fp@260: void ec_fsm_master_validate_vendor(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@260: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@292: fsm->slave->error_flag = 1; fp@260: EC_ERR("Failed to validate vendor ID of slave %i.\n", fp@260: slave->ring_position); fp@446: fsm->master_state = ec_fsm_master_error; fp@238: return; fp@238: } fp@238: fp@433: if (EC_READ_U32(fsm->fsm_sii.value) != slave->sii_vendor_id) { fp@446: EC_ERR("Slave %i has an invalid vendor ID!\n", slave->ring_position); fp@446: fsm->master_state = ec_fsm_master_error; fp@238: return; fp@238: } fp@238: fp@260: // vendor ID is ok. check product code. fp@260: fsm->master_state = ec_fsm_master_validate_product; fp@433: ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x000A, EC_FSM_SII_POSITION); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@304: Master action: ADDRESS. fp@260: Looks for slave, that have lost their configuration and writes fp@260: their station address, so that they can be reconfigured later. fp@260: */ fp@260: fp@304: void ec_fsm_master_action_addresses(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@260: fp@260: while (fsm->slave->online) { fp@260: if (fsm->slave->list.next == &fsm->master->slaves) { // last slave? fp@251: fsm->master_state = ec_fsm_master_start; fp@260: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@260: // check next slave fp@260: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@260: } fp@260: fp@446: if (fsm->master->debug_level) fp@446: EC_DBG("Reinitializing slave %i.\n", fsm->slave->ring_position); fp@260: fp@260: // write station address fp@293: ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x0010, 2); fp@293: EC_WRITE_U16(datagram->data, fsm->slave->station_address); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@304: fsm->master_state = ec_fsm_master_rewrite_addresses; fp@304: } fp@304: fp@304: /*****************************************************************************/ fp@304: fp@304: /** fp@304: Master state: VALIDATE_PRODUCT. fp@304: Validates the product ID of a slave. fp@304: */ fp@304: fp@304: void ec_fsm_master_validate_product(ec_fsm_t *fsm /**< finite state machine */) fp@304: { fp@304: ec_slave_t *slave = fsm->slave; fp@304: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@304: fsm->slave->error_flag = 1; fp@304: EC_ERR("Failed to validate product code of slave %i.\n", fp@304: slave->ring_position); fp@446: fsm->master_state = ec_fsm_master_error; fp@304: return; fp@304: } fp@304: fp@433: if (EC_READ_U32(fsm->fsm_sii.value) != slave->sii_product_code) { fp@304: EC_ERR("Slave %i: invalid product code!\n", slave->ring_position); fp@304: EC_ERR("expected 0x%08X, got 0x%08X.\n", slave->sii_product_code, fp@433: EC_READ_U32(fsm->fsm_sii.value)); fp@446: fsm->master_state = ec_fsm_master_error; fp@304: return; fp@304: } fp@304: fp@304: // have all states been validated? fp@304: if (slave->list.next == &fsm->master->slaves) { fp@304: fsm->slave = list_entry(fsm->master->slaves.next, ec_slave_t, list); fp@304: // start writing addresses to offline slaves fp@304: ec_fsm_master_action_addresses(fsm); fp@304: return; fp@304: } fp@304: fp@304: // validate next slave fp@304: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@304: fsm->master_state = ec_fsm_master_validate_vendor; fp@433: ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x0008, EC_FSM_SII_POSITION); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@409: Master state: REWRITE ADDRESS. fp@260: Checks, if the new station address has been written to the slave. fp@260: */ fp@260: fp@304: void ec_fsm_master_rewrite_addresses(ec_fsm_t *fsm fp@304: /**< finite state machine */ fp@304: ) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED fp@325: || datagram->working_counter != 1) { fp@260: EC_ERR("Failed to write station address on slave %i.\n", fp@260: slave->ring_position); fp@260: } fp@260: fp@260: if (fsm->slave->list.next == &fsm->master->slaves) { // last slave? fp@260: fsm->master_state = ec_fsm_master_start; fp@260: fsm->master_state(fsm); // execute immediately fp@260: return; fp@260: } fp@260: fp@260: // check next slave fp@260: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@304: // Write new station address to slave fp@304: ec_fsm_master_action_addresses(fsm); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@409: Master state: SCAN SLAVES. fp@260: Executes the sub-statemachine for the scanning of a slave. fp@260: */ fp@260: fp@304: void ec_fsm_master_scan_slaves(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@238: ec_master_t *master = fsm->master; fp@251: ec_slave_t *slave = fsm->slave; fp@325: fp@238: fsm->slave_state(fsm); // execute slave state machine fp@238: fp@462: if (fsm->slave_state != ec_fsm_slave_state_end fp@462: && fsm->slave_state != ec_fsm_slave_state_error) return; fp@325: fp@325: // another slave to fetch? fp@325: if (slave->list.next != &master->slaves) { fp@325: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@325: fsm->slave_state = ec_fsm_slavescan_start; fp@325: fsm->slave_state(fsm); // execute immediately fp@325: return; fp@325: } fp@325: fp@325: EC_INFO("Bus scanning completed.\n"); fp@325: fp@325: ec_master_calc_addressing(master); fp@325: fp@428: // set initial states of all slaves to PREOP to make mailbox fp@428: // communication possible fp@325: list_for_each_entry(slave, &master->slaves, list) { fp@446: ec_slave_request_state(slave, EC_SLAVE_STATE_PREOP); fp@446: } fp@446: fp@446: fsm->master_state = ec_fsm_master_end; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@409: Master state: CONFIGURE SLAVES. fp@260: Starts configuring a slave. fp@260: */ fp@260: fp@304: void ec_fsm_master_configure_slave(ec_fsm_t *fsm fp@304: /**< finite state machine */ fp@304: ) fp@260: { fp@251: fsm->slave_state(fsm); // execute slave's state machine fp@325: fp@462: if (fsm->slave_state != ec_fsm_slave_state_end fp@462: && fsm->slave_state != ec_fsm_slave_state_error) return; fp@304: fp@304: ec_fsm_master_action_process_states(fsm); fp@238: } fp@238: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@409: Master state: WRITE EEPROM. fp@269: */ fp@269: fp@304: void ec_fsm_master_write_eeprom(ec_fsm_t *fsm /**< finite state machine */) fp@269: { fp@269: ec_slave_t *slave = fsm->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@292: fsm->slave->error_flag = 1; fp@269: EC_ERR("Failed to write EEPROM contents to slave %i.\n", fp@269: slave->ring_position); fp@269: kfree(slave->new_eeprom_data); fp@269: slave->new_eeprom_data = NULL; fp@446: fsm->master_state = ec_fsm_master_error; fp@269: return; fp@269: } fp@269: fp@269: fsm->sii_offset++; fp@269: if (fsm->sii_offset < slave->new_eeprom_size) { fp@433: ec_fsm_sii_write(&fsm->fsm_sii, slave, fsm->sii_offset, fp@433: slave->new_eeprom_data + fsm->sii_offset, fp@433: EC_FSM_SII_NODE); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@269: return; fp@269: } fp@269: fp@269: // finished writing EEPROM fp@269: EC_INFO("Finished writing EEPROM of slave %i.\n", slave->ring_position); fp@269: kfree(slave->new_eeprom_data); fp@269: slave->new_eeprom_data = NULL; fp@269: fp@325: // TODO: Evaluate new EEPROM contents! fp@325: fp@269: // restart master state machine. fp@325: fsm->master_state = ec_fsm_master_start; fp@269: fsm->master_state(fsm); // execute immediately fp@419: } fp@419: fp@419: /*****************************************************************************/ fp@419: fp@419: /** fp@419: Master state: SDODICT. fp@419: */ fp@419: fp@419: void ec_fsm_master_sdodict(ec_fsm_t *fsm /**< finite state machine */) 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@446: fsm->master_state = ec_fsm_master_error; fp@419: return; fp@419: } fp@419: fp@419: // 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@423: 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@419: // restart master state machine. fp@419: fsm->master_state = ec_fsm_master_start; fp@419: fsm->master_state(fsm); // execute immediately fp@269: } fp@269: fp@430: /*****************************************************************************/ fp@430: fp@430: /** fp@430: Master state: SDO REQUEST. fp@430: */ fp@430: fp@430: void ec_fsm_master_sdo_request(ec_fsm_t *fsm /**< finite state machine */) fp@430: { fp@430: ec_master_t *master = fsm->master; fp@436: 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@430: request->return_code = -1; fp@441: master->sdo_seq_master++; fp@446: fsm->master_state = ec_fsm_master_error; fp@430: return; fp@430: } fp@430: fp@430: // SDO dictionary fetching finished fp@430: fp@430: request->return_code = 1; fp@441: master->sdo_seq_master++; fp@430: fp@430: // restart master state machine. fp@430: fsm->master_state = ec_fsm_master_start; fp@430: fsm->master_state(fsm); // execute immediately fp@430: } fp@430: fp@446: /*****************************************************************************/ fp@446: fp@446: /** fp@446: State: ERROR. fp@446: */ fp@446: fp@446: void ec_fsm_master_error(ec_fsm_t *fsm /**< finite state machine */) fp@446: { fp@446: fsm->master_state = ec_fsm_master_start; fp@446: } fp@446: fp@446: /*****************************************************************************/ fp@446: fp@446: /** fp@446: State: END. fp@446: */ fp@446: fp@446: void ec_fsm_master_end(ec_fsm_t *fsm /**< finite state machine */) fp@446: { fp@446: fsm->master_state = ec_fsm_master_start; fp@446: } fp@446: fp@238: /****************************************************************************** fp@325: * slave scan state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@409: Slave scan state: START. fp@238: First state of the slave state machine. Writes the station address to the fp@238: slave, according to its ring position. fp@238: */ fp@238: fp@308: void ec_fsm_slavescan_start(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@238: fp@238: // write station address fp@293: ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x0010, 2); fp@293: EC_WRITE_U16(datagram->data, fsm->slave->station_address); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@310: fsm->slave_state = ec_fsm_slavescan_address; fp@310: } fp@310: fp@310: /*****************************************************************************/ fp@310: fp@310: /** fp@409: Slave scan state: ADDRESS. fp@310: */ fp@310: fp@310: void ec_fsm_slavescan_address(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED fp@325: || datagram->working_counter != 1) { fp@292: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@292: EC_ERR("Failed to write station address of slave %i.\n", fp@238: fsm->slave->ring_position); fp@292: return; fp@292: } fp@292: fp@292: // Read AL state fp@293: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x0130, 2); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@310: fsm->slave_state = ec_fsm_slavescan_state; fp@310: } fp@310: fp@310: /*****************************************************************************/ fp@310: fp@310: /** fp@409: Slave scan state: STATE. fp@310: */ fp@310: fp@310: void ec_fsm_slavescan_state(ec_fsm_t *fsm /**< finite state machine */) fp@255: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: ec_slave_t *slave = fsm->slave; fp@293: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED fp@325: || datagram->working_counter != 1) { fp@291: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@292: EC_ERR("Failed to read AL state of slave %i.\n", fp@255: fsm->slave->ring_position); fp@255: return; fp@255: } fp@255: fp@293: slave->current_state = EC_READ_U8(datagram->data); fp@402: if (slave->current_state & EC_SLAVE_STATE_ACK_ERR) { fp@454: char state_str[EC_STATE_STRING_SIZE]; fp@454: ec_state_string(slave->current_state, state_str); fp@454: EC_WARN("Slave %i has state error bit set (%s)!\n", fp@454: slave->ring_position, state_str); fp@255: } fp@255: fp@238: // read base data fp@293: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x0000, 6); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@310: fsm->slave_state = ec_fsm_slavescan_base; fp@310: } fp@310: fp@310: /*****************************************************************************/ fp@310: fp@310: /** fp@409: Slave scan state: BASE. fp@310: */ fp@310: fp@310: void ec_fsm_slavescan_base(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: ec_slave_t *slave = fsm->slave; fp@293: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED fp@325: || datagram->working_counter != 1) { fp@291: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@292: EC_ERR("Failed to read base data of slave %i.\n", fp@238: slave->ring_position); fp@238: return; fp@238: } fp@238: fp@293: slave->base_type = EC_READ_U8 (datagram->data); fp@293: slave->base_revision = EC_READ_U8 (datagram->data + 1); fp@293: slave->base_build = EC_READ_U16(datagram->data + 2); fp@293: slave->base_fmmu_count = EC_READ_U8 (datagram->data + 4); fp@293: slave->base_sync_count = EC_READ_U8 (datagram->data + 5); fp@238: fp@238: if (slave->base_fmmu_count > EC_MAX_FMMUS) fp@238: slave->base_fmmu_count = EC_MAX_FMMUS; fp@238: fp@238: // read data link status fp@293: ec_datagram_nprd(datagram, slave->station_address, 0x0110, 2); fp@293: ec_master_queue_datagram(slave->master, datagram); fp@310: fsm->slave_state = ec_fsm_slavescan_datalink; fp@310: } fp@310: fp@310: /*****************************************************************************/ fp@310: fp@310: /** fp@409: Slave scan state: DATALINK. fp@310: */ fp@310: fp@310: void ec_fsm_slavescan_datalink(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@238: ec_slave_t *slave = fsm->slave; fp@238: uint16_t dl_status; fp@238: unsigned int i; fp@238: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED fp@325: || datagram->working_counter != 1) { fp@291: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@292: EC_ERR("Failed to read DL status of slave %i.\n", fp@238: slave->ring_position); fp@238: return; fp@238: } fp@238: fp@293: dl_status = EC_READ_U16(datagram->data); fp@238: for (i = 0; i < 4; i++) { fp@238: slave->dl_link[i] = dl_status & (1 << (4 + i)) ? 1 : 0; fp@238: slave->dl_loop[i] = dl_status & (1 << (8 + i * 2)) ? 1 : 0; fp@238: slave->dl_signal[i] = dl_status & (1 << (9 + i * 2)) ? 1 : 0; fp@238: } fp@238: fp@266: // Start fetching EEPROM size fp@266: fp@266: fsm->sii_offset = 0x0040; // first category header fp@433: ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset, EC_FSM_SII_NODE); fp@310: fsm->slave_state = ec_fsm_slavescan_eeprom_size; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@409: Slave scan state: EEPROM SIZE. fp@310: */ fp@310: fp@310: void ec_fsm_slavescan_eeprom_size(ec_fsm_t *fsm /**< finite state machine */) fp@266: { fp@266: ec_slave_t *slave = fsm->slave; fp@266: uint16_t cat_type, cat_size; fp@238: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@292: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@266: EC_ERR("Failed to read EEPROM size of slave %i.\n", fp@266: slave->ring_position); fp@238: return; fp@238: } fp@238: fp@433: cat_type = EC_READ_U16(fsm->fsm_sii.value); fp@433: cat_size = EC_READ_U16(fsm->fsm_sii.value + 2); fp@266: fp@266: if (cat_type != 0xFFFF) { // not the last category fp@266: fsm->sii_offset += cat_size + 2; fp@433: ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset, fp@433: EC_FSM_SII_NODE); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately fp@266: return; fp@266: } fp@266: fp@266: slave->eeprom_size = (fsm->sii_offset + 1) * 2; fp@266: fp@266: if (slave->eeprom_data) { fp@266: EC_INFO("Freeing old EEPROM data on slave %i...\n", fp@266: slave->ring_position); fp@266: kfree(slave->eeprom_data); fp@266: } fp@266: fp@266: if (!(slave->eeprom_data = fp@266: (uint8_t *) kmalloc(slave->eeprom_size, GFP_ATOMIC))) { fp@291: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@266: EC_ERR("Failed to allocate EEPROM data on slave %i.\n", fp@266: slave->ring_position); fp@266: return; fp@266: } fp@266: fp@266: // Start fetching EEPROM contents fp@266: fp@433: fsm->slave_state = ec_fsm_slavescan_eeprom_data; fp@266: fsm->sii_offset = 0x0000; fp@433: ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset, EC_FSM_SII_NODE); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@409: Slave scan state: EEPROM DATA. fp@310: */ fp@310: fp@310: void ec_fsm_slavescan_eeprom_data(ec_fsm_t *fsm /**< finite state machine */) fp@266: { fp@266: ec_slave_t *slave = fsm->slave; fp@266: uint16_t *cat_word, cat_type, cat_size; fp@266: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@292: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@266: EC_ERR("Failed to fetch EEPROM contents of slave %i.\n", fp@266: slave->ring_position); fp@238: return; fp@238: } fp@238: fp@266: // 2 words fetched fp@266: fp@266: if (fsm->sii_offset + 2 <= slave->eeprom_size / 2) { // 2 words fit fp@433: memcpy(slave->eeprom_data + fsm->sii_offset * 2, fp@433: fsm->fsm_sii.value, 4); fp@266: } fp@266: else { // copy the last word fp@433: memcpy(slave->eeprom_data + fsm->sii_offset * 2, fp@433: fsm->fsm_sii.value, 2); fp@266: } fp@266: fp@266: if (fsm->sii_offset + 2 < slave->eeprom_size / 2) { fp@266: // fetch the next 2 words fp@266: fsm->sii_offset += 2; fp@433: ec_fsm_sii_read(&fsm->fsm_sii, slave, fsm->sii_offset, fp@433: EC_FSM_SII_NODE); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute state immediately fp@266: return; fp@266: } fp@266: fp@266: // Evaluate EEPROM contents fp@266: fp@266: slave->sii_alias = fp@266: EC_READ_U16(slave->eeprom_data + 2 * 0x0004); fp@266: slave->sii_vendor_id = fp@266: EC_READ_U32(slave->eeprom_data + 2 * 0x0008); fp@266: slave->sii_product_code = fp@266: EC_READ_U32(slave->eeprom_data + 2 * 0x000A); fp@266: slave->sii_revision_number = fp@266: EC_READ_U32(slave->eeprom_data + 2 * 0x000C); fp@266: slave->sii_serial_number = fp@266: EC_READ_U32(slave->eeprom_data + 2 * 0x000E); fp@266: slave->sii_rx_mailbox_offset = fp@266: EC_READ_U16(slave->eeprom_data + 2 * 0x0018); fp@266: slave->sii_rx_mailbox_size = fp@266: EC_READ_U16(slave->eeprom_data + 2 * 0x0019); fp@266: slave->sii_tx_mailbox_offset = fp@266: EC_READ_U16(slave->eeprom_data + 2 * 0x001A); fp@266: slave->sii_tx_mailbox_size = fp@266: EC_READ_U16(slave->eeprom_data + 2 * 0x001B); fp@266: slave->sii_mailbox_protocols = fp@266: EC_READ_U16(slave->eeprom_data + 2 * 0x001C); fp@266: fp@266: // evaluate category data fp@266: cat_word = (uint16_t *) slave->eeprom_data + 0x0040; fp@266: while (EC_READ_U16(cat_word) != 0xFFFF) { fp@266: cat_type = EC_READ_U16(cat_word) & 0x7FFF; fp@266: cat_size = EC_READ_U16(cat_word + 1); fp@266: fp@266: switch (cat_type) { fp@266: case 0x000A: fp@266: if (ec_slave_fetch_strings(slave, (uint8_t *) (cat_word + 2))) fp@266: goto end; fp@266: break; fp@266: case 0x001E: fp@325: ec_slave_fetch_general(slave, (uint8_t *) (cat_word + 2)); fp@266: break; fp@266: case 0x0028: fp@266: break; fp@266: case 0x0029: fp@266: if (ec_slave_fetch_sync(slave, (uint8_t *) (cat_word + 2), fp@266: cat_size)) fp@266: goto end; fp@266: break; fp@266: case 0x0032: fp@266: if (ec_slave_fetch_pdo(slave, (uint8_t *) (cat_word + 2), fp@266: cat_size, EC_TX_PDO)) fp@266: goto end; fp@266: break; fp@266: case 0x0033: fp@266: if (ec_slave_fetch_pdo(slave, (uint8_t *) (cat_word + 2), fp@266: cat_size, EC_RX_PDO)) fp@266: goto end; fp@266: break; fp@266: default: fp@461: if (fsm->master->debug_level) fp@461: EC_WARN("Unknown category type 0x%04X in slave %i.\n", fp@461: cat_type, slave->ring_position); fp@266: } fp@266: fp@266: cat_word += cat_size + 2; fp@266: } fp@266: fp@462: fsm->slave_state = ec_fsm_slave_state_end; fp@325: return; fp@310: fp@292: end: fp@325: EC_ERR("Failed to analyze category data.\n"); fp@291: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@308: } fp@308: fp@308: /****************************************************************************** fp@325: * slave configuration state machine fp@308: *****************************************************************************/ fp@251: fp@251: /** fp@456: Slave configuration state: START. fp@456: */ fp@456: fp@462: void ec_fsm_slaveconf_state_start(ec_fsm_t *fsm /**< finite state machine */) fp@456: { fp@456: if (fsm->master->debug_level) { fp@456: EC_DBG("Configuring slave %i...\n", fsm->slave->ring_position); fp@456: } fp@456: fp@456: ec_fsm_change_start(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_INIT); fp@456: ec_fsm_change_exec(&fsm->fsm_change); fp@462: fsm->slave_state = ec_fsm_slaveconf_state_init; fp@456: } fp@456: fp@456: /*****************************************************************************/ fp@456: fp@456: /** fp@409: Slave configuration state: INIT. fp@309: */ fp@309: fp@462: void ec_fsm_slaveconf_state_init(ec_fsm_t *fsm /**< finite state machine */) fp@462: { fp@462: ec_master_t *master = fsm->master; fp@462: ec_slave_t *slave = fsm->slave; fp@462: ec_datagram_t *datagram = &fsm->datagram; fp@462: fp@462: if (ec_fsm_change_exec(&fsm->fsm_change)) return; fp@462: fp@462: if (!ec_fsm_change_success(&fsm->fsm_change)) { fp@462: slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@462: return; fp@462: } fp@462: fp@462: slave->configured = 1; fp@462: fp@462: if (master->debug_level) { fp@462: EC_DBG("Slave %i is now in INIT.\n", slave->ring_position); fp@462: } fp@462: fp@462: // check and reset CRC fault counters fp@462: //ec_slave_check_crc(slave); fp@462: // TODO: Implement state machine for CRC checking. fp@462: fp@462: if (!slave->base_fmmu_count) { // skip FMMU configuration fp@462: ec_fsm_slaveconf_enter_sync(fsm); fp@462: return; fp@462: } fp@462: fp@462: if (master->debug_level) fp@462: EC_DBG("Clearing FMMU configurations of slave %i...\n", fp@462: slave->ring_position); fp@462: fp@462: // clear FMMU configurations fp@462: ec_datagram_npwr(datagram, slave->station_address, fp@462: 0x0600, EC_FMMU_SIZE * slave->base_fmmu_count); fp@462: memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count); fp@462: ec_master_queue_datagram(master, datagram); fp@462: fsm->slave_state = ec_fsm_slaveconf_state_clear_fmmus; fp@462: } fp@462: fp@462: /*****************************************************************************/ fp@462: fp@462: /** fp@462: Slave configuration state: CLEAR FMMU. fp@462: */ fp@462: fp@462: void ec_fsm_slaveconf_state_clear_fmmus(ec_fsm_t *fsm fp@462: /**< finite state machine */) fp@462: { fp@462: ec_datagram_t *datagram = &fsm->datagram; fp@462: fp@462: if (datagram->state != EC_DATAGRAM_RECEIVED fp@462: || datagram->working_counter != 1) { fp@462: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@462: EC_ERR("Failed to clear FMMUs on slave %i.\n", fp@462: fsm->slave->ring_position); fp@462: return; fp@462: } fp@462: fp@462: ec_fsm_slaveconf_enter_sync(fsm); fp@462: } fp@462: fp@462: /*****************************************************************************/ fp@462: fp@462: /** fp@462: */ fp@462: fp@462: void ec_fsm_slaveconf_enter_sync(ec_fsm_t *fsm /**< finite state machine */) fp@309: { fp@409: ec_master_t *master = fsm->master; fp@309: ec_slave_t *slave = fsm->slave; fp@309: ec_datagram_t *datagram = &fsm->datagram; fp@325: const ec_sii_sync_t *sync; fp@420: ec_sii_sync_t mbox_sync; fp@251: fp@251: // slave is now in INIT fp@251: if (slave->current_state == slave->requested_state) { fp@462: fsm->slave_state = ec_fsm_slave_state_end; // successful fp@409: if (master->debug_level) { fp@409: EC_DBG("Finished configuration of slave %i.\n", fp@409: slave->ring_position); fp@409: } fp@325: return; fp@251: } fp@251: fp@251: if (!slave->base_sync_count) { // no sync managers fp@462: ec_fsm_slaveconf_enter_preop(fsm); fp@251: return; fp@251: } fp@251: fp@409: if (master->debug_level) { fp@409: EC_DBG("Configuring sync managers of slave %i.\n", fp@409: slave->ring_position); fp@409: } fp@409: fp@251: // configure sync managers fp@293: ec_datagram_npwr(datagram, slave->station_address, 0x0800, fp@293: EC_SYNC_SIZE * slave->base_sync_count); fp@293: memset(datagram->data, 0x00, EC_SYNC_SIZE * slave->base_sync_count); fp@251: fp@420: if (list_empty(&slave->sii_syncs)) { fp@420: if (slave->sii_rx_mailbox_offset && slave->sii_tx_mailbox_offset) { fp@420: if (slave->master->debug_level) fp@420: EC_DBG("Guessing sync manager settings for slave %i.\n", fp@420: slave->ring_position); fp@420: mbox_sync.index = 0; fp@420: mbox_sync.physical_start_address = slave->sii_tx_mailbox_offset; fp@420: mbox_sync.length = slave->sii_tx_mailbox_size; fp@420: mbox_sync.control_register = 0x26; fp@420: mbox_sync.enable = 0x01; fp@420: mbox_sync.est_length = 0; fp@420: ec_sync_config(&mbox_sync, slave, fp@420: datagram->data + EC_SYNC_SIZE * mbox_sync.index); fp@420: mbox_sync.index = 1; fp@420: mbox_sync.physical_start_address = slave->sii_rx_mailbox_offset; fp@420: mbox_sync.length = slave->sii_rx_mailbox_size; fp@420: mbox_sync.control_register = 0x22; fp@420: mbox_sync.enable = 0x01; fp@420: mbox_sync.est_length = 0; fp@420: ec_sync_config(&mbox_sync, slave, fp@420: datagram->data + EC_SYNC_SIZE * mbox_sync.index); fp@420: } fp@420: } fp@420: else { fp@420: list_for_each_entry(sync, &slave->sii_syncs, list) { fp@420: if (sync->index >= slave->base_sync_count) { fp@420: EC_ERR("Invalid sync manager configuration found!"); fp@420: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@420: return; fp@420: } fp@420: ec_sync_config(sync, slave, fp@420: datagram->data + EC_SYNC_SIZE * sync->index); fp@420: } fp@251: } fp@251: fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@462: fsm->slave_state = ec_fsm_slaveconf_state_sync; fp@309: } fp@309: fp@309: /*****************************************************************************/ fp@309: fp@309: /** fp@409: Slave configuration state: SYNC. fp@309: */ fp@309: fp@462: void ec_fsm_slaveconf_state_sync(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: ec_slave_t *slave = fsm->slave; fp@293: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED fp@325: || datagram->working_counter != 1) { fp@292: slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@260: EC_ERR("Failed to set sync managers on slave %i.\n", fp@251: slave->ring_position); fp@251: return; fp@251: } fp@251: fp@462: ec_fsm_slaveconf_enter_preop(fsm); fp@462: } fp@462: fp@462: /*****************************************************************************/ fp@462: fp@462: /** fp@462: */ fp@462: fp@462: void ec_fsm_slaveconf_enter_preop(ec_fsm_t *fsm /**< finite state machine */) fp@462: { fp@462: fsm->slave_state = ec_fsm_slaveconf_state_preop; fp@462: ec_fsm_change_start(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_PREOP); fp@434: ec_fsm_change_exec(&fsm->fsm_change); // execute immediately fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@409: Slave configuration state: PREOP. fp@309: */ fp@309: fp@462: void ec_fsm_slaveconf_state_preop(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@251: ec_slave_t *slave = fsm->slave; fp@251: ec_master_t *master = fsm->master; fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@251: unsigned int j; fp@251: fp@435: if (ec_fsm_change_exec(&fsm->fsm_change)) return; fp@434: fp@434: if (!ec_fsm_change_success(&fsm->fsm_change)) { fp@292: slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@325: return; fp@325: } fp@325: fp@251: // slave is now in PREOP fp@419: slave->jiffies_preop = fsm->datagram.jiffies_received; fp@409: fp@409: if (master->debug_level) { fp@409: EC_DBG("Slave %i is now in PREOP.\n", slave->ring_position); fp@409: } fp@409: fp@419: if (slave->current_state == slave->requested_state) { fp@462: fsm->slave_state = ec_fsm_slave_state_end; // successful fp@419: if (master->debug_level) { fp@419: EC_DBG("Finished configuration of slave %i.\n", fp@419: slave->ring_position); fp@419: } fp@419: return; fp@419: } fp@419: fp@419: if (!slave->base_fmmu_count) { // skip FMMU configuration fp@419: if (list_empty(&slave->sdo_confs)) { // skip SDO configuration fp@462: ec_fsm_slaveconf_enter_saveop(fsm); fp@338: return; fp@338: } fp@409: fp@462: ec_fsm_slaveconf_enter_sdoconf(fsm); fp@251: return; fp@251: } fp@251: fp@251: // configure FMMUs fp@293: ec_datagram_npwr(datagram, slave->station_address, fp@293: 0x0600, EC_FMMU_SIZE * slave->base_fmmu_count); fp@293: memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count); fp@251: for (j = 0; j < slave->fmmu_count; j++) { fp@275: ec_fmmu_config(&slave->fmmus[j], slave, fp@293: datagram->data + EC_FMMU_SIZE * j); fp@293: } fp@293: fp@293: ec_master_queue_datagram(master, datagram); fp@462: fsm->slave_state = ec_fsm_slaveconf_state_fmmu; fp@309: } fp@309: fp@309: /*****************************************************************************/ fp@309: fp@309: /** fp@409: Slave configuration state: FMMU. fp@309: */ fp@309: fp@462: void ec_fsm_slaveconf_state_fmmu(ec_fsm_t *fsm /**< finite state machine */) fp@309: { fp@309: ec_datagram_t *datagram = &fsm->datagram; fp@419: ec_slave_t *slave = fsm->slave; fp@309: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED fp@325: || datagram->working_counter != 1) { fp@291: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@292: EC_ERR("Failed to set FMMUs on slave %i.\n", fp@292: fsm->slave->ring_position); fp@251: return; fp@251: } fp@251: fp@419: // No CoE configuration to be applied? Jump to SAVEOP state. fp@419: if (list_empty(&slave->sdo_confs)) { // skip SDO configuration fp@462: ec_fsm_slaveconf_enter_saveop(fsm); fp@462: return; fp@462: } fp@462: fp@462: ec_fsm_slaveconf_enter_sdoconf(fsm); fp@462: } fp@462: fp@462: /*****************************************************************************/ fp@462: fp@462: /** fp@462: */ fp@462: fp@462: void ec_fsm_slaveconf_enter_sdoconf(ec_fsm_t *fsm /**< finite state machine */) fp@462: { fp@419: // start SDO configuration fp@462: fsm->slave_state = ec_fsm_slaveconf_state_sdoconf; fp@462: fsm->sdodata = list_entry(fsm->slave->sdo_confs.next, ec_sdo_data_t, list); fp@462: ec_fsm_coe_download(&fsm->fsm_coe, fsm->slave, fsm->sdodata); fp@436: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@419: } fp@419: fp@419: /*****************************************************************************/ fp@419: fp@419: /** fp@419: Slave configuration state: SDOCONF. fp@419: */ fp@419: fp@462: void ec_fsm_slaveconf_state_sdoconf(ec_fsm_t *fsm /**< finite state machine */) 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@419: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@419: return; fp@419: } fp@419: fp@419: // Another SDO to configure? fp@436: if (fsm->sdodata->list.next != &fsm->slave->sdo_confs) { fp@436: fsm->sdodata = list_entry(fsm->sdodata->list.next, fp@436: ec_sdo_data_t, list); fp@436: ec_fsm_coe_download(&fsm->fsm_coe, fsm->slave, fsm->sdodata); fp@436: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@419: return; fp@419: } fp@419: fp@419: // All SDOs are now configured. fp@419: fp@251: // set state to SAVEOP fp@462: ec_fsm_slaveconf_enter_saveop(fsm); fp@462: } fp@462: fp@462: /*****************************************************************************/ fp@462: fp@462: /** fp@462: */ fp@462: fp@462: void ec_fsm_slaveconf_enter_saveop(ec_fsm_t *fsm /**< finite state machine */) fp@462: { fp@462: fsm->slave_state = ec_fsm_slaveconf_state_saveop; fp@454: ec_fsm_change_start(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_SAVEOP); fp@434: ec_fsm_change_exec(&fsm->fsm_change); // execute immediately fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@409: Slave configuration state: SAVEOP. fp@309: */ fp@309: fp@462: void ec_fsm_slaveconf_state_saveop(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@409: ec_master_t *master = fsm->master; fp@409: ec_slave_t *slave = fsm->slave; fp@409: fp@435: if (ec_fsm_change_exec(&fsm->fsm_change)) return; fp@434: fp@434: if (!ec_fsm_change_success(&fsm->fsm_change)) { fp@292: fsm->slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@325: return; fp@325: } fp@325: fp@251: // slave is now in SAVEOP fp@409: fp@409: if (master->debug_level) { fp@411: EC_DBG("Slave %i is now in SAVEOP.\n", slave->ring_position); fp@409: } fp@409: fp@251: if (fsm->slave->current_state == fsm->slave->requested_state) { fp@462: fsm->slave_state = ec_fsm_slave_state_end; // successful fp@409: if (master->debug_level) { fp@409: EC_DBG("Finished configuration of slave %i.\n", fp@409: slave->ring_position); fp@409: } fp@251: return; fp@251: } fp@251: fp@251: // set state to OP fp@462: fsm->slave_state = ec_fsm_slaveconf_state_op; fp@454: ec_fsm_change_start(&fsm->fsm_change, slave, EC_SLAVE_STATE_OP); fp@434: ec_fsm_change_exec(&fsm->fsm_change); // execute immediately fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@409: Slave configuration state: OP fp@309: */ fp@309: fp@462: void ec_fsm_slaveconf_state_op(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@409: ec_master_t *master = fsm->master; fp@409: ec_slave_t *slave = fsm->slave; fp@409: fp@435: if (ec_fsm_change_exec(&fsm->fsm_change)) return; fp@434: fp@434: if (!ec_fsm_change_success(&fsm->fsm_change)) { fp@409: slave->error_flag = 1; fp@462: fsm->slave_state = ec_fsm_slave_state_error; fp@325: return; fp@325: } fp@325: fp@251: // slave is now in OP fp@409: fp@409: if (master->debug_level) { fp@409: EC_DBG("Slave %i is now in OP.\n", slave->ring_position); fp@409: EC_DBG("Finished configuration of slave %i.\n", slave->ring_position); fp@409: } fp@409: fp@462: fsm->slave_state = ec_fsm_slave_state_end; // successful fp@238: } fp@238: fp@238: /****************************************************************************** fp@329: * Common state functions fp@329: *****************************************************************************/ fp@286: fp@286: /** fp@325: State: ERROR. fp@325: */ fp@325: fp@462: void ec_fsm_slave_state_error(ec_fsm_t *fsm /**< finite state machine */) fp@325: { fp@325: } fp@325: fp@325: /*****************************************************************************/ fp@325: fp@325: /** fp@325: State: END. fp@325: */ fp@325: fp@462: void ec_fsm_slave_state_end(ec_fsm_t *fsm /**< finite state machine */) fp@462: { fp@462: } fp@462: fp@462: /*****************************************************************************/