1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

Merge pull request #7120 from smlng/drivers/mpl3115a2/rework

drivers/mpl3115a2: rework
This commit is contained in:
Alexandre Abadie 2017-06-29 10:03:42 +02:00 committed by GitHub
commit 725144896d
10 changed files with 429 additions and 217 deletions

View File

@ -7,6 +7,7 @@ ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += hdc1000
USEMODULE += mag3110
USEMODULE += mma8x5x
USEMODULE += tmp006
USEMODULE += mpl3115a2
USEMODULE += tcs37727
USEMODULE += tmp006
endif

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
* 2017 HAW Hamburg
*
* 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
@ -10,13 +11,11 @@
* @defgroup drivers_mpl3115a2 MPL3115A2 Pressure Sensor
* @ingroup drivers_sensors
* @brief Driver for the Freescale MPL3115A2 pressure sensor.
* The driver will initialize the sensor for
* pressure measurement. The conversion duration
* depends on oversample ratio.
* After initialization and set activ the sensor
* will make measurements at periodic times.
* The oversample ratio can be determined
* by sensor initialization.
*
* The driver will initialize the sensor for pressure measurement. The
* conversion duration depends on oversample ratio. After initialization
* the sensor can be set active to run periodic measurements. The oversample
* ratio can be configured during sensor initialization.
*
* @{
*
@ -24,6 +23,7 @@
* @brief Interface definition for the MPL3115A2 sensor driver.
*
* @author Johann Fischer <j.fischer@phytec.de>
* @author Sebastian Meiling <s@mlng.net>
*/
#ifndef MPL3115A2_H
@ -38,78 +38,95 @@ extern "C"
{
#endif
/**
* @brief Named return values
*/
enum {
MPL3115A2_OK, /**< all good */
MPL3115A2_ERROR_I2C, /**< I2C communication failed */
MPL3115A2_ERROR_DEV, /**< Device MPL3115A2 not found */
MPL3115A2_ERROR_CNF, /**< Device configuration failed */
};
#ifndef MPL3115A2_I2C_ADDRESS
#define MPL3115A2_I2C_ADDRESS 0x60 /**< Pressure Sensor Default Address */
/**
* @brief MPL3115A2 Default Address
*/
#define MPL3115A2_I2C_ADDRESS (0x60)
#endif
#define MPL3115A2_OS_RATIO_1 0 /**< Oversample Ratio 1, conversion time 6 ms */
#define MPL3115A2_OS_RATIO_2 1 /**< Oversample Ratio 2, conversion time 10 ms */
#define MPL3115A2_OS_RATIO_4 2 /**< Oversample Ratio 4, conversion time 18 ms */
#define MPL3115A2_OS_RATIO_8 3 /**< Oversample Ratio 8, conversion time 34 ms */
#define MPL3115A2_OS_RATIO_16 4 /**< Oversample Ratio 16, conversion time 66 ms */
#define MPL3115A2_OS_RATIO_32 5 /**< Oversample Ratio 32, conversion time 130 ms */
#define MPL3115A2_OS_RATIO_64 6 /**< Oversample Ratio 64, conversion time 258 ms */
#define MPL3115A2_OS_RATIO_128 7 /**< Oversample Ratio 128, conversion time 512 ms */
#define MPL3115A2_OS_RATIO_DEFAULT MPL3115A2_OS_RATIO_128 /**< Default Ratio for testing */
/**
* @name Oversample Ratio configuration
* @{
*/
enum {
MPL3115A2_OS_RATIO_1 = 0, /**< Oversample Ratio 1, conversion 6ms */
MPL3115A2_OS_RATIO_2, /**< Oversample Ratio 2, conversion 10ms */
MPL3115A2_OS_RATIO_4, /**< Oversample Ratio 4, conversion 18ms */
MPL3115A2_OS_RATIO_8, /**< Oversample Ratio 8, conversion 34ms */
MPL3115A2_OS_RATIO_16, /**< Oversample Ratio 16, conversion 66ms */
MPL3115A2_OS_RATIO_32, /**< Oversample Ratio 32, conversion 130ms */
MPL3115A2_OS_RATIO_64, /**< Oversample Ratio 64, conversion 258ms */
MPL3115A2_OS_RATIO_128, /**< Oversample Ratio 128, conversion 514ms */
};
#define MPL3115A2_OS_RATIO_DEFAULT MPL3115A2_OS_RATIO_16 /**< Default Ratio */
/** @} */
#ifndef MPL3115A2_CONVERSION_TIME
#define MPL3115A2_CONVERSION_TIME 512000 /**< Maximum Conversion Time in us */
/**
* @brief Maximum Conversion Time in microseconds [us]
*
* @note Conversion time is: ((oversampling ratio * 4) + 2) * 1000 us
*/
#define MPL3115A2_CONVERSION_TIME (514000UL)
#endif
/**
* @brief Configuration parameters
*/
typedef struct {
i2c_t i2c; /**< I2C bus the device is connected to */
uint8_t addr; /**< I2C bus address of the device */
uint8_t ratio; /**< MPL3115A2 oversampling ratio */
} mpl3115a2_params_t;
/**
* @brief Device descriptor for MPL3115A2 sensors.
*/
typedef struct {
i2c_t i2c; /**< I2C device, the sensor is connected to */
uint8_t addr; /**< the sensor's slave address on the I2C bus */
bool initialized; /**< sensor status, true if sensor is initialized */
mpl3115a2_params_t params; /**< device configuration parameters */
} mpl3115a2_t;
/**
* @brief MPL3115A2 sensor test.
* This function looks for Device ID of the MPL3115A2 sensor.
*
* @param[in] dev device descriptor of sensor
*
* @return 0 on success
* @return -1 on error
*/
int mpl3115a2_test(const mpl3115a2_t *dev);
/**
* @brief Initialize the MPL3115A2 sensor driver.
*
* @param[out] dev device descriptor of sensor to initialize
* @param[in] i2c I2C bus the sensor is connected to
* @param[in] address sensor's I2C slave address
* @param[in] os_ratio oversample rate selection
* @param[in] params configuration parameters
*
* @return 0 on success
* @return -1 if os_ratio parameter is wrong
* @return -2 if initialization of I2C bus failed
* @return -3 if sensor test failed
* @return -4 if sensor configuration failed
* @return MPL3115A2_OK on success
* @return -MPL3115A2_ERROR_I2C on I2C bus error
* @return -MPL3115A2_ERROR_DEV on device error
* @return -MPL3115A2_ERROR_CNF on config error
*/
int mpl3115a2_init(mpl3115a2_t *dev, i2c_t i2c, uint8_t address, uint8_t os_ratio);
int mpl3115a2_init(mpl3115a2_t *dev, const mpl3115a2_params_t *params);
/**
* @brief Reset the MPL3115A2 sensor. After that, the sensor should be reinitialized.
*
* @param[out] dev device descriptor of sensor
* @param[in] dev device descriptor of sensor
*
* @return 0 on success
* @return -1 on error
* @return MPL3115A2_OK on success
* @return -MPL3115A2_ERROR_I2C on error
*/
int mpl3115a2_reset(mpl3115a2_t *dev);
int mpl3115a2_reset(const mpl3115a2_t *dev);
/**
* @brief Set active mode, this enables periodic measurements.
*
* @param[out] dev device descriptor of sensor
* @param[in] dev device descriptor of sensor
*
* @return 0 on success
* @return -1 on error
* @return MPL3115A2_OK on success
* @return -MPL3115A2_ERROR_I2C on error
*/
int mpl3115a2_set_active(const mpl3115a2_t *dev);
@ -118,8 +135,8 @@ int mpl3115a2_set_active(const mpl3115a2_t *dev);
*
* @param[in] dev device descriptor of sensor
*
* @return 0 on success
* @return -1 on error
* @return MPL3115A2_OK on success
* @return -MPL3115A2_ERROR_I2C on error
*/
int mpl3115a2_set_standby(const mpl3115a2_t *dev);
@ -129,8 +146,8 @@ int mpl3115a2_set_standby(const mpl3115a2_t *dev);
* @param[in] dev device descriptor of sensor
*
* @return >0 if new data sample is ready
* @return 0 measurement in progress
* @return -1 on error
* @return MPL3115A2_OK measurement in progress
* @return -MPL3115A2_ERROR_I2C on error
*/
int mpl3115a2_is_ready(const mpl3115a2_t *dev);
@ -141,8 +158,8 @@ int mpl3115a2_is_ready(const mpl3115a2_t *dev);
* @param[out] pres pressure in Pascals
* @param[out] status sensor status register
*
* @return 0 on success
* @return -1 on error
* @return MPL3115A2_OK on success,
* @return -MPL3115A2_ERROR_I2C on error
*/
int mpl3115a2_read_pressure(const mpl3115a2_t *dev, uint32_t *pres, uint8_t *status);
@ -152,8 +169,8 @@ int mpl3115a2_read_pressure(const mpl3115a2_t *dev, uint32_t *pres, uint8_t *sta
* @param[in] dev device descriptor of sensor
* @param[out] temp temperature in \f$^\circ C \cdot 10\f$
*
* @return 0 on success
* @return -1 on error
* @return MPL3115A2_OK on success
* @return -MPL3115A2_ERROR_I2C on error
*/
int mpl3115a2_read_temp(const mpl3115a2_t *dev, int16_t *temp);

View File

@ -0,0 +1,80 @@
/*
* Copyright (C) 2017 HAW Hamburg
*
* 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_mpl3115a2
* @{
*
* @file
* @brief Default configuration for MPL3115A2 devices
*
* @author Sebastian Meiling <s@mlng.net>
*/
#ifndef MPL3115A2_PARAMS_H
#define MPL3115A2_PARAMS_H
#include "board.h"
#include "saul_reg.h"
#include "mpl3115a2.h"
#include "mpl3115a2_reg.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name Default configuration parameters for the MPL3115A2 driver
* @{
*/
#ifndef MPL3115A2_PARAM_I2C
#define MPL3115A2_PARAM_I2C I2C_DEV(0)
#endif
#ifndef MPL3115A2_PARAM_ADDR
#define MPL3115A2_PARAM_ADDR MPL3115A2_I2C_ADDRESS
#endif
#ifndef MPL3115A2_PARAM_RATIO
#define MPL3115A2_PARAM_RATIO MPL3115A2_OS_RATIO_DEFAULT
#endif
#ifndef MPL3115A2_PARAMS_DEFAULT
#define MPL3115A2_PARAMS_DEFAULT { .i2c = MPL3115A2_PARAM_I2C, \
.addr = MPL3115A2_PARAM_ADDR, \
.ratio = MPL3115A2_PARAM_RATIO }
#endif
/**@}*/
/**
* @brief MPL3115A2 configuration
*/
static const mpl3115a2_params_t mpl3115a2_params[] =
{
#ifdef MPL3115A2_PARAMS_CUSTOM
MPL3115A2_PARAMS_CUSTOM
#else
MPL3115A2_PARAMS_DEFAULT
#endif
};
/**
* @brief Additional meta information to keep in the SAUL registry
*/
static const saul_reg_info_t mpl3115a2_saul_info[] =
{
{ .name = "mpl3115a2" },
};
#ifdef __cplusplus
}
#endif
#endif /* MPL3115A2_PARAMS_H */
/** @} */

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
* 2017 HAW Hamburg
*
* 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
@ -16,169 +17,146 @@
*
* @author Johann Fischer <j.fischer@phytec.de>
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
* @author Sebastian Meiling <s@mlng.net>
*
* @}
*/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "log.h"
#include "periph/i2c.h"
#include "mpl3115a2.h"
#include "mpl3115a2_reg.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#define I2C_SPEED I2C_SPEED_FAST
#define I2C_SPEED I2C_SPEED_FAST
int mpl3115a2_test(const mpl3115a2_t *dev)
#define BUS (dev->params.i2c)
#define ADDR (dev->params.addr)
int mpl3115a2_init(mpl3115a2_t *dev, const mpl3115a2_params_t *params)
{
uint8_t reg;
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
if (i2c_read_regs(dev->i2c, dev->addr, MPL3115A2_WHO_AM_I, &reg, 1) != 1) {
/* Release the bus for other threads. */
i2c_release(dev->i2c);
return -1;
}
i2c_release(dev->i2c);
if (reg != MPL3115A2_ID) {
return -1;
}
return 0;
}
int mpl3115a2_init(mpl3115a2_t *dev, i2c_t i2c, uint8_t address, uint8_t os_ratio)
{
uint8_t reg;
assert(dev);
assert(params);
/* write device descriptor */
dev->i2c = i2c;
dev->addr = address;
dev->initialized = false;
memcpy(dev, params, sizeof(mpl3115a2_params_t));
if (os_ratio > MPL3115A2_OS_RATIO_128) {
return -1;
}
i2c_acquire(dev->i2c);
i2c_acquire(BUS);
/* initialize the I2C bus */
if (i2c_init_master(i2c, I2C_SPEED) < 0) {
i2c_release(dev->i2c);
return -2;
if (i2c_init_master(BUS, I2C_SPEED) < 0) {
i2c_release(BUS);
LOG_ERROR("mpl3115a2_init: failed to init I2C!\n");
return -MPL3115A2_ERROR_I2C;
}
i2c_release(dev->i2c);
if (mpl3115a2_test(dev)) {
return -3;
/* test device */
if (i2c_read_regs(BUS, ADDR, MPL3115A2_WHO_AM_I, &reg, 1) != 1) {
/* Release the bus for other threads. */
i2c_release(BUS);
LOG_ERROR("mpl3115a2_init: I2C error!\n");
return -MPL3115A2_ERROR_I2C;
}
if (reg != MPL3115A2_ID) {
LOG_ERROR("mpl3115a2_init: invalid WHO_AM_I value (0x%02x)!\n", (int)reg);
return -MPL3115A2_ERROR_DEV;
}
/* set sample rate */
reg = MPL3115A2_CTRL_REG1_OS(dev->params.ratio);
if (i2c_write_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(BUS);
LOG_ERROR("mpl3115a2_init: failed to set sample rate!\n");
return -MPL3115A2_ERROR_CNF;
}
/* configure device */
reg = MPL3115A2_PT_DATA_CFG_TDEFE |
MPL3115A2_PT_DATA_CFG_PDEFE |
MPL3115A2_PT_DATA_CFG_DREM;
if (i2c_write_regs(BUS, ADDR, MPL3115A2_PT_DATA_CFG, &reg, 1) != 1) {
i2c_release(BUS);
LOG_ERROR("mpl3115a2_init: config failure!\n");
return -MPL3115A2_ERROR_CNF;
}
reg = MPL3115A2_CTRL_REG1_OS(os_ratio);
i2c_release(BUS);
i2c_acquire(dev->i2c);
if (i2c_write_regs(dev->i2c, dev->addr, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -4;
}
i2c_release(dev->i2c);
reg = MPL3115A2_PT_DATA_CFG_TDEFE
| MPL3115A2_PT_DATA_CFG_PDEFE
| MPL3115A2_PT_DATA_CFG_DREM;
i2c_acquire(dev->i2c);
if (i2c_write_regs(dev->i2c, dev->addr, MPL3115A2_PT_DATA_CFG, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -4;
}
i2c_release(dev->i2c);
dev->initialized = true;
return 0;
return MPL3115A2_OK;
}
int mpl3115a2_reset(mpl3115a2_t *dev)
int mpl3115a2_reset(const mpl3115a2_t *dev)
{
uint8_t reg;
dev->initialized = false;
i2c_acquire(BUS);
reg = MPL3115A2_CTRL_REG1_RST;
i2c_acquire(dev->i2c);
if (i2c_write_regs(dev->i2c, dev->addr, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -1;
if (i2c_write_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(BUS);
LOG_ERROR("mpl3115a2_reset: failed!\n");
return -MPL3115A2_ERROR_I2C;
}
i2c_release(BUS);
i2c_release(dev->i2c);
return 0;
return MPL3115A2_OK;
}
int mpl3115a2_set_active(const mpl3115a2_t *dev)
{
uint8_t reg;
if (dev->initialized == false) {
return -1;
}
i2c_acquire(dev->i2c);
if (i2c_read_regs(dev->i2c, dev->addr, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -1;
i2c_acquire(BUS);
if (i2c_read_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(BUS);
return -MPL3115A2_ERROR_I2C;
}
reg |= MPL3115A2_CTRL_REG1_SBYB;
if (i2c_write_regs(dev->i2c, dev->addr, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -1;
if (i2c_write_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(BUS);
return -MPL3115A2_ERROR_I2C;
}
i2c_release(BUS);
i2c_release(dev->i2c);
return 0;
return MPL3115A2_OK;
}
int mpl3115a2_set_standby(const mpl3115a2_t *dev)
{
uint8_t reg;
i2c_acquire(dev->i2c);
if (i2c_read_regs(dev->i2c, dev->addr, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -1;
i2c_acquire(BUS);
if (i2c_read_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(BUS);
return -MPL3115A2_ERROR_I2C;
}
reg &= ~MPL3115A2_CTRL_REG1_SBYB;
if (i2c_write_regs(dev->i2c, dev->addr, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -1;
if (i2c_write_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1) != 1) {
i2c_release(BUS);
return -MPL3115A2_ERROR_I2C;
}
i2c_release(BUS);
i2c_release(dev->i2c);
return 0;
return MPL3115A2_OK;
}
int mpl3115a2_is_ready(const mpl3115a2_t *dev)
{
uint8_t reg;
if (dev->initialized == false) {
return -1;
i2c_acquire(BUS);
if (i2c_read_regs(BUS, ADDR, MPL3115A2_STATUS, &reg, 1) != 1) {
i2c_release(BUS);
return -MPL3115A2_ERROR_I2C;
}
i2c_release(BUS);
i2c_acquire(dev->i2c);
if (i2c_read_regs(dev->i2c, dev->addr, MPL3115A2_STATUS, &reg, 1) != 1) {
i2c_release(dev->i2c);
return -1;
}
i2c_release(dev->i2c);
return reg & MPL3115A2_STATUS_PTDR;
}
@ -186,41 +164,33 @@ int mpl3115a2_read_pressure(const mpl3115a2_t *dev, uint32_t *pres, uint8_t *sta
{
uint8_t buf[4];
if (dev->initialized == false) {
return -1;
i2c_acquire(BUS);
if (i2c_read_regs(BUS, ADDR, MPL3115A2_STATUS, buf, 4) != 4) {
i2c_release(BUS);
return -MPL3115A2_ERROR_I2C;
}
i2c_acquire(dev->i2c);
if (i2c_read_regs(dev->i2c, dev->addr, MPL3115A2_STATUS, buf, 4) != 4) {
i2c_release(dev->i2c);
return -1;
}
i2c_release(dev->i2c);
i2c_release(BUS);
*status = buf[0];
*pres = ((uint32_t)buf[1] << 16) | ((uint32_t)buf[2] << 8) | buf[3];
*pres = *pres / 64;
return 0;
return MPL3115A2_OK;
}
int mpl3115a2_read_temp(const mpl3115a2_t *dev, int16_t *temp)
{
uint8_t buf[2];
if (dev->initialized == false) {
return -1;
i2c_acquire(BUS);
if (i2c_read_regs(BUS, ADDR, MPL3115A2_OUT_T_MSB, buf, 2) != 2) {
i2c_release(BUS);
return -MPL3115A2_ERROR_I2C;
}
i2c_acquire(dev->i2c);
if (i2c_read_regs(dev->i2c, dev->addr, MPL3115A2_OUT_T_MSB, buf, 2) != 2) {
i2c_release(dev->i2c);
return -1;
}
i2c_release(dev->i2c);
i2c_release(BUS);
*temp = ((int16_t)(((int16_t)buf[0] << 8) | buf[1]) * 10) / 256;
return 0;
return MPL3115A2_OK;
}

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2017 HAW Hamburg
*
* 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_mpl3115a2
* @{
*
* @file
* @brief MPL3115A2 adaption to the RIOT actuator/sensor interface
*
* @author Sebastian Meiling <s@mlng.net>
*
* @}
*/
#include <string.h>
#include <stdio.h>
#include "log.h"
#include "saul.h"
#include "mpl3115a2.h"
static int read_pressure(const void *dev, phydat_t *res)
{
uint8_t state;
uint32_t pressure;
if (mpl3115a2_read_pressure((const mpl3115a2_t *)dev,
&pressure, &state) != MPL3115A2_OK) {
LOG_ERROR("[SAUL] mpl3115a2_read_pressure failed!\n");
return -1;
}
/* pressure values > 100000, so /100 for int16_t which matches unit hPA */
res->val[0] = (int16_t)(pressure/100);
res->unit = UNIT_PA;
res->scale = 2;
return 1;
}
static int read_temperature(const void *dev, phydat_t *res)
{
int16_t temperature;
if (mpl3115a2_read_temp((const mpl3115a2_t *)dev, &temperature) != MPL3115A2_OK) {
LOG_ERROR("[SAUL] mpl3115a2_read_temp failed!\n");
return -1;
}
res->val[0] = temperature;
res->unit = UNIT_TEMP_C;
res->scale = -1;
return 1;
}
const saul_driver_t mpl3115a2_pressure_saul_driver = {
.read = read_pressure,
.write = saul_notsup,
.type = SAUL_SENSE_PRESS,
};
const saul_driver_t mpl3115a2_temperature_saul_driver = {
.read = read_temperature,
.write = saul_notsup,
.type = SAUL_SENSE_TEMP,
};

View File

@ -287,6 +287,10 @@ void auto_init(void)
extern void auto_init_mma8x5x(void);
auto_init_mma8x5x();
#endif
#ifdef MODULE_MPL3115A2
extern void auto_init_mpl3115a2(void);
auto_init_mpl3115a2();
#endif
#ifdef MODULE_SI70XX
extern void auto_init_si70xx(void);
auto_init_si70xx();

View File

@ -0,0 +1,82 @@
/*
* Copyright (C) 2017 HAW Hamburg
*
* 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 auto_init_saul
* @{
*
* @file
* @brief Auto initialization of MPL3115A2 pressure sensor
*
* @author Sebastian Meiling <s@mlng.net>
*
* @}
*/
#ifdef MODULE_MPL3115A2
#include "log.h"
#include "saul_reg.h"
#include "mpl3115a2.h"
#include "mpl3115a2_params.h"
/**
* @brief Define the number of configured sensors
*/
#define MPL3115A2_NUM (sizeof(mpl3115a2_params) / sizeof(mpl3115a2_params[0]))
/**
* @brief Allocate memory for the device descriptors
*/
static mpl3115a2_t mpl3115a2_devs[MPL3115A2_NUM];
/**
* @brief Memory for the SAUL registry entries
*/
static saul_reg_t saul_entries[MPL3115A2_NUM * 2];
/**
* @name Reference the driver struct
* @{
*/
extern const saul_driver_t mpl3115a2_pressure_saul_driver;
extern const saul_driver_t mpl3115a2_temperature_saul_driver;
/** @} */
void auto_init_mpl3115a2(void)
{
for (unsigned i = 0; i < MPL3115A2_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing mpl3115a2 #%u\n", i);
if ((mpl3115a2_init(&mpl3115a2_devs[i], &mpl3115a2_params[i]) |
mpl3115a2_set_active(&mpl3115a2_devs[i])) != MPL3115A2_OK) {
LOG_ERROR("[auto_init_saul] error initializing mpl3115a2 #%u\n", i);
continue;
}
/* temperature */
saul_entries[(i * 2)].dev = &(mpl3115a2_devs[i]);
saul_entries[(i * 2)].name = mpl3115a2_saul_info[i].name;
saul_entries[(i * 2)].driver = &mpl3115a2_temperature_saul_driver;
/* atmospheric pressure */
saul_entries[(i * 2) + 1].dev = &(mpl3115a2_devs[i]);
saul_entries[(i * 2) + 1].name = mpl3115a2_saul_info[i].name;
saul_entries[(i * 2) + 1].driver = &mpl3115a2_pressure_saul_driver;
/* register to saul */
saul_reg_add(&(saul_entries[(i * 2)]));
saul_reg_add(&(saul_entries[(i * 2) + 1]));
}
}
#else
typedef int dont_be_pedantic;
#endif /* MODULE_MPL3115A2 */

View File

@ -6,12 +6,4 @@ FEATURES_REQUIRED = periph_i2c
USEMODULE += mpl3115a2
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_MPL3115A2_I2C ?= I2C_DEV\(0\)
TEST_MPL3115A2_ADDR ?= 0x60
# export parameters
CFLAGS += -DTEST_MPL3115A2_I2C=$(TEST_MPL3115A2_I2C)
CFLAGS += -DTEST_MPL3115A2_ADDR=$(TEST_MPL3115A2_ADDR)
include $(RIOTBASE)/Makefile.include

View File

@ -2,8 +2,8 @@
This is a manual test application for the MPL3115A2 driver.
# Usage
This test application will initialize the MPL3115A2 sensor with the following parameters:
- oversample ratio 128
This test application will initialize the MPL3115A2 sensor with parameter
oversample ratio 128
After initialization, the sensor reads the pressure and temperature values every 1s
and prints them to STDOUT.
After initialization, the sensor reads pressure, temperature values and device
state every 1s and prints them to STDOUT.

View File

@ -20,49 +20,45 @@
* @}
*/
#ifndef TEST_MPL3115A2_I2C
#error "TEST_MPL3115A2_I2C not defined"
#endif
#ifndef TEST_MPL3115A2_ADDR
#error "TEST_MPL3115A2_ADDR not defined"
#endif
#include <stdio.h>
#include <stdlib.h>
#include "xtimer.h"
#include "mpl3115a2.h"
#include "mpl3115a2_params.h"
#define SLEEP (1000 * 1000U)
#define SLEEP (1UL * US_PER_SEC)
static mpl3115a2_t dev;
int main(void)
{
mpl3115a2_t dev;
uint32_t pressure;
int16_t temp;
uint8_t status;
puts("MPL3115A2 pressure sensor driver test application\n");
printf("Initializing MPL3115A2 sensor at I2C_%i... ", TEST_MPL3115A2_I2C);
if (mpl3115a2_init(&dev, TEST_MPL3115A2_I2C, TEST_MPL3115A2_ADDR,
MPL3115A2_OS_RATIO_DEFAULT) == 0) {
puts("[OK]\n");
}
else {
puts("[Failed]");
return -1;
printf("Initializing MPL3115A2 sensor at I2C_%i... ", mpl3115a2_params[0].i2c);
if (mpl3115a2_init(&dev, &mpl3115a2_params[0]) != MPL3115A2_OK) {
puts("[FAILED] init device!");
return 1;
}
if (mpl3115a2_set_active(&dev)) {
puts("Measurement start failed.");
return -1;
if (mpl3115a2_set_active(&dev) != MPL3115A2_OK) {
puts("[FAILED] activate measurement!");
return 2;
}
puts("[SUCCESS]");
while (1) {
uint32_t pressure;
int16_t temp;
uint8_t status;
xtimer_usleep(SLEEP);
mpl3115a2_read_pressure(&dev, &pressure, &status);
printf("Pressure: %u Status: %#02x\n", (unsigned int)pressure, status);
mpl3115a2_read_temp(&dev, &temp);
printf("Temperature: %d\n", temp/10);
if ((mpl3115a2_read_pressure(&dev, &pressure, &status) |
mpl3115a2_read_temp(&dev, &temp)) != MPL3115A2_OK) {
puts("[FAILED] read values!");
}
else {
printf("Pressure: %u Pa, Temperature: %3d.%d C, State: %#02x\n",
(unsigned int)pressure, temp/10, abs(temp%10), status);
}
}
return 0;