40 #include "rtai_sched.h" |
40 #include "rtai_sched.h" |
41 #include "rtai_sem.h" |
41 #include "rtai_sem.h" |
42 |
42 |
43 // EtherCAT |
43 // EtherCAT |
44 #include "../../include/ecrt.h" |
44 #include "../../include/ecrt.h" |
|
45 #include "../../include/ecdb.h" |
45 |
46 |
46 /*****************************************************************************/ |
47 /*****************************************************************************/ |
47 |
48 |
48 MODULE_LICENSE("GPL"); |
49 MODULE_LICENSE("GPL"); |
49 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>"); |
50 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>"); |
50 MODULE_DESCRIPTION("EtherCAT RTAI sample module"); |
51 MODULE_DESCRIPTION("EtherCAT RTAI sample module"); |
51 |
52 |
52 /*****************************************************************************/ |
53 /*****************************************************************************/ |
53 |
54 |
54 // comment this for synchronous IO |
|
55 #define ASYNC |
|
56 |
|
57 // RTAI task frequency in Hz |
55 // RTAI task frequency in Hz |
58 #define FREQUENCY 10000 |
56 #define FREQUENCY 10000 |
59 |
|
60 #define TIMERTICKS (1000000000 / FREQUENCY) |
57 #define TIMERTICKS (1000000000 / FREQUENCY) |
61 |
58 |
62 /*****************************************************************************/ |
59 /*****************************************************************************/ |
63 |
60 |
64 // RTAI |
61 // RTAI |
65 RT_TASK task; |
62 RT_TASK task; |
66 SEM master_sem; |
63 SEM master_sem; |
67 cycles_t t_last_start = 0; |
64 cycles_t t_last_start = 0, t_critical; |
68 cycles_t t_critical; |
|
69 |
65 |
70 // EtherCAT |
66 // EtherCAT |
71 ec_master_t *master = NULL; |
67 ec_master_t *master = NULL; |
72 ec_domain_t *domain1 = NULL; |
68 ec_domain_t *domain1 = NULL; |
73 |
69 |
74 // data fields |
70 // data fields |
75 void *r_ssi_input; |
71 void *r_ana_out; |
76 |
72 |
77 // channels |
73 // channels |
78 uint32_t k_pos; |
74 uint32_t k_pos; |
79 |
75 |
80 ec_field_init_t domain1_fields[] = { |
76 ec_pdo_reg_t domain1_pdos[] = { |
81 {&r_ssi_input, "3", "Beckhoff", "EL5001", "InputValue", 0}, |
77 {"2", Beckhoff_EL4132_Output1, &r_ana_out}, |
82 {NULL, "2", "Beckhoff", "EL4132", "OutputValue", 0}, |
|
83 {} |
78 {} |
84 }; |
79 }; |
85 |
80 |
86 /*****************************************************************************/ |
81 /*****************************************************************************/ |
87 |
82 |
90 while (1) |
85 while (1) |
91 { |
86 { |
92 t_last_start = get_cycles(); |
87 t_last_start = get_cycles(); |
93 rt_sem_wait(&master_sem); |
88 rt_sem_wait(&master_sem); |
94 |
89 |
95 #ifdef ASYNC |
90 ecrt_master_receive(master); |
96 // receive |
|
97 ecrt_master_async_receive(master); |
|
98 ecrt_domain_process(domain1); |
91 ecrt_domain_process(domain1); |
99 #else |
92 |
100 // send and receive |
93 // process data |
101 ecrt_domain_queue(domain1); |
94 //k_pos = EC_READ_U32(r_ssi_input); |
|
95 |
102 ecrt_master_run(master); |
96 ecrt_master_run(master); |
103 ecrt_master_sync_io(master); |
97 ecrt_master_send(master); |
104 ecrt_domain_process(domain1); |
|
105 #endif |
|
106 |
|
107 // process data |
|
108 k_pos = EC_READ_U32(r_ssi_input); |
|
109 |
|
110 #ifdef ASYNC |
|
111 // send |
|
112 ecrt_domain_queue(domain1); |
|
113 ecrt_master_run(master); |
|
114 ecrt_master_async_send(master); |
|
115 #endif |
|
116 |
98 |
117 rt_sem_signal(&master_sem); |
99 rt_sem_signal(&master_sem); |
118 rt_task_wait_period(); |
100 rt_task_wait_period(); |
119 } |
101 } |
120 } |
102 } |
156 } |
138 } |
157 |
139 |
158 ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
140 ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
159 |
141 |
160 printk(KERN_INFO "Registering domain...\n"); |
142 printk(KERN_INFO "Registering domain...\n"); |
161 if (!(domain1 = ecrt_master_create_domain(master))) |
143 if (!(domain1 = ecrt_master_create_domain(master))) { |
162 { |
|
163 printk(KERN_ERR "Domain creation failed!\n"); |
144 printk(KERN_ERR "Domain creation failed!\n"); |
164 goto out_release_master; |
145 goto out_release_master; |
165 } |
146 } |
166 |
147 |
167 printk(KERN_INFO "Registering domain fields...\n"); |
148 printk(KERN_INFO "Registering PDOs...\n"); |
168 if (ecrt_domain_register_field_list(domain1, domain1_fields)) { |
149 if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) { |
169 printk(KERN_ERR "Field registration failed!\n"); |
150 printk(KERN_ERR "PDO registration failed!\n"); |
170 goto out_release_master; |
151 goto out_release_master; |
171 } |
152 } |
172 |
153 |
173 printk(KERN_INFO "Activating master...\n"); |
154 printk(KERN_INFO "Activating master...\n"); |
174 if (ecrt_master_activate(master)) { |
155 if (ecrt_master_activate(master)) { |
175 printk(KERN_ERR "Failed to activate master!\n"); |
156 printk(KERN_ERR "Failed to activate master!\n"); |
176 goto out_release_master; |
157 goto out_release_master; |
177 } |
158 } |
178 |
159 |
179 #if 0 |
160 ecrt_master_prepare(master); |
180 if (ecrt_master_fetch_sdo_lists(master)) { |
|
181 printk(KERN_ERR "Failed to fetch SDO lists!\n"); |
|
182 goto out_deactivate; |
|
183 } |
|
184 ecrt_master_print(master, 2); |
|
185 #else |
|
186 ecrt_master_print(master, 0); |
|
187 #endif |
|
188 |
|
189 #if 0 |
|
190 if (!(slave = ecrt_master_get_slave(master, "5"))) { |
|
191 printk(KERN_ERR "Failed to get slave 5!\n"); |
|
192 goto out_deactivate; |
|
193 } |
|
194 |
|
195 if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || |
|
196 ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || |
|
197 ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || |
|
198 ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || |
|
199 ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || |
|
200 ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || |
|
201 ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || |
|
202 ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || |
|
203 ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { |
|
204 printk(KERN_ERR "Failed to configure SSI slave!\n"); |
|
205 goto out_deactivate; |
|
206 } |
|
207 #endif |
|
208 |
|
209 #if 0 |
|
210 printk(KERN_INFO "Writing alias...\n"); |
|
211 if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) { |
|
212 printk(KERN_ERR "Failed to write alias!\n"); |
|
213 goto out_deactivate; |
|
214 } |
|
215 #endif |
|
216 |
|
217 #ifdef ASYNC |
|
218 // send once and wait... |
|
219 ecrt_master_prepare_async_io(master); |
|
220 #endif |
|
221 |
|
222 if (ecrt_master_start_eoe(master)) { |
|
223 printk(KERN_ERR "Failed to start EoE processing!\n"); |
|
224 goto out_deactivate; |
|
225 } |
|
226 |
161 |
227 printk("Starting cyclic sample thread...\n"); |
162 printk("Starting cyclic sample thread...\n"); |
228 requested_ticks = nano2count(TIMERTICKS); |
163 requested_ticks = nano2count(TIMERTICKS); |
229 tick_period = start_rt_timer(requested_ticks); |
164 tick_period = start_rt_timer(requested_ticks); |
230 printk(KERN_INFO "RT timer started with %i/%i ticks.\n", |
165 printk(KERN_INFO "RT timer started with %i/%i ticks.\n", |