Skip to content

Commit a037587

Browse files
committed
Merge pull request RIOT-OS#4783 from cgundogan/pr/pba-d-01-kw2x/saul_mma8652
drivers/mma8652: pba-d-01-kw2x: add support for SAUL
2 parents 0e6ade7 + 7d35c06 commit a037587

File tree

6 files changed

+210
-0
lines changed

6 files changed

+210
-0
lines changed

boards/pba-d-01-kw2x/Makefile.dep

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ ifneq (,$(filter gnrc_netif_default,$(USEMODULE)))
22
USEMODULE += kw2xrf
33
USEMODULE += gnrc_nomac
44
endif
5+
6+
ifneq (,$(filter saul_default,$(USEMODULE)))
7+
USEMODULE += mma8652
8+
endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2016 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup boards_pba-d-01-kw2x
11+
* @{
12+
*
13+
* @file
14+
* @brief MMA8652 board specific configuration
15+
*
16+
* @author Cenk Gündoğan <mail@cgundogan.de>
17+
*/
18+
19+
#ifndef MMA8652_PARAMS_H
20+
#define MMA8652_PARAMS_H
21+
22+
#include "board.h"
23+
#include "saul_reg.h"
24+
#include "mma8652.h"
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
/**
31+
* @brief MMA852 configuration
32+
*/
33+
static const mma8652_params_t mma8652_params[] =
34+
{
35+
{
36+
.i2c = MMA8652_I2C,
37+
.addr = MMA8652_ADDR,
38+
.rate = MMA8652_DATARATE_DEFAULT,
39+
.scale = MMA8652_FS_RANGE_DEFAULT,
40+
},
41+
};
42+
43+
/**
44+
* @brief Additional meta information to keep in the SAUL registry
45+
*/
46+
static const saul_reg_info_t mma8652_saul_info[] =
47+
{
48+
{
49+
.name = "mma8652",
50+
},
51+
};
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
57+
#endif /* MMA8652_PARAMS_H */
58+
/** @} */

drivers/include/mma8652.h

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ typedef struct {
6767
int16_t scale; /**< each count corresponds to (1/scale) g */
6868
} mma8652_t;
6969

70+
/**
71+
* @brief Data structure holding all the information needed for initialization
72+
*/
73+
typedef struct {
74+
i2c_t i2c; /**< I2C bus used */
75+
uint8_t addr; /**< accelerometer's I2C address */
76+
uint8_t rate; /**< accelerometer's sampling rate */
77+
uint8_t scale; /**< accelerometer's scale factor */
78+
} mma8652_params_t;
79+
7080
/**
7181
* @brief MMA8652 accelerometer test.
7282
* This function looks for Device ID of the MMA8652 accelerometer.

drivers/mma8652/mma8652_saul.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2016 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup driver_mma8652
11+
* @{
12+
*
13+
* @file
14+
* @brief MMA8652 adaption to the RIOT actuator/sensor interface
15+
*
16+
* @author Cenk Gündoğan <mail@cgundogan.de>
17+
*
18+
* @}
19+
*/
20+
21+
#include <string.h>
22+
#include <stdio.h>
23+
24+
#include "saul.h"
25+
#include "mma8652.h"
26+
27+
static int read_acc(void *dev, phydat_t *res)
28+
{
29+
int16_t x, y, z;
30+
uint8_t status;
31+
32+
mma8652_t *d = (mma8652_t *)dev;
33+
mma8652_read(d, &x, &y, &z, &status);
34+
35+
res->val[0] = x;
36+
res->val[1] = y;
37+
res->val[2] = z;
38+
res->unit = UNIT_G;
39+
res->scale = -3;
40+
41+
return 3;
42+
}
43+
44+
static int write(void *dev, phydat_t *state)
45+
{
46+
(void) dev;
47+
(void) state;
48+
return -ENOTSUP;
49+
}
50+
51+
const saul_driver_t mma8652_saul_driver = {
52+
.read = read_acc,
53+
.write = write,
54+
.type = SAUL_SENSE_ACCEL,
55+
};

sys/auto_init/auto_init.c

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ void auto_init(void)
234234
extern void auto_init_lis3dh(void);
235235
auto_init_lis3dh();
236236
#endif
237+
#ifdef MODULE_MMA8652
238+
extern void auto_init_mma8652(void);
239+
auto_init_mma8652();
240+
#endif
237241

238242
#endif /* MODULE_AUTO_INIT_SAUL */
239243
}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (C) 2016 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*
8+
*/
9+
10+
/**
11+
* @ingroup auto_init_saul
12+
* @{
13+
*
14+
* @file
15+
* @brief Auto initialization of MMA8652 accelerometer
16+
*
17+
* @author Cenk Gündoğan <mail@cgundogan.de>
18+
*
19+
* @}
20+
*/
21+
22+
#ifdef MODULE_MMA8652
23+
24+
#include "saul_reg.h"
25+
#include "mma8652.h"
26+
#include "mma8652_params.h"
27+
28+
#define ENABLE_DEBUG (0)
29+
#include "debug.h"
30+
31+
/**
32+
* @brief Define the number of configured sensors
33+
*/
34+
#define MMA8652_NUM (sizeof(mma8652_params)/sizeof(mma8652_params[0]))
35+
36+
/**
37+
* @brief Allocate memory for the device descriptors
38+
*/
39+
static mma8652_t mma8652_devs[MMA8652_NUM];
40+
41+
/**
42+
* @brief Memory for the SAUL registry entries
43+
*/
44+
static saul_reg_t saul_entries[MMA8652_NUM];
45+
46+
/**
47+
* @brief Reference the driver struct
48+
* @{
49+
*/
50+
extern saul_driver_t mma8652_saul_driver;
51+
/** @} */
52+
53+
void auto_init_mma8652(void)
54+
{
55+
for (int i = 0; i < MMA8652_NUM; i++) {
56+
const mma8652_params_t *p = &mma8652_params[i];
57+
58+
DEBUG("[auto_init_saul] initializing mma8652 acc sensor\n");
59+
60+
if (mma8652_init(&mma8652_devs[i], p->i2c, p->addr, p->rate, p->scale) < 0) {
61+
DEBUG("[auto_init_saul] error during initialization\n");
62+
return;
63+
}
64+
65+
if (mma8652_set_active(&mma8652_devs[i]) < 0) {
66+
DEBUG("[auto_init_saul] error activating mma8652\n");
67+
return;
68+
}
69+
70+
saul_entries[i].dev = &(mma8652_devs[i]);
71+
saul_entries[i].name = mma8652_saul_info[i].name;
72+
saul_entries[i].driver = &mma8652_saul_driver;
73+
saul_reg_add(&(saul_entries[i]));
74+
}
75+
}
76+
77+
#else
78+
typedef int dont_be_pedantic;
79+
#endif /* MODULE_MMA8652 */

0 commit comments

Comments
 (0)