|
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 ec_tty_t *tty; |
|
55 ec_slave_config_t *sc; |
|
56 |
|
57 size_t max_tx_data_size; |
|
58 size_t max_rx_data_size; |
|
59 |
|
60 u8 *tx_data; |
|
61 u8 tx_data_size; |
|
62 |
|
63 serial_state_t state; |
|
64 |
|
65 u8 tx_request_toggle; |
|
66 u8 tx_accepted_toggle; |
|
67 |
|
68 u8 rx_request_toggle; |
|
69 u8 rx_accepted_toggle; |
|
70 |
|
71 u16 control; |
|
72 |
|
73 u32 off_ctrl; |
|
74 u32 off_tx; |
|
75 u32 off_status; |
|
76 u32 off_rx; |
|
77 } el6002_t; |
|
78 |
|
79 static el6002_t *ser = NULL; |
|
80 |
|
81 /*****************************************************************************/ |
|
82 |
|
83 /* Beckhoff EL6002 |
|
84 * Vendor ID: 0x00000002 |
|
85 * Product code: 0x17723052 |
|
86 * Revision number: 0x00100000 |
|
87 */ |
|
88 |
|
89 ec_pdo_entry_info_t el6002_pdo_entries[] = { |
|
90 {0x7001, 0x01, 16}, /* Ctrl */ |
|
91 {0x7000, 0x11, 8}, /* Data Out 0 */ |
|
92 {0x7000, 0x12, 8}, /* Data Out 1 */ |
|
93 {0x7000, 0x13, 8}, /* Data Out 2 */ |
|
94 {0x7000, 0x14, 8}, /* Data Out 3 */ |
|
95 {0x7000, 0x15, 8}, /* Data Out 4 */ |
|
96 {0x7000, 0x16, 8}, /* Data Out 5 */ |
|
97 {0x7000, 0x17, 8}, /* Data Out 6 */ |
|
98 {0x7000, 0x18, 8}, /* Data Out 7 */ |
|
99 {0x7000, 0x19, 8}, /* Data Out 8 */ |
|
100 {0x7000, 0x1a, 8}, /* Data Out 9 */ |
|
101 {0x7000, 0x1b, 8}, /* Data Out 10 */ |
|
102 {0x7000, 0x1c, 8}, /* Data Out 11 */ |
|
103 {0x7000, 0x1d, 8}, /* Data Out 12 */ |
|
104 {0x7000, 0x1e, 8}, /* Data Out 13 */ |
|
105 {0x7000, 0x1f, 8}, /* Data Out 14 */ |
|
106 {0x7000, 0x20, 8}, /* Data Out 15 */ |
|
107 {0x7000, 0x21, 8}, /* Data Out 16 */ |
|
108 {0x7000, 0x22, 8}, /* Data Out 17 */ |
|
109 {0x7000, 0x23, 8}, /* Data Out 18 */ |
|
110 {0x7000, 0x24, 8}, /* Data Out 19 */ |
|
111 {0x7000, 0x25, 8}, /* Data Out 20 */ |
|
112 {0x7000, 0x26, 8}, /* Data Out 21 */ |
|
113 {0x7011, 0x01, 16}, /* Ctrl */ |
|
114 {0x7010, 0x11, 8}, /* Data Out 0 */ |
|
115 {0x7010, 0x12, 8}, /* Data Out 1 */ |
|
116 {0x7010, 0x13, 8}, /* Data Out 2 */ |
|
117 {0x7010, 0x14, 8}, /* Data Out 3 */ |
|
118 {0x7010, 0x15, 8}, /* Data Out 4 */ |
|
119 {0x7010, 0x16, 8}, /* Data Out 5 */ |
|
120 {0x7010, 0x17, 8}, /* Data Out 6 */ |
|
121 {0x7010, 0x18, 8}, /* Data Out 7 */ |
|
122 {0x7010, 0x19, 8}, /* Data Out 8 */ |
|
123 {0x7010, 0x1a, 8}, /* Data Out 9 */ |
|
124 {0x7010, 0x1b, 8}, /* Data Out 10 */ |
|
125 {0x7010, 0x1c, 8}, /* Data Out 11 */ |
|
126 {0x7010, 0x1d, 8}, /* Data Out 12 */ |
|
127 {0x7010, 0x1e, 8}, /* Data Out 13 */ |
|
128 {0x7010, 0x1f, 8}, /* Data Out 14 */ |
|
129 {0x7010, 0x20, 8}, /* Data Out 15 */ |
|
130 {0x7010, 0x21, 8}, /* Data Out 16 */ |
|
131 {0x7010, 0x22, 8}, /* Data Out 17 */ |
|
132 {0x7010, 0x23, 8}, /* Data Out 18 */ |
|
133 {0x7010, 0x24, 8}, /* Data Out 19 */ |
|
134 {0x7010, 0x25, 8}, /* Data Out 20 */ |
|
135 {0x7010, 0x26, 8}, /* Data Out 21 */ |
|
136 {0x6001, 0x01, 16}, /* Status */ |
|
137 {0x6000, 0x11, 8}, /* Data In 0 */ |
|
138 {0x6000, 0x12, 8}, /* Data In 1 */ |
|
139 {0x6000, 0x13, 8}, /* Data In 2 */ |
|
140 {0x6000, 0x14, 8}, /* Data In 3 */ |
|
141 {0x6000, 0x15, 8}, /* Data In 4 */ |
|
142 {0x6000, 0x16, 8}, /* Data In 5 */ |
|
143 {0x6000, 0x17, 8}, /* Data In 6 */ |
|
144 {0x6000, 0x18, 8}, /* Data In 7 */ |
|
145 {0x6000, 0x19, 8}, /* Data In 8 */ |
|
146 {0x6000, 0x1a, 8}, /* Data In 9 */ |
|
147 {0x6000, 0x1b, 8}, /* Data In 10 */ |
|
148 {0x6000, 0x1c, 8}, /* Data In 11 */ |
|
149 {0x6000, 0x1d, 8}, /* Data In 12 */ |
|
150 {0x6000, 0x1e, 8}, /* Data In 13 */ |
|
151 {0x6000, 0x1f, 8}, /* Data In 14 */ |
|
152 {0x6000, 0x20, 8}, /* Data In 15 */ |
|
153 {0x6000, 0x21, 8}, /* Data In 16 */ |
|
154 {0x6000, 0x22, 8}, /* Data In 17 */ |
|
155 {0x6000, 0x23, 8}, /* Data In 18 */ |
|
156 {0x6000, 0x24, 8}, /* Data In 19 */ |
|
157 {0x6000, 0x25, 8}, /* Data In 20 */ |
|
158 {0x6000, 0x26, 8}, /* Data In 21 */ |
|
159 {0x6011, 0x01, 16}, /* Status */ |
|
160 {0x6010, 0x11, 8}, /* Data In 0 */ |
|
161 {0x6010, 0x12, 8}, /* Data In 1 */ |
|
162 {0x6010, 0x13, 8}, /* Data In 2 */ |
|
163 {0x6010, 0x14, 8}, /* Data In 3 */ |
|
164 {0x6010, 0x15, 8}, /* Data In 4 */ |
|
165 {0x6010, 0x16, 8}, /* Data In 5 */ |
|
166 {0x6010, 0x17, 8}, /* Data In 6 */ |
|
167 {0x6010, 0x18, 8}, /* Data In 7 */ |
|
168 {0x6010, 0x19, 8}, /* Data In 8 */ |
|
169 {0x6010, 0x1a, 8}, /* Data In 9 */ |
|
170 {0x6010, 0x1b, 8}, /* Data In 10 */ |
|
171 {0x6010, 0x1c, 8}, /* Data In 11 */ |
|
172 {0x6010, 0x1d, 8}, /* Data In 12 */ |
|
173 {0x6010, 0x1e, 8}, /* Data In 13 */ |
|
174 {0x6010, 0x1f, 8}, /* Data In 14 */ |
|
175 {0x6010, 0x20, 8}, /* Data In 15 */ |
|
176 {0x6010, 0x21, 8}, /* Data In 16 */ |
|
177 {0x6010, 0x22, 8}, /* Data In 17 */ |
|
178 {0x6010, 0x23, 8}, /* Data In 18 */ |
|
179 {0x6010, 0x24, 8}, /* Data In 19 */ |
|
180 {0x6010, 0x25, 8}, /* Data In 20 */ |
|
181 {0x6010, 0x26, 8}, /* Data In 21 */ |
|
182 }; |
|
183 |
|
184 ec_pdo_info_t el6002_pdos[] = { |
|
185 {0x1604, 23, el6002_pdo_entries + 0}, /* COM RxPDO-Map Outputs Ch.1 */ |
|
186 {0x1605, 23, el6002_pdo_entries + 23}, /* COM RxPDO-Map Outputs Ch.2 */ |
|
187 {0x1a04, 23, el6002_pdo_entries + 46}, /* COM TxPDO-Map Inputs Ch.1 */ |
|
188 {0x1a05, 23, el6002_pdo_entries + 69}, /* COM TxPDO-Map Inputs Ch.2 */ |
|
189 }; |
|
190 |
|
191 ec_sync_info_t el6002_syncs[] = { |
|
192 {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE}, |
|
193 {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE}, |
|
194 {2, EC_DIR_OUTPUT, 2, el6002_pdos + 0, EC_WD_DISABLE}, |
|
195 {3, EC_DIR_INPUT, 2, el6002_pdos + 2, EC_WD_DISABLE}, |
|
196 {0xff} |
|
197 }; |
|
198 |
|
199 /****************************************************************************/ |
|
200 |
|
201 int el6002_init(el6002_t *ser, ec_master_t *master, u16 position, |
|
202 ec_domain_t *domain) |
|
203 { |
|
204 int ret = 0; |
|
205 |
|
206 ser->tty = ectty_create(); |
|
207 if (IS_ERR(ser->tty)) { |
|
208 printk(KERN_ERR PFX "Failed to create tty.\n"); |
|
209 ret = PTR_ERR(ser->tty); |
|
210 goto out_return; |
|
211 } |
|
212 |
|
213 ser->sc = NULL; |
|
214 ser->max_tx_data_size = 22; |
|
215 ser->max_rx_data_size = 22; |
|
216 ser->tx_data = NULL; |
|
217 ser->tx_data_size = 0; |
|
218 ser->state = SER_REQUEST_INIT; |
|
219 ser->tx_request_toggle = 0; |
|
220 ser->rx_accepted_toggle = 0; |
|
221 ser->control = 0x0000; |
|
222 ser->off_ctrl = 0; |
|
223 ser->off_tx = 0; |
|
224 ser->off_status = 0; |
|
225 ser->off_rx = 0; |
|
226 |
|
227 if (!(ser->sc = ecrt_master_slave_config( |
|
228 master, 0, position, Beckhoff_EL6002))) { |
|
229 printk(KERN_ERR PFX "Failed to create slave configuration.\n"); |
|
230 ret = -EBUSY; |
|
231 goto out_free_tty; |
|
232 } |
|
233 |
|
234 if (ecrt_slave_config_pdos(ser->sc, EC_END, el6002_syncs)) { |
|
235 printk(KERN_ERR PFX "Failed to configure PDOs.\n"); |
|
236 ret = -ENOMEM; |
|
237 goto out_free_tty; |
|
238 } |
|
239 |
|
240 ret = ecrt_slave_config_reg_pdo_entry( |
|
241 ser->sc, 0x7001, 0x01, domain, NULL); |
|
242 if (ret < 0) { |
|
243 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
244 goto out_free_tty; |
|
245 } |
|
246 ser->off_ctrl = ret; |
|
247 |
|
248 ret = ecrt_slave_config_reg_pdo_entry( |
|
249 ser->sc, 0x7000, 0x11, domain, NULL); |
|
250 if (ret < 0) { |
|
251 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
252 goto out_free_tty; |
|
253 } |
|
254 ser->off_tx = ret; |
|
255 |
|
256 ret = ecrt_slave_config_reg_pdo_entry( |
|
257 ser->sc, 0x6001, 0x01, domain, NULL); |
|
258 if (ret < 0) { |
|
259 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
260 goto out_free_tty; |
|
261 } |
|
262 ser->off_status = ret; |
|
263 |
|
264 ret = ecrt_slave_config_reg_pdo_entry( |
|
265 ser->sc, 0x6000, 0x11, domain, NULL); |
|
266 if (ret < 0) { |
|
267 printk(KERN_ERR PFX "Failed to register PDO entry.\n"); |
|
268 goto out_free_tty; |
|
269 } |
|
270 ser->off_rx = ret; |
|
271 |
|
272 if (ser->max_tx_data_size > 0) { |
|
273 ser->tx_data = kmalloc(ser->max_tx_data_size, GFP_KERNEL); |
|
274 if (ser->tx_data == NULL) { |
|
275 ret = -ENOMEM; |
|
276 goto out_free_tty; |
|
277 } |
|
278 } |
|
279 |
|
280 return 0; |
|
281 |
|
282 out_free_tty: |
|
283 ectty_free(ser->tty); |
|
284 out_return: |
|
285 return ret; |
|
286 } |
|
287 |
|
288 /****************************************************************************/ |
|
289 |
|
290 void el6002_clear(el6002_t *ser) |
|
291 { |
|
292 ectty_free(ser->tty); |
|
293 if (ser->tx_data) { |
|
294 kfree(ser->tx_data); |
|
295 } |
|
296 } |
|
297 |
|
298 /****************************************************************************/ |
|
299 |
|
300 void el6002_run(el6002_t *ser, u8 *pd) |
|
301 { |
|
302 u16 status = EC_READ_U16(pd + ser->off_status); |
|
303 u8 *rx_data = pd + ser->off_rx; |
|
304 uint8_t tx_accepted_toggle, rx_request_toggle; |
|
305 |
|
306 switch (ser->state) { |
|
307 case SER_READY: |
|
308 |
|
309 /* Send data */ |
|
310 |
|
311 tx_accepted_toggle = status & 0x0001; |
|
312 if (tx_accepted_toggle != ser->tx_accepted_toggle) { // ready |
|
313 ser->tx_data_size = |
|
314 ectty_tx_data(ser->tty, ser->tx_data, ser->max_tx_data_size); |
|
315 if (ser->tx_data_size) { |
|
316 printk(KERN_INFO PFX "Sending %u bytes.\n", ser->tx_data_size); |
|
317 ser->tx_request_toggle = !ser->tx_request_toggle; |
|
318 ser->tx_accepted_toggle = tx_accepted_toggle; |
|
319 } |
|
320 } |
|
321 |
|
322 /* Receive data */ |
|
323 |
|
324 rx_request_toggle = status & 0x0002; |
|
325 if (rx_request_toggle != ser->rx_request_toggle) { |
|
326 uint8_t rx_data_size = status >> 8; |
|
327 ser->rx_request_toggle = rx_request_toggle; |
|
328 printk(KERN_INFO PFX "Received %u bytes.\n", rx_data_size); |
|
329 ectty_rx_data(ser->tty, rx_data, rx_data_size); |
|
330 ser->rx_accepted_toggle = !ser->rx_accepted_toggle; |
|
331 } |
|
332 |
|
333 ser->control = |
|
334 ser->tx_request_toggle | |
|
335 ser->rx_accepted_toggle << 1 | |
|
336 ser->tx_data_size << 8; |
|
337 break; |
|
338 |
|
339 case SER_REQUEST_INIT: |
|
340 if (status & (1 << 2)) { |
|
341 ser->control = 0x0000; |
|
342 ser->state = SER_WAIT_FOR_INIT_RESPONSE; |
|
343 } else { |
|
344 ser->control = 1 << 2; // CW.2, request initialization |
|
345 } |
|
346 break; |
|
347 |
|
348 case SER_WAIT_FOR_INIT_RESPONSE: |
|
349 if (!(status & (1 << 2))) { |
|
350 printk(KERN_INFO PFX "Init successful.\n"); |
|
351 ser->tx_accepted_toggle = 1; |
|
352 ser->control = 0x0000; |
|
353 ser->state = SER_READY; |
|
354 } |
|
355 break; |
|
356 } |
|
357 |
|
358 EC_WRITE_U16(pd + ser->off_ctrl, ser->control); |
|
359 memcpy(pd + ser->off_tx, ser->tx_data, ser->tx_data_size); |
|
360 } |
|
361 |
|
362 /*****************************************************************************/ |
|
363 |
|
364 void run_serial_devices(u8 *pd) |
|
365 { |
|
366 if (ser) { |
|
367 el6002_run(ser, pd); |
|
368 } |
|
369 } |
|
370 |
|
371 /*****************************************************************************/ |
|
372 |
|
373 int create_serial_devices(ec_master_t *master, ec_domain_t *domain) |
|
374 { |
|
375 int i, ret; |
|
376 ec_master_info_t master_info; |
|
377 ec_slave_info_t slave_info; |
|
378 |
|
379 printk(KERN_INFO PFX "Registering serial devices...\n"); |
|
380 |
|
381 ret = ecrt_master(master, &master_info); |
|
382 if (ret) { |
|
383 printk(KERN_ERR PFX "Failed to obtain master information.\n"); |
|
384 return ret; |
|
385 } |
|
386 |
|
387 for (i = 0; i < master_info.slave_count; i++) { |
|
388 ret = ecrt_master_get_slave(master, i, &slave_info); |
|
389 if (ret) { |
|
390 printk(KERN_ERR PFX "Failed to obtain master information.\n"); |
|
391 return ret; |
|
392 } |
|
393 |
|
394 if (slave_info.vendor_id != VendorIdBeckhoff |
|
395 || slave_info.product_code != ProductCodeBeckhoffEL6002) { |
|
396 continue; |
|
397 } |
|
398 |
|
399 printk(KERN_INFO PFX "Creating handler for serial device" |
|
400 " at position %i\n", i); |
|
401 |
|
402 ser = kmalloc(sizeof(*ser), GFP_KERNEL); |
|
403 if (!ser) { |
|
404 printk(KERN_ERR PFX "Failed to allocate serial device object.\n"); |
|
405 return -ENOMEM; |
|
406 } |
|
407 |
|
408 ret = el6002_init(ser, master, i, domain); |
|
409 if (ret) { |
|
410 printk(KERN_ERR PFX "Failed to init serial device object.\n"); |
|
411 kfree(ser); |
|
412 ser = NULL; |
|
413 return ret; |
|
414 } |
|
415 |
|
416 break; // FIXME |
|
417 } |
|
418 |
|
419 |
|
420 printk(KERN_INFO PFX "Finished.\n"); |
|
421 return 0; |
|
422 } |
|
423 |
|
424 /*****************************************************************************/ |
|
425 |
|
426 void cleanup_serial_devices(void) |
|
427 { |
|
428 printk(KERN_INFO PFX "Cleaning up serial devices...\n"); |
|
429 |
|
430 if (ser) { |
|
431 el6002_clear(ser); |
|
432 kfree(ser); |
|
433 } |
|
434 |
|
435 printk(KERN_INFO PFX "Finished cleaning up serial devices.\n"); |
|
436 } |
|
437 |
|
438 /*****************************************************************************/ |