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/common.h"
47#include "libethercat/dc.h"
48#include "libethercat/slave.h"
49#include "libethercat/hw.h"
50#include "libethercat/regs.h"
51#include "libethercat/idx.h"
53#include "libethercat/pool.h"
54#include "libethercat/async_loop.h"
55#include "libethercat/eeprom.h"
56
64#define EC_SHORT_TIMEOUT_MBX (10000000)
65#define EC_DEFAULT_TIMEOUT_MBX (1000000000)
66#define EC_DEFAULT_DELAY (2000000)
67
68struct ec;
69struct ec_slave;
70typedef struct ec_slave ec_slave_t;
71
186
251
252#ifdef __cplusplus
253extern "C" {
254#endif
255
256#define ec_log(lvl, pre, format, ...) \
257 _ec_log(pec, (lvl), (pre), (format), ##__VA_ARGS__)
258
260
268void _ec_log(ec_t *pec, int lvl, const osal_char_t *pre, const osal_char_t *format, ...) __attribute__ ((format (printf, 4, 5)));
269
271
285int ec_open(ec_t *pec, struct hw_common *phw, int eeprom_log);
286
288
294
296
302int ec_configure_tun(ec_t *pec, osal_uint8_t ip_address[4]);
303
305
312void ec_configure_dc(ec_t *pec, osal_uint64_t timer, ec_dc_mode_t mode,
313 void (*user_cb)(void *arg, int num), void *user_cb_arg);
314
316
322int ec_create_pd_groups(ec_t *pec, osal_uint32_t pd_group_cnt);
323
325
332void ec_configure_pd_group(ec_t *pec, osal_uint16_t group, int clock_divisor,
333 void (*user_cb)(void *arg, int num), void *user_cb_arg);
334
336
342
344
354int ec_transceive(ec_t *pec, osal_uint8_t cmd, osal_uint32_t adr,
355 osal_uint8_t *data, osal_size_t datalen, osal_uint16_t *wkc);
356
358
367int ec_transmit_no_reply(ec_t *pec, osal_uint8_t cmd, osal_uint32_t adr,
368 osal_uint8_t *data, osal_size_t datalen);
369
371
378int ec_set_state(ec_t *pec, ec_state_t state);
379
380#define ec_group_will_be_sent(pec, group) (int)((((pec)->pd_groups[(group)].divisor_cnt+1) % (pec)->pd_groups[(group)].divisor) == 0)
381#define ec_group_was_sent(pec, group) (int)((pec)->pd_groups[(group)].divisor_cnt == 0)
382
384
389
391
397
399
407int ec_send_distributed_clocks_sync_with_rtc(ec_t *pec, osal_uint64_t act_rtc_time);
408
410
416
418
423
424#ifdef __cplusplus
425};
426#endif
427
429#define ec_to_adr(ado, adp) \
430 ((osal_uint32_t)(adp) << 16u) | ((ado) & 0xFFFFu)
431
433#define ec_brd(pec, ado, data, datalen, wkc) \
434 ec_transceive((pec), EC_CMD_BRD, ((osal_uint32_t)(ado) << 16u), \
435 (osal_uint8_t *)(data), (datalen), (wkc))
436
438#define ec_bwr(pec, ado, data, datalen, wkc) \
439 ec_transceive((pec), EC_CMD_BWR, ((osal_uint32_t)(ado) << 16u), \
440 (osal_uint8_t *)(data), (datalen), (wkc))
441
443#define ec_brw(pec, ado, data, datalen, wkc) \
444 ec_transceive((pec), EC_CMD_BRW, ((osal_uint32_t)(ado) << 16u), \
445 (osal_uint8_t *)(data), (datalen), (wkc))
446
448#define ec_aprd(pec, adp, ado, data, datalen, wkc) \
449 ec_transceive((pec), EC_CMD_APRD, ((osal_uint32_t)(ado) << 16u) | \
450 (*(osal_uint16_t *)&(adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
451
453#define ec_apwr(pec, adp, ado, data, datalen, wkc) \
454 ec_transceive((pec), EC_CMD_APWR, ((osal_uint32_t)(ado) << 16u) | \
455 (*(osal_uint16_t *)&(adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
456
458#define ec_aprw(pec, adp, ado, data, datalen, wkc) \
459 ec_transceive((pec), EC_CMD_APRW, ((osal_uint32_t)(ado) << 16u) | \
460 (*(osal_uint16_t *)&(adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
461
463#define ec_fprd(pec, adp, ado, data, datalen, wkc) \
464 ec_transceive((pec), EC_CMD_FPRD, ((osal_uint32_t)(ado) << 16lu) | \
465 (osal_uint32_t)((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
466
468#define ec_fpwr(pec, adp, ado, data, datalen, wkc) \
469 ec_transceive((pec), EC_CMD_FPWR, ((osal_uint32_t)(ado) << 16u) | \
470 ((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
471
473#define ec_fprw(pec, adp, ado, data, datalen, wkc) \
474 ec_transceive((pec), EC_CMD_FPRW, ((osal_uint32_t)(ado) << 16u) | \
475 ((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
476
478#define ec_frmw(pec, adp, ado, data, datalen, wkc) \
479 ec_transceive((pec), EC_CMD_FRMW, ((osal_uint32_t)(ado) << 16u) | \
480 ((adp) & 0xFFFFu), (osal_uint8_t *)(data), (datalen), (wkc))
481
483#define check_ret(fcn, ...) { \
484 if (fcn(__VA_ARGS__) != EC_OK) { \
485 ec_log(1, __func__, "" #fcn "(" #__VA_ARGS__ ") failed!\n"); \
486 } }
487
488#define check_ec_bwr(...) check_ret(ec_bwr, __VA_ARGS__)
489#define check_ec_brd(...) check_ret(ec_brd, __VA_ARGS__)
490#define check_ec_brw(...) check_ret(ec_brw, __VA_ARGS__)
491
492#define check_ec_apwr(...) check_ret(ec_apwr, __VA_ARGS__)
493#define check_ec_aprd(...) check_ret(ec_aprd, __VA_ARGS__)
494#define check_ec_aprw(...) check_ret(ec_aprw, __VA_ARGS__)
495
496#define check_ec_fpwr(...) check_ret(ec_fpwr, __VA_ARGS__)
497#define check_ec_fprd(...) check_ret(ec_fprd, __VA_ARGS__)
498#define check_ec_fprw(...) check_ret(ec_fprw, __VA_ARGS__)
499#define check_ec_frmw(...) check_ret(ec_frmw, __VA_ARGS__)
500
503#endif // LIBETHERCAT_EC_H
504
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_configure_tun(ec_t *pec, osal_uint8_t ip_address[4])
Configures tun device of EtherCAT master, used for EoE slaves.
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_transmit_no_reply(ec_t *pec, osal_uint8_t cmd, osal_uint32_t adr, osal_uint8_t *data, osal_size_t datalen)
Asyncronous ethercat read/write, answer don't care.
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:87
Definition datagram.h:89
Definition dc.h:92
process data group structure
Definition ec.h:73
int wkc_mismatch_cnt_lrw
LRW missed counter to avoid flooding log output.
Definition ec.h:150
int recv_missed_lwr
Missed continues LWR ethercat frames.
Definition ec.h:156
ec_cyclic_datagram_t cdg
Group cyclic datagram LRW case.
Definition ec.h:177
osal_uint16_t wkc_expected_lrw
expected working counter
Definition ec.h:122
int overlapping
Logical areas (inputs and outputs) are overlapping.
Definition ec.h:111
osal_uint32_t log_mbx_state
logical address mailbox state.
Definition ec.h:159
ec_cyclic_datagram_t cdg_lrd_mbx_state
Group cyclic datagram LRD mailbox state.
Definition ec.h:180
osal_size_t pd_lrw_len
inputs and outputs length if lrw is used
Definition ec.h:102
osal_uint32_t log_len
byte length at logical address
Definition ec.h:83
osal_uint16_t wkc_expected_mbx_state
Expected working counter.
Definition ec.h:167
osal_uint16_t wkc_expected_lwr
Expected working couinter LWR.
Definition ec.h:141
int use_lrw
LRW flag.
Definition ec.h:104
int wkc_mismatch_cnt_lrd
LRD missed counter to avoid flooding log output.
Definition ec.h:151
osal_size_t pdout_len
length of process data outputs
Definition ec.h:100
int wkc_mismatch_cnt_mbx_state
MBX state command missed counter to avoid.
Definition ec.h:174
int recv_missed_lrd
Missed continues LRD ethercat frames.
Definition ec.h:155
int divisor
Timer Divisor.
Definition ec.h:183
osal_uint32_t group
Number of group.
Definition ec.h:74
osal_size_t pdin_len
length of process data inputs
Definition ec.h:101
int wkc_mismatch_cnt_lwr
LWR missed counter to avoid flooding log output.
Definition ec.h:152
int skip_pd_on_wkc_mismatch
mismatch flag
Definition ec.h:113
int divisor_cnt
Actual timer cycle count.
Definition ec.h:184
osal_uint32_t log_mbx_state_len
Byte lenght at logical address mailbox state.
Definition ec.h:165
osal_uint16_t wkc_expected_lrd
Expected working couinter LRD.
Definition ec.h:132
osal_uint8_t pd[LEC_MAX_PDLEN]
process data pointer
Definition ec.h:91
int recv_missed_lrw
Missed continues LRW ethercat frames.
Definition ec.h:154
ec_cyclic_datagram_t cdg_lwr
Group cyclic datagram LWR case.
Definition ec.h:179
ec_cyclic_datagram_t cdg_lrd
Group cyclic datagram LRD case.
Definition ec.h:178
osal_uint32_t log
logical address
Definition ec.h:75
Definition slave.h:229
ethercat master structure
Definition ec.h:188
pool_t mbx_message_pool_recv_free
Pool with free receive mailbox buffers.
Definition ec.h:215
int tun_fd
tun device file descriptor
Definition ec.h:233
osal_uint32_t tun_ip
tun device ip addres
Definition ec.h:234
osal_uint16_t pd_group_cnt
count of process data groups
Definition ec.h:221
osal_uint16_t slave_cnt
count of found EtherCAT slaves
Definition ec.h:218
ec_state_t master_state
expected EtherCAT master state
Definition ec.h:239
ec_cyclic_datagram_t cdg_state
Monitor EtherCAT AL Status from slaves.
Definition ec.h:246
int tun_running
tun device handler run flag.
Definition ec.h:236
ec_dc_info_t dc
distributed clocks master settings
Definition ec.h:224
int consecutive_max_miss
max missed counter for receive frames before falling back to init
Definition ec.h:244
struct hw_common * phw
pointer to hardware interface
Definition ec.h:189
int state_transition_pending
state transition is currently pending
Definition ec.h:240
osal_task_t tun_tid
tun device handler thread id.
Definition ec.h:235
int threaded_startup
running state machine in threads for slave
Definition ec.h:242
idx_queue_t idx_q
index queue
Definition ec.h:200
osal_int64_t main_cycle_interval
Expected timer increment of one EtherCAT cycle in [ns].
Definition ec.h:210
pool_entry_t mbx_mp_recv_free_entries[LEC_MAX_MBX_ENTRIES]
static buffers for mailbox receive pool.
Definition ec.h:213
pool_t mbx_message_pool_send_free
Pool with free send mailbox buffers.
Definition ec.h:216
pool_entry_t dg_entries[LEC_MAX_DATAGRAMS]
static datagrams for datagram pool.
Definition ec.h:191
ec_async_loop_t async_loop
asynchronous message loop
Definition ec.h:225
ec_slave_t slaves[LEC_MAX_SLAVES]
array with EtherCAT slaves
Definition ec.h:219
pool_t pool
datagram pool
Definition ec.h:192
int eeprom_log
flag whether to log eeprom to stdout
Definition ec.h:238
pool_entry_t mbx_mp_send_free_entries[LEC_MAX_MBX_ENTRIES]
static buffers for mailbox send pool.
Definition ec.h:214
ec_pd_group_t pd_groups[LEC_MAX_GROUPS]
array with process data groups
Definition ec.h:222
hardware structure
Definition hw.h:123
struct ec * pec
Pointer to EtherCAT master structure.
Definition hw.h:124
index queue
Definition idx.h:63
Pool queue entry.
Definition pool.h:67
the datagram pool itself
Definition pool.h:81