author | Florian Pose <fp@igh-essen.com> |
Fri, 03 Aug 2007 13:31:13 +0000 | |
changeset 674 | 34e5601b2640 |
parent 512 | 24292123d174 |
child 814 | a51f857b1b2d |
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 |
|
512
24292123d174
Shorter critical sections in examples.
Florian Pose <fp@igh-essen.com>
parents:
509
diff
changeset
|
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 |
{ |
|
512
24292123d174
Shorter critical sections in examples.
Florian Pose <fp@igh-essen.com>
parents:
509
diff
changeset
|
81 |
// receive |
234 | 82 |
rt_sem_wait(&master_sem); |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
83 |
ecrt_master_receive(master); |
234 | 84 |
ecrt_domain_process(domain1); |
512
24292123d174
Shorter critical sections in examples.
Florian Pose <fp@igh-essen.com>
parents:
509
diff
changeset
|
85 |
rt_sem_signal(&master_sem); |
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 |
512
24292123d174
Shorter critical sections in examples.
Florian Pose <fp@igh-essen.com>
parents:
509
diff
changeset
|
91 |
rt_sem_wait(&master_sem); |
509
87baea61d0c7
Adjusted examples to 1.2.0 interface changes.
Florian Pose <fp@igh-essen.com>
parents:
452
diff
changeset
|
92 |
ecrt_domain_queue(domain1); |
234 | 93 |
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
|
94 |
ecrt_master_send(master); |
234 | 95 |
rt_sem_signal(&master_sem); |
96 |
||
97 |
msr_write_kanal_list(); |
|
98 |
} |
|
99 |
||
100 |
/*****************************************************************************/ |
|
101 |
||
232 | 102 |
void msr_run(long data) |
103 |
{ |
|
234 | 104 |
while (1) { |
232 | 105 |
t_start = get_cycles(); |
234 | 106 |
MSR_RTAITHREAD_CODE(msr_controller_run();); |
232 | 107 |
rt_task_wait_period(); |
108 |
} |
|
109 |
} |
|
110 |
||
111 |
/*****************************************************************************/ |
|
112 |
||
113 |
int msr_reg(void) |
|
114 |
{ |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
115 |
msr_reg_kanal("/ana_out", "", &k_ana_out, TDBL); |
232 | 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 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
154 |
if (!(master = ecrt_request_master(0))) { |
232 | 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 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
167 |
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
|
168 |
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
|
169 |
printk(KERN_ERR "Failed to register PDOs.\n"); |
232 | 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"); |
|
512
24292123d174
Shorter critical sections in examples.
Florian Pose <fp@igh-essen.com>
parents:
509
diff
changeset
|
224 |
MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>"); |
24292123d174
Shorter critical sections in examples.
Florian Pose <fp@igh-essen.com>
parents:
509
diff
changeset
|
225 |
MODULE_DESCRIPTION("EtherCAT RTAI MSR sample module"); |
232 | 226 |
|
227 |
module_init(init_mod); |
|
228 |
module_exit(cleanup_mod); |
|
229 |
||
230 |
/*****************************************************************************/ |