fp@1619: /****************************************************************************** fp@1619: * fp@1619: * $Id$ fp@1619: * fp@1619: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1619: * fp@1619: * This file is part of the IgH EtherCAT Master. fp@1619: * fp@1619: * The IgH EtherCAT Master is free software; you can redistribute it fp@1619: * and/or modify it under the terms of the GNU General Public License fp@1619: * as published by the Free Software Foundation; either version 2 of the fp@1619: * License, or (at your option) any later version. fp@1619: * fp@1619: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1619: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1619: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1619: * GNU General Public License for more details. fp@1619: * fp@1619: * You should have received a copy of the GNU General Public License fp@1619: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1619: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1619: * fp@1619: * The right to use EtherCAT Technology is granted and comes free of fp@1619: * charge under condition of compatibility of product made by fp@1619: * Licensee. People intending to distribute/sell products based on the fp@1619: * code, have to sign an agreement to guarantee that products using fp@1619: * software based on IgH EtherCAT master stay compatible with the actual fp@1619: * EtherCAT specification (which are released themselves as an open fp@1619: * standard) as the (only) precondition to have the right to use EtherCAT fp@1619: * Technology, IP and trade marks. fp@1619: * fp@1619: *****************************************************************************/ fp@1619: fp@1619: /** fp@1619: \file fp@1619: EtherCAT finite state machines. fp@1619: */ fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: #include "globals.h" fp@1619: #include "fsm.h" fp@1619: #include "master.h" fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: void ec_fsm_master_start(ec_fsm_t *); fp@1619: void ec_fsm_master_broadcast(ec_fsm_t *); fp@1624: void ec_fsm_master_read_states(ec_fsm_t *); fp@1619: void ec_fsm_master_validate_vendor(ec_fsm_t *); fp@1619: void ec_fsm_master_validate_product(ec_fsm_t *); fp@1624: void ec_fsm_master_rewrite_addresses(ec_fsm_t *); fp@1624: void ec_fsm_master_configure_slave(ec_fsm_t *); fp@1624: void ec_fsm_master_scan_slaves(ec_fsm_t *); fp@1624: void ec_fsm_master_write_eeprom(ec_fsm_t *); fp@1624: fp@1624: void ec_fsm_slavescan_start(ec_fsm_t *); fp@1624: void ec_fsm_slavescan_address(ec_fsm_t *); fp@1624: void ec_fsm_slavescan_state(ec_fsm_t *); fp@1624: void ec_fsm_slavescan_base(ec_fsm_t *); fp@1624: void ec_fsm_slavescan_datalink(ec_fsm_t *); fp@1624: void ec_fsm_slavescan_eeprom_size(ec_fsm_t *); fp@1624: void ec_fsm_slavescan_eeprom_data(ec_fsm_t *); fp@1624: void ec_fsm_slavescan_end(ec_fsm_t *); fp@1624: fp@1624: void ec_fsm_slaveconf_init(ec_fsm_t *); fp@1624: void ec_fsm_slaveconf_sync(ec_fsm_t *); fp@1624: void ec_fsm_slaveconf_preop(ec_fsm_t *); fp@1624: void ec_fsm_slaveconf_fmmu(ec_fsm_t *); fp@1624: void ec_fsm_slaveconf_saveop(ec_fsm_t *); fp@1624: void ec_fsm_slaveconf_op(ec_fsm_t *); fp@1624: void ec_fsm_slaveconf_end(ec_fsm_t *); fp@1619: fp@1619: void ec_fsm_sii_start_reading(ec_fsm_t *); fp@1621: void ec_fsm_sii_read_check(ec_fsm_t *); fp@1621: void ec_fsm_sii_read_fetch(ec_fsm_t *); fp@1621: void ec_fsm_sii_start_writing(ec_fsm_t *); fp@1621: void ec_fsm_sii_write_check(ec_fsm_t *); fp@1621: void ec_fsm_sii_write_check2(ec_fsm_t *); fp@1619: void ec_fsm_sii_end(ec_fsm_t *); fp@1619: void ec_fsm_sii_error(ec_fsm_t *); fp@1619: fp@1619: void ec_fsm_change_start(ec_fsm_t *); fp@1619: void ec_fsm_change_check(ec_fsm_t *); fp@1619: void ec_fsm_change_status(ec_fsm_t *); fp@1619: void ec_fsm_change_code(ec_fsm_t *); fp@1619: void ec_fsm_change_ack(ec_fsm_t *); fp@1624: void ec_fsm_change_check_ack(ec_fsm_t *); fp@1619: void ec_fsm_change_end(ec_fsm_t *); fp@1619: void ec_fsm_change_error(ec_fsm_t *); fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Constructor. fp@1619: */ fp@1619: fp@1619: int ec_fsm_init(ec_fsm_t *fsm, /**< finite state machine */ fp@1619: ec_master_t *master /**< EtherCAT master */ fp@1624: ) fp@1619: { fp@1619: fsm->master = master; fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_slaves_responding = 0; fp@1619: fsm->master_slave_states = EC_SLAVE_STATE_UNKNOWN; fp@1619: fsm->master_validation = 0; fp@1619: fp@1624: ec_datagram_init(&fsm->datagram); fp@1624: if (ec_datagram_prealloc(&fsm->datagram, EC_MAX_DATA_SIZE)) { fp@1624: EC_ERR("Failed to allocate FSM datagram.\n"); fp@1619: return -1; fp@1619: } fp@1619: fp@1619: return 0; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Destructor. fp@1619: */ fp@1619: fp@1619: void ec_fsm_clear(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1624: ec_datagram_clear(&fsm->datagram); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Resets the state machine. fp@1619: */ fp@1619: fp@1619: void ec_fsm_reset(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_slaves_responding = 0; fp@1619: fsm->master_slave_states = EC_SLAVE_STATE_UNKNOWN; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Executes the current state of the state machine. fp@1619: */ fp@1619: fp@1619: void ec_fsm_execute(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: fsm->master_state(fsm); fp@1619: } fp@1619: fp@1619: /****************************************************************************** fp@1619: * master state machine fp@1619: *****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master state: START. fp@1619: Starts with getting slave count and slave states. fp@1619: */ fp@1619: fp@1619: void ec_fsm_master_start(ec_fsm_t *fsm) fp@1619: { fp@1624: ec_datagram_brd(&fsm->datagram, 0x0130, 2); fp@1624: ec_master_queue_datagram(fsm->master, &fsm->datagram); fp@1619: fsm->master_state = ec_fsm_master_broadcast; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master state: BROADCAST. fp@1619: Processes the broadcast read slave count and slaves states. fp@1619: */ fp@1619: fp@1619: void ec_fsm_master_broadcast(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: unsigned int topology_change, states_change, i; fp@1619: ec_slave_t *slave; fp@1619: ec_master_t *master = fsm->master; fp@1619: fp@1624: if (datagram->state != EC_CMD_RECEIVED) { fp@1619: if (!master->device->link_state) { fp@1619: fsm->master_slaves_responding = 0; fp@1619: list_for_each_entry(slave, &master->slaves, list) { fp@1619: slave->online = 0; fp@1619: } fp@1619: } fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1624: topology_change = (datagram->working_counter != fp@1619: fsm->master_slaves_responding); fp@1624: states_change = (EC_READ_U8(datagram->data) != fsm->master_slave_states); fp@1624: fp@1624: fsm->master_slave_states = EC_READ_U8(datagram->data); fp@1624: fsm->master_slaves_responding = datagram->working_counter; fp@1619: fp@1619: if (topology_change) { fp@1619: EC_INFO("%i slave%s responding.\n", fp@1619: fsm->master_slaves_responding, fp@1619: fsm->master_slaves_responding == 1 ? "" : "s"); fp@1619: fp@1619: if (master->mode == EC_MASTER_MODE_RUNNING) { fp@1619: if (fsm->master_slaves_responding == master->slave_count) { fp@1619: fsm->master_validation = 1; // start validation later fp@1619: } fp@1619: else { fp@1619: EC_WARN("Invalid slave count. Bus in tainted state.\n"); fp@1619: } fp@1619: } fp@1619: } fp@1619: fp@1619: if (states_change) { fp@1619: EC_INFO("Slave states: "); fp@1619: ec_print_states(fsm->master_slave_states); fp@1619: printk(".\n"); fp@1619: } fp@1619: fp@1624: // topology change in idle mode: clear all slaves and scan the bus fp@1624: if (topology_change && master->mode == EC_MASTER_MODE_IDLE) { fp@1619: EC_INFO("Scanning bus.\n"); fp@1619: fp@1619: ec_master_eoe_stop(master); fp@1619: ec_master_clear_slaves(master); fp@1619: fp@1619: if (!fsm->master_slaves_responding) { fp@1619: // no slaves present -> finish state machine. fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // init slaves fp@1619: for (i = 0; i < fsm->master_slaves_responding; i++) { fp@1619: if (!(slave = (ec_slave_t *) kmalloc(sizeof(ec_slave_t), fp@1619: GFP_ATOMIC))) { fp@1619: EC_ERR("Failed to allocate slave %i!\n", i); fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: if (ec_slave_init(slave, master, i, i + 1)) { fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: if (kobject_add(&slave->kobj)) { fp@1619: EC_ERR("Failed to add kobject.\n"); fp@1619: kobject_put(&slave->kobj); // free fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: list_add_tail(&slave->list, &master->slaves); fp@1619: } fp@1619: fp@1619: // begin scanning of slaves fp@1619: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@1624: fsm->slave_state = ec_fsm_slavescan_start; fp@1624: fsm->master_state = ec_fsm_master_scan_slaves; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // fetch state from each slave fp@1619: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@1624: ec_datagram_nprd(&fsm->datagram, fsm->slave->station_address, 0x0130, 2); fp@1624: ec_master_queue_datagram(master, &fsm->datagram); fp@1624: fsm->master_state = ec_fsm_master_read_states; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Master action: PROC_STATES. fp@1624: Processes the slave states. fp@1624: */ fp@1624: fp@1624: void ec_fsm_master_action_process_states(ec_fsm_t *fsm fp@1624: /**< finite state machine */ fp@1624: ) fp@1624: { fp@1624: ec_master_t *master = fsm->master; fp@1624: ec_slave_t *slave; fp@1624: fp@1624: // check if any slaves are not in the state, they're supposed to be fp@1624: list_for_each_entry(slave, &master->slaves, list) { fp@1624: if (slave->error_flag || fp@1624: !slave->online || fp@1624: slave->requested_state == EC_SLAVE_STATE_UNKNOWN || fp@1624: slave->current_state == slave->requested_state) continue; fp@1624: fp@1624: EC_INFO("Changing state of slave %i from ", slave->ring_position); fp@1624: ec_print_states(slave->current_state); fp@1624: printk(" to "); fp@1624: ec_print_states(slave->requested_state); fp@1624: printk(".\n"); fp@1624: fp@1624: fsm->slave = slave; fp@1624: fsm->slave_state = ec_fsm_slaveconf_init; fp@1624: fsm->change_new = EC_SLAVE_STATE_INIT; fp@1624: fsm->change_state = ec_fsm_change_start; fp@1624: fsm->master_state = ec_fsm_master_configure_slave; fp@1624: fsm->master_state(fsm); // execute immediately fp@1624: return; fp@1624: } fp@1624: fp@1624: if (master->mode == EC_MASTER_MODE_IDLE) { fp@1624: // nothing to configure. check for pending EEPROM write operations. fp@1624: list_for_each_entry(slave, &master->slaves, list) { fp@1624: if (!slave->new_eeprom_data) continue; fp@1624: fp@1624: if (!slave->online || slave->error_flag) { fp@1624: kfree(slave->new_eeprom_data); fp@1624: slave->new_eeprom_data = NULL; fp@1624: EC_ERR("Discarding EEPROM data, slave %i not ready.\n", fp@1624: slave->ring_position); fp@1624: continue; fp@1624: } fp@1624: fp@1624: // found pending EEPROM write operation. execute it! fp@1624: EC_INFO("Writing EEPROM of slave %i...\n", slave->ring_position); fp@1624: fsm->sii_offset = 0x0000; fp@1624: memcpy(fsm->sii_value, slave->new_eeprom_data, 2); fp@1624: fsm->sii_mode = 1; fp@1624: fsm->sii_state = ec_fsm_sii_start_writing; fp@1624: fsm->slave = slave; fp@1624: fsm->master_state = ec_fsm_master_write_eeprom; fp@1624: fsm->master_state(fsm); // execute immediately fp@1624: return; fp@1624: } fp@1624: } fp@1624: fp@1624: // nothing to do. restart master state machine. fp@1624: fsm->master_state = ec_fsm_master_start; fp@1624: fsm->master_state(fsm); // execute immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master action: Get state of next slave. fp@1619: */ fp@1619: fp@1619: void ec_fsm_master_action_next_slave_state(ec_fsm_t *fsm fp@1619: /**< finite state machine */) fp@1619: { fp@1619: ec_master_t *master = fsm->master; fp@1619: ec_slave_t *slave = fsm->slave; fp@1619: fp@1624: // is there another slave to query? fp@1624: if (slave->list.next != &master->slaves) { fp@1624: // process next slave fp@1624: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@1624: ec_datagram_nprd(&fsm->datagram, fsm->slave->station_address, fp@1624: 0x0130, 2); fp@1624: ec_master_queue_datagram(master, &fsm->datagram); fp@1624: fsm->master_state = ec_fsm_master_read_states; fp@1624: return; fp@1624: } fp@1624: fp@1624: // all slave states read fp@1624: fp@1624: // check, if a bus validation has to be done fp@1624: if (fsm->master_validation) { fp@1624: fsm->master_validation = 0; fp@1624: list_for_each_entry(slave, &master->slaves, list) { fp@1624: if (slave->online) continue; fp@1624: fp@1624: // At least one slave is offline. validate! fp@1624: EC_INFO("Validating bus.\n"); fp@1624: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@1624: fsm->master_state = ec_fsm_master_validate_vendor; fp@1624: fsm->sii_offset = 0x0008; // vendor ID fp@1624: fsm->sii_mode = 0; fp@1624: fsm->sii_state = ec_fsm_sii_start_reading; fp@1624: fsm->sii_state(fsm); // execute immediately fp@1624: return; fp@1619: } fp@1624: } fp@1624: fp@1624: ec_fsm_master_action_process_states(fsm); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master state: STATES. fp@1619: Fetches the AL- and online state of a slave. fp@1619: */ fp@1619: fp@1624: void ec_fsm_master_read_states(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: uint8_t new_state; fp@1619: fp@1624: if (datagram->state != EC_CMD_RECEIVED) { fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // did the slave not respond to its station address? fp@1624: if (datagram->working_counter != 1) { fp@1619: if (slave->online) { fp@1619: slave->online = 0; fp@1619: EC_INFO("Slave %i: offline.\n", slave->ring_position); fp@1619: } fp@1619: ec_fsm_master_action_next_slave_state(fsm); fp@1619: return; fp@1619: } fp@1619: fp@1619: // slave responded fp@1624: new_state = EC_READ_U8(datagram->data); fp@1619: if (!slave->online) { // slave was offline before fp@1619: slave->online = 1; fp@1624: slave->error_flag = 0; // clear error flag fp@1619: slave->current_state = new_state; fp@1619: EC_INFO("Slave %i: online (", slave->ring_position); fp@1619: ec_print_states(new_state); fp@1619: printk(").\n"); fp@1619: } fp@1619: else if (new_state != slave->current_state) { fp@1619: EC_INFO("Slave %i: ", slave->ring_position); fp@1619: ec_print_states(slave->current_state); fp@1619: printk(" -> "); fp@1619: ec_print_states(new_state); fp@1619: printk(".\n"); fp@1619: slave->current_state = new_state; fp@1619: } fp@1619: fp@1619: ec_fsm_master_action_next_slave_state(fsm); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master state: VALIDATE_VENDOR. fp@1619: Validates the vendor ID of a slave. fp@1619: */ fp@1619: fp@1619: void ec_fsm_master_validate_vendor(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: ec_slave_t *slave = fsm->slave; fp@1619: fp@1619: fsm->sii_state(fsm); // execute SII state machine fp@1619: fp@1619: if (fsm->sii_state == ec_fsm_sii_error) { fp@1624: fsm->slave->error_flag = 1; fp@1619: EC_ERR("Failed to validate vendor ID of slave %i.\n", fp@1619: slave->ring_position); fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: if (fsm->sii_state != ec_fsm_sii_end) return; fp@1619: fp@1621: if (EC_READ_U32(fsm->sii_value) != slave->sii_vendor_id) { fp@1619: EC_ERR("Slave %i: invalid vendor ID!\n", slave->ring_position); fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // vendor ID is ok. check product code. fp@1619: fsm->master_state = ec_fsm_master_validate_product; fp@1619: fsm->sii_offset = 0x000A; // product code fp@1619: fsm->sii_mode = 0; fp@1619: fsm->sii_state = ec_fsm_sii_start_reading; fp@1619: fsm->sii_state(fsm); // execute immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1624: Master action: ADDRESS. fp@1619: Looks for slave, that have lost their configuration and writes fp@1619: their station address, so that they can be reconfigured later. fp@1619: */ fp@1619: fp@1624: void ec_fsm_master_action_addresses(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: fp@1619: while (fsm->slave->online) { fp@1619: if (fsm->slave->list.next == &fsm->master->slaves) { // last slave? fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: // check next slave fp@1619: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@1619: } fp@1619: fp@1619: EC_INFO("Reinitializing slave %i.\n", fsm->slave->ring_position); fp@1619: fp@1619: // write station address fp@1624: ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x0010, 2); fp@1624: EC_WRITE_U16(datagram->data, fsm->slave->station_address); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: fsm->master_state = ec_fsm_master_rewrite_addresses; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Master state: VALIDATE_PRODUCT. fp@1624: Validates the product ID of a slave. fp@1624: */ fp@1624: fp@1624: void ec_fsm_master_validate_product(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: fp@1624: fsm->sii_state(fsm); // execute SII state machine fp@1624: fp@1624: if (fsm->sii_state == ec_fsm_sii_error) { fp@1624: fsm->slave->error_flag = 1; fp@1624: EC_ERR("Failed to validate product code of slave %i.\n", fp@1624: slave->ring_position); fp@1624: fsm->master_state = ec_fsm_master_start; fp@1624: fsm->master_state(fsm); // execute immediately fp@1624: return; fp@1624: } fp@1624: fp@1624: if (fsm->sii_state != ec_fsm_sii_end) return; fp@1624: fp@1624: if (EC_READ_U32(fsm->sii_value) != slave->sii_product_code) { fp@1624: EC_ERR("Slave %i: invalid product code!\n", slave->ring_position); fp@1624: EC_ERR("expected 0x%08X, got 0x%08X.\n", slave->sii_product_code, fp@1624: EC_READ_U32(fsm->sii_value)); fp@1624: fsm->master_state = ec_fsm_master_start; fp@1624: fsm->master_state(fsm); // execute immediately fp@1624: return; fp@1624: } fp@1624: fp@1624: // have all states been validated? fp@1624: if (slave->list.next == &fsm->master->slaves) { fp@1624: fsm->slave = list_entry(fsm->master->slaves.next, ec_slave_t, list); fp@1624: // start writing addresses to offline slaves fp@1624: ec_fsm_master_action_addresses(fsm); fp@1624: return; fp@1624: } fp@1624: fp@1624: // validate next slave fp@1624: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@1624: fsm->master_state = ec_fsm_master_validate_vendor; fp@1624: fsm->sii_offset = 0x0008; // vendor ID fp@1624: fsm->sii_mode = 0; fp@1624: fsm->sii_state = ec_fsm_sii_start_reading; fp@1624: fsm->sii_state(fsm); // execute immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master state: ADDRESS. fp@1619: Checks, if the new station address has been written to the slave. fp@1619: */ fp@1619: fp@1624: void ec_fsm_master_rewrite_addresses(ec_fsm_t *fsm fp@1624: /**< finite state machine */ fp@1624: ) fp@1624: { fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1619: EC_ERR("Failed to write station address on slave %i.\n", fp@1619: slave->ring_position); fp@1619: } fp@1619: fp@1619: if (fsm->slave->list.next == &fsm->master->slaves) { // last slave? fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // check next slave fp@1619: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@1624: // Write new station address to slave fp@1624: ec_fsm_master_action_addresses(fsm); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master state: SCAN. fp@1619: Executes the sub-statemachine for the scanning of a slave. fp@1619: */ fp@1619: fp@1624: void ec_fsm_master_scan_slaves(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: ec_master_t *master = fsm->master; fp@1619: ec_slave_t *slave = fsm->slave; fp@1619: uint16_t coupler_index, coupler_subindex; fp@1619: uint16_t reverse_coupler_index, current_coupler_index; fp@1619: ec_slave_ident_t *ident; fp@1619: fp@1619: fsm->slave_state(fsm); // execute slave state machine fp@1619: fp@1624: if (fsm->slave_state != ec_fsm_slavescan_end) return; fp@1619: fp@1619: // have all slaves been fetched? fp@1619: if (slave->list.next == &master->slaves) { fp@1619: EC_INFO("Bus scanning completed.\n"); fp@1619: fp@1619: // identify all slaves and calculate coupler addressing fp@1619: fp@1619: coupler_index = 0; fp@1619: reverse_coupler_index = 0xFFFF; fp@1619: current_coupler_index = 0x3FFF; fp@1619: coupler_subindex = 0; fp@1619: fp@1619: list_for_each_entry(slave, &master->slaves, list) fp@1619: { fp@1619: // search for identification in "database" fp@1619: ident = slave_idents; fp@1619: while (ident->type) { fp@1619: if (ident->vendor_id == slave->sii_vendor_id fp@1619: && ident->product_code == slave->sii_product_code) { fp@1619: slave->type = ident->type; fp@1619: break; fp@1619: } fp@1619: ident++; fp@1619: } fp@1619: fp@1619: if (!slave->type) { fp@1624: EC_WARN("Unknown slave device (vendor 0x%08X," fp@1619: " code 0x%08X) at position %i.\n", fp@1619: slave->sii_vendor_id, slave->sii_product_code, fp@1619: slave->ring_position); fp@1619: } fp@1619: else { fp@1619: // if the slave is a bus coupler, change adressing base fp@1619: if (slave->type->special == EC_TYPE_BUS_COUPLER) { fp@1619: if (slave->sii_alias) fp@1619: current_coupler_index = reverse_coupler_index--; fp@1619: else fp@1619: current_coupler_index = coupler_index++; fp@1619: coupler_subindex = 0; fp@1619: } fp@1619: } fp@1619: fp@1619: // determine initial state. fp@1624: if ((slave->type && fp@1624: (slave->type->special == EC_TYPE_BUS_COUPLER || fp@1624: slave->type->special == EC_TYPE_INFRA))) { fp@1619: slave->requested_state = EC_SLAVE_STATE_OP; fp@1619: } fp@1619: else { fp@1619: if (master->mode == EC_MASTER_MODE_RUNNING) fp@1619: slave->requested_state = EC_SLAVE_STATE_PREOP; fp@1619: else fp@1619: slave->requested_state = EC_SLAVE_STATE_INIT; fp@1619: } fp@1624: slave->error_flag = 0; fp@1619: fp@1619: // calculate coupler-based slave address fp@1619: slave->coupler_index = current_coupler_index; fp@1619: slave->coupler_subindex = coupler_subindex; fp@1619: coupler_subindex++; fp@1619: } fp@1619: fp@1624: if (master->mode == EC_MASTER_MODE_IDLE) { fp@1619: // start EoE processing fp@1619: ec_master_eoe_start(master); fp@1619: } fp@1619: fp@1619: fsm->master_state = ec_fsm_master_start; fp@1619: fsm->master_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // process next slave fp@1619: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@1624: fsm->slave_state = ec_fsm_slavescan_start; fp@1619: fsm->slave_state(fsm); // execute immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Master state: CONF. fp@1619: Starts configuring a slave. fp@1619: */ fp@1619: fp@1624: void ec_fsm_master_configure_slave(ec_fsm_t *fsm fp@1624: /**< finite state machine */ fp@1624: ) fp@1619: { fp@1619: fsm->slave_state(fsm); // execute slave's state machine fp@1624: if (fsm->slave_state != ec_fsm_slaveconf_end) return; fp@1624: fp@1624: ec_fsm_master_action_process_states(fsm); fp@1619: } fp@1619: fp@1621: /*****************************************************************************/ fp@1621: fp@1621: /** fp@1621: Master state: EEPROM. fp@1621: */ fp@1621: fp@1624: void ec_fsm_master_write_eeprom(ec_fsm_t *fsm /**< finite state machine */) fp@1621: { fp@1621: ec_slave_t *slave = fsm->slave; fp@1621: fp@1621: fsm->sii_state(fsm); // execute SII state machine fp@1621: fp@1621: if (fsm->sii_state == ec_fsm_sii_error) { fp@1624: fsm->slave->error_flag = 1; fp@1621: EC_ERR("Failed to write EEPROM contents to slave %i.\n", fp@1621: slave->ring_position); fp@1621: kfree(slave->new_eeprom_data); fp@1621: slave->new_eeprom_data = NULL; fp@1621: fsm->master_state = ec_fsm_master_start; fp@1621: fsm->master_state(fsm); // execute immediately fp@1621: return; fp@1621: } fp@1621: fp@1621: if (fsm->sii_state != ec_fsm_sii_end) return; fp@1621: fp@1621: fsm->sii_offset++; fp@1621: if (fsm->sii_offset < slave->new_eeprom_size) { fp@1621: memcpy(fsm->sii_value, slave->new_eeprom_data + fsm->sii_offset, 2); fp@1621: fsm->sii_state = ec_fsm_sii_start_writing; fp@1621: fsm->sii_state(fsm); // execute immediately fp@1621: return; fp@1621: } fp@1621: fp@1621: // finished writing EEPROM fp@1621: EC_INFO("Finished writing EEPROM of slave %i.\n", slave->ring_position); fp@1621: kfree(slave->new_eeprom_data); fp@1621: slave->new_eeprom_data = NULL; fp@1621: fp@1621: // restart master state machine. fp@1624: fsm->master_state = ec_fsm_master_start; // TODO: Evaluate new contents! fp@1621: fsm->master_state(fsm); // execute immediately fp@1621: return; fp@1621: } fp@1621: fp@1619: /****************************************************************************** fp@1624: * slave scan sub state machine fp@1619: *****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Slave state: START_READING. fp@1619: First state of the slave state machine. Writes the station address to the fp@1619: slave, according to its ring position. fp@1619: */ fp@1619: fp@1624: void ec_fsm_slavescan_start(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: fp@1619: // write station address fp@1624: ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x0010, 2); fp@1624: EC_WRITE_U16(datagram->data, fsm->slave->station_address); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: fsm->slave_state = ec_fsm_slavescan_address; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: ADDRESS. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slavescan_address(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1624: EC_ERR("Failed to write station address of slave %i.\n", fp@1619: fsm->slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: // Read AL state fp@1624: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x0130, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: fsm->slave_state = ec_fsm_slavescan_state; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: STATE. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slavescan_state(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1624: EC_ERR("Failed to read AL state of slave %i.\n", fp@1619: fsm->slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: slave->current_state = EC_READ_U8(datagram->data); fp@1619: if (slave->current_state & EC_ACK) { fp@1624: EC_WARN("Slave %i has state error bit set (0x%02X)!\n", fp@1619: slave->ring_position, slave->current_state); fp@1619: slave->current_state &= 0x0F; fp@1619: } fp@1619: fp@1619: // read base data fp@1624: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x0000, 6); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: fsm->slave_state = ec_fsm_slavescan_base; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: BASE. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slavescan_base(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1624: EC_ERR("Failed to read base data of slave %i.\n", fp@1619: slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: slave->base_type = EC_READ_U8 (datagram->data); fp@1624: slave->base_revision = EC_READ_U8 (datagram->data + 1); fp@1624: slave->base_build = EC_READ_U16(datagram->data + 2); fp@1624: slave->base_fmmu_count = EC_READ_U8 (datagram->data + 4); fp@1624: slave->base_sync_count = EC_READ_U8 (datagram->data + 5); fp@1619: fp@1619: if (slave->base_fmmu_count > EC_MAX_FMMUS) fp@1619: slave->base_fmmu_count = EC_MAX_FMMUS; fp@1619: fp@1619: // read data link status fp@1624: ec_datagram_nprd(datagram, slave->station_address, 0x0110, 2); fp@1624: ec_master_queue_datagram(slave->master, datagram); fp@1624: fsm->slave_state = ec_fsm_slavescan_datalink; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: DATALINK. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slavescan_datalink(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: ec_slave_t *slave = fsm->slave; fp@1619: uint16_t dl_status; fp@1619: unsigned int i; fp@1619: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1624: EC_ERR("Failed to read DL status of slave %i.\n", fp@1619: slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: dl_status = EC_READ_U16(datagram->data); fp@1619: for (i = 0; i < 4; i++) { fp@1619: slave->dl_link[i] = dl_status & (1 << (4 + i)) ? 1 : 0; fp@1619: slave->dl_loop[i] = dl_status & (1 << (8 + i * 2)) ? 1 : 0; fp@1619: slave->dl_signal[i] = dl_status & (1 << (9 + i * 2)) ? 1 : 0; fp@1619: } fp@1619: fp@1621: // Start fetching EEPROM size fp@1621: fp@1621: fsm->sii_offset = 0x0040; // first category header fp@1619: fsm->sii_mode = 1; fp@1619: fsm->sii_state = ec_fsm_sii_start_reading; fp@1624: fsm->slave_state = ec_fsm_slavescan_eeprom_size; fp@1619: fsm->slave_state(fsm); // execute state immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1624: Slave state: EEPROM_SIZE. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slavescan_eeprom_size(ec_fsm_t *fsm /**< finite state machine */) fp@1621: { fp@1621: ec_slave_t *slave = fsm->slave; fp@1621: uint16_t cat_type, cat_size; fp@1619: fp@1619: // execute SII state machine fp@1619: fsm->sii_state(fsm); fp@1619: fp@1619: if (fsm->sii_state == ec_fsm_sii_error) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1621: EC_ERR("Failed to read EEPROM size of slave %i.\n", fp@1621: slave->ring_position); fp@1619: return; fp@1619: } fp@1619: fp@1619: if (fsm->sii_state != ec_fsm_sii_end) return; fp@1619: fp@1621: cat_type = EC_READ_U16(fsm->sii_value); fp@1621: cat_size = EC_READ_U16(fsm->sii_value + 2); fp@1621: fp@1621: if (cat_type != 0xFFFF) { // not the last category fp@1621: fsm->sii_offset += cat_size + 2; fp@1621: fsm->sii_state = ec_fsm_sii_start_reading; fp@1621: fsm->sii_state(fsm); // execute state immediately fp@1621: return; fp@1621: } fp@1621: fp@1621: slave->eeprom_size = (fsm->sii_offset + 1) * 2; fp@1621: fp@1621: if (slave->eeprom_data) { fp@1621: EC_INFO("Freeing old EEPROM data on slave %i...\n", fp@1621: slave->ring_position); fp@1621: kfree(slave->eeprom_data); fp@1621: } fp@1621: fp@1621: if (!(slave->eeprom_data = fp@1621: (uint8_t *) kmalloc(slave->eeprom_size, GFP_ATOMIC))) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1621: EC_ERR("Failed to allocate EEPROM data on slave %i.\n", fp@1621: slave->ring_position); fp@1621: return; fp@1621: } fp@1621: fp@1621: // Start fetching EEPROM contents fp@1621: fp@1621: fsm->sii_offset = 0x0000; fp@1621: fsm->sii_mode = 1; fp@1619: fsm->sii_state = ec_fsm_sii_start_reading; fp@1624: fsm->slave_state = ec_fsm_slavescan_eeprom_data; fp@1619: fsm->slave_state(fsm); // execute state immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1624: Slave state: EEPROM_DATA. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slavescan_eeprom_data(ec_fsm_t *fsm /**< finite state machine */) fp@1621: { fp@1621: ec_slave_t *slave = fsm->slave; fp@1621: uint16_t *cat_word, cat_type, cat_size; fp@1621: fp@1619: // execute SII state machine fp@1619: fsm->sii_state(fsm); fp@1619: fp@1619: if (fsm->sii_state == ec_fsm_sii_error) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1621: EC_ERR("Failed to fetch EEPROM contents of slave %i.\n", fp@1621: slave->ring_position); fp@1619: return; fp@1619: } fp@1619: fp@1619: if (fsm->sii_state != ec_fsm_sii_end) return; fp@1619: fp@1621: // 2 words fetched fp@1621: fp@1621: if (fsm->sii_offset + 2 <= slave->eeprom_size / 2) { // 2 words fit fp@1621: memcpy(slave->eeprom_data + fsm->sii_offset * 2, fsm->sii_value, 4); fp@1621: } fp@1621: else { // copy the last word fp@1621: memcpy(slave->eeprom_data + fsm->sii_offset * 2, fsm->sii_value, 2); fp@1621: } fp@1621: fp@1621: if (fsm->sii_offset + 2 < slave->eeprom_size / 2) { fp@1621: // fetch the next 2 words fp@1621: fsm->sii_offset += 2; fp@1619: fsm->sii_state = ec_fsm_sii_start_reading; fp@1621: fsm->sii_state(fsm); // execute state immediately fp@1621: return; fp@1621: } fp@1621: fp@1621: // Evaluate EEPROM contents fp@1621: fp@1621: slave->sii_alias = fp@1621: EC_READ_U16(slave->eeprom_data + 2 * 0x0004); fp@1621: slave->sii_vendor_id = fp@1621: EC_READ_U32(slave->eeprom_data + 2 * 0x0008); fp@1621: slave->sii_product_code = fp@1621: EC_READ_U32(slave->eeprom_data + 2 * 0x000A); fp@1621: slave->sii_revision_number = fp@1621: EC_READ_U32(slave->eeprom_data + 2 * 0x000C); fp@1621: slave->sii_serial_number = fp@1621: EC_READ_U32(slave->eeprom_data + 2 * 0x000E); fp@1621: slave->sii_rx_mailbox_offset = fp@1621: EC_READ_U16(slave->eeprom_data + 2 * 0x0018); fp@1621: slave->sii_rx_mailbox_size = fp@1621: EC_READ_U16(slave->eeprom_data + 2 * 0x0019); fp@1621: slave->sii_tx_mailbox_offset = fp@1621: EC_READ_U16(slave->eeprom_data + 2 * 0x001A); fp@1621: slave->sii_tx_mailbox_size = fp@1621: EC_READ_U16(slave->eeprom_data + 2 * 0x001B); fp@1621: slave->sii_mailbox_protocols = fp@1621: EC_READ_U16(slave->eeprom_data + 2 * 0x001C); fp@1621: fp@1621: // evaluate category data fp@1621: cat_word = (uint16_t *) slave->eeprom_data + 0x0040; fp@1621: while (EC_READ_U16(cat_word) != 0xFFFF) { fp@1621: cat_type = EC_READ_U16(cat_word) & 0x7FFF; fp@1621: cat_size = EC_READ_U16(cat_word + 1); fp@1621: fp@1621: switch (cat_type) { fp@1621: case 0x000A: fp@1621: if (ec_slave_fetch_strings(slave, (uint8_t *) (cat_word + 2))) fp@1621: goto end; fp@1621: break; fp@1621: case 0x001E: fp@1621: if (ec_slave_fetch_general(slave, (uint8_t *) (cat_word + 2))) fp@1621: goto end; fp@1621: break; fp@1621: case 0x0028: fp@1621: break; fp@1621: case 0x0029: fp@1621: if (ec_slave_fetch_sync(slave, (uint8_t *) (cat_word + 2), fp@1621: cat_size)) fp@1621: goto end; fp@1621: break; fp@1621: case 0x0032: fp@1621: if (ec_slave_fetch_pdo(slave, (uint8_t *) (cat_word + 2), fp@1621: cat_size, EC_TX_PDO)) fp@1621: goto end; fp@1621: break; fp@1621: case 0x0033: fp@1621: if (ec_slave_fetch_pdo(slave, (uint8_t *) (cat_word + 2), fp@1621: cat_size, EC_RX_PDO)) fp@1621: goto end; fp@1621: break; fp@1621: default: fp@1621: EC_WARN("Unknown category type 0x%04X in slave %i.\n", fp@1621: cat_type, slave->ring_position); fp@1621: } fp@1621: fp@1621: cat_word += cat_size + 2; fp@1621: } fp@1621: fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1624: fp@1624: end: fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slavescan_end; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: END. fp@1624: End state of the slave state machine. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slavescan_end(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: } fp@1624: fp@1624: /****************************************************************************** fp@1624: * slave configuration sub state machine fp@1624: *****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: INIT. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slaveconf_init(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: const ec_sync_t *sync; fp@1624: ec_eeprom_sync_t *eeprom_sync, mbox_sync; fp@1624: unsigned int j; fp@1619: fp@1619: fsm->change_state(fsm); // execute state change state machine fp@1619: fp@1619: if (fsm->change_state == ec_fsm_change_error) { fp@1624: slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; fp@1619: return; fp@1619: } fp@1619: fp@1619: if (fsm->change_state != ec_fsm_change_end) return; fp@1619: fp@1619: // slave is now in INIT fp@1619: if (slave->current_state == slave->requested_state) { fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; // successful fp@1619: return; fp@1619: } fp@1619: fp@1619: // check for slave registration fp@1619: if (!slave->type) { fp@1619: EC_WARN("Slave %i has unknown type!\n", slave->ring_position); fp@1619: } fp@1619: fp@1619: // check and reset CRC fault counters fp@1619: //ec_slave_check_crc(slave); fp@1624: // TODO! fp@1619: fp@1619: if (!slave->base_sync_count) { // no sync managers fp@1624: fsm->slave_state = ec_fsm_slaveconf_preop; fp@1624: fsm->change_new = EC_SLAVE_STATE_PREOP; fp@1624: fsm->change_state = ec_fsm_change_start; fp@1624: fsm->change_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // configure sync managers fp@1624: ec_datagram_npwr(datagram, slave->station_address, 0x0800, fp@1624: EC_SYNC_SIZE * slave->base_sync_count); fp@1624: memset(datagram->data, 0x00, EC_SYNC_SIZE * slave->base_sync_count); fp@1624: fp@1624: // does the slave supply sync manager configurations in its EEPROM? fp@1624: if (!list_empty(&slave->eeprom_syncs)) { fp@1624: list_for_each_entry(eeprom_sync, &slave->eeprom_syncs, list) { fp@1624: if (eeprom_sync->index >= slave->base_sync_count) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; fp@1624: EC_ERR("Invalid sync manager configuration found!"); fp@1624: return; fp@1624: } fp@1624: ec_eeprom_sync_config(eeprom_sync, slave, fp@1624: datagram->data + EC_SYNC_SIZE fp@1624: * eeprom_sync->index); fp@1624: } fp@1624: } fp@1619: fp@1619: // known slave type, take type's SM information fp@1624: else if (slave->type) { fp@1619: for (j = 0; slave->type->sync_managers[j] && j < EC_MAX_SYNC; j++) { fp@1619: sync = slave->type->sync_managers[j]; fp@1624: ec_sync_config(sync, slave, datagram->data + EC_SYNC_SIZE * j); fp@1619: } fp@1619: } fp@1619: fp@1619: // unknown type, but slave has mailbox fp@1619: else if (slave->sii_mailbox_protocols) fp@1619: { fp@1624: // guess mailbox settings fp@1624: mbox_sync.physical_start_address = fp@1624: slave->sii_rx_mailbox_offset; fp@1624: mbox_sync.length = slave->sii_rx_mailbox_size; fp@1624: mbox_sync.control_register = 0x26; fp@1624: mbox_sync.enable = 1; fp@1624: ec_eeprom_sync_config(&mbox_sync, slave, datagram->data); fp@1624: fp@1624: mbox_sync.physical_start_address = fp@1624: slave->sii_tx_mailbox_offset; fp@1624: mbox_sync.length = slave->sii_tx_mailbox_size; fp@1624: mbox_sync.control_register = 0x22; fp@1624: mbox_sync.enable = 1; fp@1624: ec_eeprom_sync_config(&mbox_sync, slave, fp@1624: datagram->data + EC_SYNC_SIZE); fp@1619: fp@1619: EC_INFO("Mailbox configured for unknown slave %i\n", fp@1619: slave->ring_position); fp@1619: } fp@1619: fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: fsm->slave_state = ec_fsm_slaveconf_sync; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: SYNC. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slaveconf_sync(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; fp@1619: EC_ERR("Failed to set sync managers on slave %i.\n", fp@1619: slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: fsm->slave_state = ec_fsm_slaveconf_preop; fp@1619: fsm->change_new = EC_SLAVE_STATE_PREOP; fp@1619: fsm->change_state = ec_fsm_change_start; fp@1619: fsm->change_state(fsm); // execute immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1624: Slave state: PREOP. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slaveconf_preop(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: ec_slave_t *slave = fsm->slave; fp@1619: ec_master_t *master = fsm->master; fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: unsigned int j; fp@1619: fp@1619: fsm->change_state(fsm); // execute state change state machine fp@1619: fp@1619: if (fsm->change_state == ec_fsm_change_error) { fp@1624: slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; fp@1619: return; fp@1619: } fp@1619: fp@1619: if (fsm->change_state != ec_fsm_change_end) return; fp@1619: fp@1619: // slave is now in PREOP fp@1619: if (slave->current_state == slave->requested_state) { fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; // successful fp@1619: return; fp@1619: } fp@1619: fp@1619: // stop activation here for slaves without type fp@1619: if (!slave->type) { fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; // successful fp@1619: return; fp@1619: } fp@1619: fp@1619: if (!slave->base_fmmu_count) { fp@1624: fsm->slave_state = ec_fsm_slaveconf_saveop; fp@1624: fsm->change_new = EC_SLAVE_STATE_SAVEOP; fp@1624: fsm->change_state = ec_fsm_change_start; fp@1624: fsm->change_state(fsm); // execute immediately fp@1619: return; fp@1619: } fp@1619: fp@1619: // configure FMMUs fp@1624: ec_datagram_npwr(datagram, slave->station_address, fp@1624: 0x0600, EC_FMMU_SIZE * slave->base_fmmu_count); fp@1624: memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count); fp@1619: for (j = 0; j < slave->fmmu_count; j++) { fp@1621: ec_fmmu_config(&slave->fmmus[j], slave, fp@1624: datagram->data + EC_FMMU_SIZE * j); fp@1624: } fp@1624: fp@1624: ec_master_queue_datagram(master, datagram); fp@1624: fsm->slave_state = ec_fsm_slaveconf_fmmu; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Slave state: FMMU. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slaveconf_fmmu(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; fp@1624: EC_ERR("Failed to set FMMUs on slave %i.\n", fp@1619: fsm->slave->ring_position); fp@1619: return; fp@1619: } fp@1619: fp@1619: // set state to SAVEOP fp@1624: fsm->slave_state = ec_fsm_slaveconf_saveop; fp@1619: fsm->change_new = EC_SLAVE_STATE_SAVEOP; fp@1619: fsm->change_state = ec_fsm_change_start; fp@1619: fsm->change_state(fsm); // execute immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1624: Slave state: SAVEOP. fp@1624: */ fp@1624: fp@1624: void ec_fsm_slaveconf_saveop(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: fsm->change_state(fsm); // execute state change state machine fp@1619: fp@1619: if (fsm->change_state == ec_fsm_change_error) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; fp@1619: return; fp@1619: } fp@1619: fp@1619: if (fsm->change_state != ec_fsm_change_end) return; fp@1619: fp@1619: // slave is now in SAVEOP fp@1619: if (fsm->slave->current_state == fsm->slave->requested_state) { fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; // successful fp@1619: return; fp@1619: } fp@1619: fp@1619: // set state to OP fp@1624: fsm->slave_state = ec_fsm_slaveconf_op; fp@1619: fsm->change_new = EC_SLAVE_STATE_OP; fp@1619: fsm->change_state = ec_fsm_change_start; fp@1619: fsm->change_state(fsm); // execute immediately fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1624: Slave state: OP fp@1624: */ fp@1624: fp@1624: void ec_fsm_slaveconf_op(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: fsm->change_state(fsm); // execute state change state machine fp@1619: fp@1619: if (fsm->change_state == ec_fsm_change_error) { fp@1624: fsm->slave->error_flag = 1; fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; fp@1619: return; fp@1619: } fp@1619: fp@1619: if (fsm->change_state != ec_fsm_change_end) return; fp@1619: fp@1619: // slave is now in OP fp@1624: fsm->slave_state = ec_fsm_slaveconf_end; // successful fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Slave state: END. fp@1619: End state of the slave state machine. fp@1619: */ fp@1619: fp@1624: void ec_fsm_slaveconf_end(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: } fp@1619: fp@1619: /****************************************************************************** fp@1624: * SII sub state machine fp@1619: *****************************************************************************/ fp@1619: fp@1619: /** fp@1619: SII state: START_READING. fp@1619: Starts reading the slave information interface. fp@1619: */ fp@1619: fp@1619: void ec_fsm_sii_start_reading(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: fp@1619: // initiate read operation fp@1619: if (fsm->sii_mode) { fp@1624: ec_datagram_npwr(datagram, fsm->slave->station_address, 0x502, 4); fp@1619: } fp@1619: else { fp@1624: ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x502, 4); fp@1624: } fp@1624: fp@1624: EC_WRITE_U8 (datagram->data, 0x00); // read-only access fp@1624: EC_WRITE_U8 (datagram->data + 1, 0x01); // request read operation fp@1624: EC_WRITE_U16(datagram->data + 2, fsm->sii_offset); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1621: fsm->sii_state = ec_fsm_sii_read_check; fp@1621: } fp@1621: fp@1621: /*****************************************************************************/ fp@1621: fp@1621: /** fp@1621: SII state: READ_CHECK. fp@1624: Checks, if the SII-read-datagram has been sent and issues a fetch datagram. fp@1619: */ fp@1619: fp@1621: void ec_fsm_sii_read_check(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: EC_ERR("SII: Reception of read datagram failed.\n"); fp@1619: fsm->sii_state = ec_fsm_sii_error; fp@1619: return; fp@1619: } fp@1619: fp@1619: fsm->sii_start = get_cycles(); fp@1619: fp@1624: // issue check/fetch datagram fp@1619: if (fsm->sii_mode) { fp@1624: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 10); fp@1619: } fp@1619: else { fp@1624: ec_datagram_aprd(datagram, fsm->slave->ring_position, 0x502, 10); fp@1624: } fp@1624: fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1621: fsm->sii_state = ec_fsm_sii_read_fetch; fp@1621: } fp@1621: fp@1621: /*****************************************************************************/ fp@1621: fp@1621: /** fp@1621: SII state: READ_FETCH. fp@1624: Fetches the result of an SII-read datagram. fp@1619: */ fp@1619: fp@1621: void ec_fsm_sii_read_fetch(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: EC_ERR("SII: Reception of check/fetch datagram failed.\n"); fp@1619: fsm->sii_state = ec_fsm_sii_error; fp@1619: return; fp@1619: } fp@1619: fp@1619: // check "busy bit" fp@1624: if (EC_READ_U8(datagram->data + 1) & 0x81) { fp@1619: // still busy... timeout? fp@1619: if (get_cycles() - fsm->sii_start >= (cycles_t) 10 * cpu_khz) { fp@1619: EC_ERR("SII: Timeout.\n"); fp@1619: fsm->sii_state = ec_fsm_sii_error; fp@1619: #if 0 fp@1619: EC_DBG("SII busy: %02X %02X %02X %02X\n", fp@1624: EC_READ_U8(datagram->data + 0), fp@1624: EC_READ_U8(datagram->data + 1), fp@1624: EC_READ_U8(datagram->data + 2), fp@1624: EC_READ_U8(datagram->data + 3)); fp@1619: #endif fp@1619: } fp@1619: fp@1624: // issue check/fetch datagram again fp@1619: if (fsm->sii_mode) { fp@1624: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 10); fp@1619: } fp@1619: else { fp@1624: ec_datagram_aprd(datagram, fsm->slave->ring_position, 0x502, 10); fp@1619: } fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1619: return; fp@1619: } fp@1619: fp@1619: #if 0 fp@1619: EC_DBG("SII rec: %02X %02X %02X %02X - %02X %02X %02X %02X\n", fp@1624: EC_READ_U8(datagram->data + 0), EC_READ_U8(datagram->data + 1), fp@1624: EC_READ_U8(datagram->data + 2), EC_READ_U8(datagram->data + 3), fp@1624: EC_READ_U8(datagram->data + 6), EC_READ_U8(datagram->data + 7), fp@1624: EC_READ_U8(datagram->data + 8), EC_READ_U8(datagram->data + 9)); fp@1619: #endif fp@1619: fp@1619: // SII value received. fp@1624: memcpy(fsm->sii_value, datagram->data + 6, 4); fp@1619: fsm->sii_state = ec_fsm_sii_end; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1621: SII state: START_WRITING. fp@1621: Starts reading the slave information interface. fp@1621: */ fp@1621: fp@1621: void ec_fsm_sii_start_writing(ec_fsm_t *fsm /**< finite state machine */) fp@1621: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1621: fp@1621: // initiate write operation fp@1624: ec_datagram_npwr(datagram, fsm->slave->station_address, 0x502, 8); fp@1624: EC_WRITE_U8 (datagram->data, 0x01); // enable write access fp@1624: EC_WRITE_U8 (datagram->data + 1, 0x02); // request write operation fp@1624: EC_WRITE_U32(datagram->data + 2, fsm->sii_offset); fp@1624: memcpy(datagram->data + 6, fsm->sii_value, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1621: fsm->sii_state = ec_fsm_sii_write_check; fp@1621: } fp@1621: fp@1621: /*****************************************************************************/ fp@1621: fp@1621: /** fp@1621: SII state: WRITE_CHECK. fp@1621: */ fp@1621: fp@1621: void ec_fsm_sii_write_check(ec_fsm_t *fsm /**< finite state machine */) fp@1621: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: EC_ERR("SII: Reception of write datagram failed.\n"); fp@1621: fsm->sii_state = ec_fsm_sii_error; fp@1621: return; fp@1621: } fp@1621: fp@1621: fsm->sii_start = get_cycles(); fp@1621: fp@1624: // issue check/fetch datagram fp@1624: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1621: fsm->sii_state = ec_fsm_sii_write_check2; fp@1621: } fp@1621: fp@1621: /*****************************************************************************/ fp@1621: fp@1621: /** fp@1621: SII state: WRITE_CHECK2. fp@1621: */ fp@1621: fp@1621: void ec_fsm_sii_write_check2(ec_fsm_t *fsm /**< finite state machine */) fp@1621: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1624: EC_ERR("SII: Reception of write check datagram failed.\n"); fp@1621: fsm->sii_state = ec_fsm_sii_error; fp@1621: return; fp@1621: } fp@1621: fp@1624: if (EC_READ_U8(datagram->data + 1) & 0x82) { fp@1621: // still busy... timeout? fp@1621: if (get_cycles() - fsm->sii_start >= (cycles_t) 10 * cpu_khz) { fp@1621: EC_ERR("SII: Write timeout.\n"); fp@1621: fsm->sii_state = ec_fsm_sii_error; fp@1621: } fp@1621: fp@1624: // issue check/fetch datagram again fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: } fp@1624: else if (EC_READ_U8(datagram->data + 1) & 0x40) { fp@1621: EC_ERR("SII: Write operation failed!\n"); fp@1621: fsm->sii_state = ec_fsm_sii_error; fp@1621: } fp@1621: else { // success fp@1621: fsm->sii_state = ec_fsm_sii_end; fp@1621: } fp@1621: } fp@1621: fp@1621: /*****************************************************************************/ fp@1621: fp@1621: /** fp@1619: SII state: END. fp@1619: End state of the slave SII state machine. fp@1619: */ fp@1619: fp@1619: void ec_fsm_sii_end(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: SII state: ERROR. fp@1619: End state of the slave SII state machine. fp@1619: */ fp@1619: fp@1619: void ec_fsm_sii_error(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1619: } fp@1619: fp@1619: /****************************************************************************** fp@1624: * state change sub state machine fp@1619: *****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Change state: START. fp@1619: */ fp@1619: fp@1619: void ec_fsm_change_start(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1619: ec_slave_t *slave = fsm->slave; fp@1619: fp@1619: // write new state to slave fp@1624: ec_datagram_npwr(datagram, slave->station_address, 0x0120, 2); fp@1624: EC_WRITE_U16(datagram->data, fsm->change_new); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1619: fsm->change_state = ec_fsm_change_check; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Change state: CHECK. fp@1619: */ fp@1619: fp@1619: void ec_fsm_change_check(ec_fsm_t *fsm /**< finite state machine */) fp@1619: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED) { fp@1624: fsm->change_state = ec_fsm_change_error; fp@1624: EC_ERR("Failed to send state datagram to slave %i!\n", fp@1619: fsm->slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: if (datagram->working_counter != 1) { fp@1619: fsm->change_state = ec_fsm_change_error; fp@1619: EC_ERR("Failed to set state 0x%02X on slave %i: Slave did not" fp@1619: " respond.\n", fsm->change_new, fsm->slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: fsm->change_start = get_cycles(); fp@1624: fp@1624: // read AL status from slave fp@1624: ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: fsm->change_state = ec_fsm_change_status; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Change state: STATUS. fp@1624: */ fp@1624: fp@1624: void ec_fsm_change_status(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1619: fsm->change_state = ec_fsm_change_error; fp@1619: EC_ERR("Failed to check state 0x%02X on slave %i.\n", fp@1619: fsm->change_new, slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: slave->current_state = EC_READ_U8(datagram->data); fp@1619: fp@1619: if (slave->current_state == fsm->change_new) { fp@1619: // state has been set successfully fp@1619: fsm->change_state = ec_fsm_change_end; fp@1619: return; fp@1619: } fp@1619: fp@1619: if (slave->current_state & 0x10) { fp@1619: // state change error fp@1619: fsm->change_new = slave->current_state & 0x0F; fp@1619: EC_ERR("Failed to set state 0x%02X - Slave %i refused state change" fp@1619: " (code 0x%02X)!\n", fsm->change_new, slave->ring_position, fp@1619: slave->current_state); fp@1619: // fetch AL status error code fp@1624: ec_datagram_nprd(datagram, slave->station_address, 0x0134, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1619: fsm->change_state = ec_fsm_change_code; fp@1619: return; fp@1619: } fp@1619: fp@1619: if (get_cycles() - fsm->change_start >= (cycles_t) 10 * cpu_khz) { fp@1619: // timeout while checking fp@1619: fsm->change_state = ec_fsm_change_error; fp@1619: EC_ERR("Timeout while setting state 0x%02X on slave %i.\n", fp@1619: fsm->change_new, slave->ring_position); fp@1619: return; fp@1619: } fp@1619: fp@1619: // still old state: check again fp@1624: ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Application layer status messages. fp@1619: */ fp@1619: fp@1619: const ec_code_msg_t al_status_messages[] = { fp@1619: {0x0001, "Unspecified error"}, fp@1619: {0x0011, "Invalud requested state change"}, fp@1619: {0x0012, "Unknown requested state"}, fp@1619: {0x0013, "Bootstrap not supported"}, fp@1619: {0x0014, "No valid firmware"}, fp@1619: {0x0015, "Invalid mailbox configuration"}, fp@1619: {0x0016, "Invalid mailbox configuration"}, fp@1619: {0x0017, "Invalid sync manager configuration"}, fp@1619: {0x0018, "No valid inputs available"}, fp@1619: {0x0019, "No valid outputs"}, fp@1619: {0x001A, "Synchronisation error"}, fp@1619: {0x001B, "Sync manager watchdog"}, fp@1624: {0x001C, "Invalid sync manager types"}, fp@1624: {0x001D, "Invalid output configuration"}, fp@1624: {0x001E, "Invalid input configuration"}, fp@1624: {0x001F, "Invalid watchdog configuration"}, fp@1619: {0x0020, "Slave needs cold start"}, fp@1619: {0x0021, "Slave needs INIT"}, fp@1619: {0x0022, "Slave needs PREOP"}, fp@1619: {0x0023, "Slave needs SAVEOP"}, fp@1624: {0x0030, "Invalid DC SYNCH configuration"}, fp@1624: {0x0031, "Invalid DC latch configuration"}, fp@1624: {0x0032, "PLL error"}, fp@1624: {0x0033, "Invalid DC IO error"}, fp@1624: {0x0034, "Invalid DC timeout error"}, fp@1624: {0x0042, "MBOX EOE"}, fp@1624: {0x0043, "MBOX COE"}, fp@1624: {0x0044, "MBOX FOE"}, fp@1624: {0x0045, "MBOX SOE"}, fp@1624: {0x004F, "MBOX VOE"}, fp@1619: {} fp@1619: }; fp@1619: fp@1619: /*****************************************************************************/ fp@1623: fp@1623: /** fp@1623: Change state: CODE. fp@1623: */ fp@1623: fp@1623: void ec_fsm_change_code(ec_fsm_t *fsm /**< finite state machine */) fp@1623: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1623: ec_slave_t *slave = fsm->slave; fp@1623: uint32_t code; fp@1623: const ec_code_msg_t *al_msg; fp@1623: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1623: fsm->change_state = ec_fsm_change_error; fp@1624: EC_ERR("Reception of AL status code datagram failed.\n"); fp@1624: return; fp@1624: } fp@1624: fp@1624: if ((code = EC_READ_U16(datagram->data))) { fp@1623: for (al_msg = al_status_messages; al_msg->code; al_msg++) { fp@1623: if (al_msg->code != code) continue; fp@1623: EC_ERR("AL status message 0x%04X: \"%s\".\n", fp@1623: al_msg->code, al_msg->message); fp@1623: break; fp@1623: } fp@1623: if (!al_msg->code) fp@1623: EC_ERR("Unknown AL status code 0x%04X.\n", code); fp@1623: } fp@1623: fp@1623: // acknowledge "old" slave state fp@1624: ec_datagram_npwr(datagram, slave->station_address, 0x0120, 2); fp@1624: EC_WRITE_U16(datagram->data, slave->current_state); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1623: fsm->change_state = ec_fsm_change_ack; fp@1623: } fp@1623: fp@1623: /*****************************************************************************/ fp@1623: fp@1623: /** fp@1623: Change state: ACK. fp@1623: */ fp@1623: fp@1623: void ec_fsm_change_ack(ec_fsm_t *fsm /**< finite state machine */) fp@1623: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1623: fsm->change_state = ec_fsm_change_error; fp@1624: EC_ERR("Reception of state ack datagram failed.\n"); fp@1624: return; fp@1624: } fp@1624: fp@1624: fsm->change_start = get_cycles(); fp@1623: fp@1623: // read new AL status fp@1624: ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1624: fsm->change_state = ec_fsm_change_check_ack; fp@1624: } fp@1624: fp@1624: /*****************************************************************************/ fp@1624: fp@1624: /** fp@1624: Change state: CHECK ACK. fp@1624: */ fp@1624: fp@1624: void ec_fsm_change_check_ack(ec_fsm_t *fsm /**< finite state machine */) fp@1624: { fp@1624: ec_datagram_t *datagram = &fsm->datagram; fp@1624: ec_slave_t *slave = fsm->slave; fp@1624: ec_slave_state_t ack_state; fp@1624: fp@1624: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@1623: fsm->change_state = ec_fsm_change_error; fp@1624: EC_ERR("Reception of state ack check datagram failed.\n"); fp@1624: return; fp@1624: } fp@1624: fp@1624: ack_state = EC_READ_U8(datagram->data); fp@1624: fp@1624: if (ack_state == slave->current_state) { fp@1624: fsm->change_state = ec_fsm_change_error; fp@1623: EC_INFO("Acknowleged state 0x%02X on slave %i.\n", fp@1623: slave->current_state, slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: if (get_cycles() - fsm->change_start >= (cycles_t) 100 * cpu_khz) { fp@1624: // timeout while checking fp@1624: slave->current_state = EC_SLAVE_STATE_UNKNOWN; fp@1623: fsm->change_state = ec_fsm_change_error; fp@1624: EC_ERR("Timeout while acknowleging state 0x%02X on slave %i.\n", fp@1624: fsm->change_new, slave->ring_position); fp@1624: return; fp@1624: } fp@1624: fp@1624: // reread new AL status fp@1624: ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2); fp@1624: ec_master_queue_datagram(fsm->master, datagram); fp@1623: } fp@1623: fp@1623: /*****************************************************************************/ fp@1623: fp@1623: /** fp@1623: Change state: END. fp@1623: */ fp@1623: fp@1623: void ec_fsm_change_end(ec_fsm_t *fsm /**< finite state machine */) fp@1623: { fp@1623: } fp@1623: fp@1623: /*****************************************************************************/ fp@1623: fp@1623: /** fp@1623: Change state: ERROR. fp@1623: */ fp@1623: fp@1623: void ec_fsm_change_error(ec_fsm_t *fsm /**< finite state machine */) fp@1623: { fp@1623: } fp@1623: fp@1623: /*****************************************************************************/