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>
|
|
36 |
#include "msr_param.h"
|
|
37 |
|
|
38 |
// EtherCAT
|
|
39 |
#include "../../include/ecrt.h"
|
|
40 |
|
|
41 |
#define ASYNC
|
|
42 |
|
|
43 |
#define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
|
|
44 |
#define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ)
|
|
45 |
|
|
46 |
/*****************************************************************************/
|
|
47 |
|
|
48 |
// RTAI
|
|
49 |
RT_TASK task;
|
|
50 |
SEM master_sem;
|
|
51 |
cycles_t t_start = 0, t_critical;
|
|
52 |
|
|
53 |
// MSR
|
|
54 |
extern unsigned long msr_controller_execution_time;
|
|
55 |
extern unsigned long msr_controller_call_time;
|
|
56 |
extern wait_queue_head_t msr_read_waitqueue;
|
|
57 |
int count_wakeup = 0;
|
|
58 |
|
|
59 |
// EtherCAT
|
|
60 |
ec_master_t *master = NULL;
|
|
61 |
ec_domain_t *domain1 = NULL;
|
|
62 |
|
|
63 |
// Prozessdaten
|
|
64 |
void *r_ssi;
|
|
65 |
void *r_ssi_st;
|
|
66 |
|
|
67 |
// Channels
|
|
68 |
uint32_t k_ssi;
|
|
69 |
uint32_t k_ssi_st;
|
|
70 |
|
|
71 |
ec_field_init_t domain1_fields[] = {
|
|
72 |
{&r_ssi, "0:3", "Beckhoff", "EL5001", "InputValue", 0},
|
|
73 |
{&r_ssi_st, "0:3", "Beckhoff", "EL5001", "Status", 0},
|
|
74 |
{}
|
|
75 |
};
|
|
76 |
|
|
77 |
/*****************************************************************************/
|
|
78 |
|
|
79 |
void msr_run(long data)
|
|
80 |
{
|
|
81 |
cycles_t t_last_start;
|
|
82 |
|
|
83 |
while (1)
|
|
84 |
{
|
|
85 |
t_last_start = t_start;
|
|
86 |
t_start = get_cycles();
|
|
87 |
|
|
88 |
rt_sem_wait(&master_sem);
|
|
89 |
|
|
90 |
#ifdef ASYNC
|
|
91 |
// Empfangen
|
|
92 |
ecrt_master_async_receive(master);
|
|
93 |
ecrt_domain_process(domain1);
|
|
94 |
#else
|
|
95 |
// Senden und empfangen
|
|
96 |
ecrt_domain_queue(domain1);
|
|
97 |
ecrt_master_run(master);
|
|
98 |
ecrt_master_sync_io(master);
|
|
99 |
ecrt_domain_process(domain1);
|
|
100 |
#endif
|
|
101 |
|
|
102 |
// Prozessdaten verarbeiten
|
|
103 |
k_ssi = EC_READ_U32(r_ssi);
|
|
104 |
k_ssi_st = EC_READ_U8 (r_ssi_st);
|
|
105 |
|
|
106 |
#ifdef ASYNC
|
|
107 |
// Senden
|
|
108 |
ecrt_domain_queue(domain1);
|
|
109 |
ecrt_master_run(master);
|
|
110 |
ecrt_master_async_send(master);
|
|
111 |
#endif
|
|
112 |
|
|
113 |
rt_sem_signal(&master_sem);
|
|
114 |
|
|
115 |
/* write data to MSR ring buffers */
|
|
116 |
msr_write_kanal_list();
|
|
117 |
|
|
118 |
/* wake up MSR read queue */
|
|
119 |
if(++count_wakeup >= MSR_ABTASTFREQUENZ / 10) {
|
|
120 |
wake_up_interruptible(&msr_read_waitqueue);
|
|
121 |
count_wakeup = 0;
|
|
122 |
}
|
|
123 |
|
|
124 |
/* calculate timing */
|
|
125 |
msr_controller_execution_time =
|
|
126 |
(unsigned long) (get_cycles() - t_start) * 1000 / cpu_khz;
|
|
127 |
msr_controller_call_time =
|
|
128 |
(unsigned long) (t_start - t_last_start) * 1000 / cpu_khz;
|
|
129 |
|
|
130 |
rt_task_wait_period();
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
/*****************************************************************************/
|
|
135 |
|
|
136 |
int msr_reg(void)
|
|
137 |
{
|
|
138 |
msr_reg_kanal("/ssi_position", "", &k_ssi, TUINT);
|
|
139 |
msr_reg_kanal("/ssi_status", "", &k_ssi_st, TUINT);
|
|
140 |
return 0;
|
|
141 |
}
|
|
142 |
|
|
143 |
/*****************************************************************************/
|
|
144 |
|
|
145 |
int request_lock(void *data)
|
|
146 |
{
|
|
147 |
// too close to the next RT cycle: deny access...
|
|
148 |
if (get_cycles() - t_start > t_critical) return -1;
|
|
149 |
|
|
150 |
// allow access
|
|
151 |
rt_sem_wait(&master_sem);
|
|
152 |
return 0;
|
|
153 |
}
|
|
154 |
|
|
155 |
/*****************************************************************************/
|
|
156 |
|
|
157 |
void release_lock(void *data)
|
|
158 |
{
|
|
159 |
rt_sem_signal(&master_sem);
|
|
160 |
}
|
|
161 |
|
|
162 |
/*****************************************************************************/
|
|
163 |
|
|
164 |
int __init init_mod(void)
|
|
165 |
{
|
|
166 |
RTIME ticks;
|
|
167 |
#if 0
|
|
168 |
ec_slave_t *slave;
|
|
169 |
#endif
|
|
170 |
|
|
171 |
printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n");
|
|
172 |
|
|
173 |
rt_sem_init(&master_sem, 1);
|
|
174 |
t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80%
|
|
175 |
|
|
176 |
if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) {
|
|
177 |
printk(KERN_ERR "Failed to initialize rtlib!\n");
|
|
178 |
goto out_return;
|
|
179 |
}
|
|
180 |
|
|
181 |
if ((master = ecrt_request_master(0)) == NULL) {
|
|
182 |
printk(KERN_ERR "Failed to request master 0!\n");
|
|
183 |
goto out_msr_cleanup;
|
|
184 |
}
|
|
185 |
|
|
186 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL);
|
|
187 |
|
|
188 |
printk(KERN_INFO "Creating domains...\n");
|
|
189 |
if (!(domain1 = ecrt_master_create_domain(master))) {
|
|
190 |
printk(KERN_ERR "Failed to create domains!\n");
|
|
191 |
goto out_release_master;
|
|
192 |
}
|
|
193 |
|
|
194 |
printk(KERN_INFO "Registering domain fields...\n");
|
|
195 |
if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
|
|
196 |
printk(KERN_ERR "Failed to register domain fields.\n");
|
|
197 |
goto out_release_master;
|
|
198 |
}
|
|
199 |
|
|
200 |
printk(KERN_INFO "Activating master...\n");
|
|
201 |
if (ecrt_master_activate(master)) {
|
|
202 |
printk(KERN_ERR "Could not activate master!\n");
|
|
203 |
goto out_release_master;
|
|
204 |
}
|
|
205 |
|
|
206 |
#if 0
|
|
207 |
if (ecrt_master_fetch_sdo_lists(master)) {
|
|
208 |
printk(KERN_ERR "Failed to fetch SDO lists!\n");
|
|
209 |
goto out_deactivate;
|
|
210 |
}
|
|
211 |
ecrt_master_print(master, 2);
|
|
212 |
#else
|
|
213 |
ecrt_master_print(master, 0);
|
|
214 |
#endif
|
|
215 |
|
|
216 |
#if 0
|
|
217 |
if (!(slave = ecrt_master_get_slave(master, "0:3"))) {
|
|
218 |
printk(KERN_ERR "Failed to get slave!\n");
|
|
219 |
goto out_deactivate;
|
|
220 |
}
|
|
221 |
|
|
222 |
if (
|
|
223 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 1) || // disable frame error bit
|
|
224 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 0) || // power failure bit
|
|
225 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || // inhibit time
|
|
226 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 4, 0) || // test mode
|
|
227 |
ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 1) || // dualcode
|
|
228 |
ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 5) || // 125kbaud
|
|
229 |
ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || // single-turn
|
|
230 |
ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size
|
|
231 |
ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length
|
|
232 |
ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 30000) // inhibit time in us
|
|
233 |
) {
|
|
234 |
printk(KERN_ERR "Failed to configure SSI slave!\n");
|
|
235 |
goto out_deactivate;
|
|
236 |
}
|
|
237 |
#endif
|
|
238 |
|
|
239 |
#if 0
|
|
240 |
if (!(slave = ecrt_master_get_slave(master, "1:0"))) {
|
|
241 |
printk(KERN_ERR "Failed to get slave!\n");
|
|
242 |
goto out_deactivate;
|
|
243 |
}
|
|
244 |
if (ecrt_slave_write_alias(slave, 0x5678)) {
|
|
245 |
printk(KERN_ERR "Failed to write alias!\n");
|
|
246 |
goto out_deactivate;
|
|
247 |
}
|
|
248 |
#endif
|
|
249 |
|
|
250 |
#ifdef ASYNC
|
|
251 |
// Einmal senden und warten...
|
|
252 |
ecrt_master_prepare_async_io(master);
|
|
253 |
#endif
|
|
254 |
|
|
255 |
if (ecrt_master_start_eoe(master)) {
|
|
256 |
printk(KERN_ERR "Failed to start EoE processing!\n");
|
|
257 |
goto out_deactivate;
|
|
258 |
}
|
|
259 |
|
|
260 |
printk("Starting cyclic sample thread...\n");
|
|
261 |
ticks = start_rt_timer(nano2count(TIMERTICKS));
|
|
262 |
if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) {
|
|
263 |
printk(KERN_ERR "Failed to init RTAI task!\n");
|
|
264 |
goto out_stop_timer;
|
|
265 |
}
|
|
266 |
if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) {
|
|
267 |
printk(KERN_ERR "Failed to run RTAI task!\n");
|
|
268 |
goto out_stop_task;
|
|
269 |
}
|
|
270 |
|
|
271 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n");
|
|
272 |
return 0;
|
|
273 |
|
|
274 |
out_stop_task:
|
|
275 |
rt_task_delete(&task);
|
|
276 |
out_stop_timer:
|
|
277 |
stop_rt_timer();
|
|
278 |
out_deactivate:
|
|
279 |
ecrt_master_deactivate(master);
|
|
280 |
out_release_master:
|
|
281 |
ecrt_release_master(master);
|
|
282 |
out_msr_cleanup:
|
|
283 |
msr_rtlib_cleanup();
|
|
284 |
out_return:
|
|
285 |
rt_sem_delete(&master_sem);
|
|
286 |
return -1;
|
|
287 |
}
|
|
288 |
|
|
289 |
/*****************************************************************************/
|
|
290 |
|
|
291 |
void __exit cleanup_mod(void)
|
|
292 |
{
|
|
293 |
printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n");
|
|
294 |
|
|
295 |
rt_task_delete(&task);
|
|
296 |
stop_rt_timer();
|
|
297 |
ecrt_master_deactivate(master);
|
|
298 |
ecrt_release_master(master);
|
|
299 |
rt_sem_delete(&master_sem);
|
|
300 |
msr_rtlib_cleanup();
|
|
301 |
|
|
302 |
printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n");
|
|
303 |
}
|
|
304 |
|
|
305 |
/*****************************************************************************/
|
|
306 |
|
|
307 |
MODULE_LICENSE("GPL");
|
|
308 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
|
|
309 |
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module");
|
|
310 |
|
|
311 |
module_init(init_mod);
|
|
312 |
module_exit(cleanup_mod);
|
|
313 |
|
|
314 |
/*****************************************************************************/
|