author | Florian Pose <fp@igh-essen.com> |
Mon, 09 Oct 2006 14:43:20 +0000 | |
changeset 387 | e6e708a5d502 |
parent 348 | 8b19d6fab6f5 |
child 416 | 4f007cd2a79d |
child 1732 | 1cc865ba17c2 |
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 |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
59 |
void *r_ana_out; |
220 | 60 |
|
61 |
// channels |
|
62 |
uint32_t k_pos; |
|
63 |
uint8_t k_stat; |
|
64 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
65 |
ec_pdo_reg_t domain1_pdos[] = { |
348 | 66 |
{"2", Beckhoff_EL4132_Output1, &r_ana_out}, |
67 |
{"3", Beckhoff_EL5001_Value, NULL}, |
|
220 | 68 |
{} |
69 |
}; |
|
70 |
||
71 |
/*****************************************************************************/ |
|
72 |
||
73 |
void run(unsigned long data) |
|
74 |
{ |
|
75 |
static unsigned int counter = 0; |
|
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); |
|
85 |
||
86 |
// send |
|
87 |
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
|
88 |
ecrt_master_send(master); |
220 | 89 |
|
90 |
spin_unlock(&master_lock); |
|
91 |
||
92 |
if (counter) { |
|
93 |
counter--; |
|
94 |
} |
|
95 |
else { |
|
96 |
counter = FREQUENCY; |
|
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
97 |
//printk(KERN_INFO "input = "); |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
98 |
//for (i = 0; i < 22; i++) |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
99 |
// printk("%02X ", *((uint8_t *) r_kbus_in + i)); |
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
100 |
//printk("\n"); |
220 | 101 |
} |
102 |
||
103 |
// restart timer |
|
104 |
timer.expires += HZ / FREQUENCY; |
|
105 |
add_timer(&timer); |
|
106 |
} |
|
107 |
||
108 |
/*****************************************************************************/ |
|
109 |
||
110 |
int request_lock(void *data) |
|
111 |
{ |
|
226
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
112 |
spin_lock_bh(&master_lock); |
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
113 |
return 0; // access allowed |
220 | 114 |
} |
115 |
||
116 |
/*****************************************************************************/ |
|
117 |
||
118 |
void release_lock(void *data) |
|
119 |
{ |
|
226
07247920a7ba
EoE with workqueue; bugfix in ec_master_init()
Florian Pose <fp@igh-essen.com>
parents:
223
diff
changeset
|
120 |
spin_unlock_bh(&master_lock); |
220 | 121 |
} |
122 |
||
123 |
/*****************************************************************************/ |
|
124 |
||
125 |
int __init init_mini_module(void) |
|
126 |
{ |
|
329
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
127 |
ec_slave_t *slave; |
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
128 |
|
220 | 129 |
printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
130 |
||
131 |
if ((master = ecrt_request_master(0)) == NULL) { |
|
132 |
printk(KERN_ERR "Requesting master 0 failed!\n"); |
|
133 |
goto out_return; |
|
134 |
} |
|
135 |
||
136 |
ecrt_master_callbacks(master, request_lock, release_lock, NULL); |
|
137 |
||
138 |
printk(KERN_INFO "Registering domain...\n"); |
|
139 |
if (!(domain1 = ecrt_master_create_domain(master))) |
|
140 |
{ |
|
141 |
printk(KERN_ERR "Domain creation failed!\n"); |
|
142 |
goto out_release_master; |
|
143 |
} |
|
144 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
145 |
printk(KERN_INFO "Registering PDOs...\n"); |
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 |
} |
|
150 |
||
348 | 151 |
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
|
152 |
goto out_release_master; |
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
153 |
|
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
154 |
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
|
155 |
goto out_release_master; |
d004349777fc
SDO configuration interface, SDO download state machine.
Florian Pose <fp@igh-essen.com>
parents:
325
diff
changeset
|
156 |
|
220 | 157 |
printk(KERN_INFO "Activating master...\n"); |
158 |
if (ecrt_master_activate(master)) { |
|
159 |
printk(KERN_ERR "Failed to activate master!\n"); |
|
160 |
goto out_release_master; |
|
161 |
} |
|
162 |
||
325
7833cf70c4f2
VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents:
280
diff
changeset
|
163 |
ecrt_master_prepare(master); |
220 | 164 |
|
165 |
printk("Starting cyclic sample thread.\n"); |
|
166 |
init_timer(&timer); |
|
167 |
timer.function = run; |
|
168 |
timer.expires = jiffies + 10; |
|
169 |
add_timer(&timer); |
|
170 |
||
171 |
printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
|
172 |
return 0; |
|
173 |
||
174 |
out_release_master: |
|
175 |
ecrt_release_master(master); |
|
176 |
out_return: |
|
177 |
return -1; |
|
178 |
} |
|
179 |
||
180 |
/*****************************************************************************/ |
|
181 |
||
182 |
void __exit cleanup_mini_module(void) |
|
183 |
{ |
|
184 |
printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
|
185 |
||
186 |
if (master) { |
|
187 |
del_timer_sync(&timer); |
|
188 |
printk(KERN_INFO "Deactivating master...\n"); |
|
189 |
ecrt_master_deactivate(master); |
|
190 |
ecrt_release_master(master); |
|
191 |
} |
|
192 |
||
193 |
printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
|
194 |
} |
|
195 |
||
196 |
/*****************************************************************************/ |
|
197 |
||
198 |
MODULE_LICENSE("GPL"); |
|
199 |
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>"); |
|
200 |
MODULE_DESCRIPTION ("EtherCAT minimal test environment"); |
|
201 |
||
202 |
module_init(init_mini_module); |
|
203 |
module_exit(cleanup_mini_module); |
|
204 |
||
205 |
/*****************************************************************************/ |
|
206 |