author | Florian Pose <fp@igh-essen.com> |
Wed, 19 Jul 2006 13:15:02 +0000 | |
changeset 306 | 45886de3db87 |
parent 246 | 0bf7c769de06 |
child 325 | 7833cf70c4f2 |
permissions | -rw-r--r-- |
232 | 1 |
/****************************************************************************** |
2 |
* |
|
3 |
* Sample module for use with IgH MSR library. |
|
4 |
* |
|
5 |
* $Id$ |
|
6 |
* |
|
7 |
* Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH |
|
8 |
* |
|
9 |
* This file is part of the IgH EtherCAT Master. |
|
10 |
* |
|
11 |
* The IgH EtherCAT Master is free software; you can redistribute it |
|
12 |
* and/or modify it under the terms of the GNU General Public License |
|
246
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
13 |
* as published by the Free Software Foundation; either version 2 of the |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
14 |
* License, or (at your option) any later version. |
232 | 15 |
* |
16 |
* The IgH EtherCAT Master is distributed in the hope that it will be |
|
17 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 |
* GNU General Public License for more details. |
|
20 |
* |
|
21 |
* You should have received a copy of the GNU General Public License |
|
22 |
* along with the IgH EtherCAT Master; if not, write to the Free Software |
|
23 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
24 |
* |
|
246
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
25 |
* The right to use EtherCAT Technology is granted and comes free of |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
26 |
* charge under condition of compatibility of product made by |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
27 |
* Licensee. People intending to distribute/sell products based on the |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
28 |
* code, have to sign an agreement to guarantee that products using |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
29 |
* software based on IgH EtherCAT master stay compatible with the actual |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
30 |
* EtherCAT specification (which are released themselves as an open |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
31 |
* standard) as the (only) precondition to have the right to use EtherCAT |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
32 |
* Technology, IP and trade marks. |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
33 |
* |
232 | 34 |
*****************************************************************************/ |
35 |
||
36 |
// Linux |
|
37 |
#include <linux/module.h> |
|
38 |
||
39 |
// RTAI |
|
40 |
#include "rtai_sched.h" |
|
41 |
#include "rtai_sem.h" |
|
42 |
||
43 |
// RT_lib |
|
44 |
#include <msr_main.h> |
|
45 |
#include <msr_reg.h> |
|
234 | 46 |
#include <msr_time.h> |
232 | 47 |
#include "msr_param.h" |
48 |
||
49 |
// EtherCAT |
|
50 |
#include "../../include/ecrt.h" |
|
51 |
||
52 |
#define ASYNC |
|
53 |
||
54 |
#define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ) |
|
55 |
#define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ) |
|
56 |
||
57 |
/*****************************************************************************/ |
|
58 |
||
59 |
// RTAI |
|
60 |
RT_TASK task; |
|
61 |
SEM master_sem; |
|
62 |
cycles_t t_start = 0, t_critical; |
|
63 |
||
64 |
// EtherCAT |
|
65 |
ec_master_t *master = NULL; |
|
66 |
ec_domain_t *domain1 = NULL; |
|
67 |
||
234 | 68 |
// raw process data |
232 | 69 |
void *r_ssi; |
70 |
void *r_ssi_st; |
|
71 |
||
72 |
// Channels |
|
73 |
uint32_t k_ssi; |
|
74 |
uint32_t k_ssi_st; |
|
75 |
||
76 |
ec_field_init_t domain1_fields[] = { |
|
77 |
{&r_ssi, "0:3", "Beckhoff", "EL5001", "InputValue", 0}, |
|
78 |
{&r_ssi_st, "0:3", "Beckhoff", "EL5001", "Status", 0}, |
|
79 |
{} |
|
80 |
}; |
|
81 |
||
82 |
/*****************************************************************************/ |
|
83 |
||
234 | 84 |
void msr_controller_run(void) |
85 |
{ |
|
86 |
rt_sem_wait(&master_sem); |
|
87 |
||
88 |
#ifdef ASYNC |
|
89 |
// Empfangen |
|
90 |
ecrt_master_async_receive(master); |
|
91 |
ecrt_domain_process(domain1); |
|
92 |
#else |
|
93 |
// Senden und empfangen |
|
94 |
ecrt_domain_queue(domain1); |
|
95 |
ecrt_master_run(master); |
|
96 |
ecrt_master_sync_io(master); |
|
97 |
ecrt_domain_process(domain1); |
|
98 |
#endif |
|
99 |
||
100 |
// Prozessdaten verarbeiten |
|
101 |
k_ssi = EC_READ_U32(r_ssi); |
|
102 |
k_ssi_st = EC_READ_U8 (r_ssi_st); |
|
103 |
||
104 |
#ifdef ASYNC |
|
105 |
// Senden |
|
106 |
ecrt_domain_queue(domain1); |
|
107 |
ecrt_master_run(master); |
|
108 |
ecrt_master_async_send(master); |
|
109 |
#endif |
|
110 |
||
111 |
rt_sem_signal(&master_sem); |
|
112 |
||
113 |
msr_write_kanal_list(); |
|
114 |
} |
|
115 |
||
116 |
/*****************************************************************************/ |
|
117 |
||
232 | 118 |
void msr_run(long data) |
119 |
{ |
|
234 | 120 |
while (1) { |
232 | 121 |
t_start = get_cycles(); |
234 | 122 |
MSR_RTAITHREAD_CODE(msr_controller_run();); |
232 | 123 |
rt_task_wait_period(); |
124 |
} |
|
125 |
} |
|
126 |
||
127 |
/*****************************************************************************/ |
|
128 |
||
129 |
int msr_reg(void) |
|
130 |
{ |
|
131 |
msr_reg_kanal("/ssi_position", "", &k_ssi, TUINT); |
|
132 |
msr_reg_kanal("/ssi_status", "", &k_ssi_st, TUINT); |
|
133 |
return 0; |
|
134 |
} |
|
135 |
||
136 |
/*****************************************************************************/ |
|
137 |
||
138 |
int request_lock(void *data) |
|
139 |
{ |
|
140 |
// too close to the next RT cycle: deny access... |
|
141 |
if (get_cycles() - t_start > t_critical) return -1; |
|
142 |
||
143 |
// allow access |
|
144 |
rt_sem_wait(&master_sem); |
|
145 |
return 0; |
|
146 |
} |
|
147 |
||
148 |
/*****************************************************************************/ |
|
149 |
||
150 |
void release_lock(void *data) |
|
151 |
{ |
|
152 |
rt_sem_signal(&master_sem); |
|
153 |
} |
|
154 |
||
155 |
/*****************************************************************************/ |
|
156 |
||
157 |
int __init init_mod(void) |
|
158 |
{ |
|
159 |
RTIME ticks; |
|
160 |
#if 0 |
|
161 |
ec_slave_t *slave; |
|
162 |
#endif |
|
163 |
||
164 |
printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n"); |
|
165 |
||
166 |
rt_sem_init(&master_sem, 1); |
|
167 |
t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80% |
|
168 |
||
169 |
if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) { |
|
170 |
printk(KERN_ERR "Failed to initialize rtlib!\n"); |
|
171 |
goto out_return; |
|
172 |
} |
|
173 |
||
174 |
if ((master = ecrt_request_master(0)) == NULL) { |
|
175 |
printk(KERN_ERR "Failed to request master 0!\n"); |
|
176 |
goto out_msr_cleanup; |
|
177 |
} |
|
178 |
||
179 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
180 |
||
181 |
printk(KERN_INFO "Creating domains...\n"); |
|
182 |
if (!(domain1 = ecrt_master_create_domain(master))) { |
|
183 |
printk(KERN_ERR "Failed to create domains!\n"); |
|
184 |
goto out_release_master; |
|
185 |
} |
|
186 |
||
187 |
printk(KERN_INFO "Registering domain fields...\n"); |
|
188 |
if (ecrt_domain_register_field_list(domain1, domain1_fields)) { |
|
189 |
printk(KERN_ERR "Failed to register domain fields.\n"); |
|
190 |
goto out_release_master; |
|
191 |
} |
|
192 |
||
193 |
printk(KERN_INFO "Activating master...\n"); |
|
194 |
if (ecrt_master_activate(master)) { |
|
195 |
printk(KERN_ERR "Could not activate master!\n"); |
|
196 |
goto out_release_master; |
|
197 |
} |
|
198 |
||
199 |
#if 0 |
|
200 |
if (ecrt_master_fetch_sdo_lists(master)) { |
|
201 |
printk(KERN_ERR "Failed to fetch SDO lists!\n"); |
|
202 |
goto out_deactivate; |
|
203 |
} |
|
204 |
ecrt_master_print(master, 2); |
|
205 |
#else |
|
206 |
ecrt_master_print(master, 0); |
|
207 |
#endif |
|
208 |
||
209 |
#if 0 |
|
210 |
if (!(slave = ecrt_master_get_slave(master, "0:3"))) { |
|
211 |
printk(KERN_ERR "Failed to get slave!\n"); |
|
212 |
goto out_deactivate; |
|
213 |
} |
|
214 |
||
215 |
if ( |
|
216 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 1) || // disable frame error bit |
|
217 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 0) || // power failure bit |
|
218 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || // inhibit time |
|
219 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 4, 0) || // test mode |
|
240
e55b0572933c
SSI sensor changes in MSR example
Florian Pose <fp@igh-essen.com>
parents:
234
diff
changeset
|
220 |
ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || // graycode |
232 | 221 |
ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 5) || // 125kbaud |
222 |
ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || // single-turn |
|
223 |
ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size |
|
224 |
ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length |
|
240
e55b0572933c
SSI sensor changes in MSR example
Florian Pose <fp@igh-essen.com>
parents:
234
diff
changeset
|
225 |
ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 50) // inhibit time in us |
232 | 226 |
) { |
227 |
printk(KERN_ERR "Failed to configure SSI slave!\n"); |
|
228 |
goto out_deactivate; |
|
229 |
} |
|
230 |
#endif |
|
231 |
||
232 |
#if 0 |
|
233 |
if (!(slave = ecrt_master_get_slave(master, "1:0"))) { |
|
234 |
printk(KERN_ERR "Failed to get slave!\n"); |
|
235 |
goto out_deactivate; |
|
236 |
} |
|
237 |
if (ecrt_slave_write_alias(slave, 0x5678)) { |
|
238 |
printk(KERN_ERR "Failed to write alias!\n"); |
|
239 |
goto out_deactivate; |
|
240 |
} |
|
241 |
#endif |
|
242 |
||
243 |
#ifdef ASYNC |
|
244 |
// Einmal senden und warten... |
|
245 |
ecrt_master_prepare_async_io(master); |
|
246 |
#endif |
|
247 |
||
248 |
if (ecrt_master_start_eoe(master)) { |
|
249 |
printk(KERN_ERR "Failed to start EoE processing!\n"); |
|
250 |
goto out_deactivate; |
|
251 |
} |
|
252 |
||
253 |
printk("Starting cyclic sample thread...\n"); |
|
254 |
ticks = start_rt_timer(nano2count(TIMERTICKS)); |
|
255 |
if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) { |
|
256 |
printk(KERN_ERR "Failed to init RTAI task!\n"); |
|
257 |
goto out_stop_timer; |
|
258 |
} |
|
259 |
if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) { |
|
260 |
printk(KERN_ERR "Failed to run RTAI task!\n"); |
|
261 |
goto out_stop_task; |
|
262 |
} |
|
263 |
||
264 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n"); |
|
265 |
return 0; |
|
266 |
||
267 |
out_stop_task: |
|
268 |
rt_task_delete(&task); |
|
269 |
out_stop_timer: |
|
270 |
stop_rt_timer(); |
|
271 |
out_deactivate: |
|
272 |
ecrt_master_deactivate(master); |
|
273 |
out_release_master: |
|
274 |
ecrt_release_master(master); |
|
275 |
out_msr_cleanup: |
|
276 |
msr_rtlib_cleanup(); |
|
277 |
out_return: |
|
278 |
rt_sem_delete(&master_sem); |
|
279 |
return -1; |
|
280 |
} |
|
281 |
||
282 |
/*****************************************************************************/ |
|
283 |
||
284 |
void __exit cleanup_mod(void) |
|
285 |
{ |
|
286 |
printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n"); |
|
287 |
||
288 |
rt_task_delete(&task); |
|
289 |
stop_rt_timer(); |
|
290 |
ecrt_master_deactivate(master); |
|
291 |
ecrt_release_master(master); |
|
292 |
rt_sem_delete(&master_sem); |
|
293 |
msr_rtlib_cleanup(); |
|
294 |
||
295 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n"); |
|
296 |
} |
|
297 |
||
298 |
/*****************************************************************************/ |
|
299 |
||
300 |
MODULE_LICENSE("GPL"); |
|
301 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
302 |
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module"); |
|
303 |
||
304 |
module_init(init_mod); |
|
305 |
module_exit(cleanup_mod); |
|
306 |
||
307 |
/*****************************************************************************/ |