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@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@260: void ec_fsm_master_proc_states(ec_fsm_t *); fp@251: void ec_fsm_master_scan(ec_fsm_t *); fp@260: void ec_fsm_master_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@260: void ec_fsm_master_reconfigure(ec_fsm_t *); fp@260: void ec_fsm_master_address(ec_fsm_t *); fp@251: void ec_fsm_master_conf(ec_fsm_t *); fp@269: void ec_fsm_master_eeprom(ec_fsm_t *); fp@251: fp@251: void ec_fsm_slave_start_reading(ec_fsm_t *); fp@292: void ec_fsm_slave_read_state(ec_fsm_t *); fp@238: void ec_fsm_slave_read_base(ec_fsm_t *); fp@238: void ec_fsm_slave_read_dl(ec_fsm_t *); fp@266: void ec_fsm_slave_eeprom_size(ec_fsm_t *); fp@266: void ec_fsm_slave_fetch_eeprom(ec_fsm_t *); fp@266: void ec_fsm_slave_fetch_eeprom2(ec_fsm_t *); fp@251: void ec_fsm_slave_end(ec_fsm_t *); fp@251: fp@251: void ec_fsm_slave_conf(ec_fsm_t *); fp@251: void ec_fsm_slave_sync(ec_fsm_t *); fp@251: void ec_fsm_slave_preop(ec_fsm_t *); fp@251: void ec_fsm_slave_fmmu(ec_fsm_t *); fp@251: void ec_fsm_slave_saveop(ec_fsm_t *); fp@251: void ec_fsm_slave_op(ec_fsm_t *); fp@251: void ec_fsm_slave_op2(ec_fsm_t *); fp@238: fp@238: void ec_fsm_sii_start_reading(ec_fsm_t *); fp@269: void ec_fsm_sii_read_check(ec_fsm_t *); fp@269: void ec_fsm_sii_read_fetch(ec_fsm_t *); fp@269: void ec_fsm_sii_start_writing(ec_fsm_t *); fp@269: void ec_fsm_sii_write_check(ec_fsm_t *); fp@269: void ec_fsm_sii_write_check2(ec_fsm_t *); fp@260: void ec_fsm_sii_end(ec_fsm_t *); fp@238: void ec_fsm_sii_error(ec_fsm_t *); fp@238: fp@251: void ec_fsm_change_start(ec_fsm_t *); fp@251: void ec_fsm_change_check(ec_fsm_t *); fp@251: void ec_fsm_change_status(ec_fsm_t *); fp@251: void ec_fsm_change_code(ec_fsm_t *); fp@251: void ec_fsm_change_ack(ec_fsm_t *); fp@251: void ec_fsm_change_ack2(ec_fsm_t *); fp@251: void ec_fsm_change_end(ec_fsm_t *); fp@251: void ec_fsm_change_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@292: ) 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@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@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@260: */ fp@260: fp@260: void ec_fsm_execute(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@238: fsm->master_state(fsm); fp@238: } fp@238: fp@238: /****************************************************************************** fp@238: * master 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@293: if (datagram->state != EC_CMD_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@262: if (master->mode == EC_MASTER_MODE_RUNNING) { 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@260: EC_INFO("Slave states: "); fp@260: ec_print_states(fsm->master_slave_states); fp@260: printk(".\n"); fp@260: } fp@260: fp@260: // topology change in free-run mode: clear all slaves and scan the bus fp@260: if (topology_change && master->mode == EC_MASTER_MODE_FREERUN) { 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@260: if (!fsm->master_slaves_responding) { 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@260: for (i = 0; i < fsm->master_slaves_responding; 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@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@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@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@260: fsm->slave_state = ec_fsm_slave_start_reading; fp@260: fsm->master_state = ec_fsm_master_scan; 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@260: fsm->master_state = ec_fsm_master_states; 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@260: // have all states been read? 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@292: fsm->master_state = ec_fsm_master_states; fp@292: return; fp@292: } fp@292: fp@292: // all slave stated read; 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@292: fsm->sii_offset = 0x0008; // vendor ID fp@292: fsm->sii_mode = 0; fp@292: fsm->sii_state = ec_fsm_sii_start_reading; fp@292: fsm->sii_state(fsm); // execute immediately fp@292: return; fp@251: } fp@292: } fp@292: fp@292: fsm->master_state = ec_fsm_master_proc_states; fp@292: fsm->master_state(fsm); // execute immediately fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: STATES. fp@260: Fetches the AL- and online state of a slave. fp@260: */ fp@260: fp@260: void ec_fsm_master_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@293: if (datagram->state != EC_CMD_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@260: slave->online = 1; fp@291: slave->error_flag = 0; // clear error flag fp@260: slave->current_state = new_state; fp@260: EC_INFO("Slave %i: online (", slave->ring_position); fp@260: ec_print_states(new_state); fp@260: printk(").\n"); fp@260: } fp@260: else if (new_state != slave->current_state) { fp@260: EC_INFO("Slave %i: ", slave->ring_position); fp@260: ec_print_states(slave->current_state); fp@260: printk(" -> "); fp@260: ec_print_states(new_state); fp@260: printk(".\n"); 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: PROC_STATES. fp@260: Processes the slave states. fp@260: */ fp@260: fp@260: void ec_fsm_master_proc_states(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@260: ec_master_t *master = fsm->master; fp@260: ec_slave_t *slave; fp@260: fp@260: // check if any slaves are not in the state, they're supposed to be fp@260: list_for_each_entry(slave, &master->slaves, list) { fp@291: if (slave->error_flag || fp@291: !slave->online || fp@260: slave->requested_state == EC_SLAVE_STATE_UNKNOWN || fp@260: slave->current_state == slave->requested_state) continue; fp@260: fp@260: EC_INFO("Changing state of slave %i from ", slave->ring_position); fp@260: ec_print_states(slave->current_state); fp@260: printk(" to "); fp@260: ec_print_states(slave->requested_state); fp@260: printk(".\n"); fp@260: fp@260: fsm->slave = slave; fp@260: fsm->slave_state = ec_fsm_slave_conf; fp@260: fsm->change_new = EC_SLAVE_STATE_INIT; fp@260: fsm->change_state = ec_fsm_change_start; fp@260: fsm->master_state = ec_fsm_master_conf; fp@260: fsm->master_state(fsm); // execute immediately fp@260: return; fp@260: } fp@260: fp@269: if (master->mode == EC_MASTER_MODE_FREERUN) { fp@269: // nothing to configure. check for pending EEPROM write operations. fp@269: list_for_each_entry(slave, &master->slaves, list) { fp@269: if (!slave->new_eeprom_data) continue; fp@269: fp@292: if (!slave->online || slave->error_flag) { fp@292: kfree(slave->new_eeprom_data); fp@292: slave->new_eeprom_data = NULL; fp@292: EC_ERR("Discarding EEPROM data, slave %i not ready.\n", fp@292: slave->ring_position); fp@292: continue; fp@292: } fp@292: fp@269: // found pending EEPROM write operation. execute it! fp@269: EC_INFO("Writing EEPROM of slave %i...\n", slave->ring_position); fp@269: fsm->sii_offset = 0x0000; fp@269: memcpy(fsm->sii_value, slave->new_eeprom_data, 2); fp@269: fsm->sii_mode = 1; fp@269: fsm->sii_state = ec_fsm_sii_start_writing; fp@269: fsm->slave = slave; fp@269: fsm->master_state = ec_fsm_master_eeprom; fp@269: fsm->master_state(fsm); // execute immediately fp@269: return; fp@269: } fp@269: } fp@269: fp@269: // nothing to do. restart master state machine. fp@260: fsm->master_state = ec_fsm_master_start; fp@260: fsm->master_state(fsm); // execute immediately 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@260: fsm->sii_state(fsm); // execute SII state machine fp@260: fp@260: if (fsm->sii_state == ec_fsm_sii_error) { 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@260: if (fsm->sii_state != ec_fsm_sii_end) return; fp@260: fp@269: if (EC_READ_U32(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@260: fsm->sii_offset = 0x000A; // product code fp@260: fsm->sii_mode = 0; fp@260: fsm->sii_state = ec_fsm_sii_start_reading; fp@260: fsm->sii_state(fsm); // execute immediately fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: VALIDATE_PRODUCT. fp@260: Validates the product ID of a slave. fp@260: */ fp@260: fp@260: void ec_fsm_master_validate_product(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@260: fp@260: fsm->sii_state(fsm); // execute SII state machine fp@260: fp@260: if (fsm->sii_state == ec_fsm_sii_error) { fp@292: fsm->slave->error_flag = 1; fp@260: EC_ERR("Failed to validate product code of slave %i.\n", fp@260: slave->ring_position); 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 (fsm->sii_state != ec_fsm_sii_end) return; fp@260: fp@269: if (EC_READ_U32(fsm->sii_value) != slave->sii_product_code) { fp@260: EC_ERR("Slave %i: invalid product code!\n", slave->ring_position); fp@260: EC_ERR("expected 0x%08X, got 0x%08X.\n", slave->sii_product_code, fp@269: EC_READ_U32(fsm->sii_value)); 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: // have all states been validated? fp@260: if (slave->list.next == &fsm->master->slaves) { fp@260: fsm->slave = list_entry(fsm->master->slaves.next, ec_slave_t, list); fp@260: fsm->master_state = ec_fsm_master_reconfigure; fp@260: return; fp@260: } fp@260: fp@260: // validate next slave fp@260: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@260: fsm->master_state = ec_fsm_master_validate_vendor; fp@260: fsm->sii_offset = 0x0008; // vendor ID fp@260: fsm->sii_mode = 0; fp@260: fsm->sii_state = ec_fsm_sii_start_reading; fp@260: fsm->sii_state(fsm); // execute immediately fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: RECONFIGURE. 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@260: void ec_fsm_master_reconfigure(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@260: fsm->master_state = ec_fsm_master_address; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: ADDRESS. fp@260: Checks, if the new station address has been written to the slave. fp@260: */ fp@260: fp@260: void ec_fsm_master_address(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@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || 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@260: fsm->master_state = ec_fsm_master_reconfigure; fp@238: fsm->master_state(fsm); // execute immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@260: Master state: SCAN. fp@260: Executes the sub-statemachine for the scanning of a slave. fp@260: */ fp@260: fp@260: void ec_fsm_master_scan(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@260: uint16_t coupler_index, coupler_subindex; fp@260: uint16_t reverse_coupler_index, current_coupler_index; fp@260: ec_slave_ident_t *ident; fp@238: fp@238: fsm->slave_state(fsm); // execute slave state machine fp@238: fp@251: if (fsm->slave_state != ec_fsm_slave_end) return; fp@238: fp@238: // have all slaves been fetched? fp@260: if (slave->list.next == &master->slaves) { fp@251: EC_INFO("Bus scanning completed.\n"); fp@251: fp@251: // identify all slaves and calculate coupler addressing fp@251: fp@251: coupler_index = 0; fp@251: reverse_coupler_index = 0xFFFF; fp@251: current_coupler_index = 0x3FFF; fp@251: coupler_subindex = 0; fp@251: fp@251: list_for_each_entry(slave, &master->slaves, list) fp@251: { fp@251: // search for identification in "database" fp@251: ident = slave_idents; fp@251: while (ident->type) { fp@251: if (ident->vendor_id == slave->sii_vendor_id fp@251: && ident->product_code == slave->sii_product_code) { fp@251: slave->type = ident->type; fp@251: break; fp@251: } fp@251: ident++; fp@251: } fp@251: fp@251: if (!slave->type) { fp@292: EC_WARN("Unknown slave device (vendor 0x%08X," fp@251: " code 0x%08X) at position %i.\n", fp@251: slave->sii_vendor_id, slave->sii_product_code, fp@251: slave->ring_position); fp@251: } fp@251: else { fp@251: // if the slave is a bus coupler, change adressing base fp@251: if (slave->type->special == EC_TYPE_BUS_COUPLER) { fp@251: if (slave->sii_alias) fp@251: current_coupler_index = reverse_coupler_index--; fp@251: else fp@251: current_coupler_index = coupler_index++; fp@251: coupler_subindex = 0; fp@251: } fp@251: } fp@251: fp@251: // determine initial state. fp@290: if ((slave->type && fp@292: (slave->type->special == EC_TYPE_BUS_COUPLER || fp@292: slave->type->special == EC_TYPE_INFRA))) { fp@251: slave->requested_state = EC_SLAVE_STATE_OP; fp@251: } fp@251: else { fp@251: if (master->mode == EC_MASTER_MODE_RUNNING) fp@251: slave->requested_state = EC_SLAVE_STATE_PREOP; fp@251: else fp@251: slave->requested_state = EC_SLAVE_STATE_INIT; fp@251: } fp@291: slave->error_flag = 0; fp@251: fp@251: // calculate coupler-based slave address fp@251: slave->coupler_index = current_coupler_index; fp@251: slave->coupler_subindex = coupler_subindex; fp@251: coupler_subindex++; fp@251: } fp@251: fp@260: if (master->mode == EC_MASTER_MODE_FREERUN) { fp@260: // start EoE processing fp@260: ec_master_eoe_start(master); fp@260: } fp@260: fp@251: fsm->master_state = ec_fsm_master_start; fp@238: fsm->master_state(fsm); // execute immediately fp@238: return; fp@238: } fp@238: fp@238: // process next slave fp@238: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@251: fsm->slave_state = ec_fsm_slave_start_reading; fp@238: fsm->slave_state(fsm); // execute immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@260: Master state: CONF. fp@260: Starts configuring a slave. fp@260: */ fp@260: fp@260: void ec_fsm_master_conf(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@251: fsm->slave_state(fsm); // execute slave's state machine fp@251: if (fsm->slave_state != ec_fsm_slave_end) return; fp@260: fsm->master_state = ec_fsm_master_proc_states; fp@238: fsm->master_state(fsm); // execute immediately fp@238: } fp@238: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@269: Master state: EEPROM. fp@269: */ fp@269: fp@269: void ec_fsm_master_eeprom(ec_fsm_t *fsm /**< finite state machine */) fp@269: { fp@269: ec_slave_t *slave = fsm->slave; fp@269: fp@269: fsm->sii_state(fsm); // execute SII state machine fp@269: fp@269: if (fsm->sii_state == ec_fsm_sii_error) { 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: if (fsm->sii_state != ec_fsm_sii_end) return; fp@269: fp@269: fsm->sii_offset++; fp@269: if (fsm->sii_offset < slave->new_eeprom_size) { fp@269: memcpy(fsm->sii_value, slave->new_eeprom_data + fsm->sii_offset, 2); fp@269: fsm->sii_state = ec_fsm_sii_start_writing; fp@269: fsm->sii_state(fsm); // 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@269: // restart master state machine. 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@238: /****************************************************************************** fp@238: * slave state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@260: Slave state: START_READING. 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@260: void ec_fsm_slave_start_reading(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@292: fsm->slave_state = ec_fsm_slave_read_state; fp@255: } fp@255: fp@255: /*****************************************************************************/ fp@255: fp@255: /** fp@260: Slave state: READ_STATUS. fp@260: */ fp@260: fp@292: void ec_fsm_slave_read_state(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@292: fsm->slave->error_flag = 1; fp@292: fsm->slave_state = ec_fsm_slave_end; 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@255: fsm->slave_state = ec_fsm_slave_read_base; fp@255: } fp@255: fp@255: /*****************************************************************************/ fp@255: fp@255: /** fp@260: Slave state: READ_BASE. fp@260: */ fp@260: fp@260: void ec_fsm_slave_read_base(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@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@291: fsm->slave->error_flag = 1; fp@292: fsm->slave_state = ec_fsm_slave_end; 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@255: if (slave->current_state & EC_ACK) { fp@292: EC_WARN("Slave %i has state error bit set (0x%02X)!\n", fp@255: slave->ring_position, slave->current_state); fp@255: slave->current_state &= 0x0F; 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@238: fsm->slave_state = ec_fsm_slave_read_dl; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@260: Slave state: READ_DL. fp@260: */ fp@260: fp@260: void ec_fsm_slave_read_dl(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@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@291: fsm->slave->error_flag = 1; fp@292: fsm->slave_state = ec_fsm_slave_end; 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@266: fsm->slave_state = ec_fsm_slave_eeprom_size; fp@266: } fp@266: fp@266: /*****************************************************************************/ fp@266: fp@266: /** fp@266: Slave state: EEPROM_SIZE. fp@266: Read the actual size of the EEPROM to allocate the EEPROM image. fp@266: */ fp@266: fp@266: void ec_fsm_slave_eeprom_size(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@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@291: fsm->slave->error_flag = 1; fp@292: fsm->slave_state = ec_fsm_slave_end; 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@260: fsm->sii_mode = 1; fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@266: fsm->slave_state = ec_fsm_slave_fetch_eeprom; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@266: Slave state: FETCH_EEPROM. fp@266: */ fp@266: fp@266: void ec_fsm_slave_fetch_eeprom(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@238: // execute SII state machine fp@238: fsm->sii_state(fsm); fp@238: fp@238: if (fsm->sii_state == ec_fsm_sii_error) { fp@292: fsm->slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; 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@260: if (fsm->sii_state != ec_fsm_sii_end) return; fp@238: fp@269: cat_type = EC_READ_U16(fsm->sii_value); fp@269: cat_size = EC_READ_U16(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@266: fsm->sii_state = ec_fsm_sii_start_reading; fp@266: fsm->sii_state(fsm); // 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@292: fsm->slave_state = ec_fsm_slave_end; 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@266: fsm->sii_offset = 0x0000; fp@266: fsm->sii_mode = 1; fp@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@266: fsm->slave_state = ec_fsm_slave_fetch_eeprom2; fp@238: fsm->slave_state(fsm); // execute state immediately fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@266: Slave state: FETCH_EEPROM2. fp@266: */ fp@266: fp@266: void ec_fsm_slave_fetch_eeprom2(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@238: // execute SII state machine fp@238: fsm->sii_state(fsm); fp@238: fp@238: if (fsm->sii_state == ec_fsm_sii_error) { fp@292: fsm->slave->error_flag = 1; fp@266: fsm->slave_state = ec_fsm_slave_end; 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@260: if (fsm->sii_state != ec_fsm_sii_end) return; fp@238: fp@266: // 2 words fetched fp@266: fp@266: if (fsm->sii_offset + 2 <= slave->eeprom_size / 2) { // 2 words fit fp@269: memcpy(slave->eeprom_data + fsm->sii_offset * 2, fsm->sii_value, 4); fp@266: } fp@266: else { // copy the last word fp@269: memcpy(slave->eeprom_data + fsm->sii_offset * 2, 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@238: fsm->sii_state = ec_fsm_sii_start_reading; fp@266: fsm->sii_state(fsm); // 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@266: if (ec_slave_fetch_general(slave, (uint8_t *) (cat_word + 2))) fp@266: goto end; 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@292: end: fp@291: fsm->slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: CONF. fp@260: */ fp@260: fp@260: void ec_fsm_slave_conf(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: fp@251: fsm->change_state(fsm); // execute state change state machine fp@251: fp@251: if (fsm->change_state == ec_fsm_change_error) { fp@292: slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; fp@251: return; fp@251: } fp@251: fp@251: if (fsm->change_state != ec_fsm_change_end) return; fp@251: fp@251: // slave is now in INIT fp@251: if (slave->current_state == slave->requested_state) { fp@292: fsm->slave_state = ec_fsm_slave_end; // successful fp@251: return; fp@251: } fp@251: fp@251: // check for slave registration fp@251: if (!slave->type) { fp@251: EC_WARN("Slave %i has unknown type!\n", slave->ring_position); fp@251: } fp@251: fp@251: // check and reset CRC fault counters fp@251: //ec_slave_check_crc(slave); fp@251: fp@251: if (!slave->base_fmmu_count) { // no fmmus fp@251: fsm->slave_state = ec_fsm_slave_sync; fp@251: fsm->slave_state(fsm); // execute immediately fp@251: return; fp@251: } fp@251: fp@251: // reset FMMUs fp@293: ec_datagram_npwr(datagram, slave->station_address, 0x0600, fp@293: EC_FMMU_SIZE * slave->base_fmmu_count); fp@293: memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count); fp@293: ec_master_queue_datagram(master, datagram); fp@251: fsm->slave_state = ec_fsm_slave_sync; fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: SYNC. fp@260: Configure sync managers. fp@260: */ fp@260: fp@260: void ec_fsm_slave_sync(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@251: ec_slave_t *slave = fsm->slave; fp@251: unsigned int j; fp@251: const ec_sync_t *sync; fp@251: ec_eeprom_sync_t *eeprom_sync, mbox_sync; fp@251: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@292: slave->error_flag = 1; fp@292: fsm->slave_state = ec_fsm_slave_end; fp@260: EC_ERR("Failed to reset FMMUs of slave %i.\n", fp@251: slave->ring_position); fp@251: return; fp@251: } fp@251: fp@251: if (!slave->base_sync_count) { // no sync managers fp@251: fsm->slave_state = ec_fsm_slave_preop; fp@251: fsm->slave_state(fsm); // execute immediately fp@251: return; fp@251: } fp@251: 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@251: // known slave type, take type's SM information fp@251: if (slave->type) { fp@251: for (j = 0; slave->type->sync_managers[j] && j < EC_MAX_SYNC; j++) { fp@251: sync = slave->type->sync_managers[j]; fp@293: ec_sync_config(sync, slave, datagram->data + EC_SYNC_SIZE * j); fp@251: } fp@251: } fp@251: fp@251: // unknown type, but slave has mailbox fp@251: else if (slave->sii_mailbox_protocols) fp@251: { fp@251: // does it supply sync manager configurations in its EEPROM? fp@251: if (!list_empty(&slave->eeprom_syncs)) { fp@251: list_for_each_entry(eeprom_sync, &slave->eeprom_syncs, list) { fp@251: if (eeprom_sync->index >= slave->base_sync_count) { fp@291: fsm->slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; fp@292: EC_ERR("Invalid sync manager configuration found!"); fp@251: return; fp@251: } fp@298: ec_eeprom_sync_config(eeprom_sync, slave, fp@293: datagram->data + EC_SYNC_SIZE fp@251: * eeprom_sync->index); fp@251: } fp@251: } fp@251: fp@251: // no sync manager information; guess mailbox settings fp@251: else { fp@251: mbox_sync.physical_start_address = fp@251: slave->sii_rx_mailbox_offset; fp@251: mbox_sync.length = slave->sii_rx_mailbox_size; fp@251: mbox_sync.control_register = 0x26; fp@251: mbox_sync.enable = 1; fp@298: ec_eeprom_sync_config(&mbox_sync, slave, datagram->data); fp@251: fp@251: mbox_sync.physical_start_address = fp@251: slave->sii_tx_mailbox_offset; fp@251: mbox_sync.length = slave->sii_tx_mailbox_size; fp@251: mbox_sync.control_register = 0x22; fp@251: mbox_sync.enable = 1; fp@298: ec_eeprom_sync_config(&mbox_sync, slave, fp@293: datagram->data + EC_SYNC_SIZE); fp@251: } fp@251: fp@251: EC_INFO("Mailbox configured for unknown slave %i\n", fp@251: slave->ring_position); fp@251: } fp@251: fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@251: fsm->slave_state = ec_fsm_slave_preop; fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: PREOP. fp@260: Change slave state to PREOP. fp@260: */ fp@260: fp@260: void ec_fsm_slave_preop(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@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@292: slave->error_flag = 1; fp@292: fsm->slave_state = ec_fsm_slave_end; 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@251: fsm->change_new = EC_SLAVE_STATE_PREOP; fp@251: fsm->change_state = ec_fsm_change_start; fp@251: fsm->slave_state = ec_fsm_slave_fmmu; fp@251: fsm->change_state(fsm); // execute immediately fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: FMMU. fp@260: Configure FMMUs. fp@260: */ fp@260: fp@260: void ec_fsm_slave_fmmu(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@251: fsm->change_state(fsm); // execute state change state machine fp@251: fp@251: if (fsm->change_state == ec_fsm_change_error) { fp@292: slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; fp@251: return; fp@251: } fp@251: fp@251: if (fsm->change_state != ec_fsm_change_end) return; fp@251: fp@251: // slave is now in PREOP fp@251: if (slave->current_state == slave->requested_state) { fp@292: fsm->slave_state = ec_fsm_slave_end; // successful fp@251: return; fp@251: } fp@251: fp@251: // stop activation here for slaves without type fp@251: if (!slave->type) { fp@292: fsm->slave_state = ec_fsm_slave_end; // successful fp@251: return; fp@251: } fp@251: fp@251: if (!slave->base_fmmu_count) { fp@251: fsm->slave_state = ec_fsm_slave_saveop; fp@251: fsm->slave_state(fsm); // 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@251: fsm->slave_state = ec_fsm_slave_saveop; fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: SAVEOP. fp@260: Set slave state to SAVEOP. fp@260: */ fp@260: fp@260: void ec_fsm_slave_saveop(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@293: if (fsm->slave->base_fmmu_count && (datagram->state != EC_CMD_RECEIVED || fp@293: datagram->working_counter != 1)) { fp@291: fsm->slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; 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@251: // set state to SAVEOP fp@251: fsm->slave_state = ec_fsm_slave_op; fp@251: fsm->change_new = EC_SLAVE_STATE_SAVEOP; fp@251: fsm->change_state = ec_fsm_change_start; fp@251: fsm->change_state(fsm); // execute immediately fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: OP. fp@260: Set slave state to OP. fp@260: */ fp@260: fp@260: void ec_fsm_slave_op(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@251: fsm->change_state(fsm); // execute state change state machine fp@251: fp@251: if (fsm->change_state == ec_fsm_change_error) { fp@292: fsm->slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; fp@251: return; fp@251: } fp@251: fp@251: if (fsm->change_state != ec_fsm_change_end) return; fp@251: fp@251: // slave is now in SAVEOP fp@251: if (fsm->slave->current_state == fsm->slave->requested_state) { fp@292: fsm->slave_state = ec_fsm_slave_end; // successful fp@251: return; fp@251: } fp@251: fp@251: // set state to OP fp@251: fsm->slave_state = ec_fsm_slave_op2; fp@251: fsm->change_new = EC_SLAVE_STATE_OP; fp@251: fsm->change_state = ec_fsm_change_start; fp@251: fsm->change_state(fsm); // execute immediately fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: OP2 fp@260: Executes the change state machine, until the OP state is set. fp@260: */ fp@260: fp@260: void ec_fsm_slave_op2(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@251: fsm->change_state(fsm); // execute state change state machine fp@251: fp@251: if (fsm->change_state == ec_fsm_change_error) { fp@292: fsm->slave->error_flag = 1; fp@251: fsm->slave_state = ec_fsm_slave_end; fp@251: return; fp@251: } fp@251: fp@251: if (fsm->change_state != ec_fsm_change_end) return; fp@251: fp@251: // slave is now in OP fp@292: fsm->slave_state = ec_fsm_slave_end; // successful fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Slave state: END. fp@238: End state of the slave state machine. fp@238: */ fp@238: fp@260: void ec_fsm_slave_end(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@238: } fp@238: fp@238: /****************************************************************************** fp@238: * SII state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@260: SII state: START_READING. fp@238: Starts reading the slave information interface. fp@238: */ fp@238: fp@260: void ec_fsm_sii_start_reading(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@238: fp@238: // initiate read operation fp@260: if (fsm->sii_mode) { fp@293: ec_datagram_npwr(datagram, fsm->slave->station_address, 0x502, 4); fp@260: } fp@260: else { fp@293: ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x502, 4); fp@293: } fp@293: fp@293: EC_WRITE_U8 (datagram->data, 0x00); // read-only access fp@293: EC_WRITE_U8 (datagram->data + 1, 0x01); // request read operation fp@293: EC_WRITE_U16(datagram->data + 2, fsm->sii_offset); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@269: fsm->sii_state = ec_fsm_sii_read_check; fp@269: } fp@269: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@269: SII state: READ_CHECK. fp@293: Checks, if the SII-read-datagram has been sent and issues a fetch datagram. fp@238: */ fp@238: fp@269: void ec_fsm_sii_read_check(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@293: EC_ERR("SII: Reception of read datagram failed.\n"); fp@238: fsm->sii_state = ec_fsm_sii_error; fp@238: return; fp@238: } fp@238: fp@260: fsm->sii_start = get_cycles(); fp@260: fp@293: // issue check/fetch datagram fp@260: if (fsm->sii_mode) { fp@293: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 10); fp@260: } fp@260: else { fp@293: ec_datagram_aprd(datagram, fsm->slave->ring_position, 0x502, 10); fp@293: } fp@293: fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@269: fsm->sii_state = ec_fsm_sii_read_fetch; fp@269: } fp@269: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@269: SII state: READ_FETCH. fp@293: Fetches the result of an SII-read datagram. fp@238: */ fp@238: fp@269: void ec_fsm_sii_read_fetch(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@293: EC_ERR("SII: Reception of check/fetch datagram failed.\n"); fp@238: fsm->sii_state = ec_fsm_sii_error; fp@238: return; fp@238: } fp@238: fp@238: // check "busy bit" fp@293: if (EC_READ_U8(datagram->data + 1) & 0x81) { fp@260: // still busy... timeout? fp@260: if (get_cycles() - fsm->sii_start >= (cycles_t) 10 * cpu_khz) { fp@260: EC_ERR("SII: Timeout.\n"); fp@260: fsm->sii_state = ec_fsm_sii_error; fp@260: #if 0 fp@260: EC_DBG("SII busy: %02X %02X %02X %02X\n", fp@293: EC_READ_U8(datagram->data + 0), fp@293: EC_READ_U8(datagram->data + 1), fp@293: EC_READ_U8(datagram->data + 2), fp@293: EC_READ_U8(datagram->data + 3)); fp@260: #endif fp@260: } fp@260: fp@293: // issue check/fetch datagram again fp@260: if (fsm->sii_mode) { fp@293: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 10); fp@260: } fp@260: else { fp@293: ec_datagram_aprd(datagram, fsm->slave->ring_position, 0x502, 10); fp@260: } fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@260: return; fp@260: } fp@260: fp@260: #if 0 fp@260: EC_DBG("SII rec: %02X %02X %02X %02X - %02X %02X %02X %02X\n", fp@293: EC_READ_U8(datagram->data + 0), EC_READ_U8(datagram->data + 1), fp@293: EC_READ_U8(datagram->data + 2), EC_READ_U8(datagram->data + 3), fp@293: EC_READ_U8(datagram->data + 6), EC_READ_U8(datagram->data + 7), fp@293: EC_READ_U8(datagram->data + 8), EC_READ_U8(datagram->data + 9)); fp@260: #endif fp@260: fp@260: // SII value received. fp@293: memcpy(fsm->sii_value, datagram->data + 6, 4); fp@260: fsm->sii_state = ec_fsm_sii_end; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@269: SII state: START_WRITING. fp@269: Starts reading the slave information interface. fp@269: */ fp@269: fp@269: void ec_fsm_sii_start_writing(ec_fsm_t *fsm /**< finite state machine */) fp@269: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@269: fp@269: // initiate write operation fp@293: ec_datagram_npwr(datagram, fsm->slave->station_address, 0x502, 8); fp@293: EC_WRITE_U8 (datagram->data, 0x01); // enable write access fp@293: EC_WRITE_U8 (datagram->data + 1, 0x02); // request write operation fp@293: EC_WRITE_U32(datagram->data + 2, fsm->sii_offset); fp@293: memcpy(datagram->data + 6, fsm->sii_value, 2); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@269: fsm->sii_state = ec_fsm_sii_write_check; fp@269: } fp@269: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@269: SII state: WRITE_CHECK. fp@269: */ fp@269: fp@269: void ec_fsm_sii_write_check(ec_fsm_t *fsm /**< finite state machine */) fp@269: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@293: EC_ERR("SII: Reception of write datagram failed.\n"); fp@269: fsm->sii_state = ec_fsm_sii_error; fp@269: return; fp@269: } fp@269: fp@269: fsm->sii_start = get_cycles(); fp@269: fp@293: // issue check/fetch datagram fp@293: ec_datagram_nprd(datagram, fsm->slave->station_address, 0x502, 2); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@269: fsm->sii_state = ec_fsm_sii_write_check2; fp@269: } fp@269: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@269: SII state: WRITE_CHECK2. fp@269: */ fp@269: fp@269: void ec_fsm_sii_write_check2(ec_fsm_t *fsm /**< finite state machine */) fp@269: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@293: EC_ERR("SII: Reception of write check datagram failed.\n"); fp@269: fsm->sii_state = ec_fsm_sii_error; fp@269: return; fp@269: } fp@269: fp@293: if (EC_READ_U8(datagram->data + 1) & 0x82) { fp@269: // still busy... timeout? fp@269: if (get_cycles() - fsm->sii_start >= (cycles_t) 10 * cpu_khz) { fp@269: EC_ERR("SII: Write timeout.\n"); fp@269: fsm->sii_state = ec_fsm_sii_error; fp@269: } fp@269: fp@293: // issue check/fetch datagram again fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@293: } fp@293: else if (EC_READ_U8(datagram->data + 1) & 0x40) { fp@269: EC_ERR("SII: Write operation failed!\n"); fp@269: fsm->sii_state = ec_fsm_sii_error; fp@269: } fp@269: else { // success fp@269: fsm->sii_state = ec_fsm_sii_end; fp@269: } fp@269: } fp@269: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@260: SII state: END. fp@238: End state of the slave SII state machine. fp@238: */ fp@238: fp@260: void ec_fsm_sii_end(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: SII state: ERROR. fp@238: End state of the slave SII state machine. fp@238: */ fp@238: fp@260: void ec_fsm_sii_error(ec_fsm_t *fsm /**< finite state machine */) fp@238: { fp@238: } fp@238: fp@251: /****************************************************************************** fp@251: * state change state machine fp@251: *****************************************************************************/ fp@251: fp@251: /** fp@260: Change state: START. fp@260: */ fp@260: fp@260: void ec_fsm_change_start(ec_fsm_t *fsm /**< finite state machine */) fp@251: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@251: ec_slave_t *slave = fsm->slave; fp@251: fp@251: // write new state to slave fp@293: ec_datagram_npwr(datagram, slave->station_address, 0x0120, 2); fp@293: EC_WRITE_U16(datagram->data, fsm->change_new); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@251: fsm->change_state = ec_fsm_change_check; fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Change state: CHECK. fp@260: */ fp@260: fp@260: void ec_fsm_change_check(ec_fsm_t *fsm /**< finite state machine */) fp@260: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: ec_slave_t *slave = fsm->slave; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED) { fp@292: fsm->change_state = ec_fsm_change_error; fp@293: EC_ERR("Failed to send state datagram to slave %i!\n", fp@260: fsm->slave->ring_position); fp@292: return; fp@292: } fp@292: fp@293: if (datagram->working_counter != 1) { fp@251: fsm->change_state = ec_fsm_change_error; fp@260: EC_ERR("Failed to set state 0x%02X on slave %i: Slave did not" fp@260: " respond.\n", fsm->change_new, fsm->slave->ring_position); fp@260: return; fp@260: } fp@260: fp@260: fsm->change_start = get_cycles(); fp@251: fp@251: // read AL status from slave fp@293: ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@251: fsm->change_state = ec_fsm_change_status; fp@251: } fp@251: fp@251: /*****************************************************************************/ fp@251: fp@251: /** fp@260: Change state: STATUS. fp@260: */ fp@260: fp@260: void ec_fsm_change_status(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@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@292: fsm->change_state = ec_fsm_change_error; fp@260: EC_ERR("Failed to check state 0x%02X on slave %i.\n", fp@260: fsm->change_new, slave->ring_position); fp@251: return; fp@251: } fp@251: fp@293: slave->current_state = EC_READ_U8(datagram->data); fp@251: fp@260: if (slave->current_state == fsm->change_new) { fp@260: // state has been set successfully fp@260: fsm->change_state = ec_fsm_change_end; fp@260: return; fp@260: } fp@260: fp@260: if (slave->current_state & 0x10) { fp@260: // state change error fp@260: fsm->change_new = slave->current_state & 0x0F; fp@251: EC_ERR("Failed to set state 0x%02X - Slave %i refused state change" fp@251: " (code 0x%02X)!\n", fsm->change_new, slave->ring_position, fp@251: slave->current_state); fp@251: // fetch AL status error code fp@293: ec_datagram_nprd(datagram, slave->station_address, 0x0134, 2); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@251: fsm->change_state = ec_fsm_change_code; fp@251: return; fp@251: } fp@251: fp@260: if (get_cycles() - fsm->change_start >= (cycles_t) 10 * cpu_khz) { fp@260: // timeout while checking fp@260: fsm->change_state = ec_fsm_change_error; fp@260: EC_ERR("Timeout while setting state 0x%02X on slave %i.\n", fp@260: fsm->change_new, slave->ring_position); fp@260: return; fp@260: } fp@260: fp@260: // still old state: check again fp@293: ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@251: Application layer status messages. fp@251: */ fp@251: fp@251: const ec_code_msg_t al_status_messages[] = { fp@251: {0x0001, "Unspecified error"}, fp@251: {0x0011, "Invalud requested state change"}, fp@251: {0x0012, "Unknown requested state"}, fp@251: {0x0013, "Bootstrap not supported"}, fp@251: {0x0014, "No valid firmware"}, fp@251: {0x0015, "Invalid mailbox configuration"}, fp@251: {0x0016, "Invalid mailbox configuration"}, fp@251: {0x0017, "Invalid sync manager configuration"}, fp@251: {0x0018, "No valid inputs available"}, fp@251: {0x0019, "No valid outputs"}, fp@251: {0x001A, "Synchronisation error"}, fp@251: {0x001B, "Sync manager watchdog"}, fp@297: {0x001C, "Invalid sync manager types"}, fp@297: {0x001D, "Invalid output configuration"}, fp@297: {0x001E, "Invalid input configuration"}, fp@297: {0x001F, "Invalid watchdog configuration"}, fp@251: {0x0020, "Slave needs cold start"}, fp@251: {0x0021, "Slave needs INIT"}, fp@251: {0x0022, "Slave needs PREOP"}, fp@251: {0x0023, "Slave needs SAVEOP"}, fp@297: {0x0030, "Invalid DC SYNCH configuration"}, fp@297: {0x0031, "Invalid DC latch configuration"}, fp@297: {0x0032, "PLL error"}, fp@297: {0x0033, "Invalid DC IO error"}, fp@297: {0x0034, "Invalid DC timeout error"}, fp@297: {0x0042, "MBOX EOE"}, fp@297: {0x0043, "MBOX COE"}, fp@297: {0x0044, "MBOX FOE"}, fp@297: {0x0045, "MBOX SOE"}, fp@297: {0x004F, "MBOX VOE"}, fp@251: {} fp@251: }; fp@251: fp@251: /*****************************************************************************/ fp@286: fp@286: /** fp@286: Change state: CODE. fp@286: */ fp@286: fp@286: void ec_fsm_change_code(ec_fsm_t *fsm /**< finite state machine */) fp@286: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@286: ec_slave_t *slave = fsm->slave; fp@286: uint32_t code; fp@286: const ec_code_msg_t *al_msg; fp@286: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@292: fsm->change_state = ec_fsm_change_error; fp@293: EC_ERR("Reception of AL status code datagram failed.\n"); fp@293: return; fp@293: } fp@293: fp@293: if ((code = EC_READ_U16(datagram->data))) { fp@286: for (al_msg = al_status_messages; al_msg->code; al_msg++) { fp@286: if (al_msg->code != code) continue; fp@286: EC_ERR("AL status message 0x%04X: \"%s\".\n", fp@286: al_msg->code, al_msg->message); fp@286: break; fp@286: } fp@286: if (!al_msg->code) fp@286: EC_ERR("Unknown AL status code 0x%04X.\n", code); fp@286: } fp@286: fp@286: // acknowledge "old" slave state fp@293: ec_datagram_npwr(datagram, slave->station_address, 0x0120, 2); fp@293: EC_WRITE_U16(datagram->data, slave->current_state); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@286: fsm->change_state = ec_fsm_change_ack; fp@286: } fp@286: fp@286: /*****************************************************************************/ fp@286: fp@286: /** fp@286: Change state: ACK. fp@286: */ fp@286: fp@286: void ec_fsm_change_ack(ec_fsm_t *fsm /**< finite state machine */) fp@286: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: ec_slave_t *slave = fsm->slave; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@292: fsm->change_state = ec_fsm_change_error; fp@293: EC_ERR("Reception of state ack datagram failed.\n"); fp@286: return; fp@286: } fp@286: fp@286: // read new AL status fp@293: ec_datagram_nprd(datagram, slave->station_address, 0x0130, 2); fp@293: ec_master_queue_datagram(fsm->master, datagram); fp@286: fsm->change_state = ec_fsm_change_ack2; fp@286: } fp@286: fp@286: /*****************************************************************************/ fp@286: fp@286: /** fp@286: Change state: ACK. fp@286: Acknowledge 2. fp@286: */ fp@286: fp@286: void ec_fsm_change_ack2(ec_fsm_t *fsm /**< finite state machine */) fp@286: { fp@293: ec_datagram_t *datagram = &fsm->datagram; fp@293: ec_slave_t *slave = fsm->slave; fp@293: fp@293: if (datagram->state != EC_CMD_RECEIVED || datagram->working_counter != 1) { fp@292: fsm->change_state = ec_fsm_change_error; fp@293: EC_ERR("Reception of state ack check datagram failed.\n"); fp@293: return; fp@293: } fp@293: fp@293: slave->current_state = EC_READ_U8(datagram->data); fp@292: fp@292: if (slave->current_state == fsm->change_new) { fp@286: fsm->change_state = ec_fsm_change_error; fp@286: EC_INFO("Acknowleged state 0x%02X on slave %i.\n", fp@286: slave->current_state, slave->ring_position); fp@292: return; fp@292: } fp@292: fp@292: fsm->change_state = ec_fsm_change_error; fp@286: EC_WARN("Failed to acknowledge state 0x%02X on slave %i" fp@286: " - Timeout!\n", fsm->change_new, slave->ring_position); fp@286: } fp@286: fp@286: /*****************************************************************************/ fp@286: fp@286: /** fp@286: Change state: END. fp@286: */ fp@286: fp@286: void ec_fsm_change_end(ec_fsm_t *fsm /**< finite state machine */) fp@286: { fp@286: } fp@286: fp@286: /*****************************************************************************/ fp@286: fp@286: /** fp@286: Change state: ERROR. fp@286: */ fp@286: fp@286: void ec_fsm_change_error(ec_fsm_t *fsm /**< finite state machine */) fp@286: { fp@286: } fp@286: fp@286: /*****************************************************************************/