libethercat - an embeddable realtime EtherCAT-master library
Communicate with EtherCAT slaves attached to a Network interface.
Loading...
Searching...
No Matches
ec.h
Go to the documentation of this file.
1
13/*
14 * This file is part of libethercat.
15 *
16 * libethercat is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 3 of the License, or (at your option) any later version.
20 *
21 * libethercat is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with libethercat (LICENSE.LGPL-V3); if not, write
28 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
29 * Floor, Boston, MA 02110-1301, USA.
30 *
31 * Please note that the use of the EtherCAT technology, the EtherCAT
32 * brand name and the EtherCAT logo is only permitted if the property
33 * rights of Beckhoff Automation GmbH are observed. For further
34 * information please contact Beckhoff Automation GmbH & Co. KG,
35 * Hülshorstweg 20, D-33415 Verl, Germany (www.beckhoff.com) or the
36 * EtherCAT Technology Group, Ostendstraße 196, D-90482 Nuremberg,
37 * Germany (ETG, www.ethercat.org).
38 *
39 */
40
41#ifndef LIBETHERCAT_EC_H
42#define LIBETHERCAT_EC_H
43
44#include <libosal/types.h>
45
46#include "libethercat/settings.h"
47#include "libethercat/common.h"
48#include "libethercat/dc.h"
49#include "libethercat/slave.h"
50#include "libethercat/hw.h"
51#include "libethercat/regs.h"
52#include "libethercat/idx.h"
54#include "libethercat/pool.h"
55#include "libethercat/async_loop.h"
56#include "libethercat/eeprom.h"
57
58#if LIBETHERCAT_BUILD_POSIX == 1
59#include "libethercat/veth.h"
60#endif
61
69#define EC_SHORT_TIMEOUT_MBX (10000000)
70#define EC_DEFAULT_TIMEOUT_MBX (1000000000)
71#define EC_DEFAULT_DELAY (2000000)
72
73struct ec;
74struct ec_slave;
75typedef struct ec_slave ec_slave_t;
76
191
192typedef struct ec_statistics {
193 osal_uint64_t lost_datagrams;
195
197typedef struct ec {
198 struct hw_common *phw;
199
221
226
227 osal_uint16_t slave_cnt;
229
230 osal_uint16_t pd_group_cnt;
232
236
242#if LIBETHERCAT_BUILD_POSIX == 1
243 ec_veth_t veth;
244#endif
245
247
249 ec_state_t master_state;
251
253
259 void (*user_cb_state_transition)(void *arg, ec_t *pec, ec_state_t target_state, osal_bool_t up);
262
264
266
268
269 ec_statistics_t stats;
270
271 void *ec_time_func_user;
272 osal_uint64_t (*ec_time_func)(ec_t *pec);
273
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)));
277
278#ifdef __cplusplus
279extern "C" {
280#endif
281
282#define ec_log(lvl, pre, format, ...) \
283 _ec_log(pec, (lvl), (pre), (format), ##__VA_ARGS__)
284
286
294void _ec_log(ec_t *pec, int lvl, const osal_char_t *pre, const osal_char_t *format, ...) __attribute__ ((format (printf, 4, 5)));
295
297
311int ec_open(ec_t *pec, struct hw_common *phw, int eeprom_log);
312
314
320
322
329void ec_configure_dc(ec_t *pec, osal_uint64_t timer, ec_dc_mode_t mode,
330 void (*user_cb)(void *arg, int num), void *user_cb_arg);
331
333
339int ec_create_pd_groups(ec_t *pec, osal_uint32_t pd_group_cnt);
340
342
349void ec_configure_pd_group(ec_t *pec, osal_uint16_t group, int clock_divisor,
350 void (*user_cb)(void *arg, int num), void *user_cb_arg);
351
353
359
361
371int ec_transceive(ec_t *pec, osal_uint8_t cmd, osal_uint32_t adr,
372 osal_uint8_t *data, osal_size_t datalen, osal_uint16_t *wkc);
373
375
382int ec_set_state(ec_t *pec, ec_state_t state);
383
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)
386
388
393
395
401
403
411int ec_send_distributed_clocks_sync_with_rtc(ec_t *pec, osal_uint64_t act_rtc_time);
412
414
420
422
427
428#ifdef __cplusplus
429};
430#endif
431
433#define ec_to_adr(ado, adp) \
434 ((osal_uint32_t)(adp) << 16u) | ((ado) & 0xFFFFu)
435
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))
440
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))
445
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))
450
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))
455
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))
460
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))
465
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))
470
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))
475
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))
480
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))
485
487#define check_ret(fcn, ...) { \
488 if (fcn(__VA_ARGS__) != EC_OK) { \
489 ec_log(1, __func__, "" #fcn "(" #__VA_ARGS__ ") failed!\n"); \
490 } }
491
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__)
495
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__)
499
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__)
504
507#endif // LIBETHERCAT_EC_H
508
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 datagram
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
ethercat index
Data pool implementation.
EtherCAT slave functions.
Definition async_loop.h:88
Definition datagram.h:89
Definition dc.h:91
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
Definition slave.h:230
Definition ec.h:192
Definition veth.h:50
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