Skip to content

Commit

Permalink
MERGECOMMIT: periph_can,candev: socketcan pkg for native RIOT-OS#17533
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLockau-MLPA authored and fjmolinas committed Apr 14, 2022
1 parent 32790eb commit 71a8e27
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 48 deletions.
1 change: 1 addition & 0 deletions boards/native/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ifneq (,$(filter mtd,$(USEMODULE)))
endif

ifneq (,$(filter periph_can,$(FEATURES_USED)))
USEPKG += libsocketcan
ifeq ($(OS),Linux)
CFLAGS += -DCAN_DLL_NUMOF=2
endif
Expand Down
5 changes: 4 additions & 1 deletion cpu/native/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ ifeq ($(BUILDOSXNATIVE),1)
endif

ifneq (,$(filter periph_can,$(USEMODULE)))
LINKFLAGS += -lsocketcan
ifeq (,$(filter libsocketcan,$(USEPKG)))
# link system libsocketcan if not using the provided package
LINKFLAGS += -lsocketcan
endif
endif

TOOLCHAINS_SUPPORTED = gnu llvm afl
Expand Down
10 changes: 10 additions & 0 deletions pkg/libsocketcan/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) ML!PA Consulting GmbH
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#

config PACKAGE_LIBSOCKETCAN
bool "libsocketcan2 32bit for native Linux builds"
depends on BOARD_NATIVE
9 changes: 9 additions & 0 deletions pkg/libsocketcan/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PKG_NAME=libsocketcan
PKG_URL=https://git.pengutronix.de/git/tools/libsocketcan
PKG_VERSION=077def398ad303043d73339112968e5112d8d7c8
PKG_LICENSE=LGPL-2.1

include $(RIOTBASE)/pkg/pkg.mk

all:
$(QQ)"$(MAKE)" -C $(PKG_SOURCE_DIR)/src -f $(CURDIR)/Makefile.$(PKG_NAME)
1 change: 1 addition & 0 deletions pkg/libsocketcan/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NATIVEINCLUDES += -I"$(PKGDIRBASE)/libsocketcan/include"
9 changes: 9 additions & 0 deletions pkg/libsocketcan/Makefile.libsocketcan
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MODULE=libsocketcan

CFLAGS += -Wno-strict-prototypes
CFLAGS += -Wno-pointer-arith
CFLAGS += -Wno-old-style-definition

SRC += libsocketcan.c

include $(RIOTBASE)/Makefile.base
16 changes: 16 additions & 0 deletions pkg/libsocketcan/doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @defgroup pkg_libsocketcan Socketcan library
* @ingroup pkg
* @ingroup sys_serialization
* @brief Provides libsocketcan for native 32bit linux builds when not available otherwise
*
* # Introduction
*
* "This library allows you to control some basic functions in SocketCAN from userspace."
*
* # License
*
* Licensed under LGPL 2.1.
*
* @see https://git.pengutronix.de/cgit/tools/libsocketcan
*/
14 changes: 0 additions & 14 deletions tests/candev/Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
BOARD ?= nucleo-f767zi

include ../Makefile.tests_common

USEMODULE += shell
USEMODULE += can
USEMODULE += isrpipe


# define the CAN driver you want to use here
CAN_DRIVER ?= MCP2515

ifeq ($(CAN_DRIVER), PERIPH_CAN)
FEATURES_REQUIRED += periph_can
else ifeq ($(CAN_DRIVER), MCP2515)
USEMODULE += mcp2515
else ifeq ($(CAN_DRIVER), CAN_ALT)
# other can modules can be defined here
endif

include $(RIOTBASE)/Makefile.include
14 changes: 14 additions & 0 deletions tests/candev/Makefile.board.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ifneq (,$(filter native,$(BOARD)))
CAN_DRIVER ?= PERIPH_CAN
endif

# define the CAN driver you want to use here
CAN_DRIVER ?= MCP2515

ifeq ($(CAN_DRIVER), PERIPH_CAN)
FEATURES_REQUIRED += periph_can
else ifeq ($(CAN_DRIVER), MCP2515)
USEMODULE += mcp2515
else ifeq ($(CAN_DRIVER), CAN_ALT)
# other can modules can be defined here
endif
52 changes: 19 additions & 33 deletions tests/candev/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
* @}
*/

#define ENABLE_DEBUG 0

#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <isrpipe.h>
#include <stdio.h>
Expand All @@ -49,6 +46,9 @@ static candev_mcp2515_t mcp2515_dev;
/* add includes for other candev drivers here */
#endif

#define ENABLE_DEBUG 0
#include <debug.h>

/* Default is not using loopback test mode */
#ifndef CONFIG_USE_LOOPBACK_MODE
#define CONFIG_USE_LOOPBACK_MODE 0
Expand Down Expand Up @@ -96,7 +96,6 @@ static int _send(int argc, char **argv)

static int _receive(int argc, char **argv)
{
uint8_t buf[CAN_MAX_DLEN];
int n = 1;

if (argc > 1) {
Expand All @@ -108,26 +107,21 @@ static int _receive(int argc, char **argv)
}

for (int i = 0; i < n; i++) {
uint32_t can_id = 0;
uint8_t can_dlc = 0;
struct can_frame frame;

puts("Reading from Rxbuf...");
isrpipe_read(&rxbuf, buf, 4); /* can-id */
can_id = ((uint32_t)buf[0] << 24) |
((uint32_t)buf[1] << 16) |
((uint32_t)buf[2] << 8) |
((uint32_t)buf[3]);
isrpipe_read(&rxbuf, buf, 1); /* can-dlc */
can_dlc = buf[0];
if (can_dlc > 0) {
isrpipe_read(&rxbuf, buf, can_dlc); /* data */
isrpipe_read(&rxbuf, (uint8_t *)&(frame.can_id), sizeof(frame.can_id));
frame.can_id &= 0x1FFFFFFF; /* clear invalid bits */
isrpipe_read(&rxbuf, (uint8_t *)&(frame.can_dlc), 1);
printf("id: %" PRIx32 " dlc: %" PRIx8, frame.can_id, frame.can_dlc);
if (frame.can_dlc > 0) {
printf(" data: ");
isrpipe_read(&rxbuf, frame.data, frame.can_dlc); /* data */
for (int i = 0; i < frame.can_dlc; i++) {
printf("0x%X ", frame.data[i]);
}
}

printf("id: %" PRIx32 " dlc: %" PRIx8 " Data: \n", can_id, can_dlc);
for (int i = 0; i < can_dlc; i++) {
printf("0x%X ", buf[i]);
}
puts("");
putchar('\n');
}

return 0;
Expand Down Expand Up @@ -163,25 +157,17 @@ static void _can_event_callback(candev_t *dev, candev_event_t event, void *arg)

frame = (struct can_frame *)arg;

DEBUG("\tid: %" PRIx32 " dlc: %" PRIx8 " Data: \n\t", frame->can_id,
DEBUG(" id: %" PRIx32 " dlc: %" PRIx8 " data: ", frame->can_id & 0x1FFFFFFF,
frame->can_dlc);
for (uint8_t i = 0; i < frame->can_dlc; i++) {
DEBUG("0x%X ", frame->data[i]);
}
DEBUG(" ");
DEBUG_PUTS("");

/* Store in buffer until user requests the data */
isrpipe_write_one(&rxbuf,
(uint8_t)((frame->can_id & 0x1FFFFFFF) >> 24));
isrpipe_write_one(&rxbuf,
(uint8_t)((frame->can_id & 0xFF0000) >> 16));
isrpipe_write_one(&rxbuf, (uint8_t)((frame->can_id & 0xFF00) >> 8));
isrpipe_write_one(&rxbuf, (uint8_t)((frame->can_id & 0xFF)));

isrpipe_write(&rxbuf, (uint8_t *)&(frame->can_id), sizeof(frame->can_id));
isrpipe_write_one(&rxbuf, frame->can_dlc);
for (uint8_t i = 0; i < frame->can_dlc; i++) {
isrpipe_write_one(&rxbuf, frame->data[i]);
}
isrpipe_write(&rxbuf, frame->data, frame->can_dlc);

break;
case CANDEV_EVENT_RX_ERROR:
Expand Down

0 comments on commit 71a8e27

Please sign in to comment.