|
1 /****************************************************************************** |
|
2 * |
|
3 * $Id$ |
|
4 * |
|
5 * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH |
|
6 * |
|
7 * This file is part of the IgH EtherCAT Master. |
|
8 * |
|
9 * The IgH EtherCAT Master is free software; you can redistribute it and/or |
|
10 * modify it under the terms of the GNU General Public License version 2, as |
|
11 * published by the Free Software Foundation. |
|
12 * |
|
13 * The IgH EtherCAT Master is distributed in the hope that it will be useful, |
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
16 * Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License along |
|
19 * with the IgH EtherCAT Master; if not, write to the Free Software |
|
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
21 * |
|
22 * --- |
|
23 * |
|
24 * The license mentioned above concerns the source code only. Using the |
|
25 * EtherCAT technology and brand is only permitted in compliance with the |
|
26 * industrial property and similar rights of Beckhoff Automation GmbH. |
|
27 * |
|
28 *****************************************************************************/ |
|
29 |
|
30 #include <linux/module.h> |
|
31 #include <linux/err.h> |
|
32 |
|
33 #include "../../include/ecrt.h" // EtherCAT realtime interface |
|
34 #include "../../include/ectty.h" // EtherCAT TTY interface |
|
35 |
|
36 /*****************************************************************************/ |
|
37 |
|
38 // Optional features |
|
39 #define PFX "ec_tty_example: " |
|
40 |
|
41 /*****************************************************************************/ |
|
42 |
|
43 #define VendorIdBeckhoff 0x00000002 |
|
44 #define ProductCodeBeckhoffEL6002 0x17723052 |
|
45 #define Beckhoff_EL6002 VendorIdBeckhoff, ProductCodeBeckhoffEL6002 |
|
46 |
|
47 typedef enum { |
|
48 SER_REQUEST_INIT, |
|
49 SER_WAIT_FOR_INIT_RESPONSE, |
|
50 SER_READY |
|
51 } serial_state_t; |
|
52 |
|
53 typedef struct { |
|
54 struct list_head list; |
|
55 |
|
56 ec_tty_t *tty; |
|
57 ec_slave_config_t *sc; |
|
58 |
|
59 size_t max_tx_data_size; |
|
60 size_t max_rx_data_size; |
|
61 |
|
62 u8 *tx_data; |
|
63 u8 tx_data_size; |
|
64 |
|
65 serial_state_t state; |
|
66 |
|
67 u8 tx_request_toggle; |
|
68 u8 tx_accepted_toggle; |
|
69 |
|
70 u8 rx_request_toggle; |
|
71 u8 rx_accepted_toggle; |
|
72 |
|
73 u16 control; |
|
74 |
|
75 u32 off_ctrl; |
|
76 u32 off_tx; |
|
77 u32 off_status; |
|
78 u32 off_rx; |
|
79 } el6002_t; |
|
80 |
|
81 LIST_HEAD(handlers); |
|
82 |
|
83 /*****************************************************************************/ |
|
84 |
|
85 /* Beckhoff EL6002 |
|
86 * Vendor ID: 0x00000002 |
|
87 * Product code: 0x17723052 |
|
88 * Revision number: 0x00100000 |
|
89 */ |
|
90 |
|
91 ec_pdo_entry_info_t el6002_pdo_entries[] = { |
|
92 {0x7001, 0x01, 16}, /* Ctrl */ |
|
93 {0x7000, 0x11, 8}, /* Data Out 0 */ |
|
94 {0x7000, 0x12, 8}, /* Data Out 1 */ |
|
95 {0x7000, 0x13, 8}, /* Data Out 2 */ |
|
96 {0x7000, 0x14, 8}, /* Data Out 3 */ |
|
97 {0x7000, 0x15, 8}, /* Data Out 4 */ |
|
98 {0x7000, 0x16, 8}, /* Data Out 5 */ |
|
99 {0x7000, 0x17, 8}, /* Data Out 6 */ |
|
100 {0x7000, 0x18, 8}, /* Data Out 7 */ |
|
101 {0x7000, 0x19, 8}, /* Data Out 8 */ |
|
102 {0x7000, 0x1a, 8}, /* Data Out 9 */ |
|
103 {0x7000, 0x1b, 8}, /* Data Out 10 */ |
|
104 {0x7000, 0x1c, 8}, /* Data Out 11 */ |
|
105 {0x7000, 0x1d, 8}, /* Data Out 12 */ |
|
106 {0x7000, 0x1e, 8}, /* Data Out 13 */ |
|
107 {0x7000, 0x1f, 8}, /* Data Out 14 */ |
|
108 {0x7000, 0x20, 8}, /* Data Out 15 */ |
|
109 {0x7000, 0x21, 8}, /* Data Out 16 */ |
|
110 {0x7000, 0x22, 8}, /* Data Out 17 */ |
|
111 {0x7000, 0x23, 8}, /* Data Out 18 */ |
|
112 {0x7000, 0x24, 8}, /* Data Out 19 */ |
|
113 {0x7000, 0x25, 8}, /* Data Out 20 */ |
|
114 {0x7000, 0x26, 8}, /* Data Out 21 */ |
|
115 {0x7011, 0x01, 16}, /* Ctrl */ |
|
116 {0x7010, 0x11, 8}, /* Data Out 0 */ |
|
117 {0x7010, 0x12, 8}, /* Data Out 1 */ |
|
118 {0x7010, 0x13, 8}, /* Data Out 2 */ |
|
119 {0x7010, 0x14, 8}, /* Data Out 3 */ |
|
120 {0x7010, 0x15, 8}, /* Data Out 4 */ |
|
121 {0x7010, 0x16, 8}, /* Data Out 5 */ |
|
122 {0x7010, 0x17, 8}, /* Data Out 6 */ |
|
123 {0x7010, 0x18, 8}, /* Data Out 7 */ |
|
124 {0x7010, 0x19, 8}, /* Data Out 8 */ |
|
125 {0x7010, 0x1a, 8}, /* Data Out 9 */ |
|
126 {0x7010, 0x1b, 8}, /* Data Out 10 */ |
|
127 {0x7010, 0x1c, 8}, /* Data Out 11 */ |
|
128 {0x7010, 0x1d, 8}, /* Data Out 12 */ |
|
129 {0x7010, 0x1e, 8}, /* Data Out 13 */ |
|
130 {0x7010, 0x1f, 8}, /* Data Out 14 */ |
|
131 {0x7010, 0x20, 8}, /* Data Out 15 */ |
|
132 {0x7010, 0x21, 8}, /* Data Out 16 */ |
|
133 {0x7010, 0x22, 8}, /* Data Out 17 */ |
|
134 {0x7010, 0x23, 8}, /* Data Out 18 */ |
|
135 {0x7010, 0x24, 8}, /* Data Out 19 */ |
|
136 {0x7010, 0x25, 8}, /* Data Out 20 */ |
|
137 {0x7010, 0x26, 8}, /* Data Out 21 */ |
|
138 {0x6001, 0x01, 16}, /* Status */ |
|
139 {0x6000, 0x11, 8}, /* Data In 0 */ |
|
140 {0x6000, 0x12, 8}, /* Data In 1 */ |
|
141 {0x6000, 0x13, 8}, /* Data In 2 */ |
|
142 {0x6000, 0x14, 8}, /* Data In 3 */ |
|
143 {0x6000, 0x15, 8}, /* Data In 4 */ |
|
144 {0x6000, 0x16, 8}, /* Data In 5 */ |
|
145 {0x6000, 0x17, 8}, /* Data In 6 */ |
|
146 {0x6000, 0x18, 8}, /* Data In 7 */ |
|
147 {0x6000, 0x19, 8}, /* Data In 8 */ |
|
148 {0x6000, 0x1a, 8}, /* Data In 9 */ |
|
149 {0x6000, 0x1b, 8}, /* Data In 10 */ |
|
150 {0x6000, 0x1c, 8}, /* Data In 11 */ |
|
151 {0x6000, 0x1d, 8}, /* Data In 12 */ |
|
152 {0x6000, 0x1e, 8}, /* Data In 13 */ |
|
153 {0x6000, 0x1f, 8}, /* Data In 14 */ |
|
154 {0x6000, 0x20, 8}, /* Data In 15 */ |
|
155 {0x6000, 0x21, 8}, /* Data In 16 */ |
|
156 {0x6000, 0x22, 8}, /* Data In 17 */ |
|
157 {0x6000, 0x23, 8}, /* Data In 18 */ |
|
158 {0x6000, 0x24, 8}, /* Data In 19 */ |
|
159 {0x6000, 0x25, 8}, /* Data In 20 */ |
|
160 {0x6000, 0x26, 8}, /* Data In 21 */ |
|
161 {0x6011, 0x01, 16}, /* Status */ |
|
162 {0x6010, 0x11, 8}, /* Data In 0 */ |
|
163 {0x6010, 0x12, 8}, /* Data In 1 */ |
|
164 {0x6010, 0x13, 8}, /* Data In 2 */ |
|
165 {0x6010, 0x14, 8}, /* Data In 3 */ |
|
166 {0x6010, 0x15, 8}, /* Data In 4 */ |
|
167 {0x6010, 0x16, 8}, /* Data In 5 */ |
|
168 {0x6010, 0x17, 8}, /* Data In 6 */ |
|
169 {0x6010, 0x18, 8}, /* Data In 7 */ |
|
170 {0x6010, 0x19, 8}, /* Data In 8 */ |
|
171 {0x6010, 0x1a, 8}, /* Data In 9 */ |
|
172 {0x6010, 0x1b, 8}, /* Data In 10 */ |
|
173 {0x6010, 0x1c, 8}, /* Data In 11 */ |
|
174 {0x6010, 0x1d, 8}, /* Data In 12 */ |
|
175 {0x6010, 0x1e, 8}, /* Data In 13 */ |
|
176 {0x6010, 0x1f, 8}, /* Data In 14 */ |
|
177 {0x6010, 0x20, 8}, /* Data In 15 */ |
|
178 {0x6010, 0x21, 8}, /* Data In 16 */ |
|
179 {0x6010, 0x22, 8}, /* Data In 17 */ |
|
180 {0x6010, 0x23, 8}, /* Data In 18 */ |
|
181 {0x6010, 0x24, 8}, /* Data In 19 */ |
|
182 {0x6010, 0x25, 8}, /* Data In 20 */ |
|
183 {0x6010, 0x26, 8}, /* Data In 21 */ |
|
184 }; |
|
185 |
|
186 ec_pdo_info_t el6002_pdos[] = { |
|
187 {0x1604, 23, el6002_pdo_entries + 0}, /* COM RxPDO-Map Outputs Ch.1 */ |
|
188 {0x1605, 23, el6002_pdo_entries + 23}, /* COM RxPDO-Map Outputs Ch.2 */ |
|
189 {0x1a04, 23, el6002_pdo_entries + 46}, /* COM TxPDO-Map Inputs Ch.1 */ |
|
190 {0x1a05, 23, el6002_pdo_entries + 69}, /* COM TxPDO-Map Inputs Ch.2 */ |
|
191 }; |
|
192 |
|
193 ec_sync_info_t el6002_syncs[] = { |
|
194 {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE}, |
|
195 {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE}, |
|
196 {2, EC_DIR_OUTPUT, 2, el6002_pdos + 0, EC_WD_DISABLE}, |
|
197 {3, EC_DIR_INPUT, 2, el6002_pdos + 2, EC_WD_DISABLE}, |
|
198 {0xff} |
|
199 }; |
|
200 |
|
201 /****************************************************************************/ |
|
202 |
|
203 int el6002_init(el6002_t *ser, ec_master_t *master, u16 position, |
|
204 ec_domain_t *domain) |
|
205 { |
|
206 int ret = 0; |
|
207 |
|
208 ser->tty = ectty_create(); |
|
209 if (IS_ERR(ser->tty)) { |
|
210 printk(KERN_ERR PFX "Failed to create tty.\n"); |
|
211 ret = PTR_ERR(ser->tty); |
|
212 goto out_return; |
|
213 } |
|
214 |
|
215 ser->sc = NULL; |
|
216 ser->max_tx_data_size = 22; |
|
217 ser->max_rx_data_size = 22; |
|
218 ser->tx_data = NULL; |
|
219 ser->tx_data_size = 0; |
|
220 ser->state = SER_REQUEST_INIT; |
|
221 ser->tx_request_toggle = 0; |
|
222 ser->rx_accepted_toggle = 0; |
|
223 ser->control = 0x0000; |
|
224 ser->off_ctrl = 0; |
|
225 ser->off_tx = 0; |
|
226 ser->off_status = 0; |
|
227 ser->off_rx = 0; |
|
228 |
|
229 if (!(ser->sc = ecrt_master_slave_config( |
|
230 master, 0, position, Beckhoff_EL6002))) { |
|
231 printk(KERN_ERR PFX "Failed to create slave configuration.\n"); |
|
232 ret = -EBUSY; |
|
233 goto out_free_tty; |
|
234 } |
|
235 |
|
236 if (ecrt_slave_config_pdos(ser->sc, EC_END, el6002_syncs)) { |
|
237 printk(KERN_ERR PFX "Failed to configure PDOs.\n"); |
|
238 ret = -ENOMEM; |
|
239 goto out_free_tty; |
|
240 } |
|
241 |
|
242 ret = ecrt_slave_config_reg_pdo_entry( |
|
243 ser->sc, 0x7001, 0x01, domain, NULL); |
|
244 if (ret < 0) { |
|
245 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
246 goto out_free_tty; |
|
247 } |
|
248 ser->off_ctrl = ret; |
|
249 |
|
250 ret = ecrt_slave_config_reg_pdo_entry( |
|
251 ser->sc, 0x7000, 0x11, domain, NULL); |
|
252 if (ret < 0) { |
|
253 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
254 goto out_free_tty; |
|
255 } |
|
256 ser->off_tx = ret; |
|
257 |
|
258 ret = ecrt_slave_config_reg_pdo_entry( |
|
259 ser->sc, 0x6001, 0x01, domain, NULL); |
|
260 if (ret < 0) { |
|
261 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
262 goto out_free_tty; |
|
263 } |
|
264 ser->off_status = ret; |
|
265 |
|
266 ret = ecrt_slave_config_reg_pdo_entry( |
|
267 ser->sc, 0x6000, 0x11, domain, NULL); |
|
268 if (ret < 0) { |
|
269 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
270 goto out_free_tty; |
|
271 } |
|
272 ser->off_rx = ret; |
|
273 |
|
274 if (ser->max_tx_data_size > 0) { |
|
275 ser->tx_data = kmalloc(ser->max_tx_data_size, GFP_KERNEL); |
|
276 if (ser->tx_data == NULL) { |
|
277 ret = -ENOMEM; |
|
278 goto out_free_tty; |
|
279 } |
|
280 } |
|
281 |
|
282 return 0; |
|
283 |
|
284 out_free_tty: |
|
285 ectty_free(ser->tty); |
|
286 out_return: |
|
287 return ret; |
|
288 } |
|
289 |
|
290 /****************************************************************************/ |
|
291 |
|
292 void el6002_clear(el6002_t *ser) |
|
293 { |
|
294 ectty_free(ser->tty); |
|
295 if (ser->tx_data) { |
|
296 kfree(ser->tx_data); |
|
297 } |
|
298 } |
|
299 |
|
300 /****************************************************************************/ |
|
301 |
|
302 void el6002_run(el6002_t *ser, u8 *pd) |
|
303 { |
|
304 u16 status = EC_READ_U16(pd + ser->off_status); |
|
305 u8 *rx_data = pd + ser->off_rx; |
|
306 uint8_t tx_accepted_toggle, rx_request_toggle; |
|
307 |
|
308 switch (ser->state) { |
|
309 case SER_READY: |
|
310 |
|
311 /* Send data */ |
|
312 |
|
313 tx_accepted_toggle = status & 0x0001; |
|
314 if (tx_accepted_toggle != ser->tx_accepted_toggle) { // ready |
|
315 ser->tx_data_size = |
|
316 ectty_tx_data(ser->tty, ser->tx_data, ser->max_tx_data_size); |
|
317 if (ser->tx_data_size) { |
|
318 printk(KERN_INFO PFX "Sending %u bytes.\n", ser->tx_data_size); |
|
319 ser->tx_request_toggle = !ser->tx_request_toggle; |
|
320 ser->tx_accepted_toggle = tx_accepted_toggle; |
|
321 } |
|
322 } |
|
323 |
|
324 /* Receive data */ |
|
325 |
|
326 rx_request_toggle = status & 0x0002; |
|
327 if (rx_request_toggle != ser->rx_request_toggle) { |
|
328 uint8_t rx_data_size = status >> 8; |
|
329 ser->rx_request_toggle = rx_request_toggle; |
|
330 printk(KERN_INFO PFX "Received %u bytes.\n", rx_data_size); |
|
331 ectty_rx_data(ser->tty, rx_data, rx_data_size); |
|
332 ser->rx_accepted_toggle = !ser->rx_accepted_toggle; |
|
333 } |
|
334 |
|
335 ser->control = |
|
336 ser->tx_request_toggle | |
|
337 ser->rx_accepted_toggle << 1 | |
|
338 ser->tx_data_size << 8; |
|
339 break; |
|
340 |
|
341 case SER_REQUEST_INIT: |
|
342 if (status & (1 << 2)) { |
|
343 ser->control = 0x0000; |
|
344 ser->state = SER_WAIT_FOR_INIT_RESPONSE; |
|
345 } else { |
|
346 ser->control = 1 << 2; // CW.2, request initialization |
|
347 } |
|
348 break; |
|
349 |
|
350 case SER_WAIT_FOR_INIT_RESPONSE: |
|
351 if (!(status & (1 << 2))) { |
|
352 printk(KERN_INFO PFX "Init successful.\n"); |
|
353 ser->tx_accepted_toggle = 1; |
|
354 ser->control = 0x0000; |
|
355 ser->state = SER_READY; |
|
356 } |
|
357 break; |
|
358 } |
|
359 |
|
360 EC_WRITE_U16(pd + ser->off_ctrl, ser->control); |
|
361 memcpy(pd + ser->off_tx, ser->tx_data, ser->tx_data_size); |
|
362 } |
|
363 |
|
364 /*****************************************************************************/ |
|
365 |
|
366 void run_serial_devices(u8 *pd) |
|
367 { |
|
368 el6002_t *ser; |
|
369 |
|
370 list_for_each_entry(ser, &handlers, list) { |
|
371 el6002_run(ser, pd); |
|
372 } |
|
373 } |
|
374 |
|
375 /*****************************************************************************/ |
|
376 |
|
377 int create_serial_devices(ec_master_t *master, ec_domain_t *domain) |
|
378 { |
|
379 int i, ret; |
|
380 ec_master_info_t master_info; |
|
381 ec_slave_info_t slave_info; |
|
382 el6002_t *ser, *next; |
|
383 |
|
384 printk(KERN_INFO PFX "Registering serial devices...\n"); |
|
385 |
|
386 ret = ecrt_master(master, &master_info); |
|
387 if (ret) { |
|
388 printk(KERN_ERR PFX "Failed to obtain master information.\n"); |
|
389 goto out_return; |
|
390 } |
|
391 |
|
392 for (i = 0; i < master_info.slave_count; i++) { |
|
393 ret = ecrt_master_get_slave(master, i, &slave_info); |
|
394 if (ret) { |
|
395 printk(KERN_ERR PFX "Failed to obtain slave information.\n"); |
|
396 goto out_free_handlers; |
|
397 } |
|
398 |
|
399 if (slave_info.vendor_id != VendorIdBeckhoff |
|
400 || slave_info.product_code != ProductCodeBeckhoffEL6002) { |
|
401 continue; |
|
402 } |
|
403 |
|
404 printk(KERN_INFO PFX "Creating handler for serial device" |
|
405 " at position %i\n", i); |
|
406 |
|
407 ser = kmalloc(sizeof(*ser), GFP_KERNEL); |
|
408 if (!ser) { |
|
409 printk(KERN_ERR PFX "Failed to allocate serial device object.\n"); |
|
410 ret = -ENOMEM; |
|
411 goto out_free_handlers; |
|
412 } |
|
413 |
|
414 ret = el6002_init(ser, master, i, domain); |
|
415 if (ret) { |
|
416 printk(KERN_ERR PFX "Failed to init serial device object.\n"); |
|
417 kfree(ser); |
|
418 goto out_free_handlers; |
|
419 } |
|
420 |
|
421 list_add_tail(&ser->list, &handlers); |
|
422 } |
|
423 |
|
424 |
|
425 printk(KERN_INFO PFX "Finished.\n"); |
|
426 return 0; |
|
427 |
|
428 out_free_handlers: |
|
429 list_for_each_entry_safe(ser, next, &handlers, list) { |
|
430 list_del(&ser->list); |
|
431 el6002_clear(ser); |
|
432 kfree(ser); |
|
433 } |
|
434 out_return: |
|
435 return ret; |
|
436 } |
|
437 |
|
438 /*****************************************************************************/ |
|
439 |
|
440 void free_serial_devices(void) |
|
441 { |
|
442 el6002_t *ser, *next; |
|
443 |
|
444 printk(KERN_INFO PFX "Cleaning up serial devices...\n"); |
|
445 |
|
446 list_for_each_entry_safe(ser, next, &handlers, list) { |
|
447 list_del(&ser->list); |
|
448 el6002_clear(ser); |
|
449 kfree(ser); |
|
450 } |
|
451 |
|
452 printk(KERN_INFO PFX "Finished cleaning up serial devices.\n"); |
|
453 } |
|
454 |
|
455 /*****************************************************************************/ |