220
|
1 |
/******************************************************************************
|
|
2 |
*
|
|
3 |
* m i n i . c
|
|
4 |
*
|
|
5 |
* Minimal module for EtherCAT.
|
|
6 |
*
|
|
7 |
* $Id$
|
|
8 |
*
|
|
9 |
* Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH
|
|
10 |
*
|
|
11 |
* This file is part of the IgH EtherCAT Master.
|
|
12 |
*
|
|
13 |
* The IgH EtherCAT Master is free software; you can redistribute it
|
|
14 |
* and/or modify it under the terms of the GNU General Public License
|
|
15 |
* as published by the Free Software Foundation; version 2 of the License.
|
|
16 |
*
|
|
17 |
* The IgH EtherCAT Master is distributed in the hope that it will be
|
|
18 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 |
* GNU General Public License for more details.
|
|
21 |
*
|
|
22 |
* You should have received a copy of the GNU General Public License
|
|
23 |
* along with the IgH EtherCAT Master; if not, write to the Free Software
|
|
24 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
25 |
*
|
|
26 |
*****************************************************************************/
|
|
27 |
|
|
28 |
#include <linux/module.h>
|
|
29 |
#include <linux/delay.h>
|
|
30 |
#include <linux/timer.h>
|
|
31 |
#include <linux/spinlock.h>
|
|
32 |
#include <linux/interrupt.h>
|
|
33 |
|
|
34 |
#include "../include/ecrt.h" // EtherCAT realtime interface
|
|
35 |
|
|
36 |
#define ASYNC
|
|
37 |
#define FREQUENCY 100
|
|
38 |
|
|
39 |
/*****************************************************************************/
|
|
40 |
|
|
41 |
struct timer_list timer;
|
|
42 |
|
|
43 |
// EtherCAT
|
|
44 |
ec_master_t *master = NULL;
|
|
45 |
ec_domain_t *domain1 = NULL;
|
|
46 |
spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
|
|
47 |
|
|
48 |
// data fields
|
|
49 |
//void *r_ssi_input, *r_ssi_status, *r_4102[3];
|
|
50 |
|
|
51 |
// channels
|
|
52 |
uint32_t k_pos;
|
|
53 |
uint8_t k_stat;
|
|
54 |
|
|
55 |
ec_field_init_t domain1_fields[] = {
|
|
56 |
{NULL, "3", "Beckhoff", "EL5001", "InputValue", 0},
|
|
57 |
{NULL, "2", "Beckhoff", "EL4132", "OutputValue", 0},
|
|
58 |
{}
|
|
59 |
};
|
|
60 |
|
|
61 |
/*****************************************************************************/
|
|
62 |
|
|
63 |
void run(unsigned long data)
|
|
64 |
{
|
|
65 |
static unsigned int counter = 0;
|
|
66 |
|
|
67 |
spin_lock(&master_lock);
|
|
68 |
|
|
69 |
#ifdef ASYNC
|
|
70 |
// receive
|
|
71 |
ecrt_master_async_receive(master);
|
|
72 |
ecrt_domain_process(domain1);
|
|
73 |
#else
|
|
74 |
// send and receive
|
|
75 |
ecrt_domain_queue(domain1);
|
|
76 |
ecrt_master_run(master);
|
|
77 |
ecrt_master_sync_io(master);
|
|
78 |
ecrt_domain_process(domain1);
|
|
79 |
#endif
|
|
80 |
|
|
81 |
// process data
|
|
82 |
//k_pos = EC_READ_U32(r_ssi);
|
|
83 |
|
|
84 |
#ifdef ASYNC
|
|
85 |
// send
|
|
86 |
ecrt_domain_queue(domain1);
|
|
87 |
ecrt_master_run(master);
|
|
88 |
ecrt_master_async_send(master);
|
|
89 |
#endif
|
|
90 |
|
|
91 |
spin_unlock(&master_lock);
|
|
92 |
|
|
93 |
if (counter) {
|
|
94 |
counter--;
|
|
95 |
}
|
|
96 |
else {
|
|
97 |
counter = FREQUENCY;
|
|
98 |
//printk(KERN_INFO "k_pos = %i\n", k_pos);
|
|
99 |
//printk(KERN_INFO "k_stat = 0x%02X\n", k_stat);
|
|
100 |
}
|
|
101 |
|
|
102 |
// restart timer
|
|
103 |
timer.expires += HZ / FREQUENCY;
|
|
104 |
add_timer(&timer);
|
|
105 |
}
|
|
106 |
|
|
107 |
/*****************************************************************************/
|
|
108 |
|
|
109 |
int request_lock(void *data)
|
|
110 |
{
|
|
111 |
unsigned int tries = 0;
|
|
112 |
while (1) {
|
|
113 |
if (spin_trylock(&master_lock)) {
|
|
114 |
if (tries) printk(KERN_INFO "lock: %i tries needed.\n", tries);
|
|
115 |
return 1;
|
|
116 |
}
|
|
117 |
tries++;
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
/*****************************************************************************/
|
|
122 |
|
|
123 |
void release_lock(void *data)
|
|
124 |
{
|
|
125 |
spin_unlock(&master_lock);
|
|
126 |
}
|
|
127 |
|
|
128 |
/*****************************************************************************/
|
|
129 |
|
|
130 |
int __init init_mini_module(void)
|
|
131 |
{
|
|
132 |
printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
|
|
133 |
|
|
134 |
if ((master = ecrt_request_master(0)) == NULL) {
|
|
135 |
printk(KERN_ERR "Requesting master 0 failed!\n");
|
|
136 |
goto out_return;
|
|
137 |
}
|
|
138 |
|
|
139 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL);
|
|
140 |
|
|
141 |
printk(KERN_INFO "Registering domain...\n");
|
|
142 |
if (!(domain1 = ecrt_master_create_domain(master)))
|
|
143 |
{
|
|
144 |
printk(KERN_ERR "Domain creation failed!\n");
|
|
145 |
goto out_release_master;
|
|
146 |
}
|
|
147 |
|
|
148 |
printk(KERN_INFO "Registering domain fields...\n");
|
|
149 |
if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
|
|
150 |
printk(KERN_ERR "Field registration failed!\n");
|
|
151 |
goto out_release_master;
|
|
152 |
}
|
|
153 |
|
|
154 |
printk(KERN_INFO "Activating master...\n");
|
|
155 |
if (ecrt_master_activate(master)) {
|
|
156 |
printk(KERN_ERR "Failed to activate master!\n");
|
|
157 |
goto out_release_master;
|
|
158 |
}
|
|
159 |
|
|
160 |
#if 0
|
|
161 |
if (ecrt_master_fetch_sdo_lists(master)) {
|
|
162 |
printk(KERN_ERR "Failed to fetch SDO lists!\n");
|
|
163 |
goto out_deactivate;
|
|
164 |
}
|
|
165 |
ecrt_master_print(master, 2);
|
|
166 |
#else
|
|
167 |
ecrt_master_print(master, 0);
|
|
168 |
#endif
|
|
169 |
|
|
170 |
#if 0
|
|
171 |
if (!(slave = ecrt_master_get_slave(master, "5"))) {
|
|
172 |
printk(KERN_ERR "Failed to get slave 5!\n");
|
|
173 |
goto out_deactivate;
|
|
174 |
}
|
|
175 |
|
|
176 |
if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) ||
|
|
177 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) ||
|
|
178 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) ||
|
|
179 |
ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) ||
|
|
180 |
ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) ||
|
|
181 |
ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) ||
|
|
182 |
ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) ||
|
|
183 |
ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) ||
|
|
184 |
ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) {
|
|
185 |
printk(KERN_ERR "Failed to configure SSI slave!\n");
|
|
186 |
goto out_deactivate;
|
|
187 |
}
|
|
188 |
#endif
|
|
189 |
|
|
190 |
#if 0
|
|
191 |
printk(KERN_INFO "Writing alias...\n");
|
|
192 |
if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) {
|
|
193 |
printk(KERN_ERR "Failed to write alias!\n");
|
|
194 |
goto out_deactivate;
|
|
195 |
}
|
|
196 |
#endif
|
|
197 |
|
|
198 |
#ifdef ASYNC
|
|
199 |
// send once and wait...
|
|
200 |
ecrt_master_prepare_async_io(master);
|
|
201 |
#endif
|
|
202 |
|
|
203 |
#if 1
|
|
204 |
if (ecrt_master_start_eoe(master)) {
|
|
205 |
printk(KERN_ERR "Failed to start EoE processing!\n");
|
|
206 |
goto out_deactivate;
|
|
207 |
}
|
|
208 |
#endif
|
|
209 |
|
|
210 |
printk("Starting cyclic sample thread.\n");
|
|
211 |
init_timer(&timer);
|
|
212 |
timer.function = run;
|
|
213 |
timer.expires = jiffies + 10;
|
|
214 |
add_timer(&timer);
|
|
215 |
|
|
216 |
printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
|
|
217 |
return 0;
|
|
218 |
|
|
219 |
#if 1
|
|
220 |
out_deactivate:
|
|
221 |
ecrt_master_deactivate(master);
|
|
222 |
#endif
|
|
223 |
out_release_master:
|
|
224 |
ecrt_release_master(master);
|
|
225 |
out_return:
|
|
226 |
return -1;
|
|
227 |
}
|
|
228 |
|
|
229 |
/*****************************************************************************/
|
|
230 |
|
|
231 |
void __exit cleanup_mini_module(void)
|
|
232 |
{
|
|
233 |
printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");
|
|
234 |
|
|
235 |
if (master) {
|
|
236 |
del_timer_sync(&timer);
|
|
237 |
printk(KERN_INFO "Deactivating master...\n");
|
|
238 |
ecrt_master_deactivate(master);
|
|
239 |
ecrt_release_master(master);
|
|
240 |
}
|
|
241 |
|
|
242 |
printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
|
|
243 |
}
|
|
244 |
|
|
245 |
/*****************************************************************************/
|
|
246 |
|
|
247 |
MODULE_LICENSE("GPL");
|
|
248 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
|
|
249 |
MODULE_DESCRIPTION ("EtherCAT minimal test environment");
|
|
250 |
|
|
251 |
module_init(init_mini_module);
|
|
252 |
module_exit(cleanup_mini_module);
|
|
253 |
|
|
254 |
/*****************************************************************************/
|
|
255 |
|