master/command.c
changeset 144 fdc24bf62f80
parent 104 052bc82d5442
child 195 674071846ee3
equal deleted inserted replaced
143:f6c4f38b699f 144:fdc24bf62f80
    15 #include "master.h"
    15 #include "master.h"
    16 
    16 
    17 /*****************************************************************************/
    17 /*****************************************************************************/
    18 
    18 
    19 #define EC_FUNC_HEADER \
    19 #define EC_FUNC_HEADER \
       
    20     if (unlikely(ec_command_prealloc(command, data_size))) \
       
    21         return -1; \
    20     command->index = 0; \
    22     command->index = 0; \
    21     command->working_counter = 0; \
    23     command->working_counter = 0; \
    22     command->state = EC_CMD_INIT;
    24     command->state = EC_CMD_INIT;
    23 
    25 
    24 #define EC_FUNC_WRITE_FOOTER \
    26 #define EC_FUNC_FOOTER \
    25     command->data_size = data_size; \
    27     command->data_size = data_size; \
    26     memcpy(command->data, data, data_size);
    28     memset(command->data, 0x00, data_size); \
    27 
    29     return 0;
    28 #define EC_FUNC_READ_FOOTER \
    30 
    29     command->data_size = data_size; \
    31 /*****************************************************************************/
    30     memset(command->data, 0x00, data_size);
    32 
       
    33 /**
       
    34    EtherCAT-Kommando-Konstruktor.
       
    35 */
       
    36 
       
    37 void ec_command_init(ec_command_t *command)
       
    38 {
       
    39     command->type = EC_CMD_NONE;
       
    40     command->address.logical = 0x00000000;
       
    41     command->data = NULL;
       
    42     command->mem_size = 0;
       
    43     command->data_size = 0;
       
    44     command->index = 0x00;
       
    45     command->working_counter = 0x00;
       
    46     command->state = EC_CMD_INIT;
       
    47 }
       
    48 
       
    49 /*****************************************************************************/
       
    50 
       
    51 /**
       
    52    EtherCAT-Kommando-Destruktor.
       
    53 */
       
    54 
       
    55 void ec_command_clear(ec_command_t *command)
       
    56 {
       
    57     if (command->data) kfree(command->data);
       
    58 }
       
    59 
       
    60 /*****************************************************************************/
       
    61 
       
    62 /**
       
    63    Alloziert Speicher.
       
    64 */
       
    65 
       
    66 int ec_command_prealloc(ec_command_t *command, size_t size)
       
    67 {
       
    68     if (size <= command->mem_size) return 0;
       
    69 
       
    70     if (command->data) {
       
    71         kfree(command->data);
       
    72         command->data = NULL;
       
    73         command->mem_size = 0;
       
    74     }
       
    75 
       
    76     if (!(command->data = kmalloc(size, GFP_KERNEL))) {
       
    77         EC_ERR("Failed to allocate %i bytes of command memory!\n", size);
       
    78         return -1;
       
    79     }
       
    80 
       
    81     command->mem_size = size;
       
    82     return 0;
       
    83 }
    31 
    84 
    32 /*****************************************************************************/
    85 /*****************************************************************************/
    33 
    86 
    34 /**
    87 /**
    35    Initialisiert ein EtherCAT-NPRD-Kommando.
    88    Initialisiert ein EtherCAT-NPRD-Kommando.
    36 
    89 
    37    Node-adressed physical read.
    90    Node-adressed physical read.
    38 */
    91 */
    39 
    92 
    40 void ec_command_init_nprd(ec_command_t *command,
    93 int ec_command_nprd(ec_command_t *command,
    41                           /**< EtherCAT-Rahmen */
    94                     /**< EtherCAT-Rahmen */
    42                           uint16_t node_address,
    95                     uint16_t node_address,
    43                           /**< Adresse des Knotens (Slaves) */
    96                     /**< Adresse des Knotens (Slaves) */
    44                           uint16_t offset,
    97                     uint16_t offset,
    45                           /**< Physikalische Speicheradresse im Slave */
    98                     /**< Physikalische Speicheradresse im Slave */
    46                           size_t data_size
    99                     size_t data_size
    47                           /**< Länge der zu lesenden Daten */
   100                     /**< Länge der zu lesenden Daten */
    48                           )
   101                     )
    49 {
   102 {
    50     if (unlikely(node_address == 0x0000))
   103     if (unlikely(node_address == 0x0000))
    51         EC_WARN("Using node address 0x0000!\n");
   104         EC_WARN("Using node address 0x0000!\n");
    52 
   105 
    53     EC_FUNC_HEADER;
   106     EC_FUNC_HEADER;
    54 
       
    55     command->type = EC_CMD_NPRD;
   107     command->type = EC_CMD_NPRD;
    56     command->address.physical.slave = node_address;
   108     command->address.physical.slave = node_address;
    57     command->address.physical.mem = offset;
   109     command->address.physical.mem = offset;
    58 
   110     EC_FUNC_FOOTER;
    59     EC_FUNC_READ_FOOTER;
       
    60 }
   111 }
    61 
   112 
    62 /*****************************************************************************/
   113 /*****************************************************************************/
    63 
   114 
    64 /**
   115 /**
    65    Initialisiert ein EtherCAT-NPWR-Kommando.
   116    Initialisiert ein EtherCAT-NPWR-Kommando.
    66 
   117 
    67    Node-adressed physical write.
   118    Node-adressed physical write.
    68 */
   119 */
    69 
   120 
    70 void ec_command_init_npwr(ec_command_t *command,
   121 int ec_command_npwr(ec_command_t *command,
    71                           /**< EtherCAT-Rahmen */
   122                     /**< EtherCAT-Rahmen */
    72                           uint16_t node_address,
   123                     uint16_t node_address,
    73                           /**< Adresse des Knotens (Slaves) */
   124                     /**< Adresse des Knotens (Slaves) */
    74                           uint16_t offset,
   125                     uint16_t offset,
    75                           /**< Physikalische Speicheradresse im Slave */
   126                     /**< Physikalische Speicheradresse im Slave */
    76                           size_t data_size,
   127                     size_t data_size
    77                           /**< Länge der zu schreibenden Daten */
   128                     /**< Länge der zu schreibenden Daten */
    78                           const uint8_t *data
   129                     )
    79                           /**< Zeiger auf Speicher mit zu schreibenden Daten */
       
    80                           )
       
    81 {
   130 {
    82     if (unlikely(node_address == 0x0000))
   131     if (unlikely(node_address == 0x0000))
    83         EC_WARN("Using node address 0x0000!\n");
   132         EC_WARN("Using node address 0x0000!\n");
    84 
   133 
    85     EC_FUNC_HEADER;
   134     EC_FUNC_HEADER;
    86 
       
    87     command->type = EC_CMD_NPWR;
   135     command->type = EC_CMD_NPWR;
    88     command->address.physical.slave = node_address;
   136     command->address.physical.slave = node_address;
    89     command->address.physical.mem = offset;
   137     command->address.physical.mem = offset;
    90 
   138     EC_FUNC_FOOTER;
    91     EC_FUNC_WRITE_FOOTER;
       
    92 }
   139 }
    93 
   140 
    94 /*****************************************************************************/
   141 /*****************************************************************************/
    95 
   142 
    96 /**
   143 /**
    97    Initialisiert ein EtherCAT-APRD-Kommando.
   144    Initialisiert ein EtherCAT-APRD-Kommando.
    98 
   145 
    99    Autoincrement physical read.
   146    Autoincrement physical read.
   100 */
   147 */
   101 
   148 
   102 void ec_command_init_aprd(ec_command_t *command,
   149 int ec_command_aprd(ec_command_t *command,
   103                           /**< EtherCAT-Rahmen */
   150                     /**< EtherCAT-Rahmen */
   104                           uint16_t ring_position,
   151                     uint16_t ring_position,
   105                           /**< Position des Slaves im Bus */
   152                     /**< Position des Slaves im Bus */
   106                           uint16_t offset,
   153                     uint16_t offset,
   107                           /**< Physikalische Speicheradresse im Slave */
   154                     /**< Physikalische Speicheradresse im Slave */
   108                           size_t data_size
   155                     size_t data_size
   109                           /**< Länge der zu lesenden Daten */
   156                     /**< Länge der zu lesenden Daten */
   110                           )
   157                     )
   111 {
   158 {
   112     EC_FUNC_HEADER;
   159     EC_FUNC_HEADER;
   113 
       
   114     command->type = EC_CMD_APRD;
   160     command->type = EC_CMD_APRD;
   115     command->address.physical.slave = (int16_t) ring_position * (-1);
   161     command->address.physical.slave = (int16_t) ring_position * (-1);
   116     command->address.physical.mem = offset;
   162     command->address.physical.mem = offset;
   117 
   163     EC_FUNC_FOOTER;
   118     EC_FUNC_READ_FOOTER;
       
   119 }
   164 }
   120 
   165 
   121 /*****************************************************************************/
   166 /*****************************************************************************/
   122 
   167 
   123 /**
   168 /**
   124    Initialisiert ein EtherCAT-APWR-Kommando.
   169    Initialisiert ein EtherCAT-APWR-Kommando.
   125 
   170 
   126    Autoincrement physical write.
   171    Autoincrement physical write.
   127 */
   172 */
   128 
   173 
   129 void ec_command_init_apwr(ec_command_t *command,
   174 int ec_command_apwr(ec_command_t *command,
   130                           /**< EtherCAT-Rahmen */
   175                     /**< EtherCAT-Rahmen */
   131                           uint16_t ring_position,
   176                     uint16_t ring_position,
   132                           /**< Position des Slaves im Bus */
   177                     /**< Position des Slaves im Bus */
   133                           uint16_t offset,
   178                     uint16_t offset,
   134                           /**< Physikalische Speicheradresse im Slave */
   179                     /**< Physikalische Speicheradresse im Slave */
   135                           size_t data_size,
   180                     size_t data_size
   136                           /**< Länge der zu schreibenden Daten */
   181                     /**< Länge der zu schreibenden Daten */
   137                           const uint8_t *data
   182                     )
   138                           /**< Zeiger auf Speicher mit zu schreibenden Daten */
   183 {
   139                           )
   184     EC_FUNC_HEADER;
   140 {
       
   141     EC_FUNC_HEADER;
       
   142 
       
   143     command->type = EC_CMD_APWR;
   185     command->type = EC_CMD_APWR;
   144     command->address.physical.slave = (int16_t) ring_position * (-1);
   186     command->address.physical.slave = (int16_t) ring_position * (-1);
   145     command->address.physical.mem = offset;
   187     command->address.physical.mem = offset;
   146 
   188     EC_FUNC_FOOTER;
   147     EC_FUNC_WRITE_FOOTER;
       
   148 }
   189 }
   149 
   190 
   150 /*****************************************************************************/
   191 /*****************************************************************************/
   151 
   192 
   152 /**
   193 /**
   153    Initialisiert ein EtherCAT-BRD-Kommando.
   194    Initialisiert ein EtherCAT-BRD-Kommando.
   154 
   195 
   155    Broadcast read.
   196    Broadcast read.
   156 */
   197 */
   157 
   198 
   158 void ec_command_init_brd(ec_command_t *command,
   199 int ec_command_brd(ec_command_t *command,
   159                          /**< EtherCAT-Rahmen */
   200                    /**< EtherCAT-Rahmen */
   160                          uint16_t offset,
   201                    uint16_t offset,
   161                          /**< Physikalische Speicheradresse im Slave */
   202                    /**< Physikalische Speicheradresse im Slave */
   162                          size_t data_size
   203                    size_t data_size
   163                          /**< Länge der zu lesenden Daten */
   204                    /**< Länge der zu lesenden Daten */
   164                          )
   205                    )
   165 {
   206 {
   166     EC_FUNC_HEADER;
   207     EC_FUNC_HEADER;
   167 
       
   168     command->type = EC_CMD_BRD;
   208     command->type = EC_CMD_BRD;
   169     command->address.physical.slave = 0x0000;
   209     command->address.physical.slave = 0x0000;
   170     command->address.physical.mem = offset;
   210     command->address.physical.mem = offset;
   171 
   211     EC_FUNC_FOOTER;
   172     EC_FUNC_READ_FOOTER;
       
   173 }
   212 }
   174 
   213 
   175 /*****************************************************************************/
   214 /*****************************************************************************/
   176 
   215 
   177 /**
   216 /**
   178    Initialisiert ein EtherCAT-BWR-Kommando.
   217    Initialisiert ein EtherCAT-BWR-Kommando.
   179 
   218 
   180    Broadcast write.
   219    Broadcast write.
   181 */
   220 */
   182 
   221 
   183 void ec_command_init_bwr(ec_command_t *command,
   222 int ec_command_bwr(ec_command_t *command,
   184                          /**< EtherCAT-Rahmen */
   223                    /**< EtherCAT-Rahmen */
   185                          uint16_t offset,
   224                    uint16_t offset,
   186                          /**< Physikalische Speicheradresse im Slave */
   225                    /**< Physikalische Speicheradresse im Slave */
   187                          size_t data_size,
   226                    size_t data_size
   188                          /**< Länge der zu schreibenden Daten */
   227                    /**< Länge der zu schreibenden Daten */
   189                          const uint8_t *data
   228                    )
   190                          /**< Zeiger auf Speicher mit zu schreibenden Daten */
   229 {
   191                          )
   230     EC_FUNC_HEADER;
   192 {
       
   193     EC_FUNC_HEADER;
       
   194 
       
   195     command->type = EC_CMD_BWR;
   231     command->type = EC_CMD_BWR;
   196     command->address.physical.slave = 0x0000;
   232     command->address.physical.slave = 0x0000;
   197     command->address.physical.mem = offset;
   233     command->address.physical.mem = offset;
   198 
   234     EC_FUNC_FOOTER;
   199     EC_FUNC_WRITE_FOOTER;
       
   200 }
   235 }
   201 
   236 
   202 /*****************************************************************************/
   237 /*****************************************************************************/
   203 
   238 
   204 /**
   239 /**
   205    Initialisiert ein EtherCAT-LRW-Kommando.
   240    Initialisiert ein EtherCAT-LRW-Kommando.
   206 
   241 
   207    Logical read write.
   242    Logical read write.
   208 */
   243 */
   209 
   244 
   210 void ec_command_init_lrw(ec_command_t *command,
   245 int ec_command_lrw(ec_command_t *command,
   211                          /**< EtherCAT-Rahmen */
   246                    /**< EtherCAT-Rahmen */
   212                          uint32_t offset,
   247                    uint32_t offset,
   213                          /**< Logische Startadresse */
   248                    /**< Logische Startadresse */
   214                          size_t data_size,
   249                    size_t data_size
   215                          /**< Länge der zu lesenden/schreibenden Daten */
   250                    /**< Länge der zu lesenden/schreibenden Daten */
   216                          uint8_t *data
   251                    )
   217                          /**< Zeiger auf die Daten */
   252 {
   218                          )
   253     EC_FUNC_HEADER;
   219 {
       
   220     EC_FUNC_HEADER;
       
   221 
       
   222     command->type = EC_CMD_LRW;
   254     command->type = EC_CMD_LRW;
   223     command->address.logical = offset;
   255     command->address.logical = offset;
   224 
   256     EC_FUNC_FOOTER;
   225     EC_FUNC_WRITE_FOOTER;
       
   226 }
   257 }
   227 
   258 
   228 /*****************************************************************************/
   259 /*****************************************************************************/
   229 
   260 
   230 /* Emacs-Konfiguration
   261 /* Emacs-Konfiguration