From ba8ab6c88bd1cf8cdb76f11275c2aa26e92ffe1e Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Mon, 15 Jan 2018 14:59:19 +0100 Subject: [PATCH] drivers/lis2dh12: add SAUL mapping --- drivers/include/lis2dh12.h | 6 ++++ drivers/lis2dh12/include/lis2dh12_params.h | 23 +++++++++---- drivers/lis2dh12/lis2dh12_saul.c | 38 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 drivers/lis2dh12/lis2dh12_saul.c diff --git a/drivers/include/lis2dh12.h b/drivers/include/lis2dh12.h index 2d525dc2a3..37d47477d4 100644 --- a/drivers/include/lis2dh12.h +++ b/drivers/include/lis2dh12.h @@ -33,6 +33,7 @@ #ifndef LIS2DH12_H #define LIS2DH12_H +#include "saul.h" #include "periph/spi.h" #include "periph/gpio.h" @@ -98,6 +99,11 @@ enum { LIS2DH12_NODEV = -2, /**< unable to talk to device */ }; +/** + * @brief Export the SAUL interface for this driver + */ +extern const saul_driver_t lis2dh12_saul_driver; + /** * @brief Initialize the given LIS2DH12 sensor device * diff --git a/drivers/lis2dh12/include/lis2dh12_params.h b/drivers/lis2dh12/include/lis2dh12_params.h index d6bbd45aec..2ac1a03df0 100644 --- a/drivers/lis2dh12/include/lis2dh12_params.h +++ b/drivers/lis2dh12/include/lis2dh12_params.h @@ -21,6 +21,7 @@ #include "board.h" #include "lis2dh12.h" +#include "saul_reg.h" #ifdef __cplusplus extern "C" { @@ -43,10 +44,16 @@ extern "C" { #define LIS2DH12_PARAM_RATE LIS2DH12_RATE_100HZ #endif -#define LIS2DH12_PARAMS_DEFAULT { .spi = LIS2DH12_PARAM_SPI, \ +#ifndef LIS2DH12_PARAMS +#define LIS2DH12_PARAMS { .spi = LIS2DH12_PARAM_SPI, \ .cs = LIS2DH12_PARAM_CS, \ .scale = LIS2DH12_PARAM_SCALE, \ .rate = LIS2DH12_PARAM_RATE } +#endif + +#ifndef LIS2DH12_SAULINFO +#define LIS2DH12_SAULINFO { .name = "lis2dh12" } +#endif /**@}*/ /** @@ -54,11 +61,15 @@ extern "C" { */ static const lis2dh12_params_t lis2dh12_params[] = { -#ifdef LIS2DH12_PARAMS_BOARD - LIS2DH12_PARAMS_BOARD, -#else - LIS2DH12_PARAMS_DEFAULT, -#endif + LIS2DH12_PARAMS +}; + +/** + * @brief Additional meta information to keep in the SAUL registry + */ +static const saul_reg_info_t lis2dh12_saul_info[] = +{ + LIS2DH12_SAULINFO }; #ifdef __cplusplus diff --git a/drivers/lis2dh12/lis2dh12_saul.c b/drivers/lis2dh12/lis2dh12_saul.c new file mode 100644 index 0000000000..b3c878296b --- /dev/null +++ b/drivers/lis2dh12/lis2dh12_saul.c @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2018 Freie Universität Berlin + * + * 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. + */ + +/** + * @ingroup drivers_lis2dh12 + * @{ + * + * @file + * @brief LIS2DH12 accelerometer SAUL mapping + * + * @author Hauke Petersen + * + * @} + */ + +#include "saul.h" +#include "lis2dh12.h" + +static int read_accelerometer(const void *dev, phydat_t *res) +{ + if (lis2dh12_read((const lis2dh12_t *)dev, res->val) != LIS2DH12_OK) { + return 0; + } + res->unit = UNIT_G; + res->scale = -3; + return 3; +} + +const saul_driver_t lis2dh12_saul_driver = { + .read = read_accelerometer, + .write = saul_notsup, + .type = SAUL_SENSE_ACCEL +};