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