41#ifndef LIBETHERCAT_EC_H
42#define LIBETHERCAT_EC_H
44#include <libosal/types.h>
46#include "libethercat/settings.h"
51#include "libethercat/regs.h"
55#include "libethercat/async_loop.h"
58#if LIBETHERCAT_BUILD_POSIX == 1
59#include "libethercat/veth.h"
69#define EC_SHORT_TIMEOUT_MBX (10000000)
70#define EC_DEFAULT_TIMEOUT_MBX (1000000000)
71#define EC_DEFAULT_DELAY (2000000)
193 osal_uint64_t lost_datagrams;
242#if LIBETHERCAT_BUILD_POSIX == 1
271 void *ec_time_func_user;
272 osal_uint64_t (*ec_time_func)(
ec_t *pec);
274 void *ec_log_func_user;
275 void (*ec_log_func)(
ec_t *pec,
int lvl,
const osal_char_t *format, ...) __attribute__ ((format (printf, 3, 4)));
282#define ec_log(lvl, pre, format, ...) \
283 _ec_log(pec, (lvl), (pre), (format), ##__VA_ARGS__)
294void _ec_log(
ec_t *
pec,
int lvl,
const osal_char_t *pre,
const osal_char_t *format, ...) __attribute__ ((format (printf, 4, 5)));
330 void (*user_cb)(
void *arg,
int num),
void *user_cb_arg);
350 void (*user_cb)(
void *arg,
int num),
void *user_cb_arg);
372 osal_uint8_t *data, osal_size_t datalen, osal_uint16_t *wkc);
384#define ec_group_will_be_sent(pec, group) (int)((((pec)->pd_groups[(group)].divisor_cnt+1) % (pec)->pd_groups[(group)].divisor) == 0)
385#define ec_group_was_sent(pec, group) (int)((pec)->pd_groups[(group)].divisor_cnt == 0)
433#define ec_to_adr(ado, adp) \
434 ((osal_uint32_t)(adp) << 16u) | ((ado) & 0xFFFFu)
437#define ec_brd(pec, ado, data, datalen, wkc) \
438 ec_transceive((pec), EC_CMD_BRD, ((osal_uint32_t)(ado) << 16u), \
439 (osal_uint8_t *)(data), (datalen), (wkc))
442#define ec_bwr(pec, ado, data, datalen, wkc) \
443 ec_transceive((pec), EC_CMD_BWR, ((osal_uint32_t)(ado) << 16u), \
444 (osal_uint8_t *)(data), (datalen), (wkc))
447#define ec_brw(pec, ado, data, datalen, wkc) \
448 ec_transceive((pec), EC_CMD_BRW, ((osal_uint32_t)(ado) << 16u), \
449 (osal_uint8_t *)(data), (datalen), (wkc))
452#define ec_aprd(pec, adp, ado, data, datalen, wkc) \
453 ec_transceive((pec), EC_CMD_APRD, ((osal_uint32_t)(ado) << 16u) | \
454 (*(osal_uint16_t *)&(adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
457#define ec_apwr(pec, adp, ado, data, datalen, wkc) \
458 ec_transceive((pec), EC_CMD_APWR, ((osal_uint32_t)(ado) << 16u) | \
459 (*(osal_uint16_t *)&(adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
462#define ec_aprw(pec, adp, ado, data, datalen, wkc) \
463 ec_transceive((pec), EC_CMD_APRW, ((osal_uint32_t)(ado) << 16u) | \
464 (*(osal_uint16_t *)&(adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
467#define ec_fprd(pec, adp, ado, data, datalen, wkc) \
468 ec_transceive((pec), EC_CMD_FPRD, ((osal_uint32_t)(ado) << 16lu) | \
469 (osal_uint32_t)((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
472#define ec_fpwr(pec, adp, ado, data, datalen, wkc) \
473 ec_transceive((pec), EC_CMD_FPWR, ((osal_uint32_t)(ado) << 16u) | \
474 ((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
477#define ec_fprw(pec, adp, ado, data, datalen, wkc) \
478 ec_transceive((pec), EC_CMD_FPRW, ((osal_uint32_t)(ado) << 16u) | \
479 ((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
482#define ec_frmw(pec, adp, ado, data, datalen, wkc) \
483 ec_transceive((pec), EC_CMD_FRMW, ((osal_uint32_t)(ado) << 16u) | \
484 ((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
487#define check_ret(fcn, ...) { \
488 if (fcn(__VA_ARGS__) != EC_OK) { \
489 ec_log(1, __func__, "" #fcn "(" #__VA_ARGS__ ") failed!\n"); \
492#define check_ec_bwr(...) check_ret(ec_bwr, __VA_ARGS__)
493#define check_ec_brd(...) check_ret(ec_brd, __VA_ARGS__)
494#define check_ec_brw(...) check_ret(ec_brw, __VA_ARGS__)
496#define check_ec_apwr(...) check_ret(ec_apwr, __VA_ARGS__)
497#define check_ec_aprd(...) check_ret(ec_aprd, __VA_ARGS__)
498#define check_ec_aprw(...) check_ret(ec_aprw, __VA_ARGS__)
500#define check_ec_fpwr(...) check_ret(ec_fpwr, __VA_ARGS__)
501#define check_ec_fprd(...) check_ret(ec_fprd, __VA_ARGS__)
502#define check_ec_fprw(...) check_ret(ec_fprw, __VA_ARGS__)
503#define check_ec_frmw(...) check_ret(ec_frmw, __VA_ARGS__)
ethercat master common stuff
#define LEC_MAX_PDLEN
Maximum process data length.
Definition common.h:71
#define LEC_MAX_MBX_ENTRIES
Maximum number of mailbox entries.
Definition common.h:79
#define LEC_MAX_DATAGRAMS
Maximum number of datagrams.
Definition common.h:111
#define LEC_MAX_SLAVES
Maximum number of EtherCAT slaves supported.
Definition common.h:55
#define LEC_MAX_GROUPS
Maximum number of EtherCAT groups supported.
Definition common.h:63
ethercat distributed clocks support.
EtherCAT eeprom access fuctions.
int ec_send_process_data(ec_t *pec)
send process data with logical commands
void ec_configure_pd_group(ec_t *pec, osal_uint16_t group, int clock_divisor, void(*user_cb)(void *arg, int num), void *user_cb_arg)
Configure process data group settings.
struct ec ec_t
ethercat master structure
int ec_destroy_pd_groups(ec_t *pec)
Destroy process data groups.
int ec_open(ec_t *pec, struct hw_common *phw, int eeprom_log)
Open ethercat master.
struct ec_pd_group ec_pd_group_t
process data group structure
int ec_close(ec_t *pec)
Closes ethercat master.
int ec_send_distributed_clocks_sync(ec_t *pec)
Send distributed clocks sync datagram.
int ec_transceive(ec_t *pec, osal_uint8_t cmd, osal_uint32_t adr, osal_uint8_t *data, osal_size_t datalen, osal_uint16_t *wkc)
Syncronous ethercat read/write.
int ec_send_brd_ec_state(ec_t *pec)
Send broadcast read to ec state.
int ec_send_distributed_clocks_sync_with_rtc(ec_t *pec, osal_uint64_t act_rtc_time)
Send distributed clock sync datagram.
int ec_set_state(ec_t *pec, ec_state_t state)
Set state on ethercat bus.
void _ec_log(ec_t *pec, int lvl, const osal_char_t *pre, const osal_char_t *format,...)
EtherCAT logging function.
int ec_create_pd_groups(ec_t *pec, osal_uint32_t pd_group_cnt)
Create process data groups.
int ec_get_slave_count(ec_t *pec)
Return current slave count.
void ec_configure_dc(ec_t *pec, osal_uint64_t timer, ec_dc_mode_t mode, void(*user_cb)(void *arg, int num), void *user_cb_arg)
Configures distributed clocks settings on EtherCAT master.
hardware access functions
Data pool implementation.
EtherCAT slave functions.
Definition async_loop.h:88
process data group structure
Definition ec.h:78
int wkc_mismatch_cnt_lrw
LRW missed counter to avoid flooding log output.
Definition ec.h:155
int recv_missed_lwr
Missed continues LWR ethercat frames.
Definition ec.h:161
ec_cyclic_datagram_t cdg
Group cyclic datagram LRW case.
Definition ec.h:182
osal_uint16_t wkc_expected_lrw
expected working counter
Definition ec.h:127
int overlapping
Logical areas (inputs and outputs) are overlapping.
Definition ec.h:116
osal_uint32_t log_mbx_state
logical address mailbox state.
Definition ec.h:164
ec_cyclic_datagram_t cdg_lrd_mbx_state
Group cyclic datagram LRD mailbox state.
Definition ec.h:185
osal_size_t pd_lrw_len
inputs and outputs length if lrw is used
Definition ec.h:107
osal_uint32_t log_len
byte length at logical address
Definition ec.h:88
osal_uint16_t wkc_expected_mbx_state
Expected working counter.
Definition ec.h:172
osal_uint16_t wkc_expected_lwr
Expected working couinter LWR.
Definition ec.h:146
int use_lrw
LRW flag.
Definition ec.h:109
int wkc_mismatch_cnt_lrd
LRD missed counter to avoid flooding log output.
Definition ec.h:156
osal_size_t pdout_len
length of process data outputs
Definition ec.h:105
int wkc_mismatch_cnt_mbx_state
MBX state command missed counter to avoid.
Definition ec.h:179
int recv_missed_lrd
Missed continues LRD ethercat frames.
Definition ec.h:160
int divisor
Timer Divisor.
Definition ec.h:188
osal_uint32_t group
Number of group.
Definition ec.h:79
osal_size_t pdin_len
length of process data inputs
Definition ec.h:106
int wkc_mismatch_cnt_lwr
LWR missed counter to avoid flooding log output.
Definition ec.h:157
int skip_pd_on_wkc_mismatch
mismatch flag
Definition ec.h:118
int divisor_cnt
Actual timer cycle count.
Definition ec.h:189
osal_uint32_t log_mbx_state_len
Byte lenght at logical address mailbox state.
Definition ec.h:170
osal_uint16_t wkc_expected_lrd
Expected working couinter LRD.
Definition ec.h:137
osal_uint8_t pd[LEC_MAX_PDLEN]
process data pointer
Definition ec.h:96
int recv_missed_lrw
Missed continues LRW ethercat frames.
Definition ec.h:159
ec_cyclic_datagram_t cdg_lwr
Group cyclic datagram LWR case.
Definition ec.h:184
ec_cyclic_datagram_t cdg_lrd
Group cyclic datagram LRD case.
Definition ec.h:183
osal_uint32_t log
logical address
Definition ec.h:80
ethercat master structure
Definition ec.h:197
pool_t mbx_message_pool_recv_free
Pool with free receive mailbox buffers.
Definition ec.h:224
osal_uint16_t pd_group_cnt
count of process data groups
Definition ec.h:230
osal_uint16_t slave_cnt
count of found EtherCAT slaves
Definition ec.h:227
ec_state_t master_state
expected EtherCAT master state
Definition ec.h:249
ec_cyclic_datagram_t cdg_state
Monitor EtherCAT AL Status from slaves.
Definition ec.h:267
ec_dc_info_t dc
distributed clocks master settings
Definition ec.h:233
int consecutive_max_miss
max missed counter for receive frames before falling back to init
Definition ec.h:265
struct hw_common * phw
pointer to hardware interface
Definition ec.h:198
int state_transition_pending
state transition is currently pending
Definition ec.h:250
int threaded_startup
running state machine in threads for slave
Definition ec.h:263
idx_queue_t idx_q
index queue
Definition ec.h:209
osal_int64_t main_cycle_interval
Expected timer increment of one EtherCAT cycle in [ns].
Definition ec.h:219
pool_entry_t mbx_mp_recv_free_entries[LEC_MAX_MBX_ENTRIES]
static buffers for mailbox receive pool.
Definition ec.h:222
pool_t mbx_message_pool_send_free
Pool with free send mailbox buffers.
Definition ec.h:225
void * user_cb_state_transition_arg
Arg for the callback.
Definition ec.h:261
pool_entry_t dg_entries[LEC_MAX_DATAGRAMS]
static datagrams for datagram pool.
Definition ec.h:200
ec_async_loop_t async_loop
asynchronous message loop
Definition ec.h:234
pool_t mbx_gw_recv_pool
receive mbx gateway message pool
Definition ec.h:246
ec_slave_t slaves[LEC_MAX_SLAVES]
array with EtherCAT slaves
Definition ec.h:228
void(* user_cb_state_transition)(void *arg, ec_t *pec, ec_state_t target_state, osal_bool_t up)
The callback is executed before each state transition.
Definition ec.h:259
pool_t pool
datagram pool
Definition ec.h:201
int eeprom_log
flag whether to log eeprom to stdout
Definition ec.h:248
pool_entry_t mbx_mp_send_free_entries[LEC_MAX_MBX_ENTRIES]
static buffers for mailbox send pool.
Definition ec.h:223
ec_pd_group_t pd_groups[LEC_MAX_GROUPS]
array with process data groups
Definition ec.h:231
hardware structure
Definition hw.h:121
struct ec * pec
Pointer to EtherCAT master structure.
Definition hw.h:122
index queue
Definition idx.h:63
Pool queue entry.
Definition pool.h:67
the datagram pool itself
Definition pool.h:84