Blame view

RIOT/drivers/include/mma8652.h 6.91 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  /*
   * Copyright (C) 2014 PHYTEC Messtechnik GmbH
   *
   * 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.
   */
  
  /**
   * @defgroup    drivers_mma8652 MMA8652 Accelerometer
   * @ingroup     drivers_sensors
   * @brief       Driver for the Freescale MMA8652 3-Axis accelerometer.
   *              The driver will initialize the accelerometer for
   *              best resolution (12 bit).
   *              After initialization and set activ the accelerometer
   *              will make measurements at periodic times.
   *              The measurements period and scale range can be determined by
   *              accelerometer initialization.
   *              This driver only implements basic functionality.
   *
   * @{
   *
   * @file
   * @brief       Interface definition for the MMA8652 accelerometer driver.
   *
   * @author      Johann Fischer <j.fischer@phytec.de>
   */
  
  #ifndef MMA8652_H
  #define MMA8652_H
  
  #include <stdint.h>
  #include <stdbool.h>
  #include "periph/i2c.h"
  
  #ifdef __cplusplus
  extern "C"
  {
  #endif
  
  #ifndef MMA8652_I2C_ADDRESS
  #define MMA8652_I2C_ADDRESS           0x1D /**< Accelerometer Default Address */
  #endif
  
  #define MMA8652_DATARATE_800HZ        0 /**< 800 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_400HZ        1 /**< 400 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_200HZ        2 /**< 200 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_100HZ        3 /**< 100 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_50HZ         4 /**< 50 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_1HZ25        5 /**< 12.5 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_6HZ25        6 /**< 6.25 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_1HZ56        7 /**< 1.56 Hz Ouput Data Rate in WAKE mode */
  #define MMA8652_DATARATE_DEFAULT      MMA8652_DATARATE_6HZ25 /**< Data Rate for testing */
  
  #define MMA8652_FS_RANGE_2G           0 /**< +/- 2 g Full Scale Range */
  #define MMA8652_FS_RANGE_4G           1 /**< +/- 4 g Full Scale Range */
  #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.
   */
  typedef struct {
      i2c_t i2c;              /**< I2C device, the accelerometer is connected to */
      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;
  
  /**
   * @brief Data structure holding all the information needed for initialization
   */
  typedef struct {
      i2c_t i2c;              /**< I2C bus used */
      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;
  
  /**
   * @brief MMA8652 accelerometer test.
   * This function looks for Device ID of the MMA8652 accelerometer.
   *
   * @param[in]  dev          device descriptor of accelerometer
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mma8652_test(mma8652_t *dev);
  
  /**
   * @brief Initialize the MMA8652 accelerometer driver.
   *
   * @param[out] dev          device descriptor of accelerometer to initialize
   * @param[in]  i2c          I2C bus the accelerometer is connected to
   * @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
   * @return                  -2 if initialization of I2C bus failed
   * @return                  -3 if accelerometer test failed
   * @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, uint8_t type);
  
  /**
   * @brief Set user offset correction.
   * Offset correction registers will be erased after accelerometer reset.
   *
   * @param[out] dev          device descriptor of accelerometer to initialize
   * @param[in]  x            offset correction value for x-axis
   * @param[in]  y            offset correction value for y-axis
   * @param[in]  z            offset correction value for z-axis
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mma8652_set_user_offset(mma8652_t *dev, int8_t x, int8_t y, int8_t z);
  
  /**
   * @brief Reset the MMA8652 accelerometer. After that accelerometer should be reinitialized.
   *
   * @param[out] dev          device descriptor of accelerometer to reset
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mma8652_reset(mma8652_t *dev);
  
  /**
   * @brief Set active mode, this enables periodic measurements.
   *
   * @param[out] dev          device descriptor of accelerometer to reset
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mma8652_set_active(mma8652_t *dev);
  
  /**
   * @brief Set standby mode.
   *
   * @param[in]  dev          device descriptor of accelerometer
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mma8652_set_standby(mma8652_t *dev);
  
  /**
   * @brief Check for new set of measurement data.
   *
   * @param[in]  dev          device descriptor of accelerometer
   *
   * @return                  >0 if x- ,y- ,z-axis new sample is ready
   * @return                  0 measurement in progress
   * @return                  -1 on error
   */
  int mma8652_is_ready(mma8652_t *dev);
  
  /**
   * @brief Read accelerometer's data.
   * Acceleration will be calculated as:<br>
   *     \f$ a = \frac{value \cdot 1000}{1024} \cdot mg \f$ if full scale is set to 2g<br>
   *     \f$ a = \frac{value \cdot 1000}{512} \cdot mg \f$ if full scale is set to 4g<br>
   *     \f$ a = \frac{value \cdot 1000}{256} \cdot mg \f$ if full scale is set to 8g<br>
   *
   * @param[in]  dev          device descriptor of accelerometer
   * @param[out] x            x-axis value in mg
   * @param[out] y            y-axis value in mg
   * @param[out] z            z-axis value in mg
   * @param[out] status       accelerometer status register
   *
   * @return                  0 on success
   * @return                  -1 on error
   */
  int mma8652_read(mma8652_t *dev, int16_t *x, int16_t *y, int16_t *z, uint8_t *status);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif
  /** @} */