author | Florian Pose <fp@igh-essen.com> |
Wed, 19 Jul 2006 15:28:34 +0000 | |
changeset 308 | d50b139c8601 |
parent 280 | 35c99cdda518 |
child 325 | 7833cf70c4f2 |
permissions | -rw-r--r-- |
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 |
|
246
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
226
diff
changeset
|
15 |
* 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:
226
diff
changeset
|
16 |
* License, or (at your option) any later version. |
220 | 17 |
* |
18 |
* The IgH EtherCAT Master is distributed in the hope that it will be |
|
19 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 |
* GNU General Public License for more details. |
|
22 |
* |
|
23 |
* You should have received a copy of the GNU General Public License |
|
24 |
* along with the IgH EtherCAT Master; if not, write to the Free Software |
|
25 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
26 |
* |
|
246
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
226
diff
changeset
|
27 |
* 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:
226
diff
changeset
|
28 |
* 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:
226
diff
changeset
|
29 |
* 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:
226
diff
changeset
|
30 |
* 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:
226
diff
changeset
|
31 |
* 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:
226
diff
changeset
|
32 |
* 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:
226
diff
changeset
|
33 |
* 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:
226
diff
changeset
|
34 |
* Technology, IP and trade marks. |
0bf7c769de06
Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents:
226
diff
changeset
|
35 |
* |
220 | 36 |
*****************************************************************************/ |
37 |
||
38 |
#include <linux/module.h> |
|
39 |
#include <linux/delay.h> |
|
40 |
#include <linux/timer.h> |
|
41 |
#include <linux/spinlock.h> |
|
42 |
#include <linux/interrupt.h> |
|
43 |
||
223
daa5e5656b35
Applied new path information to example modules.
Florian Pose <fp@igh-essen.com>
parents:
220
diff
changeset
|
44 |
#include "../../include/ecrt.h" // EtherCAT realtime interface |
220 | 45 |
|
46 |
#define ASYNC |
|
47 |
#define FREQUENCY 100 |
|
48 |
||
49 |
/*****************************************************************************/ |
|
50 |
||
51 |
struct timer_list timer; |
|
52 |
||
53 |
// EtherCAT |
|
54 |
ec_master_t *master = NULL; |
|
55 |
ec_domain_t *domain1 = NULL; |
|
56 |
spinlock_t master_lock = SPIN_LOCK_UNLOCKED; |
|
57 |
||
58 |
// data fields |
|
59 |
//void *r_ssi_input, *r_ssi_status, *r_4102[3]; |
|
280
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
60 |
void *r_kbus_in, *r_kbus_out; |
220 | 61 |
|
62 |
// channels |
|
63 |
uint32_t k_pos; |
|
64 |
uint8_t k_stat; |
|
65 |
||
66 |
ec_field_init_t domain1_fields[] = { |
|
280
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
67 |
//{&r_kbus_out, "0", "Beckhoff", "BK1120", "Outputs", 0}, |
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
68 |
//{&r_kbus_in, "0", "Beckhoff", "BK1120", "Inputs", 0}, |
220 | 69 |
{} |
70 |
}; |
|
71 |
||
72 |
/*****************************************************************************/ |
|
73 |
||
74 |
void run(unsigned long data) |
|
75 |
{ |
|
76 |
static unsigned int counter = 0; |
|
280
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
77 |
unsigned int i; |
220 | 78 |
|
79 |
spin_lock(&master_lock); |
|
80 |
||
81 |
#ifdef ASYNC |
|
82 |
// receive |
|
83 |
ecrt_master_async_receive(master); |
|
84 |
ecrt_domain_process(domain1); |
|
85 |
#else |
|
86 |
// send and receive |
|
87 |
ecrt_domain_queue(domain1); |
|
88 |
ecrt_master_run(master); |
|
89 |
ecrt_master_sync_io(master); |
|
90 |
ecrt_domain_process(domain1); |
|
91 |
#endif |
|
92 |
||
93 |
// process data |
|
94 |
//k_pos = EC_READ_U32(r_ssi); |
|
95 |
||
96 |
#ifdef ASYNC |
|
97 |
// send |
|
98 |
ecrt_domain_queue(domain1); |
|
99 |
ecrt_master_run(master); |
|
100 |
ecrt_master_async_send(master); |
|
101 |
#endif |
|
102 |
||
103 |
spin_unlock(&master_lock); |
|
104 |
||
105 |
if (counter) { |
|
106 |
counter--; |
|
107 |
} |
|
108 |
else { |
|
109 |
counter = FREQUENCY; |
|
280
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
110 |
printk(KERN_INFO "input = "); |
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
111 |
for (i = 0; i < 22; i++) printk("%02X ", *((uint8_t *) r_kbus_in + i)); |
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
112 |
printk("\n"); |
220 | 113 |
} |
114 |
||
115 |
// restart timer |
|
116 |
timer.expires += HZ / FREQUENCY; |
|
117 |
add_timer(&timer); |
|
118 |
} |
|
119 |
||
120 |
/*****************************************************************************/ |
|
121 |
||
122 |
int request_lock(void *data) |
|
123 |
{ |
|
226
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
124 |
spin_lock_bh(&master_lock); |
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
125 |
return 0; // access allowed |
220 | 126 |
} |
127 |
||
128 |
/*****************************************************************************/ |
|
129 |
||
130 |
void release_lock(void *data) |
|
131 |
{ |
|
226
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
132 |
spin_unlock_bh(&master_lock); |
220 | 133 |
} |
134 |
||
135 |
/*****************************************************************************/ |
|
136 |
||
137 |
int __init init_mini_module(void) |
|
138 |
{ |
|
275
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
139 |
ec_slave_t *slave; |
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
140 |
|
220 | 141 |
printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
142 |
||
143 |
if ((master = ecrt_request_master(0)) == NULL) { |
|
144 |
printk(KERN_ERR "Requesting master 0 failed!\n"); |
|
145 |
goto out_return; |
|
146 |
} |
|
147 |
||
148 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
149 |
||
150 |
printk(KERN_INFO "Registering domain...\n"); |
|
151 |
if (!(domain1 = ecrt_master_create_domain(master))) |
|
152 |
{ |
|
153 |
printk(KERN_ERR "Domain creation failed!\n"); |
|
154 |
goto out_release_master; |
|
155 |
} |
|
156 |
||
157 |
printk(KERN_INFO "Registering domain fields...\n"); |
|
158 |
if (ecrt_domain_register_field_list(domain1, domain1_fields)) { |
|
159 |
printk(KERN_ERR "Field registration failed!\n"); |
|
160 |
goto out_release_master; |
|
161 |
} |
|
162 |
||
275
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
163 |
#if 1 |
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
164 |
printk(KERN_INFO "Setting variable data field sizes...\n"); |
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
165 |
if (!(slave = ecrt_master_get_slave(master, "0"))) { |
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
166 |
printk(KERN_ERR "Failed to get slave!\n"); |
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
167 |
goto out_deactivate; |
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
168 |
} |
280
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
169 |
ecrt_slave_field_size(slave, "Outputs", 0, 0x16); |
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
170 |
ecrt_slave_field_size(slave, "Inputs", 0, 0x16); |
275
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
171 |
#endif |
ba51285d4ef6
Variable-sized data fields, BK1120.
Florian Pose <fp@igh-essen.com>
parents:
246
diff
changeset
|
172 |
|
220 | 173 |
printk(KERN_INFO "Activating master...\n"); |
174 |
if (ecrt_master_activate(master)) { |
|
175 |
printk(KERN_ERR "Failed to activate master!\n"); |
|
176 |
goto out_release_master; |
|
177 |
} |
|
178 |
||
280
35c99cdda518
types.c: BK1120 outputs and EL1004.
Florian Pose <fp@igh-essen.com>
parents:
275
diff
changeset
|
179 |
#if 1 |
220 | 180 |
if (ecrt_master_fetch_sdo_lists(master)) { |
181 |
printk(KERN_ERR "Failed to fetch SDO lists!\n"); |
|
182 |
goto out_deactivate; |
|
183 |
} |
|
184 |
ecrt_master_print(master, 2); |
|
185 |
#else |
|
186 |
ecrt_master_print(master, 0); |
|
187 |
#endif |
|
188 |
||
189 |
#if 0 |
|
190 |
if (!(slave = ecrt_master_get_slave(master, "5"))) { |
|
191 |
printk(KERN_ERR "Failed to get slave 5!\n"); |
|
192 |
goto out_deactivate; |
|
193 |
} |
|
194 |
||
195 |
if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || |
|
196 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || |
|
197 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || |
|
198 |
ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || |
|
199 |
ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || |
|
200 |
ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || |
|
201 |
ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || |
|
202 |
ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || |
|
203 |
ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { |
|
204 |
printk(KERN_ERR "Failed to configure SSI slave!\n"); |
|
205 |
goto out_deactivate; |
|
206 |
} |
|
207 |
#endif |
|
208 |
||
209 |
#ifdef ASYNC |
|
210 |
// send once and wait... |
|
211 |
ecrt_master_prepare_async_io(master); |
|
212 |
#endif |
|
213 |
||
214 |
#if 1 |
|
215 |
if (ecrt_master_start_eoe(master)) { |
|
216 |
printk(KERN_ERR "Failed to start EoE processing!\n"); |
|
217 |
goto out_deactivate; |
|
218 |
} |
|
219 |
#endif |
|
220 |
||
221 |
printk("Starting cyclic sample thread.\n"); |
|
222 |
init_timer(&timer); |
|
223 |
timer.function = run; |
|
224 |
timer.expires = jiffies + 10; |
|
225 |
add_timer(&timer); |
|
226 |
||
227 |
printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
|
228 |
return 0; |
|
229 |
||
230 |
#if 1 |
|
231 |
out_deactivate: |
|
232 |
ecrt_master_deactivate(master); |
|
233 |
#endif |
|
234 |
out_release_master: |
|
235 |
ecrt_release_master(master); |
|
236 |
out_return: |
|
237 |
return -1; |
|
238 |
} |
|
239 |
||
240 |
/*****************************************************************************/ |
|
241 |
||
242 |
void __exit cleanup_mini_module(void) |
|
243 |
{ |
|
244 |
printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
|
245 |
||
246 |
if (master) { |
|
247 |
del_timer_sync(&timer); |
|
248 |
printk(KERN_INFO "Deactivating master...\n"); |
|
249 |
ecrt_master_deactivate(master); |
|
250 |
ecrt_release_master(master); |
|
251 |
} |
|
252 |
||
253 |
printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
|
254 |
} |
|
255 |
||
256 |
/*****************************************************************************/ |
|
257 |
||
258 |
MODULE_LICENSE("GPL"); |
|
259 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
260 |
MODULE_DESCRIPTION ("EtherCAT minimal test environment"); |
|
261 |
||
262 |
module_init(init_mini_module); |
|
263 |
module_exit(cleanup_mini_module); |
|
264 |
||
265 |
/*****************************************************************************/ |
|
266 |