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@879: * EtherCAT Pdo assignment 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@879: #include "fsm_pdo_assign.h" fp@879: fp@879: /*****************************************************************************/ fp@879: fp@879: void ec_fsm_pdo_assign_state_start(ec_fsm_pdo_assign_t *); fp@879: void ec_fsm_pdo_assign_state_zero_count(ec_fsm_pdo_assign_t *); fp@879: void ec_fsm_pdo_assign_state_add_pdo(ec_fsm_pdo_assign_t *); fp@879: void ec_fsm_pdo_assign_state_pdo_count(ec_fsm_pdo_assign_t *); fp@879: void ec_fsm_pdo_assign_state_end(ec_fsm_pdo_assign_t *); fp@879: void ec_fsm_pdo_assign_state_error(ec_fsm_pdo_assign_t *); fp@879: fp@879: void ec_fsm_pdo_assign_next_dir(ec_fsm_pdo_assign_t *); fp@635: fp@635: /*****************************************************************************/ fp@635: fp@792: /** Constructor. fp@792: */ fp@879: void ec_fsm_pdo_assign_init( fp@879: ec_fsm_pdo_assign_t *fsm, /**< Pdo assignment 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@854: ec_sdo_request_init(&fsm->request); fp@635: } fp@635: fp@635: /*****************************************************************************/ fp@635: fp@792: /** Destructor. fp@792: */ fp@879: void ec_fsm_pdo_assign_clear( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@792: ) fp@792: { fp@854: ec_sdo_request_clear(&fsm->request); fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@879: /** Start Pdo assignment state machine. fp@879: */ fp@879: void ec_fsm_pdo_assign_start( fp@879: ec_fsm_pdo_assign_t *fsm, /**< Pdo assignment state machine. */ fp@635: ec_slave_t *slave /**< slave to configure */ fp@635: ) fp@635: { fp@635: fsm->slave = slave; fp@879: fsm->state = ec_fsm_pdo_assign_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@879: int ec_fsm_pdo_assign_running( fp@879: const ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@879: ) fp@879: { fp@879: return fsm->state != ec_fsm_pdo_assign_state_end fp@879: && fsm->state != ec_fsm_pdo_assign_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@879: int ec_fsm_pdo_assign_exec( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@635: ) fp@635: { fp@635: fsm->state(fsm); fp@879: return ec_fsm_pdo_assign_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@879: int ec_fsm_pdo_assign_success( fp@879: const ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@879: ) fp@879: { fp@879: return fsm->state == ec_fsm_pdo_assign_state_end; fp@635: } fp@635: fp@635: /****************************************************************************** fp@792: * State functions. fp@635: *****************************************************************************/ fp@635: fp@879: /** Start Pdo assignment. fp@879: */ fp@879: void ec_fsm_pdo_assign_state_start( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@635: ) fp@635: { fp@792: if (!fsm->slave->config) { fp@879: fsm->state = ec_fsm_pdo_assign_state_end; fp@792: return; fp@792: } fp@792: fp@821: fsm->dir = (ec_direction_t) -1; // next is EC_DIR_OUTPUT fp@890: fsm->num_configured_dirs = 0; fp@879: ec_fsm_pdo_assign_next_dir(fsm); fp@879: } fp@879: fp@879: /*****************************************************************************/ fp@879: fp@879: /** Process Pdo assignment of next direction. fp@879: */ fp@879: void ec_fsm_pdo_assign_next_dir( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@821: ) fp@821: { fp@827: fsm->dir++; fp@827: fp@821: for (; fsm->dir <= EC_DIR_INPUT; fsm->dir++) { fp@879: fsm->pdos = &fsm->slave->config->pdos[fsm->dir]; fp@821: fp@821: if (!(fsm->sync = ec_slave_get_pdo_sync(fsm->slave, fsm->dir))) { fp@879: if (!list_empty(&fsm->pdos->list)) { fp@821: EC_ERR("No sync manager for direction %u!\n", fsm->dir); fp@879: fsm->state = ec_fsm_pdo_assign_state_end; fp@821: return; fp@821: } fp@660: continue; fp@635: } fp@660: fp@1050: if (fsm->slave->master->debug_level) { fp@1050: EC_DBG("Sync Pdos: "); fp@1050: ec_pdo_list_print(&fsm->sync->pdos); fp@1050: printk("\n"); fp@1050: EC_DBG("Config Pdos: "); fp@1050: ec_pdo_list_print(fsm->pdos); fp@1050: printk("\n"); fp@1050: } fp@1050: fp@879: // check if assignment has to be altered fp@879: if (ec_pdo_list_equal(&fsm->sync->pdos, fsm->pdos)) fp@821: continue; fp@821: fp@879: // Pdo assignment has to be changed. Does the slave support this? fp@879: if (!(fsm->slave->sii.mailbox_protocols & EC_MBOX_COE) fp@835: || (fsm->slave->sii.has_general fp@835: && !fsm->slave->sii.coe_details.enable_pdo_assign)) { fp@879: EC_ERR("Slave %u does not support assigning Pdos!\n", fp@835: fsm->slave->ring_position); fp@879: fsm->state = ec_fsm_pdo_assign_state_error; fp@835: return; fp@835: } fp@835: fp@890: fsm->num_configured_dirs++; fp@890: fp@821: if (fsm->slave->master->debug_level) { fp@879: EC_DBG("Changing Pdo assignment for SM%u of slave %u.\n", fp@821: fsm->sync->index, fsm->slave->ring_position); fp@792: } fp@821: fp@854: if (ec_sdo_request_alloc(&fsm->request, 2)) { fp@879: fsm->state = ec_fsm_pdo_assign_state_error; fp@854: return; fp@854: } fp@854: fp@821: // set mapped Pdo count to zero fp@875: EC_WRITE_U8(fsm->request.data, 0); // zero Pdos mapped fp@854: fsm->request.data_size = 1; fp@854: ec_sdo_request_address(&fsm->request, 0x1C10 + fsm->sync->index, 0); fp@858: ecrt_sdo_request_write(&fsm->request); fp@801: if (fsm->slave->master->debug_level) fp@821: EC_DBG("Setting Pdo count to zero for SM%u.\n", fsm->sync->index); fp@821: fp@879: fsm->state = ec_fsm_pdo_assign_state_zero_count; fp@859: ec_fsm_coe_transfer(fsm->fsm_coe, fsm->slave, &fsm->request); fp@821: ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately fp@821: return; fp@821: } fp@821: fp@890: if (fsm->slave->master->debug_level && !fsm->num_configured_dirs) fp@890: EC_DBG("Pdo assignments of slave %u are already configured" fp@890: " correctly.\n", fsm->slave->ring_position); fp@879: fsm->state = ec_fsm_pdo_assign_state_end; fp@879: } fp@879: fp@879: /*****************************************************************************/ fp@879: fp@879: /** Assign next Pdo. fp@879: */ fp@879: ec_pdo_t *ec_fsm_pdo_assign_next_pdo( fp@879: const ec_fsm_pdo_assign_t *fsm, /**< Pdo assignment state machine. */ fp@814: const struct list_head *list /**< current Pdo list item */ fp@792: ) fp@792: { fp@792: list = list->next; fp@879: if (list == &fsm->pdos->list) fp@814: return NULL; // no next Pdo fp@792: return list_entry(list, ec_pdo_t, list); fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@879: /** Assign a Pdo. fp@879: */ fp@879: void ec_fsm_pdo_assign_add_pdo( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@801: ) fp@801: { fp@875: EC_WRITE_U16(fsm->request.data, fsm->pdo->index); fp@854: fsm->request.data_size = 2; fp@854: ec_sdo_request_address(&fsm->request, fp@854: 0x1C10 + fsm->sync->index, fsm->pdo_count); fp@858: ecrt_sdo_request_write(&fsm->request); fp@801: if (fsm->slave->master->debug_level) fp@879: EC_DBG("Assigning Pdo 0x%04X at position %u.\n", fp@854: fsm->pdo->index, fsm->pdo_count); fp@801: fp@879: fsm->state = ec_fsm_pdo_assign_state_add_pdo; fp@859: ec_fsm_coe_transfer(fsm->fsm_coe, fsm->slave, &fsm->request); fp@801: ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately fp@801: } fp@801: fp@801: /*****************************************************************************/ fp@801: fp@879: /** Set the number of assigned Pdos to zero. fp@879: */ fp@879: void ec_fsm_pdo_assign_state_zero_count( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment 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@879: EC_ERR("Failed to clear Pdo assignment of slave %u.\n", fp@635: fsm->slave->ring_position); fp@879: fsm->state = ec_fsm_pdo_assign_state_error; fp@879: return; fp@879: } fp@879: fp@879: // assign all Pdos belonging to the current sync manager fp@635: fp@814: // find first Pdo fp@879: if (!(fsm->pdo = ec_fsm_pdo_assign_next_pdo(fsm, &fsm->pdos->list))) { fp@635: if (fsm->slave->master->debug_level) fp@879: EC_DBG("No Pdos to assign for SM%u of slave %u.\n", fp@635: fsm->sync->index, fsm->slave->ring_position); fp@879: ec_fsm_pdo_assign_next_dir(fsm); fp@879: return; fp@879: } fp@879: fp@879: // assign first Pdo fp@635: fsm->pdo_count = 1; fp@879: ec_fsm_pdo_assign_add_pdo(fsm); fp@879: } fp@879: fp@879: /*****************************************************************************/ fp@879: fp@879: /** Add a Pdo to the sync managers Pdo assignment. fp@879: */ fp@879: void ec_fsm_pdo_assign_state_add_pdo( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment 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@814: 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@879: fsm->state = ec_fsm_pdo_assign_state_error; fp@635: return; fp@635: } fp@635: fp@814: // find next Pdo fp@879: if (!(fsm->pdo = ec_fsm_pdo_assign_next_pdo(fsm, &fsm->pdo->list))) { fp@814: // no more Pdos to map. write Pdo count fp@875: EC_WRITE_U8(fsm->request.data, fsm->pdo_count); fp@854: fsm->request.data_size = 1; fp@854: ec_sdo_request_address(&fsm->request, 0x1C10 + fsm->sync->index, 0); fp@858: ecrt_sdo_request_write(&fsm->request); fp@635: if (fsm->slave->master->debug_level) fp@879: EC_DBG("Setting number of assigned Pdos to %u.\n", fp@635: fsm->pdo_count); fp@635: fp@879: fsm->state = ec_fsm_pdo_assign_state_pdo_count; fp@859: ec_fsm_coe_transfer(fsm->fsm_coe, fsm->slave, &fsm->request); fp@635: ec_fsm_coe_exec(fsm->fsm_coe); // execute immediately fp@635: return; fp@635: } fp@635: fp@879: // add next Pdo to assignment fp@635: fsm->pdo_count++; fp@879: ec_fsm_pdo_assign_add_pdo(fsm); fp@879: } fp@879: fp@879: /*****************************************************************************/ fp@879: fp@879: /** Set the number of assigned Pdos. fp@879: */ fp@879: void ec_fsm_pdo_assign_state_pdo_count( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment 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@879: EC_ERR("Failed to set number of assigned Pdos for slave %u.\n", fp@635: fsm->slave->ring_position); fp@879: fsm->state = ec_fsm_pdo_assign_state_error; fp@635: return; fp@635: } fp@635: fp@635: if (fsm->slave->master->debug_level) fp@890: EC_DBG("Successfully configured Pdo assignment for SM%u of" fp@890: " slave %u.\n", fsm->sync->index, fsm->slave->ring_position); fp@635: fp@879: // assignment for this direction finished fp@879: ec_fsm_pdo_assign_next_dir(fsm); fp@635: } fp@635: fp@635: /****************************************************************************** fp@635: * Common state functions fp@635: *****************************************************************************/ fp@635: fp@792: /** State: ERROR. fp@792: */ fp@879: void ec_fsm_pdo_assign_state_error( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@635: ) fp@635: { fp@635: } fp@635: fp@635: /*****************************************************************************/ fp@635: fp@792: /** State: END. fp@792: */ fp@879: void ec_fsm_pdo_assign_state_end( fp@879: ec_fsm_pdo_assign_t *fsm /**< Pdo assignment state machine. */ fp@879: ) fp@879: { fp@879: } fp@879: fp@879: /*****************************************************************************/