author | Florian Pose <fp@igh-essen.com> |
Fri, 27 Oct 2006 09:57:30 +0000 | |
changeset 452 | d8ae13a5a910 |
parent 451 | cd6b819c2351 |
child 509 | 87baea61d0c7 |
permissions | -rw-r--r-- |
232 | 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 |
|
246
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
11 |
* 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
|
12 |
* License, or (at your option) any later version. |
232 | 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 |
* |
|
246
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
23 |
* 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
|
24 |
* 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
|
25 |
* 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
|
26 |
* 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
|
27 |
* 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
|
28 |
* 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
|
29 |
* 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
|
30 |
* Technology, IP and trade marks. |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
240
diff
changeset
|
31 |
* |
232 | 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> |
|
234 | 44 |
#include <msr_time.h> |
232 | 45 |
|
46 |
// EtherCAT |
|
47 |
#include "../../include/ecrt.h" |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
48 |
#include "../../include/ecdb.h" |
232 | 49 |
|
451
cd6b819c2351
MSR example: Replaced msr_load and msr_unload with init script, removed msr_param.h
Florian Pose <fp@igh-essen.com>
parents:
449
diff
changeset
|
50 |
#define MSR_ABTASTFREQUENZ 1000 |
cd6b819c2351
MSR example: Replaced msr_load and msr_unload with init script, removed msr_param.h
Florian Pose <fp@igh-essen.com>
parents:
449
diff
changeset
|
51 |
|
232 | 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 |
||
234 | 66 |
// raw process data |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
67 |
void *r_ana_out; |
232 | 68 |
|
69 |
// Channels |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
70 |
double k_ana_out; |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
71 |
|
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
72 |
ec_pdo_reg_t domain1_pdos[] = { |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
73 |
{"3", Beckhoff_EL4132_Output1, &r_ana_out}, |
232 | 74 |
{} |
75 |
}; |
|
76 |
||
77 |
/*****************************************************************************/ |
|
78 |
||
234 | 79 |
void msr_controller_run(void) |
80 |
{ |
|
81 |
rt_sem_wait(&master_sem); |
|
82 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
83 |
// receive |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
84 |
ecrt_master_receive(master); |
234 | 85 |
ecrt_domain_process(domain1); |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
86 |
|
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
87 |
// Process data |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
88 |
EC_WRITE_S16(r_ana_out, k_ana_out / 10.0 * 0x7FFF); |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
89 |
|
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
90 |
// Send |
234 | 91 |
ecrt_master_run(master); |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
92 |
ecrt_master_send(master); |
234 | 93 |
|
94 |
rt_sem_signal(&master_sem); |
|
95 |
||
96 |
msr_write_kanal_list(); |
|
97 |
} |
|
98 |
||
99 |
/*****************************************************************************/ |
|
100 |
||
232 | 101 |
void msr_run(long data) |
102 |
{ |
|
234 | 103 |
while (1) { |
232 | 104 |
t_start = get_cycles(); |
234 | 105 |
MSR_RTAITHREAD_CODE(msr_controller_run();); |
232 | 106 |
rt_task_wait_period(); |
107 |
} |
|
108 |
} |
|
109 |
||
110 |
/*****************************************************************************/ |
|
111 |
||
112 |
int msr_reg(void) |
|
113 |
{ |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
114 |
msr_reg_kanal("/ana_out", "", &k_ana_out, TDBL); |
232 | 115 |
return 0; |
116 |
} |
|
117 |
||
118 |
/*****************************************************************************/ |
|
119 |
||
120 |
int request_lock(void *data) |
|
121 |
{ |
|
122 |
// too close to the next RT cycle: deny access... |
|
123 |
if (get_cycles() - t_start > t_critical) return -1; |
|
124 |
||
125 |
// allow access |
|
126 |
rt_sem_wait(&master_sem); |
|
127 |
return 0; |
|
128 |
} |
|
129 |
||
130 |
/*****************************************************************************/ |
|
131 |
||
132 |
void release_lock(void *data) |
|
133 |
{ |
|
134 |
rt_sem_signal(&master_sem); |
|
135 |
} |
|
136 |
||
137 |
/*****************************************************************************/ |
|
138 |
||
139 |
int __init init_mod(void) |
|
140 |
{ |
|
141 |
RTIME ticks; |
|
142 |
||
143 |
printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n"); |
|
144 |
||
145 |
rt_sem_init(&master_sem, 1); |
|
146 |
t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80% |
|
147 |
||
148 |
if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) { |
|
149 |
printk(KERN_ERR "Failed to initialize rtlib!\n"); |
|
150 |
goto out_return; |
|
151 |
} |
|
152 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
153 |
if (!(master = ecrt_request_master(0))) { |
232 | 154 |
printk(KERN_ERR "Failed to request master 0!\n"); |
155 |
goto out_msr_cleanup; |
|
156 |
} |
|
157 |
||
158 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
159 |
||
160 |
printk(KERN_INFO "Creating domains...\n"); |
|
161 |
if (!(domain1 = ecrt_master_create_domain(master))) { |
|
162 |
printk(KERN_ERR "Failed to create domains!\n"); |
|
163 |
goto out_release_master; |
|
164 |
} |
|
165 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
166 |
printk(KERN_INFO "Registering PDOs...\n"); |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
167 |
if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) { |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
168 |
printk(KERN_ERR "Failed to register PDOs.\n"); |
232 | 169 |
goto out_release_master; |
170 |
} |
|
171 |
||
172 |
printk(KERN_INFO "Activating master...\n"); |
|
173 |
if (ecrt_master_activate(master)) { |
|
174 |
printk(KERN_ERR "Could not activate master!\n"); |
|
175 |
goto out_release_master; |
|
176 |
} |
|
177 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
178 |
ecrt_master_prepare(master); |
232 | 179 |
|
180 |
printk("Starting cyclic sample thread...\n"); |
|
181 |
ticks = start_rt_timer(nano2count(TIMERTICKS)); |
|
182 |
if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) { |
|
183 |
printk(KERN_ERR "Failed to init RTAI task!\n"); |
|
184 |
goto out_stop_timer; |
|
185 |
} |
|
186 |
if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) { |
|
187 |
printk(KERN_ERR "Failed to run RTAI task!\n"); |
|
188 |
goto out_stop_task; |
|
189 |
} |
|
190 |
||
191 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n"); |
|
192 |
return 0; |
|
193 |
||
194 |
out_stop_task: |
|
195 |
rt_task_delete(&task); |
|
196 |
out_stop_timer: |
|
197 |
stop_rt_timer(); |
|
198 |
ecrt_master_deactivate(master); |
|
199 |
out_release_master: |
|
200 |
ecrt_release_master(master); |
|
201 |
out_msr_cleanup: |
|
202 |
msr_rtlib_cleanup(); |
|
203 |
out_return: |
|
204 |
rt_sem_delete(&master_sem); |
|
205 |
return -1; |
|
206 |
} |
|
207 |
||
208 |
/*****************************************************************************/ |
|
209 |
||
210 |
void __exit cleanup_mod(void) |
|
211 |
{ |
|
212 |
printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n"); |
|
213 |
||
214 |
rt_task_delete(&task); |
|
215 |
stop_rt_timer(); |
|
216 |
ecrt_release_master(master); |
|
217 |
rt_sem_delete(&master_sem); |
|
218 |
msr_rtlib_cleanup(); |
|
219 |
||
220 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n"); |
|
221 |
} |
|
222 |
||
223 |
/*****************************************************************************/ |
|
224 |
||
225 |
MODULE_LICENSE("GPL"); |
|
226 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
227 |
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module"); |
|
228 |
||
229 |
module_init(init_mod); |
|
230 |
module_exit(cleanup_mod); |
|
231 |
||
232 |
/*****************************************************************************/ |