1 //--------------------------------------------------------------- |
|
2 // |
|
3 // e c _ m a s t e r . h |
|
4 // |
|
5 // $LastChangedDate$ |
|
6 // $Author$ |
|
7 // |
|
8 //--------------------------------------------------------------- |
|
9 |
|
10 #include <pcap.h> |
|
11 #include <libnet.h> |
|
12 #include <pthread.h> |
|
13 |
|
14 #include "ec_slave.h" |
|
15 #include "ec_command.h" |
|
16 |
|
17 //--------------------------------------------------------------- |
|
18 |
|
19 typedef struct |
|
20 { |
|
21 EtherCAT_slave_t *slaves; // Slaves array |
|
22 unsigned int slave_count; // Number of slaves |
|
23 |
|
24 EtherCAT_command_t *first_command; // List of commands |
|
25 |
|
26 pcap_t *pcap_handle; // Handle for libpcap |
|
27 libnet_t *net_handle; // Handle for libnet |
|
28 |
|
29 unsigned char command_index; // Current command index |
|
30 |
|
31 unsigned char *process_data; |
|
32 unsigned int process_data_length; |
|
33 void (*pre_cb)(unsigned char *); |
|
34 void (*post_cb)(unsigned char *); |
|
35 pthread_t thread; |
|
36 int thread_continue; |
|
37 unsigned int cycle_time; |
|
38 |
|
39 double bus_time; |
|
40 |
|
41 double last_jitter; |
|
42 double last_cycle_time; |
|
43 double last_cycle_work_time; |
|
44 double last_cycle_busy_rate; |
|
45 } |
|
46 EtherCAT_master_t; |
|
47 |
|
48 //--------------------------------------------------------------- |
|
49 |
|
50 // Master creation and deletion |
|
51 int EtherCAT_master_init(EtherCAT_master_t *, char *); |
|
52 void EtherCAT_master_clear(EtherCAT_master_t *); |
|
53 |
|
54 // Checking for slaves |
|
55 int EtherCAT_check_slaves(EtherCAT_master_t *, EtherCAT_slave_t *, unsigned int); |
|
56 void EtherCAT_clear_slaves(EtherCAT_master_t *); |
|
57 |
|
58 // Slave information interface |
|
59 int EtherCAT_read_slave_information(EtherCAT_master_t *, |
|
60 unsigned short int, |
|
61 unsigned short int, |
|
62 unsigned int *); |
|
63 |
|
64 // EtherCAT commands |
|
65 EtherCAT_command_t *EtherCAT_read(EtherCAT_master_t *, |
|
66 unsigned short, |
|
67 unsigned short, |
|
68 unsigned int); |
|
69 EtherCAT_command_t *EtherCAT_write(EtherCAT_master_t *, |
|
70 unsigned short, |
|
71 unsigned short, |
|
72 unsigned int, |
|
73 const unsigned char *); |
|
74 EtherCAT_command_t *EtherCAT_position_read(EtherCAT_master_t *, |
|
75 short, |
|
76 unsigned short, |
|
77 unsigned int); |
|
78 EtherCAT_command_t *EtherCAT_position_write(EtherCAT_master_t *, |
|
79 short, |
|
80 unsigned short, |
|
81 unsigned int, |
|
82 const unsigned char *); |
|
83 EtherCAT_command_t *EtherCAT_broadcast_read(EtherCAT_master_t *, |
|
84 unsigned short, |
|
85 unsigned int); |
|
86 EtherCAT_command_t *EtherCAT_broadcast_write(EtherCAT_master_t *, |
|
87 unsigned short, |
|
88 unsigned int, |
|
89 const unsigned char *); |
|
90 EtherCAT_command_t *EtherCAT_logical_read_write(EtherCAT_master_t *, |
|
91 unsigned int, |
|
92 unsigned int, |
|
93 unsigned char *); |
|
94 |
|
95 void EtherCAT_remove_command(EtherCAT_master_t *, EtherCAT_command_t *); |
|
96 |
|
97 // Slave states |
|
98 int EtherCAT_state_change(EtherCAT_master_t *, EtherCAT_slave_t *, unsigned char); |
|
99 int EtherCAT_broadcast_state_change(EtherCAT_master_t *, unsigned char); |
|
100 |
|
101 int EtherCAT_activate_slave(EtherCAT_master_t *, EtherCAT_slave_t *); |
|
102 int EtherCAT_deactivate_slave(EtherCAT_master_t *, EtherCAT_slave_t *); |
|
103 |
|
104 // Sending and receiving |
|
105 int EtherCAT_send_receive(EtherCAT_master_t *); |
|
106 |
|
107 int EtherCAT_start(EtherCAT_master_t *, |
|
108 unsigned int, |
|
109 void (*)(unsigned char *), |
|
110 void (*)(unsigned char *), |
|
111 unsigned int); |
|
112 int EtherCAT_stop(EtherCAT_master_t *); |
|
113 |
|
114 // Private functions |
|
115 void add_command(EtherCAT_master_t *, EtherCAT_command_t *); |
|
116 void set_byte(unsigned char *, unsigned int, unsigned char); |
|
117 void set_word(unsigned char *, unsigned int, unsigned int); |
|
118 void *thread_function(void *); |
|
119 |
|
120 //--------------------------------------------------------------- |
|