Skip to content

Commit

Permalink
tests/conn_can: make test support CAN FD
Browse files Browse the repository at this point in the history
Increase Shell buffer size for 64 bytes payload length of CAN FD frame.
This also implies to increase main thread stack size and especially for
native architectures.
Add two new sub-commands to test_can command:
* fdsend: to send a CAN FD frame
* fdsendrtr: to send a CAN FD RTR frame (payload length = 0).

Signed-off-by: Gilles DOFFE <[email protected]>
  • Loading branch information
gdoffe committed Nov 3, 2024
1 parent 1d31978 commit 3347170
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
14 changes: 14 additions & 0 deletions tests/drivers/candev/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ USEMODULE += can
USEMODULE += isrpipe

include $(RIOTBASE)/Makefile.include

# Loop delay
ifneq (,$(filter fdcan,$(USEMODULE)))
ifneq (1,$(RIOT_CI_BUILD))
# Check your CAN transceiver datasheet to apply the good loop delay in nanoseconds.
# This configuration is mandatory if you are using CAN FD with bitrate switching.
# Can be configured here or through Kconfig
#CFLAGS += -DCONFIG_FDCAN_DEVICE_TRANSCEIVER_LOOP_DELAY=0

# Allow only a default value for CI
else
CFLAGS += -DCONFIG_FDCAN_DEVICE_TRANSCEIVER_LOOP_DELAY=0
endif
endif
20 changes: 20 additions & 0 deletions tests/sys/conn_can/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@ include $(RIOTBASE)/Makefile.include
ifndef CONFIG_GNRC_PKTBUF_SIZE
CFLAGS += -DCONFIG_GNRC_PKTBUF_SIZE=4096
endif

ifneq (,$(filter native native64,$(BOARD)))
CFLAGS += -DTHREAD_STACKSIZE_MAIN=8192
else
CFLAGS += -DTHREAD_STACKSIZE_MAIN=2048
endif

# Loop delay
ifneq (,$(filter fdcan,$(USEMODULE)))
ifneq (1,$(RIOT_CI_BUILD))
# Check your CAN transceiver datasheet to apply the good loop delay in nanoseconds.
# This configuration is mandatory if you are using CAN FD with bitrate switching.
# Can be configured here or through Kconfig
#CFLAGS += -DCONFIG_FDCAN_DEVICE_TRANSCEIVER_LOOP_DELAY=0

# Allow only a default value for CI
else
CFLAGS += -DCONFIG_FDCAN_DEVICE_TRANSCEIVER_LOOP_DELAY=0
endif
endif
2 changes: 2 additions & 0 deletions tests/sys/conn_can/fdcan-ci.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_FDCAN_DEVICE_SET_TRANSCEIVER_LOOP_DELAY=y
CONFIG_FDCAN_DEVICE_TRANSCEIVER_LOOP_DELAY=0
55 changes: 48 additions & 7 deletions tests/sys/conn_can/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

#include "can/can_trx.h"

#define SHELL_BUFSIZE 512 /* Needed for CAN FD frame */

#ifdef MODULE_TJA1042
#include "tja1042.h"
tja1042_trx_t tja1042 = { .trx.driver = &tja1042_driver,
Expand Down Expand Up @@ -95,6 +97,10 @@ static void print_usage(void)
puts("test_can list");
puts("test_can send ifnum can_id [B1 [B2 [B3 [B4 [B5 [B6 [B7 [B8]]]]]]]]");
puts("test_can sendrtr ifnum can_id length(0..8)");
#ifdef MODULE_FDCAN
puts("test_can fdsend ifnum can_id [B1 [B2 [B3 [B4 [B5 [B6 [B7 ... [B64]]]]]]]]");
puts("test_can fdsendrtr ifnum can_id length(0..64)");
#endif
printf("test_can recv ifnum user_id timeout can_id1 [can_id2..can_id%d]\n",
MAX_FILTER);
puts("test_can close user_id");
Expand Down Expand Up @@ -130,7 +136,7 @@ static int _list(int argc, char **argv) {
return 0;
}

static int _send(int argc, char **argv, bool rtr)
static int _send(int argc, char **argv, bool rtr, bool is_fd)
{
if (argc < 5) {
print_usage();
Expand All @@ -150,10 +156,27 @@ static int _send(int argc, char **argv, bool rtr)
frame.can_id = strtoul(argv[3], NULL, 16);
frame.len = argc - 4;
}
if (frame.len > 8) {

#ifdef MODULE_FDCAN
if (is_fd) {
frame.flags |= CANFD_FDF;
if (frame.len > DEFAULT_CAN_MAX_DLEN) {
puts("Invalid length");
return 1;
}
}
else if (frame.len > CAN_MAX_DLEN) {
puts("Invalid length");
return 1;
}
#else
(void) is_fd;

if (frame.len > DEFAULT_CAN_MAX_DLEN) {
puts("Invalid length");
return 1;
}
#endif

if (rtr) {
for (int i = 0; i < frame.len; i++) {
Expand Down Expand Up @@ -203,7 +226,17 @@ static int _receive(int argc, char **argv)
}
for (int i = 0; i < filt_num; i++) {
filters[thread_nb][i].can_id = strtoul(argv[5 + i], NULL, 16);
filters[thread_nb][i].can_mask = 0xffffffff;
if (filters[thread_nb][i].can_id > CAN_SFF_MASK) {
filters[thread_nb][i].can_mask = CAN_EFF_MASK;
}
else {
filters[thread_nb][i].can_mask = CAN_SFF_MASK;
}
}
if (filt_num <= 0) {
filt_num = 1;
filters[thread_nb][0].can_id = 0;
filters[thread_nb][0].can_mask = 0;
}
uint32_t timeout = strtoul(argv[4], NULL, 0);
msg_t msg;
Expand Down Expand Up @@ -541,11 +574,19 @@ static int _can_handler(int argc, char **argv)
return _list(argc, argv);
}
else if (strncmp(argv[1], "send", 5) == 0) {
return _send(argc, argv, false);
return _send(argc, argv, false, false);
}
else if (strncmp(argv[1], "sendrtr", 8) == 0) {
return _send(argc, argv, true);
return _send(argc, argv, true, false);
}
#ifdef MODULE_FDCAN
else if (strncmp(argv[1], "fdsend", 5) == 0) {
return _send(argc, argv, false, true);
}
else if (strncmp(argv[1], "fdsendrtr", 8) == 0) {
return _send(argc, argv, true, true);
}
#endif
else if (strncmp(argv[1], "recv", 5) == 0) {
return _receive(argc, argv);
}
Expand Down Expand Up @@ -736,8 +777,8 @@ int main(void)
(void*)i, "receive_thread");
}

char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
char line_buf[SHELL_BUFSIZE];
shell_run(_commands, line_buf, SHELL_BUFSIZE);

return 0;
}

0 comments on commit 3347170

Please sign in to comment.