Newer
Older
Florian Pose
committed
/******************************************************************************
* s l a v e . c
Florian Pose
committed
* $Id$
Florian Pose
committed
*****************************************************************************/
Florian Pose
committed
#include <linux/delay.h>
Florian Pose
committed
#include "../include/EtherCAT_si.h"
#include "globals.h"
#include "slave.h"
Florian Pose
committed
#include "frame.h"
Florian Pose
committed
/*****************************************************************************/
Florian Pose
committed
*/
Florian Pose
committed
void ec_slave_init(ec_slave_t *slave, /**< EtherCAT-Slave */
ec_master_t *master /**< EtherCAT-Master */
)
{
slave->master = master;
slave->base_type = 0;
slave->base_revision = 0;
slave->base_build = 0;
slave->base_fmmu_count = 0;
slave->base_sync_count = 0;
slave->ring_position = 0;
slave->station_address = 0;
slave->sii_vendor_id = 0;
slave->sii_product_code = 0;
slave->sii_revision_number = 0;
slave->sii_serial_number = 0;
slave->type = NULL;
slave->registered = 0;
slave->fmmu_count = 0;
}
Florian Pose
committed
/*****************************************************************************/
Florian Pose
committed
/**
EtherCAT-Slave-Destruktor.
Florian Pose
committed
void ec_slave_clear(ec_slave_t *slave /**< EtherCAT-Slave */)
Florian Pose
committed
// Nichts freizugeben
}
/*****************************************************************************/
/**
Liest alle bentigten Informationen aus einem Slave.
*/
int ec_slave_fetch(ec_slave_t *slave /**< EtherCAT-Slave */)
{
ec_frame_t frame;
// Read base data
ec_frame_init_nprd(&frame, slave->master, slave->station_address,
0x0000, 6);
if (unlikely(ec_frame_send_receive(&frame))) return -1;
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: Slave %i did not respond while reading base"
" data!\n", slave->ring_position);
return -1;
}
Florian Pose
committed
slave->base_type = EC_READ_U8 (frame.data);
slave->base_revision = EC_READ_U8 (frame.data + 1);
slave->base_build = EC_READ_U16(frame.data + 2);
slave->base_fmmu_count = EC_READ_U8 (frame.data + 4);
slave->base_sync_count = EC_READ_U8 (frame.data + 5);
Florian Pose
committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
if (slave->base_fmmu_count > EC_MAX_FMMUS)
slave->base_fmmu_count = EC_MAX_FMMUS;
// Read identification from "Slave Information Interface" (SII)
if (unlikely(ec_slave_sii_read(slave, 0x0008, &slave->sii_vendor_id))) {
printk(KERN_ERR "EtherCAT: Could not read SII vendor id!\n");
return -1;
}
if (unlikely(ec_slave_sii_read(slave, 0x000A, &slave->sii_product_code))) {
printk(KERN_ERR "EtherCAT: Could not read SII product code!\n");
return -1;
}
if (unlikely(ec_slave_sii_read(slave, 0x000C,
&slave->sii_revision_number))) {
printk(KERN_ERR "EtherCAT: Could not read SII revision number!\n");
return -1;
}
if (unlikely(ec_slave_sii_read(slave, 0x000E,
&slave->sii_serial_number))) {
printk(KERN_ERR "EtherCAT: Could not read SII serial number!\n");
return -1;
}
return 0;
}
/*****************************************************************************/
/**
Liest Daten aus dem Slave-Information-Interface
eines EtherCAT-Slaves.
\return 0 bei Erfolg, sonst < 0
*/
int ec_slave_sii_read(ec_slave_t *slave,
/**< EtherCAT-Slave */
unsigned short int offset,
/**< Adresse des zu lesenden SII-Registers */
unsigned int *target
/**< Zeiger auf einen 4 Byte groen Speicher zum Ablegen
der Daten */
)
{
ec_frame_t frame;
unsigned char data[10];
unsigned int tries_left;
// Initiate read operation
Florian Pose
committed
EC_WRITE_U8 (data, 0x00);
EC_WRITE_U8 (data + 1, 0x01);
EC_WRITE_U16(data + 2, offset);
EC_WRITE_U16(data + 4, 0x0000);
Florian Pose
committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x502, 6,
data);
if (unlikely(ec_frame_send_receive(&frame))) return -1;
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: SII-read - Slave %i did not respond!\n",
slave->ring_position);
return -1;
}
// Der Slave legt die Informationen des Slave-Information-Interface
// in das Datenregister und lscht daraufhin ein Busy-Bit. Solange
// den Status auslesen, bis das Bit weg ist.
tries_left = 100;
while (likely(tries_left))
{
udelay(10);
ec_frame_init_nprd(&frame, slave->master, slave->station_address, 0x502,
10);
if (unlikely(ec_frame_send_receive(&frame))) return -1;
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: SII-read status -"
" Slave %i did not respond!\n", slave->ring_position);
return -1;
}
Florian Pose
committed
if (likely((EC_READ_U8(frame.data + 1) & 0x81) == 0)) {
Florian Pose
committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
memcpy(target, frame.data + 6, 4);
break;
}
tries_left--;
}
if (unlikely(!tries_left)) {
printk(KERN_WARNING "EtherCAT: SSI-read. Slave %i timed out!\n",
slave->ring_position);
return -1;
}
return 0;
}
/*****************************************************************************/
/**
Besttigt einen Fehler beim Zustandswechsel.
FIXME Funktioniert noch nicht...
*/
void ec_slave_state_ack(ec_slave_t *slave,
/**< Slave, dessen Zustand gendert werden soll */
uint8_t state
/**< Alter Zustand */
)
{
ec_frame_t frame;
unsigned char data[2];
unsigned int tries_left;
Florian Pose
committed
EC_WRITE_U16(data, state | EC_ACK);
Florian Pose
committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0120,
2, data);
if (unlikely(ec_frame_send_receive(&frame) != 0)) {
printk(KERN_ERR "EtherCAT: Could no acknowledge state %02X - Unable to"
" send!\n", state);
return;
}
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: Could not acknowledge state %02X - Slave"
" %i did not respond!\n", state, slave->ring_position);
return;
}
tries_left = 100;
while (likely(tries_left))
{
udelay(10);
ec_frame_init_nprd(&frame, slave->master, slave->station_address,
0x0130, 2);
if (unlikely(ec_frame_send_receive(&frame) != 0)) {
printk(KERN_ERR "EtherCAT: Could not check state acknowledgement"
" %02X - Unable to send!\n", state);
return;
}
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: Could not check state acknowledgement"
" %02X - Slave %i did not respond!\n", state,
slave->ring_position);
return;
}
Florian Pose
committed
if (unlikely(EC_READ_U8(frame.data) != state)) {
Florian Pose
committed
printk(KERN_ERR "EtherCAT: Could not acknowledge state %02X on"
" slave %i (code %02X)!\n", state, slave->ring_position,
Florian Pose
committed
EC_READ_U8(frame.data));
Florian Pose
committed
return;
}
Florian Pose
committed
if (likely(EC_READ_U8(frame.data) == state)) {
Florian Pose
committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
printk(KERN_INFO "EtherCAT: Acknowleged state %02X on slave %i.\n",
state, slave->ring_position);
return;
}
tries_left--;
}
if (unlikely(!tries_left)) {
printk(KERN_ERR "EtherCAT: Could not check state acknowledgement %02X"
" of slave %i - Timeout while checking!\n", state,
slave->ring_position);
return;
}
}
/*****************************************************************************/
/**
ndert den Zustand eines Slaves.
\return 0 bei Erfolg, sonst < 0
*/
int ec_slave_state_change(ec_slave_t *slave,
/**< Slave, dessen Zustand gendert werden soll */
uint8_t state
/**< Neuer Zustand */
)
{
ec_frame_t frame;
unsigned char data[2];
unsigned int tries_left;
Florian Pose
committed
EC_WRITE_U16(data, state);
Florian Pose
committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0120,
2, data);
if (unlikely(ec_frame_send_receive(&frame) != 0)) {
printk(KERN_ERR "EtherCAT: Could not set state %02X - Unable to"
" send!\n", state);
return -1;
}
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: Could not set state %02X - Slave %i did not"
" respond!\n", state, slave->ring_position);
return -1;
}
tries_left = 100;
while (likely(tries_left))
{
udelay(10);
ec_frame_init_nprd(&frame, slave->master, slave->station_address,
0x0130, 2);
if (unlikely(ec_frame_send_receive(&frame) != 0)) {
printk(KERN_ERR "EtherCAT: Could not check state %02X - Unable to"
" send!\n", state);
return -1;
}
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: Could not check state %02X - Slave %i"
" did not respond!\n", state, slave->ring_position);
return -1;
}
Florian Pose
committed
if (unlikely(EC_READ_U8(frame.data) & 0x10)) { // State change error
Florian Pose
committed
printk(KERN_ERR "EtherCAT: Could not set state %02X - Slave %i"
" refused state change (code %02X)!\n", state,
Florian Pose
committed
slave->ring_position, EC_READ_U8(frame.data));
ec_slave_state_ack(slave, EC_READ_U8(frame.data) & 0x0F);
Florian Pose
committed
return -1;
}
Florian Pose
committed
if (likely(EC_READ_U8(frame.data) == (state & 0x0F))) {
Florian Pose
committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// State change successful
break;
}
tries_left--;
}
if (unlikely(!tries_left)) {
printk(KERN_ERR "EtherCAT: Could not check state %02X of slave %i -"
" Timeout while checking!\n", state,
slave->ring_position);
return -1;
}
return 0;
}
/*****************************************************************************/
/**
Merkt eine FMMU-Konfiguration vor.
Die FMMU wird so konfiguriert, dass sie den gesamten Datenbereich des
entsprechenden Sync-Managers abdeckt. Fr jede Domne werden separate
FMMUs konfiguriert.
Wenn die entsprechende FMMU bereits konfiguriert ist, wird dies als
Erfolg zurckgegeben.
\return 0 bei Erfolg, sonst < 0
*/
int ec_slave_set_fmmu(ec_slave_t *slave, /**< EtherCAT-Slave */
const ec_domain_t *domain, /**< Domne */
const ec_sync_t *sync /**< Sync-Manager */
)
{
unsigned int i;
// FMMU schon vorgemerkt?
for (i = 0; i < slave->fmmu_count; i++)
if (slave->fmmus[i].domain == domain && slave->fmmus[i].sync == sync)
return 0;
if (slave->fmmu_count >= slave->base_fmmu_count) {
printk(KERN_ERR "EtherCAT: Slave %i supports only %i FMMUs.\n",
slave->ring_position, slave->base_fmmu_count);
return -1;
}
slave->fmmus[slave->fmmu_count].domain = domain;
slave->fmmus[slave->fmmu_count].sync = sync;
slave->fmmus[slave->fmmu_count].logical_start_address = 0;
slave->fmmu_count++;
slave->registered = 1;
return 0;
}
/*****************************************************************************/
/**
Gibt alle Informationen ber einen EtherCAT-Slave aus.
*/
void ec_slave_print(const ec_slave_t *slave /**< EtherCAT-Slave */)
{
printk(KERN_INFO "--- EtherCAT slave information ---\n");
if (slave->type) {
printk(KERN_INFO " Vendor \"%s\", Product \"%s\": %s\n",
slave->type->vendor_name, slave->type->product_name,
slave->type->description);
}
else {
printk(KERN_INFO " *** This slave has no type information! ***\n");
}
printk(KERN_INFO " Ring position: %i, Station address: 0x%04X\n",
slave->ring_position, slave->station_address);
printk(KERN_INFO " Base information:\n");
printk(KERN_INFO " Type %u, Revision %i, Build %i\n",
slave->base_type, slave->base_revision, slave->base_build);
printk(KERN_INFO " Supported FMMUs: %i, Sync managers: %i\n",
slave->base_fmmu_count, slave->base_sync_count);
printk(KERN_INFO " Slave information interface:\n");
printk(KERN_INFO " Vendor-ID: 0x%08X, Product code: 0x%08X\n",
slave->sii_vendor_id, slave->sii_product_code);
printk(KERN_INFO " Revision number: 0x%08X, Serial number: 0x%08X\n",
slave->sii_revision_number, slave->sii_serial_number);
Florian Pose
committed
/*****************************************************************************/
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
Gibt die Zhlerstnde der CRC-Fault-Counter aus und setzt diese zurck.
\return 0 bei Erfolg, sonst < 0
*/
int ec_slave_check_crc(ec_slave_t *slave /**< EtherCAT-Slave */)
{
ec_frame_t frame;
uint8_t data[4];
ec_frame_init_nprd(&frame, slave->master, slave->station_address, 0x0300,
4);
if (unlikely(ec_frame_send_receive(&frame))) {
printk(KERN_WARNING "EtherCAT: Reading CRC fault counters failed"
" on slave %i - Could not send command!\n",
slave->ring_position);
return -1;
}
if (unlikely(frame.working_counter != 1)) {
printk(KERN_WARNING "EtherCAT: Reading CRC fault counters -"
" Slave %i did not respond!\n", slave->ring_position);
return -1;
}
// No CRC faults.
Florian Pose
committed
if (!EC_READ_U16(frame.data) && !EC_READ_U16(frame.data + 2)) return 0;
printk(KERN_INFO "EtherCAT: CRC faults on slave %i. A: %i, B: %i\n",
Florian Pose
committed
slave->ring_position, EC_READ_U16(frame.data),
EC_READ_U16(frame.data + 2));
// Reset CRC counters
Florian Pose
committed
EC_WRITE_U16(data, 0x0000);
EC_WRITE_U16(data + 2, 0x0000);
ec_frame_init_npwr(&frame, slave->master, slave->station_address, 0x0300,
4, data);
if (unlikely(ec_frame_send_receive(&frame))) return -1;
if (unlikely(frame.working_counter != 1)) {
printk(KERN_ERR "EtherCAT: Resetting CRC fault counters - Slave"
" %i did not respond!\n", slave->ring_position);
return -1;
}
return 0;
}
/*****************************************************************************/
/* Emacs-Konfiguration
;;; Local Variables: ***
Florian Pose
committed
;;; c-basic-offset:4 ***