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