darra-ethercat-master 2.7.0

Commercial EtherCAT master protocol stack, real-time kernel driver integration, Windows and Linux support, multi-language SDKs, complex topology and hot-plug support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
139
140
141
142
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
175
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
210
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
248
249
250
251
252
253
254
255
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
290
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
327
328
329
330
331
332
333
334
335
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
428
429
430
431
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

pub mod constants {

    pub const ECT_MBXPROT_AOE: u16 = 0x0001;

    pub const ECT_MBXPROT_EOE: u16 = 0x0002;

    pub const ECT_MBXPROT_COE: u16 = 0x0004;

    pub const ECT_MBXPROT_FOE: u16 = 0x0008;

    pub const ECT_MBXPROT_SOE: u16 = 0x0010;

    pub const ECT_MBXPROT_VOE: u16 = 0x0020;

    pub const ECT_COEDET_SDO: u8 = 0x01;

    pub const ECT_COEDET_SDOINFO: u8 = 0x02;

    pub const ECT_COEDET_PDOASSIGN: u8 = 0x04;

    pub const ECT_COEDET_PDOCONFIG: u8 = 0x08;

    pub const ECT_COEDET_UPLOAD: u8 = 0x10;

    pub const ECT_COEDET_SDOCA: u8 = 0x20;
}

#[derive(Clone, Debug, Default)]
#[repr(C)]
pub struct PdoStats {
    pub read_count: u32,
    pub write_count: u32,
    pub error_count: u32,
    pub total_bytes_read: u32,
    pub total_bytes_written: u32,
    pub last_cycle_time_ns: u64,
    pub min_cycle_time_ns: u64,
    pub max_cycle_time_ns: u64,
    pub avg_cycle_time_ns: u64,
}

#[derive(Clone, Debug)]
#[repr(C, packed)]
pub struct PdoMappingEntry {
    pub index: u16,
    pub sub_index: u8,
    pub bit_length: u8,
    pub bit_offset: u16,
    pub name: [u8; 64],
    pub unit: [u8; 16],
    pub readable: u8,
    pub writable: u8,
}

#[derive(Clone, Debug, Default)]
#[repr(C, packed)]
pub struct CommunicationStatsLocal {
    pub total_cycles: u32,
    pub successful_cycles: u32,
    pub failed_cycles: u32,
    pub timeout_cycles: u32,
    pub average_cycle_time_us: f64,
    pub max_cycle_time_us: f64,
    pub min_cycle_time_us: f64,
}

#[derive(Clone, Debug, Default)]
pub struct SlaveInfo {
    pub vendor_id: u32,
    pub product_code: u32,
    pub revision_number: u32,
    pub serial_number: u32,
    pub device_name: String,
    pub vendor_name: String,
    pub sync_manager_count: u16,
    pub fmmu_count: u16,
    pub supports_coe: bool,
    pub supports_foe: bool,
    pub supports_eoe: bool,
    pub supports_soe: bool,
}

#[derive(Clone, Debug, Default)]
#[repr(C, packed)]
pub struct RealtimeStats {
    pub current_cycle_time_us: f64,
    pub jitter_us: f64,
    pub lost_frames: u32,
    pub corrupted_frames: u32,
    pub bus_utilization: f64,
}

#[derive(Clone, Debug)]
#[repr(C, packed)]
pub struct NetworkInfo {

    pub name: [u8; 128],

    pub desc: [u8; 128],

    pub slave_num: i16,

    pub redundant_slave_num: i16,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C, packed)]
pub struct EcSmt {

    pub start_addr: u16,

    pub sm_length: u16,

    pub sm_flags: u32,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C, packed)]
pub struct EcFmmut {

    pub log_start: u32,

    pub log_length: u16,

    pub log_start_bit: u8,

    pub log_end_bit: u8,

    pub phys_start: u16,

    pub phys_start_bit: u8,

    pub fmmu_type: u8,

    pub fmmu_active: u8,

    pub unused1: u8,

    pub unused2: u16,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveIoDesc {

    pub bits: u16,

    pub bytes: u32,

    pub ptr: usize,

    pub offset: u32,

    pub startbit: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveFsoe {

    pub capable: u8,

    pub connection: usize,

    pub sm_context: usize,

    pub connection_id: u16,

    pub safety_address: u16,

    pub safe_input_size: u16,

    pub safe_output_size: u16,

    pub pdo_input_offset: u32,

    pub pdo_output_offset: u32,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveMailbox {

    pub length: u16,

    pub write_offset: u16,

    pub read_length: u16,

    pub read_offset: u16,

    pub supported_proto: u16,

    pub cnt: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveEepromConfig {

    pub _reserved_sii: u16,

    pub read_8byte: u8,

    pub pdi: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveHandler {

    pub state: i32,

    pub rmp_state: i32,

    pub instate_ex: u16,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveIdentity {

    pub vendor_id: u32,

    pub product_id: u32,

    pub revision: u32,

    pub serial: u32,

    pub itype: u16,

    pub dtype: u16,
}

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct SlaveMetadata {

    pub identity: SlaveIdentity,

    pub group_name: [u8; 41],

    pub device_name: [u8; 41],

    pub sm_count: u16,
}

impl Default for SlaveMetadata {
    fn default() -> Self {
        Self {
            identity: SlaveIdentity::default(),
            group_name: [0; 41],
            device_name: [0; 41],
            sm_count: 0,
        }
    }
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveCapabilities {

    pub is_optional: u8,

    pub supports_frame_repeat: u8,

    pub mailbox_side: u8,

    pub coe_details: u8,

    pub foe_details: u8,

    pub eoe_details: u8,

    pub soe_details: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveRuntime {

    pub ebus_current: i16,

    pub block_lrw: u8,

    pub group: u8,

    pub is_lost: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveSmFmmu {

    pub sm_type: [u8; 8],

    pub sm_app_length: [u16; 8],

    pub fmmu_func: [u8; 4],

    pub fmmu_unused: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveProtoMbx {

    pub in_ptr: usize,

    pub in_full: u8,

    pub overrun: i32,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlavePdoConfig {

    pub assignment_enabled: u8,

    pub configuration_enabled: u8,

    pub config_initialized: u8,

    pub supports_complete_access: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveTopology {

    pub has_dc: u8,

    pub phy_type: u8,

    pub link_count: u8,

    pub active_ports: u8,

    pub consumed_ports: u8,

    pub parent: u16,

    pub parent_port: u8,

    pub entry_port: u8,
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SlaveDc {

    pub recvtime: [i32; 4],

    pub propagation_delay: i32,

    pub next: u16,

    pub prev: u16,

    pub cycle0: i32,

    pub cycle1: i32,

    pub shift: i32,

    pub active: u16,
}

#[derive(Clone, Debug)]
#[repr(C)]
pub struct EcSlave {

    pub state: u16,

    pub al_status_code: u16,

    pub config_addr: u16,

    pub alias_addr: u16,

    pub fsoe: SlaveFsoe,

    pub output: SlaveIoDesc,

    pub input: SlaveIoDesc,

    pub sm: [EcSmt; 8],

    pub fmmu: [EcFmmut; 4],

    pub sm_fmmu: SlaveSmFmmu,

    pub mbx: SlaveMailbox,

    pub topo: SlaveTopology,

    pub dc: SlaveDc,

    pub eeprom_config: SlaveEepromConfig,

    pub runtime: SlaveRuntime,

    pub po2so_config: usize,

    pub handler: SlaveHandler,

    pub coe: SlaveProtoMbx,

    pub soe: SlaveProtoMbx,

    pub foe: SlaveProtoMbx,

    pub eoe: SlaveProtoMbx,

    pub voe: SlaveProtoMbx,

    pub aoe: SlaveProtoMbx,

    pub mbx_status: usize,

    pub metadata: SlaveMetadata,

    pub pdo_config: SlavePdoConfig,

    pub capabilities: SlaveCapabilities,
}

#[derive(Clone, Debug)]
#[repr(C)]
pub struct EcState {

    pub state: u16,

    pub al_status_code: u16,

    pub slave_count: i32,

    pub loop_cycle: u32,

    pub dc_cycle: u32,

    pub dc_time: i64,

    pub iomap: [u8; 65536],

    pub iomap_mutex: usize,

    pub iomap_buffer: [u8; 65536],

    pub buffer_version: u32,

    pub buffer_dirty: u8,

    pub mutex_protection: u8,

    pub pdo_cpu_affinity: i8,

    pub dc_auto_shift_enabled: u8,

    pub use_udp: u8,

    pub adaptive_timeout_enabled: u8,

    pub overlapping_groups: u8,

    pub packed_mode: u8,

    pub frame_high_priority: u8,

    pub vlan_id: u16,

    pub vlan_priority: u8,

    pub timeout_init_to_preop: u32,

    pub timeout_preop_to_safeop: u32,

    pub timeout_safeop_to_op: u32,

    pub wd_pd_timeout_ms: u16,

    pub wd_pdi_timeout_ms: u16,

    pub filter_threshold: u32,

    pub frame_repeat_count: u8,

    pub active_group_count: u8,

    pub _group_pad: [u8; 2],

    pub group_config_raw: [u8; 160],
}

#[derive(Clone, Debug)]
#[repr(C)]
pub struct InternalDiagnostics {

    pub cache_cnt: u32,

    pub rt_cnt: u32,

    pub cycle_count: u32,

    pub error_cache_cnt: u32,

    pub error_cnt: u32,

    pub wkc: u16,

    pub expected_wkc: u16,

    pub cycle_time_span: u32,

    pub frame_errors: u32,

    pub lost_frames: u32,

    pub checksum_errors: u32,

    pub timeout_frames: u32,

    pub rx_error_count: [u32; 512],

    pub tx_error_count: [u32; 512],

    pub lost_link_count: [u16; 512],

    pub invalid_frame_count: [u16; 512],

    pub working_counter_errors: u16,

    pub consecutive_wkc_errors: u16,

    pub total_wkc_mismatches: u32,

    pub cycle_start_time: u64,

    pub cycle_time_accumulator: f64,

    pub last_snapshot_time: u64,

    pub snapshot_cycle_count: u32,

    pub last_cycle_time_us: f64,

    pub jitter_accumulator: f64,

    pub max_jitter_in_second: f64,

    pub jitter_sample_count: u32,

}

#[derive(Clone, Debug)]
pub struct FoEOptionsLocal {

    pub enable_crc: bool,

    pub strict_mode: bool,

    pub auto_append_crc: bool,

    pub expected_crc: u32,
}

impl Default for FoEOptionsLocal {
    fn default() -> Self {
        Self {
            enable_crc: false,
            strict_mode: true,
            auto_append_crc: true,
            expected_crc: 0,
        }
    }
}

pub struct EcGroupConfigHelper;

impl EcGroupConfigHelper {

    const GROUP_CONFIG_SIZE: usize = 20;

    pub fn get_group_enabled(raw: &[u8], group: u8) -> bool {
        if group >= 8 { return false; }
        raw[group as usize * Self::GROUP_CONFIG_SIZE] != 0
    }

    pub fn set_group_enabled(raw: &mut [u8], group: u8, enabled: bool) {
        if group >= 8 { return; }
        raw[group as usize * Self::GROUP_CONFIG_SIZE] = if enabled { 1 } else { 0 };
    }

    pub fn get_group_cycle_divider(raw: &[u8], group: u8) -> u8 {
        if group >= 8 { return 0; }
        raw[group as usize * Self::GROUP_CONFIG_SIZE + 1]
    }

    pub fn set_group_cycle_divider(raw: &mut [u8], group: u8, divider: u8) {
        if group >= 8 { return; }
        raw[group as usize * Self::GROUP_CONFIG_SIZE + 1] = divider;
    }

    pub fn get_group_expected_wkc(raw: &[u8], group: u8) -> u16 {
        if group >= 8 { return 0; }
        let off = group as usize * Self::GROUP_CONFIG_SIZE + 2;
        u16::from_le_bytes([raw[off], raw[off + 1]])
    }

    pub fn get_group_slave_count(raw: &[u8], group: u8) -> u16 {
        if group >= 8 { return 0; }
        let off = group as usize * Self::GROUP_CONFIG_SIZE + 4;
        u16::from_le_bytes([raw[off], raw[off + 1]])
    }

    pub fn get_group_frame_repeat_eligible(raw: &[u8], group: u8) -> bool {
        if group >= 8 { return false; }
        raw[group as usize * Self::GROUP_CONFIG_SIZE + 6] != 0
    }

    pub fn set_group_frame_repeat_eligible(raw: &mut [u8], group: u8, eligible: bool) {
        if group >= 8 { return; }
        raw[group as usize * Self::GROUP_CONFIG_SIZE + 6] = if eligible { 1 } else { 0 };
    }

    pub fn get_group_actual_wkc(raw: &[u8], group: u8) -> u16 {
        if group >= 8 { return 0; }
        let off = group as usize * Self::GROUP_CONFIG_SIZE + 8;
        u16::from_le_bytes([raw[off], raw[off + 1]])
    }

    pub fn get_group_consecutive_miss(raw: &[u8], group: u8) -> u16 {
        if group >= 8 { return 0; }
        let off = group as usize * Self::GROUP_CONFIG_SIZE + 10;
        u16::from_le_bytes([raw[off], raw[off + 1]])
    }

    pub fn get_group_total_frames(raw: &[u8], group: u8) -> u32 {
        if group >= 8 { return 0; }
        let off = group as usize * Self::GROUP_CONFIG_SIZE + 12;
        u32::from_le_bytes([raw[off], raw[off + 1], raw[off + 2], raw[off + 3]])
    }

    pub fn get_group_mismatch_frames(raw: &[u8], group: u8) -> u32 {
        if group >= 8 { return 0; }
        let off = group as usize * Self::GROUP_CONFIG_SIZE + 16;
        u32::from_le_bytes([raw[off], raw[off + 1], raw[off + 2], raw[off + 3]])
    }
}