37 #include <linux/interrupt.h> |
37 #include <linux/interrupt.h> |
38 |
38 |
39 #include "../../include/ecrt.h" // EtherCAT realtime interface |
39 #include "../../include/ecrt.h" // EtherCAT realtime interface |
40 #include "../../include/ecdb.h" // EtherCAT slave database |
40 #include "../../include/ecdb.h" // EtherCAT slave database |
41 |
41 |
|
42 #define PFX "ec_mini: " |
|
43 |
42 #define FREQUENCY 100 |
44 #define FREQUENCY 100 |
43 |
45 |
44 //#define KBUS |
46 //#define KBUS |
45 |
47 |
46 /*****************************************************************************/ |
48 /*****************************************************************************/ |
47 |
49 |
48 struct timer_list timer; |
50 static struct timer_list timer; |
49 |
51 |
50 // EtherCAT |
52 // EtherCAT |
51 ec_master_t *master = NULL; |
53 static ec_master_t *master = NULL; |
52 ec_domain_t *domain1 = NULL; |
54 static ec_domain_t *domain1 = NULL; |
53 spinlock_t master_lock = SPIN_LOCK_UNLOCKED; |
55 spinlock_t master_lock = SPIN_LOCK_UNLOCKED; |
|
56 static ec_master_status_t master_status, old_status = {}; |
54 |
57 |
55 // data fields |
58 // data fields |
56 #ifdef KBUS |
59 #ifdef KBUS |
57 void *r_inputs; |
60 static void *r_inputs; |
58 void *r_outputs; |
61 static void *r_outputs; |
59 #endif |
62 #endif |
60 |
63 |
61 void *r_dig_out; |
64 static void *r_dig_out; |
62 |
65 |
63 #if 1 |
66 #if 1 |
64 ec_pdo_reg_t domain1_pdos[] = { |
67 ec_pdo_reg_t domain1_pdos[] = { |
65 {"4", Beckhoff_EL2004_Outputs, &r_dig_out}, |
68 {"4", Beckhoff_EL2004_Outputs, &r_dig_out}, |
66 {} |
69 {} |
88 counter--; |
91 counter--; |
89 } |
92 } |
90 else { |
93 else { |
91 counter = FREQUENCY; |
94 counter = FREQUENCY; |
92 blink = !blink; |
95 blink = !blink; |
|
96 |
|
97 spin_lock(&master_lock); |
|
98 ecrt_master_get_status(master, &master_status); |
|
99 spin_unlock(&master_lock); |
|
100 |
|
101 if (master_status.bus_status != old_status.bus_status) { |
|
102 printk(KERN_INFO PFX "bus status changed to %u.\n", |
|
103 master_status.bus_status); |
|
104 } |
|
105 if (master_status.bus_tainted != old_status.bus_tainted) { |
|
106 printk(KERN_INFO PFX "tainted flag changed to %u.\n", |
|
107 master_status.bus_tainted); |
|
108 } |
|
109 if (master_status.slaves_responding != |
|
110 old_status.slaves_responding) { |
|
111 printk(KERN_INFO PFX "slaves_responding changed to %u.\n", |
|
112 master_status.slaves_responding); |
|
113 } |
|
114 |
|
115 old_status = master_status; |
93 } |
116 } |
94 |
117 |
95 #ifdef KBUS |
118 #ifdef KBUS |
96 EC_WRITE_U8(r_outputs + 2, blink ? 0xFF : 0x00); |
119 EC_WRITE_U8(r_outputs + 2, blink ? 0xFF : 0x00); |
97 #endif |
120 #endif |
133 { |
156 { |
134 #if 0 |
157 #if 0 |
135 ec_slave_t *slave; |
158 ec_slave_t *slave; |
136 #endif |
159 #endif |
137 |
160 |
138 printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
161 printk(KERN_INFO PFX "Starting...\n"); |
139 |
162 |
140 if (!(master = ecrt_request_master(0))) { |
163 if (!(master = ecrt_request_master(0))) { |
141 printk(KERN_ERR "Requesting master 0 failed!\n"); |
164 printk(KERN_ERR PFX "Requesting master 0 failed!\n"); |
142 goto out_return; |
165 goto out_return; |
143 } |
166 } |
144 |
167 |
145 ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
168 ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
146 |
169 |
147 printk(KERN_INFO "Registering domain...\n"); |
170 printk(KERN_INFO PFX "Registering domain...\n"); |
148 if (!(domain1 = ecrt_master_create_domain(master))) { |
171 if (!(domain1 = ecrt_master_create_domain(master))) { |
149 printk(KERN_ERR "Domain creation failed!\n"); |
172 printk(KERN_ERR PFX "Domain creation failed!\n"); |
150 goto out_release_master; |
173 goto out_release_master; |
151 } |
174 } |
152 |
175 |
153 printk(KERN_INFO "Registering PDOs...\n"); |
176 printk(KERN_INFO PFX "Registering PDOs...\n"); |
154 #if 1 |
177 #if 1 |
155 if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) { |
178 if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) { |
156 printk(KERN_ERR "PDO registration failed!\n"); |
179 printk(KERN_ERR PFX "PDO registration failed!\n"); |
157 goto out_release_master; |
180 goto out_release_master; |
158 } |
181 } |
159 #endif |
182 #endif |
160 |
183 |
161 #ifdef KBUS |
184 #ifdef KBUS |
162 if (!ecrt_domain_register_pdo_range(domain1, "0", Beckhoff_BK1120, |
185 if (!ecrt_domain_register_pdo_range(domain1, "0", Beckhoff_BK1120, |
163 EC_DIR_OUTPUT, 0, 4, &r_outputs)) { |
186 EC_DIR_OUTPUT, 0, 4, &r_outputs)) { |
164 printk(KERN_ERR "PDO registration failed!\n"); |
187 printk(KERN_ERR PFX "PDO registration failed!\n"); |
165 goto out_release_master; |
188 goto out_release_master; |
166 } |
189 } |
167 if (!ecrt_domain_register_pdo_range(domain1, "0", Beckhoff_BK1120, |
190 if (!ecrt_domain_register_pdo_range(domain1, "0", Beckhoff_BK1120, |
168 EC_DIR_INPUT, 0, 4, &r_inputs)) { |
191 EC_DIR_INPUT, 0, 4, &r_inputs)) { |
169 printk(KERN_ERR "PDO registration failed!\n"); |
192 printk(KERN_ERR PFX "PDO registration failed!\n"); |
170 goto out_release_master; |
193 goto out_release_master; |
171 } |
194 } |
172 #endif |
195 #endif |
173 |
196 |
174 #if 0 |
197 #if 0 |
177 |
200 |
178 if (ecrt_slave_conf_sdo8(slave, 0x4061, 1, 0)) |
201 if (ecrt_slave_conf_sdo8(slave, 0x4061, 1, 0)) |
179 goto out_release_master; |
202 goto out_release_master; |
180 #endif |
203 #endif |
181 |
204 |
182 printk(KERN_INFO "Activating master...\n"); |
205 printk(KERN_INFO PFX "Activating master...\n"); |
183 if (ecrt_master_activate(master)) { |
206 if (ecrt_master_activate(master)) { |
184 printk(KERN_ERR "Failed to activate master!\n"); |
207 printk(KERN_ERR PFX "Failed to activate master!\n"); |
185 goto out_release_master; |
208 goto out_release_master; |
186 } |
209 } |
187 |
210 |
188 printk("Starting cyclic sample thread.\n"); |
211 printk(KERN_INFO PFX "Starting cyclic sample thread.\n"); |
189 init_timer(&timer); |
212 init_timer(&timer); |
190 timer.function = run; |
213 timer.function = run; |
191 timer.expires = jiffies + 10; |
214 timer.expires = jiffies + 10; |
192 add_timer(&timer); |
215 add_timer(&timer); |
193 |
216 |
194 printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
217 printk(KERN_INFO PFX "Started.\n"); |
195 return 0; |
218 return 0; |
196 |
219 |
197 out_release_master: |
220 out_release_master: |
198 ecrt_release_master(master); |
221 ecrt_release_master(master); |
199 out_return: |
222 out_return: |
202 |
225 |
203 /*****************************************************************************/ |
226 /*****************************************************************************/ |
204 |
227 |
205 void __exit cleanup_mini_module(void) |
228 void __exit cleanup_mini_module(void) |
206 { |
229 { |
207 printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
230 printk(KERN_INFO PFX "Stopping...\n"); |
208 |
231 |
209 del_timer_sync(&timer); |
232 del_timer_sync(&timer); |
210 printk(KERN_INFO "Releasing master...\n"); |
233 printk(KERN_INFO PFX "Releasing master...\n"); |
211 ecrt_release_master(master); |
234 ecrt_release_master(master); |
212 |
235 |
213 printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
236 printk(KERN_INFO PFX "Stopped.\n"); |
214 } |
237 } |
215 |
238 |
216 /*****************************************************************************/ |
239 /*****************************************************************************/ |
217 |
240 |
218 MODULE_LICENSE("GPL"); |
241 MODULE_LICENSE("GPL"); |