author | Florian Pose <fp@igh-essen.com> |
Mon, 25 Sep 2006 15:55:46 +0000 | |
changeset 370 | 148155bb9abc |
parent 325 | 7833cf70c4f2 |
child 449 | 3caf8ff4d8a2 |
child 1732 | 1cc865ba17c2 |
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" |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
51 |
#include "../../include/ecdb.h" |
232 | 52 |
|
53 |
#define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ) |
|
54 |
#define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ) |
|
55 |
||
56 |
/*****************************************************************************/ |
|
57 |
||
58 |
// RTAI |
|
59 |
RT_TASK task; |
|
60 |
SEM master_sem; |
|
61 |
cycles_t t_start = 0, t_critical; |
|
62 |
||
63 |
// EtherCAT |
|
64 |
ec_master_t *master = NULL; |
|
65 |
ec_domain_t *domain1 = NULL; |
|
66 |
||
234 | 67 |
// raw process data |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
68 |
void *r_ana_out; |
232 | 69 |
|
70 |
// Channels |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
71 |
double k_ana_out; |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
72 |
|
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
73 |
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
|
74 |
{"3", Beckhoff_EL4132_Output1, &r_ana_out}, |
232 | 75 |
{} |
76 |
}; |
|
77 |
||
78 |
/*****************************************************************************/ |
|
79 |
||
234 | 80 |
void msr_controller_run(void) |
81 |
{ |
|
82 |
rt_sem_wait(&master_sem); |
|
83 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
84 |
// receive |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
85 |
ecrt_master_receive(master); |
234 | 86 |
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
|
87 |
|
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
88 |
// Process data |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
89 |
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
|
90 |
|
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
91 |
// Send |
234 | 92 |
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
|
93 |
ecrt_master_send(master); |
234 | 94 |
|
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 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
179 |
ecrt_master_prepare(master); |
232 | 180 |
|
181 |
printk("Starting cyclic sample thread...\n"); |
|
182 |
ticks = start_rt_timer(nano2count(TIMERTICKS)); |
|
183 |
if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) { |
|
184 |
printk(KERN_ERR "Failed to init RTAI task!\n"); |
|
185 |
goto out_stop_timer; |
|
186 |
} |
|
187 |
if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) { |
|
188 |
printk(KERN_ERR "Failed to run RTAI task!\n"); |
|
189 |
goto out_stop_task; |
|
190 |
} |
|
191 |
||
192 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n"); |
|
193 |
return 0; |
|
194 |
||
195 |
out_stop_task: |
|
196 |
rt_task_delete(&task); |
|
197 |
out_stop_timer: |
|
198 |
stop_rt_timer(); |
|
199 |
ecrt_master_deactivate(master); |
|
200 |
out_release_master: |
|
201 |
ecrt_release_master(master); |
|
202 |
out_msr_cleanup: |
|
203 |
msr_rtlib_cleanup(); |
|
204 |
out_return: |
|
205 |
rt_sem_delete(&master_sem); |
|
206 |
return -1; |
|
207 |
} |
|
208 |
||
209 |
/*****************************************************************************/ |
|
210 |
||
211 |
void __exit cleanup_mod(void) |
|
212 |
{ |
|
213 |
printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n"); |
|
214 |
||
215 |
rt_task_delete(&task); |
|
216 |
stop_rt_timer(); |
|
217 |
ecrt_master_deactivate(master); |
|
218 |
ecrt_release_master(master); |
|
219 |
rt_sem_delete(&master_sem); |
|
220 |
msr_rtlib_cleanup(); |
|
221 |
||
222 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n"); |
|
223 |
} |
|
224 |
||
225 |
/*****************************************************************************/ |
|
226 |
||
227 |
MODULE_LICENSE("GPL"); |
|
228 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
229 |
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module"); |
|
230 |
||
231 |
module_init(init_mod); |
|
232 |
module_exit(cleanup_mod); |
|
233 |
||
234 |
/*****************************************************************************/ |