From 890519913521ffda10e6408f9f0aaeed235e754d Mon Sep 17 00:00:00 2001 From: Vincent Dupont Date: Fri, 19 May 2017 15:41:53 +0200 Subject: [PATCH] can_trx: add CAN transceiver interface --- drivers/can_trx/Makefile | 1 + drivers/can_trx/can_trx.c | 53 +++++++++++++++++ drivers/include/can/can_trx.h | 108 ++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 drivers/can_trx/Makefile create mode 100644 drivers/can_trx/can_trx.c create mode 100644 drivers/include/can/can_trx.h diff --git a/drivers/can_trx/Makefile b/drivers/can_trx/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/drivers/can_trx/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/can_trx/can_trx.c b/drivers/can_trx/can_trx.c new file mode 100644 index 0000000000..0f6057fc84 --- /dev/null +++ b/drivers/can_trx/can_trx.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2016 OTA keys S.A. + * + * 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. + */ + +/** + * @defgroup drivers_can transceiver + * @ingroup drivers + * @brief generic transceiver interface + * + * @{ + * + * @file + * @brief generic transceiver interface + * + * @author Vincent Dupont + */ + +#include +#include + +#include "can/can_trx.h" + +int can_trx_init(can_trx_t *dev) +{ + if (dev == NULL) { + return -ENODEV; + } + + if (dev->driver->init) { + return dev->driver->init(dev); + } + else { + return -ENOTSUP; + } +} + +int can_trx_set_mode(can_trx_t *dev, can_trx_mode_t mode) +{ + if (dev == NULL) { + return -ENODEV; + } + + if (dev->driver->set_mode) { + return dev->driver->set_mode(dev, mode); + } + else { + return -ENOTSUP; + } +} diff --git a/drivers/include/can/can_trx.h b/drivers/include/can/can_trx.h new file mode 100644 index 0000000000..432b81ca09 --- /dev/null +++ b/drivers/include/can/can_trx.h @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2016 OTA keys S.A. + * + * 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. + */ + +/** + * @defgroup trx_can CAN transceiver + * @ingroup can + * @ingroup drivers + * @brief generic transceiver interface + * + * @{ + * + * @file + * @brief generic transceiver interface + * + * @author Aurelien Gonce + * @author Vincent Dupont + */ +#ifndef CAN_CAN_TRX_H +#define CAN_CAN_TRX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * trx transceiver mode + */ +typedef enum { + TRX_NORMAL_MODE = 0, + TRX_SILENT_MODE, + TRX_SLEEP_MODE, + /* single wire can modes */ + TRX_HIGH_SPEED_MODE, + TRX_HIGH_VOLTAGE_WAKE_UP_MODE +} can_trx_mode_t; + +/** + * @brief forward declaration of trx_driver + */ +typedef struct trx_driver trx_driver_t; + +/** + * @brief Generic transceiver descriptor + */ +typedef struct can_trx { + const trx_driver_t *driver; /**< driver */ + can_trx_mode_t mode; /**< current mode */ +} can_trx_t; + +/** + * @brief Generic transceiver driver + */ +struct trx_driver { + /** + * @brief initialize the trx device + * + * @param[in] dev Transceiver to initialize + * + * @return 0 on success + * @return < 0 on error + */ + int (*init)(can_trx_t *dev); + + /** + * @brief set mode interface + * + * @param[in] dev Transceiver to set + * @param[in] mode Mode to set + * + * @return 0 on success + * @return < 0 on error + */ + int (*set_mode)(can_trx_t *dev, can_trx_mode_t mode); +}; + +/** + * @brief initialize a transceiver + * + * @param[in] dev Transceiver to initialize + * + * @return 0 on success + * @return < 0 on error + */ +int can_trx_init(can_trx_t *dev); + +/** + * @brief transceiver set mode + * + * @param[in] dev Transceiver to set + * @param[in] mode Mode to set + * + * @return 0 on success + * @return < 0 on error + */ +int can_trx_set_mode(can_trx_t *dev, can_trx_mode_t mode); + + +#ifdef __cplusplus +} +#endif + +#endif /* CAN_CAN_TRX_H */ +/** @} */