fp@436: /****************************************************************************** fp@436: * fp@436: * $Id$ fp@436: * fp@436: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@436: * fp@436: * This file is part of the IgH EtherCAT Master. fp@436: * fp@436: * The IgH EtherCAT Master is free software; you can redistribute it fp@436: * and/or modify it under the terms of the GNU General Public License fp@436: * as published by the Free Software Foundation; either version 2 of the fp@436: * License, or (at your option) any later version. fp@436: * fp@436: * The IgH EtherCAT Master is distributed in the hope that it will be fp@436: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@436: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@436: * GNU General Public License for more details. fp@436: * fp@436: * You should have received a copy of the GNU General Public License fp@436: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@436: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@436: * fp@436: * The right to use EtherCAT Technology is granted and comes free of fp@436: * charge under condition of compatibility of product made by fp@436: * Licensee. People intending to distribute/sell products based on the fp@436: * code, have to sign an agreement to guarantee that products using fp@436: * software based on IgH EtherCAT master stay compatible with the actual fp@436: * EtherCAT specification (which are released themselves as an open fp@436: * standard) as the (only) precondition to have the right to use EtherCAT fp@436: * Technology, IP and trade marks. fp@436: * fp@436: *****************************************************************************/ fp@436: fp@436: /** fp@436: \file fp@436: EtherCAT CoE state machines. fp@436: */ fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: #include "globals.h" fp@436: #include "master.h" fp@436: #include "mailbox.h" fp@436: #include "fsm_coe.h" fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: void ec_fsm_coe_dict_start(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_request(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_check(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_response(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *); fp@436: fp@436: void ec_fsm_coe_down_start(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_down_request(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_down_check(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_down_response(ec_fsm_coe_t *); fp@436: fp@436: void ec_fsm_coe_up_start(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_up_request(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_up_check(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_up_response(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *); fp@436: fp@436: void ec_fsm_coe_end(ec_fsm_coe_t *); fp@436: void ec_fsm_coe_error(ec_fsm_coe_t *); fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@814: Sdo abort messages. fp@814: The "abort Sdo transfer request" supplies an abort code, fp@436: which can be translated to clear text. This table does fp@436: the mapping of the codes and messages. fp@436: */ fp@436: fp@436: const ec_code_msg_t sdo_abort_messages[] = { fp@436: {0x05030000, "Toggle bit not changed"}, fp@814: {0x05040000, "Sdo protocol timeout"}, fp@436: {0x05040001, "Client/Server command specifier not valid or unknown"}, fp@436: {0x05040005, "Out of memory"}, fp@436: {0x06010000, "Unsupported access to an object"}, fp@436: {0x06010001, "Attempt to read a write-only object"}, fp@436: {0x06010002, "Attempt to write a read-only object"}, fp@436: {0x06020000, "This object does not exist in the object directory"}, fp@814: {0x06040041, "The object cannot be mapped into the Pdo"}, fp@436: {0x06040042, "The number and length of the objects to be mapped would" fp@814: " exceed the Pdo length"}, fp@436: {0x06040043, "General parameter incompatibility reason"}, fp@436: {0x06040047, "Gerneral internal incompatibility in device"}, fp@436: {0x06060000, "Access failure due to a hardware error"}, fp@436: {0x06070010, "Data type does not match, length of service parameter does" fp@436: " not match"}, fp@436: {0x06070012, "Data type does not match, length of service parameter too" fp@436: " high"}, fp@436: {0x06070013, "Data type does not match, length of service parameter too" fp@436: " low"}, fp@436: {0x06090011, "Subindex does not exist"}, fp@436: {0x06090030, "Value range of parameter exceeded"}, fp@436: {0x06090031, "Value of parameter written too high"}, fp@436: {0x06090032, "Value of parameter written too low"}, fp@436: {0x06090036, "Maximum value is less than minimum value"}, fp@436: {0x08000000, "General error"}, fp@436: {0x08000020, "Data cannot be transferred or stored to the application"}, fp@436: {0x08000021, "Data cannot be transferred or stored to the application" fp@436: " because of local control"}, fp@436: {0x08000022, "Data cannot be transferred or stored to the application" fp@436: " because of the present device state"}, fp@436: {0x08000023, "Object dictionary dynamic generation fails or no object" fp@436: " dictionary is present"}, fp@436: {} fp@436: }; fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@814: Outputs an Sdo abort message. fp@436: */ fp@436: fp@436: void ec_canopen_abort_msg(uint32_t abort_code) fp@436: { fp@436: const ec_code_msg_t *abort_msg; fp@436: fp@436: for (abort_msg = sdo_abort_messages; abort_msg->code; abort_msg++) { fp@436: if (abort_msg->code == abort_code) { fp@814: EC_ERR("Sdo abort message 0x%08X: \"%s\".\n", fp@436: abort_msg->code, abort_msg->message); fp@436: return; fp@436: } fp@436: } fp@436: fp@814: EC_ERR("Unknown Sdo abort code 0x%08X.\n", abort_code); fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: Constructor. fp@436: */ fp@436: fp@436: void ec_fsm_coe_init(ec_fsm_coe_t *fsm, /**< finite state machine */ fp@436: ec_datagram_t *datagram /**< datagram */ fp@436: ) fp@436: { fp@436: fsm->state = NULL; fp@436: fsm->datagram = datagram; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: Destructor. fp@436: */ fp@436: fp@436: void ec_fsm_coe_clear(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@814: Starts reading a slaves' Sdo dictionary. fp@436: */ fp@436: fp@436: void ec_fsm_coe_dictionary(ec_fsm_coe_t *fsm, /**< finite state machine */ fp@436: ec_slave_t *slave /**< EtherCAT slave */ fp@436: ) fp@436: { fp@436: fsm->slave = slave; fp@436: fsm->state = ec_fsm_coe_dict_start; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@814: Starts to download an Sdo to a slave. fp@436: */ fp@436: fp@436: void ec_fsm_coe_download(ec_fsm_coe_t *fsm, /**< finite state machine */ fp@436: ec_slave_t *slave, /**< EtherCAT slave */ fp@814: ec_sdo_data_t *sdodata /**< Sdo data object */ fp@436: ) fp@436: { fp@436: fsm->slave = slave; fp@436: fsm->sdodata = sdodata; fp@436: fsm->state = ec_fsm_coe_down_start; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@814: Starts to upload an Sdo from a slave. fp@436: */ fp@436: fp@436: void ec_fsm_coe_upload(ec_fsm_coe_t *fsm, /**< finite state machine */ fp@436: ec_slave_t *slave, /**< EtherCAT slave */ fp@814: ec_sdo_request_t *request /**< Sdo request */ fp@436: ) fp@436: { fp@436: fsm->slave = slave; fp@436: fsm->request = request; fp@436: fsm->state = ec_fsm_coe_up_start; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: Executes the current state of the state machine. fp@436: \return false, if state machine has terminated fp@436: */ fp@436: fp@436: int ec_fsm_coe_exec(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: fsm->state(fsm); fp@436: fp@436: return fsm->state != ec_fsm_coe_end && fsm->state != ec_fsm_coe_error; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: Returns, if the state machine terminated with success. fp@436: \return non-zero if successful. fp@436: */ fp@436: fp@436: int ec_fsm_coe_success(ec_fsm_coe_t *fsm /**< Finite state machine */) fp@436: { fp@436: return fsm->state == ec_fsm_coe_end; fp@436: } fp@436: fp@436: /****************************************************************************** fp@436: * CoE dictionary state machine fp@436: *****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT START. fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_start(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: uint8_t *data; fp@436: fp@825: if (!(slave->sii_mailbox_protocols & EC_MBOX_COE)) { fp@825: EC_ERR("Slave %u does not support CoE!\n", slave->ring_position); fp@825: fsm->state = ec_fsm_coe_error; fp@825: return; fp@825: } fp@825: fp@436: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x8 << 12); // Sdo information fp@436: EC_WRITE_U8 (data + 2, 0x01); // Get OD List Request fp@436: EC_WRITE_U8 (data + 3, 0x00); fp@436: EC_WRITE_U16(data + 4, 0x0000); fp@814: EC_WRITE_U16(data + 6, 0x0001); // deliver all Sdos! fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_request; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT REQUEST. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_request(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: request again? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@505: EC_ERR("Failed to receive CoE dictionary request datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE dictionary request failed on slave %i: ", fp@713: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: fsm->cycles_start = datagram->cycles_sent; fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_check; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT CHECK. fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_check(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@594: EC_ERR("Failed to receive CoE mailbox check datagram for slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE mailbox check datagram failed on slave %i: ", fp@436: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!ec_slave_mbox_check(datagram)) { fp@436: if (datagram->cycles_received fp@436: - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { fp@436: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Timeout while checking Sdo dictionary on slave %i.\n", fp@436: slave->ring_position); fp@436: return; fp@436: } fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: return; fp@436: } fp@436: fp@436: // Fetch response fp@436: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_response; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT RESPONSE. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_response(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: uint8_t *data, mbox_prot; fp@436: size_t rec_size; fp@436: unsigned int sdo_count, i; fp@769: uint16_t sdo_index, fragments_left; fp@436: ec_sdo_t *sdo; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: request again? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@505: EC_ERR("Failed to receive CoE dictionary response datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE dictionary response failed on slave %i: ", fp@713: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!(data = ec_slave_mbox_fetch(slave, datagram, fp@436: &mbox_prot, &rec_size))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (mbox_prot != 0x03) { // CoE fp@446: EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@436: fsm->state = ec_fsm_coe_error; fp@769: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 == 0x8 && // Sdo information fp@436: (EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response fp@814: EC_ERR("Sdo information error response at slave %i!\n", fp@436: slave->ring_position); fp@436: ec_canopen_abort_msg(EC_READ_U32(data + 6)); fp@436: fsm->state = ec_fsm_coe_error; fp@769: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 != 0x8 || // Sdo information fp@436: (EC_READ_U8 (data + 2) & 0x7F) != 0x02) { // Get OD List response fp@814: EC_ERR("Invalid Sdo list response at slave %i!\n", fp@436: slave->ring_position); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@769: return; fp@436: } fp@436: fp@436: if (rec_size < 8) { fp@436: EC_ERR("Invalid data size!\n"); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@769: return; fp@436: } fp@436: fp@436: sdo_count = (rec_size - 8) / 2; fp@436: fp@436: for (i = 0; i < sdo_count; i++) { fp@436: sdo_index = EC_READ_U16(data + 8 + i * 2); fp@436: if (!sdo_index) { fp@461: if (slave->master->debug_level) fp@814: EC_WARN("Sdo dictionary of slave %i contains index 0x0000.\n", fp@461: slave->ring_position); fp@436: continue; fp@436: } fp@436: fp@436: if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_ATOMIC))) { fp@814: EC_ERR("Failed to allocate memory for Sdo!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (ec_sdo_init(sdo, sdo_index, slave)) { fp@814: EC_ERR("Failed to init Sdo!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: list_add_tail(&sdo->list, &slave->sdo_dictionary); fp@436: } fp@436: fp@769: fragments_left = EC_READ_U16(data + 4); fp@769: if (slave->master->debug_level && fragments_left) { fp@814: EC_DBG("Sdo list fragments left: %u\n", fragments_left); fp@769: } fp@769: fp@769: if (EC_READ_U8(data + 2) & 0x80 || fragments_left) { // more messages waiting. check again. fp@436: fsm->cycles_start = datagram->cycles_sent; fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_check; fp@436: return; fp@436: } fp@436: fp@436: if (list_empty(&slave->sdo_dictionary)) { fp@814: // no Sdos in dictionary. finished. fp@436: fsm->state = ec_fsm_coe_end; // success fp@436: return; fp@436: } fp@436: fp@814: // fetch Sdo descriptions fp@436: fsm->sdo = list_entry(slave->sdo_dictionary.next, ec_sdo_t, list); fp@436: fp@436: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x8 << 12); // Sdo information fp@436: EC_WRITE_U8 (data + 2, 0x03); // Get object description request fp@436: EC_WRITE_U8 (data + 3, 0x00); fp@436: EC_WRITE_U16(data + 4, 0x0000); fp@814: EC_WRITE_U16(data + 6, fsm->sdo->index); // Sdo index fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_desc_request; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT DESC REQUEST. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: check for response first? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Failed to receive CoE Sdo description request datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Reception of CoE Sdo description" fp@713: " request failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: fsm->cycles_start = datagram->cycles_sent; fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_desc_check; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT DESC CHECK. fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@594: EC_ERR("Failed to receive CoE mailbox check datagram from slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@436: return; fp@436: } fp@436: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE mailbox check" fp@713: " datagram failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@505: return; fp@505: } fp@505: fp@436: if (!ec_slave_mbox_check(datagram)) { fp@436: if (datagram->cycles_received fp@436: - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { fp@436: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Timeout while checking Sdo description on slave %i.\n", fp@436: slave->ring_position); fp@436: return; fp@436: } fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: return; fp@436: } fp@436: fp@436: // Fetch response fp@436: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_desc_response; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT DESC RESPONSE. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *fsm fp@436: /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: ec_sdo_t *sdo = fsm->sdo; fp@436: uint8_t *data, mbox_prot; fp@436: size_t rec_size, name_size; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: request again? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Failed to receive CoE Sdo description response datagram from" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Reception of CoE Sdo description" fp@713: " response failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!(data = ec_slave_mbox_fetch(slave, datagram, fp@436: &mbox_prot, &rec_size))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (mbox_prot != 0x03) { // CoE fp@446: EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 == 0x8 && // Sdo information fp@436: (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response fp@814: EC_ERR("Sdo information error response at slave %i while" fp@814: " fetching Sdo 0x%04X!\n", slave->ring_position, fp@436: sdo->index); fp@436: ec_canopen_abort_msg(EC_READ_U32(data + 6)); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 != 0x8 || // Sdo information fp@436: (EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response fp@814: EC_READ_U16(data + 6) != sdo->index) { // Sdo index fp@436: EC_ERR("Invalid object description response at slave %i while" fp@814: " fetching Sdo 0x%04X!\n", slave->ring_position, fp@436: sdo->index); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (rec_size < 12) { fp@436: EC_ERR("Invalid data size!\n"); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: sdo->subindices = EC_READ_U8(data + 10); fp@436: sdo->object_code = EC_READ_U8(data + 11); fp@436: fp@436: name_size = rec_size - 12; fp@436: if (name_size) { fp@436: if (!(sdo->name = kmalloc(name_size + 1, GFP_ATOMIC))) { fp@814: EC_ERR("Failed to allocate Sdo name!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: memcpy(sdo->name, data + 12, name_size); fp@436: sdo->name[name_size] = 0; fp@436: } fp@436: fp@436: if (EC_READ_U8(data + 2) & 0x80) { fp@436: EC_ERR("Fragment follows (not implemented)!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: // start fetching entries fp@436: fp@436: fsm->subindex = 0; fp@436: fp@436: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x8 << 12); // Sdo information fp@436: EC_WRITE_U8 (data + 2, 0x05); // Get entry description request fp@436: EC_WRITE_U8 (data + 3, 0x00); fp@436: EC_WRITE_U16(data + 4, 0x0000); fp@814: EC_WRITE_U16(data + 6, sdo->index); // Sdo index fp@814: EC_WRITE_U8 (data + 8, fsm->subindex); // Sdo subindex fp@436: EC_WRITE_U8 (data + 9, 0x00); // value info (no values) fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_entry_request; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT ENTRY REQUEST. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *fsm fp@436: /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: check for response first? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Failed to receive CoE Sdo entry request datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Reception of CoE Sdo entry request failed on slave %i: ", fp@713: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: fsm->cycles_start = datagram->cycles_sent; fp@436: fp@505: ec_slave_mbox_prepare_check(slave, datagram); // can not fail fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_entry_check; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT ENTRY CHECK. fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *fsm fp@436: /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@594: EC_ERR("Failed to receive CoE mailbox check datagram from slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@436: return; fp@436: } fp@436: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE mailbox check" fp@713: " datagram failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@505: return; fp@505: } fp@505: fp@436: if (!ec_slave_mbox_check(datagram)) { fp@436: if (datagram->cycles_received fp@436: - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { fp@436: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Timeout while checking Sdo entry on slave %i.\n", fp@436: slave->ring_position); fp@436: return; fp@436: } fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: return; fp@436: } fp@436: fp@436: // Fetch response fp@436: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_entry_response; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DICT ENTRY RESPONSE. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *fsm fp@436: /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: ec_sdo_t *sdo = fsm->sdo; fp@436: uint8_t *data, mbox_prot; fp@436: size_t rec_size, data_size; fp@436: ec_sdo_entry_t *entry; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: request again? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Failed to receive CoE Sdo description response datagram from" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Reception of CoE Sdo description" fp@713: " response failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!(data = ec_slave_mbox_fetch(slave, datagram, fp@436: &mbox_prot, &rec_size))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (mbox_prot != 0x03) { // CoE fp@446: EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 == 0x8 && // Sdo information fp@436: (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response fp@814: EC_ERR("Sdo information error response at slave %i while" fp@814: " fetching Sdo entry 0x%04X:%i!\n", slave->ring_position, fp@436: sdo->index, fsm->subindex); fp@436: ec_canopen_abort_msg(EC_READ_U32(data + 6)); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 != 0x8 || // Sdo information fp@436: (EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response fp@814: EC_READ_U16(data + 6) != sdo->index || // Sdo index fp@814: EC_READ_U8(data + 8) != fsm->subindex) { // Sdo subindex fp@436: EC_ERR("Invalid entry description response at slave %i while" fp@814: " fetching Sdo entry 0x%04X:%i!\n", slave->ring_position, fp@436: sdo->index, fsm->subindex); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (rec_size < 16) { fp@436: EC_ERR("Invalid data size!\n"); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: data_size = rec_size - 16; fp@436: fp@436: if (!(entry = (ec_sdo_entry_t *) fp@436: kmalloc(sizeof(ec_sdo_entry_t), GFP_ATOMIC))) { fp@436: EC_ERR("Failed to allocate entry!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (ec_sdo_entry_init(entry, fsm->subindex, sdo)) { fp@436: EC_ERR("Failed to init entry!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: entry->data_type = EC_READ_U16(data + 10); fp@436: entry->bit_length = EC_READ_U16(data + 12); fp@436: fp@436: if (data_size) { fp@484: uint8_t *desc; fp@484: if (!(desc = kmalloc(data_size + 1, GFP_ATOMIC))) { fp@814: EC_ERR("Failed to allocate Sdo entry name!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@484: memcpy(desc, data + 16, data_size); fp@484: desc[data_size] = 0; fp@484: entry->description = desc; fp@436: } fp@436: fp@436: list_add_tail(&entry->list, &sdo->entries); fp@436: fp@436: if (fsm->subindex < sdo->subindices) { fp@436: fsm->subindex++; fp@436: fp@436: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x8 << 12); // Sdo information fp@436: EC_WRITE_U8 (data + 2, 0x05); // Get entry description request fp@436: EC_WRITE_U8 (data + 3, 0x00); fp@436: EC_WRITE_U16(data + 4, 0x0000); fp@814: EC_WRITE_U16(data + 6, sdo->index); // Sdo index fp@814: EC_WRITE_U8 (data + 8, fsm->subindex); // Sdo subindex fp@436: EC_WRITE_U8 (data + 9, 0x00); // value info (no values) fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_entry_request; fp@436: return; fp@436: } fp@436: fp@814: // another Sdo description to fetch? fp@436: if (fsm->sdo->list.next != &slave->sdo_dictionary) { fp@436: fsm->sdo = list_entry(fsm->sdo->list.next, ec_sdo_t, list); fp@436: fp@436: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x8 << 12); // Sdo information fp@436: EC_WRITE_U8 (data + 2, 0x03); // Get object description request fp@436: EC_WRITE_U8 (data + 3, 0x00); fp@436: EC_WRITE_U16(data + 4, 0x0000); fp@814: EC_WRITE_U16(data + 6, fsm->sdo->index); // Sdo index fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_dict_desc_request; fp@436: return; fp@436: } fp@436: fp@436: fsm->state = ec_fsm_coe_end; fp@436: } fp@436: fp@436: /****************************************************************************** fp@436: * CoE state machine fp@436: *****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DOWN START. fp@436: */ fp@436: fp@436: void ec_fsm_coe_down_start(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: ec_sdo_data_t *sdodata = fsm->sdodata; fp@436: uint8_t *data; fp@436: fp@459: if (fsm->slave->master->debug_level) fp@814: EC_DBG("Downloading Sdo 0x%04X:%i to slave %i.\n", fp@459: sdodata->index, sdodata->subindex, slave->ring_position); fp@436: fp@825: if (!(slave->sii_mailbox_protocols & EC_MBOX_COE)) { fp@825: EC_ERR("Slave %u does not support CoE!\n", slave->ring_position); fp@825: fsm->state = ec_fsm_coe_error; fp@825: return; fp@825: } fp@825: fp@436: if (slave->sii_rx_mailbox_size < 6 + 10 + sdodata->size) { fp@814: EC_ERR("Sdo fragmenting not supported yet!\n"); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, fp@436: sdodata->size + 10))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x2 << 12); // Sdo request fp@436: EC_WRITE_U8 (data + 2, (0x1 // size specified fp@436: | 0x1 << 5)); // Download request fp@436: EC_WRITE_U16(data + 3, sdodata->index); fp@436: EC_WRITE_U8 (data + 5, sdodata->subindex); fp@436: EC_WRITE_U32(data + 6, sdodata->size); fp@436: memcpy(data + 10, sdodata->data, sdodata->size); fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_down_request; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DOWN REQUEST. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_down_request(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: check for response first? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@505: EC_ERR("Failed to receive CoE download request datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE download request failed on slave %i: ", fp@713: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: fsm->cycles_start = datagram->cycles_sent; fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_down_check; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DOWN CHECK. fp@436: */ fp@436: fp@436: void ec_fsm_coe_down_check(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@594: EC_ERR("Failed to receive CoE mailbox check datagram for slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE mailbox check" fp@713: " datagram failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!ec_slave_mbox_check(datagram)) { fp@436: if (datagram->cycles_received fp@436: - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { fp@436: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Timeout while checking Sdo configuration on slave %i.\n", fp@436: slave->ring_position); fp@436: return; fp@436: } fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: return; fp@436: } fp@436: fp@436: // Fetch response fp@436: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_down_response; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: DOWN RESPONSE. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_down_response(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: uint8_t *data, mbox_prot; fp@436: size_t rec_size; fp@436: ec_sdo_data_t *sdodata = fsm->sdodata; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: request again? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@505: EC_ERR("Failed to receive CoE download response datagram from" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE download response failed on slave %i: ", fp@713: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!(data = ec_slave_mbox_fetch(slave, datagram, fp@436: &mbox_prot, &rec_size))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (mbox_prot != 0x03) { // CoE fp@446: EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (rec_size < 6) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: EC_ERR("Received data is too small (%i bytes):\n", rec_size); fp@436: ec_print_data(data, rec_size); fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 == 0x2 && // Sdo request fp@814: EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort Sdo transfer request fp@814: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Sdo download 0x%04X:%X (%i bytes) aborted on slave %i.\n", fp@436: sdodata->index, sdodata->subindex, sdodata->size, fp@436: slave->ring_position); fp@436: if (rec_size < 10) { fp@436: EC_ERR("Incomplete Abort command:\n"); fp@436: ec_print_data(data, rec_size); fp@436: } fp@436: else fp@436: ec_canopen_abort_msg(EC_READ_U32(data + 6)); fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response fp@436: EC_READ_U8 (data + 2) >> 5 != 0x3 || // Download response fp@436: EC_READ_U16(data + 3) != sdodata->index || // index fp@436: EC_READ_U8 (data + 5) != sdodata->subindex) { // subindex fp@436: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Sdo download 0x%04X:%X (%i bytes) failed:\n", fp@436: sdodata->index, sdodata->subindex, sdodata->size); fp@814: EC_ERR("Invalid Sdo download response at slave %i!\n", fp@436: slave->ring_position); fp@436: ec_print_data(data, rec_size); fp@436: return; fp@436: } fp@436: fp@436: fsm->state = ec_fsm_coe_end; // success fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: UP START. fp@436: */ fp@436: fp@436: void ec_fsm_coe_up_start(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: ec_master_t *master = slave->master; fp@436: ec_sdo_request_t *request = fsm->request; fp@436: ec_sdo_entry_t *entry = request->entry; fp@436: uint8_t *data; fp@436: fp@459: if (master->debug_level) fp@814: EC_DBG("Uploading Sdo 0x%04X:%i from slave %i.\n", fp@739: entry->sdo->index, entry->subindex, slave->ring_position); fp@436: fp@825: if (!(slave->sii_mailbox_protocols & EC_MBOX_COE)) { fp@825: EC_ERR("Slave %u does not support CoE!\n", slave->ring_position); fp@825: fsm->state = ec_fsm_coe_error; fp@825: return; fp@825: } fp@825: fp@476: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x2 << 12); // Sdo request fp@436: EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request fp@739: EC_WRITE_U16(data + 3, entry->sdo->index); fp@436: EC_WRITE_U8 (data + 5, entry->subindex); fp@476: memset(data + 6, 0x00, 4); fp@436: fp@436: if (master->debug_level) { fp@436: EC_DBG("Upload request:\n"); fp@476: ec_print_data(data, 10); fp@436: } fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_up_request; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: UP REQUEST. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_up_request(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: check for response first? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@594: EC_ERR("Failed to receive CoE upload request for slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE upload request failed on slave %i: ", fp@713: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: fsm->cycles_start = datagram->cycles_sent; fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_up_check; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: UP CHECK. fp@436: */ fp@436: fp@436: void ec_fsm_coe_up_check(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@594: EC_ERR("Failed to receive CoE mailbox check datagram from slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE mailbox check" fp@713: " datagram failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!ec_slave_mbox_check(datagram)) { fp@436: if (datagram->cycles_received fp@436: - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { fp@436: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Timeout while checking Sdo upload on slave %i.\n", fp@436: slave->ring_position); fp@436: return; fp@436: } fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: return; fp@436: } fp@436: fp@436: // Fetch response fp@436: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_up_response; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: UP RESPONSE. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_up_response(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: ec_master_t *master = slave->master; fp@436: uint8_t *data, mbox_prot; fp@436: size_t rec_size, data_size; fp@436: ec_sdo_request_t *request = fsm->request; fp@436: ec_sdo_entry_t *entry = request->entry; fp@436: uint32_t complete_size; fp@436: unsigned int expedited, size_specified; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: request again? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@505: EC_ERR("Failed to receive CoE upload response datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE upload response failed on slave %i: ", fp@713: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!(data = ec_slave_mbox_fetch(slave, datagram, fp@436: &mbox_prot, &rec_size))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (master->debug_level) { fp@436: EC_DBG("Upload response:\n"); fp@436: ec_print_data(data, rec_size); fp@436: } fp@436: fp@436: if (mbox_prot != 0x03) { // CoE fp@436: EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@436: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@769: if (rec_size < 3) { fp@814: EC_ERR("Received currupted Sdo upload response (%u bytes)!\n", rec_size); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@769: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 == 0x2 && // Sdo request fp@814: EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort Sdo transfer request fp@814: EC_ERR("Sdo upload 0x%04X:%X aborted on slave %i.\n", fp@739: entry->sdo->index, entry->subindex, slave->ring_position); fp@769: if (rec_size >= 10) fp@769: ec_canopen_abort_msg(EC_READ_U32(data + 6)); fp@769: else fp@769: EC_ERR("No abort message.\n"); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@769: if (request->data) { fp@769: kfree(request->data); fp@769: request->data = NULL; fp@769: } fp@769: request->size = 0; fp@769: fp@769: // normal or expedited? fp@436: expedited = EC_READ_U8(data + 2) & 0x02; fp@436: fp@436: if (expedited) { fp@769: if (rec_size < 7) { fp@814: EC_ERR("Received currupted Sdo expedited upload" fp@769: " response (only %u bytes)!\n", rec_size); fp@769: ec_print_data(data, rec_size); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@814: if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response fp@769: EC_READ_U8 (data + 2) >> 5 != 0x2 || // upload response fp@769: EC_READ_U16(data + 3) != entry->sdo->index || // index fp@769: EC_READ_U8 (data + 5) != entry->subindex) { // subindex fp@814: EC_ERR("Sdo upload 0x%04X:%X failed:\n", entry->sdo->index, entry->subindex); fp@814: EC_ERR("Invalid Sdo upload response at slave %i!\n", fp@769: slave->ring_position); fp@769: ec_print_data(data, rec_size); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@436: size_specified = EC_READ_U8(data + 2) & 0x01; fp@436: if (size_specified) { fp@436: complete_size = 4 - ((EC_READ_U8(data + 2) & 0x0C) >> 2); fp@769: } else { fp@436: complete_size = 4; fp@436: } fp@769: fp@769: if (rec_size < 6 + complete_size) { fp@814: EC_ERR("Received currupted Sdo expedited upload" fp@769: " response (only %u bytes)!\n", rec_size); fp@769: ec_print_data(data, rec_size); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@446: if (!(request->data = (uint8_t *) fp@769: kmalloc(complete_size + 1, GFP_ATOMIC))) { fp@814: EC_ERR("Failed to allocate %i bytes of Sdo data!\n", fp@769: complete_size); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: request->data[complete_size] = 0x00; // just to be sure... fp@769: fp@436: memcpy(request->data, data + 6, complete_size); fp@436: request->size = complete_size; fp@769: fp@769: } else { // normal fp@769: if (rec_size < 10) { fp@814: EC_ERR("Received currupted Sdo normal upload" fp@769: " response (only %u bytes)!\n", rec_size); fp@769: ec_print_data(data, rec_size); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@814: if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response fp@769: EC_READ_U8 (data + 2) >> 5 != 0x2 || // upload response fp@769: EC_READ_U16(data + 3) != entry->sdo->index || // index fp@769: EC_READ_U8 (data + 5) != entry->subindex) { // subindex fp@814: EC_ERR("Sdo upload 0x%04X:%X failed:\n", entry->sdo->index, entry->subindex); fp@814: EC_ERR("Invalid Sdo upload response at slave %i!\n", fp@769: slave->ring_position); fp@769: ec_print_data(data, rec_size); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@769: data_size = rec_size - 10; fp@769: complete_size = EC_READ_U32(data + 6); fp@769: fp@769: if (!complete_size) { fp@769: EC_ERR("No complete size supplied!\n"); fp@769: ec_print_data(data, rec_size); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: fp@769: if (!(request->data = (uint8_t *) fp@769: kmalloc(complete_size + 1, GFP_ATOMIC))) { fp@814: EC_ERR("Failed to allocate %i bytes of Sdo data!\n", fp@769: complete_size); fp@769: fsm->state = ec_fsm_coe_error; fp@769: return; fp@769: } fp@769: request->data[complete_size] = 0x00; // just to be sure... fp@769: fp@436: memcpy(request->data, data + 10, data_size); fp@769: request->size = complete_size; fp@436: fsm->toggle = 0; fp@436: fp@436: if (data_size < complete_size) { fp@814: EC_WARN("Sdo data incomplete (%i / %i).\n", fp@446: data_size, complete_size); fp@446: fp@446: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, fp@446: 0x03, 3))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x2 << 12); // Sdo request fp@436: EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle fp@436: | 0x3 << 5)); // upload segment request fp@436: fp@436: if (master->debug_level) { fp@436: EC_DBG("Upload segment request:\n"); fp@436: ec_print_data(data, 3); fp@436: } fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_up_seg_request; fp@436: return; fp@436: } fp@436: } fp@436: fp@436: fsm->state = ec_fsm_coe_end; // success fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: UP REQUEST. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: check for response first? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@505: EC_ERR("Failed to receive CoE upload segment request datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE upload segment" fp@713: " request failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: fsm->cycles_start = datagram->cycles_sent; fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_up_seg_check; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: UP CHECK. fp@436: */ fp@436: fp@436: void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@594: EC_ERR("Failed to receive CoE mailbox check datagram for slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE mailbox check" fp@713: " datagram failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!ec_slave_mbox_check(datagram)) { fp@436: if (datagram->cycles_received fp@436: - fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { fp@436: fsm->state = ec_fsm_coe_error; fp@814: EC_ERR("Timeout while checking Sdo upload segment on slave %i.\n", fp@436: slave->ring_position); fp@436: return; fp@436: } fp@436: fp@436: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: return; fp@436: } fp@436: fp@436: // Fetch response fp@436: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_up_seg_response; fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: CoE state: UP RESPONSE. fp@505: \todo Timeout behavior fp@436: */ fp@436: fp@436: void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: ec_datagram_t *datagram = fsm->datagram; fp@436: ec_slave_t *slave = fsm->slave; fp@436: ec_master_t *master = slave->master; fp@436: uint8_t *data, mbox_prot; fp@436: size_t rec_size, data_size; fp@436: ec_sdo_request_t *request = fsm->request; fp@436: ec_sdo_entry_t *entry = request->entry; fp@436: uint32_t seg_size; fp@436: unsigned int last_segment; fp@436: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; // FIXME: request again? fp@505: fp@505: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@505: fsm->state = ec_fsm_coe_error; fp@505: EC_ERR("Failed to receive CoE upload segment response datagram for" fp@594: " slave %i (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@505: fsm->state = ec_fsm_coe_error; fp@713: EC_ERR("Reception of CoE upload segment" fp@713: " response failed on slave %i: ", slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@436: return; fp@436: } fp@436: fp@436: if (!(data = ec_slave_mbox_fetch(slave, datagram, fp@436: &mbox_prot, &rec_size))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (master->debug_level) { fp@436: EC_DBG("Upload segment response:\n"); fp@436: ec_print_data(data, rec_size); fp@436: } fp@436: fp@436: if (mbox_prot != 0x03) { // CoE fp@446: EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: if (rec_size < 10) { fp@814: EC_ERR("Received currupted Sdo upload segment response!\n"); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 == 0x2 && // Sdo request fp@814: EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort Sdo transfer request fp@814: EC_ERR("Sdo upload 0x%04X:%X aborted on slave %i.\n", fp@739: entry->sdo->index, entry->subindex, slave->ring_position); fp@436: ec_canopen_abort_msg(EC_READ_U32(data + 6)); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: if (EC_READ_U16(data) >> 12 != 0x3 || // Sdo response fp@436: EC_READ_U8 (data + 2) >> 5 != 0x0) { // upload segment response fp@814: EC_ERR("Sdo upload 0x%04X:%X failed:\n", entry->sdo->index, entry->subindex); fp@814: EC_ERR("Invalid Sdo upload segment response at slave %i!\n", fp@436: slave->ring_position); fp@436: ec_print_data(data, rec_size); fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@436: last_segment = EC_READ_U8(data + 2) & 0x01; fp@436: seg_size = (EC_READ_U8(data + 2) & 0xE) >> 1; fp@436: data_size = rec_size - 10; fp@436: fp@436: if (data_size != seg_size) { fp@814: EC_WARN("Sdo segment data invalid (%i / %i)" fp@436: " - Fragmenting not implemented.\n", fp@436: data_size, seg_size); fp@436: } fp@436: fp@436: memcpy(request->data + request->size, data + 10, data_size); fp@436: request->size += data_size; fp@436: fp@436: if (!last_segment) { fp@436: fsm->toggle = !fsm->toggle; fp@436: fp@436: if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 3))) { fp@436: fsm->state = ec_fsm_coe_error; fp@436: return; fp@436: } fp@436: fp@814: EC_WRITE_U16(data, 0x2 << 12); // Sdo request fp@436: EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle fp@436: | 0x3 << 5)); // upload segment request fp@436: fp@436: if (master->debug_level) { fp@436: EC_DBG("Upload segment request:\n"); fp@436: ec_print_data(data, 3); fp@436: } fp@436: fp@505: fsm->retries = EC_FSM_RETRIES; fp@436: fsm->state = ec_fsm_coe_up_seg_request; fp@436: return; fp@436: } fp@436: fp@436: fsm->state = ec_fsm_coe_end; // success fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: State: ERROR. fp@436: */ fp@436: fp@436: void ec_fsm_coe_error(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: } fp@436: fp@436: /*****************************************************************************/ fp@436: fp@436: /** fp@436: State: END. fp@436: */ fp@436: fp@436: void ec_fsm_coe_end(ec_fsm_coe_t *fsm /**< finite state machine */) fp@436: { fp@436: } fp@436: fp@436: /*****************************************************************************/