fp@98: /****************************************************************************** fp@98: * fp@98: * $Id$ fp@98: * fp@197: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@197: * fp@197: * This file is part of the IgH EtherCAT Master. fp@197: * fp@197: * The IgH EtherCAT Master is free software; you can redistribute it fp@197: * and/or modify it under the terms of the GNU General Public License fp@197: * as published by the Free Software Foundation; version 2 of the License. fp@197: * fp@197: * The IgH EtherCAT Master is distributed in the hope that it will be fp@197: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@197: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@197: * GNU General Public License for more details. fp@197: * fp@197: * You should have received a copy of the GNU General Public License fp@197: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@197: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@197: * fp@98: *****************************************************************************/ fp@98: fp@199: /** fp@199: \file fp@199: Methods of an EtherCAT command. fp@199: */ fp@199: fp@199: /*****************************************************************************/ fp@199: fp@98: #include fp@98: fp@98: #include "command.h" fp@98: #include "master.h" fp@98: fp@98: /*****************************************************************************/ fp@98: fp@199: /** \cond */ fp@199: fp@98: #define EC_FUNC_HEADER \ fp@144: if (unlikely(ec_command_prealloc(command, data_size))) \ fp@144: return -1; \ fp@98: command->index = 0; \ fp@98: command->working_counter = 0; \ fp@98: command->state = EC_CMD_INIT; fp@98: fp@144: #define EC_FUNC_FOOTER \ fp@98: command->data_size = data_size; \ fp@144: memset(command->data, 0x00, data_size); \ fp@144: return 0; fp@144: fp@199: /** \endcond */ fp@199: fp@144: /*****************************************************************************/ fp@144: fp@144: /** fp@195: Command constructor. fp@197: */ fp@197: fp@197: void ec_command_init(ec_command_t *command /**< EtherCAT command */) fp@144: { fp@144: command->type = EC_CMD_NONE; fp@144: command->address.logical = 0x00000000; fp@144: command->data = NULL; fp@144: command->mem_size = 0; fp@144: command->data_size = 0; fp@144: command->index = 0x00; fp@144: command->working_counter = 0x00; fp@144: command->state = EC_CMD_INIT; fp@208: command->t_sent = 0; fp@144: } fp@144: fp@144: /*****************************************************************************/ fp@144: fp@144: /** fp@195: Command destructor. fp@197: */ fp@197: fp@197: void ec_command_clear(ec_command_t *command /**< EtherCAT command */) fp@144: { fp@144: if (command->data) kfree(command->data); fp@144: } fp@144: fp@144: /*****************************************************************************/ fp@144: fp@144: /** fp@195: Allocates command data memory. fp@197: If the allocated memory is already larger than requested, nothing ist done. fp@197: \return 0 in case of success, else < 0 fp@197: */ fp@197: fp@197: int ec_command_prealloc(ec_command_t *command, /**< EtherCAT command */ fp@197: size_t size /**< New size in bytes */ fp@197: ) fp@144: { fp@144: if (size <= command->mem_size) return 0; fp@144: fp@144: if (command->data) { fp@144: kfree(command->data); fp@144: command->data = NULL; fp@144: command->mem_size = 0; fp@144: } fp@144: fp@144: if (!(command->data = kmalloc(size, GFP_KERNEL))) { fp@144: EC_ERR("Failed to allocate %i bytes of command memory!\n", size); fp@144: return -1; fp@144: } fp@144: fp@144: command->mem_size = size; fp@144: return 0; fp@144: } fp@98: fp@98: /*****************************************************************************/ fp@98: fp@98: /** fp@195: Initializes an EtherCAT NPRD command. fp@98: Node-adressed physical read. fp@195: \return 0 in case of success, else < 0 fp@98: */ fp@98: fp@144: int ec_command_nprd(ec_command_t *command, fp@195: /**< EtherCAT command */ fp@144: uint16_t node_address, fp@195: /**< configured station address */ fp@195: uint16_t offset, fp@195: /**< physical memory address */ fp@195: size_t data_size fp@195: /**< number of bytes to read */ fp@144: ) fp@98: { fp@98: if (unlikely(node_address == 0x0000)) fp@98: EC_WARN("Using node address 0x0000!\n"); fp@98: fp@98: EC_FUNC_HEADER; fp@98: command->type = EC_CMD_NPRD; fp@98: command->address.physical.slave = node_address; fp@98: command->address.physical.mem = offset; fp@144: EC_FUNC_FOOTER; fp@98: } fp@98: fp@98: /*****************************************************************************/ fp@98: fp@98: /** fp@195: Initializes an EtherCAT NPWR command. fp@98: Node-adressed physical write. fp@195: \return 0 in case of success, else < 0 fp@98: */ fp@98: fp@144: int ec_command_npwr(ec_command_t *command, fp@195: /**< EtherCAT command */ fp@144: uint16_t node_address, fp@195: /**< configured station address */ fp@195: uint16_t offset, fp@195: /**< physical memory address */ fp@195: size_t data_size fp@195: /**< number of bytes to write */ fp@144: ) fp@98: { fp@98: if (unlikely(node_address == 0x0000)) fp@98: EC_WARN("Using node address 0x0000!\n"); fp@98: fp@98: EC_FUNC_HEADER; fp@98: command->type = EC_CMD_NPWR; fp@98: command->address.physical.slave = node_address; fp@98: command->address.physical.mem = offset; fp@144: EC_FUNC_FOOTER; fp@98: } fp@98: fp@98: /*****************************************************************************/ fp@98: fp@98: /** fp@195: Initializes an EtherCAT APRD command. fp@98: Autoincrement physical read. fp@195: \return 0 in case of success, else < 0 fp@98: */ fp@98: fp@144: int ec_command_aprd(ec_command_t *command, fp@195: /**< EtherCAT command */ fp@144: uint16_t ring_position, fp@195: /**< auto-increment position */ fp@195: uint16_t offset, fp@195: /**< physical memory address */ fp@195: size_t data_size fp@195: /**< number of bytes to read */ fp@144: ) fp@144: { fp@144: EC_FUNC_HEADER; fp@98: command->type = EC_CMD_APRD; fp@98: command->address.physical.slave = (int16_t) ring_position * (-1); fp@98: command->address.physical.mem = offset; fp@144: EC_FUNC_FOOTER; fp@98: } fp@98: fp@98: /*****************************************************************************/ fp@98: fp@98: /** fp@195: Initializes an EtherCAT APWR command. fp@98: Autoincrement physical write. fp@195: \return 0 in case of success, else < 0 fp@98: */ fp@98: fp@144: int ec_command_apwr(ec_command_t *command, fp@195: /**< EtherCAT command */ fp@144: uint16_t ring_position, fp@195: /**< auto-increment position */ fp@195: uint16_t offset, fp@195: /**< physical memory address */ fp@195: size_t data_size fp@195: /**< number of bytes to write */ fp@144: ) fp@144: { fp@144: EC_FUNC_HEADER; fp@98: command->type = EC_CMD_APWR; fp@98: command->address.physical.slave = (int16_t) ring_position * (-1); fp@98: command->address.physical.mem = offset; fp@144: EC_FUNC_FOOTER; fp@98: } fp@98: fp@98: /*****************************************************************************/ fp@98: fp@98: /** fp@195: Initializes an EtherCAT BRD command. fp@98: Broadcast read. fp@195: \return 0 in case of success, else < 0 fp@98: */ fp@98: fp@144: int ec_command_brd(ec_command_t *command, fp@195: /**< EtherCAT command */ fp@144: uint16_t offset, fp@195: /**< physical memory address */ fp@144: size_t data_size fp@195: /**< number of bytes to read */ fp@144: ) fp@144: { fp@144: EC_FUNC_HEADER; fp@98: command->type = EC_CMD_BRD; fp@98: command->address.physical.slave = 0x0000; fp@98: command->address.physical.mem = offset; fp@144: EC_FUNC_FOOTER; fp@98: } fp@98: fp@98: /*****************************************************************************/ fp@98: fp@98: /** fp@195: Initializes an EtherCAT BWR command. fp@98: Broadcast write. fp@195: \return 0 in case of success, else < 0 fp@98: */ fp@98: fp@144: int ec_command_bwr(ec_command_t *command, fp@195: /**< EtherCAT command */ fp@144: uint16_t offset, fp@195: /**< physical memory address */ fp@144: size_t data_size fp@195: /**< number of bytes to write */ fp@144: ) fp@144: { fp@144: EC_FUNC_HEADER; fp@98: command->type = EC_CMD_BWR; fp@98: command->address.physical.slave = 0x0000; fp@98: command->address.physical.mem = offset; fp@144: EC_FUNC_FOOTER; fp@98: } fp@98: fp@98: /*****************************************************************************/ fp@98: fp@98: /** fp@195: Initializes an EtherCAT LRW command. fp@98: Logical read write. fp@195: \return 0 in case of success, else < 0 fp@98: */ fp@98: fp@144: int ec_command_lrw(ec_command_t *command, fp@195: /**< EtherCAT command */ fp@144: uint32_t offset, fp@195: /**< logical address */ fp@144: size_t data_size fp@195: /**< number of bytes to read/write */ fp@144: ) fp@144: { fp@144: EC_FUNC_HEADER; fp@98: command->type = EC_CMD_LRW; fp@98: command->address.logical = offset; fp@144: EC_FUNC_FOOTER; fp@98: } fp@98: fp@98: /*****************************************************************************/