libethercat - an embeddable realtime EtherCAT-master library
Communicate with EtherCAT slaves attached to a Network interface.
Loading...
Searching...
No Matches
datagram.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_DATAGRAM_H
42#define LIBETHERCAT_DATAGRAM_H
43
44#include <libosal/types.h>
45#include <libosal/mutex.h>
46
47#include "libethercat/common.h"
48#include "libethercat/pool.h"
49#include "libethercat/idx.h"
50
58#define EC_WKC_SIZE (2u)
59
60typedef struct __attribute__((__packed__)) ec_frame {
61 osal_uint8_t mac_dest[6];
62 osal_uint8_t mac_src[6];
63 osal_uint16_t ethertype;
64
65 osal_uint16_t len : 11;
66 osal_uint16_t reserved : 1;
67 osal_uint16_t type : 4;
69
70#define ec_frame_hdr_length (sizeof(ec_frame_t))
71#define ec_frame_length(f) ((f)->len)
72#define ec_frame_end(pframe) (&((osal_uint8_t *)(pframe))[(pframe)->len])
73
74typedef struct __attribute__((__packed__)) ec_datagram {
75 osal_uint8_t cmd;
76 osal_uint8_t idx;
77 osal_uint32_t adr;
78 // \brief auto inc address + phys mem
79 // \brief configured address + phys mem
80 osal_uint16_t len : 11;
81 osal_uint16_t reserved : 4;
82 osal_uint16_t next : 1;
83 osal_uint16_t irq;
85
86#define ec_datagram_hdr_length (sizeof(ec_datagram_t))
87#define ec_datagram_length(pdg) (ec_datagram_hdr_length + (pdg)->len + EC_WKC_SIZE)
88
89typedef struct ec_cyclic_datagram {
90 osal_mutex_t lock;
93
94 osal_uint64_t recv_timeout_ns;
95 osal_timer_t timeout;
96
97 void (*user_cb)(void *arg, int num);
100
101#ifdef __cplusplus
102extern "C" {
103#endif
104
106
114int ec_cyclic_datagram_init(ec_cyclic_datagram_t *cdg, osal_uint64_t recv_timeout);
115
117
125
127
135
137
148void ec_frame_add_datagram_phys(ec_frame_t *frame, osal_uint8_t cmd, osal_uint8_t idx,
149 osal_uint16_t adp, osal_uint16_t ado, osal_uint8_t *payload, osal_size_t payload_len);
150
152
162void ec_frame_add_datagram_log(ec_frame_t *frame, osal_uint8_t cmd, osal_uint8_t idx,
163 osal_uint32_t adr, osal_uint8_t *payload, osal_size_t payload_len);
164
166
171static inline ec_datagram_t *ec_datagram_cast(osal_uint8_t *p) {
172 // cppcheck-suppress misra-c2012-11.3
173 return ((ec_datagram_t *)(&((osal_uint8_t *)(p))[0]));
174}
175
177
180static inline void ec_datagram_mark_next(ec_datagram_t *pdg) {
181 // cppcheck-suppress misra-c2012-11.3
182 ((ec_datagram_t *)(pdg))->next = 1;
183}
184
186
192 // cppcheck-suppress misra-c2012-11.3
193 return (ec_datagram_t *)(&(((osal_uint8_t *)(pf))[sizeof(ec_frame_t)]));
194}
195
197
203 // cppcheck-suppress misra-c2012-11.3
204 return (ec_datagram_t *)(&(((osal_uint8_t *)(pdg))[ec_datagram_length((pdg))]));
205}
206
208
213static inline osal_uint8_t *ec_datagram_payload(ec_datagram_t *pdg) {
214 // cppcheck-suppress misra-c2012-11.3
215 return (&(((osal_uint8_t *)(pdg))[sizeof(ec_datagram_t)]));
216}
217
219
224static inline osal_uint16_t ec_datagram_wkc(ec_datagram_t *pdg) {
225 // cppcheck-suppress misra-c2012-11.3
226 return (*(osal_uint16_t *)(&(((osal_uint8_t *)pdg)[ec_datagram_length(pdg) - 2u])));
227}
228
229#ifdef __cplusplus
230}
231#endif
232
235#endif /* LIBETHERCAT_DATAGRAM_H */
236
ethercat master common stuff
static void ec_datagram_mark_next(ec_datagram_t *pdg)
Marking next field in datagram.
Definition datagram.h:180
void ec_frame_add_datagram_log(ec_frame_t *frame, osal_uint8_t cmd, osal_uint8_t idx, osal_uint32_t adr, osal_uint8_t *payload, osal_size_t payload_len)
Add datagram at the end of frame.
static osal_uint8_t * ec_datagram_payload(ec_datagram_t *pdg)
Get pointer to datagram payload.
Definition datagram.h:213
struct ec_cyclic_datagram ec_cyclic_datagram_t
EtherCAT cyclic datagram type.
static osal_uint16_t ec_datagram_wkc(ec_datagram_t *pdg)
Get working counter of datagram.
Definition datagram.h:224
static ec_datagram_t * ec_datagram_first(ec_frame_t *pf)
Get pointer to first datagram in frame.
Definition datagram.h:191
#define ec_datagram_length(pdg)
EtherCAT datagram length.
Definition datagram.h:87
static ec_datagram_t * ec_datagram_cast(osal_uint8_t *p)
Cast pointer to ec_datagram_t type.
Definition datagram.h:171
static ec_datagram_t * ec_datagram_next(ec_datagram_t *pdg)
Get pointer to next datagram in frame.
Definition datagram.h:202
int ec_cyclic_datagram_init(ec_cyclic_datagram_t *cdg, osal_uint64_t recv_timeout)
Initialize cyclic datagram structure.
int ec_cyclic_datagram_destroy(ec_cyclic_datagram_t *cdg)
Destroy cyclic datagram structure.
struct ec_frame ec_frame_t
EtherCAT frame type.
int ec_frame_init(ec_frame_t *frame)
Initialize empty frame.
void ec_frame_add_datagram_phys(ec_frame_t *frame, osal_uint8_t cmd, osal_uint8_t idx, osal_uint16_t adp, osal_uint16_t ado, osal_uint8_t *payload, osal_size_t payload_len)
Add datagram at the end of frame.
struct ec_datagram ec_datagram_t
EtherCAT datagram type.
ethercat index
Data pool implementation.
Definition datagram.h:89
void(* user_cb)(void *arg, int num)
User callback.
Definition datagram.h:97
pool_entry_t * p_entry
EtherCAT datagram from pool.
Definition datagram.h:91
osal_mutex_t lock
Lock for cyclic datagram structure.
Definition datagram.h:90
osal_timer_t timeout
Timer holding actual timeout.
Definition datagram.h:95
idx_entry_t * p_idx
EtherCAT datagram index from pool.
Definition datagram.h:92
void * user_cb_arg
User argument for user_cb.
Definition datagram.h:98
osal_uint64_t recv_timeout_ns
Datagram receive timeout in [ns].
Definition datagram.h:94
Definition datagram.h:74
osal_uint16_t reserved
not used
Definition datagram.h:81
osal_uint32_t adr
logical address
Definition datagram.h:77
osal_uint16_t len
datagram length
Definition datagram.h:80
osal_uint16_t irq
reserved for future use
Definition datagram.h:83
osal_uint8_t idx
datagram index
Definition datagram.h:76
osal_uint16_t next
0 - last datagram, 1 - more follow
Definition datagram.h:82
osal_uint8_t cmd
ethercat command
Definition datagram.h:75
Definition datagram.h:60
osal_uint16_t reserved
not used
Definition datagram.h:66
osal_uint16_t type
protocol type, 4 - EtherCAT command
Definition datagram.h:67
osal_uint16_t ethertype
ethertype, should be 0x88A4
Definition datagram.h:63
osal_uint16_t len
frame length
Definition datagram.h:65
index entry
Definition idx.h:54
Pool queue entry.
Definition pool.h:67