mma8652: add support for all mma8x5x accelerometers

pr/spi.typo
Vincent Dupont 7 years ago
parent 9a39a804e3
commit b9b838f1dc

@ -57,6 +57,15 @@ extern "C"
#define MMA8652_FS_RANGE_8G 2 /**< +/- 8 g Full Scale Range */
#define MMA8652_FS_RANGE_DEFAULT MMA8652_FS_RANGE_2G /**< Full-Scale Range for testing */
enum {
MMA8x5x_TYPE_MMA8652 = 0,
MMA8x5x_TYPE_MMA8653,
MMA8x5x_TYPE_MMA8451,
MMA8x5x_TYPE_MMA8452,
MMA8x5x_TYPE_MMA8453,
MMA8x5x_TYPE_MAX,
};
/**
* @brief Device descriptor for MMA8652 accelerometer.
*/
@ -65,6 +74,7 @@ typedef struct {
uint8_t addr; /**< the accelerometer's slave address on the I2C bus */
bool initialized; /**< accelerometer status, true if accelerometer is initialized */
int16_t scale; /**< each count corresponds to (1/scale) g */
uint8_t type; /**< mma8x5x type */
} mma8652_t;
/**
@ -75,6 +85,7 @@ typedef struct {
uint8_t addr; /**< accelerometer's I2C address */
uint8_t rate; /**< accelerometer's sampling rate */
uint8_t scale; /**< accelerometer's scale factor */
uint8_t type; /**< mma8x5x type */
} mma8652_params_t;
/**
@ -96,6 +107,7 @@ int mma8652_test(mma8652_t *dev);
* @param[in] address accelerometer's I2C slave address
* @param[in] dr output data rate selection in WAKE mode
* @param[in] range full scale range
* @param[in] type mma8x5x type
*
* @return 0 on success
* @return -1 if parameters are wrong
@ -104,7 +116,7 @@ int mma8652_test(mma8652_t *dev);
* @return -4 if setting to STANDBY mode failed
* @return -5 if accelerometer configuration failed
*/
int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t range);
int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t range, uint8_t type);
/**
* @brief Set user offset correction.

@ -26,6 +26,8 @@ extern "C"
{
#endif
#include "mma8652.h"
#define MMA8652_STATUS 0x00 /**< Data or FIFO Status */
#define MMA8652_OUT_X_MSB 0x01 /**< [7:0] are 8 MSBs of X data */
#define MMA8652_OUT_X_LSB 0x02 /**< [7:4] are 4 LSBs of X data */
@ -246,7 +248,14 @@ extern "C"
/**
* @brief Device ID
*/
#define MMA8652_ID 0x4A
static const uint8_t mma8x5x_device_id[MMA8x5x_TYPE_MAX] =
{
0x4A, /* MMA8652_ID */
0x5A, /* MMA8653_ID */
0x1A, /* MMA8451_ID */
0x2A, /* MMA8452_ID */
0x3A, /* MMA8453_ID */
};
#ifdef __cplusplus
}

@ -44,14 +44,14 @@ int mma8652_test(mma8652_t *dev)
}
i2c_release(dev->i2c);
if (reg != MMA8652_ID) {
if (reg != mma8x5x_device_id[dev->type]) {
return -1;
}
return 0;
}
int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t range)
int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t range, uint8_t type)
{
char reg;
@ -60,10 +60,12 @@ int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t
dev->addr = address;
dev->initialized = false;
if (dr > MMA8652_DATARATE_1HZ56 || range > MMA8652_FS_RANGE_8G) {
if (dr > MMA8652_DATARATE_1HZ56 || range > MMA8652_FS_RANGE_8G || type >= MMA8x5x_TYPE_MAX) {
return -1;
}
dev->type = type;
i2c_acquire(dev->i2c);
/* initialize the I2C bus */
if (i2c_init_master(i2c, I2C_SPEED) < 0) {

@ -57,7 +57,7 @@ void auto_init_mma8652(void)
DEBUG("[auto_init_saul] initializing mma8652 acc sensor\n");
if (mma8652_init(&mma8652_devs[i], p->i2c, p->addr, p->rate, p->scale) < 0) {
if (mma8652_init(&mma8652_devs[i], p->i2c, p->addr, p->rate, p->scale, p->type) < 0) {
DEBUG("[auto_init_saul] error during initialization\n");
return;
}

@ -12,6 +12,7 @@ TEST_MMA8652_ADDR ?= 0x1D
TEST_MMA8652_USER_OFFSET_X ?= 0
TEST_MMA8652_USER_OFFSET_Y ?= 0
TEST_MMA8652_USER_OFFSET_Z ?= 0
TEST_MMA8x5x_TYPE ?= MMA8x5x_TYPE_MMA8652
# export parameters
CFLAGS += -DTEST_MMA8652_I2C=$(TEST_MMA8652_I2C)
@ -19,5 +20,6 @@ CFLAGS += -DTEST_MMA8652_ADDR=$(TEST_MMA8652_ADDR)
CFLAGS += -DTEST_MMA8652_USER_OFFSET_X=$(TEST_MMA8652_USER_OFFSET_X)
CFLAGS += -DTEST_MMA8652_USER_OFFSET_Y=$(TEST_MMA8652_USER_OFFSET_Y)
CFLAGS += -DTEST_MMA8652_USER_OFFSET_Z=$(TEST_MMA8652_USER_OFFSET_Z)
CFLAGS += -DTEST_MMA8x5x_TYPE=$(TEST_MMA8x5x_TYPE)
include $(RIOTBASE)/Makefile.include

@ -26,6 +26,9 @@
#ifndef TEST_MMA8652_ADDR
#error "TEST_MMA8652_ADDR not defined"
#endif
#ifndef TEST_MMA8x5x_TYPE
#error "TEST_MMA8x5x_TYPE not defined"
#endif
#include <stdio.h>
@ -45,7 +48,8 @@ int main(void)
if (mma8652_init(&dev, TEST_MMA8652_I2C, TEST_MMA8652_ADDR,
MMA8652_DATARATE_DEFAULT,
MMA8652_FS_RANGE_DEFAULT) == 0) {
MMA8652_FS_RANGE_DEFAULT,
TEST_MMA8x5x_TYPE) == 0) {
puts("[OK]\n");
}
else {

Loading…
Cancel
Save