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@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@251: fp@325: void ec_fsm_startup_start(ec_fsm_t *); fp@325: void ec_fsm_startup_broadcast(ec_fsm_t *); fp@325: void ec_fsm_startup_scan(ec_fsm_t *); fp@325: fp@325: void ec_fsm_configuration_start(ec_fsm_t *); fp@325: void ec_fsm_configuration_conf(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@309: void ec_fsm_slaveconf_init(ec_fsm_t *); fp@308: void ec_fsm_slaveconf_sync(ec_fsm_t *); fp@308: void ec_fsm_slaveconf_preop(ec_fsm_t *); fp@419: void ec_fsm_slaveconf_fmmu(ec_fsm_t *); fp@409: void ec_fsm_slaveconf_sdoconf(ec_fsm_t *); fp@308: void ec_fsm_slaveconf_saveop(ec_fsm_t *); fp@308: void ec_fsm_slaveconf_op(ec_fsm_t *); fp@238: fp@325: void ec_fsm_end(ec_fsm_t *); fp@325: void ec_fsm_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: Resets the state machine. fp@260: */ fp@260: fp@260: void ec_fsm_reset(ec_fsm_t *fsm /**< finite state machine */) fp@238: { 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@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@325: return fsm->master_state != ec_fsm_end && fp@325: fsm->master_state != ec_fsm_error; fp@325: } fp@325: fp@325: /*****************************************************************************/ fp@325: fp@343: /** fp@435: Initializes the master startup state machine. fp@435: */ fp@435: fp@435: void ec_fsm_startup(ec_fsm_t *fsm) fp@435: { fp@435: fsm->master_state = ec_fsm_startup_start; fp@435: } fp@435: fp@435: /*****************************************************************************/ fp@435: fp@435: /** fp@343: Returns, if the master startup state machine terminated with success. fp@343: \return non-zero if successful. fp@343: */ fp@343: fp@343: int ec_fsm_startup_success(ec_fsm_t *fsm /**< Finite state machine */) fp@325: { fp@325: return fsm->master_state == ec_fsm_end; fp@325: } fp@325: fp@325: /*****************************************************************************/ fp@325: fp@343: /** fp@343: Initializes the master configuration state machine. fp@343: */ fp@343: fp@325: void ec_fsm_configuration(ec_fsm_t *fsm) fp@325: { fp@325: fsm->master_state = ec_fsm_configuration_start; fp@325: } fp@325: fp@325: /*****************************************************************************/ fp@325: fp@343: /** fp@343: Returns, if the master confuguration state machine terminated with success. fp@343: \return non-zero if successful. fp@343: */ fp@343: fp@343: int ec_fsm_configuration_success(ec_fsm_t *fsm /**< Finite state machine */) fp@325: { fp@325: return fsm->master_state == ec_fsm_end; fp@325: } fp@325: fp@238: /****************************************************************************** fp@325: * master startup state machine fp@325: *****************************************************************************/ fp@325: fp@325: /** fp@325: Master state: START. fp@325: Starts with getting slave count and slave states. fp@325: */ fp@325: fp@325: void ec_fsm_startup_start(ec_fsm_t *fsm) fp@325: { fp@325: ec_datagram_brd(&fsm->datagram, 0x0130, 2); fp@325: ec_master_queue_datagram(fsm->master, &fsm->datagram); fp@325: fsm->master_state = ec_fsm_startup_broadcast; fp@325: } fp@325: fp@325: /*****************************************************************************/ fp@325: fp@325: /** fp@325: Master state: BROADCAST. fp@325: Processes the broadcast read slave count and slaves states. fp@325: */ fp@325: fp@325: void ec_fsm_startup_broadcast(ec_fsm_t *fsm /**< finite state machine */) fp@325: { fp@325: ec_datagram_t *datagram = &fsm->datagram; fp@325: unsigned int i; fp@325: ec_slave_t *slave; fp@325: ec_master_t *master = fsm->master; fp@325: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@397: EC_ERR("Failed to receive broadcast datagram.\n"); fp@325: fsm->master_state = ec_fsm_error; fp@325: return; fp@325: } fp@325: fp@325: EC_INFO("Scanning bus.\n"); fp@325: fp@325: ec_master_clear_slaves(master); fp@325: fp@325: master->slave_count = datagram->working_counter; fp@325: fp@325: if (!master->slave_count) { fp@325: // no slaves present -> finish state machine. fp@325: fsm->master_state = ec_fsm_end; fp@325: return; fp@325: } fp@325: fp@325: // init slaves fp@325: for (i = 0; i < master->slave_count; i++) { fp@325: if (!(slave = (ec_slave_t *) kmalloc(sizeof(ec_slave_t), fp@325: GFP_KERNEL))) { fp@325: EC_ERR("Failed to allocate slave %i!\n", i); fp@325: fsm->master_state = ec_fsm_error; fp@325: return; fp@325: } fp@325: fp@325: if (ec_slave_init(slave, master, i, i + 1)) { fp@325: fsm->master_state = ec_fsm_error; fp@325: return; fp@325: } fp@325: fp@325: if (kobject_add(&slave->kobj)) { fp@325: EC_ERR("Failed to add kobject.\n"); fp@325: kobject_put(&slave->kobj); // free fp@325: fsm->master_state = ec_fsm_error; fp@325: return; fp@325: } fp@325: fp@325: list_add_tail(&slave->list, &master->slaves); fp@325: } fp@325: fp@325: // begin scanning of slaves fp@325: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@325: fsm->slave_state = ec_fsm_slavescan_start; fp@325: fsm->master_state = ec_fsm_startup_scan; fp@325: fsm->master_state(fsm); // execute immediately fp@325: return; fp@325: } fp@325: fp@325: /*****************************************************************************/ fp@325: fp@325: /** fp@325: Master state: SCAN. fp@325: Executes the sub-statemachine for the scanning of a slave. fp@325: */ fp@325: fp@325: void ec_fsm_startup_scan(ec_fsm_t *fsm /**< finite state machine */) fp@325: { fp@325: ec_master_t *master = fsm->master; fp@325: ec_slave_t *slave = fsm->slave; fp@325: fp@325: fsm->slave_state(fsm); // execute slave state machine fp@325: fp@325: if (fsm->slave_state == ec_fsm_error) { fp@325: EC_ERR("Slave scanning failed.\n"); fp@325: fsm->master_state = ec_fsm_error; fp@325: return; fp@325: } fp@325: fp@325: if (fsm->slave_state != ec_fsm_end) return; fp@325: fp@325: // another slave to scan? 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@325: fsm->master_state = ec_fsm_end; fp@325: } fp@325: fp@325: /****************************************************************************** fp@325: * master configuration state machine fp@325: *****************************************************************************/ fp@325: fp@343: /** fp@343: Master configuration state machine: START. fp@343: */ fp@343: fp@325: void ec_fsm_configuration_start(ec_fsm_t *fsm /**< finite state machine */) fp@325: { fp@325: ec_master_t *master = fsm->master; fp@325: fp@325: if (list_empty(&master->slaves)) { fp@325: fsm->master_state = ec_fsm_end; fp@325: return; fp@325: } fp@325: fp@325: // begin configuring slaves fp@325: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@325: fsm->slave_state = ec_fsm_slaveconf_init; fp@434: ec_fsm_change(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_INIT); fp@325: fsm->master_state = ec_fsm_configuration_conf; fp@325: fsm->master_state(fsm); // execute immediately fp@325: } fp@325: fp@325: /*****************************************************************************/ fp@325: fp@325: /** fp@325: Master state: CONF. fp@325: */ fp@325: fp@325: void ec_fsm_configuration_conf(ec_fsm_t *fsm /**< finite state machine */) fp@325: { fp@325: ec_master_t *master = fsm->master; fp@325: ec_slave_t *slave = fsm->slave; fp@325: fp@325: fsm->slave_state(fsm); // execute slave's state machine fp@325: fp@325: if (fsm->slave_state == ec_fsm_error) { fp@325: fsm->master_state = ec_fsm_error; fp@325: return; fp@325: } fp@325: fp@325: if (fsm->slave_state != ec_fsm_end) return; fp@325: fp@325: // another slave to configure? 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_slaveconf_init; fp@434: ec_fsm_change(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_INIT); fp@325: fsm->master_state(fsm); // execute immediately fp@325: return; fp@325: } fp@325: fp@325: fsm->master_state = ec_fsm_end; fp@325: } fp@325: fp@325: /****************************************************************************** fp@325: * 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@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately 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: EC_INFO("Scanning bus.\n"); fp@260: fp@260: ec_master_eoe_stop(master); fp@260: ec_master_clear_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@260: fsm->master_state = ec_fsm_master_start; fp@251: fsm->master_state(fsm); // execute immediately 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@325: ec_master_clear_slaves(master); 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: if (ec_slave_init(slave, master, i, i + 1)) { fp@325: // freeing of "slave" already done fp@325: ec_master_clear_slaves(master); 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: if (kobject_add(&slave->kobj)) { fp@260: EC_ERR("Failed to add kobject.\n"); fp@260: kobject_put(&slave->kobj); // free fp@325: ec_master_clear_slaves(master); 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: list_add_tail(&slave->list, &master->slaves); fp@260: } fp@260: 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@430: ec_sdo_request_t *request, *next_request; 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@421: && (slave->configured fp@421: || slave->current_state == EC_SLAVE_STATE_INIT))) continue; fp@421: fp@437: ec_state_string(slave->current_state, old_state); fp@437: ec_state_string(slave->requested_state, new_state); fp@437: fp@421: if (!slave->configured fp@421: && slave->current_state != EC_SLAVE_STATE_INIT) { fp@437: EC_INFO("Reconfiguring slave %i (%s -> %s).\n", fp@437: slave->ring_position, old_state, new_state); fp@437: } fp@437: else if (slave->current_state != slave->requested_state) { fp@437: EC_INFO("Changing state of slave %i (%s -> %s).\n", fp@421: slave->ring_position, old_state, new_state); fp@421: } fp@304: fp@304: fsm->slave = slave; fp@309: fsm->slave_state = ec_fsm_slaveconf_init; fp@434: ec_fsm_change(&fsm->fsm_change, fsm->slave, EC_SLAVE_STATE_INIT); fp@304: fsm->master_state = ec_fsm_master_configure_slave; fp@304: fsm->master_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@430: // check, if there are pending SDO requests fp@430: list_for_each_entry_safe(request, next_request, fp@430: &master->sdo_requests, queue) { fp@430: // TODO: critical section! fp@430: list_del_init(&request->queue); fp@430: fp@430: slave = request->sdo->slave; fp@430: fp@430: if (slave->current_state == EC_SLAVE_STATE_INIT fp@430: || !slave->online fp@430: || slave->error_flag) { fp@430: request->return_code = -1; fp@430: wake_up_interruptible(&master->sdo_wait_queue); fp@430: continue; fp@430: } fp@430: fp@430: // start uploading SDO fp@430: fsm->slave = slave; fp@430: fsm->master_state = ec_fsm_master_sdo_request; fp@436: fsm->sdo_request = request; fp@436: ec_fsm_coe_upload(&fsm->fsm_coe, slave, request); fp@436: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@430: return; fp@430: } fp@430: fp@419: // check, if slaves have an SDO dictionary to read out. fp@419: list_for_each_entry(slave, &master->slaves, list) { fp@419: if (!(slave->sii_mailbox_protocols & EC_MBOX_COE) fp@419: || slave->sdo_dictionary_fetched fp@419: || slave->current_state == EC_SLAVE_STATE_INIT fp@427: || jiffies - slave->jiffies_preop < EC_WAIT_SDO_DICT * HZ fp@419: || !slave->online fp@419: || slave->error_flag) continue; fp@419: fp@419: if (master->debug_level) { fp@423: EC_DBG("Fetching SDO dictionary from slave %i.\n", fp@419: slave->ring_position); fp@419: } fp@419: fp@419: if (kobject_add(&slave->sdo_kobj)) { fp@419: EC_ERR("Failed to add SDO kobj of slave %i.\n", fp@419: slave->ring_position); fp@419: slave->error_flag = 1; fp@419: fsm->master_state = ec_fsm_master_start; fp@419: fsm->master_state(fsm); // execute immediately fp@419: return; fp@419: } fp@419: fp@419: slave->sdo_dictionary_fetched = 1; fp@419: fp@419: // start fetching SDO dictionary fp@419: fsm->slave = slave; fp@419: fsm->master_state = ec_fsm_master_sdodict; fp@436: ec_fsm_coe_dictionary(&fsm->fsm_coe, slave); fp@436: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@419: return; fp@419: } fp@419: fp@306: if (master->mode == EC_MASTER_MODE_IDLE) { fp@304: // nothing to configure. 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@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@304: // nothing to do. restart master state machine. fp@304: fsm->master_state = ec_fsm_master_start; fp@304: fsm->master_state(fsm); // execute immediately 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@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately 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@260: EC_INFO("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@403: char cur_state[EC_STATE_STRING_SIZE]; fp@260: slave->online = 1; fp@291: slave->error_flag = 0; // clear error flag fp@260: slave->current_state = new_state; fp@325: ec_state_string(slave->current_state, cur_state); fp@325: EC_INFO("Slave %i: online (%s).\n", slave->ring_position, cur_state); fp@260: } fp@260: else if (new_state != slave->current_state) { fp@403: char old_state[EC_STATE_STRING_SIZE], cur_state[EC_STATE_STRING_SIZE]; fp@325: ec_state_string(slave->current_state, old_state); fp@325: ec_state_string(new_state, cur_state); fp@325: EC_INFO("Slave %i: %s -> %s.\n", fp@325: slave->ring_position, old_state, cur_state); fp@260: slave->current_state = new_state; fp@260: } fp@260: 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@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@238: fp@433: if (EC_READ_U32(fsm->fsm_sii.value) != slave->sii_vendor_id) { fp@260: EC_ERR("Slave %i: invalid vendor ID!\n", slave->ring_position); fp@238: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately 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@260: EC_INFO("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@304: fsm->master_state = ec_fsm_master_start; fp@304: fsm->master_state(fsm); // execute immediately 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@304: fsm->master_state = ec_fsm_master_start; fp@304: fsm->master_state(fsm); // execute immediately 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@325: if (fsm->slave_state != ec_fsm_end fp@325: && fsm->slave_state != ec_fsm_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@428: slave->requested_state = EC_SLAVE_STATE_PREOP; fp@325: } fp@325: fp@325: fsm->master_state = ec_fsm_master_start; fp@325: fsm->master_state(fsm); // execute immediately 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@325: if (fsm->slave_state != ec_fsm_end fp@325: && fsm->slave_state != ec_fsm_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@269: fsm->master_state = ec_fsm_master_start; fp@269: fsm->master_state(fsm); // execute immediately 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@426: fsm->master_state = ec_fsm_master_start; fp@426: fsm->master_state(fsm); // execute immediately 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@430: wake_up_interruptible(&master->sdo_wait_queue); fp@430: fsm->master_state = ec_fsm_master_start; fp@430: fsm->master_state(fsm); // execute immediately fp@430: return; fp@430: } fp@430: fp@430: // SDO dictionary fetching finished fp@430: fp@430: request->return_code = 1; fp@430: wake_up_interruptible(&master->sdo_wait_queue); 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@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@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_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@292: EC_WARN("Slave %i has state error bit set (0x%02X)!\n", fp@255: slave->ring_position, slave->current_state); 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@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_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@266: EC_WARN("Unknown category type 0x%04X in slave %i.\n", fp@266: cat_type, slave->ring_position); fp@266: } fp@266: fp@266: cat_word += cat_size + 2; fp@266: } fp@266: fp@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_error; fp@308: } fp@308: fp@308: /****************************************************************************** fp@325: * slave configuration state machine fp@308: *****************************************************************************/ fp@251: fp@251: /** fp@409: Slave configuration state: INIT. fp@309: */ fp@309: fp@309: void ec_fsm_slaveconf_init(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@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@325: fsm->slave_state = ec_fsm_error; fp@325: return; fp@325: } fp@325: fp@421: slave->configured = 1; fp@421: fp@409: if (master->debug_level) { fp@409: EC_DBG("Slave %i is now in INIT.\n", slave->ring_position); fp@409: } fp@409: fp@251: // slave is now in INIT fp@251: if (slave->current_state == slave->requested_state) { fp@325: fsm->slave_state = ec_fsm_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: // check and reset CRC fault counters fp@251: //ec_slave_check_crc(slave); fp@325: // TODO: Implement state machine for CRC checking. fp@251: fp@251: if (!slave->base_sync_count) { // no sync managers fp@308: fsm->slave_state = ec_fsm_slaveconf_preop; fp@434: ec_fsm_change(&fsm->fsm_change, slave, EC_SLAVE_STATE_PREOP); fp@434: ec_fsm_change_exec(&fsm->fsm_change); // execute immediately 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@420: fsm->slave_state = ec_fsm_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@309: fsm->slave_state = ec_fsm_slaveconf_sync; fp@309: } fp@309: fp@309: /*****************************************************************************/ fp@309: fp@309: /** fp@409: Slave configuration state: SYNC. fp@309: */ fp@309: fp@309: void ec_fsm_slaveconf_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@325: fsm->slave_state = ec_fsm_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@309: fsm->slave_state = ec_fsm_slaveconf_preop; fp@434: ec_fsm_change(&fsm->fsm_change, 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@309: void ec_fsm_slaveconf_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@325: fsm->slave_state = ec_fsm_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@419: fsm->slave_state = ec_fsm_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@338: fsm->slave_state = ec_fsm_slaveconf_saveop; fp@434: ec_fsm_change(&fsm->fsm_change, slave, EC_SLAVE_STATE_SAVEOP); fp@434: ec_fsm_change_exec(&fsm->fsm_change); // execute immediately fp@338: return; fp@338: } fp@409: fp@409: // start SDO configuration fp@338: fsm->slave_state = ec_fsm_slaveconf_sdoconf; fp@436: fsm->sdodata = list_entry(slave->sdo_confs.next, ec_sdo_data_t, list); fp@436: ec_fsm_coe_download(&fsm->fsm_coe, slave, fsm->sdodata); fp@436: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately 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@309: fsm->slave_state = ec_fsm_slaveconf_fmmu; fp@309: } fp@309: fp@309: /*****************************************************************************/ fp@309: fp@309: /** fp@409: Slave configuration state: FMMU. fp@309: */ fp@309: fp@309: void ec_fsm_slaveconf_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@325: fsm->slave_state = ec_fsm_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@419: // set state to SAVEOP fp@419: fsm->slave_state = ec_fsm_slaveconf_saveop; fp@434: ec_fsm_change(&fsm->fsm_change, slave, EC_SLAVE_STATE_SAVEOP); fp@434: ec_fsm_change_exec(&fsm->fsm_change); // execute immediately fp@419: return; fp@419: } fp@419: fp@419: // start SDO configuration fp@419: fsm->slave_state = ec_fsm_slaveconf_sdoconf; fp@436: fsm->sdodata = list_entry(slave->sdo_confs.next, ec_sdo_data_t, list); fp@436: ec_fsm_coe_download(&fsm->fsm_coe, 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@419: void ec_fsm_slaveconf_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@419: fsm->slave_state = ec_fsm_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@309: fsm->slave_state = ec_fsm_slaveconf_saveop; fp@434: ec_fsm_change(&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@309: void ec_fsm_slaveconf_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@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_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@309: fsm->slave_state = ec_fsm_slaveconf_op; fp@434: ec_fsm_change(&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@309: void ec_fsm_slaveconf_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@325: fsm->slave_state = ec_fsm_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@325: fsm->slave_state = ec_fsm_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@325: void ec_fsm_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@325: void ec_fsm_end(ec_fsm_t *fsm /**< finite state machine */) fp@325: { fp@325: } fp@325: fp@325: /*****************************************************************************/