IgH EtherCAT Master  1.5.3
sdo_request.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2006-2023 Florian Pose, Ingenieurgemeinschaft IgH
4  *
5  * This file is part of the IgH EtherCAT Master.
6  *
7  * The IgH EtherCAT Master is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version 2, as
9  * published by the Free Software Foundation.
10  *
11  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with the IgH EtherCAT Master; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * ---
21  *
22  * The license mentioned above concerns the source code only. Using the
23  * EtherCAT technology and brand is only permitted in compliance with the
24  * industrial property and similar rights of Beckhoff Automation GmbH.
25  *
26  *****************************************************************************/
27 
32 /*****************************************************************************/
33 
34 #include <linux/module.h>
35 #include <linux/jiffies.h>
36 #include <linux/slab.h>
37 
38 #include "sdo_request.h"
39 
40 /*****************************************************************************/
41 
44 #define EC_SDO_REQUEST_RESPONSE_TIMEOUT 1000
45 
46 /*****************************************************************************/
47 
49 
50 /*****************************************************************************/
51 
55  ec_sdo_request_t *req
56  )
57 {
58  req->complete_access = 0;
59  req->data = NULL;
60  req->mem_size = 0;
61  req->data_size = 0;
62  req->issue_timeout = 0; // no timeout
64  req->dir = EC_DIR_INVALID;
65  req->state = EC_INT_REQUEST_INIT;
66  req->jiffies_start = 0U;
67  req->jiffies_sent = 0U;
68  req->errno = 0;
69  req->abort_code = 0x00000000;
70 }
71 
72 /*****************************************************************************/
73 
77  ec_sdo_request_t *req
78  )
79 {
81 }
82 
83 /*****************************************************************************/
84 
92  ec_sdo_request_t *req,
93  const ec_sdo_request_t *other
94  )
95 {
96  req->complete_access = other->complete_access;
97  req->index = other->index;
98  req->subindex = other->subindex;
99  return ec_sdo_request_copy_data(req, other->data, other->data_size);
100 }
101 
102 /*****************************************************************************/
103 
107  ec_sdo_request_t *req
108  )
109 {
110  if (req->data) {
111  kfree(req->data);
112  req->data = NULL;
113  }
114 
115  req->mem_size = 0;
116  req->data_size = 0;
117 }
118 
119 /*****************************************************************************/
120 
128  ec_sdo_request_t *req,
129  size_t size
130  )
131 {
132  if (size <= req->mem_size)
133  return 0;
134 
136 
137  if (!(req->data = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
138  EC_ERR("Failed to allocate %zu bytes of SDO memory.\n", size);
139  return -ENOMEM;
140  }
141 
142  req->mem_size = size;
143  req->data_size = 0;
144  return 0;
145 }
146 
147 /*****************************************************************************/
148 
157  ec_sdo_request_t *req,
158  const uint8_t *source,
159  size_t size
160  )
161 {
162  int ret = ec_sdo_request_alloc(req, size);
163  if (ret < 0)
164  return ret;
165 
166  memcpy(req->data, source, size);
167  req->data_size = size;
168  return 0;
169 }
170 
171 /*****************************************************************************/
172 
178 {
179  return req->issue_timeout
180  && jiffies - req->jiffies_start > HZ * req->issue_timeout / 1000;
181 }
182 
183 /*****************************************************************************
184  * Application interface.
185  ****************************************************************************/
186 
187 void ecrt_sdo_request_index(ec_sdo_request_t *req, uint16_t index,
188  uint8_t subindex)
189 {
190  req->index = index;
191  req->subindex = subindex;
192 }
193 
194 /*****************************************************************************/
195 
196 void ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
197 {
198  req->issue_timeout = timeout;
199 }
200 
201 /*****************************************************************************/
202 
204 {
205  return req->data;
206 }
207 
208 /*****************************************************************************/
209 
211 {
212  return req->data_size;
213 }
214 
215 /*****************************************************************************/
216 
218 {
220 }
221 
222 /*****************************************************************************/
223 
225 {
226  req->dir = EC_DIR_INPUT;
227  req->state = EC_INT_REQUEST_QUEUED;
228  req->errno = 0;
229  req->abort_code = 0x00000000;
230  req->jiffies_start = jiffies;
231 }
232 
233 /*****************************************************************************/
234 
236 {
237  req->dir = EC_DIR_OUTPUT;
238  req->state = EC_INT_REQUEST_QUEUED;
239  req->errno = 0;
240  req->abort_code = 0x00000000;
241  req->jiffies_start = jiffies;
242 }
243 
244 /*****************************************************************************/
245 
248 EXPORT_SYMBOL(ecrt_sdo_request_index);
249 EXPORT_SYMBOL(ecrt_sdo_request_timeout);
250 EXPORT_SYMBOL(ecrt_sdo_request_data);
251 EXPORT_SYMBOL(ecrt_sdo_request_data_size);
252 EXPORT_SYMBOL(ecrt_sdo_request_state);
253 EXPORT_SYMBOL(ecrt_sdo_request_read);
254 EXPORT_SYMBOL(ecrt_sdo_request_write);
255 
258 /*****************************************************************************/
CANopen SDO request.
Definition: sdo_request.h:46
ec_internal_request_state_t state
SDO request state.
Definition: sdo_request.h:61
uint32_t response_timeout
Maximum time in ms, the transfer is retried, if the slave does not respond.
Definition: sdo_request.h:56
size_t mem_size
Size of SDO data memory.
Definition: sdo_request.h:51
uint16_t index
SDO index.
Definition: sdo_request.h:48
uint32_t abort_code
SDO request abort code.
Definition: sdo_request.h:66
void ec_sdo_request_clear_data(ec_sdo_request_t *)
SDO request destructor.
Definition: sdo_request.c:106
int ec_sdo_request_timed_out(const ec_sdo_request_t *req)
Checks, if the timeout was exceeded.
Definition: sdo_request.c:177
uint8_t * data
Pointer to SDO data.
Definition: sdo_request.h:50
void ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
Set the timeout for an SDO request.
Definition: sdo_request.c:196
uint8_t * ecrt_sdo_request_data(ec_sdo_request_t *req)
Access to the SDO request&#39;s data.
Definition: sdo_request.c:203
ec_direction_t dir
Direction.
Definition: sdo_request.h:58
void ecrt_sdo_request_read(ec_sdo_request_t *req)
Schedule an SDO read operation.
Definition: sdo_request.c:224
void ec_sdo_request_clear(ec_sdo_request_t *req)
SDO request destructor.
Definition: sdo_request.c:76
int ec_sdo_request_copy_data(ec_sdo_request_t *req, const uint8_t *source, size_t size)
Copies SDO data from an external source.
Definition: sdo_request.c:156
EtherCAT CANopen SDO request structure.
unsigned long jiffies_sent
Jiffies, when the upload/download request was sent.
Definition: sdo_request.h:63
int ec_sdo_request_copy(ec_sdo_request_t *req, const ec_sdo_request_t *other)
Copy another SDO request.
Definition: sdo_request.c:91
int errno
Error number.
Definition: sdo_request.h:65
#define EC_SDO_REQUEST_RESPONSE_TIMEOUT
Default timeout in ms to wait for SDO transfer responses.
Definition: sdo_request.c:44
Values read by the master.
Definition: ecrt.h:449
ec_request_state_t ecrt_sdo_request_state(const ec_sdo_request_t *req)
Get the current state of the SDO request.
Definition: sdo_request.c:217
uint8_t subindex
SDO subindex.
Definition: sdo_request.h:49
size_t data_size
Size of SDO data.
Definition: sdo_request.h:52
size_t ecrt_sdo_request_data_size(const ec_sdo_request_t *req)
Returns the current SDO data size.
Definition: sdo_request.c:210
Invalid direction.
Definition: ecrt.h:447
void ec_sdo_request_init(ec_sdo_request_t *req)
SDO request constructor.
Definition: sdo_request.c:54
#define EC_ERR(fmt, args...)
Convenience macro for printing EtherCAT-specific errors to syslog.
Definition: globals.h:215
ec_request_state_t
Request state.
Definition: ecrt.h:546
int ec_sdo_request_alloc(ec_sdo_request_t *req, size_t size)
Pre-allocates the data memory.
Definition: sdo_request.c:127
Values written by the master.
Definition: ecrt.h:448
const ec_request_state_t ec_request_state_translation_table[]
Global request state type translation table.
Definition: module.c:662
uint32_t issue_timeout
Maximum time in ms, the processing of the request may take.
Definition: sdo_request.h:54
void ecrt_sdo_request_write(ec_sdo_request_t *req)
Schedule an SDO write operation.
Definition: sdo_request.c:235
unsigned long jiffies_start
Jiffies, when the request was issued.
Definition: sdo_request.h:62
uint8_t complete_access
SDO shall be transferred completely.
Definition: sdo_request.h:53
void ecrt_sdo_request_index(ec_sdo_request_t *req, uint16_t index, uint8_t subindex)
Set the SDO index and subindex.
Definition: sdo_request.c:187