|
1 /****************************************************************************** |
|
2 * |
|
3 * m i n i . c |
|
4 * |
|
5 * Minimal module for EtherCAT. |
|
6 * |
|
7 * $Id$ |
|
8 * |
|
9 * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH |
|
10 * |
|
11 * This file is part of the IgH EtherCAT Master. |
|
12 * |
|
13 * The IgH EtherCAT Master is free software; you can redistribute it |
|
14 * and/or modify it under the terms of the GNU General Public License |
|
15 * as published by the Free Software Foundation; either version 2 of the |
|
16 * License, or (at your option) any later version. |
|
17 * |
|
18 * The IgH EtherCAT Master is distributed in the hope that it will be |
|
19 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 * GNU General Public License for more details. |
|
22 * |
|
23 * You should have received a copy of the GNU General Public License |
|
24 * along with the IgH EtherCAT Master; if not, write to the Free Software |
|
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
26 * |
|
27 * The right to use EtherCAT Technology is granted and comes free of |
|
28 * charge under condition of compatibility of product made by |
|
29 * Licensee. People intending to distribute/sell products based on the |
|
30 * code, have to sign an agreement to guarantee that products using |
|
31 * software based on IgH EtherCAT master stay compatible with the actual |
|
32 * EtherCAT specification (which are released themselves as an open |
|
33 * standard) as the (only) precondition to have the right to use EtherCAT |
|
34 * Technology, IP and trade marks. |
|
35 * |
|
36 *****************************************************************************/ |
|
37 |
|
38 #include <linux/module.h> |
|
39 #include <linux/delay.h> |
|
40 #include <linux/timer.h> |
|
41 #include <linux/spinlock.h> |
|
42 #include <linux/interrupt.h> |
|
43 |
|
44 #include "../../include/ecrt.h" // EtherCAT realtime interface |
|
45 |
|
46 #define ASYNC |
|
47 #define FREQUENCY 100 |
|
48 |
|
49 /*****************************************************************************/ |
|
50 |
|
51 struct timer_list timer; |
|
52 |
|
53 // EtherCAT |
|
54 ec_master_t *master = NULL; |
|
55 ec_domain_t *domain1 = NULL; |
|
56 spinlock_t master_lock = SPIN_LOCK_UNLOCKED; |
|
57 |
|
58 // data fields |
|
59 //void *r_ssi_input, *r_ssi_status, *r_4102[3]; |
|
60 |
|
61 // channels |
|
62 uint32_t k_pos; |
|
63 uint8_t k_stat; |
|
64 |
|
65 ec_field_init_t domain1_fields[] = { |
|
66 {NULL, "3", "Beckhoff", "EL5001", "InputValue", 0}, |
|
67 {NULL, "2", "Beckhoff", "EL4132", "OutputValue", 0}, |
|
68 {} |
|
69 }; |
|
70 |
|
71 /*****************************************************************************/ |
|
72 |
|
73 void run(unsigned long data) |
|
74 { |
|
75 static unsigned int counter = 0; |
|
76 |
|
77 spin_lock(&master_lock); |
|
78 |
|
79 #ifdef ASYNC |
|
80 // receive |
|
81 ecrt_master_async_receive(master); |
|
82 ecrt_domain_process(domain1); |
|
83 #else |
|
84 // send and receive |
|
85 ecrt_domain_queue(domain1); |
|
86 ecrt_master_run(master); |
|
87 ecrt_master_sync_io(master); |
|
88 ecrt_domain_process(domain1); |
|
89 #endif |
|
90 |
|
91 // process data |
|
92 //k_pos = EC_READ_U32(r_ssi); |
|
93 |
|
94 #ifdef ASYNC |
|
95 // send |
|
96 ecrt_domain_queue(domain1); |
|
97 ecrt_master_run(master); |
|
98 ecrt_master_async_send(master); |
|
99 #endif |
|
100 |
|
101 spin_unlock(&master_lock); |
|
102 |
|
103 if (counter) { |
|
104 counter--; |
|
105 } |
|
106 else { |
|
107 counter = FREQUENCY; |
|
108 //printk(KERN_INFO "k_pos = %i\n", k_pos); |
|
109 //printk(KERN_INFO "k_stat = 0x%02X\n", k_stat); |
|
110 } |
|
111 |
|
112 // restart timer |
|
113 timer.expires += HZ / FREQUENCY; |
|
114 add_timer(&timer); |
|
115 } |
|
116 |
|
117 /*****************************************************************************/ |
|
118 |
|
119 int request_lock(void *data) |
|
120 { |
|
121 spin_lock_bh(&master_lock); |
|
122 return 0; // access allowed |
|
123 } |
|
124 |
|
125 /*****************************************************************************/ |
|
126 |
|
127 void release_lock(void *data) |
|
128 { |
|
129 spin_unlock_bh(&master_lock); |
|
130 } |
|
131 |
|
132 /*****************************************************************************/ |
|
133 |
|
134 int __init init_mini_module(void) |
|
135 { |
|
136 printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
|
137 |
|
138 if ((master = ecrt_request_master(0)) == NULL) { |
|
139 printk(KERN_ERR "Requesting master 0 failed!\n"); |
|
140 goto out_return; |
|
141 } |
|
142 |
|
143 ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
144 |
|
145 printk(KERN_INFO "Registering domain...\n"); |
|
146 if (!(domain1 = ecrt_master_create_domain(master))) |
|
147 { |
|
148 printk(KERN_ERR "Domain creation failed!\n"); |
|
149 goto out_release_master; |
|
150 } |
|
151 |
|
152 printk(KERN_INFO "Registering domain fields...\n"); |
|
153 if (ecrt_domain_register_field_list(domain1, domain1_fields)) { |
|
154 printk(KERN_ERR "Field registration failed!\n"); |
|
155 goto out_release_master; |
|
156 } |
|
157 |
|
158 printk(KERN_INFO "Activating master...\n"); |
|
159 if (ecrt_master_activate(master)) { |
|
160 printk(KERN_ERR "Failed to activate master!\n"); |
|
161 goto out_release_master; |
|
162 } |
|
163 |
|
164 #if 0 |
|
165 if (ecrt_master_fetch_sdo_lists(master)) { |
|
166 printk(KERN_ERR "Failed to fetch SDO lists!\n"); |
|
167 goto out_deactivate; |
|
168 } |
|
169 ecrt_master_print(master, 2); |
|
170 #else |
|
171 ecrt_master_print(master, 0); |
|
172 #endif |
|
173 |
|
174 #if 0 |
|
175 if (!(slave = ecrt_master_get_slave(master, "5"))) { |
|
176 printk(KERN_ERR "Failed to get slave 5!\n"); |
|
177 goto out_deactivate; |
|
178 } |
|
179 |
|
180 if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || |
|
181 ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || |
|
182 ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || |
|
183 ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || |
|
184 ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || |
|
185 ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || |
|
186 ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || |
|
187 ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || |
|
188 ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { |
|
189 printk(KERN_ERR "Failed to configure SSI slave!\n"); |
|
190 goto out_deactivate; |
|
191 } |
|
192 #endif |
|
193 |
|
194 #if 0 |
|
195 printk(KERN_INFO "Writing alias...\n"); |
|
196 if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) { |
|
197 printk(KERN_ERR "Failed to write alias!\n"); |
|
198 goto out_deactivate; |
|
199 } |
|
200 #endif |
|
201 |
|
202 #ifdef ASYNC |
|
203 // send once and wait... |
|
204 ecrt_master_prepare_async_io(master); |
|
205 #endif |
|
206 |
|
207 #if 1 |
|
208 if (ecrt_master_start_eoe(master)) { |
|
209 printk(KERN_ERR "Failed to start EoE processing!\n"); |
|
210 goto out_deactivate; |
|
211 } |
|
212 #endif |
|
213 |
|
214 printk("Starting cyclic sample thread.\n"); |
|
215 init_timer(&timer); |
|
216 timer.function = run; |
|
217 timer.expires = jiffies + 10; |
|
218 add_timer(&timer); |
|
219 |
|
220 printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
|
221 return 0; |
|
222 |
|
223 #if 1 |
|
224 out_deactivate: |
|
225 ecrt_master_deactivate(master); |
|
226 #endif |
|
227 out_release_master: |
|
228 ecrt_release_master(master); |
|
229 out_return: |
|
230 return -1; |
|
231 } |
|
232 |
|
233 /*****************************************************************************/ |
|
234 |
|
235 void __exit cleanup_mini_module(void) |
|
236 { |
|
237 printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
|
238 |
|
239 if (master) { |
|
240 del_timer_sync(&timer); |
|
241 printk(KERN_INFO "Deactivating master...\n"); |
|
242 ecrt_master_deactivate(master); |
|
243 ecrt_release_master(master); |
|
244 } |
|
245 |
|
246 printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
|
247 } |
|
248 |
|
249 /*****************************************************************************/ |
|
250 |
|
251 MODULE_LICENSE("GPL"); |
|
252 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
253 MODULE_DESCRIPTION ("EtherCAT minimal test environment"); |
|
254 |
|
255 module_init(init_mini_module); |
|
256 module_exit(cleanup_mini_module); |
|
257 |
|
258 /*****************************************************************************/ |
|
259 |