0
|
1 |
/****************************************************************
|
|
2 |
*
|
|
3 |
* e c _ m a s t e r . c
|
|
4 |
*
|
|
5 |
* Methoden für einen EtherCAT-Master.
|
|
6 |
*
|
|
7 |
* $Date$
|
|
8 |
* $Author$
|
|
9 |
*
|
|
10 |
***************************************************************/
|
|
11 |
|
|
12 |
#include <linux/kernel.h>
|
|
13 |
#include <linux/string.h>
|
|
14 |
#include <linux/slab.h>
|
|
15 |
#include <linux/delay.h>
|
|
16 |
|
|
17 |
#include "ec_globals.h"
|
|
18 |
#include "ec_master.h"
|
|
19 |
#include "ec_dbg.h"
|
|
20 |
|
|
21 |
// FIXME: Klappt nur solange, wie es nur einen Master gibt! fp
|
|
22 |
static int ASYNC = 0;
|
|
23 |
|
|
24 |
/***************************************************************/
|
|
25 |
|
|
26 |
/**
|
|
27 |
Konstruktor des EtherCAT-Masters.
|
|
28 |
|
|
29 |
@param master Zeiger auf den zu initialisierenden
|
|
30 |
EtherCAT-Master
|
|
31 |
@param dev Zeiger auf das EtherCAT-gerät, mit dem der
|
|
32 |
Master arbeiten soll
|
|
33 |
|
|
34 |
@return 0 bei Erfolg, sonst < 0 (= dev ist NULL)
|
|
35 |
*/
|
|
36 |
|
|
37 |
int EtherCAT_master_init(EtherCAT_master_t *master,
|
|
38 |
EtherCAT_device_t *dev)
|
|
39 |
{
|
|
40 |
unsigned int i;
|
|
41 |
|
|
42 |
if (!dev)
|
|
43 |
{
|
|
44 |
EC_DBG(KERN_ERR "EtherCAT: Master init without device!\n");
|
|
45 |
return -1;
|
|
46 |
}
|
|
47 |
|
|
48 |
master->slaves = NULL;
|
|
49 |
master->slave_count = 0;
|
|
50 |
master->first_command = NULL;
|
|
51 |
master->process_data_command = NULL;
|
|
52 |
master->dev = dev;
|
|
53 |
master->command_index = 0x00;
|
|
54 |
master->tx_data_length = 0;
|
|
55 |
master->process_data = NULL;
|
|
56 |
master->process_data_length = 0;
|
|
57 |
master->cmd_ring_index = 0;
|
|
58 |
|
|
59 |
for (i = 0; i < ECAT_COMMAND_RING_SIZE; i++)
|
|
60 |
{
|
|
61 |
EtherCAT_command_init(&master->cmd_ring[i]);
|
|
62 |
}
|
|
63 |
|
|
64 |
return 0;
|
|
65 |
}
|
|
66 |
|
|
67 |
/***************************************************************/
|
|
68 |
|
|
69 |
/**
|
|
70 |
Destruktor eines EtherCAT-Masters.
|
|
71 |
|
|
72 |
Entfernt alle Kommandos aus der Liste, löscht den Zeiger
|
|
73 |
auf das Slave-Array und gibt die Prozessdaten frei.
|
|
74 |
|
|
75 |
@param master Zeiger auf den zu löschenden Master
|
|
76 |
*/
|
|
77 |
|
|
78 |
void EtherCAT_master_clear(EtherCAT_master_t *master)
|
|
79 |
{
|
|
80 |
EC_DBG("Master clear");
|
|
81 |
|
|
82 |
// Remove all pending commands
|
|
83 |
while (master->first_command)
|
|
84 |
{
|
|
85 |
EtherCAT_remove_command(master, master->first_command);
|
|
86 |
}
|
|
87 |
|
|
88 |
// Remove all slaves
|
|
89 |
EtherCAT_clear_slaves(master);
|
|
90 |
|
|
91 |
if (master->process_data)
|
|
92 |
{
|
|
93 |
kfree(master->process_data);
|
|
94 |
master->process_data = NULL;
|
|
95 |
}
|
|
96 |
|
|
97 |
master->process_data_length = 0;
|
|
98 |
|
|
99 |
EC_DBG("done...\n");
|
|
100 |
}
|
|
101 |
|
|
102 |
/***************************************************************/
|
|
103 |
|
|
104 |
/**
|
|
105 |
Überprüft die angeschlossenen Slaves.
|
|
106 |
|
|
107 |
Vergleicht die an den Bus angeschlossenen Slaves mit
|
|
108 |
den im statischen-Slave-Array vorgegebenen Konfigurationen.
|
|
109 |
Stimmen Anzahl oder Typen nicht überein, gibt diese
|
|
110 |
Methode einen Fehler aus.
|
|
111 |
|
|
112 |
@param master Der EtherCAT-Master
|
|
113 |
@param slaves Zeiger auf ein statisches Slave-Array
|
|
114 |
@param slave_count Anzahl der Slaves im Array
|
|
115 |
|
|
116 |
@return 0 bei Erfolg, sonst < 0
|
|
117 |
*/
|
|
118 |
|
|
119 |
int EtherCAT_check_slaves(EtherCAT_master_t *master,
|
|
120 |
EtherCAT_slave_t *slaves,
|
|
121 |
unsigned int slave_count)
|
|
122 |
{
|
|
123 |
EtherCAT_command_t *cmd;
|
|
124 |
EtherCAT_slave_t *cur;
|
|
125 |
unsigned int i, j, found, length, pos;
|
|
126 |
unsigned char data[2];
|
|
127 |
|
|
128 |
// EtherCAT_clear_slaves() must be called before!
|
|
129 |
if (master->slaves || master->slave_count)
|
|
130 |
{
|
|
131 |
EC_DBG(KERN_ERR "EtherCAT duplicate slave check!");
|
|
132 |
return -1;
|
|
133 |
}
|
|
134 |
|
|
135 |
// No slaves.
|
|
136 |
if (slave_count == 0)
|
|
137 |
{
|
|
138 |
EC_DBG(KERN_ERR "EtherCAT: No slaves in list!");
|
|
139 |
return -1;
|
|
140 |
}
|
|
141 |
|
|
142 |
// Determine number of slaves on bus
|
|
143 |
|
|
144 |
if ((cmd = EtherCAT_broadcast_read(master, 0x0000, 4)) == NULL)
|
|
145 |
{
|
|
146 |
return -1;
|
|
147 |
}
|
|
148 |
|
|
149 |
if (EtherCAT_async_send_receive(master) != 0)
|
|
150 |
{
|
|
151 |
return -1;
|
|
152 |
}
|
|
153 |
|
|
154 |
if (cmd->working_counter != slave_count)
|
|
155 |
{
|
|
156 |
EC_DBG(KERN_ERR "EtherCAT: Wrong number of slaves on bus: %i / %i\n",
|
|
157 |
cmd->working_counter, slave_count);
|
|
158 |
EtherCAT_remove_command(master, cmd);
|
|
159 |
|
|
160 |
return -1;
|
|
161 |
}
|
|
162 |
else
|
|
163 |
{
|
|
164 |
EC_DBG("EtherCAT: Slave count on bus: %i. Found: %i.\n",
|
|
165 |
cmd->working_counter, slave_count);
|
|
166 |
}
|
|
167 |
|
|
168 |
EtherCAT_remove_command(master, cmd);
|
|
169 |
|
|
170 |
// For every slave in the list
|
|
171 |
for (i = 0; i < slave_count; i++)
|
|
172 |
{
|
|
173 |
cur = &slaves[i];
|
|
174 |
|
|
175 |
if (!cur->desc)
|
|
176 |
{
|
|
177 |
EC_DBG(KERN_ERR "EtherCAT: Slave %i has no description.\n", i);
|
|
178 |
return -1;
|
|
179 |
}
|
|
180 |
|
|
181 |
// Set ring position
|
|
182 |
cur->ring_position = -i;
|
|
183 |
cur->station_address = i + 1;
|
|
184 |
|
|
185 |
// Write station address
|
|
186 |
|
|
187 |
data[0] = cur->station_address & 0x00FF;
|
|
188 |
data[1] = (cur->station_address & 0xFF00) >> 8;
|
|
189 |
|
|
190 |
if ((cmd = EtherCAT_position_write(master, cur->ring_position, 0x0010, 2, data)) == NULL)
|
|
191 |
{
|
|
192 |
return -1;
|
|
193 |
}
|
|
194 |
|
|
195 |
if (EtherCAT_async_send_receive(master) != 0)
|
|
196 |
{
|
|
197 |
return -1;
|
|
198 |
}
|
|
199 |
|
|
200 |
if (cmd->working_counter != 1)
|
|
201 |
{
|
|
202 |
EC_DBG(KERN_ERR "EtherCAT: Slave %i did not repond while writing station address!\n", i);
|
|
203 |
return -1;
|
|
204 |
}
|
|
205 |
|
|
206 |
EtherCAT_remove_command(master, cmd);
|
|
207 |
|
|
208 |
// Read base data
|
|
209 |
|
|
210 |
if ((cmd = EtherCAT_read(master, cur->station_address, 0x0000, 4)) == NULL)
|
|
211 |
{
|
|
212 |
EC_DBG(KERN_ERR "EtherCAT: Could not create command!\n");
|
|
213 |
return -1;
|
|
214 |
}
|
|
215 |
|
|
216 |
if (EtherCAT_async_send_receive(master) != 0)
|
|
217 |
{
|
|
218 |
EtherCAT_remove_command(master, cmd);
|
|
219 |
|
|
220 |
EC_DBG(KERN_ERR "EtherCAT: Could not send command!\n");
|
|
221 |
return -1;
|
|
222 |
}
|
|
223 |
|
|
224 |
if (cmd->working_counter != 1)
|
|
225 |
{
|
|
226 |
EtherCAT_remove_command(master, cmd);
|
|
227 |
|
|
228 |
EC_DBG(KERN_ERR "EtherCAT: Slave %i did not respond while reading base data!\n", i);
|
|
229 |
return -1;
|
|
230 |
}
|
|
231 |
|
|
232 |
// Get base data
|
|
233 |
cur->type = cmd->data[0];
|
|
234 |
cur->revision = cmd->data[1];
|
|
235 |
cur->build = cmd->data[2] | (cmd->data[3] << 8);
|
|
236 |
|
|
237 |
EtherCAT_remove_command(master, cmd);
|
|
238 |
|
|
239 |
// Read identification from "Slave Information Interface" (SII)
|
|
240 |
|
|
241 |
if (EtherCAT_read_slave_information(master, cur->station_address,
|
|
242 |
0x0008, &cur->vendor_id) != 0)
|
|
243 |
{
|
|
244 |
EC_DBG(KERN_ERR "EtherCAT: Could not read SII!\n");
|
|
245 |
return -1;
|
|
246 |
}
|
|
247 |
|
|
248 |
if (EtherCAT_read_slave_information(master, cur->station_address,
|
|
249 |
0x000A, &cur->product_code) != 0)
|
|
250 |
{
|
|
251 |
EC_DBG(KERN_ERR "EtherCAT: Could not read SII!\n");
|
|
252 |
return -1;
|
|
253 |
}
|
|
254 |
|
|
255 |
if (EtherCAT_read_slave_information(master, cur->station_address,
|
|
256 |
0x000E, &cur->revision_number) != 0)
|
|
257 |
{
|
|
258 |
EC_DBG(KERN_ERR "EtherCAT: Could not read SII!\n");
|
|
259 |
return -1;
|
|
260 |
}
|
|
261 |
|
|
262 |
// Search for identification in "database"
|
|
263 |
|
|
264 |
found = 0;
|
|
265 |
|
|
266 |
for (j = 0; j < slave_idents_count; j++)
|
|
267 |
{
|
|
268 |
if (slave_idents[j].vendor_id == cur->vendor_id
|
|
269 |
&& slave_idents[j].product_code == cur->product_code)
|
|
270 |
{
|
|
271 |
found = 1;
|
|
272 |
|
|
273 |
if (cur->desc != slave_idents[j].desc)
|
|
274 |
{
|
|
275 |
EC_DBG(KERN_ERR "EtherCAT: Unexpected slave device \"%s %s\""
|
|
276 |
" at position %i. Expected: \"%s %s\"\n",
|
|
277 |
slave_idents[j].desc->vendor_name,
|
|
278 |
slave_idents[j].desc->product_name, i,
|
|
279 |
cur->desc->vendor_name, cur->desc->product_name);
|
|
280 |
return -1;
|
|
281 |
}
|
|
282 |
|
|
283 |
break;
|
|
284 |
}
|
|
285 |
}
|
|
286 |
|
|
287 |
if (!found)
|
|
288 |
{
|
|
289 |
EC_DBG(KERN_ERR "EtherCAT: Unknown slave device (vendor %X, code %X) at position %i.\n",
|
|
290 |
i, cur->vendor_id, cur->product_code);
|
|
291 |
return -1;
|
|
292 |
}
|
|
293 |
}
|
|
294 |
|
|
295 |
length = 0;
|
|
296 |
for (i = 0; i < slave_count; i++)
|
|
297 |
{
|
|
298 |
length += slaves[i].desc->data_length;
|
|
299 |
}
|
|
300 |
|
|
301 |
if ((master->process_data = (unsigned char *)
|
|
302 |
kmalloc(sizeof(unsigned char) * length, GFP_KERNEL)) == NULL)
|
|
303 |
{
|
|
304 |
EC_DBG(KERN_ERR "EtherCAT: Could not allocate %i bytes for process data.\n", length);
|
|
305 |
return -1;
|
|
306 |
}
|
|
307 |
|
|
308 |
master->process_data_length = length;
|
|
309 |
memset(master->process_data, 0x00, length);
|
|
310 |
|
|
311 |
pos = 0;
|
|
312 |
for (i = 0; i < slave_count; i++)
|
|
313 |
{
|
|
314 |
slaves[i].process_data = master->process_data + pos;
|
|
315 |
slaves[i].logical_address0 = pos;
|
|
316 |
|
|
317 |
EC_DBG(KERN_DEBUG "EtherCAT: Slave %i - \"%s %s\" - Logical Address 0x%08X\n",
|
|
318 |
i, slaves[i].desc->vendor_name, slaves[i].desc->product_name, pos);
|
|
319 |
|
|
320 |
pos += slaves[i].desc->data_length;
|
|
321 |
}
|
|
322 |
|
|
323 |
master->slaves = slaves;
|
|
324 |
master->slave_count = slave_count;
|
|
325 |
|
|
326 |
return 0;
|
|
327 |
}
|
|
328 |
|
|
329 |
/***************************************************************/
|
|
330 |
|
|
331 |
/**
|
|
332 |
Entfernt den Zeiger auf das Slave-Array.
|
|
333 |
|
|
334 |
@param master EtherCAT-Master
|
|
335 |
*/
|
|
336 |
|
|
337 |
void EtherCAT_clear_slaves(EtherCAT_master_t *master)
|
|
338 |
{
|
|
339 |
master->slaves = NULL;
|
|
340 |
master->slave_count = 0;
|
|
341 |
}
|
|
342 |
|
|
343 |
/***************************************************************/
|
|
344 |
|
|
345 |
/**
|
|
346 |
Liest Daten aus dem Slave-Information-Interface
|
|
347 |
eines EtherCAT-Slaves.
|
|
348 |
|
|
349 |
@param master EtherCAT-Master
|
|
350 |
@param node_address Knotenadresse des Slaves
|
|
351 |
@param offset Adresse des zu lesenden SII-Registers
|
|
352 |
@param target Zeiger auf einen 4 Byte großen Speicher
|
|
353 |
zum Ablegen der Daten
|
|
354 |
|
|
355 |
@return 0 bei Erfolg, sonst < 0
|
|
356 |
*/
|
|
357 |
|
|
358 |
int EtherCAT_read_slave_information(EtherCAT_master_t *master,
|
|
359 |
unsigned short int node_address,
|
|
360 |
unsigned short int offset,
|
|
361 |
unsigned int *target)
|
|
362 |
{
|
|
363 |
EtherCAT_command_t *cmd;
|
|
364 |
unsigned char data[10];
|
|
365 |
unsigned int tries_left;
|
|
366 |
|
|
367 |
// Initiate read operation
|
|
368 |
|
|
369 |
data[0] = 0x00;
|
|
370 |
data[1] = 0x01;
|
|
371 |
data[2] = offset & 0xFF;
|
|
372 |
data[3] = (offset & 0xFF00) >> 8;
|
|
373 |
data[4] = 0x00;
|
|
374 |
data[5] = 0x00;
|
|
375 |
|
|
376 |
if ((cmd = EtherCAT_write(master, node_address, 0x502, 6, data)) == NULL)
|
|
377 |
return -2;
|
|
378 |
|
|
379 |
if (EtherCAT_async_send_receive(master) != 0)
|
|
380 |
{
|
|
381 |
EtherCAT_remove_command(master, cmd);
|
|
382 |
return -3;
|
|
383 |
}
|
|
384 |
|
|
385 |
if (cmd->working_counter != 1)
|
|
386 |
{
|
|
387 |
EtherCAT_remove_command(master, cmd);
|
|
388 |
EC_DBG(KERN_ERR "EtherCAT: SII-read - Slave %04X did not respond!\n",
|
|
389 |
node_address);
|
|
390 |
return -4;
|
|
391 |
}
|
|
392 |
|
|
393 |
EtherCAT_remove_command(master, cmd);
|
|
394 |
|
|
395 |
// Get status of read operation
|
|
396 |
|
|
397 |
// ?? FIXME warum hier tries ?? Hm
|
|
398 |
|
|
399 |
// Der Slave legt die Informationen des Slave-Information-Interface
|
|
400 |
// in das Datenregister und löscht daraufhin ein Busy-Bit. Solange
|
|
401 |
// den Status auslesen, bis das Bit weg ist. fp
|
|
402 |
|
|
403 |
tries_left = 1000;
|
|
404 |
while (tries_left)
|
|
405 |
{
|
|
406 |
udelay(10);
|
|
407 |
|
|
408 |
if ((cmd = EtherCAT_read(master, node_address, 0x502, 10)) == NULL)
|
|
409 |
return -2;
|
|
410 |
|
|
411 |
if (EtherCAT_async_send_receive(master) != 0)
|
|
412 |
{
|
|
413 |
EtherCAT_remove_command(master, cmd);
|
|
414 |
return -3;
|
|
415 |
}
|
|
416 |
|
|
417 |
if (cmd->working_counter != 1)
|
|
418 |
{
|
|
419 |
EtherCAT_remove_command(master, cmd);
|
|
420 |
EC_DBG(KERN_ERR "EtherCAT: SII-read status - Slave %04X did not respond!\n",
|
|
421 |
node_address);
|
|
422 |
return -4;
|
|
423 |
}
|
|
424 |
|
|
425 |
if ((cmd->data[1] & 0x81) == 0)
|
|
426 |
{
|
|
427 |
memcpy(target, cmd->data + 6, 4);
|
|
428 |
EtherCAT_remove_command(master, cmd);
|
|
429 |
break;
|
|
430 |
}
|
|
431 |
|
|
432 |
EtherCAT_remove_command(master, cmd);
|
|
433 |
|
|
434 |
tries_left--;
|
|
435 |
}
|
|
436 |
|
|
437 |
if (!tries_left)
|
|
438 |
{
|
|
439 |
EC_DBG(KERN_WARNING "EtherCAT: SSI-read. Slave %04X timed out!\n",
|
|
440 |
node_address);
|
|
441 |
return -1;
|
|
442 |
}
|
|
443 |
|
|
444 |
return 0;
|
|
445 |
}
|
|
446 |
|
|
447 |
/***************************************************************/
|
|
448 |
|
|
449 |
/**
|
|
450 |
Führt ein asynchrones Senden und Empfangen aus.
|
|
451 |
|
|
452 |
Sendet alle wartenden Kommandos und wartet auf die
|
|
453 |
entsprechenden Antworten.
|
|
454 |
|
|
455 |
@param master EtherCAT-Master
|
|
456 |
|
|
457 |
@return 0 bei Erfolg, sonst < 0
|
|
458 |
*/
|
|
459 |
|
|
460 |
int EtherCAT_async_send_receive(EtherCAT_master_t *master)
|
|
461 |
{
|
|
462 |
unsigned int wait_cycles;
|
|
463 |
int i;
|
|
464 |
|
|
465 |
// Send all commands
|
|
466 |
|
|
467 |
//EC_DBG("Master async send");
|
|
468 |
|
|
469 |
for (i = 0; i < ECAT_NUM_RETRIES; i++)
|
|
470 |
{
|
|
471 |
ASYNC = 1;
|
|
472 |
if (EtherCAT_send(master) < 0) return -1;
|
|
473 |
ASYNC = 0;
|
|
474 |
|
|
475 |
// Wait until something is received or an error has occurred
|
|
476 |
wait_cycles = 10;
|
|
477 |
while (master->dev->state == ECAT_DS_SENT && wait_cycles)
|
|
478 |
{
|
|
479 |
udelay(1000);
|
|
480 |
wait_cycles--;
|
|
481 |
}
|
|
482 |
|
|
483 |
//EC_DBG("Master async send: tries %d",tries_left);
|
|
484 |
|
|
485 |
if (!wait_cycles)
|
|
486 |
{
|
|
487 |
EC_DBG(KERN_ERR "EtherCAT: Asynchronous receive timeout.\n");
|
|
488 |
continue;
|
|
489 |
}
|
|
490 |
|
|
491 |
if (master->dev->state != ECAT_DS_RECEIVED)
|
|
492 |
{
|
|
493 |
EC_DBG(KERN_ERR "EtherCAT: Asyncronous send error. State %i\n", master->dev->state);
|
|
494 |
continue;
|
|
495 |
}
|
|
496 |
|
|
497 |
// Receive all commands
|
|
498 |
if (EtherCAT_receive(master) < 0)
|
|
499 |
{
|
|
500 |
// Noch mal versuchen
|
|
501 |
master->dev->state = ECAT_DS_READY;
|
|
502 |
EC_DBG("Retry Asynchronous send/recieve: %d", i);
|
|
503 |
continue;
|
|
504 |
}
|
|
505 |
|
|
506 |
return 0; // Erfolgreich
|
|
507 |
}
|
|
508 |
|
|
509 |
return -1;
|
|
510 |
}
|
|
511 |
|
|
512 |
/***************************************************************/
|
|
513 |
|
|
514 |
/**
|
|
515 |
Sendet alle wartenden Kommandos.
|
|
516 |
|
|
517 |
Errechnet erst die benötigte Gesamt-Rahmenlänge, und erstellt
|
|
518 |
dann alle Kommando-Bytefolgen im statischen Sendespeicher.
|
|
519 |
Danach wird die Sendefunktion des EtherCAT-Gerätes aufgerufen.
|
|
520 |
|
|
521 |
@param master EtherCAT-Master
|
|
522 |
|
|
523 |
@return 0 bei Erfolg, sonst < 0
|
|
524 |
*/
|
|
525 |
|
|
526 |
int EtherCAT_send(EtherCAT_master_t *master)
|
|
527 |
{
|
|
528 |
unsigned int i, length, framelength, pos;
|
|
529 |
EtherCAT_command_t *cmd;
|
|
530 |
int cmdcnt = 0;
|
|
531 |
|
|
532 |
length = 0;
|
|
533 |
for (cmd = master->first_command; cmd != NULL; cmd = cmd->next)
|
|
534 |
{
|
|
535 |
if (cmd->state != ECAT_CS_READY) continue;
|
|
536 |
length += cmd->data_length + 12;
|
|
537 |
cmdcnt++;
|
|
538 |
}
|
|
539 |
|
|
540 |
if (length == 0)
|
|
541 |
{
|
|
542 |
EC_DBG(KERN_WARNING "EtherCAT: Nothing to send...\n");
|
|
543 |
return 0;
|
|
544 |
}
|
|
545 |
|
|
546 |
framelength = length + 2;
|
|
547 |
if (framelength < 46) framelength = 46;
|
|
548 |
|
|
549 |
if (ASYNC && framelength > 46)
|
|
550 |
EC_DBG(KERN_WARNING "Framelength: %d", framelength);
|
|
551 |
|
|
552 |
if (ASYNC && cmdcnt > 1)
|
|
553 |
EC_DBG(KERN_WARNING "CMDcount: %d", cmdcnt);
|
|
554 |
|
|
555 |
master->tx_data[0] = length & 0xFF;
|
|
556 |
master->tx_data[1] = ((length & 0x700) >> 8) | 0x10;
|
|
557 |
pos = 2;
|
|
558 |
|
|
559 |
for (cmd = master->first_command; cmd != NULL; cmd = cmd->next)
|
|
560 |
{
|
|
561 |
if (cmd->state != ECAT_CS_READY) continue;
|
|
562 |
|
|
563 |
cmd->index = master->command_index;
|
|
564 |
master->command_index = (master->command_index + 1) % 0x0100;
|
|
565 |
|
|
566 |
cmd->state = ECAT_CS_SENT;
|
|
567 |
|
|
568 |
master->tx_data[pos + 0] = cmd->type;
|
|
569 |
master->tx_data[pos + 1] = cmd->index;
|
|
570 |
|
|
571 |
master->tx_data[pos + 2] = cmd->address.raw[0];
|
|
572 |
master->tx_data[pos + 3] = cmd->address.raw[1];
|
|
573 |
master->tx_data[pos + 4] = cmd->address.raw[2];
|
|
574 |
master->tx_data[pos + 5] = cmd->address.raw[3];
|
|
575 |
|
|
576 |
master->tx_data[pos + 6] = cmd->data_length & 0xFF;
|
|
577 |
master->tx_data[pos + 7] = (cmd->data_length & 0x700) >> 8;
|
|
578 |
|
|
579 |
if (cmd->next) master->tx_data[pos + 7] |= 0x80;
|
|
580 |
|
|
581 |
master->tx_data[pos + 8] = 0x00;
|
|
582 |
master->tx_data[pos + 9] = 0x00;
|
|
583 |
|
|
584 |
if (cmd->type == ECAT_CMD_APWR
|
|
585 |
|| cmd->type == ECAT_CMD_NPWR
|
|
586 |
|| cmd->type == ECAT_CMD_BWR
|
|
587 |
|| cmd->type == ECAT_CMD_LRW) // Write
|
|
588 |
{
|
|
589 |
for (i = 0; i < cmd->data_length; i++) master->tx_data[pos + 10 + i] = cmd->data[i];
|
|
590 |
}
|
|
591 |
else // Read
|
|
592 |
{
|
|
593 |
for (i = 0; i < cmd->data_length; i++) master->tx_data[pos + 10 + i] = 0x00;
|
|
594 |
}
|
|
595 |
|
|
596 |
master->tx_data[pos + 10 + cmd->data_length] = 0x00;
|
|
597 |
master->tx_data[pos + 11 + cmd->data_length] = 0x00;
|
|
598 |
|
|
599 |
pos += 12 + cmd->data_length;
|
|
600 |
}
|
|
601 |
|
|
602 |
// Pad with zeros
|
|
603 |
while (pos < 46) master->tx_data[pos++] = 0x00;
|
|
604 |
|
|
605 |
master->tx_data_length = framelength;
|
|
606 |
|
|
607 |
#ifdef DEBUG_SEND_RECEIVE
|
|
608 |
EC_DBG(KERN_DEBUG "\n");
|
|
609 |
EC_DBG(KERN_DEBUG ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
|
|
610 |
EC_DBG(KERN_DEBUG);
|
|
611 |
for (i = 0; i < framelength; i++)
|
|
612 |
{
|
|
613 |
EC_DBG("%02X ", master->tx_data[i]);
|
|
614 |
|
|
615 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
616 |
}
|
|
617 |
EC_DBG("\n");
|
|
618 |
EC_DBG(KERN_DEBUG "-----------------------------------------------\n");
|
|
619 |
#endif
|
|
620 |
|
|
621 |
// Send frame
|
|
622 |
if (EtherCAT_device_send(master->dev, master->tx_data, framelength) != 0)
|
|
623 |
{
|
|
624 |
EC_DBG(KERN_ERR "EtherCAT: Could not send!\n");
|
|
625 |
EC_DBG(KERN_DEBUG "EtherCAT: tx_data content:\n");
|
|
626 |
EC_DBG(KERN_DEBUG);
|
|
627 |
for (i = 0; i < framelength; i++)
|
|
628 |
{
|
|
629 |
EC_DBG("%02X ", master->tx_data[i]);
|
|
630 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
631 |
}
|
|
632 |
EC_DBG("\n");
|
|
633 |
return -1;
|
|
634 |
}
|
|
635 |
|
|
636 |
return 0;
|
|
637 |
}
|
|
638 |
|
|
639 |
/***************************************************************/
|
|
640 |
|
|
641 |
/**
|
|
642 |
Holt alle empfangenen Kommandos vom EtherCAT-Gerät.
|
|
643 |
|
|
644 |
Kopiert einen empfangenen Rahmen vom EtherCAT-Gerät
|
|
645 |
in den Statischen Empfangsspeicher und ordnet dann
|
|
646 |
allen gesendeten Kommandos ihre Antworten zu.
|
|
647 |
|
|
648 |
@param master EtherCAT-Master
|
|
649 |
|
|
650 |
@return 0 bei Erfolg, sonst < 0
|
|
651 |
*/
|
|
652 |
|
|
653 |
int EtherCAT_receive(EtherCAT_master_t *master)
|
|
654 |
{
|
|
655 |
EtherCAT_command_t *cmd;
|
|
656 |
unsigned int length, pos, found, rx_data_length;
|
|
657 |
unsigned int command_follows, command_type, command_index;
|
|
658 |
unsigned int i;
|
|
659 |
|
|
660 |
// Copy received data into master buffer (with locking)
|
|
661 |
rx_data_length = EtherCAT_device_receive(master->dev, master->rx_data,
|
|
662 |
ECAT_FRAME_BUFFER_SIZE);
|
|
663 |
|
|
664 |
#ifdef DEBUG_SEND_RECEIVE
|
|
665 |
for (i = 0; i < rx_data_length; i++)
|
|
666 |
{
|
|
667 |
if (master->rx_data[i] == master->tx_data[i]) EC_DBG(" ");
|
|
668 |
else EC_DBG("%02X ", master->rx_data[i]);
|
|
669 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
670 |
}
|
|
671 |
EC_DBG("\n");
|
|
672 |
EC_DBG(KERN_DEBUG "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
|
|
673 |
EC_DBG(KERN_DEBUG "\n");
|
|
674 |
#endif
|
|
675 |
|
|
676 |
if (rx_data_length < 2)
|
|
677 |
{
|
|
678 |
EC_DBG(KERN_ERR "EtherCAT: Received corrupted frame (illegal length)!\n");
|
|
679 |
EC_DBG(KERN_DEBUG "EtherCAT: tx_data content:\n");
|
|
680 |
EC_DBG(KERN_DEBUG);
|
|
681 |
for (i = 0; i < master->tx_data_length; i++)
|
|
682 |
{
|
|
683 |
EC_DBG("%02X ", master->tx_data[i]);
|
|
684 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
685 |
}
|
|
686 |
EC_DBG("\n");
|
|
687 |
EC_DBG(KERN_DEBUG "EtherCAT: rx_data content:\n");
|
|
688 |
EC_DBG(KERN_DEBUG);
|
|
689 |
for (i = 0; i < rx_data_length; i++)
|
|
690 |
{
|
|
691 |
EC_DBG("%02X ", master->rx_data[i]);
|
|
692 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
693 |
}
|
|
694 |
EC_DBG("\n");
|
|
695 |
return -1;
|
|
696 |
}
|
|
697 |
|
|
698 |
// Länge des gesamten Frames prüfen
|
|
699 |
length = ((master->rx_data[1] & 0x07) << 8) | (master->rx_data[0] & 0xFF);
|
|
700 |
|
|
701 |
if (length > rx_data_length)
|
|
702 |
{
|
|
703 |
EC_DBG(KERN_ERR "EtherCAT: Received corrupted frame (length does not match)!\n");
|
|
704 |
EC_DBG(KERN_DEBUG "EtherCAT: tx_data content:\n");
|
|
705 |
EC_DBG(KERN_DEBUG);
|
|
706 |
for (i = 0; i < master->tx_data_length; i++)
|
|
707 |
{
|
|
708 |
EC_DBG("%02X ", master->tx_data[i]);
|
|
709 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
710 |
}
|
|
711 |
EC_DBG("\n");
|
|
712 |
EC_DBG(KERN_DEBUG "EtherCAT: rx_data content:\n");
|
|
713 |
EC_DBG(KERN_DEBUG);
|
|
714 |
for (i = 0; i < rx_data_length; i++)
|
|
715 |
{
|
|
716 |
EC_DBG("%02X ", master->rx_data[i]);
|
|
717 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
718 |
}
|
|
719 |
EC_DBG("\n");
|
|
720 |
return -1;
|
|
721 |
}
|
|
722 |
|
|
723 |
pos = 2; // LibPCAP: 16
|
|
724 |
command_follows = 1;
|
|
725 |
while (command_follows)
|
|
726 |
{
|
|
727 |
if (pos + 10 > rx_data_length)
|
|
728 |
{
|
|
729 |
EC_DBG(KERN_ERR "EtherCAT: Received frame with incomplete command header!\n");
|
|
730 |
EC_DBG(KERN_DEBUG "EtherCAT: tx_data content:\n");
|
|
731 |
EC_DBG(KERN_DEBUG);
|
|
732 |
for (i = 0; i < master->tx_data_length; i++)
|
|
733 |
{
|
|
734 |
EC_DBG("%02X ", master->tx_data[i]);
|
|
735 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
736 |
}
|
|
737 |
EC_DBG("\n");
|
|
738 |
EC_DBG(KERN_DEBUG "EtherCAT: rx_data content:\n");
|
|
739 |
EC_DBG(KERN_DEBUG);
|
|
740 |
for (i = 0; i < rx_data_length; i++)
|
|
741 |
{
|
|
742 |
EC_DBG("%02X ", master->rx_data[i]);
|
|
743 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
744 |
}
|
|
745 |
EC_DBG("\n");
|
|
746 |
return -1;
|
|
747 |
}
|
|
748 |
|
|
749 |
command_type = master->rx_data[pos];
|
|
750 |
command_index = master->rx_data[pos + 1];
|
|
751 |
length = (master->rx_data[pos + 6] & 0xFF)
|
|
752 |
| ((master->rx_data[pos + 7] & 0x07) << 8);
|
|
753 |
command_follows = master->rx_data[pos + 7] & 0x80;
|
|
754 |
|
|
755 |
if (pos + length + 12 > rx_data_length)
|
|
756 |
{
|
|
757 |
EC_DBG(KERN_ERR "EtherCAT: Received frame with incomplete command data!\n");
|
|
758 |
EC_DBG(KERN_DEBUG "EtherCAT: tx_data content:\n");
|
|
759 |
EC_DBG(KERN_DEBUG);
|
|
760 |
for (i = 0; i < master->tx_data_length; i++)
|
|
761 |
{
|
|
762 |
EC_DBG("%02X ", master->tx_data[i]);
|
|
763 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
764 |
}
|
|
765 |
EC_DBG("\n");
|
|
766 |
EC_DBG(KERN_DEBUG "EtherCAT: rx_data content:\n");
|
|
767 |
EC_DBG(KERN_DEBUG);
|
|
768 |
for (i = 0; i < rx_data_length; i++)
|
|
769 |
{
|
|
770 |
EC_DBG("%02X ", master->rx_data[i]);
|
|
771 |
if ((i + 1) % 16 == 0) EC_DBG("\n" KERN_DEBUG);
|
|
772 |
}
|
|
773 |
EC_DBG("\n");
|
|
774 |
return -1;
|
|
775 |
}
|
|
776 |
|
|
777 |
found = 0;
|
|
778 |
|
|
779 |
for (cmd = master->first_command; cmd != NULL; cmd = cmd->next)
|
|
780 |
{
|
|
781 |
if (cmd->state == ECAT_CS_SENT
|
|
782 |
&& cmd->type == command_type
|
|
783 |
&& cmd->index == command_index
|
|
784 |
&& cmd->data_length == length)
|
|
785 |
{
|
|
786 |
found = 1;
|
|
787 |
cmd->state = ECAT_CS_RECEIVED;
|
|
788 |
|
|
789 |
// Empfangene Daten in Kommandodatenspeicher kopieren
|
|
790 |
memcpy(cmd->data, master->rx_data + pos + 10, length);
|
|
791 |
|
|
792 |
// Working-Counter setzen
|
|
793 |
cmd->working_counter = (master->rx_data[pos + length + 10] & 0xFF)
|
|
794 |
| ((master->rx_data[pos + length + 11] & 0xFF) << 8);
|
|
795 |
}
|
|
796 |
}
|
|
797 |
|
|
798 |
if (!found)
|
|
799 |
{
|
|
800 |
EC_DBG(KERN_WARNING "EtherCAT: WARNING - Command not assigned!\n");
|
|
801 |
}
|
|
802 |
|
|
803 |
pos += length + 12;
|
|
804 |
}
|
|
805 |
|
|
806 |
for (cmd = master->first_command; cmd != NULL; cmd = cmd->next)
|
|
807 |
{
|
|
808 |
if (cmd->state == ECAT_CS_SENT)
|
|
809 |
{
|
|
810 |
EC_DBG(KERN_WARNING "EtherCAT: WARNING - One command received no response!\n");
|
|
811 |
}
|
|
812 |
}
|
|
813 |
|
|
814 |
master->dev->state = ECAT_DS_READY;
|
|
815 |
|
|
816 |
return 0;
|
|
817 |
}
|
|
818 |
|
|
819 |
/***************************************************************/
|
|
820 |
|
|
821 |
#define ECAT_FUNC_HEADER \
|
|
822 |
EtherCAT_command_t *cmd; \
|
|
823 |
if ((cmd = alloc_cmd(master)) == NULL) \
|
|
824 |
{ \
|
|
825 |
EC_DBG(KERN_ERR "EtherCAT: Out of memory while creating command!\n"); \
|
|
826 |
return NULL; \
|
|
827 |
} \
|
|
828 |
EtherCAT_command_init(cmd)
|
|
829 |
|
|
830 |
#define ECAT_FUNC_WRITE_FOOTER \
|
|
831 |
cmd->data_length = length; \
|
|
832 |
memcpy(cmd->data, data, length); \
|
|
833 |
add_command(master, cmd); \
|
|
834 |
return cmd
|
|
835 |
|
|
836 |
#define ECAT_FUNC_READ_FOOTER \
|
|
837 |
cmd->data_length = length; \
|
|
838 |
add_command(master, cmd); \
|
|
839 |
return cmd
|
|
840 |
|
|
841 |
/***************************************************************/
|
|
842 |
|
|
843 |
/**
|
|
844 |
Erstellt ein EtherCAT-NPRD-Kommando.
|
|
845 |
|
|
846 |
Alloziert ein "node-adressed physical read"-Kommando
|
|
847 |
und fügt es in die Liste des Masters ein.
|
|
848 |
|
|
849 |
@param master EtherCAT-Master
|
|
850 |
@param node_address Adresse des Knotens (Slaves)
|
|
851 |
@param offset Physikalische Speicheradresse im Slave
|
|
852 |
@param length Länge der zu lesenden Daten
|
|
853 |
|
|
854 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
855 |
*/
|
|
856 |
|
|
857 |
EtherCAT_command_t *EtherCAT_read(EtherCAT_master_t *master,
|
|
858 |
unsigned short node_address,
|
|
859 |
unsigned short offset,
|
|
860 |
unsigned int length)
|
|
861 |
{
|
|
862 |
if (node_address == 0x0000)
|
|
863 |
EC_DBG(KERN_WARNING "EtherCAT: Using node address 0x0000!\n");
|
|
864 |
|
|
865 |
ECAT_FUNC_HEADER;
|
|
866 |
|
|
867 |
cmd->type = ECAT_CMD_NPRD;
|
|
868 |
cmd->address.phy.dev.node = node_address;
|
|
869 |
cmd->address.phy.mem = offset;
|
|
870 |
|
|
871 |
ECAT_FUNC_READ_FOOTER;
|
|
872 |
}
|
|
873 |
|
|
874 |
/***************************************************************/
|
|
875 |
|
|
876 |
/**
|
|
877 |
Erstellt ein EtherCAT-NPWR-Kommando.
|
|
878 |
|
|
879 |
Alloziert ein "node-adressed physical write"-Kommando
|
|
880 |
und fügt es in die Liste des Masters ein.
|
|
881 |
|
|
882 |
@param master EtherCAT-Master
|
|
883 |
@param node_address Adresse des Knotens (Slaves)
|
|
884 |
@param offset Physikalische Speicheradresse im Slave
|
|
885 |
@param length Länge der zu schreibenden Daten
|
|
886 |
@param data Zeiger auf Speicher mit zu schreibenden Daten
|
|
887 |
|
|
888 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
889 |
*/
|
|
890 |
|
|
891 |
EtherCAT_command_t *EtherCAT_write(EtherCAT_master_t *master,
|
|
892 |
unsigned short node_address,
|
|
893 |
unsigned short offset,
|
|
894 |
unsigned int length,
|
|
895 |
const unsigned char *data)
|
|
896 |
{
|
|
897 |
if (node_address == 0x0000)
|
|
898 |
EC_DBG(KERN_WARNING "WARNING: Using node address 0x0000!\n");
|
|
899 |
|
|
900 |
ECAT_FUNC_HEADER;
|
|
901 |
|
|
902 |
cmd->type = ECAT_CMD_NPWR;
|
|
903 |
cmd->address.phy.dev.node = node_address;
|
|
904 |
cmd->address.phy.mem = offset;
|
|
905 |
|
|
906 |
ECAT_FUNC_WRITE_FOOTER;
|
|
907 |
}
|
|
908 |
|
|
909 |
/***************************************************************/
|
|
910 |
|
|
911 |
/**
|
|
912 |
Erstellt ein EtherCAT-APRD-Kommando.
|
|
913 |
|
|
914 |
Alloziert ein "autoincerement physical read"-Kommando
|
|
915 |
und fügt es in die Liste des Masters ein.
|
|
916 |
|
|
917 |
@param master EtherCAT-Master
|
|
918 |
@param ring_position (Negative) Position des Slaves im Bus
|
|
919 |
@param offset Physikalische Speicheradresse im Slave
|
|
920 |
@param length Länge der zu lesenden Daten
|
|
921 |
|
|
922 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
923 |
*/
|
|
924 |
|
|
925 |
EtherCAT_command_t *EtherCAT_position_read(EtherCAT_master_t *master,
|
|
926 |
short ring_position,
|
|
927 |
unsigned short offset,
|
|
928 |
unsigned int length)
|
|
929 |
{
|
|
930 |
ECAT_FUNC_HEADER;
|
|
931 |
|
|
932 |
cmd->type = ECAT_CMD_APRD;
|
|
933 |
cmd->address.phy.dev.pos = ring_position;
|
|
934 |
cmd->address.phy.mem = offset;
|
|
935 |
|
|
936 |
ECAT_FUNC_READ_FOOTER;
|
|
937 |
}
|
|
938 |
|
|
939 |
/***************************************************************/
|
|
940 |
|
|
941 |
/**
|
|
942 |
Erstellt ein EtherCAT-APWR-Kommando.
|
|
943 |
|
|
944 |
Alloziert ein "autoincrement physical write"-Kommando
|
|
945 |
und fügt es in die Liste des Masters ein.
|
|
946 |
|
|
947 |
@param master EtherCAT-Master
|
|
948 |
@param ring_position (Negative) Position des Slaves im Bus
|
|
949 |
@param offset Physikalische Speicheradresse im Slave
|
|
950 |
@param length Länge der zu schreibenden Daten
|
|
951 |
@param data Zeiger auf Speicher mit zu schreibenden Daten
|
|
952 |
|
|
953 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
954 |
*/
|
|
955 |
|
|
956 |
EtherCAT_command_t *EtherCAT_position_write(EtherCAT_master_t *master,
|
|
957 |
short ring_position,
|
|
958 |
unsigned short offset,
|
|
959 |
unsigned int length,
|
|
960 |
const unsigned char *data)
|
|
961 |
{
|
|
962 |
ECAT_FUNC_HEADER;
|
|
963 |
|
|
964 |
cmd->type = ECAT_CMD_APWR;
|
|
965 |
cmd->address.phy.dev.pos = ring_position;
|
|
966 |
cmd->address.phy.mem = offset;
|
|
967 |
|
|
968 |
ECAT_FUNC_WRITE_FOOTER;
|
|
969 |
}
|
|
970 |
|
|
971 |
/***************************************************************/
|
|
972 |
|
|
973 |
/**
|
|
974 |
Erstellt ein EtherCAT-BRD-Kommando.
|
|
975 |
|
|
976 |
Alloziert ein "broadcast read"-Kommando
|
|
977 |
und fügt es in die Liste des Masters ein.
|
|
978 |
|
|
979 |
@param master EtherCAT-Master
|
|
980 |
@param offset Physikalische Speicheradresse im Slave
|
|
981 |
@param length Länge der zu lesenden Daten
|
|
982 |
|
|
983 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
984 |
*/
|
|
985 |
|
|
986 |
EtherCAT_command_t *EtherCAT_broadcast_read(EtherCAT_master_t *master,
|
|
987 |
unsigned short offset,
|
|
988 |
unsigned int length)
|
|
989 |
{
|
|
990 |
ECAT_FUNC_HEADER;
|
|
991 |
|
|
992 |
cmd->type = ECAT_CMD_BRD;
|
|
993 |
cmd->address.phy.dev.node = 0x0000;
|
|
994 |
cmd->address.phy.mem = offset;
|
|
995 |
|
|
996 |
ECAT_FUNC_READ_FOOTER;
|
|
997 |
}
|
|
998 |
|
|
999 |
/***************************************************************/
|
|
1000 |
|
|
1001 |
/**
|
|
1002 |
Erstellt ein EtherCAT-BWR-Kommando.
|
|
1003 |
|
|
1004 |
Alloziert ein "broadcast write"-Kommando
|
|
1005 |
und fügt es in die Liste des Masters ein.
|
|
1006 |
|
|
1007 |
@param master EtherCAT-Master
|
|
1008 |
@param offset Physikalische Speicheradresse im Slave
|
|
1009 |
@param length Länge der zu schreibenden Daten
|
|
1010 |
@param data Zeiger auf Speicher mit zu schreibenden Daten
|
|
1011 |
|
|
1012 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
1013 |
*/
|
|
1014 |
|
|
1015 |
EtherCAT_command_t *EtherCAT_broadcast_write(EtherCAT_master_t *master,
|
|
1016 |
unsigned short offset,
|
|
1017 |
unsigned int length,
|
|
1018 |
const unsigned char *data)
|
|
1019 |
{
|
|
1020 |
ECAT_FUNC_HEADER;
|
|
1021 |
|
|
1022 |
cmd->type = ECAT_CMD_BWR;
|
|
1023 |
cmd->address.phy.dev.node = 0x0000;
|
|
1024 |
cmd->address.phy.mem = offset;
|
|
1025 |
|
|
1026 |
ECAT_FUNC_WRITE_FOOTER;
|
|
1027 |
}
|
|
1028 |
|
|
1029 |
/***************************************************************/
|
|
1030 |
|
|
1031 |
/**
|
|
1032 |
Erstellt ein EtherCAT-LRW-Kommando.
|
|
1033 |
|
|
1034 |
Alloziert ein "logical read write"-Kommando
|
|
1035 |
und fügt es in die Liste des Masters ein.
|
|
1036 |
|
|
1037 |
@param master EtherCAT-Master
|
|
1038 |
@param offset Logische Speicheradresse
|
|
1039 |
@param length Länge der zu lesenden/schreibenden Daten
|
|
1040 |
@param data Zeiger auf Speicher mit zu lesenden/schreibenden Daten
|
|
1041 |
|
|
1042 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
1043 |
*/
|
|
1044 |
|
|
1045 |
EtherCAT_command_t *EtherCAT_logical_read_write(EtherCAT_master_t *master,
|
|
1046 |
unsigned int offset,
|
|
1047 |
unsigned int length,
|
|
1048 |
unsigned char *data)
|
|
1049 |
{
|
|
1050 |
ECAT_FUNC_HEADER;
|
|
1051 |
|
|
1052 |
cmd->type = ECAT_CMD_LRW;
|
|
1053 |
cmd->address.logical = offset;
|
|
1054 |
|
|
1055 |
ECAT_FUNC_WRITE_FOOTER;
|
|
1056 |
}
|
|
1057 |
|
|
1058 |
/***************************************************************/
|
|
1059 |
|
|
1060 |
/**
|
|
1061 |
Alloziert ein neues Kommando aus dem Kommandoring.
|
|
1062 |
|
|
1063 |
Durchsucht den Kommandoring nach einem freien Kommando,
|
|
1064 |
reserviert es und gibt dessen Adresse zurück.
|
|
1065 |
|
|
1066 |
@param master EtherCAT-Master
|
|
1067 |
|
|
1068 |
@return Adresse des Kommandos bei Erfolg, sonst NULL
|
|
1069 |
*/
|
|
1070 |
|
|
1071 |
EtherCAT_command_t *alloc_cmd(EtherCAT_master_t *master)
|
|
1072 |
{
|
|
1073 |
int j;
|
|
1074 |
|
|
1075 |
for (j = 0; j < ECAT_COMMAND_RING_SIZE; j++) // Einmal rum suchen
|
|
1076 |
{
|
|
1077 |
// Solange suchen, bis freies Kommando gefunden
|
|
1078 |
if (master->cmd_ring[master->cmd_ring_index].reserved == 0)
|
|
1079 |
{
|
|
1080 |
master->cmd_ring[master->cmd_ring_index].reserved = 1; // Belegen
|
|
1081 |
return &master->cmd_ring[master->cmd_ring_index];
|
|
1082 |
}
|
|
1083 |
|
|
1084 |
master->cmd_ring_index++;
|
|
1085 |
master->cmd_ring_index %= ECAT_COMMAND_RING_SIZE;
|
|
1086 |
}
|
|
1087 |
|
|
1088 |
EC_DBG(KERN_WARNING "EtherCAT: Command ring full!\n");
|
|
1089 |
|
|
1090 |
return NULL; // Nix gefunden
|
|
1091 |
}
|
|
1092 |
|
|
1093 |
/***************************************************************/
|
|
1094 |
|
|
1095 |
/**
|
|
1096 |
Fügt ein Kommando in die Liste des Masters ein.
|
|
1097 |
|
|
1098 |
@param master EtherCAT-Master
|
|
1099 |
@param cmd Zeiger auf das einzufügende Kommando
|
|
1100 |
*/
|
|
1101 |
|
|
1102 |
void add_command(EtherCAT_master_t *master,
|
|
1103 |
EtherCAT_command_t *cmd)
|
|
1104 |
{
|
|
1105 |
EtherCAT_command_t **last_cmd;
|
|
1106 |
|
|
1107 |
// Find the address of the last pointer in the list
|
|
1108 |
last_cmd = &(master->first_command);
|
|
1109 |
while (*last_cmd) last_cmd = &(*last_cmd)->next;
|
|
1110 |
|
|
1111 |
// Let this pointer point to the new command
|
|
1112 |
*last_cmd = cmd;
|
|
1113 |
}
|
|
1114 |
|
|
1115 |
/***************************************************************/
|
|
1116 |
|
|
1117 |
/**
|
|
1118 |
Entfernt ein Kommando aus der Liste und gibt es frei.
|
|
1119 |
|
|
1120 |
Prüft erst, ob das Kommando in der Liste des Masters
|
|
1121 |
ist. Wenn ja, wird es entfernt und die Reservierung wird
|
|
1122 |
aufgehoben.
|
|
1123 |
|
|
1124 |
@param master EtherCAT-Master
|
|
1125 |
@param rem_cmd Zeiger auf das zu entfernende Kommando
|
|
1126 |
|
|
1127 |
@return 0 bei Erfolg, sonst < 0
|
|
1128 |
*/
|
|
1129 |
|
|
1130 |
void EtherCAT_remove_command(EtherCAT_master_t *master,
|
|
1131 |
EtherCAT_command_t *rem_cmd)
|
|
1132 |
{
|
|
1133 |
EtherCAT_command_t **last_cmd;
|
|
1134 |
|
|
1135 |
last_cmd = &master->first_command;
|
|
1136 |
while (*last_cmd)
|
|
1137 |
{
|
|
1138 |
if (*last_cmd == rem_cmd)
|
|
1139 |
{
|
|
1140 |
*last_cmd = rem_cmd->next;
|
|
1141 |
|
|
1142 |
EtherCAT_command_clear(rem_cmd);
|
|
1143 |
|
|
1144 |
return;
|
|
1145 |
}
|
|
1146 |
|
|
1147 |
last_cmd = &(*last_cmd)->next;
|
|
1148 |
}
|
|
1149 |
|
|
1150 |
EC_DBG(KERN_WARNING "EtherCAT: Trying to remove non-existent command!\n");
|
|
1151 |
}
|
|
1152 |
|
|
1153 |
/***************************************************************/
|
|
1154 |
|
|
1155 |
/**
|
|
1156 |
Ändert den Zustand eines Slaves (asynchron).
|
|
1157 |
|
|
1158 |
Führt eine (asynchrone) Zustandsänderung bei einem Slave durch.
|
|
1159 |
|
|
1160 |
@param master EtherCAT-Master
|
|
1161 |
@param slave Slave, dessen Zustand geändert werden soll
|
|
1162 |
@param state_and_ack Neuer Zustand, evtl. mit gesetztem
|
|
1163 |
Acknowledge-Flag
|
|
1164 |
|
|
1165 |
@return 0 bei Erfolg, sonst < 0
|
|
1166 |
*/
|
|
1167 |
|
|
1168 |
int EtherCAT_state_change(EtherCAT_master_t *master,
|
|
1169 |
EtherCAT_slave_t *slave,
|
|
1170 |
unsigned char state_and_ack)
|
|
1171 |
{
|
|
1172 |
EtherCAT_command_t *cmd;
|
|
1173 |
unsigned char data[2];
|
|
1174 |
unsigned int tries_left;
|
|
1175 |
|
|
1176 |
data[0] = state_and_ack;
|
|
1177 |
data[1] = 0x00;
|
|
1178 |
|
|
1179 |
if ((cmd = EtherCAT_write(master, slave->station_address,
|
|
1180 |
0x0120, 2, data)) == NULL)
|
|
1181 |
{
|
|
1182 |
EC_DBG(KERN_ERR "EtherCAT: Could not set state %02X - Out of memory!\n", state_and_ack);
|
|
1183 |
return -1;
|
|
1184 |
}
|
|
1185 |
|
|
1186 |
if (EtherCAT_async_send_receive(master) != 0)
|
|
1187 |
{
|
|
1188 |
EtherCAT_remove_command(master, cmd);
|
|
1189 |
|
|
1190 |
EC_DBG(KERN_ERR "EtherCAT: Could not set state %02X - Unable to send!\n", state_and_ack);
|
|
1191 |
return -2;
|
|
1192 |
}
|
|
1193 |
|
|
1194 |
if (cmd->working_counter != 1)
|
|
1195 |
{
|
|
1196 |
EtherCAT_remove_command(master, cmd);
|
|
1197 |
|
|
1198 |
EC_DBG(KERN_ERR "EtherCAT: Could not set state %02X - Device did not respond!\n", state_and_ack);
|
|
1199 |
return -3;
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
EtherCAT_remove_command(master, cmd);
|
|
1203 |
|
|
1204 |
slave->requested_state = state_and_ack & 0x0F;
|
|
1205 |
|
|
1206 |
tries_left = 1000;
|
|
1207 |
|
|
1208 |
while (tries_left)
|
|
1209 |
{
|
|
1210 |
udelay(10);
|
|
1211 |
|
|
1212 |
if ((cmd = EtherCAT_read(master, slave->station_address,
|
|
1213 |
0x0130, 2)) == NULL)
|
|
1214 |
{
|
|
1215 |
EC_DBG(KERN_ERR "EtherCAT: Could not check state %02X - Out of memory!\n", state_and_ack);
|
|
1216 |
return -1;
|
|
1217 |
}
|
|
1218 |
|
|
1219 |
if (EtherCAT_async_send_receive(master) != 0)
|
|
1220 |
{
|
|
1221 |
EtherCAT_remove_command(master, cmd);
|
|
1222 |
|
|
1223 |
EC_DBG(KERN_ERR "EtherCAT: Could not check state %02X - Unable to send!\n", state_and_ack);
|
|
1224 |
return -2;
|
|
1225 |
}
|
|
1226 |
|
|
1227 |
if (cmd->working_counter != 1)
|
|
1228 |
{
|
|
1229 |
EtherCAT_remove_command(master, cmd);
|
|
1230 |
|
|
1231 |
EC_DBG(KERN_ERR "EtherCAT: Could not check state %02X - Device did not respond!\n", state_and_ack);
|
|
1232 |
return -3;
|
|
1233 |
}
|
|
1234 |
|
|
1235 |
if (cmd->data[0] & 0x10) // State change error
|
|
1236 |
{
|
|
1237 |
EtherCAT_remove_command(master, cmd);
|
|
1238 |
|
|
1239 |
EC_DBG(KERN_ERR "EtherCAT: Could not set state %02X - Device refused state change (code %02X)!\n", state_and_ack, cmd->data[0]);
|
|
1240 |
return -4;
|
|
1241 |
}
|
|
1242 |
|
|
1243 |
if (cmd->data[0] == (state_and_ack & 0x0F)) // State change successful
|
|
1244 |
{
|
|
1245 |
EtherCAT_remove_command(master, cmd);
|
|
1246 |
|
|
1247 |
break;
|
|
1248 |
}
|
|
1249 |
|
|
1250 |
EtherCAT_remove_command(master, cmd);
|
|
1251 |
|
|
1252 |
tries_left--;
|
|
1253 |
}
|
|
1254 |
|
|
1255 |
if (!tries_left)
|
|
1256 |
{
|
|
1257 |
EC_DBG(KERN_ERR "EtherCAT: Could not check state %02X - Timeout while checking!\n", state_and_ack);
|
|
1258 |
return -5;
|
|
1259 |
}
|
|
1260 |
|
|
1261 |
slave->current_state = state_and_ack & 0x0F;
|
|
1262 |
|
|
1263 |
return 0;
|
|
1264 |
}
|
|
1265 |
|
|
1266 |
/***************************************************************/
|
|
1267 |
|
|
1268 |
/**
|
|
1269 |
Konfiguriert einen Slave und setzt den Operational-Zustand.
|
|
1270 |
|
|
1271 |
Führt eine komplette Konfiguration eines Slaves durch,
|
|
1272 |
setzt Sync-Manager und FMMU's, führt die entsprechenden
|
|
1273 |
Zustandsübergänge durch, bis der Slave betriebsbereit ist.
|
|
1274 |
|
|
1275 |
@param master EtherCAT-Master
|
|
1276 |
@param slave Zu aktivierender Slave
|
|
1277 |
|
|
1278 |
@return 0 bei Erfolg, sonst < 0
|
|
1279 |
*/
|
|
1280 |
|
|
1281 |
int EtherCAT_activate_slave(EtherCAT_master_t *master,
|
|
1282 |
EtherCAT_slave_t *slave)
|
|
1283 |
{
|
|
1284 |
EtherCAT_command_t *cmd;
|
|
1285 |
const EtherCAT_slave_desc_t *desc;
|
|
1286 |
unsigned char fmmu[16];
|
|
1287 |
unsigned char data[256];
|
|
1288 |
|
|
1289 |
desc = slave->desc;
|
|
1290 |
|
|
1291 |
if (EtherCAT_state_change(master, slave, ECAT_STATE_INIT) != 0)
|
|
1292 |
{
|
|
1293 |
return -1;
|
|
1294 |
}
|
|
1295 |
|
|
1296 |
// Resetting FMMU's
|
|
1297 |
|
|
1298 |
memset(data, 0x00, 256);
|
|
1299 |
|
|
1300 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0600, 256, data)) == NULL)
|
|
1301 |
return -1;
|
|
1302 |
|
|
1303 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1304 |
{
|
|
1305 |
EtherCAT_remove_command(master, cmd);
|
|
1306 |
|
|
1307 |
return -1;
|
|
1308 |
}
|
|
1309 |
|
|
1310 |
if (cmd->working_counter != 1)
|
|
1311 |
{
|
|
1312 |
EtherCAT_remove_command(master, cmd);
|
|
1313 |
|
|
1314 |
EC_DBG(KERN_ERR "EtherCAT: Resetting FMMUs - Slave %04X did not respond!\n",
|
|
1315 |
slave->station_address);
|
|
1316 |
return -2;
|
|
1317 |
}
|
|
1318 |
|
|
1319 |
EtherCAT_remove_command(master, cmd);
|
|
1320 |
|
|
1321 |
// Resetting Sync Manager channels
|
|
1322 |
|
|
1323 |
if (desc->type != ECAT_ST_SIMPLE_NOSYNC)
|
|
1324 |
{
|
|
1325 |
memset(data, 0x00, 256);
|
|
1326 |
|
|
1327 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0800, 256, data)) == NULL)
|
|
1328 |
return -1;
|
|
1329 |
|
|
1330 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1331 |
{
|
|
1332 |
EtherCAT_remove_command(master, cmd);
|
|
1333 |
|
|
1334 |
return -1;
|
|
1335 |
}
|
|
1336 |
|
|
1337 |
if (cmd->working_counter != 1)
|
|
1338 |
{
|
|
1339 |
EtherCAT_remove_command(master, cmd);
|
|
1340 |
|
|
1341 |
EC_DBG(KERN_ERR "EtherCAT: Resetting SMs - Slave %04X did not respond!\n",
|
|
1342 |
slave->station_address);
|
|
1343 |
return -2;
|
|
1344 |
}
|
|
1345 |
|
|
1346 |
EtherCAT_remove_command(master, cmd);
|
|
1347 |
}
|
|
1348 |
|
|
1349 |
// Init Mailbox communication
|
|
1350 |
|
|
1351 |
if (desc->type == ECAT_ST_MAILBOX)
|
|
1352 |
{
|
|
1353 |
if (desc->sm0)
|
|
1354 |
{
|
|
1355 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0800, 8, desc->sm0)) == NULL)
|
|
1356 |
return -1;
|
|
1357 |
|
|
1358 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1359 |
{
|
|
1360 |
EtherCAT_remove_command(master, cmd);
|
|
1361 |
|
|
1362 |
return -1;
|
|
1363 |
}
|
|
1364 |
|
|
1365 |
if (cmd->working_counter != 1)
|
|
1366 |
{
|
|
1367 |
EtherCAT_remove_command(master, cmd);
|
|
1368 |
|
|
1369 |
EC_DBG(KERN_ERR "EtherCAT: Setting SM0 - Slave %04X did not respond!\n",
|
|
1370 |
slave->station_address);
|
|
1371 |
return -3;
|
|
1372 |
}
|
|
1373 |
|
|
1374 |
EtherCAT_remove_command(master, cmd);
|
|
1375 |
}
|
|
1376 |
|
|
1377 |
if (desc->sm1)
|
|
1378 |
{
|
|
1379 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0808, 8, desc->sm1)) == NULL)
|
|
1380 |
return -1;
|
|
1381 |
|
|
1382 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1383 |
{
|
|
1384 |
EtherCAT_remove_command(master, cmd);
|
|
1385 |
|
|
1386 |
return -1;
|
|
1387 |
}
|
|
1388 |
|
|
1389 |
if (cmd->working_counter != 1)
|
|
1390 |
{
|
|
1391 |
EtherCAT_remove_command(master, cmd);
|
|
1392 |
|
|
1393 |
EC_DBG(KERN_ERR "EtherCAT: Setting SM1 - Slave %04X did not respond!\n",
|
|
1394 |
slave->station_address);
|
|
1395 |
return -2;
|
|
1396 |
}
|
|
1397 |
|
|
1398 |
EtherCAT_remove_command(master, cmd);
|
|
1399 |
}
|
|
1400 |
}
|
|
1401 |
|
|
1402 |
// Change state to PREOP
|
|
1403 |
|
|
1404 |
if (EtherCAT_state_change(master, slave, ECAT_STATE_PREOP) != 0)
|
|
1405 |
{
|
|
1406 |
return -5;
|
|
1407 |
}
|
|
1408 |
|
|
1409 |
// Set FMMU's
|
|
1410 |
|
|
1411 |
if (desc->fmmu0)
|
|
1412 |
{
|
|
1413 |
memcpy(fmmu, desc->fmmu0, 16);
|
|
1414 |
|
|
1415 |
fmmu[0] = slave->logical_address0 & 0x000000FF;
|
|
1416 |
fmmu[1] = (slave->logical_address0 & 0x0000FF00) >> 8;
|
|
1417 |
fmmu[2] = (slave->logical_address0 & 0x00FF0000) >> 16;
|
|
1418 |
fmmu[3] = (slave->logical_address0 & 0xFF000000) >> 24;
|
|
1419 |
|
|
1420 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0600, 16, fmmu)) == NULL)
|
|
1421 |
return -1;
|
|
1422 |
|
|
1423 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1424 |
{
|
|
1425 |
EtherCAT_remove_command(master, cmd);
|
|
1426 |
|
|
1427 |
return -1;
|
|
1428 |
}
|
|
1429 |
|
|
1430 |
if (cmd->working_counter != 1)
|
|
1431 |
{
|
|
1432 |
EtherCAT_remove_command(master, cmd);
|
|
1433 |
|
|
1434 |
EC_DBG(KERN_ERR "EtherCAT: Setting FMMU0 - Slave %04X did not respond!\n",
|
|
1435 |
slave->station_address);
|
|
1436 |
return -2;
|
|
1437 |
}
|
|
1438 |
|
|
1439 |
EtherCAT_remove_command(master, cmd);
|
|
1440 |
}
|
|
1441 |
|
|
1442 |
// Set Sync Managers
|
|
1443 |
|
|
1444 |
if (desc->type != ECAT_ST_MAILBOX)
|
|
1445 |
{
|
|
1446 |
if (desc->sm0)
|
|
1447 |
{
|
|
1448 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0800, 8, desc->sm0)) == NULL)
|
|
1449 |
return -1;
|
|
1450 |
|
|
1451 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1452 |
{
|
|
1453 |
EtherCAT_remove_command(master, cmd);
|
|
1454 |
|
|
1455 |
return -1;
|
|
1456 |
}
|
|
1457 |
|
|
1458 |
if (cmd->working_counter != 1)
|
|
1459 |
{
|
|
1460 |
EtherCAT_remove_command(master, cmd);
|
|
1461 |
|
|
1462 |
EC_DBG(KERN_ERR "EtherCAT: Setting SM0 - Slave %04X did not respond!\n",
|
|
1463 |
slave->station_address);
|
|
1464 |
return -3;
|
|
1465 |
}
|
|
1466 |
|
|
1467 |
EtherCAT_remove_command(master, cmd);
|
|
1468 |
}
|
|
1469 |
|
|
1470 |
if (desc->sm1)
|
|
1471 |
{
|
|
1472 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0808, 8, desc->sm1)) == NULL)
|
|
1473 |
return -1;
|
|
1474 |
|
|
1475 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1476 |
{
|
|
1477 |
EtherCAT_remove_command(master, cmd);
|
|
1478 |
|
|
1479 |
return -1;
|
|
1480 |
}
|
|
1481 |
|
|
1482 |
if (cmd->working_counter != 1)
|
|
1483 |
{
|
|
1484 |
EtherCAT_remove_command(master, cmd);
|
|
1485 |
|
|
1486 |
EC_DBG(KERN_ERR "EtherCAT: Setting SM1 - Slave %04X did not respond!\n",
|
|
1487 |
slave->station_address);
|
|
1488 |
return -3;
|
|
1489 |
}
|
|
1490 |
|
|
1491 |
EtherCAT_remove_command(master, cmd);
|
|
1492 |
}
|
|
1493 |
}
|
|
1494 |
|
|
1495 |
if (desc->sm2)
|
|
1496 |
{
|
|
1497 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0810, 8, desc->sm2)) == NULL)
|
|
1498 |
return -1;
|
|
1499 |
|
|
1500 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1501 |
{
|
|
1502 |
EtherCAT_remove_command(master, cmd);
|
|
1503 |
|
|
1504 |
return -1;
|
|
1505 |
}
|
|
1506 |
|
|
1507 |
if (cmd->working_counter != 1)
|
|
1508 |
{
|
|
1509 |
EtherCAT_remove_command(master, cmd);
|
|
1510 |
|
|
1511 |
EC_DBG(KERN_ERR "EtherCAT: Setting SM2 - Slave %04X did not respond!\n",
|
|
1512 |
slave->station_address);
|
|
1513 |
return -3;
|
|
1514 |
}
|
|
1515 |
|
|
1516 |
EtherCAT_remove_command(master, cmd);
|
|
1517 |
}
|
|
1518 |
|
|
1519 |
if (desc->sm3)
|
|
1520 |
{
|
|
1521 |
if ((cmd = EtherCAT_write(master, slave->station_address, 0x0818, 8, desc->sm3)) == NULL)
|
|
1522 |
return -1;
|
|
1523 |
|
|
1524 |
if (EtherCAT_async_send_receive(master) < 0)
|
|
1525 |
{
|
|
1526 |
EtherCAT_remove_command(master, cmd);
|
|
1527 |
|
|
1528 |
return -1;
|
|
1529 |
}
|
|
1530 |
|
|
1531 |
if (cmd->working_counter != 1)
|
|
1532 |
{
|
|
1533 |
EtherCAT_remove_command(master, cmd);
|
|
1534 |
|
|
1535 |
EC_DBG(KERN_ERR "EtherCAT: Setting SM3 - Slave %04X did not respond!\n",
|
|
1536 |
slave->station_address);
|
|
1537 |
return -3;
|
|
1538 |
}
|
|
1539 |
|
|
1540 |
EtherCAT_remove_command(master, cmd);
|
|
1541 |
}
|
|
1542 |
|
|
1543 |
// Change state to SAVEOP
|
|
1544 |
if (EtherCAT_state_change(master, slave, ECAT_STATE_SAVEOP) != 0)
|
|
1545 |
{
|
|
1546 |
return -12;
|
|
1547 |
}
|
|
1548 |
|
|
1549 |
// Change state to OP
|
|
1550 |
if (EtherCAT_state_change(master, slave, ECAT_STATE_OP) != 0)
|
|
1551 |
{
|
|
1552 |
return -13;
|
|
1553 |
}
|
|
1554 |
|
|
1555 |
return 0;
|
|
1556 |
}
|
|
1557 |
|
|
1558 |
/***************************************************************/
|
|
1559 |
|
|
1560 |
/**
|
|
1561 |
Setzt einen Slave zurück in den Init-Zustand.
|
|
1562 |
|
|
1563 |
@param master EtherCAT-Master
|
|
1564 |
@param slave Zu deaktivierender Slave
|
|
1565 |
|
|
1566 |
@return 0 bei Erfolg, sonst < 0
|
|
1567 |
*/
|
|
1568 |
|
|
1569 |
int EtherCAT_deactivate_slave(EtherCAT_master_t *master,
|
|
1570 |
EtherCAT_slave_t *slave)
|
|
1571 |
{
|
|
1572 |
if (EtherCAT_state_change(master, slave, ECAT_STATE_INIT) != 0)
|
|
1573 |
{
|
|
1574 |
return -1;
|
|
1575 |
}
|
|
1576 |
|
|
1577 |
return 0;
|
|
1578 |
}
|
|
1579 |
|
|
1580 |
/***************************************************************/
|
|
1581 |
|
|
1582 |
/**
|
|
1583 |
Aktiviert alle Slaves.
|
|
1584 |
|
|
1585 |
@see EtherCAT_activate_slave
|
|
1586 |
|
|
1587 |
@param master EtherCAT-Master
|
|
1588 |
|
|
1589 |
@return 0 bei Erfolg, sonst < 0
|
|
1590 |
*/
|
|
1591 |
|
|
1592 |
int EtherCAT_activate_all_slaves(EtherCAT_master_t *master)
|
|
1593 |
{
|
|
1594 |
unsigned int i;
|
|
1595 |
|
|
1596 |
for (i = 0; i < master->slave_count; i++)
|
|
1597 |
{
|
|
1598 |
EC_DBG("Activate Slave: %d\n",i);
|
|
1599 |
|
|
1600 |
if (EtherCAT_activate_slave(master, &master->slaves[i]) < 0)
|
|
1601 |
{
|
|
1602 |
return -1;
|
|
1603 |
}
|
|
1604 |
|
|
1605 |
EC_DBG("done...\n");
|
|
1606 |
}
|
|
1607 |
|
|
1608 |
return 0;
|
|
1609 |
}
|
|
1610 |
|
|
1611 |
/***************************************************************/
|
|
1612 |
|
|
1613 |
/**
|
|
1614 |
Deaktiviert alle Slaves.
|
|
1615 |
|
|
1616 |
@see EtherCAT_deactivate_slave
|
|
1617 |
|
|
1618 |
@param master EtherCAT-Master
|
|
1619 |
|
|
1620 |
@return 0 bei Erfolg, sonst < 0
|
|
1621 |
*/
|
|
1622 |
|
|
1623 |
int EtherCAT_deactivate_all_slaves(EtherCAT_master_t *master)
|
|
1624 |
{
|
|
1625 |
unsigned int i;
|
|
1626 |
int ret = 0;
|
|
1627 |
|
|
1628 |
for (i = 0; i < master->slave_count; i++)
|
|
1629 |
{
|
|
1630 |
EC_DBG("Deactivate Slave: %d\n",i);
|
|
1631 |
|
|
1632 |
if (EtherCAT_deactivate_slave(master, &master->slaves[i]) < 0)
|
|
1633 |
{
|
|
1634 |
ret = -1;
|
|
1635 |
}
|
|
1636 |
|
|
1637 |
EC_DBG("done...\n");
|
|
1638 |
}
|
|
1639 |
|
|
1640 |
return ret;
|
|
1641 |
}
|
|
1642 |
|
|
1643 |
/***************************************************************/
|
|
1644 |
|
|
1645 |
/**
|
|
1646 |
Sendet alle Prozessdaten an die Slaves.
|
|
1647 |
|
|
1648 |
Erstellt ein "logical read write"-Kommando mit den
|
|
1649 |
Prozessdaten des Masters und sendet es an den Bus.
|
|
1650 |
|
|
1651 |
@param master EtherCAT-Master
|
|
1652 |
|
|
1653 |
@return 0 bei Erfolg, sonst < 0
|
|
1654 |
*/
|
|
1655 |
|
|
1656 |
int EtherCAT_write_process_data(EtherCAT_master_t *master)
|
|
1657 |
{
|
|
1658 |
if (master->process_data_command)
|
|
1659 |
{
|
|
1660 |
EtherCAT_remove_command(master, master->process_data_command);
|
|
1661 |
EtherCAT_command_clear(master->process_data_command);
|
|
1662 |
master->process_data_command = NULL;
|
|
1663 |
}
|
|
1664 |
|
|
1665 |
if ((master->process_data_command
|
|
1666 |
= EtherCAT_logical_read_write(master, 0,
|
|
1667 |
master->process_data_length,
|
|
1668 |
master->process_data)) == NULL)
|
|
1669 |
{
|
|
1670 |
EC_DBG(KERN_ERR "EtherCAT: Could not allocate process data command!\n");
|
|
1671 |
return -1;
|
|
1672 |
}
|
|
1673 |
|
|
1674 |
if (EtherCAT_send(master) < 0)
|
|
1675 |
{
|
|
1676 |
EtherCAT_remove_command(master, master->process_data_command);
|
|
1677 |
EtherCAT_command_clear(master->process_data_command);
|
|
1678 |
master->process_data_command = NULL;
|
|
1679 |
EC_DBG(KERN_ERR "EtherCAT: Could not send process data command!\n");
|
|
1680 |
return -1;
|
|
1681 |
}
|
|
1682 |
|
|
1683 |
return 0;
|
|
1684 |
}
|
|
1685 |
|
|
1686 |
/***************************************************************/
|
|
1687 |
|
|
1688 |
/**
|
|
1689 |
Empfängt alle Prozessdaten von den Slaves.
|
|
1690 |
|
|
1691 |
Empfängt ein zuvor gesendetes "logical read write"-Kommando
|
|
1692 |
und kopiert die empfangenen daten in den Prozessdatenspeicher
|
|
1693 |
des Masters.
|
|
1694 |
|
|
1695 |
@param master EtherCAT-Master
|
|
1696 |
|
|
1697 |
@return 0 bei Erfolg, sonst < 0
|
|
1698 |
*/
|
|
1699 |
|
|
1700 |
int EtherCAT_read_process_data(EtherCAT_master_t *master)
|
|
1701 |
{
|
|
1702 |
int ret = -1;
|
|
1703 |
|
|
1704 |
if (!master->process_data_command)
|
|
1705 |
{
|
|
1706 |
EC_DBG(KERN_WARNING "EtherCAT: No process data command available!\n");
|
|
1707 |
return -1;
|
|
1708 |
}
|
|
1709 |
|
|
1710 |
if (EtherCAT_receive(master) < 0)
|
|
1711 |
{
|
|
1712 |
EC_DBG(KERN_ERR "EtherCAT: Could not receive cyclic command!\n");
|
|
1713 |
}
|
|
1714 |
else if (master->process_data_command->state != ECAT_CS_RECEIVED)
|
|
1715 |
{
|
|
1716 |
EC_DBG(KERN_WARNING "EtherCAT: Process data command not received!\n");
|
|
1717 |
}
|
|
1718 |
else
|
|
1719 |
{
|
|
1720 |
// Daten von Kommando in Prozessdaten des Master kopieren
|
|
1721 |
memcpy(master->process_data, master->process_data_command->data, master->process_data_length);
|
|
1722 |
ret = 0;
|
|
1723 |
}
|
|
1724 |
|
|
1725 |
EtherCAT_remove_command(master, master->process_data_command);
|
|
1726 |
EtherCAT_command_clear(master->process_data_command);
|
|
1727 |
master->process_data_command = NULL;
|
|
1728 |
|
|
1729 |
return ret;
|
|
1730 |
}
|
|
1731 |
|
|
1732 |
/***************************************************************/
|
|
1733 |
|
|
1734 |
/**
|
|
1735 |
Setzt einen Byte-Wert im Speicher.
|
|
1736 |
|
|
1737 |
@param data Startadresse
|
|
1738 |
@param offset Byte-Offset
|
|
1739 |
@param value Wert
|
|
1740 |
*/
|
|
1741 |
|
|
1742 |
void set_byte(unsigned char *data,
|
|
1743 |
unsigned int offset,
|
|
1744 |
unsigned char value)
|
|
1745 |
{
|
|
1746 |
data[offset] = value;
|
|
1747 |
}
|
|
1748 |
|
|
1749 |
/***************************************************************/
|
|
1750 |
|
|
1751 |
/**
|
|
1752 |
Setzt einen Word-Wert im Speicher.
|
|
1753 |
|
|
1754 |
@param data Startadresse
|
|
1755 |
@param offset Byte-Offset
|
|
1756 |
@param value Wert
|
|
1757 |
*/
|
|
1758 |
|
|
1759 |
void set_word(unsigned char *data,
|
|
1760 |
unsigned int offset,
|
|
1761 |
unsigned int value)
|
|
1762 |
{
|
|
1763 |
data[offset] = value & 0xFF;
|
|
1764 |
data[offset + 1] = (value & 0xFF00) >> 8;
|
|
1765 |
}
|
|
1766 |
|
|
1767 |
/***************************************************************/
|