From 8d0ac30c76ba17ad3157957cd80359e9b7049ec3 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Thu, 30 Jan 2025 10:58:45 +0100 Subject: [PATCH] tests/drivers/candev: minor cleanup - Do not hard code the number of the CAN interface to use. (This also allows specifying it via make command line / environment variable.) - Make less use of preprocessor and rely on compiler to eliminate dead branches and unused variables. --- tests/drivers/candev/Makefile | 5 ++ tests/drivers/candev/main.c | 89 +++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/tests/drivers/candev/Makefile b/tests/drivers/candev/Makefile index 18e61e9ed15b..31441664a156 100644 --- a/tests/drivers/candev/Makefile +++ b/tests/drivers/candev/Makefile @@ -6,4 +6,9 @@ USEMODULE += shell USEMODULE += can USEMODULE += isrpipe +# The number of the CAN bus to use can be controlled via environment +# variable CAN_DEV: +CAN_DEV ?= 0 +CFLAGS += -DCONFIG_CAN_DEV=$(CAN_DEV) + include $(RIOTBASE)/Makefile.include diff --git a/tests/drivers/candev/main.c b/tests/drivers/candev/main.c index 6376d0a96d0c..fb845466a9d1 100644 --- a/tests/drivers/candev/main.c +++ b/tests/drivers/candev/main.c @@ -19,42 +19,38 @@ * @} */ -#include -#include #include #include #include #include -#include "shell.h" -#include "test_utils/expect.h" -#include "can/device.h" - -#if IS_USED(MODULE_PERIPH_CAN) -#include "periph/can.h" -#include "can_params.h" #include "board.h" -#include "periph_conf.h" -#include "periph/gpio.h" - -static can_t periph_dev; - -#elif defined(MODULE_MCP2515) +#include "can_params.h" #include "candev_mcp2515.h" -#include "mcp2515_params.h" +#include "periph/can.h" +#include "periph_conf.h" +#include "shell.h" +#include "test_utils/expect.h" -static candev_mcp2515_t mcp2515_dev; +/* The params header is only in the include path when the module is used */ +#if MODULE_MCP2515 +# include "mcp2515_params.h" +#endif -#else -/* add includes for other candev drivers here */ +#ifdef BOARD_SAME54_XPRO +# include "periph/gpio.h" #endif #define ENABLE_DEBUG 0 -#include +#include "debug.h" /* Default is not using loopback test mode */ #ifndef CONFIG_USE_LOOPBACK_MODE -#define CONFIG_USE_LOOPBACK_MODE 0 +# define CONFIG_USE_LOOPBACK_MODE 0 +#endif + +#ifndef CONFIG_CAN_DEV +# define CONFIG_CAN_DEV 0 #endif #define RX_RINGBUFFER_SIZE 128 /* Needs to be a power of 2! */ @@ -63,6 +59,20 @@ static uint8_t rx_ringbuf[RX_RINGBUFFER_SIZE]; static candev_t *candev = NULL; +/* Only one of them is actually used, depending on the driver selected. + * We rely on the compiler to garbage collect the unused */ +static can_t periph_dev; +static candev_mcp2515_t mcp2515_dev; + +/* The params header is only in the include path when the module is used, + * so we fall back to a NULL ptr if not */ +#if MODULE_MCP2515 +static candev_mcp2515_conf_t *mcp2515_conf = &candev_mcp2515_conf[CONFIG_CAN_DEV]; +#else +static candev_mcp2515_conf_t *mcp2515_conf = NULL; +#endif + + static int _send(int argc, char **argv) { int ret = 0; @@ -275,26 +285,33 @@ static void _can_event_callback(candev_t *dev, candev_event_t event, void *arg) int main(void) { - puts("candev test application\n"); isrpipe_init(&rxbuf, (uint8_t *)rx_ringbuf, sizeof(rx_ringbuf)); -#if IS_USED(MODULE_PERIPH_CAN) - puts("Initializing CAN periph device"); - can_init(&periph_dev, &(candev_conf[0])); - candev = &(periph_dev.candev); + if (IS_USED(MODULE_PERIPH_CAN)) { + puts("Initializing CAN periph device"); + can_init(&periph_dev, &(candev_conf[CONFIG_CAN_DEV])); + candev = &(periph_dev.candev); #if defined(BOARD_SAME54_XPRO) - gpio_init(AT6561_STBY_PIN, GPIO_OUT); - gpio_clear(AT6561_STBY_PIN); -#endif -#elif defined(MODULE_MCP2515) - puts("Initializing MCP2515"); - candev_mcp2515_init(&mcp2515_dev, &candev_mcp2515_conf[0]); - candev = (candev_t *)&mcp2515_dev; - -#else - /* add initialization for other candev drivers here */ + gpio_init(AT6561_STBY_PIN, GPIO_OUT); + gpio_clear(AT6561_STBY_PIN); #endif + } + else if (IS_USED(MODULE_MCP2515)) { + puts("Initializing MCP2515"); + candev_mcp2515_init(&mcp2515_dev, mcp2515_conf); + candev = &mcp2515_dev.candev; + } + else { + /* No CAN driver is used or used CAN driver is not integrated in this + * test yet. We use an undefined function name to let this fail at + * compile time. The conditions above are all compile time constants + * and the compiler will eliminate the dead branches. So if any of them + * matched, this function call will not be part of the compiled object + * file and linking will work. */ + extern void the_can_test_apps_depends_on_a_supported_can_driver_but_none_is_used(void); + the_can_test_apps_depends_on_a_supported_can_driver_but_none_is_used(); + } expect(candev);