Skip to content

Commit

Permalink
Merge pull request robert-burger#15 from common/feat/hw_rework
Browse files Browse the repository at this point in the history
Feat/hw rework
  • Loading branch information
robert-burger authored and GitHub Enterprise committed Aug 21, 2024
2 parents 1a9cca4 + 0a22024 commit 536272d
Show file tree
Hide file tree
Showing 18 changed files with 796 additions and 367 deletions.
10 changes: 4 additions & 6 deletions include/libethercat/ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ typedef struct ec_pd_group {

//! ethercat master structure
typedef struct ec {
hw_t hw; //!< pointer to hardware interface
struct hw_common *phw; //!< pointer to hardware interface

pool_entry_t dg_entries[LEC_MAX_DATAGRAMS]; //!< static datagrams for datagram pool.
pool_t pool; //!< datagram pool
Expand Down Expand Up @@ -242,7 +242,7 @@ extern "C" {
#endif

extern void *ec_log_func_user;
extern void (*ec_log_func)(int lvl, void *user, const osal_char_t *format, ...);
extern void (*ec_log_func)(int lvl, void *user, const osal_char_t *format, ...) __attribute__ ((format (printf, 3, 4)));

//! \brief EtherCAT logging function
/*!
Expand All @@ -266,13 +266,11 @@ void ec_log(int lvl, const osal_char_t *pre, const osal_char_t *format, ...) __a
* a initial scan of the bus.
*
* \param[out] pec Ethercat master instance pointer.
* \param[in] ifname Ethercat master interface name.
* \param[in] prio Receive thread priority.
* \param[in] cpumask Receive thread cpumask.
* \param[in] phw Ethercat master network device access.
* \param[in] eeprom_log Log eeprom to stdout.
* \return 0 on succes, otherwise error code
*/
int ec_open(ec_t *pec, const osal_char_t *ifname, int prio, int cpumask, int eeprom_log);
int ec_open(ec_t *pec, struct hw_common *phw, int eeprom_log);

//! \brief Closes ethercat master.
/*!
Expand Down
64 changes: 21 additions & 43 deletions include/libethercat/hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
#include <drv/sbuf_hdr.h>
#endif

#define container_of(ptr, type, member) ({ \
__typeof__( ((type *)0)->member ) *__mptr = (void *)(ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

/** \defgroup hardware_group HW
*
* This modules contains main EtherCAT hardware functions.
Expand All @@ -64,15 +68,15 @@

// forward decl
struct ec;
struct hw;
struct hw_common;

//! Receive a frame from an EtherCAT hw device.
/*!
* \param[in] phw Pointer to hw handle.
*
* \return 0 or negative error code
*/
typedef int (*hw_device_recv_t)(struct hw *phw);
typedef int (*hw_device_recv_t)(struct hw_common *phw);

//! Send a frame from an EtherCAT hw device.
/*!
Expand All @@ -81,13 +85,13 @@ typedef int (*hw_device_recv_t)(struct hw *phw);
*
* \return 0 or negative error code
*/
typedef int (*hw_device_send_t)(struct hw *phw, ec_frame_t *pframe);
typedef int (*hw_device_send_t)(struct hw_common *phw, ec_frame_t *pframe);

//! Doing internal stuff when finished sending frames
/*!
* \param[in] phw Pointer to hw handle.
*/
typedef void (*hw_device_send_finished_t)(struct hw *phw);
typedef void (*hw_device_send_finished_t)(struct hw_common *phw);

//! Get a free tx buffer from underlying hw device.
/*!
Expand All @@ -96,19 +100,15 @@ typedef void (*hw_device_send_finished_t)(struct hw *phw);
*
* \return 0 or negative error code
*/
typedef int (*hw_device_get_tx_buffer_t)(struct hw *phw, ec_frame_t **ppframe);
typedef int (*hw_device_get_tx_buffer_t)(struct hw_common *phw, ec_frame_t **ppframe);

#define ETH_FRAME_LEN 0x1518

//! hardware structure
typedef struct hw {
typedef struct hw_common {
struct ec *pec; //!< Pointer to EtherCAT master structure.

int sockfd; //!< raw socket file descriptor
osal_uint32_t mtu_size; //!< mtu size

// receiver thread settings
osal_task_t rxthread; //!< receiver thread handle
int rxthreadrunning; //!< receiver thread running flag

osal_mutex_t hw_lock; //!< transmit lock

pool_t tx_high; //!< high priority datagrams
Expand All @@ -124,70 +124,48 @@ typedef struct hw {
hw_device_send_t send; //!< \brief Function to send frames via device.
hw_device_send_finished_t send_finished; //!< \brief Function to be called after frames were sent.
hw_device_get_tx_buffer_t get_tx_buffer; //!< \brief Function to retreave next TX buffer.

#define ETH_FRAME_LEN 0x1518
osal_uint8_t send_frame[ETH_FRAME_LEN]; //!< \brief Static send frame.
osal_uint8_t recv_frame[ETH_FRAME_LEN]; //!< \brief Static receive frame.
osal_bool_t polling_mode; //!< \brief Special interrupt-less polling-mode flag.

#if LIBETHERCAT_BUILD_DEVICE_SOCK_RAW_MMAPED == 1
int mmap_packets; //!< \brief Doing mmap packets.
osal_char_t *rx_ring; //!< kernel mmap receive buffers
osal_char_t *tx_ring; //!< kernel mmap send buffers

osal_off_t rx_ring_offset; //!< \brief Offset in RX ring.
osal_off_t tx_ring_offset; //!< \brief Offset in TX ring.
#endif

#if LIBETHERCAT_BUILD_DEVICE_PIKEOS == 1
osal_bool_t use_sbuf;

vm_file_desc_t fd; //!< \brief Driver file descriptor.
drv_sbuf_desc_t sbuf; //!< \brief Driver SBUF descriptor.
#endif
} hw_t; //!< \brief Hardware struct type.
} hw_common_t; //!< \brief Hardware struct type.

#ifdef __cplusplus
extern "C" {
#endif

//! open a new hw
/*!
* \param phw return hw
* \param devname ethernet device name
* \param prio receive thread prio
* \param cpumask receive thread cpumask
* \param[in] phw Pointer to hw structure.
* \param[in] pec Pointer to master structure.
*
* \return 0 or negative error code
*/
int hw_open(hw_t *phw, struct ec *pec, const osal_char_t *devname, int prio, int cpumask);
int hw_open(struct hw_common *phw, struct ec *pec);

//! destroys a hw
/*!
* \param phw hw handle
* \return 0 or negative error code
*/
int hw_close(hw_t *phw);
int hw_close(struct hw_common *phw);

//! start sending queued ethercat datagrams
/*!
* \param phw hardware handle
* \return 0 or error code
*/
int hw_tx_low(hw_t *phw);
int hw_tx_low(struct hw_common *phw);

//! start sending queued ethercat datagrams
/*!
* \param phw hardware handle
* \return 0 or error code
*/
int hw_tx(hw_t *phw);
int hw_tx(struct hw_common *phw);

//! Process a received EtherCAT frame
/*!
* \param[in] phw Pointer to hw handle.
* \param[in] pframe Pointer to received EtherCAT frame.
*/
void hw_process_rx_frame(hw_t *phw, ec_frame_t *pframe);
void hw_process_rx_frame(struct hw_common *phw, ec_frame_t *pframe);

#ifdef __cplusplus
}
Expand Down
18 changes: 17 additions & 1 deletion include/libethercat/hw_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,30 @@

#include <libethercat/hw.h>

typedef struct hw_file {
struct hw_common common;

int fd; //!< \brief file descriptor

osal_uint8_t send_frame[ETH_FRAME_LEN]; //!< \brief Static send frame.
osal_uint8_t recv_frame[ETH_FRAME_LEN]; //!< \brief Static receive frame.
osal_bool_t polling_mode; //!< \brief Special interrupt-less polling-mode flag.

// receiver thread settings in non-polling mode
osal_task_t rxthread; //!< receiver thread handle
int rxthreadrunning; //!< receiver thread running flag
} hw_file_t;

//! Opens EtherCAT hw device.
/*!
* \param[in] phw Pointer to hw handle.
* \param[in] devname Null-terminated string to EtherCAT hw device name.
* \param[in] prio Priority for receiver thread.
* \param[in] cpu_mask CPU mask for receiver thread.
*
* \return 0 or negative error code
*/
int hw_device_file_open(hw_t *phw, const osal_char_t *devname);
int hw_device_file_open(struct hw_file *phw, struct ec *pec, const osal_char_t *devname, int prio, int cpu_mask);

#endif // LIBETHERCAT_HW_FILE_H

21 changes: 20 additions & 1 deletion include/libethercat/hw_pikeos.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,33 @@

#include <libethercat/hw.h>

typedef struct hw_pikeos {
struct hw_common common;

osal_bool_t use_sbuf;

vm_file_desc_t fd; //!< \brief Driver file descriptor.
drv_sbuf_desc_t sbuf; //!< \brief Driver SBUF descriptor.

osal_uint8_t send_frame[ETH_FRAME_LEN]; //!< \brief Static send frame.
osal_uint8_t recv_frame[ETH_FRAME_LEN]; //!< \brief Static receive frame.

// receiver thread
osal_task_t rxthread; //!< receiver thread handle
int rxthreadrunning; //!< receiver thread running flag
} hw_pikeos_t;

//! Opens EtherCAT hw device.
/*!
* \param[in] phw Pointer to hw handle.
* \param[in] devname Null-terminated string to EtherCAT hw device name.
* \param[in] prio Priority for receiver thread.
* \param[in] cpu_mask CPU mask for receiver thread.
*
* \return 0 or negative error code
*/
int hw_device_pikeos_open(hw_t *phw, const osal_char_t *devname);
int hw_device_pikeos_open(struct hw_pikeos *phw, const osal_char_t *devname,
int prio, int cpumask);

#endif // LIBETHERCAT_HW_PIKEOS_H

Expand Down
20 changes: 19 additions & 1 deletion include/libethercat/hw_sock_raw.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,33 @@
#define LIBETHERCAT_HW_SOCK_RAW_H

#include <libethercat/hw.h>
#include <libethercat/ec.h>
#include <libosal/task.h>

typedef struct hw_sock_raw {
struct hw_common common;

int sockfd; //!< raw socket file descriptor

osal_uint8_t send_frame[ETH_FRAME_LEN]; //!< \brief Static send frame.
osal_uint8_t recv_frame[ETH_FRAME_LEN]; //!< \brief Static receive frame.

// receiver thread settings
osal_task_t rxthread; //!< receiver thread handle
int rxthreadrunning; //!< receiver thread running flag
} hw_sock_raw_t;

//! Opens EtherCAT hw device.
/*!
* \param[in] phw Pointer to hw handle.
* \param[in] pec Pointer to master structure.
* \param[in] devname Null-terminated string to EtherCAT hw device name.
* \param[in] prio Priority for receiver thread.
* \param[in] cpu_mask CPU mask for receiver thread.
*
* \return 0 or negative error code
*/
int hw_device_sock_raw_open(hw_t *phw, const osal_char_t *devname);
int hw_device_sock_raw_open(struct hw_sock_raw *phw, struct ec *pec, const osal_char_t *devname, int prio, int cpu_mask);

#endif // LIBETHERCAT_HW_SOCK_RAW_H

27 changes: 24 additions & 3 deletions include/libethercat/hw_sock_raw_mmaped.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,36 @@
#define LIBETHERCAT_HW_SOCK_RAW_MMAPED_H

#include <libethercat/hw.h>
#include <libosal/task.h>

typedef struct hw_sock_raw_mmaped {
struct hw_common common;

int sockfd; //!< raw socket file descriptor

osal_uint8_t send_frame[ETH_FRAME_LEN]; //!< \brief Static send frame.
osal_uint8_t recv_frame[ETH_FRAME_LEN]; //!< \brief Static receive frame.

int mmap_packets; //!< \brief Doing mmap packets.
osal_char_t *rx_ring; //!< kernel mmap receive buffers
osal_char_t *tx_ring; //!< kernel mmap send buffers

osal_off_t rx_ring_offset; //!< \brief Offset in RX ring.
osal_off_t tx_ring_offset; //!< \brief Offset in TX ring.

// receiver thread settings
osal_task_t rxthread; //!< receiver thread handle
int rxthreadrunning; //!< receiver thread running flag
} hw_sock_raw_mmaped_t;

//! Opens EtherCAT hw device.
/*!
* \param[in] phw Pointer to hw handle.
* \param[in] devname Null-terminated string to EtherCAT hw device name.
* \param[in] phw_sock_raw_mmaped Pointer to sock_raw_mmmaped hw handle.
* \param[in] devname Null-terminated string to EtherCAT hw device name.
*
* \return 0 or negative error code
*/
int hw_device_sock_raw_mmaped_open(hw_t *phw, const osal_char_t *devname);
int hw_device_sock_raw_mmaped_open(struct hw_sock_raw_mmaped *phw_sock_raw_mmaped, const osal_char_t *devname);

#endif // LIBETHERCAT_HW_SOCK_RAW_MMAPED_H

Expand Down
5 changes: 5 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@ libethercat_la_SOURCES = coe.c coe_master.c slave.c datagram.c pool.c async_loop
hw.c soe.c foe.c mbx.c eeprom.c dc.c idx.c mii.c eoe.c

if BUILD_DEVICE_SOCK_RAW_LEGACY
include_HEADERS += $(top_srcdir)/include/libethercat/hw_sock_raw.h
libethercat_la_SOURCES += hw_sock_raw.c
endif

if BUILD_DEVICE_SOCK_RAW_MMAPED
include_HEADERS += $(top_srcdir)/include/libethercat/hw_sock_raw_mmaped.h
libethercat_la_SOURCES += hw_sock_raw_mmaped.c
endif

if BUILD_DEVICE_FILE
include_HEADERS += $(top_srcdir)/include/libethercat/hw_file.h
libethercat_la_SOURCES += hw_file.c
endif

if BUILD_DEVICE_BPF
include_HEADERS += $(top_srcdir)/include/libethercat/hw_bpf.h
libethercat_la_SOURCES += hw_bpf.c
endif

if BUILD_PIKEOS
include_HEADERS += $(top_srcdir)/include/libethercat/hw_pikeos.h
libethercat_la_SOURCES += hw_pikeos.c
endif

Expand Down
4 changes: 2 additions & 2 deletions src/dc.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ static inline osal_uint64_t get_packet_duration(ec_t *pec) {
p_entry->user_cb = anon_cb;

// queue frame and trigger tx
pool_put(&pec->hw.tx_high, p_entry);
pool_put(&pec->phw->tx_high, p_entry);

osal_uint64_t start = osal_timer_gettime_nsec();
hw_tx(&pec->hw);
hw_tx(pec->phw);

// wait for completion
osal_timer_t to;
Expand Down
Loading

0 comments on commit 536272d

Please sign in to comment.