IgH EtherCAT Master  1.6.9
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
25
26/****************************************************************************/
27
28#include <linux/module.h>
29#include <linux/jiffies.h>
30#include <linux/slab.h>
31
32#include "sdo_request.h"
33
34/****************************************************************************/
35
38#define EC_SDO_REQUEST_RESPONSE_TIMEOUT 1000
39
40/****************************************************************************/
41
43
44/****************************************************************************/
45
50 )
51{
52 req->complete_access = 0;
53 req->data = NULL;
54 req->mem_size = 0;
55 req->data_size = 0;
56 req->issue_timeout = 0; // no timeout
58 req->dir = EC_DIR_INVALID;
59 req->state = EC_INT_REQUEST_INIT;
60 req->jiffies_start = 0U;
61 req->jiffies_sent = 0U;
62 req->errno = 0;
63 req->abort_code = 0x00000000;
64}
65
66/****************************************************************************/
67
76
77/****************************************************************************/
78
86 ec_sdo_request_t *req,
87 const ec_sdo_request_t *other
88 )
89{
90 req->complete_access = other->complete_access;
91 req->index = other->index;
92 req->subindex = other->subindex;
93 return ec_sdo_request_copy_data(req, other->data, other->data_size);
94}
95
96/****************************************************************************/
97
101 ec_sdo_request_t *req
102 )
103{
104 if (req->data) {
105 kfree(req->data);
106 req->data = NULL;
107 }
108
109 req->mem_size = 0;
110 req->data_size = 0;
111}
112
113/****************************************************************************/
114
122 ec_sdo_request_t *req,
123 size_t size
124 )
125{
126 if (size <= req->mem_size)
127 return 0;
128
130
131 if (!(req->data = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
132 EC_ERR("Failed to allocate %zu bytes of SDO memory.\n", size);
133 return -ENOMEM;
134 }
135
136 req->mem_size = size;
137 req->data_size = 0;
138 return 0;
139}
140
141/****************************************************************************/
142
151 ec_sdo_request_t *req,
152 const uint8_t *source,
153 size_t size
154 )
155{
156 int ret = ec_sdo_request_alloc(req, size);
157 if (ret < 0)
158 return ret;
159
160 memcpy(req->data, source, size);
161 req->data_size = size;
162 return 0;
163}
164
165/****************************************************************************/
166
172{
173 return req->issue_timeout
174 && jiffies - req->jiffies_start > HZ * req->issue_timeout / 1000;
175}
176
177/*****************************************************************************
178 * Application interface.
179 ****************************************************************************/
180
182 uint8_t subindex)
183{
184 req->index = index;
185 req->subindex = subindex;
186 return 0;
187}
188
189/****************************************************************************/
190
192{
193 req->issue_timeout = timeout;
194 return 0;
195}
196
197/****************************************************************************/
198
200{
201 return req->data;
202}
203
204/****************************************************************************/
205
207{
208 return req->data_size;
209}
210
211/****************************************************************************/
212
217
218/****************************************************************************/
219
221{
222 req->dir = EC_DIR_INPUT;
223 req->state = EC_INT_REQUEST_QUEUED;
224 req->errno = 0;
225 req->abort_code = 0x00000000;
226 req->jiffies_start = jiffies;
227 return 0;
228}
229
230/****************************************************************************/
231
233{
234 req->dir = EC_DIR_OUTPUT;
235 req->state = EC_INT_REQUEST_QUEUED;
236 req->errno = 0;
237 req->abort_code = 0x00000000;
238 req->jiffies_start = jiffies;
239 return 0;
240}
241
242/****************************************************************************/
243
245
246EXPORT_SYMBOL(ecrt_sdo_request_index);
247EXPORT_SYMBOL(ecrt_sdo_request_timeout);
248EXPORT_SYMBOL(ecrt_sdo_request_data);
249EXPORT_SYMBOL(ecrt_sdo_request_data_size);
250EXPORT_SYMBOL(ecrt_sdo_request_state);
251EXPORT_SYMBOL(ecrt_sdo_request_read);
252EXPORT_SYMBOL(ecrt_sdo_request_write);
253
255
256/****************************************************************************/
const ec_request_state_t ec_request_state_translation_table[]
Global request state type translation table.
Definition module.c:658
#define EC_ERR(fmt, args...)
Convenience macro for printing EtherCAT-specific errors to syslog.
Definition globals.h:224
int ecrt_sdo_request_read(ec_sdo_request_t *req)
Schedule an SDO read operation.
int ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
Set the timeout for an SDO request.
size_t ecrt_sdo_request_data_size(const ec_sdo_request_t *req)
Returns the current SDO data size.
struct ec_sdo_request ec_sdo_request_t
Definition ecrt.h:309
uint8_t * ecrt_sdo_request_data(const ec_sdo_request_t *req)
Access to the SDO request's data.
int ecrt_sdo_request_write(ec_sdo_request_t *req)
Schedule an SDO write operation.
ec_request_state_t ecrt_sdo_request_state(const ec_sdo_request_t *req)
Get the current state of the SDO request.
ec_request_state_t
Request state.
Definition ecrt.h:604
int ecrt_sdo_request_index(ec_sdo_request_t *req, uint16_t index, uint8_t subindex)
Set the SDO index and subindex.
@ EC_DIR_INVALID
Invalid direction.
Definition ecrt.h:505
@ EC_DIR_INPUT
Values read by the master.
Definition ecrt.h:507
@ EC_DIR_OUTPUT
Values written by the master.
Definition ecrt.h:506
void ec_sdo_request_init(ec_sdo_request_t *req)
SDO request constructor.
Definition sdo_request.c:48
int ec_sdo_request_alloc(ec_sdo_request_t *req, size_t size)
Pre-allocates the data memory.
int ec_sdo_request_timed_out(const ec_sdo_request_t *req)
Checks, if the timeout was exceeded.
int ec_sdo_request_copy(ec_sdo_request_t *req, const ec_sdo_request_t *other)
Copy another SDO request.
Definition sdo_request.c:85
#define EC_SDO_REQUEST_RESPONSE_TIMEOUT
Default timeout in ms to wait for SDO transfer responses.
Definition sdo_request.c:38
void ec_sdo_request_clear_data(ec_sdo_request_t *)
SDO request destructor.
void ec_sdo_request_clear(ec_sdo_request_t *req)
SDO request destructor.
Definition sdo_request.c:70
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.
EtherCAT CANopen SDO request structure.
uint32_t response_timeout
Maximum time in ms, the transfer is retried, if the slave does not respond.
Definition sdo_request.h:50
uint32_t issue_timeout
Maximum time in ms, the processing of the request may take.
Definition sdo_request.h:48
ec_internal_request_state_t state
SDO request state.
Definition sdo_request.h:55
int errno
Error number.
Definition sdo_request.h:59
size_t mem_size
Size of SDO data memory.
Definition sdo_request.h:45
uint8_t complete_access
SDO shall be transferred completely.
Definition sdo_request.h:47
size_t data_size
Size of SDO data.
Definition sdo_request.h:46
uint8_t * data
Pointer to SDO data.
Definition sdo_request.h:44
unsigned long jiffies_start
Jiffies, when the request was issued.
Definition sdo_request.h:56
uint32_t abort_code
SDO request abort code.
Definition sdo_request.h:60
ec_direction_t dir
Direction.
Definition sdo_request.h:52
unsigned long jiffies_sent
Jiffies, when the upload/download request was sent.
Definition sdo_request.h:57
uint16_t index
SDO index.
Definition sdo_request.h:42
uint8_t subindex
SDO subindex.
Definition sdo_request.h:43