Skip to content

Commit

Permalink
Merge pull request #8381 from haukepetersen/add_driver_lis2dh12
Browse files Browse the repository at this point in the history
drivers: add support for LIS2DH12 acceleromter (SPI mode)
  • Loading branch information
haukepetersen authored Jan 18, 2018
2 parents 0352681 + 0c8b131 commit a896d95
Show file tree
Hide file tree
Showing 16 changed files with 705 additions and 2 deletions.
2 changes: 1 addition & 1 deletion boards/ruuvitag/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
include $(RIOTBOARD)/common/nrf52xxxdk/Makefile.dep

ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += lis2dh12_spi
# TODO: enable drivers once their adaption/implementations are merged
# USEMODULE += bme280_spi
# USEMODULE += lis2dh12_spi
endif

ifneq (,$(filter gnrc_netdev_default,$(USEMODULE)))
Expand Down
10 changes: 9 additions & 1 deletion boards/ruuvitag/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ extern "C" {
#define LED0_PIN GPIO_PIN(0, 17)
#define LED1_PIN GPIO_PIN(0, 19)

/* as a shortcut, we include the two CS pins into the LED_MASK, this will make
* sure both pins are initialized as output and set during board initialization.
* This is needed so that the BMX280 and LIS2DH12 device drivers do not
* interfere... */
#define CS_PIN_MASK ((1 << 3) | (1 << 8))

#define LED_PORT NRF_P0
#define LED0_MASK (1 << 17)
#define LED1_MASK (1 << 19)
#define LED_MASK (LED0_MASK | LED1_MASK)

/* mask used in the common board.c file to initialize and set the given pins */
#define LED_MASK (LED0_MASK | LED1_MASK | CS_PIN_MASK)

#define LED0_ON (LED_PORT->OUTCLR = LED0_MASK)
#define LED0_OFF (LED_PORT->OUTSET = LED0_MASK)
Expand Down
10 changes: 10 additions & 0 deletions drivers/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ ifneq (,$(filter l3g4200d,$(USEMODULE)))
FEATURES_REQUIRED += periph_i2c
endif

ifneq (,$(filter lis2dh12%,$(USEMODULE)))
ifneq (,$(filter lis2dh12_spi,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio
FEATURES_REQUIRED += periph_spi
else
FEATURES_REQUIRED += periph_i2c
endif
USEMODULE += lis2dh12
endif

ifneq (,$(filter lis3dh,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio
FEATURES_REQUIRED += periph_spi
Expand Down
3 changes: 3 additions & 0 deletions drivers/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ endif
ifneq (,$(filter rn2%3,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/rn2xx3/include
endif
ifneq (,$(filter lis2dh12,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/lis2dh12/include
endif
155 changes: 155 additions & 0 deletions drivers/include/lis2dh12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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.
*/

/**
* @defgroup drivers_lis2dh12 LIS2DH12 Accelerometer
* @ingroup drivers_sensors
* @brief Driver for the STM LIS2DH12 accelerometer
*
* This device driver provides a minimal interface to LIS2DH12 devices. As of
* now, it only provides very basic access to the device. The driver configures
* the device to continuously read the acceleration data with statically
* defined scale and rate, and with a fixed 10-bit resolution. The LIS2DH12's
* FIFO is bypassed, so the driver might not be sufficient for use cases where
* the complete history of readings is of interest.
*
* Also, the current version of the driver supports only interfacing the sensor
* via SPI. The driver is however written in a way, that adding I2C interface
* support is quite simple, as all bus related functions (acquire, release,
* read, write) are cleanly separated in the code.
*
* @{
* @file
* @brief Interface definition for the STM LIS2DH12 accelerometer
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/

#ifndef LIS2DH12_H
#define LIS2DH12_H

#include "saul.h"
#include "periph/spi.h"
#include "periph/gpio.h"

#ifdef __cplusplus
extern "C" {
#endif

/* I2C support is not implemented (yet), so throw an error when selected */
#ifndef MODULE_LIS2DH12_SPI
#error "LIS2DH12 error: I2C mode is not supported, yet. Use module li2dh12_spi"
#endif

/**
* @brief Available scale values
*/
typedef enum {
LIS2DH12_SCALE_2G = 0x00, /**< +- 2g */
LIS2DH12_SCALE_4G = 0x10, /**< +- 4g */
LIS2DH12_SCALE_8G = 0x20, /**< +- 8g */
LIS2DH12_SCALE_16G = 0x30, /**< +- 16g */
} lis2dh12_scale_t;

/**
* @brief Available sampling rates
*
* @note The device does also support some additional rates for specific low-
* power modes, but those are as of now not supported by this driver
*/
typedef enum {
LIS2DH12_RATE_1HZ = 0x17, /**< sample with 1Hz */
LIS2DH12_RATE_10HZ = 0x27, /**< sample with 10Hz */
LIS2DH12_RATE_25HZ = 0x37, /**< sample with 25Hz */
LIS2DH12_RATE_50HZ = 0x47, /**< sample with 50Hz */
LIS2DH12_RATE_100HZ = 0x57, /**< sample with 100Hz */
LIS2DH12_RATE_200HZ = 0x67, /**< sample with 200Hz */
LIS2DH12_RATE_400HZ = 0x77, /**< sample with 400Hz */
} lis2dh12_rate_t;

/**
* @brief LIS2DH12 configuration parameters
*/
typedef struct {
spi_t spi; /**< SPI bus the device is connected to */
gpio_t cs; /**< connected chip select pin */
lis2dh12_scale_t scale; /**< sampling sensitivity used */
lis2dh12_rate_t rate; /**< sampling rate used */
} lis2dh12_params_t;

/**
* @brief LIS2DH12 device descriptor
*/
typedef struct {
const lis2dh12_params_t *p; /**< device configuration */
uint16_t comp; /**< scale compensation factor */
} lis2dh12_t;

/**
* @brief Status and error return codes
*/
enum {
LIS2DH12_OK = 0, /**< everything was fine */
LIS2DH12_NOBUS = -1, /**< bus interface error */
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
*
* @param[out] dev device descriptor
* @param[in] params static device configuration
*
* @return LIS2DH12_OK on success
* @return LIS2DH12_NOBUS on bus errors
* @return LIS2DH12_NODEV if no LIS2DH12 device was found on the bus
*/
int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params);

/**
* @brief Read acceleration data from the given device
*
* @param[in] dev device descriptor
* @param[out] data acceleration data in mili-g, **MUST** hold 3 values
*
* @return LIS2DH12_OK on success
* @return LIS2DH12_NOBUS on bus error
*/
int lis2dh12_read(const lis2dh12_t *dev, int16_t *data);

/**
* @brief Power on the given device
*
* @param[in] dev device descriptor
*
* @return LIS2DH12_OK on success
* @return LIS2DH12_NOBUS on bus error
*/
int lis2dh12_poweron(const lis2dh12_t *dev);

/**
* @brief Power off the given device
*
* @param[in] dev device descriptor
*
* @return LIS2DH12_OK on success
* @return LIS2DH12_NOBUS on bus error
*/
int lis2dh12_poweroff(const lis2dh12_t *dev);

#ifdef __cplusplus
}
#endif

#endif /* LIS2DH12_H */
/** @} */
1 change: 1 addition & 0 deletions drivers/lis2dh12/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
80 changes: 80 additions & 0 deletions drivers/lis2dh12/include/lis2dh12_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 Default configuration for LIS2DH12 devices
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/

#ifndef LIS2DH12_PARAMS_H
#define LIS2DH12_PARAMS_H

#include "board.h"
#include "lis2dh12.h"
#include "saul_reg.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @name Set default configuration parameters for LIS2DH12 devices
* @{
*/
#ifndef LIS2DH12_PARAM_SPI
#define LIS2DH12_PARAM_SPI SPI_DEV(0)
#endif
#ifndef LIS2DH12_PARAM_CS
#define LIS2DH12_PARAM_CS GPIO_PIN(0, 0)
#endif
#ifndef LIS2DH12_PARAM_SCALE
#define LIS2DH12_PARAM_SCALE LIS2DH12_SCALE_2G
#endif
#ifndef LIS2DH12_PARAM_RATE
#define LIS2DH12_PARAM_RATE LIS2DH12_RATE_100HZ
#endif

#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
/**@}*/

/**
* @brief LIS2DH12 configuration
*/
static const lis2dh12_params_t lis2dh12_params[] =
{
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
}
#endif

#endif /* LIS2DH12_PARAMS_H */
/** @} */
Loading

0 comments on commit a896d95

Please sign in to comment.