author | Florian Pose <fp@igh-essen.com> |
Wed, 17 May 2006 09:22:31 +0000 | |
changeset 241 | cd90a22cab28 |
parent 226 | 07247920a7ba |
child 246 | 0bf7c769de06 |
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 |
|
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 |
||
223
daa5e5656b35
Applied new path information to example modules.
Florian Pose <fp@igh-essen.com>
parents:
220
diff
changeset
|
34 |
#include "../../include/ecrt.h" // EtherCAT realtime interface |
220 | 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 |
{ |
|
226
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
111 |
spin_lock_bh(&master_lock); |
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
112 |
return 0; // access allowed |
220 | 113 |
} |
114 |
||
115 |
/*****************************************************************************/ |
|
116 |
||
117 |
void release_lock(void *data) |
|
118 |
{ |
|
226
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
119 |
spin_unlock_bh(&master_lock); |
220 | 120 |
} |
121 |
||
122 |
/*****************************************************************************/ |
|
123 |
||
124 |
int __init init_mini_module(void) |
|
125 |
{ |
|
126 |
printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
|
127 |
||
128 |
if ((master = ecrt_request_master(0)) == NULL) { |
|
129 |
printk(KERN_ERR "Requesting master 0 failed!\n"); |
|
130 |
goto out_return; |
|
131 |
} |
|
132 |
||
133 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
134 |
||
135 |
printk(KERN_INFO "Registering domain...\n"); |
|
136 |
if (!(domain1 = ecrt_master_create_domain(master))) |
|
137 |
{ |
|
138 |
printk(KERN_ERR "Domain creation failed!\n"); |
|
139 |
goto out_release_master; |
|
140 |
} |
|
141 |
||
142 |
printk(KERN_INFO "Registering domain fields...\n"); |
|
143 |
if (ecrt_domain_register_field_list(domain1, domain1_fields)) { |
|
144 |
printk(KERN_ERR "Field registration failed!\n"); |
|
145 |
goto out_release_master; |
|
146 |
} |
|
147 |
||
148 |
printk(KERN_INFO "Activating master...\n"); |
|
149 |
if (ecrt_master_activate(master)) { |
|
150 |
printk(KERN_ERR "Failed to activate master!\n"); |
|
151 |
goto out_release_master; |
|
152 |
} |
|
153 |
||
154 |
#if 0 |
|
155 |
if (ecrt_master_fetch_sdo_lists(master)) { |
|
156 |
printk(KERN_ERR "Failed to fetch SDO lists!\n"); |
|
157 |
goto out_deactivate; |
|
158 |
} |
|
159 |
ecrt_master_print(master, 2); |
|
160 |
#else |
|
161 |
ecrt_master_print(master, 0); |
|
162 |
#endif |
|
163 |
||
164 |
#if 0 |
|
165 |
if (!(slave = ecrt_master_get_slave(master, "5"))) { |
|
166 |
printk(KERN_ERR "Failed to get slave 5!\n"); |
|
167 |
goto out_deactivate; |
|
168 |
} |
|
169 |
||
170 |
if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || |
|
171 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || |
|
172 |
ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || |
|
173 |
ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || |
|
174 |
ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || |
|
175 |
ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || |
|
176 |
ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || |
|
177 |
ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || |
|
178 |
ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { |
|
179 |
printk(KERN_ERR "Failed to configure SSI slave!\n"); |
|
180 |
goto out_deactivate; |
|
181 |
} |
|
182 |
#endif |
|
183 |
||
184 |
#if 0 |
|
185 |
printk(KERN_INFO "Writing alias...\n"); |
|
186 |
if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) { |
|
187 |
printk(KERN_ERR "Failed to write alias!\n"); |
|
188 |
goto out_deactivate; |
|
189 |
} |
|
190 |
#endif |
|
191 |
||
192 |
#ifdef ASYNC |
|
193 |
// send once and wait... |
|
194 |
ecrt_master_prepare_async_io(master); |
|
195 |
#endif |
|
196 |
||
197 |
#if 1 |
|
198 |
if (ecrt_master_start_eoe(master)) { |
|
199 |
printk(KERN_ERR "Failed to start EoE processing!\n"); |
|
200 |
goto out_deactivate; |
|
201 |
} |
|
202 |
#endif |
|
203 |
||
204 |
printk("Starting cyclic sample thread.\n"); |
|
205 |
init_timer(&timer); |
|
206 |
timer.function = run; |
|
207 |
timer.expires = jiffies + 10; |
|
208 |
add_timer(&timer); |
|
209 |
||
210 |
printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
|
211 |
return 0; |
|
212 |
||
213 |
#if 1 |
|
214 |
out_deactivate: |
|
215 |
ecrt_master_deactivate(master); |
|
216 |
#endif |
|
217 |
out_release_master: |
|
218 |
ecrt_release_master(master); |
|
219 |
out_return: |
|
220 |
return -1; |
|
221 |
} |
|
222 |
||
223 |
/*****************************************************************************/ |
|
224 |
||
225 |
void __exit cleanup_mini_module(void) |
|
226 |
{ |
|
227 |
printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
|
228 |
||
229 |
if (master) { |
|
230 |
del_timer_sync(&timer); |
|
231 |
printk(KERN_INFO "Deactivating master...\n"); |
|
232 |
ecrt_master_deactivate(master); |
|
233 |
ecrt_release_master(master); |
|
234 |
} |
|
235 |
||
236 |
printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
|
237 |
} |
|
238 |
||
239 |
/*****************************************************************************/ |
|
240 |
||
241 |
MODULE_LICENSE("GPL"); |
|
242 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
243 |
MODULE_DESCRIPTION ("EtherCAT minimal test environment"); |
|
244 |
||
245 |
module_init(init_mini_module); |
|
246 |
module_exit(cleanup_mini_module); |
|
247 |
||
248 |
/*****************************************************************************/ |
|
249 |