Skip to content

Commit

Permalink
native/can: fix CAN init
Browse files Browse the repository at this point in the history
Native CAN device was not properly ported to periph_can interface.
This commit fixes this by renaming all needed structures and files so
auto_init_can can initialize the native device. FEATURES_PROVIDED is
also updated for native.
  • Loading branch information
Vincent Dupont committed Sep 30, 2020
1 parent 98c39d5 commit b4f2903
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 92 deletions.
1 change: 1 addition & 0 deletions cpu/native/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ config NATIVE_OS_DARWIN

config NATIVE_OS_LINUX
bool
select HAS_PERIPH_CAN
select HAS_PERIPH_GPIO
select HAS_PERIPH_GPIO_IRQ
select HAS_PERIPH_SPI
Expand Down
2 changes: 2 additions & 0 deletions cpu/native/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ ifeq ($(OS),Linux)
FEATURES_PROVIDED += periph_spi
# Hardware GPIO access is only available on Linux hosts
FEATURES_PROVIDED += periph_gpio periph_gpio_irq
# CAN is only supported on Linux through socketCAN
FEATURES_PROVIDED += periph_can
endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* @author Vincent Dupont <vincent@otakeys.com>
*/

#ifndef CANDEV_LINUX_PARAMS_H
#define CANDEV_LINUX_PARAMS_H
#ifndef CAN_PARAMS_H
#define CAN_PARAMS_H

#include "candev_linux.h"
#include "can/device.h"
Expand All @@ -29,7 +29,7 @@ extern "C" {
/**
* @brief Default parameters (device names)
*/
static candev_params_t candev_linux_params[] = {
static const candev_params_t candev_params[] = {
{ .name = "can0", },
{ .name = "can1", },
};
Expand All @@ -38,5 +38,5 @@ static candev_params_t candev_linux_params[] = {
}
#endif

#endif /* CANDEV_LINUX_PARAMS_H */
#endif /* CAN_PARAMS_H */
/** @} */
24 changes: 10 additions & 14 deletions cpu/native/include/candev_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ extern "C" {
/**
* Linux candev configuration
*/
typedef struct candev_linux_conf {
typedef struct candev_conf {
/** local interface name */
char interface_name[CAN_MAX_SIZE_INTERFACE_NAME + 1];
} candev_linux_conf_t;
} can_conf_t;

/** CAN device configuration type can_conf_t is redefined by native CAN */
#define HAVE_CAN_CONF_T

#ifndef CANDEV_LINUX_MAX_FILTERS_RX
/**
Expand Down Expand Up @@ -74,25 +77,18 @@ typedef struct candev_linux_conf {
typedef struct candev_linux {
candev_t candev; /**< candev base structure */
int sock; /**< local socket id */
const candev_linux_conf_t *conf; /**< device configuration */
const can_conf_t *conf; /**< device configuration */
/** filter list */
struct can_filter filters[CANDEV_LINUX_MAX_FILTERS_RX];
} candev_linux_t;
} can_t;

/**
* @brief Device specific initialization function
*
* @param[inout] dev the device to initialize
* @param[in] conf the device configuration
*
* @return 0 on success
*/
int candev_linux_init(candev_linux_t *dev, const candev_linux_conf_t *conf);
/** CAN device type can_t is redefined by native CAN */
#define HAVE_CAN_T

/**
* @brief Array containing socketCAN device names
*/
extern candev_linux_conf_t candev_linux_conf[CAN_DLL_NUMOF];
extern can_conf_t candev_conf[CAN_DLL_NUMOF];

#endif /* defined(__linux__) */

Expand Down
4 changes: 4 additions & 0 deletions cpu/native/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ typedef enum {
#endif
/** @} */

#ifdef MODULE_PERIPH_CAN
#include "candev_linux.h"
#endif

#ifdef __cplusplus
}
#endif
Expand Down
26 changes: 13 additions & 13 deletions cpu/native/can/candev_linux.c → cpu/native/periph/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int _remove_filter(candev_t *candev, const struct can_filter *filter);
static int _power_up(candev_t *candev);
static int _power_down(candev_t *candev);

static int _set_bittiming(candev_linux_t *dev, struct can_bittiming *bittiming);
static int _set_bittiming(can_t *dev, struct can_bittiming *bittiming);

static const candev_driver_t candev_linux_driver = {
.send = _send,
Expand All @@ -69,7 +69,7 @@ static const candev_driver_t candev_linux_driver = {
static candev_event_t _can_error_to_can_evt(struct can_frame can_frame_err);
static void _callback_can_sigio(int sock, void *arg);

candev_linux_conf_t candev_linux_conf[CAN_DLL_NUMOF] = {
can_conf_t candev_conf[CAN_DLL_NUMOF] = {
#if CAN_DLL_NUMOF >= 1
{
.interface_name = "vcan0",
Expand All @@ -82,9 +82,9 @@ candev_linux_conf_t candev_linux_conf[CAN_DLL_NUMOF] = {
#endif
};

int candev_linux_init(candev_linux_t *dev, const candev_linux_conf_t *conf)
int can_init(can_t *dev, const can_conf_t *conf)
{
memset(dev, 0, sizeof(candev_linux_t));
memset(dev, 0, sizeof(can_t));
dev->candev.driver = &candev_linux_driver;
dev->conf = conf;
dev->candev.bittiming.bitrate = CANDEV_LINUX_DEFAULT_BITRATE;
Expand Down Expand Up @@ -129,7 +129,7 @@ static candev_event_t _can_error_to_can_evt(struct can_frame can_frame_err)
static void _callback_can_sigio(int sockfd, void *arg)
{
(void) sockfd;
candev_linux_t *dev = (candev_linux_t *) arg;
can_t *dev = (can_t *) arg;

if (dev->candev.event_callback) {
dev->candev.event_callback(&dev->candev, CANDEV_EVENT_ISR, NULL);
Expand All @@ -149,7 +149,7 @@ static int _init(candev_t *candev)
int ret;

DEBUG("Will start linux CAN init\n");
candev_linux_t *dev = (candev_linux_t *)candev;
can_t *dev = (can_t *)candev;

if ((strlen(dev->conf->interface_name) == 0)
|| (strlen(dev->conf->interface_name) > CAN_MAX_SIZE_INTERFACE_NAME)) {
Expand Down Expand Up @@ -212,7 +212,7 @@ static int _init(candev_t *candev)
static int _send(candev_t *candev, const struct can_frame *frame)
{
int nbytes;
candev_linux_t *dev = (candev_linux_t *)candev;
can_t *dev = (can_t *)candev;

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

Expand All @@ -232,7 +232,7 @@ static void _isr(candev_t *candev)
{
int nbytes;
struct can_frame rcv_frame;
candev_linux_t *dev = (candev_linux_t *)candev;
can_t *dev = (can_t *)candev;

if (dev == NULL) {
return;
Expand Down Expand Up @@ -272,7 +272,7 @@ static void _isr(candev_t *candev)

}

static int _set_bittiming(candev_linux_t *dev, struct can_bittiming *bittiming)
static int _set_bittiming(can_t *dev, struct can_bittiming *bittiming)
{
int res;

Expand Down Expand Up @@ -300,7 +300,7 @@ static int _set_bittiming(candev_linux_t *dev, struct can_bittiming *bittiming)

static int _set(candev_t *candev, canopt_t opt, void *value, size_t value_len)
{
candev_linux_t *dev = (candev_linux_t *) candev;
can_t *dev = (can_t *) candev;
int res = 0;

switch (opt) {
Expand Down Expand Up @@ -349,7 +349,7 @@ 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)
{
candev_linux_t *dev = (candev_linux_t *) candev;
can_t *dev = (can_t *) candev;
int res = 0;

switch (opt) {
Expand Down Expand Up @@ -456,7 +456,7 @@ static int _get(candev_t *candev, canopt_t opt, void *value, size_t max_len)

static int _set_filter(candev_t *candev, const struct can_filter *filter)
{
candev_linux_t *dev = (candev_linux_t *)candev;
can_t *dev = (can_t *)candev;

if (filter == NULL) {
DEBUG("candev_native: _set_filter: error filter NULL\n");
Expand Down Expand Up @@ -500,7 +500,7 @@ static int _set_filter(candev_t *candev, const struct can_filter *filter)

static int _remove_filter(candev_t *candev, const struct can_filter *filter)
{
candev_linux_t *dev = (candev_linux_t *)candev;
can_t *dev = (can_t *)candev;

if (filter == NULL) {
DEBUG("candev_native: _remove_filter: error filter NULL\n");
Expand Down
2 changes: 1 addition & 1 deletion cpu/native/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e
usage_exit(EXIT_FAILURE);
}
optarg++;
strncpy(candev_linux_conf[i].interface_name, optarg,
strncpy(candev_conf[i].interface_name, optarg,
CAN_MAX_SIZE_INTERFACE_NAME);
}
break;
Expand Down
53 changes: 0 additions & 53 deletions sys/auto_init/can/auto_init_can_linux.c

This file was deleted.

1 change: 1 addition & 0 deletions sys/auto_init/can/auto_init_periph_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @}
*/

#include "periph/can.h"
#include "can/device.h"
#include "can_params.h"

Expand Down
5 changes: 0 additions & 5 deletions sys/auto_init/can/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ void auto_init_candev(void)
isotp_init(isotp_stack, ISOTP_STACK_SIZE, ISOTP_PRIORITY, "isotp");
#endif

#ifdef MODULE_CAN_LINUX
extern void auto_init_can_native(void);
auto_init_can_native();
#endif

#ifdef MODULE_PERIPH_CAN
extern void auto_init_periph_can(void);
auto_init_periph_can();
Expand Down
4 changes: 2 additions & 2 deletions tests/candev/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <candev_linux.h>

static candev_linux_t linux_dev;
static can_t linux_dev;

#else
/* add other candev drivers here */
Expand Down Expand Up @@ -194,7 +194,7 @@ int main(void)
isrpipe_init(&rxbuf, (uint8_t *)rx_ringbuf, sizeof(rx_ringbuf));
#if IS_USED(MODULE_CAN_LINUX)
puts("Initializing Linux Can device");
candev_linux_init( &linux_dev, &(candev_linux_conf[0])); /* vcan0 */
can_init( &linux_dev, &(candev_conf[0])); /* vcan0 */
candev = (candev_t *)&linux_dev;
#else
/* add initialization for other candev drivers here */
Expand Down

0 comments on commit b4f2903

Please sign in to comment.