fp@1744: /****************************************************************************** fp@1744: * fp@1744: * $Id$ fp@1744: * fp@1744: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1744: * fp@1744: * This file is part of the IgH EtherCAT Master. fp@1744: * fp@1744: * The IgH EtherCAT Master is free software; you can redistribute it fp@1744: * and/or modify it under the terms of the GNU General Public License fp@1744: * as published by the Free Software Foundation; either version 2 of the fp@1744: * License, or (at your option) any later version. fp@1744: * fp@1744: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1744: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1744: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1744: * GNU General Public License for more details. fp@1744: * fp@1744: * You should have received a copy of the GNU General Public License fp@1744: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1744: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1744: * fp@1744: * The right to use EtherCAT Technology is granted and comes free of fp@1744: * charge under condition of compatibility of product made by fp@1744: * Licensee. People intending to distribute/sell products based on the fp@1744: * code, have to sign an agreement to guarantee that products using fp@1744: * software based on IgH EtherCAT master stay compatible with the actual fp@1744: * EtherCAT specification (which are released themselves as an open fp@1744: * standard) as the (only) precondition to have the right to use EtherCAT fp@1744: * Technology, IP and trade marks. fp@1744: * fp@1744: *****************************************************************************/ fp@1744: fp@1744: /** fp@1744: \file fp@1744: EtherCAT PDO mapping state machine. fp@1744: */ fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: #include "globals.h" fp@1744: #include "master.h" fp@1744: #include "mailbox.h" fp@1744: #include "fsm_mapping.h" fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: void ec_fsm_mapping_state_start(ec_fsm_mapping_t *); fp@1744: void ec_fsm_mapping_state_zero_count(ec_fsm_mapping_t *); fp@1744: void ec_fsm_mapping_state_add_pdo(ec_fsm_mapping_t *); fp@1744: void ec_fsm_mapping_state_pdo_count(ec_fsm_mapping_t *); fp@1744: void ec_fsm_mapping_state_end(ec_fsm_mapping_t *); fp@1744: void ec_fsm_mapping_state_error(ec_fsm_mapping_t *); fp@1744: fp@1744: void ec_fsm_mapping_next_sync(ec_fsm_mapping_t *); fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * Constructor. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_init(ec_fsm_mapping_t *fsm, /**< mapping state machine */ fp@1744: ec_fsm_coe_t *fsm_coe /**< CoE state machine to use */ fp@1744: ) fp@1744: { fp@1744: fsm->fsm_coe = fsm_coe; fp@1744: fsm->sdodata.data = (uint8_t *) &fsm->sdo_value; fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * Destructor. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_clear(ec_fsm_mapping_t *fsm /**< mapping state machine */) fp@1744: { fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * Start PDO mapping configuration state machine. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_start( fp@1744: ec_fsm_mapping_t *fsm, /**< mapping state machine */ fp@1744: ec_slave_t *slave /**< slave to configure */ fp@1744: ) fp@1744: { fp@1744: fsm->slave = slave; fp@1744: fsm->state = ec_fsm_mapping_state_start; fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * \return false, if state machine has terminated fp@1744: */ fp@1744: fp@1744: int ec_fsm_mapping_running( fp@1744: const ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: return fsm->state != ec_fsm_mapping_state_end fp@1744: && fsm->state != ec_fsm_mapping_state_error; fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * Executes the current state of the state machine. fp@1744: * If the state machine's datagram is not sent or received yet, the execution fp@1744: * of the state machine is delayed to the next cycle. fp@1744: * \return false, if state machine has terminated fp@1744: */ fp@1744: fp@1744: int ec_fsm_mapping_exec( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: fsm->state(fsm); fp@1744: return ec_fsm_mapping_running(fsm); fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * \return true, if the state machine terminated gracefully fp@1744: */ fp@1744: fp@1744: int ec_fsm_mapping_success( fp@1744: const ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: return fsm->state == ec_fsm_mapping_state_end; fp@1744: } fp@1744: fp@1744: /****************************************************************************** fp@1746: * state functions fp@1744: *****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Start mapping configuration. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_state_start( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: fsm->dir = EC_DIR_OUTPUT; fp@1744: ec_fsm_mapping_next_sync(fsm); fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Process mapping of next sync manager. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_next_sync( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: while (1) { fp@1744: if (fsm->dir > EC_DIR_INPUT) { fp@1744: // no more directions to configure mappings for fp@1744: fsm->state = ec_fsm_mapping_state_end; fp@1744: return; fp@1744: } fp@1744: fp@1744: if (!(fsm->sync = ec_slave_get_pdo_sync(fsm->slave, fsm->dir))) { fp@1744: // no sync manager found for this direction fp@1744: fsm->dir++; fp@1744: continue; fp@1744: } fp@1744: fp@1744: fsm->dir++; fp@1744: if (fsm->sync->alt_mapping) fp@1744: break; fp@1744: } fp@1744: fp@1744: if (fsm->slave->master->debug_level) { fp@1744: EC_DBG("Configuring PDO mapping for SM%u of slave %i.\n", fp@1744: fsm->sync->index, fsm->slave->ring_position); fp@1744: } fp@1744: fp@1744: // set mapped PDO count to zero fp@1744: fsm->sdodata.index = 0x1C10 + fsm->sync->index; fp@1744: fsm->sdodata.subindex = 0; // mapped PDO count fp@1744: EC_WRITE_U8(&fsm->sdo_value, 0); // zero PDOs mapped fp@1744: fsm->sdodata.size = 1; fp@1744: if (fsm->slave->master->debug_level) fp@1744: EC_DBG("Setting PDO count to zero for SM%u.\n", fsm->sync->index); fp@1744: fp@1744: fsm->state = ec_fsm_mapping_state_zero_count; fp@1744: ec_fsm_coe_download(fsm->fsm_coe, fsm->slave, &fsm->sdodata); fp@1744: ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Process mapping of next PDO. fp@1744: */ fp@1744: fp@1744: ec_pdo_t *ec_fsm_mapping_next_pdo( fp@1744: ec_fsm_mapping_t *fsm, /**< mapping state machine */ fp@1744: struct list_head *list /**< current PDO list item */ fp@1744: ) fp@1744: { fp@1744: ec_pdo_t *pdo; fp@1744: fp@1744: do { fp@1744: list = list->next; fp@1744: if (list == &fsm->sync->pdos) fp@1744: return NULL; // no next PDO fp@1744: pdo = list_entry(list, ec_pdo_t, list); fp@1744: } fp@1744: while (pdo->sync_index != fsm->sync->index); fp@1744: fp@1744: return pdo; fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Set the number of mapped PDOs to zero. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_state_zero_count( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: if (ec_fsm_coe_exec(fsm->fsm_coe)) return; fp@1744: fp@1744: if (!ec_fsm_coe_success(fsm->fsm_coe)) { fp@1744: EC_ERR("Failed to clear PDO mapping for slave %u.\n", fp@1744: fsm->slave->ring_position); fp@1744: fsm->state = ec_fsm_mapping_state_error; fp@1744: return; fp@1744: } fp@1744: fp@1744: // map all PDOs belonging to the current sync manager fp@1744: fp@1744: // find first PDO fp@1744: if (!(fsm->pdo = ec_fsm_mapping_next_pdo( fp@1744: fsm, &fsm->sync->pdos))) { fp@1744: if (fsm->slave->master->debug_level) fp@1744: EC_DBG("No PDOs to map for SM%u of slave %u.\n", fp@1744: fsm->sync->index, fsm->slave->ring_position); fp@1744: ec_fsm_mapping_next_sync(fsm); fp@1744: return; fp@1744: } fp@1744: fp@1744: // add first PDO to mapping fp@1744: fsm->pdo_count = 1; fp@1744: fsm->sdodata.subindex = fsm->pdo_count; fp@1744: EC_WRITE_U16(&fsm->sdo_value, fsm->pdo->index); fp@1744: fsm->sdodata.size = 2; fp@1744: fp@1744: if (fsm->slave->master->debug_level) fp@1744: EC_DBG("Mapping PDO 0x%04X at position %u.\n", fp@1744: fsm->pdo->index, fsm->sdodata.subindex); fp@1744: fp@1744: fsm->state = ec_fsm_mapping_state_add_pdo; fp@1744: ec_fsm_coe_download(fsm->fsm_coe, fsm->slave, &fsm->sdodata); fp@1744: ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Add a PDO to the sync managers mapping. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_state_add_pdo( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: if (ec_fsm_coe_exec(fsm->fsm_coe)) return; fp@1744: fp@1744: if (!ec_fsm_coe_success(fsm->fsm_coe)) { fp@1744: EC_ERR("Failed to map PDO 0x%04X for SM%u of slave %u.\n", fp@1744: fsm->pdo->index, fsm->sync->index, fsm->slave->ring_position); fp@1744: fsm->state = ec_fsm_mapping_state_error; fp@1744: return; fp@1744: } fp@1744: fp@1744: // find next PDO fp@1744: if (!(fsm->pdo = ec_fsm_mapping_next_pdo( fp@1744: fsm, &fsm->pdo->list))) { fp@1744: // no more PDOs to map. write PDO count fp@1744: fsm->sdodata.subindex = 0; fp@1744: EC_WRITE_U8(&fsm->sdo_value, fsm->pdo_count); fp@1744: fsm->sdodata.size = 1; fp@1744: fp@1744: if (fsm->slave->master->debug_level) fp@1744: EC_DBG("Setting number of mapped PDOs to %u.\n", fp@1744: fsm->pdo_count); fp@1744: fp@1744: fsm->state = ec_fsm_mapping_state_pdo_count; fp@1744: ec_fsm_coe_download(fsm->fsm_coe, fsm->slave, &fsm->sdodata); fp@1744: ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately fp@1744: return; fp@1744: } fp@1744: fp@1744: // add next PDO to mapping fp@1744: fsm->pdo_count++; fp@1744: fsm->sdodata.subindex = fsm->pdo_count; fp@1744: EC_WRITE_U16(&fsm->sdo_value, fsm->pdo->index); fp@1744: fsm->sdodata.size = 2; fp@1744: fp@1744: if (fsm->slave->master->debug_level) fp@1744: EC_DBG("Mapping PDO 0x%04X at position %u.\n", fp@1744: fsm->pdo->index, fsm->sdodata.subindex); fp@1744: fp@1744: ec_fsm_coe_download(fsm->fsm_coe, fsm->slave, &fsm->sdodata); fp@1744: ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Set the number of mapped PDOs. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_state_pdo_count( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: if (ec_fsm_coe_exec(fsm->fsm_coe)) return; fp@1744: fp@1744: if (!ec_fsm_coe_success(fsm->fsm_coe)) { fp@1744: EC_ERR("Failed to set number of mapped PDOs for slave %u.\n", fp@1744: fsm->slave->ring_position); fp@1744: fsm->state = ec_fsm_mapping_state_error; fp@1744: return; fp@1744: } fp@1744: fp@1744: if (fsm->slave->master->debug_level) fp@1744: EC_DBG("Successfully set PDO mapping for SM%u of slave %u.\n", fp@1744: fsm->sync->index, fsm->slave->ring_position); fp@1744: fp@1744: // mapping configuration for this sync manager complete. fp@1744: ec_fsm_mapping_next_sync(fsm); fp@1744: } fp@1744: fp@1744: /****************************************************************************** fp@1744: * Common state functions fp@1744: *****************************************************************************/ fp@1744: fp@1744: /** fp@1744: State: ERROR. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_state_error( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: State: END. fp@1744: */ fp@1744: fp@1744: void ec_fsm_mapping_state_end( fp@1744: ec_fsm_mapping_t *fsm /**< mapping state machine */ fp@1744: ) fp@1744: { fp@1744: } fp@1744: fp@1744: /*****************************************************************************/