1 /****************************************************************************** |
|
2 * |
|
3 * $Id$ |
|
4 * |
|
5 * Copyright (C) 2006 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 |
|
10 * and/or modify it under the terms of the GNU General Public License |
|
11 * as published by the Free Software Foundation; either version 2 of the |
|
12 * License, or (at your option) any later version. |
|
13 * |
|
14 * The IgH EtherCAT Master is distributed in the hope that it will be |
|
15 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 * GNU General Public License for more details. |
|
18 * |
|
19 * You should have received a copy of the GNU General Public License |
|
20 * along with the IgH EtherCAT Master; if not, write to the Free Software |
|
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
22 * |
|
23 * The right to use EtherCAT Technology is granted and comes free of |
|
24 * charge under condition of compatibility of product made by |
|
25 * Licensee. People intending to distribute/sell products based on the |
|
26 * code, have to sign an agreement to guarantee that products using |
|
27 * software based on IgH EtherCAT master stay compatible with the actual |
|
28 * EtherCAT specification (which are released themselves as an open |
|
29 * standard) as the (only) precondition to have the right to use EtherCAT |
|
30 * Technology, IP and trade marks. |
|
31 * |
|
32 *****************************************************************************/ |
|
33 |
|
34 // Linux |
|
35 #include <linux/module.h> |
|
36 |
|
37 // RTAI |
|
38 #include "rtai_sched.h" |
|
39 #include "rtai_sem.h" |
|
40 |
|
41 // RT_lib |
|
42 #include <msr_main.h> |
|
43 #include <msr_reg.h> |
|
44 #include <msr_time.h> |
|
45 |
|
46 // EtherCAT |
|
47 #include "../../include/ecrt.h" |
|
48 #include "../../include/ecdb.h" |
|
49 |
|
50 #define MSR_ABTASTFREQUENZ 1000 |
|
51 |
|
52 #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ) |
|
53 #define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ) |
|
54 |
|
55 /*****************************************************************************/ |
|
56 |
|
57 // RTAI |
|
58 RT_TASK task; |
|
59 SEM master_sem; |
|
60 cycles_t t_start = 0, t_critical; |
|
61 |
|
62 // EtherCAT |
|
63 ec_master_t *master = NULL; |
|
64 ec_domain_t *domain1 = NULL; |
|
65 |
|
66 // raw process data |
|
67 void *r_ana_out; |
|
68 |
|
69 // channels |
|
70 double k_ana_out; |
|
71 |
|
72 ec_pdo_reg_t domain1_pdos[] = { |
|
73 {"3", Beckhoff_EL4132_Output1, &r_ana_out}, |
|
74 {} |
|
75 }; |
|
76 |
|
77 /*****************************************************************************/ |
|
78 |
|
79 void msr_controller_run(void) |
|
80 { |
|
81 // receive |
|
82 rt_sem_wait(&master_sem); |
|
83 ecrt_master_receive(master); |
|
84 ecrt_domain_process(domain1); |
|
85 rt_sem_signal(&master_sem); |
|
86 |
|
87 // Process data |
|
88 EC_WRITE_S16(r_ana_out, k_ana_out / 10.0 * 0x7FFF); |
|
89 |
|
90 // Send |
|
91 rt_sem_wait(&master_sem); |
|
92 ecrt_domain_queue(domain1); |
|
93 ecrt_master_run(master); |
|
94 ecrt_master_send(master); |
|
95 rt_sem_signal(&master_sem); |
|
96 |
|
97 msr_write_kanal_list(); |
|
98 } |
|
99 |
|
100 /*****************************************************************************/ |
|
101 |
|
102 void msr_run(long data) |
|
103 { |
|
104 while (1) { |
|
105 t_start = get_cycles(); |
|
106 MSR_RTAITHREAD_CODE(msr_controller_run();); |
|
107 rt_task_wait_period(); |
|
108 } |
|
109 } |
|
110 |
|
111 /*****************************************************************************/ |
|
112 |
|
113 int msr_reg(void) |
|
114 { |
|
115 msr_reg_kanal("/ana_out", "", &k_ana_out, TDBL); |
|
116 return 0; |
|
117 } |
|
118 |
|
119 /*****************************************************************************/ |
|
120 |
|
121 int request_lock(void *data) |
|
122 { |
|
123 // too close to the next RT cycle: deny access... |
|
124 if (get_cycles() - t_start > t_critical) return -1; |
|
125 |
|
126 // allow access |
|
127 rt_sem_wait(&master_sem); |
|
128 return 0; |
|
129 } |
|
130 |
|
131 /*****************************************************************************/ |
|
132 |
|
133 void release_lock(void *data) |
|
134 { |
|
135 rt_sem_signal(&master_sem); |
|
136 } |
|
137 |
|
138 /*****************************************************************************/ |
|
139 |
|
140 int __init init_mod(void) |
|
141 { |
|
142 RTIME ticks; |
|
143 |
|
144 printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n"); |
|
145 |
|
146 rt_sem_init(&master_sem, 1); |
|
147 t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80% |
|
148 |
|
149 if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) { |
|
150 printk(KERN_ERR "Failed to initialize rtlib!\n"); |
|
151 goto out_return; |
|
152 } |
|
153 |
|
154 if (!(master = ecrt_request_master(0))) { |
|
155 printk(KERN_ERR "Failed to request master 0!\n"); |
|
156 goto out_msr_cleanup; |
|
157 } |
|
158 |
|
159 ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
160 |
|
161 printk(KERN_INFO "Creating domains...\n"); |
|
162 if (!(domain1 = ecrt_master_create_domain(master))) { |
|
163 printk(KERN_ERR "Failed to create domains!\n"); |
|
164 goto out_release_master; |
|
165 } |
|
166 |
|
167 printk(KERN_INFO "Registering Pdos...\n"); |
|
168 if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) { |
|
169 printk(KERN_ERR "Failed to register Pdos.\n"); |
|
170 goto out_release_master; |
|
171 } |
|
172 |
|
173 printk(KERN_INFO "Activating master...\n"); |
|
174 if (ecrt_master_activate(master)) { |
|
175 printk(KERN_ERR "Could not activate master!\n"); |
|
176 goto out_release_master; |
|
177 } |
|
178 |
|
179 printk("Starting cyclic sample thread...\n"); |
|
180 ticks = start_rt_timer(nano2count(TIMERTICKS)); |
|
181 if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) { |
|
182 printk(KERN_ERR "Failed to init RTAI task!\n"); |
|
183 goto out_stop_timer; |
|
184 } |
|
185 if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) { |
|
186 printk(KERN_ERR "Failed to run RTAI task!\n"); |
|
187 goto out_stop_task; |
|
188 } |
|
189 |
|
190 printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n"); |
|
191 return 0; |
|
192 |
|
193 out_stop_task: |
|
194 rt_task_delete(&task); |
|
195 out_stop_timer: |
|
196 stop_rt_timer(); |
|
197 out_release_master: |
|
198 ecrt_release_master(master); |
|
199 out_msr_cleanup: |
|
200 msr_rtlib_cleanup(); |
|
201 out_return: |
|
202 rt_sem_delete(&master_sem); |
|
203 return -1; |
|
204 } |
|
205 |
|
206 /*****************************************************************************/ |
|
207 |
|
208 void __exit cleanup_mod(void) |
|
209 { |
|
210 printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n"); |
|
211 |
|
212 rt_task_delete(&task); |
|
213 stop_rt_timer(); |
|
214 ecrt_release_master(master); |
|
215 rt_sem_delete(&master_sem); |
|
216 msr_rtlib_cleanup(); |
|
217 |
|
218 printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n"); |
|
219 } |
|
220 |
|
221 /*****************************************************************************/ |
|
222 |
|
223 MODULE_LICENSE("GPL"); |
|
224 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>"); |
|
225 MODULE_DESCRIPTION("EtherCAT RTAI MSR sample module"); |
|
226 |
|
227 module_init(init_mod); |
|
228 module_exit(cleanup_mod); |
|
229 |
|
230 /*****************************************************************************/ |
|