37#ifndef __EC_MASTER_H__
38#define __EC_MASTER_H__
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>
46#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
47#include <linux/semaphore.h>
49#include <asm/semaphore.h>
73#define EC_MASTER_INFO(master, fmt, args...) \
74 printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args)
85#define EC_MASTER_ERR(master, fmt, args...) \
86 printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args)
97#define EC_MASTER_WARN(master, fmt, args...) \
98 printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args)
111#define EC_MASTER_DBG(master, level, fmt, args...) \
113 if (master->debug_level >= level) { \
114 printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
115 master->index, ##args); \
124#define EC_EXT_RING_SIZE 32
184#if EC_MAX_NUM_DEVICES < 1
185#error Invalid number of devices
199#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
212 const uint8_t *
macs[EC_MAX_NUM_DEVICES];
213#if EC_MAX_NUM_DEVICES > 1
214 unsigned int num_devices;
322 const uint8_t *, dev_t,
struct class *,
unsigned int,
unsigned int);
327#if EC_MAX_NUM_DEVICES > 1
328#define ec_master_num_devices(MASTER) ((MASTER)->num_devices)
330#define ec_master_num_devices(MASTER) 1
347 const uint8_t *,
size_t);
381 uint16_t, uint32_t, uint32_t);
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.
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...
int ec_master_enter_operation_phase(ec_master_t *)
Transition function from IDLE to OPERATION phase.
ec_domain_t * ecrt_master_create_domain_err(ec_master_t *)
Same as ecrt_master_create_domain(), but with ERR_PTR() return value.
void ec_master_leave_operation_phase(ec_master_t *)
Transition function from OPERATION to IDLE phase.
unsigned int ec_master_domain_count(const ec_master_t *)
Get the number of domains.
void ec_master_init_static(void)
Static variables initializer.
const ec_domain_t * ec_master_find_domain_const(const ec_master_t *, unsigned int)
Get a domain via its position in the list.
void ec_master_leave_idle_phase(ec_master_t *)
Transition function from IDLE to ORPHANED phase.
ec_domain_t * ec_master_find_domain(ec_master_t *, unsigned int)
Get a domain via its position in the list.
unsigned int ec_master_config_count(const ec_master_t *)
Get the number of slave configurations provided by the application.
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.
int ec_master_debug_level(ec_master_t *, unsigned int)
Set the debug level.
void ec_master_clear_eoe_handlers(ec_master_t *)
Clear and free all EoE handlers.
uint16_t ec_master_eoe_handler_count(const ec_master_t *)
Get the number of EoE handlers.
int ec_master_enter_idle_phase(ec_master_t *)
Transition function from ORPHANED to IDLE phase.
void ec_master_clear(ec_master_t *)
Destructor.
void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *)
Places a datagram in the datagram queue.
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.
const unsigned int rate_intervals[EC_RATE_COUNT]
List of intervals for statistics [s].
#define EC_EXT_RING_SIZE
Size of the external datagram ring.
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.
void ec_master_clear_slaves(ec_master_t *)
Clear all slaves.
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.
void ec_master_output_stats(ec_master_t *)
Output master statistics.
void ec_master_request_op(ec_master_t *)
Request OP state for configured slaves.
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.
void ec_master_queue_datagram_ext(ec_master_t *, ec_datagram_t *)
Places a datagram in the non-application datagram queue.
void ec_master_attach_slave_configs(ec_master_t *)
Attaches the slave configurations to the slaves.
ec_slave_config_t * ec_master_get_config(const ec_master_t *, unsigned int)
Get a slave configuration via its position in the list.
void ec_master_calc_dc(ec_master_t *)
Distributed-clocks calculations.
void ec_master_eoe_stop(ec_master_t *)
Stops the Ethernet over EtherCAT processing.
void ec_master_internal_receive_cb(void *)
Internal receiving callback.
void ec_master_internal_send_cb(void *)
Internal sending callback.
void ec_master_eoe_start(ec_master_t *)
Starts Ethernet over EtherCAT processing on demand.
ec_master_phase_t
EtherCAT master phase.
@ EC_ORPHANED
Orphaned phase.
@ EC_OPERATION
Operation phase.
void ec_master_receive_datagrams(ec_master_t *, ec_device_t *, const uint8_t *, size_t)
Processes a received frame.
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.
EtherCAT master character device.
u64 tx_count
Number of frames sent.
u64 rx_bytes
Number of bytes received.
u64 last_loss
Tx/Rx difference of last statistics cycle.
u64 last_tx_bytes
Number of bytes sent of last statistics cycle.
u64 last_tx_count
Number of frames sent of last statistics cycle.
u64 tx_bytes
Number of bytes sent.
u64 rx_count
Number of frames received.
u64 last_rx_bytes
Number of bytes received of last statistics cycle.
u64 last_rx_count
Number of frames received of last statistics cycle.
unsigned long jiffies
Jiffies of last statistic cycle.
Ethernet over EtherCAT (EoE) handler.
Finite state machine of an EtherCAT master.
struct list_head datagram_queue
Datagram queue.
struct task_struct * eoe_thread
EoE thread.
unsigned int injection_seq_fsm
Datagram injection sequence number for the FSM side.
struct task_struct * thread
Master thread.
struct list_head eoe_handlers
Ethernet over EtherCAT handlers.
ec_datagram_t fsm_datagram
Datagram used for state machines.
struct list_head emerg_reg_requests
Emergency register access requests.
struct semaphore master_sem
Master semaphore.
struct list_head ext_datagram_queue
Queue for non-application datagrams.
ec_datagram_t sync_datagram
Datagram used for DC drift compensation.
size_t max_queue_size
Maximum size of datagram queue.
wait_queue_head_t request_queue
Wait queue for external requests from user space.
struct list_head fsm_exec_list
Slave FSM execution list.
struct list_head sii_requests
SII write requests.
ec_fsm_master_t fsm
Master state machine.
u64 app_time
Time of the last ecrt_master_sync() call.
wait_queue_head_t scan_queue
Queue for processes that wait for slave scanning.
struct semaphore io_sem
Semaphore used in IDLE phase.
unsigned int scan_busy
Current scan state.
unsigned int send_interval
Interval between two calls to ecrt_master_send().
unsigned int slave_count
Number of slaves on the bus.
void * app_cb_data
Application callback data.
void(* app_receive_cb)(void *)
Application's receive datagrams callback.
void(* receive_cb)(void *)
Current receive datagrams callback.
uint8_t datagram_index
Current datagram index.
unsigned int ext_ring_idx_fsm
Index in external datagram ring for FSM side.
ec_device_stats_t device_stats
Device statistics.
void(* send_cb)(void *)
Current send datagrams callback.
unsigned int config_busy
State of slave configuration.
ec_slave_t * dc_ref_clock
DC reference clock slave.
ec_datagram_t ref_sync_datagram
Datagram used for synchronizing the reference clock to the master clock.
struct list_head domains
List of domains.
struct semaphore config_sem
Semaphore protecting the config_busy variable and the allow_config flag.
unsigned int fsm_exec_count
Number of entries in execution list.
void(* app_send_cb)(void *)
Application's send datagrams callback.
u64 dc_ref_time
Common reference timestamp for DC start times.
ec_slave_t * fsm_slave
Slave that is queried next for FSM exec.
unsigned int injection_seq_rt
Datagram injection sequence number for the realtime side.
wait_queue_head_t config_queue
Queue for processes that wait for slave configuration.
struct semaphore scan_sem
Semaphore protecting the scan_busy variable and the allow_scan flag.
unsigned int run_on_cpu
bind kernel threads to this cpu
ec_datagram_t sync_mon_datagram
Datagram used for DC synchronisation monitoring.
ec_slave_t * slaves
Array of slaves on the bus.
unsigned int active
Master has been activated.
unsigned int ext_ring_idx_rt
Index in external datagram ring for RT side.
unsigned int debug_level
Master debug level.
ec_stats_t stats
Cyclic statistics.
ec_slave_config_t * dc_ref_config
Application-selected DC reference clock slave config.
struct device * class_device
Master class device.
struct semaphore device_sem
Device semaphore.
ec_cdev_t cdev
Master character device.
unsigned int reserved
True, if the master is in use.
unsigned int allow_scan
True, if slave scanning is allowed.
unsigned int config_changed
The configuration changed.
struct list_head configs
List of slave configurations.
const uint8_t * macs[EC_MAX_NUM_DEVICES]
Device MAC addresses.
ec_datagram_t ext_datagram_ring[EC_EXT_RING_SIZE]
External datagram ring.
ec_master_phase_t phase
Master phase.
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
void * cb_data
Current callback data.
struct semaphore ext_queue_sem
Semaphore protecting the ext_datagram_queue.
EtherCAT slave configuration.
unsigned int corrupted
corrupted frames
unsigned long output_jiffies
time of last output
unsigned int timeouts
datagram timeouts
unsigned int unmatched
unmatched datagrams (received, but not queued any longer)