Skip to content

Commit

Permalink
Merge pull request #6886 from smlng/driver/mag3110/rework
Browse files Browse the repository at this point in the history
drivers: mag3110 rework
  • Loading branch information
miri64 committed Apr 14, 2017
2 parents abd3957 + 17e78f7 commit 812c557
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 186 deletions.
4 changes: 2 additions & 2 deletions boards/microbit/include/board.h
Expand Up @@ -68,15 +68,15 @@ extern "C" {
* @name MMA8653 accelerometer configuration
* @{
*/
#define MMA8653_PARAM_I2C I2C_0,
#define MMA8653_PARAM_I2C I2C_DEV(0)
#define MMA8653_PARAM_ADDR 0x1d
/** @} */

/**
* @name MAG3110 magnetometer configuration
* @{
*/
#define MAG3110_PARAM_I2C I2C_0,
#define MAG3110_PARAM_I2C I2C_DEV(0)
#define MAG3110_PARAM_ADDR 0x0e
/** @} */

Expand Down
1 change: 1 addition & 0 deletions boards/pba-d-01-kw2x/Makefile.dep
Expand Up @@ -3,6 +3,7 @@ ifneq (,$(filter netdev_default gnrc_netdev_default,$(USEMODULE)))
endif

ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += mag3110
USEMODULE += mma8x5x
USEMODULE += hdc1000
USEMODULE += tcs37727
Expand Down
60 changes: 60 additions & 0 deletions boards/pba-d-01-kw2x/include/mag3110_params.h
@@ -0,0 +1,60 @@
/*
* 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 boards_pba-d-01-kw2x
* @{
*
* @file
* @brief MAG3110 board specific configuration
*
* @author Sebastian Meiling <s@mlng.net>
*/

#ifndef MAG3110_PARAMS_H
#define MAG3110_PARAMS_H

#include "board.h"
#include "saul_reg.h"
#include "mag3110.h"
#include "mag3110_reg.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief MAG3310 configuration
*/
static const mag3110_params_t mag3110_params[] =
{
{
.i2c = MAG3110_I2C,
.addr = MAG3110_ADDR,
.type = MAG3110_ID,
.dros = MAG3110_DROS_DEFAULT,
.offset = { 0, 0, 0 }
}
};

/**
* @brief Additional meta information to keep in the SAUL registry
*/
static const saul_reg_info_t mag3110_saul_info[] =
{
{
.name = "mag3110"
}
};

#ifdef __cplusplus
}
#endif

#endif /* MAG3110_PARAMS_H */
/** @} */
63 changes: 38 additions & 25 deletions drivers/include/mag3110.h
@@ -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
Expand All @@ -25,6 +26,7 @@
* @brief Interface definition for the MAG3110 magnetometer driver.
*
* @author Johann Fischer <j.fischer@phytec.de>
* @author Sebastian Meiling <s@mlng.net>
*/

#ifndef MAG3110_H
Expand All @@ -39,6 +41,16 @@ extern "C"
{
#endif

/**
* @brief Named return values
*/
enum {
MAG3110_OK, /**< all good */
MAG3110_ERROR_I2C, /**< I2C communication failed */
MAG3110_ERROR_DEV, /**< Device MAG3110 not found */
MAG3110_ERROR_CNF, /**< Device configuration failed */
};

#ifndef MAG3110_I2C_ADDRESS
#define MAG3110_I2C_ADDRESS 0x0E /**< Magnetometer Default Address */
#endif
Expand Down Expand Up @@ -77,41 +89,45 @@ extern "C"
#define MAG3110_DROS_0008_128 31 /**< Output Rate 0.08 Hz, Over Sample Ratio 128 */
#define MAG3110_DROS_DEFAULT MAG3110_DROS_0125_128 /**< Default Setting for testing */

/**
* @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 type; /**< device type */
uint8_t dros; /**< sampling rate to use */
int16_t offset[3]; /**< data offset in X, Y, and Z direction */
} mag3110_params_t;

/**
* @brief Device descriptor for MAG3110 magnetometer.
*/
typedef struct {
i2c_t i2c; /**< I2C device, the magnetometer is connected to */
uint8_t addr; /**< the magnetometer's slave address on the I2C bus */
bool initialized; /**< magnetometer status, true if magnetometer is initialized */
mag3110_params_t params; /**< device configuration parameters */
} mag3110_t;

/**
* @brief MAG3110 magnetometer test.
* This function looks for Device ID of the MAG3110 magnetometer.
*
* @param[in] dev device descriptor of magnetometer
*
* @return 0 on success
* @return -1 on error
* @brief Data type for the result data
*/
int mag3110_test(mag3110_t *dev);
typedef struct {
int16_t x; /**< acceleration in X direction */
int16_t y; /**< acceleration in Y direction */
int16_t z; /**< acceleration in Z direction */
} mag3110_data_t;

/**
* @brief Initialise the MAG3110 magnetometer driver.
*
* @param[out] dev device descriptor of magnetometer to initialize
* @param[in] i2c I2C bus the magnetometer is connected to
* @param[in] address magnetometer's I2C slave address
* @param[in] dros data rate and over sampling selection
* @param[in] params configuration parameters
*
* @return 0 on success
* @return -1 if dros parameter is wrong
* @return -2 if initialization of I2C bus failed
* @return -3 if magnetometer test failed
* @return -4 if magnetometer configuration failed
* @return -1 if I2C communication failed
* @return -2 if magnetometer test failed
* @return -3 if magnetometer configuration failed
*/
int mag3110_init(mag3110_t *dev, i2c_t i2c, uint8_t address, uint8_t dros);
int mag3110_init(mag3110_t *dev, const mag3110_params_t *params);

/**
* @brief Set user offset correction.
Expand Down Expand Up @@ -163,16 +179,13 @@ int mag3110_is_ready(mag3110_t *dev);
* To get the actual values for the magnetic field in \f$\mu T\f$,
* one have to divide the returned values from the magnetometer by 10.
*
* @param[in] dev device descriptor of magnetometer
* @param[out] x x-axis magnetic field strength
* @param[out] y y-axis magnetic field strength
* @param[out] z z-axis magnetic field strength
* @param[out] status magnetometer status register
* @param[in] dev device descriptor of accelerometer
* @param[out] data the current magnetic field strength
*
* @return 0 on success
* @return -1 on error
*/
int mag3110_read(mag3110_t *dev, int16_t *x, int16_t *y, int16_t *z, uint8_t *status);
int mag3110_read(mag3110_t *dev, mag3110_data_t *data);

/**
* @brief Read die temperature.
Expand Down
76 changes: 76 additions & 0 deletions drivers/mag3110/include/mag3110_params.h
@@ -0,0 +1,76 @@
/*
* 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_mag3110
* @{
*
* @file
* @brief Default configuration for MAG3110 devices
*
* @author Sebastian Meiling <s@mlng.net>
*/

#ifndef MAG3110_PARAMS_H
#define MAG3110_PARAMS_H

#include "board.h"
#include "saul_reg.h"
#include "mag3110.h"
#include "mag3110_reg.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @name Default configuration parameters for the MAG3110 driver
* @{
*/
#ifndef MAG3110_PARAM_I2C
#define MAG3110_PARAM_I2C (I2C_DEV(0))
#endif
#ifndef MAG3110_PARAM_ADDR
#define MAG3110_PARAM_ADDR (MAG3110_I2C_ADDRESS)
#endif
#ifndef MAG3110_PARAM_OFFSET
#define MAG3110_PARAM_OFFSET { 0, 0, 0 }
#endif
#ifndef MAG3110_PARAMS
#define MAG3110_PARAMS { .i2c = MAG3110_PARAM_I2C, \
.addr = MAG3110_PARAM_ADDR, \
.type = MAG3110_ID, \
.dros = MAG3110_DROS_DEFAULT, \
.offset = MAG3110_PARAM_OFFSET }
#endif
/**@}*/

/**
* @brief MAG3110 configuration
*/
static const mag3110_params_t mag3110_params[] =
{
MAG3110_PARAMS
};

/**
* @brief Additional meta information to keep in the SAUL registry
*/
static const saul_reg_info_t mag3110_saul_info[] =
{
{
.name = "mag3110"
}
};

#ifdef __cplusplus
}
#endif

#endif /* MAG3110_PARAMS_H */
/** @} */

0 comments on commit 812c557

Please sign in to comment.