fp@98: /****************************************************************************** fp@98: * fp@98: * c o m m a n d . c fp@98: * fp@195: * Methods of an EtherCAT command. fp@98: * fp@98: * $Id$ fp@98: * fp@98: *****************************************************************************/ fp@98: fp@98: #include fp@98: #include fp@98: fp@98: #include "command.h" fp@98: #include "master.h" fp@98: fp@98: /*****************************************************************************/ fp@98: 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@144: /*****************************************************************************/ fp@144: fp@144: /** fp@195: Command constructor. fp@144: */ fp@144: fp@144: void ec_command_init(ec_command_t *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@144: } fp@144: fp@144: /*****************************************************************************/ fp@144: fp@144: /** fp@195: Command destructor. fp@144: */ fp@144: fp@144: void ec_command_clear(ec_command_t *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@195: \return 0 in case of success, else < 0 fp@144: */ fp@144: fp@144: int ec_command_prealloc(ec_command_t *command, size_t size) 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: /*****************************************************************************/