datagram.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  *
00003  *  $Id: datagram.c 490 2006-08-02 12:25:25Z fp $
00004  *
00005  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
00006  *
00007  *  This file is part of the IgH EtherCAT Master.
00008  *
00009  *  The IgH EtherCAT Master is free software; you can redistribute it
00010  *  and/or modify it under the terms of the GNU General Public License
00011  *  as published by the Free Software Foundation; either version 2 of the
00012  *  License, or (at your option) any later version.
00013  *
00014  *  The IgH EtherCAT Master is distributed in the hope that it will be
00015  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with the IgH EtherCAT Master; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  *  The right to use EtherCAT Technology is granted and comes free of
00024  *  charge under condition of compatibility of product made by
00025  *  Licensee. People intending to distribute/sell products based on the
00026  *  code, have to sign an agreement to guarantee that products using
00027  *  software based on IgH EtherCAT master stay compatible with the actual
00028  *  EtherCAT specification (which are released themselves as an open
00029  *  standard) as the (only) precondition to have the right to use EtherCAT
00030  *  Technology, IP and trade marks.
00031  *
00032  *****************************************************************************/
00033 
00039 /*****************************************************************************/
00040 
00041 #include <linux/slab.h>
00042 
00043 #include "datagram.h"
00044 #include "master.h"
00045 
00046 /*****************************************************************************/
00047 
00050 #define EC_FUNC_HEADER \
00051     if (unlikely(ec_datagram_prealloc(datagram, data_size))) \
00052         return -1; \
00053     datagram->index = 0; \
00054     datagram->working_counter = 0; \
00055     datagram->state = EC_CMD_INIT;
00056 
00057 #define EC_FUNC_FOOTER \
00058     datagram->data_size = data_size; \
00059     memset(datagram->data, 0x00, data_size); \
00060     return 0;
00061 
00064 /*****************************************************************************/
00065 
00070 void ec_datagram_init(ec_datagram_t *datagram )
00071 {
00072     datagram->type = EC_CMD_NONE;
00073     datagram->address.logical = 0x00000000;
00074     datagram->data = NULL;
00075     datagram->mem_size = 0;
00076     datagram->data_size = 0;
00077     datagram->index = 0x00;
00078     datagram->working_counter = 0x00;
00079     datagram->state = EC_CMD_INIT;
00080     datagram->t_sent = 0;
00081 }
00082 
00083 /*****************************************************************************/
00084 
00089 void ec_datagram_clear(ec_datagram_t *datagram )
00090 {
00091     if (datagram->data) kfree(datagram->data);
00092 }
00093 
00094 /*****************************************************************************/
00095 
00102 int ec_datagram_prealloc(ec_datagram_t *datagram, 
00103                          size_t size 
00104                          )
00105 {
00106     if (size <= datagram->mem_size) return 0;
00107 
00108     if (datagram->data) {
00109         kfree(datagram->data);
00110         datagram->data = NULL;
00111         datagram->mem_size = 0;
00112     }
00113 
00114     if (!(datagram->data = kmalloc(size, GFP_KERNEL))) {
00115         EC_ERR("Failed to allocate %i bytes of datagram memory!\n", size);
00116         return -1;
00117     }
00118 
00119     datagram->mem_size = size;
00120     return 0;
00121 }
00122 
00123 /*****************************************************************************/
00124 
00131 int ec_datagram_nprd(ec_datagram_t *datagram,
00133                      uint16_t node_address,
00135                      uint16_t offset,
00137                      size_t data_size
00139                      )
00140 {
00141     if (unlikely(node_address == 0x0000))
00142         EC_WARN("Using node address 0x0000!\n");
00143 
00144     EC_FUNC_HEADER;
00145     datagram->type = EC_CMD_NPRD;
00146     datagram->address.physical.slave = node_address;
00147     datagram->address.physical.mem = offset;
00148     EC_FUNC_FOOTER;
00149 }
00150 
00151 /*****************************************************************************/
00152 
00159 int ec_datagram_npwr(ec_datagram_t *datagram,
00161                      uint16_t node_address,
00163                      uint16_t offset,
00165                      size_t data_size
00167                      )
00168 {
00169     if (unlikely(node_address == 0x0000))
00170         EC_WARN("Using node address 0x0000!\n");
00171 
00172     EC_FUNC_HEADER;
00173     datagram->type = EC_CMD_NPWR;
00174     datagram->address.physical.slave = node_address;
00175     datagram->address.physical.mem = offset;
00176     EC_FUNC_FOOTER;
00177 }
00178 
00179 /*****************************************************************************/
00180 
00187 int ec_datagram_aprd(ec_datagram_t *datagram,
00189                      uint16_t ring_position,
00191                      uint16_t offset,
00193                      size_t data_size
00195                      )
00196 {
00197     EC_FUNC_HEADER;
00198     datagram->type = EC_CMD_APRD;
00199     datagram->address.physical.slave = (int16_t) ring_position * (-1);
00200     datagram->address.physical.mem = offset;
00201     EC_FUNC_FOOTER;
00202 }
00203 
00204 /*****************************************************************************/
00205 
00212 int ec_datagram_apwr(ec_datagram_t *datagram,
00214                      uint16_t ring_position,
00216                      uint16_t offset,
00218                      size_t data_size
00220                      )
00221 {
00222     EC_FUNC_HEADER;
00223     datagram->type = EC_CMD_APWR;
00224     datagram->address.physical.slave = (int16_t) ring_position * (-1);
00225     datagram->address.physical.mem = offset;
00226     EC_FUNC_FOOTER;
00227 }
00228 
00229 /*****************************************************************************/
00230 
00237 int ec_datagram_brd(ec_datagram_t *datagram,
00239                     uint16_t offset,
00241                     size_t data_size
00243                     )
00244 {
00245     EC_FUNC_HEADER;
00246     datagram->type = EC_CMD_BRD;
00247     datagram->address.physical.slave = 0x0000;
00248     datagram->address.physical.mem = offset;
00249     EC_FUNC_FOOTER;
00250 }
00251 
00252 /*****************************************************************************/
00253 
00260 int ec_datagram_bwr(ec_datagram_t *datagram,
00262                     uint16_t offset,
00264                     size_t data_size
00266                     )
00267 {
00268     EC_FUNC_HEADER;
00269     datagram->type = EC_CMD_BWR;
00270     datagram->address.physical.slave = 0x0000;
00271     datagram->address.physical.mem = offset;
00272     EC_FUNC_FOOTER;
00273 }
00274 
00275 /*****************************************************************************/
00276 
00283 int ec_datagram_lrw(ec_datagram_t *datagram,
00285                     uint32_t offset,
00287                     size_t data_size
00289                     )
00290 {
00291     EC_FUNC_HEADER;
00292     datagram->type = EC_CMD_LRW;
00293     datagram->address.logical = offset;
00294     EC_FUNC_FOOTER;
00295 }
00296 
00297 /*****************************************************************************/

Generated on Wed Aug 2 18:41:43 2006 for IgH EtherCAT master by  doxygen 1.4.6