IgH EtherCAT Master  1.5.2
master.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * $Id$
4 *
5 * Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH
6 *
7 * This file is part of the IgH EtherCAT Master.
8 *
9 * The IgH EtherCAT Master is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2, as
11 * published by the Free Software Foundation.
12 *
13 * The IgH EtherCAT Master is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 * Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with the IgH EtherCAT Master; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * ---
23 *
24 * The license mentioned above concerns the source code only. Using the
25 * EtherCAT technology and brand is only permitted in compliance with the
26 * industrial property and similar rights of Beckhoff Automation GmbH.
27 *
28 *****************************************************************************/
29
35/*****************************************************************************/
36
37#ifndef __EC_MASTER_H__
38#define __EC_MASTER_H__
39
40#include <linux/version.h>
41#include <linux/list.h>
42#include <linux/timer.h>
43#include <linux/wait.h>
44#include <linux/kthread.h>
45
46#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
47#include <linux/semaphore.h>
48#else
49#include <asm/semaphore.h>
50#endif
51
52#include "device.h"
53#include "domain.h"
54#include "ethernet.h"
55#include "fsm_master.h"
56#include "cdev.h"
57
58#ifdef EC_RTDM
59#include "rtdm.h"
60#endif
61
62/*****************************************************************************/
63
73#define EC_MASTER_INFO(master, fmt, args...) \
74 printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args)
75
85#define EC_MASTER_ERR(master, fmt, args...) \
86 printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args)
87
97#define EC_MASTER_WARN(master, fmt, args...) \
98 printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args)
99
111#define EC_MASTER_DBG(master, level, fmt, args...) \
112 do { \
113 if (master->debug_level >= level) { \
114 printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
115 master->index, ##args); \
116 } \
117 } while (0)
118
119
124#define EC_EXT_RING_SIZE 32
125
126/*****************************************************************************/
127
130typedef enum {
133 EC_IDLE,
138
139/*****************************************************************************/
140
143typedef struct {
144 unsigned int timeouts;
145 unsigned int corrupted;
146 unsigned int unmatched;
148 unsigned long output_jiffies;
149} ec_stats_t;
150
151/*****************************************************************************/
152
155typedef struct {
167 s32 tx_frame_rates[EC_RATE_COUNT];
170 s32 rx_frame_rates[EC_RATE_COUNT];
173 s32 tx_byte_rates[EC_RATE_COUNT];
175 s32 rx_byte_rates[EC_RATE_COUNT];
177 s32 loss_rates[EC_RATE_COUNT];
179 unsigned long jiffies;
181
182/*****************************************************************************/
183
184#if EC_MAX_NUM_DEVICES < 1
185#error Invalid number of devices
186#endif
187
188/*****************************************************************************/
189
194struct ec_master {
195 unsigned int index;
196 unsigned int reserved;
199#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
200 struct device *class_device;
201#else
202 struct class_device *class_device;
203#endif
204
205#ifdef EC_RTDM
206 ec_rtdm_dev_t rtdm_dev;
207#endif
208
209 struct semaphore master_sem;
211 ec_device_t devices[EC_MAX_NUM_DEVICES];
212 const uint8_t *macs[EC_MAX_NUM_DEVICES];
213#if EC_MAX_NUM_DEVICES > 1
214 unsigned int num_devices;
217#endif
218 struct semaphore device_sem;
224 unsigned int active;
225 unsigned int config_changed;
226 unsigned int injection_seq_fsm;
228 unsigned int injection_seq_rt;
232 unsigned int slave_count;
234 /* Configuration applied by the application. */
235 struct list_head configs;
236 struct list_head domains;
250 unsigned int scan_busy;
251 unsigned int allow_scan;
252 struct semaphore scan_sem;
254 wait_queue_head_t scan_queue;
257 unsigned int config_busy;
258 struct semaphore config_sem;
260 wait_queue_head_t config_queue;
263 struct list_head datagram_queue;
266 struct list_head ext_datagram_queue;
268 struct semaphore ext_queue_sem;
273 unsigned int ext_ring_idx_rt;
275 unsigned int ext_ring_idx_fsm;
277 unsigned int send_interval;
282 struct list_head fsm_exec_list;
283 unsigned int fsm_exec_count;
285 unsigned int debug_level;
286 unsigned int run_on_cpu;
289 struct task_struct *thread;
291#ifdef EC_EOE
292 struct task_struct *eoe_thread;
293 struct list_head eoe_handlers;
294#endif
295
296 struct semaphore io_sem;
298 void (*send_cb)(void *);
299 void (*receive_cb)(void *);
300 void *cb_data;
301 void (*app_send_cb)(void *);
303 void (*app_receive_cb)(void *);
307 struct list_head sii_requests;
308 struct list_head emerg_reg_requests;
311 wait_queue_head_t request_queue;
313};
314
315/*****************************************************************************/
316
317// static funtions
318void ec_master_init_static(void);
319
320// master creation/deletion
321int ec_master_init(ec_master_t *, unsigned int, const uint8_t *,
322 const uint8_t *, dev_t, struct class *, unsigned int, unsigned int);
324
327#if EC_MAX_NUM_DEVICES > 1
328#define ec_master_num_devices(MASTER) ((MASTER)->num_devices)
329#else
330#define ec_master_num_devices(MASTER) 1
331#endif
332
333// phase transitions
338
339#ifdef EC_EOE
340// EoE
343#endif
344
345// datagram IO
347 const uint8_t *, size_t);
350
351// misc.
352void ec_master_set_send_interval(ec_master_t *, unsigned int);
354ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t);
355const ec_slave_t *ec_master_find_slave_const(const ec_master_t *, uint16_t,
356 uint16_t);
358#ifdef EC_EOE
360#endif
362
363unsigned int ec_master_config_count(const ec_master_t *);
365 const ec_master_t *, unsigned int);
367 const ec_master_t *, unsigned int);
368unsigned int ec_master_domain_count(const ec_master_t *);
371 unsigned int);
372#ifdef EC_EOE
375#endif
376
377int ec_master_debug_level(ec_master_t *, unsigned int);
378
381 uint16_t, uint32_t, uint32_t);
382
385
386void ec_master_internal_send_cb(void *);
388
389extern const unsigned int rate_intervals[EC_RATE_COUNT]; // see master.c
390
391/*****************************************************************************/
392
393#endif
EtherCAT master character device.
EtherCAT device structure.
EtherCAT domain structure.
Ethernet over EtherCAT (EoE)
EtherCAT master state machine.
#define EC_RATE_COUNT
Number of statistic rate intervals to maintain.
Definition: globals.h:60
void ec_master_set_send_interval(ec_master_t *, unsigned int)
Sets the expected interval between calls to ecrt_master_send and calculates the maximum amount of dat...
Definition: master.c:916
int ec_master_enter_operation_phase(ec_master_t *)
Transition function from IDLE to OPERATION phase.
Definition: master.c:698
ec_domain_t * ecrt_master_create_domain_err(ec_master_t *)
Same as ecrt_master_create_domain(), but with ERR_PTR() return value.
Definition: master.c:2274
void ec_master_leave_operation_phase(ec_master_t *)
Transition function from OPERATION to IDLE phase.
Definition: master.c:780
unsigned int ec_master_domain_count(const ec_master_t *)
Get the number of domains.
Definition: master.c:1943
void ec_master_init_static(void)
Static variables initializer.
Definition: master.c:121
const ec_domain_t * ec_master_find_domain_const(const ec_master_t *, unsigned int)
Get a domain via its position in the list.
Definition: master.c:1992
void ec_master_leave_idle_phase(ec_master_t *)
Transition function from IDLE to ORPHANED phase.
Definition: master.c:674
ec_domain_t * ec_master_find_domain(ec_master_t *, unsigned int)
Get a domain via its position in the list.
Definition: master.c:1977
unsigned int ec_master_config_count(const ec_master_t *)
Get the number of slave configurations provided by the application.
Definition: master.c:1880
ec_slave_t * ec_master_find_slave(ec_master_t *, uint16_t, uint16_t)
Finds a slave in the bus, given the alias and position.
Definition: master.c:1848
int ec_master_debug_level(ec_master_t *, unsigned int)
Set the debug level.
Definition: master.c:2056
void ec_master_clear_eoe_handlers(ec_master_t *)
Clear and free all EoE handlers.
Definition: master.c:442
uint16_t ec_master_eoe_handler_count(const ec_master_t *)
Get the number of EoE handlers.
Definition: master.c:2009
int ec_master_enter_idle_phase(ec_master_t *)
Transition function from ORPHANED to IDLE phase.
Definition: master.c:641
void ec_master_clear(ec_master_t *)
Destructor.
Definition: master.c:395
void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *)
Places a datagram in the datagram queue.
Definition: master.c:952
const ec_slave_config_t * ec_master_get_config_const(const ec_master_t *, unsigned int)
Get a slave configuration via its position in the list.
Definition: master.c:1928
const unsigned int rate_intervals[EC_RATE_COUNT]
List of intervals for statistics [s].
Definition: master.c:100
#define EC_EXT_RING_SIZE
Size of the external datagram ring.
Definition: master.h:124
ec_slave_config_t * ecrt_master_slave_config_err(ec_master_t *, uint16_t, uint16_t, uint32_t, uint32_t)
Same as ecrt_master_slave_config(), but with ERR_PTR() return value.
Definition: master.c:2578
void ec_master_clear_slaves(ec_master_t *)
Clear all slaves.
Definition: master.c:478
const ec_slave_t * ec_master_find_slave_const(const ec_master_t *, uint16_t, uint16_t)
Finds a slave in the bus, given the alias and position.
Definition: master.c:1864
void ec_master_output_stats(ec_master_t *)
Output master statistics.
Definition: master.c:1275
void ec_master_request_op(ec_master_t *)
Request OP state for configured slaves.
Definition: master.c:2240
const ec_eoe_t * ec_master_get_eoe_handler_const(const ec_master_t *, uint16_t)
Get an EoE handler via its position in the list.
Definition: master.c:2031
void ec_master_queue_datagram_ext(ec_master_t *, ec_datagram_t *)
Places a datagram in the non-application datagram queue.
Definition: master.c:985
void ec_master_attach_slave_configs(ec_master_t *)
Attaches the slave configurations to the slaves.
Definition: master.c:1808
ec_slave_config_t * ec_master_get_config(const ec_master_t *, unsigned int)
Get a slave configuration via its position in the list.
Definition: master.c:1913
void ec_master_calc_dc(ec_master_t *)
Distributed-clocks calculations.
Definition: master.c:2223
void ec_master_eoe_stop(ec_master_t *)
Stops the Ethernet over EtherCAT processing.
Definition: master.c:1728
void ec_master_internal_receive_cb(void *)
Internal receiving callback.
Definition: master.c:563
void ec_master_internal_send_cb(void *)
Internal sending callback.
Definition: master.c:549
void ec_master_eoe_start(ec_master_t *)
Starts Ethernet over EtherCAT processing on demand.
Definition: master.c:1694
ec_master_phase_t
EtherCAT master phase.
Definition: master.h:130
@ EC_IDLE
Idle phase.
Definition: master.h:133
@ EC_ORPHANED
Orphaned phase.
Definition: master.h:131
@ EC_OPERATION
Operation phase.
Definition: master.h:135
void ec_master_receive_datagrams(ec_master_t *, ec_device_t *, const uint8_t *, size_t)
Processes a received frame.
Definition: master.c:1137
int ec_master_init(ec_master_t *, unsigned int, const uint8_t *, const uint8_t *, dev_t, struct class *, unsigned int, unsigned int)
Master constructor.
Definition: master.c:142
RTDM interface.
EtherCAT master character device.
Definition: cdev.h:49
EtherCAT datagram.
Definition: datagram.h:87
Device statistics.
Definition: master.h:155
u64 tx_count
Number of frames sent.
Definition: master.h:156
u64 rx_bytes
Number of bytes received.
Definition: master.h:163
u64 last_loss
Tx/Rx difference of last statistics cycle.
Definition: master.h:166
u64 last_tx_bytes
Number of bytes sent of last statistics cycle.
Definition: master.h:162
u64 last_tx_count
Number of frames sent of last statistics cycle.
Definition: master.h:157
u64 tx_bytes
Number of bytes sent.
Definition: master.h:161
u64 rx_count
Number of frames received.
Definition: master.h:158
u64 last_rx_bytes
Number of bytes received of last statistics cycle.
Definition: master.h:164
u64 last_rx_count
Number of frames received of last statistics cycle.
Definition: master.h:159
unsigned long jiffies
Jiffies of last statistic cycle.
Definition: master.h:179
EtherCAT device.
Definition: device.h:82
EtherCAT domain.
Definition: domain.h:55
Ethernet over EtherCAT (EoE) handler.
Definition: ethernet.h:77
Finite state machine of an EtherCAT master.
Definition: fsm_master.h:68
EtherCAT master.
Definition: master.h:194
struct list_head datagram_queue
Datagram queue.
Definition: master.h:263
struct task_struct * eoe_thread
EoE thread.
Definition: master.h:292
unsigned int injection_seq_fsm
Datagram injection sequence number for the FSM side.
Definition: master.h:226
struct task_struct * thread
Master thread.
Definition: master.h:289
struct list_head eoe_handlers
Ethernet over EtherCAT handlers.
Definition: master.h:293
ec_datagram_t fsm_datagram
Datagram used for state machines.
Definition: master.h:222
struct list_head emerg_reg_requests
Emergency register access requests.
Definition: master.h:308
struct semaphore master_sem
Master semaphore.
Definition: master.h:209
struct list_head ext_datagram_queue
Queue for non-application datagrams.
Definition: master.h:266
ec_datagram_t sync_datagram
Datagram used for DC drift compensation.
Definition: master.h:242
size_t max_queue_size
Maximum size of datagram queue.
Definition: master.h:279
wait_queue_head_t request_queue
Wait queue for external requests from user space.
Definition: master.h:311
struct list_head fsm_exec_list
Slave FSM execution list.
Definition: master.h:282
struct list_head sii_requests
SII write requests.
Definition: master.h:307
ec_fsm_master_t fsm
Master state machine.
Definition: master.h:221
u64 app_time
Time of the last ecrt_master_sync() call.
Definition: master.h:238
wait_queue_head_t scan_queue
Queue for processes that wait for slave scanning.
Definition: master.h:254
struct semaphore io_sem
Semaphore used in IDLE phase.
Definition: master.h:296
unsigned int scan_busy
Current scan state.
Definition: master.h:250
unsigned int send_interval
Interval between two calls to ecrt_master_send().
Definition: master.h:277
unsigned int slave_count
Number of slaves on the bus.
Definition: master.h:232
void * app_cb_data
Application callback data.
Definition: master.h:305
void(* app_receive_cb)(void *)
Application's receive datagrams callback.
Definition: master.h:303
void(* receive_cb)(void *)
Current receive datagrams callback.
Definition: master.h:299
uint8_t datagram_index
Current datagram index.
Definition: master.h:264
unsigned int index
Index.
Definition: master.h:195
unsigned int ext_ring_idx_fsm
Index in external datagram ring for FSM side.
Definition: master.h:275
ec_device_stats_t device_stats
Device statistics.
Definition: master.h:219
void(* send_cb)(void *)
Current send datagrams callback.
Definition: master.h:298
unsigned int config_busy
State of slave configuration.
Definition: master.h:257
ec_slave_t * dc_ref_clock
DC reference clock slave.
Definition: master.h:248
ec_datagram_t ref_sync_datagram
Datagram used for synchronizing the reference clock to the master clock.
Definition: master.h:240
struct list_head domains
List of domains.
Definition: master.h:236
struct semaphore config_sem
Semaphore protecting the config_busy variable and the allow_config flag.
Definition: master.h:258
unsigned int fsm_exec_count
Number of entries in execution list.
Definition: master.h:283
void(* app_send_cb)(void *)
Application's send datagrams callback.
Definition: master.h:301
u64 dc_ref_time
Common reference timestamp for DC start times.
Definition: master.h:239
ec_slave_t * fsm_slave
Slave that is queried next for FSM exec.
Definition: master.h:281
unsigned int injection_seq_rt
Datagram injection sequence number for the realtime side.
Definition: master.h:228
wait_queue_head_t config_queue
Queue for processes that wait for slave configuration.
Definition: master.h:260
struct semaphore scan_sem
Semaphore protecting the scan_busy variable and the allow_scan flag.
Definition: master.h:252
unsigned int run_on_cpu
bind kernel threads to this cpu
Definition: master.h:286
ec_datagram_t sync_mon_datagram
Datagram used for DC synchronisation monitoring.
Definition: master.h:244
ec_slave_t * slaves
Array of slaves on the bus.
Definition: master.h:231
unsigned int active
Master has been activated.
Definition: master.h:224
unsigned int ext_ring_idx_rt
Index in external datagram ring for RT side.
Definition: master.h:273
unsigned int debug_level
Master debug level.
Definition: master.h:285
ec_stats_t stats
Cyclic statistics.
Definition: master.h:287
ec_slave_config_t * dc_ref_config
Application-selected DC reference clock slave config.
Definition: master.h:246
struct device * class_device
Master class device.
Definition: master.h:200
struct semaphore device_sem
Device semaphore.
Definition: master.h:218
ec_cdev_t cdev
Master character device.
Definition: master.h:198
unsigned int reserved
True, if the master is in use.
Definition: master.h:196
unsigned int allow_scan
True, if slave scanning is allowed.
Definition: master.h:251
unsigned int config_changed
The configuration changed.
Definition: master.h:225
struct list_head configs
List of slave configurations.
Definition: master.h:235
const uint8_t * macs[EC_MAX_NUM_DEVICES]
Device MAC addresses.
Definition: master.h:212
ec_datagram_t ext_datagram_ring[EC_EXT_RING_SIZE]
External datagram ring.
Definition: master.h:271
ec_master_phase_t phase
Master phase.
Definition: master.h:223
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition: master.h:211
void * cb_data
Current callback data.
Definition: master.h:300
struct semaphore ext_queue_sem
Semaphore protecting the ext_datagram_queue.
Definition: master.h:268
EtherCAT RTDM device.
Definition: rtdm.h:44
EtherCAT slave configuration.
Definition: slave_config.h:119
EtherCAT slave.
Definition: slave.h:177
Cyclic statistics.
Definition: master.h:143
unsigned int corrupted
corrupted frames
Definition: master.h:145
unsigned long output_jiffies
time of last output
Definition: master.h:148
unsigned int timeouts
datagram timeouts
Definition: master.h:144
unsigned int unmatched
unmatched datagrams (received, but not queued any longer)
Definition: master.h:146