author | Florian Pose <fp@igh-essen.com> |
Tue, 24 Oct 2006 08:09:42 +0000 | |
changeset 437 | ef80f2faa2c5 |
parent 416 | 4f007cd2a79d |
child 446 | 8fede404c01f |
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 |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
45 |
#include "../../include/ecdb.h" // EtherCAT slave database |
220 | 46 |
|
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 |
|
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
59 |
void *r_inputs; |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
60 |
void *r_outputs; |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
61 |
|
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
62 |
#if 0 |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
63 |
ec_pdo_reg_t domain1_pdos[] = { |
348 | 64 |
{"2", Beckhoff_EL4132_Output1, &r_ana_out}, |
65 |
{"3", Beckhoff_EL5001_Value, NULL}, |
|
220 | 66 |
{} |
67 |
}; |
|
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
68 |
#endif |
220 | 69 |
|
70 |
/*****************************************************************************/ |
|
71 |
||
72 |
void run(unsigned long data) |
|
73 |
{ |
|
74 |
static unsigned int counter = 0; |
|
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
75 |
static unsigned int einaus = 0; |
220 | 76 |
|
77 |
spin_lock(&master_lock); |
|
78 |
||
79 |
// receive |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
80 |
ecrt_master_receive(master); |
220 | 81 |
ecrt_domain_process(domain1); |
82 |
||
83 |
// process data |
|
84 |
//k_pos = EC_READ_U32(r_ssi); |
|
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
85 |
EC_WRITE_U8(r_outputs + 2, einaus ? 0xFF : 0x00); |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
86 |
|
220 | 87 |
|
88 |
// send |
|
89 |
ecrt_master_run(master); |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
90 |
ecrt_master_send(master); |
220 | 91 |
|
92 |
spin_unlock(&master_lock); |
|
93 |
||
94 |
if (counter) { |
|
95 |
counter--; |
|
96 |
} |
|
97 |
else { |
|
98 |
counter = FREQUENCY; |
|
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
99 |
einaus = !einaus; |
220 | 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 |
{ |
|
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
126 |
// ec_slave_t *slave; |
329
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
127 |
|
220 | 128 |
printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
129 |
||
130 |
if ((master = ecrt_request_master(0)) == NULL) { |
|
131 |
printk(KERN_ERR "Requesting master 0 failed!\n"); |
|
132 |
goto out_return; |
|
133 |
} |
|
134 |
||
135 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
136 |
||
137 |
printk(KERN_INFO "Registering domain...\n"); |
|
138 |
if (!(domain1 = ecrt_master_create_domain(master))) |
|
139 |
{ |
|
140 |
printk(KERN_ERR "Domain creation failed!\n"); |
|
141 |
goto out_release_master; |
|
142 |
} |
|
143 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
144 |
printk(KERN_INFO "Registering PDOs...\n"); |
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
145 |
#if 0 |
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
146 |
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:
280
diff
changeset
|
147 |
printk(KERN_ERR "PDO registration failed!\n"); |
220 | 148 |
goto out_release_master; |
149 |
} |
|
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
150 |
#endif |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
151 |
if (!ecrt_domain_register_pdo_range(domain1, "0", Beckhoff_BK1120, |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
152 |
EC_DIR_OUTPUT, 0, 4, &r_outputs)) { |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
153 |
printk(KERN_ERR "PDO registration failed!\n"); |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
154 |
goto out_release_master; |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
155 |
} |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
156 |
if (!ecrt_domain_register_pdo_range(domain1, "0", Beckhoff_BK1120, |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
157 |
EC_DIR_INPUT, 0, 4, &r_inputs)) { |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
158 |
printk(KERN_ERR "PDO registration failed!\n"); |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
159 |
goto out_release_master; |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
160 |
} |
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
161 |
|
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
162 |
#if 0 |
348 | 163 |
if (!(slave = ecrt_master_get_slave(master, "3"))) |
329
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
164 |
goto out_release_master; |
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
165 |
|
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
166 |
if (ecrt_slave_conf_sdo8(slave, 0x4061, 1, 0)) |
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
167 |
goto out_release_master; |
416
4f007cd2a79d
ecrt_domain_register_pdo_range() implemented.
Florian Pose <fp@igh-essen.com>
parents:
348
diff
changeset
|
168 |
#endif |
329
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
169 |
|
220 | 170 |
printk(KERN_INFO "Activating master...\n"); |
171 |
if (ecrt_master_activate(master)) { |
|
172 |
printk(KERN_ERR "Failed to activate master!\n"); |
|
173 |
goto out_release_master; |
|
174 |
} |
|
175 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
176 |
ecrt_master_prepare(master); |
220 | 177 |
|
178 |
printk("Starting cyclic sample thread.\n"); |
|
179 |
init_timer(&timer); |
|
180 |
timer.function = run; |
|
181 |
timer.expires = jiffies + 10; |
|
182 |
add_timer(&timer); |
|
183 |
||
184 |
printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
|
185 |
return 0; |
|
186 |
||
187 |
out_release_master: |
|
188 |
ecrt_release_master(master); |
|
189 |
out_return: |
|
190 |
return -1; |
|
191 |
} |
|
192 |
||
193 |
/*****************************************************************************/ |
|
194 |
||
195 |
void __exit cleanup_mini_module(void) |
|
196 |
{ |
|
197 |
printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
|
198 |
||
199 |
if (master) { |
|
200 |
del_timer_sync(&timer); |
|
201 |
printk(KERN_INFO "Deactivating master...\n"); |
|
202 |
ecrt_master_deactivate(master); |
|
203 |
ecrt_release_master(master); |
|
204 |
} |
|
205 |
||
206 |
printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
|
207 |
} |
|
208 |
||
209 |
/*****************************************************************************/ |
|
210 |
||
211 |
MODULE_LICENSE("GPL"); |
|
212 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
213 |
MODULE_DESCRIPTION ("EtherCAT minimal test environment"); |
|
214 |
||
215 |
module_init(init_mini_module); |
|
216 |
module_exit(cleanup_mini_module); |
|
217 |
||
218 |
/*****************************************************************************/ |
|
219 |