1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers: rework mpl3115a2 pressure sensor

This commit is contained in:
smlng 2017-04-22 20:54:39 +02:00
parent 5505c800f2
commit c7375529e3
3 changed files with 234 additions and 176 deletions

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