|
1 /****************************************************************************** |
|
2 * |
|
3 * m s r _ r t . c |
|
4 * |
|
5 * Kernelmodul für 2.6 Kernel zur Meßdatenerfassung, Steuerung und Regelung. |
|
6 * |
|
7 * Autor: Wilhelm Hagemeister, Florian Pose |
|
8 * |
|
9 * (C) Copyright IgH 2002 |
|
10 * Ingenieurgemeinschaft IgH |
|
11 * Heinz-Bäcker Str. 34 |
|
12 * D-45356 Essen |
|
13 * Tel.: +49 201/61 99 31 |
|
14 * Fax.: +49 201/61 98 36 |
|
15 * E-mail: hm@igh-essen.com |
|
16 * |
|
17 * $Id$ |
|
18 * |
|
19 *****************************************************************************/ |
|
20 |
|
21 // Linux |
|
22 #include <linux/module.h> |
|
23 #include <linux/ipipe.h> |
|
24 #include <linux/slab.h> |
|
25 #include <linux/vmalloc.h> |
|
26 #include <linux/delay.h> |
|
27 |
|
28 // RT_lib |
|
29 #include <msr_main.h> |
|
30 #include <msr_utils.h> |
|
31 #include <msr_messages.h> |
|
32 #include <msr_float.h> |
|
33 #include <msr_reg.h> |
|
34 #include <msr_time.h> |
|
35 #include "msr_param.h" |
|
36 |
|
37 // EtherCAT |
|
38 #include "../include/ecrt.h" |
|
39 |
|
40 #define ASYNC |
|
41 |
|
42 // Defines/Makros |
|
43 #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ) |
|
44 |
|
45 /*****************************************************************************/ |
|
46 /* Globale Variablen */ |
|
47 |
|
48 // Adeos |
|
49 static struct ipipe_domain this_domain; |
|
50 static struct ipipe_sysinfo sys_info; |
|
51 |
|
52 // EtherCAT |
|
53 ec_master_t *master = NULL; |
|
54 ec_domain_t *domain1 = NULL; |
|
55 |
|
56 // Prozessdaten |
|
57 void *r_ssi; |
|
58 void *r_ssi_st; |
|
59 |
|
60 // KanŽäle |
|
61 uint32_t k_ssi; |
|
62 uint32_t k_ssi_st; |
|
63 |
|
64 ec_field_init_t domain1_fields[] = { |
|
65 {&r_ssi, "5", "Beckhoff", "EL5001", "InputValue", 0}, |
|
66 {&r_ssi_st, "5", "Beckhoff", "EL5001", "Status", 0}, |
|
67 {} |
|
68 }; |
|
69 |
|
70 /*****************************************************************************/ |
|
71 |
|
72 static void msr_controller_run(void) |
|
73 { |
|
74 #ifdef ASYNC |
|
75 // Empfangen |
|
76 ecrt_master_async_receive(master); |
|
77 ecrt_domain_process(domain1); |
|
78 #else |
|
79 // Senden und empfangen |
|
80 ecrt_domain_queue(domain1); |
|
81 ecrt_master_run(master); |
|
82 ecrt_master_sync_io(master); |
|
83 ecrt_domain_process(domain1); |
|
84 #endif |
|
85 |
|
86 // Prozessdaten verarbeiten |
|
87 k_ssi = EC_READ_U32(r_ssi); |
|
88 k_ssi_st = EC_READ_U8 (r_ssi_st); |
|
89 |
|
90 #ifdef ASYNC |
|
91 // Senden |
|
92 ecrt_domain_queue(domain1); |
|
93 ecrt_master_run(master); |
|
94 ecrt_master_async_send(master); |
|
95 #endif |
|
96 } |
|
97 |
|
98 /*****************************************************************************/ |
|
99 |
|
100 int msr_globals_register(void) |
|
101 { |
|
102 msr_reg_kanal("/ssi_position", "", &k_ssi, TUINT); |
|
103 msr_reg_kanal("/ssi_status", "", &k_ssi_st, TUINT); |
|
104 return 0; |
|
105 } |
|
106 |
|
107 /*****************************************************************************/ |
|
108 |
|
109 void msr_run(unsigned irq) |
|
110 { |
|
111 static int counter = 0; |
|
112 |
|
113 MSR_ADEOS_INTERRUPT_CODE(msr_controller_run(); msr_write_kanal_list();); |
|
114 |
|
115 ipipe_control_irq(irq, 0, IPIPE_ENABLE_MASK); // Interrupt bestŽätigen |
|
116 if (++counter >= HZREDUCTION) { |
|
117 ipipe_propagate_irq(irq); // und weiterreichen |
|
118 counter = 0; |
|
119 } |
|
120 } |
|
121 |
|
122 /*****************************************************************************/ |
|
123 |
|
124 void domain_entry(void) |
|
125 { |
|
126 printk("Domain %s started.\n", ipipe_current_domain->name); |
|
127 |
|
128 ipipe_get_sysinfo(&sys_info); |
|
129 ipipe_virtualize_irq(ipipe_current_domain,sys_info.archdep.tmirq, |
|
130 &msr_run, NULL, IPIPE_HANDLE_MASK); |
|
131 |
|
132 ipipe_tune_timer(1000000000UL / MSR_ABTASTFREQUENZ, 0); |
|
133 } |
|
134 |
|
135 /*****************************************************************************/ |
|
136 |
|
137 int __init init_rt_module(void) |
|
138 { |
|
139 struct ipipe_domain_attr attr; //ipipe |
|
140 #if 1 |
|
141 ec_slave_t *slave; |
|
142 #endif |
|
143 |
|
144 // Als allererstes die RT-Lib initialisieren |
|
145 if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_globals_register) < 0) { |
|
146 printk(KERN_ERR "Failed to initialize rtlib!\n"); |
|
147 goto out_return; |
|
148 } |
|
149 |
|
150 if ((master = ecrt_request_master(0)) == NULL) { |
|
151 printk(KERN_ERR "Failed to request master 0!\n"); |
|
152 goto out_msr_cleanup; |
|
153 } |
|
154 |
|
155 //ecrt_master_print(master, 2); |
|
156 |
|
157 printk(KERN_INFO "Creating domains...\n"); |
|
158 if (!(domain1 = ecrt_master_create_domain(master))) { |
|
159 printk(KERN_ERR "Failed to create domains!\n"); |
|
160 goto out_release_master; |
|
161 } |
|
162 |
|
163 printk(KERN_INFO "Registering domain fields...\n"); |
|
164 if (ecrt_domain_register_field_list(domain1, domain1_fields)) { |
|
165 printk(KERN_ERR "Failed to register domain fields.\n"); |
|
166 goto out_release_master; |
|
167 } |
|
168 |
|
169 printk(KERN_INFO "Activating master...\n"); |
|
170 if (ecrt_master_activate(master)) { |
|
171 printk(KERN_ERR "Could not activate master!\n"); |
|
172 goto out_release_master; |
|
173 } |
|
174 |
|
175 #if 0 |
|
176 if (ecrt_master_fetch_sdo_lists(master)) { |
|
177 printk(KERN_ERR "Failed to fetch SDO lists!\n"); |
|
178 goto out_deactivate; |
|
179 } |
|
180 ecrt_master_print(master, 2); |
|
181 #else |
|
182 ecrt_master_print(master, 1); |
|
183 #endif |
|
184 |
|
185 #if 1 |
|
186 if (!(slave = ecrt_master_get_slave(master, "5"))) { |
|
187 printk(KERN_ERR "Failed to get slave!\n"); |
|
188 goto out_deactivate; |
|
189 } |
|
190 |
|
191 if ( |
|
192 ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 1) || // disable frame error bit |
|
193 ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 0) || // power failure bit |
|
194 ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || // inhibit time |
|
195 ecrt_slave_sdo_write_exp8(slave, 0x4061, 4, 0) || // test mode |
|
196 ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 1) || // dualcode |
|
197 ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 5) || // 125kbaud |
|
198 ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || // single-turn |
|
199 ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size |
|
200 ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length |
|
201 ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 30000) // inhibit time in us |
|
202 ) { |
|
203 printk(KERN_ERR "Failed to configure SSI slave!\n"); |
|
204 goto out_deactivate; |
|
205 } |
|
206 #endif |
|
207 |
|
208 #ifdef ASYNC |
|
209 // Einmal senden und warten... |
|
210 ecrt_master_prepare_async_io(master); |
|
211 #endif |
|
212 |
|
213 ipipe_init_attr(&attr); |
|
214 attr.name = "IPIPE-MSR-MODULE"; |
|
215 attr.priority = IPIPE_ROOT_PRIO + 1; |
|
216 attr.entry = &domain_entry; |
|
217 ipipe_register_domain(&this_domain, &attr); |
|
218 return 0; |
|
219 |
|
220 #if 1 |
|
221 out_deactivate: |
|
222 ecrt_master_deactivate(master); |
|
223 #endif |
|
224 out_release_master: |
|
225 ecrt_release_master(master); |
|
226 out_msr_cleanup: |
|
227 msr_rtlib_cleanup(); |
|
228 out_return: |
|
229 return -1; |
|
230 } |
|
231 |
|
232 /*****************************************************************************/ |
|
233 |
|
234 void __exit cleanup_rt_module(void) |
|
235 { |
|
236 printk(KERN_INFO "Cleanign up rt module...\n"); |
|
237 |
|
238 ipipe_tune_timer(1000000000UL / HZ, 0); // Alten Timertakt wiederherstellen |
|
239 ipipe_unregister_domain(&this_domain); |
|
240 |
|
241 printk(KERN_INFO "=== Stopping EtherCAT environment... ===\n"); |
|
242 ecrt_master_deactivate(master); |
|
243 ecrt_release_master(master); |
|
244 printk(KERN_INFO "=== EtherCAT environment stopped. ===\n"); |
|
245 |
|
246 msr_rtlib_cleanup(); |
|
247 } |
|
248 |
|
249 /*****************************************************************************/ |
|
250 |
|
251 #define EC_LIT(X) #X |
|
252 #define EC_STR(X) EC_LIT(X) |
|
253 #define COMPILE_INFO "Revision " EC_STR(SVNREV) \ |
|
254 ", compiled by " EC_STR(USER) \ |
|
255 " at " __DATE__ " " __TIME__ |
|
256 |
|
257 MODULE_LICENSE("GPL"); |
|
258 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
259 MODULE_DESCRIPTION ("EtherCAT real-time test environment"); |
|
260 MODULE_VERSION(COMPILE_INFO); |
|
261 |
|
262 module_init(init_rt_module); |
|
263 module_exit(cleanup_rt_module); |
|
264 |
|
265 /*****************************************************************************/ |
|
266 |
|
267 /* Emacs-Konfiguration |
|
268 ;;; Local Variables: *** |
|
269 ;;; c-basic-offset:4 *** |
|
270 ;;; End: *** |
|
271 */ |