Skip to content

Commit

Permalink
cpu/native: enable CAN FD support
Browse files Browse the repository at this point in the history
As CAN FD is already supported by SocketCAN on Linux, just enable the
fdcan pseudomodule and allow CAN FD frames.

Signed-off-by: Gilles DOFFE <[email protected]>
  • Loading branch information
gdoffe committed Apr 5, 2024
1 parent b564305 commit c895744
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cpu/native/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ USEMODULE += periph

# UART is needed by startup.c
USEMODULE += periph_uart

USEMODULE += fdcan
28 changes: 16 additions & 12 deletions cpu/native/periph/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
#include "debug.h"

static int _init(candev_t *candev);
static int _send(candev_t *candev, const struct can_frame *frame);
static int _send(candev_t *candev, const can_frame_t *frame);
static void _isr(candev_t *candev);
static int _set(candev_t *candev, canopt_t opt, void *value, size_t value_len);
static int _get(candev_t *candev, canopt_t opt, void *value, size_t max_len);
static int _abort(candev_t *candev, const struct can_frame *frame);
static int _abort(candev_t *candev, const can_frame_t *frame);
static int _set_filter(candev_t *candev, const struct can_filter *filter);
static int _remove_filter(candev_t *candev, const struct can_filter *filter);
static int _power_up(candev_t *candev);
Expand All @@ -67,7 +67,7 @@ static const candev_driver_t candev_linux_driver = {
.remove_filter = _remove_filter,
};

static candev_event_t _can_error_to_can_evt(struct can_frame can_frame_err);
static candev_event_t _can_error_to_can_evt(can_frame_t can_frame_err);
static void _callback_can_sigio(int sock, void *arg);

can_conf_t candev_conf[CAN_DLL_NUMOF] = {
Expand All @@ -94,7 +94,7 @@ int can_init(can_t *dev, const can_conf_t *conf)
return 0;
}

static candev_event_t _can_error_to_can_evt(struct can_frame can_frame_err)
static candev_event_t _can_error_to_can_evt(can_frame_t can_frame_err)
{
candev_event_t can_evt = CANDEV_EVENT_NOEVENT;
can_err_mask_t can_err_type = can_frame_err.can_id & CAN_ERR_MASK;
Expand All @@ -103,7 +103,7 @@ static candev_event_t _can_error_to_can_evt(struct can_frame can_frame_err)
can_evt = CANDEV_EVENT_TX_ERROR;
}
else if (can_err_type & CAN_ERR_CRTL) {
switch(can_frame_err.data[1]) {
switch (can_frame_err.data[1]) {
case CAN_ERR_CRTL_RX_OVERFLOW:
can_evt = CANDEV_EVENT_RX_ERROR;
break;
Expand Down Expand Up @@ -168,7 +168,11 @@ static int _init(candev_t *candev)
can_err_mask_t err_mask = CAN_ERR_TX_TIMEOUT |
CAN_ERR_BUSOFF |
CAN_ERR_CRTL;
ret = real_setsockopt(dev->sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
ret = real_setsockopt(dev->sock, SOL_CAN_RAW,
#ifdef MODULE_FDCAN
CAN_RAW_FD_FRAMES |
#endif
CAN_RAW_ERR_FILTER,
&err_mask, sizeof(err_mask));

if (ret < 0) {
Expand Down Expand Up @@ -210,12 +214,12 @@ static int _init(candev_t *candev)
return 0;
}

static int _send(candev_t *candev, const struct can_frame *frame)
static int _send(candev_t *candev, const can_frame_t *frame)
{
int nbytes;
can_t *dev = (can_t *)candev;

nbytes = real_write(dev->sock, frame, sizeof(struct can_frame));
nbytes = real_write(dev->sock, frame, sizeof(can_frame_t));

if (nbytes < frame->len) {
real_printf("CAN write op failed, nbytes=%i\n", nbytes);
Expand All @@ -232,22 +236,22 @@ static int _send(candev_t *candev, const struct can_frame *frame)
static void _isr(candev_t *candev)
{
int nbytes;
struct can_frame rcv_frame;
can_frame_t rcv_frame;
can_t *dev = (can_t *)candev;

if (dev == NULL) {
return;
}

DEBUG("candev_native _isr: CAN SIGIO interrupt received, sock = %i\n", dev->sock);
nbytes = real_read(dev->sock, &rcv_frame, sizeof(struct can_frame));
nbytes = real_read(dev->sock, &rcv_frame, sizeof(can_frame_t));

if (nbytes < 0) { /* SIGIO signal was probably due to an error with the socket */
DEBUG("candev_native _isr: read: error during read\n");
return;
}

if (nbytes < (int)sizeof(struct can_frame)) {
if (nbytes < (int)sizeof(can_frame_t)) {
DEBUG("candev_native _isr: read: incomplete CAN frame\n");
return;
}
Expand Down Expand Up @@ -544,7 +548,7 @@ static int _remove_filter(candev_t *candev, const struct can_filter *filter)
return 0;
}

static int _abort(candev_t *candev, const struct can_frame *frame)
static int _abort(candev_t *candev, const can_frame_t *frame)
{
(void)frame;
(void)candev;
Expand Down

0 comments on commit c895744

Please sign in to comment.