ChibiOS  19.1.4
lsm303dlhc.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2016..2018 Rocco Marco Guglielmi
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 /**
22  * @file lsm303dlhc.h
23  * @brief LSM303DLHC MEMS interface module header.
24  *
25  * @addtogroup LSM303DLHC
26  * @ingroup EX_ST
27  * @{
28  */
29 #ifndef _LSM303DLHC_H_
30 #define _LSM303DLHC_H_
31 
32 #include "hal_accelerometer.h"
33 #include "hal_compass.h"
34 
35 /*===========================================================================*/
36 /* Driver constants. */
37 /*===========================================================================*/
38 
39 /**
40  * @name Version identification
41  * @{
42  */
43 /**
44  * @brief LSM303DLHC driver version string.
45  */
46 #define EX_LSM303DLHC_VERSION "1.1.2"
47 
48 /**
49  * @brief LSM303DLHC driver version major number.
50  */
51 #define EX_LSM303DLHC_MAJOR 1
52 
53 /**
54  * @brief LSM303DLHC driver version minor number.
55  */
56 #define EX_LSM303DLHC_MINOR 1
57 
58 /**
59  * @brief LSM303DLHC driver version patch number.
60  */
61 #define EX_LSM303DLHC_PATCH 2
62 /** @} */
63 
64 /**
65  * @brief LSM303DLHC accelerometer subsystem characteristics.
66  * @note Sensitivity is expressed as milli-G/LSB whereas
67  * 1 milli-G = 0.00980665 m/s^2.
68  * @note Bias is expressed as milli-G.
69  *
70  * @{
71  */
72 #define LSM303DLHC_ACC_NUMBER_OF_AXES 3U
73 
74 #define LSM303DLHC_ACC_2G 2.0f
75 #define LSM303DLHC_ACC_4G 4.0f
76 #define LSM303DLHC_ACC_8G 8.0f
77 #define LSM303DLHC_ACC_16G 16.0f
78 
79 #define LSM303DLHC_ACC_SENS_2G 0.0610f
80 #define LSM303DLHC_ACC_SENS_4G 0.1221f
81 #define LSM303DLHC_ACC_SENS_8G 0.2442f
82 #define LSM303DLHC_ACC_SENS_16G 0.4884f
83 
84 #define LSM303DLHC_ACC_BIAS 0.0f
85 /** @} */
86 
87 /**
88  * @brief LSM303DLHC compass subsystem characteristics.
89  * @note Sensitivity is expressed as G/LSB whereas G stands for Gauss.
90  * @note Bias is expressed as G.
91  *
92  * @{
93  */
94 #define LSM303DLHC_COMP_NUMBER_OF_AXES 3U
95 
96 #define LSM303DLHC_COMP_1P3GA 1.3f
97 #define LSM303DLHC_COMP_1P9GA 1.9f
98 #define LSM303DLHC_COMP_2P5GA 2.5f
99 #define LSM303DLHC_COMP_4P0GA 4.0f
100 #define LSM303DLHC_COMP_4P7GA 4.7f
101 #define LSM303DLHC_COMP_5P6GA 5.6f
102 #define LSM303DLHC_COMP_8P1GA 8.1f
103 
104 #define LSM303DLHC_COMP_SENS_XY_1P3GA 0.000909f
105 #define LSM303DLHC_COMP_SENS_XY_1P9GA 0.001169f
106 #define LSM303DLHC_COMP_SENS_XY_2P5GA 0.0014925f
107 #define LSM303DLHC_COMP_SENS_XY_4P0GA 0.0022222f
108 #define LSM303DLHC_COMP_SENS_XY_4P7GA 0.0025000f
109 #define LSM303DLHC_COMP_SENS_XY_5P6GA 0.0030303f
110 #define LSM303DLHC_COMP_SENS_XY_8P1GA 0.0043478f
111 
112 #define LSM303DLHC_COMP_SENS_Z_1P3GA 0.0010204f
113 #define LSM303DLHC_COMP_SENS_Z_1P9GA 0.0013071f
114 #define LSM303DLHC_COMP_SENS_Z_2P5GA 0.0016666f
115 #define LSM303DLHC_COMP_SENS_Z_4P0GA 0.0025000f
116 #define LSM303DLHC_COMP_SENS_Z_4P7GA 0.0028169f
117 #define LSM303DLHC_COMP_SENS_Z_5P6GA 0.0033898f
118 #define LSM303DLHC_COMP_SENS_Z_8P1GA 0.0048780f
119 
120 #define LSM303DLHC_COMP_BIAS 0.0f
121 /** @} */
122 
123 /**
124  * @name LSM303DLHC communication interfaces related bit masks
125  * @{
126  */
127 #define LSM303DLHC_DI_MASK 0xFF
128 #define LSM303DLHC_DI(n) (1 << n)
129 #define LSM303DLHC_AD_MASK 0x7F
130 #define LSM303DLHC_AD(n) (1 << n)
131 #define LSM303DLHC_MS (1 << 7)
132 /** @} */
133 
134 /**
135  * @name LSM303DLHC register addresses
136  * @{
137  */
138 #define LSM303DLHC_AD_ACC_CTRL_REG1 0x20
139 #define LSM303DLHC_AD_ACC_CTRL_REG2 0x21
140 #define LSM303DLHC_AD_ACC_CTRL_REG3 0x22
141 #define LSM303DLHC_AD_ACC_CTRL_REG4 0x23
142 #define LSM303DLHC_AD_ACC_CTRL_REG5 0x24
143 #define LSM303DLHC_AD_ACC_CTRL_REG6 0x25
144 #define LSM303DLHC_AD_ACC_REFERENCE 0x26
145 #define LSM303DLHC_AD_ACC_STATUS_REG 0x27
146 #define LSM303DLHC_AD_ACC_OUT_X_L 0x28
147 #define LSM303DLHC_AD_ACC_OUT_X_H 0x29
148 #define LSM303DLHC_AD_ACC_OUT_Y_L 0x2A
149 #define LSM303DLHC_AD_ACC_OUT_Y_H 0x2B
150 #define LSM303DLHC_AD_ACC_OUT_Z_L 0x2C
151 #define LSM303DLHC_AD_ACC_OUT_Z_H 0x2D
152 #define LSM303DLHC_AD_ACC_FIFO_CTRL_REG 0x2E
153 #define LSM303DLHC_AD_ACC_FIFO_SRC_REG 0x2F
154 #define LSM303DLHC_AD_ACC_INT1_CFG 0x30
155 #define LSM303DLHC_AD_ACC_INT1_SRC 0x31
156 #define LSM303DLHC_AD_ACC_INT1_THS 0x32
157 #define LSM303DLHC_AD_ACC_INT1_DURATION 0x33
158 #define LSM303DLHC_AD_ACC_INT2_CFG 0x34
159 #define LSM303DLHC_AD_ACC_INT2_SRC 0x35
160 #define LSM303DLHC_AD_ACC_INT2_THS 0x36
161 #define LSM303DLHC_AD_ACC_INT2_DURATION 0x37
162 #define LSM303DLHC_AD_ACC_CLICK_CFG 0x38
163 #define LSM303DLHC_AD_ACC_CLICK_SRC 0x39
164 #define LSM303DLHC_AD_ACC_CLICK_THS 0x3A
165 #define LSM303DLHC_AD_ACC_TIME_LIMIT 0x3B
166 #define LSM303DLHC_AD_ACC_TIME_LATENCY 0x3C
167 #define LSM303DLHC_AD_ACC_TIME_WINDOW 0x3D
168 #define LSM303DLHC_AD_COMP_CRA_REG 0x00
169 #define LSM303DLHC_AD_COMP_CRB_REG 0x01
170 #define LSM303DLHC_AD_COMP_MR_REG 0x02
171 #define LSM303DLHC_AD_COMP_OUT_X_H 0x03
172 #define LSM303DLHC_AD_COMP_OUT_X_L 0x04
173 #define LSM303DLHC_AD_COMP_OUT_Z_H 0x05
174 #define LSM303DLHC_AD_COMP_OUT_Z_L 0x06
175 #define LSM303DLHC_AD_COMP_OUT_Y_H 0x07
176 #define LSM303DLHC_AD_COMP_OUT_Y_L 0x08
177 #define LSM303DLHC_AD_COMP_SR_REG 0x09
178 #define LSM303DLHC_AD_COMP_IRA_REG 0x0A
179 #define LSM303DLHC_AD_COMP_IRB_REG 0x0B
180 #define LSM303DLHC_AD_COMP_IRC_REG 0x0C
181 #define LSM303DLHC_AD_COMP_TEMP_OUT_H 0x31
182 #define LSM303DLHC_AD_COMP_TEMP_OUT_L 0x32
183 /** @} */
184 
185 /**
186  * @name LSM303DLHC_CTRL_REG1_A register bits definitions
187  * @{
188  */
189 #define LSM303DLHC_CTRL_REG1_A_MASK 0xFF
190 #define LSM303DLHC_CTRL_REG1_A_XEN (1 << 0)
191 #define LSM303DLHC_CTRL_REG1_A_YEN (1 << 1)
192 #define LSM303DLHC_CTRL_REG1_A_ZEN (1 << 2)
193 #define LSM303DLHC_CTRL_REG1_A_LPEN (1 << 3)
194 #define LSM303DLHC_CTRL_REG1_A_ODR0 (1 << 4)
195 #define LSM303DLHC_CTRL_REG1_A_ODR1 (1 << 5)
196 #define LSM303DLHC_CTRL_REG1_A_ODR2 (1 << 6)
197 #define LSM303DLHC_CTRL_REG1_A_ODR3 (1 << 7)
198 /** @} */
199 
200 /**
201  * @name LSM303DLHC_CTRL_REG2_A register bits definitions
202  * @{
203  */
204 #define LSM303DLHC_CTRL_REG2_A_MASK 0xFF
205 #define LSM303DLHC_CTRL_REG2_A_HPIS1 (1 << 0)
206 #define LSM303DLHC_CTRL_REG2_A_HPIS2 (1 << 1)
207 #define LSM303DLHC_CTRL_REG2_A_HPCLICK (1 << 2)
208 #define LSM303DLHC_CTRL_REG2_A_FDS (1 << 3)
209 #define LSM303DLHC_CTRL_REG2_A_HPCF1 (1 << 4)
210 #define LSM303DLHC_CTRL_REG2_A_HPCF2 (1 << 5)
211 #define LSM303DLHC_CTRL_REG2_A_HPM0 (1 << 6)
212 #define LSM303DLHC_CTRL_REG2_A_HPM1 (1 << 7)
213 /** @} */
214 
215 /**
216  * @name LSM303DLHC_CTRL_REG3_A register bits definitions
217  * @{
218  */
219 #define LSM303DLHC_CTRL_REG3_A_MASK 0xFD
220 #define LSM303DLHC_CTRL_REG3_A_I1_OVERRUN (1 << 1)
221 #define LSM303DLHC_CTRL_REG3_A_I1_WTM (1 << 2)
222 #define LSM303DLHC_CTRL_REG3_A_I1_DRDY2 (1 << 3)
223 #define LSM303DLHC_CTRL_REG3_A_I1_DRDY1 (1 << 4)
224 #define LSM303DLHC_CTRL_REG3_A_I1_AOI2 (1 << 5)
225 #define LSM303DLHC_CTRL_REG3_A_I1_AOI1 (1 << 6)
226 #define LSM303DLHC_CTRL_REG3_A_I1_CLICK (1 << 7)
227 /** @} */
228 
229 /**
230  * @name LSM303DLHC_CTRL_REG4_A register bits definitions
231  * @{
232  */
233 #define LSM303DLHC_CTRL_REG4_A_MASK 0xF9
234 #define LSM303DLHC_CTRL_REG4_A_SIM (1 << 0)
235 #define LSM303DLHC_CTRL_REG4_A_HR (1 << 3)
236 #define LSM303DLHC_CTRL_REG4_A_FS_MASK 0x30
237 #define LSM303DLHC_CTRL_REG4_A_FS0 (1 << 4)
238 #define LSM303DLHC_CTRL_REG4_A_FS1 (1 << 5)
239 #define LSM303DLHC_CTRL_REG4_A_BLE (1 << 6)
240 #define LSM303DLHC_CTRL_REG4_A_BDU (1 << 7)
241 /** @} */
242 
243 /**
244  * @name LSM303DLHC_CTRL_REG5_A register bits definitions
245  * @{
246  */
247 #define LSM303DLHC_CTRL_REG5_A_MASK 0xCF
248 #define LSM303DLHC_CTRL_REG5_A_D4D_INT2 (1 << 0)
249 #define LSM303DLHC_CTRL_REG5_A_LIR_INT2 (1 << 1)
250 #define LSM303DLHC_CTRL_REG5_A_D4D_INT1 (1 << 2)
251 #define LSM303DLHC_CTRL_REG5_A_LIR_INT1 (1 << 3)
252 #define LSM303DLHC_CTRL_REG5_A_FIFO_EN (1 << 6)
253 #define LSM303DLHC_CTRL_REG5_A_BOOT (1 << 7)
254 /** @} */
255 
256 /**
257  * @name LSM303DLHC_CTRL_REG6_A register bits definitions
258  * @{
259  */
260 #define LSM303DLHC_CTRL_REG6_A_MASK 0xFA
261 #define LSM303DLHC_CTRL_REG6_A_H_LACTIVE (1 << 1)
262 #define LSM303DLHC_CTRL_REG6_A_P2_ACT (1 << 3)
263 #define LSM303DLHC_CTRL_REG6_A_BOOT_I1 (1 << 4)
264 #define LSM303DLHC_CTRL_REG6_A_I2_INT2 (1 << 5)
265 #define LSM303DLHC_CTRL_REG6_A_I2_INT1 (1 << 6)
266 #define LSM303DLHC_CTRL_REG6_A_I2_CLICKEN (1 << 7)
267 /** @} */
268 
269 /**
270  * @name LSM303DLHC_CRA_REG_M register bits definitions
271  * @{
272  */
273 #define LSM303DLHC_CRA_REG_M_MASK 0x9C
274 #define LSM303DLHC_CRA_REG_M_DO0 (1 << 2)
275 #define LSM303DLHC_CRA_REG_M_DO1 (1 << 3)
276 #define LSM303DLHC_CRA_REG_M_DO2 (1 << 4)
277 #define LSM303DLHC_CRA_REG_M_TEMP_EN (1 << 7)
278 /** @} */
279 
280 /**
281  * @name LSM303DLHC_CRB_REG_M register bits definitions
282  * @{
283  */
284 #define LSM303DLHC_CRB_REG_M_MASK 0xE0
285 #define LSM303DLHC_CRB_REG_M_GN_MASK 0xE0
286 #define LSM303DLHC_CRB_REG_M_GN0 (1 << 5)
287 #define LSM303DLHC_CRB_REG_M_GN1 (1 << 6)
288 #define LSM303DLHC_CRB_REG_M_GN2 (1 << 7)
289 
290 /**
291  * @name LSM303DLHC_CRB_REG_M register bits definitions
292  * @{
293  */
294 #define LSM303DLHC_MR_REG_M_MASK 0x03
295 #define LSM303DLHC_MR_REG_M_MD0 (1 << 0)
296 #define LSM303DLHC_MR_REG_M_MD1 (1 << 1)
297 /** @} */
298 
299 /*===========================================================================*/
300 /* Driver pre-compile time settings. */
301 /*===========================================================================*/
302 
303 /**
304  * @name Configuration options
305  * @{
306  */
307 /**
308  * @brief LSM303DLHC SPI interface switch.
309  * @details If set to @p TRUE the support for SPI is included.
310  * @note The default is @p FALSE.
311  */
312 #if !defined(LSM303DLHC_USE_SPI) || defined(__DOXYGEN__)
313 #define LSM303DLHC_USE_SPI FALSE
314 #endif
315 
316 /**
317  * @brief LSM303DLHC shared SPI switch.
318  * @details If set to @p TRUE the device acquires SPI bus ownership
319  * on each transaction.
320  * @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION.
321  */
322 #if !defined(LSM303DLHC_SHARED_SPI) || defined(__DOXYGEN__)
323 #define LSM303DLHC_SHARED_SPI FALSE
324 #endif
325 
326 /**
327  * @brief LSM303DLHC I2C interface switch.
328  * @details If set to @p TRUE the support for I2C is included.
329  * @note The default is @p TRUE.
330  */
331 #if !defined(LSM303DLHC_USE_I2C) || defined(__DOXYGEN__)
332 #define LSM303DLHC_USE_I2C TRUE
333 #endif
334 
335 /**
336  * @brief LSM303DLHC shared I2C switch.
337  * @details If set to @p TRUE the device acquires I2C bus ownership
338  * on each transaction.
339  * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
340  */
341 #if !defined(LSM303DLHC_SHARED_I2C) || defined(__DOXYGEN__)
342 #define LSM303DLHC_SHARED_I2C FALSE
343 #endif
344 
345 /**
346  * @brief LSM303DLHC advanced configurations switch.
347  * @details If set to @p TRUE more configurations are available.
348  * @note The default is @p FALSE.
349  */
350 #if !defined(LSM303DLHC_USE_ADVANCED) || defined(__DOXYGEN__)
351 #define LSM303DLHC_USE_ADVANCED FALSE
352 #endif
353 /** @} */
354 
355 /*===========================================================================*/
356 /* Derived constants and error checks. */
357 /*===========================================================================*/
358 
359 #if !(LSM303DLHC_USE_SPI ^ LSM303DLHC_USE_I2C)
360 #error "LSM303DLHC_USE_SPI and LSM303DLHC_USE_I2C cannot be both true or both false"
361 #endif
362 
363 #if LSM303DLHC_USE_SPI && !HAL_USE_SPI
364 #error "LSM303DLHC_USE_SPI requires HAL_USE_SPI"
365 #endif
366 
367 #if LSM303DLHC_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
368 #error "LSM303DLHC_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
369 #endif
370 
371 #if LSM303DLHC_USE_I2C && !HAL_USE_I2C
372 #error "LSM303DLHC_USE_I2C requires HAL_USE_I2C"
373 #endif
374 
375 #if LSM303DLHC_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
376 #error "LSM303DLHC_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
377 #endif
378 
379 /**
380  * @todo Add support for LSM303DLHC over SPI.
381  */
382 #if LSM303DLHC_USE_SPI
383 #error "LSM303DLHC over SPI still not supported"
384 #endif
385 
386 /*===========================================================================*/
387 /* Driver data structures and types. */
388 /*===========================================================================*/
389 
390 /**
391  * @name LSM303DLHC accelerometer subsystem data structures and types.
392  * @{
393  */
394 /**
395  * @brief Structure representing a LSM303DLHC driver.
396  */
398 
399 /**
400  * @brief LSM303DLHC accelerometer subsystem full scale.
401  */
402 typedef enum {
403  LSM303DLHC_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
404  LSM303DLHC_ACC_FS_4G = 0x10, /**< Full scale ±4g. */
405  LSM303DLHC_ACC_FS_8G = 0x20, /**< Full scale ±8g. */
406  LSM303DLHC_ACC_FS_16G = 0x30 /**< Full scale ±16g. */
408 
409 /**
410  * @brief LSM303DLHC accelerometer subsystem output data rate.
411  */
412 typedef enum {
413  LSM303DLHC_ACC_ODR_PD = 0x00, /**< Power down */
414  LSM303DLHC_ACC_ODR_1Hz = 0x10, /**< ODR 1 Hz */
415  LSM303DLHC_ACC_ODR_10Hz = 0x20, /**< ODR 10 Hz */
416  LSM303DLHC_ACC_ODR_25Hz = 0x30, /**< ODR 25 Hz */
417  LSM303DLHC_ACC_ODR_50Hz = 0x40, /**< ODR 50 Hz */
418  LSM303DLHC_ACC_ODR_100Hz = 0x50, /**< ODR 100 Hz */
419  LSM303DLHC_ACC_ODR_200Hz = 0x60, /**< ODR 200 Hz */
420  LSM303DLHC_ACC_ODR_400Hz = 0x70, /**< ODR 400 Hz */
421  LSM303DLHC_ACC_ODR_1620Hz = 0x80, /**< ODR 1620 Hz (LP only) */
422  LSM303DLHC_ACC_ODR_1344Hz = 0x90 /**< ODR 1344 Hz or 5376 Hz in LP */
424 
425 /**
426  * @brief LSM303DLHC accelerometer subsystem axes enabling.
427  */
428 typedef enum {
429  LSM303DLHC_ACC_AE_DISABLED = 0x00,/**< All axes disabled. */
430  LSM303DLHC_ACC_AE_X = 0x01, /**< Only X-axis enabled. */
431  LSM303DLHC_ACC_AE_Y = 0x02, /**< Only Y-axis enabled. */
432  LSM303DLHC_ACC_AE_XY = 0x03, /**< X and Y axes enabled. */
433  LSM303DLHC_ACC_AE_Z = 0x04, /**< Only Z-axis enabled. */
434  LSM303DLHC_ACC_AE_XZ = 0x05, /**< X and Z axes enabled. */
435  LSM303DLHC_ACC_AE_YZ = 0x06, /**< Y and Z axes enabled. */
436  LSM303DLHC_ACC_AE_XYZ = 0x07 /**< All axes enabled. */
438 
439 /**
440  * @brief LSM303DLHC accelerometer subsystem low power mode.
441  */
442 typedef enum {
443  LSM303DLHC_ACC_LP_DISABLED = 0x00,/**< Low power mode disabled. */
444  LSM303DLHC_ACC_LP_ENABLED = 0x40 /**< Low power mode enabled. */
446 
447 /**
448  * @brief LSM303DLHC accelerometer subsystem high resolution mode.
449  */
450 typedef enum {
451  LSM303DLHC_ACC_HR_DISABLED = 0x00,/**< High resolution mode disabled. */
452  LSM303DLHC_ACC_HR_ENABLED = 0x08 /**< High resolution mode enabled. */
454 
455 /**
456  * @brief LSM303DLHC accelerometer subsystem block data update.
457  */
458 typedef enum {
459  LSM303DLHC_ACC_BDU_CONT = 0x00, /**< Continuous update */
460  LSM303DLHC_ACC_BDU_BLOCK = 0x80 /**< Update blocked */
462 
463 /**
464  * @brief LSM303DLHC accelerometer endianness.
465  */
466 typedef enum {
467  LSM303DLHC_ACC_END_LITTLE = 0x00, /**< Little Endian */
468  LSM303DLHC_ACC_END_BIG = 0x40 /**< Big Endian */
470 
471 /**
472  * @name LSM303DLHC compass subsystem data structures and types.
473  * @{
474  */
475 /**
476  * @brief LSM303DLHC compass subsystem full scale.
477  */
478 typedef enum {
479  LSM303DLHC_COMP_FS_1P3GA = 0x20, /**< Full scale ±1.3 Gauss */
480  LSM303DLHC_COMP_FS_1P9GA = 0x40, /**< Full scale ±1.9 Gauss */
481  LSM303DLHC_COMP_FS_2P5GA = 0x60, /**< Full scale ±2.5 Gauss */
482  LSM303DLHC_COMP_FS_4P0GA = 0x80, /**< Full scale ±4.0 Gauss */
483  LSM303DLHC_COMP_FS_4P7GA = 0xA0, /**< Full scale ±4.7 Gauss */
484  LSM303DLHC_COMP_FS_5P6GA = 0xC0, /**< Full scale ±5.6 Gauss */
485  LSM303DLHC_COMP_FS_8P1GA = 0xE0 /**< Full scale ±8.1 Gauss */
487 
488 /**
489  * @brief LSM303DLHC compass subsystem output data rate.
490  */
491 typedef enum {
492  LSM303DLHC_COMP_ODR_0P75HZ = 0x00,/**< ODR 0.75 Hz */
493  LSM303DLHC_COMP_ODR_1P5HZ = 0x04, /**< ODR 1.5 Hz */
494  LSM303DLHC_COMP_ODR_3P0HZ = 0x08, /**< ODR 3 Hz */
495  LSM303DLHC_COMP_ODR_7P5HZ = 0x0C, /**< ODR 7.5 Hz */
496  LSM303DLHC_COMP_ODR_15HZ = 0x10, /**< ODR 15 Hz */
497  LSM303DLHC_COMP_ODR_30HZ = 0x14, /**< ODR 30 Hz */
498  LSM303DLHC_COMP_ODR_75HZ = 0x18, /**< ODR 75 Hz */
499  LSM303DLHC_COMP_ODR_220HZ = 0x1C /**< ODR 220 Hz */
501 
502 /**
503  * @brief LSM303DLHC compass subsystem working mode.
504  */
505 typedef enum {
506  LSM303DLHC_COMP_MD_CONT = 0x00, /**< Continuous-Conversion Mode */
507  LSM303DLHC_COMP_MD_BLOCK = 0x01, /**< Single-Conversion Mode */
508  LSM303DLHC_COMP_MD_SLEEP = 0x02 /**< Sleep Mode */
510 
511 /**
512  * @name LSM303DLHC main system data structures and types.
513  * @{
514  */
515 /**
516  * @brief Driver state machine possible states.
517  */
518 typedef enum {
519  LSM303DLHC_UNINIT = 0, /**< Not initialized. */
520  LSM303DLHC_STOP = 1, /**< Stopped. */
521  LSM303DLHC_READY = 2, /**< Ready. */
523 
524 /**
525  * @brief LSM303DLHC configuration structure.
526  */
527 typedef struct {
528  /**
529  * @brief I2C driver associated to this LSM303DLHC.
530  */
532  /**
533  * @brief I2C configuration associated to this LSM303DLHC.
534  */
536  /**
537  * @brief LSM303DLHC accelerometer subsystem initial sensitivity.
538  */
540  /**
541  * @brief LSM303DLHC accelerometer subsystem initial bias.
542  */
543  float *accbias;
544  /**
545  * @brief LSM303DLHC accelerometer subsystem initial full scale.
546  */
548  /**
549  * @brief LSM303DLHC accelerometer subsystem output data rate.
550  */
552 #if LSM303DLHC_ACC_USE_ADVANCED || defined(__DOXYGEN__)
553  /**
554  * @brief LSM303DLHC accelerometer subsystem low power mode.
555  */
557  /**
558  * @brief LSM303DLHC accelerometer subsystem high resolution mode.
559  */
561  /**
562  * @brief LSM303DLHC accelerometer subsystem block data update.
563  */
565  /**
566  * @brief LSM303DLHC accelerometer endianness.
567  */
569 #endif
570  /**
571  * @brief LSM303DLHC compass initial sensitivity.
572  */
574  /**
575  * @brief LSM303DLHC compass initial bias.
576  */
577  float *compbias;
578  /**
579  * @brief LSM303DLHC compass subsystem initial full scale.
580  */
582  /**
583  * @brief LSM303DLHC compass subsystem output data rate.
584  */
586 #if LSM303DLHC_COMP_USE_ADVANCED || defined(__DOXYGEN__)
587  /**
588  * @brief LSM303DLHC compass subsystem working mode.
589  */
591 #endif
593 
594 /**
595  * @brief @p LSM303DLHC specific methods.
596  */
597 #define _lsm303dlhc_methods_alone \
598  /* Change full scale value of LSM303DLHC accelerometer subsystem.*/ \
599  msg_t (*acc_set_full_scale)(LSM303DLHCDriver *devp, \
600  lsm303dlhc_acc_fs_t fs); \
601  /* Change full scale value of LSM303DLHC compass subsystem.*/ \
602  msg_t (*comp_set_full_scale)(LSM303DLHCDriver *devp, \
603  lsm303dlhc_comp_fs_t fs); \
604 
605 /**
606  * @brief @p LSM303DLHC specific methods with inherited ones.
607  */
608 #define _lsm303dlhc_methods \
609  _base_object_methods \
610  _lsm303dlhc_methods_alone
611 
612 /**
613  * @extends BaseObjectVMT
614  *
615  * @brief @p LSM303DLHC virtual methods table.
616  */
619 };
620 
621 /**
622  * @brief @p LSM303DLHCDriver specific data.
623  */
624 #define _lsm303dlhc_data \
625  _base_sensor_data \
626  /* Driver state.*/ \
627  lsm303dlhc_state_t state; \
628  /* Current configuration data.*/ \
629  const LSM303DLHCConfig *config; \
630  /* Accelerometer subsystem axes number.*/ \
631  size_t accaxes; \
632  /* Accelerometer subsystem current sensitivity.*/ \
633  float accsensitivity[LSM303DLHC_ACC_NUMBER_OF_AXES]; \
634  /* Accelerometer subsystem current bias .*/ \
635  float accbias[LSM303DLHC_ACC_NUMBER_OF_AXES]; \
636  /* Accelerometer subsystem current full scale value.*/ \
637  float accfullscale; \
638  /* Compass subsystem axes number.*/ \
639  size_t compaxes; \
640  /* Compass subsystem current sensitivity.*/ \
641  float compsensitivity[LSM303DLHC_COMP_NUMBER_OF_AXES];\
642  /* Compass subsystem current bias.*/ \
643  float compbias[LSM303DLHC_COMP_NUMBER_OF_AXES]; \
644  /* Compass subsystem current full scale value.*/ \
645  float compfullscale;
646 
647 /**
648  * @brief LSM303DLHC 6-axis accelerometer/compass class.
649  */
651  /** @brief Virtual Methods Table.*/
652  const struct LSM303DLHCVMT *vmt;
653  /** @brief Base accelerometer interface.*/
655  /** @brief Base compass interface.*/
658 };
659 /** @} */
660 
661 /*===========================================================================*/
662 /* Driver macros. */
663 /*===========================================================================*/
664 
665 /**
666  * @brief Return the number of axes of the BaseAccelerometer.
667  *
668  * @param[in] devp pointer to @p LSM303DLHCDriver.
669  *
670  * @return the number of axes.
671  *
672  * @api
673  */
674 #define lsm303dlhcAccelerometerGetAxesNumber(devp) \
675  accelerometerGetAxesNumber(&((devp)->acc_if))
676 
677 /**
678  * @brief Retrieves raw data from the BaseAccelerometer.
679  * @note This data is retrieved from MEMS register without any algebraical
680  * manipulation.
681  * @note The axes array must be at least the same size of the
682  * BaseAccelerometer axes number.
683  *
684  * @param[in] devp pointer to @p LSM303DLHCDriver.
685  * @param[out] axes a buffer which would be filled with raw data.
686  *
687  * @return The operation status.
688  * @retval MSG_OK if the function succeeded.
689  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
690  * be retrieved using @p i2cGetErrors().
691  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
692  *
693  * @api
694  */
695 #define lsm303dlhcAccelerometerReadRaw(devp, axes) \
696  accelerometerReadRaw(&((devp)->acc_if), axes)
697 
698 /**
699  * @brief Retrieves cooked data from the BaseAccelerometer.
700  * @note This data is manipulated according to the formula
701  * cooked = (raw * sensitivity) - bias.
702  * @note Final data is expressed as milli-G.
703  * @note The axes array must be at least the same size of the
704  * BaseAccelerometer axes number.
705  *
706  * @param[in] devp pointer to @p LSM303DLHCDriver.
707  * @param[out] axes a buffer which would be filled with cooked data.
708  *
709  * @return The operation status.
710  * @retval MSG_OK if the function succeeded.
711  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
712  * be retrieved using @p i2cGetErrors().
713  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
714  *
715  * @api
716  */
717 #define lsm303dlhcAccelerometerReadCooked(devp, axes) \
718  accelerometerReadCooked(&((devp)->acc_if), axes)
719 
720 /**
721  * @brief Set bias values for the BaseAccelerometer.
722  * @note Bias must be expressed as milli-G.
723  * @note The bias buffer must be at least the same size of the
724  * BaseAccelerometer axes number.
725  *
726  * @param[in] devp pointer to @p LSM303DLHCDriver.
727  * @param[in] bp a buffer which contains biases.
728  *
729  * @return The operation status.
730  * @retval MSG_OK if the function succeeded.
731  *
732  * @api
733  */
734 #define lsm303dlhcAccelerometerSetBias(devp, bp) \
735  accelerometerSetBias(&((devp)->acc_if), bp)
736 
737 /**
738  * @brief Reset bias values for the BaseAccelerometer.
739  * @note Default biases value are obtained from device datasheet when
740  * available otherwise they are considered zero.
741  *
742  * @param[in] devp pointer to @p LSM303DLHCDriver.
743  *
744  * @return The operation status.
745  * @retval MSG_OK if the function succeeded.
746  *
747  * @api
748  */
749 #define lsm303dlhcAccelerometerResetBias(devp) \
750  accelerometerResetBias(&((devp)->acc_if))
751 
752 /**
753  * @brief Set sensitivity values for the BaseAccelerometer.
754  * @note Sensitivity must be expressed as milli-G/LSB.
755  * @note The sensitivity buffer must be at least the same size of the
756  * BaseAccelerometer axes number.
757  *
758  * @param[in] devp pointer to @p LSM303DLHCDriver.
759  * @param[in] sp a buffer which contains sensitivities.
760  *
761  * @return The operation status.
762  * @retval MSG_OK if the function succeeded.
763  *
764  * @api
765  */
766 #define lsm303dlhcAccelerometerSetSensitivity(devp, sp) \
767  accelerometerSetSensitivity(&((devp)->acc_if), sp)
768 
769 /**
770  * @brief Reset sensitivity values for the BaseAccelerometer.
771  * @note Default sensitivities value are obtained from device datasheet.
772  *
773  * @param[in] devp pointer to @p LSM303DLHCDriver.
774  *
775  * @return The operation status.
776  * @retval MSG_OK if the function succeeded.
777  * @retval MSG_RESET otherwise.
778  *
779  * @api
780  */
781 #define lsm303dlhcAccelerometerResetSensitivity(devp) \
782  accelerometerResetSensitivity(&((devp)->acc_if))
783 
784 /**
785  * @brief Changes the LSM303DLHCDriver accelerometer fullscale value.
786  * @note This function also rescale sensitivities and biases based on
787  * previous and next fullscale value.
788  * @note A recalibration is highly suggested after calling this function.
789  *
790  * @param[in] devp pointer to @p LSM303DLHCDriver.
791  * @param[in] fs new fullscale value.
792  *
793  * @return The operation status.
794  * @retval MSG_OK if the function succeeded.
795  * @retval MSG_RESET otherwise.
796  *
797  * @api
798  */
799 #define lsm303dlhcAccelerometerSetFullScale(devp, fs) \
800  (devp)->vmt->acc_set_full_scale(devp, fs)
801 
802 /**
803  * @brief Return the number of axes of the BaseCompass.
804  *
805  * @param[in] devp pointer to @p LSM303DLHCDriver.
806  *
807  * @return the number of axes.
808  *
809  * @api
810  */
811 #define lsm303dlhcCompassGetAxesNumber(devp) \
812  compassGetAxesNumber(&((devp)->comp_if))
813 
814 /**
815  * @brief Retrieves raw data from the BaseCompass.
816  * @note This data is retrieved from MEMS register without any algebraical
817  * manipulation.
818  * @note The axes array must be at least the same size of the
819  * BaseCompass axes number.
820  *
821  * @param[in] devp pointer to @p BaseCompass interface.
822  * @param[out] axes a buffer which would be filled with raw data.
823  *
824  * @return The operation status.
825  * @retval MSG_OK if the function succeeded.
826  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
827  * be retrieved using @p i2cGetErrors().
828  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
829  *
830  * @api
831  */
832 #define lsm303dlhcCompassReadRaw(devp, axes) \
833  compassReadRaw(&((devp)->comp_if), axes)
834 
835 /**
836  * @brief Retrieves cooked data from the BaseCompass.
837  * @note This data is manipulated according to the formula
838  * cooked = (raw * sensitivity) - bias.
839  * @note Final data is expressed as G.
840  * @note The axes array must be at least the same size of the
841  * BaseCompass axes number.
842  *
843  * @param[in] devp pointer to @p BaseCompass interface.
844  * @param[out] axes a buffer which would be filled with cooked data.
845  *
846  * @return The operation status.
847  * @retval MSG_OK if the function succeeded.
848  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
849  * be retrieved using @p i2cGetErrors().
850  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
851  *
852  * @api
853  */
854 #define lsm303dlhcCompassReadCooked(devp, axes) \
855  compassReadCooked(&((devp)->comp_if), axes)
856 
857 /**
858  * @brief Set bias values for the BaseCompass.
859  * @note Bias must be expressed as G.
860  * @note The bias buffer must be at least the same size of the
861  * BaseCompass axes number.
862  *
863  * @param[in] devp pointer to @p BaseCompass interface.
864  * @param[in] bp a buffer which contains biases.
865  *
866  * @return The operation status.
867  * @retval MSG_OK if the function succeeded.
868  *
869  * @api
870  */
871 #define lsm303dlhcCompassSetBias(devp, bp) \
872  compassSetBias(&((devp)->comp_if), bp)
873 
874 /**
875  * @brief Reset bias values for the BaseCompass.
876  * @note Default biases value are obtained from device datasheet when
877  * available otherwise they are considered zero.
878  *
879  * @param[in] devp pointer to @p LSM303DLHCDriver.
880  *
881  * @return The operation status.
882  * @retval MSG_OK if the function succeeded.
883  *
884  * @api
885  */
886 #define lsm303dlhcCompassResetBias(devp) \
887  compassResetBias(&((devp)->comp_if))
888 
889 /**
890  * @brief Set sensitivity values for the BaseCompass.
891  * @note Sensitivity must be expressed as G/LSB.
892  * @note The sensitivity buffer must be at least the same size of the
893  * BaseCompass axes number.
894  *
895  * @param[in] devp pointer to @p LSM303DLHCDriver.
896  * @param[in] sp a buffer which contains sensitivities.
897  *
898  * @return The operation status.
899  * @retval MSG_OK if the function succeeded.
900  *
901  * @api
902  */
903 #define lsm303dlhcCompassSetSensitivity(devp, sp) \
904  compassSetSensitivity(&((devp)->comp_if), sp)
905 
906 /**
907  * @brief Reset sensitivity values for the BaseCompass.
908  * @note Default sensitivities value are obtained from device datasheet.
909  *
910  * @param[in] devp pointer to @p LSM303DLHCDriver.
911  *
912  * @return The operation status.
913  * @retval MSG_OK if the function succeeded.
914  * @retval MSG_RESET otherwise.
915  *
916  * @api
917  */
918 #define lsm303dlhcCompassResetSensitivity(devp) \
919  compassResetSensitivity(&((devp)->comp_if))
920 
921 /**
922  * @brief Changes the LSM303DLHCDriver compass fullscale value.
923  * @note This function also rescale sensitivities and biases based on
924  * previous and next fullscale value.
925  * @note A recalibration is highly suggested after calling this function.
926  *
927  * @param[in] devp pointer to @p LSM303DLHCDriver.
928  * @param[in] fs new fullscale value.
929  *
930  * @return The operation status.
931  * @retval MSG_OK if the function succeeded.
932  * @retval MSG_RESET otherwise.
933  *
934  * @api
935  */
936 #define lsm303dlhcCompassSetFullScale(devp, fs) \
937  (devp)->vmt->comp_set_full_scale(devp, fs)
938 
939 /*===========================================================================*/
940 /* External declarations. */
941 /*===========================================================================*/
942 
943 #ifdef __cplusplus
944 extern "C" {
945 #endif
947  void lsm303dlhcStart(LSM303DLHCDriver *devp, const LSM303DLHCConfig *config);
948  void lsm303dlhcStop(LSM303DLHCDriver *devp);
949 #ifdef __cplusplus
950 }
951 #endif
952 
953 #endif /* _LSM303DLHC_H_ */
954 
955 /** @} */
#define _lsm303dlhc_methods
LSM303DLHC specific methods with inherited ones.
Definition: lsm303dlhc.h:608
lsm303dlhc_comp_md_t
LSM303DLHC compass subsystem working mode.
Definition: lsm303dlhc.h:505
lsm303dlhc_acc_lp_t acclowpower
LSM303DLHC accelerometer subsystem low power mode.
Definition: lsm303dlhc.h:556
float * compbias
LSM303DLHC compass initial bias.
Definition: lsm303dlhc.h:577
lsm303dlhc_acc_end_t
LSM303DLHC accelerometer endianness.
Definition: lsm303dlhc.h:466
lsm303dlhc_comp_md_t compmode
LSM303DLHC compass subsystem working mode.
Definition: lsm303dlhc.h:590
Type of I2C driver configuration structure.
Definition: hal_i2c_lld.h:75
Base compass class.
Definition: hal_compass.h:86
float * accsensitivity
LSM303DLHC accelerometer subsystem initial sensitivity.
Definition: lsm303dlhc.h:539
float * accbias
LSM303DLHC accelerometer subsystem initial bias.
Definition: lsm303dlhc.h:543
lsm303dlhc_comp_odr_t
LSM303DLHC compass subsystem output data rate.
Definition: lsm303dlhc.h:491
lsm303dlhc_comp_odr_t compoutputdatarate
LSM303DLHC compass subsystem output data rate.
Definition: lsm303dlhc.h:585
const I2CConfig * i2ccfg
I2C configuration associated to this LSM303DLHC.
Definition: lsm303dlhc.h:535
void lsm303dlhcStop(LSM303DLHCDriver *devp)
Deactivates the LSM303DLHC Complex Driver peripheral.
Definition: lsm303dlhc.c:1142
I2CDriver * i2cp
I2C driver associated to this LSM303DLHC.
Definition: lsm303dlhc.h:531
lsm303dlhc_acc_ae_t
LSM303DLHC accelerometer subsystem axes enabling.
Definition: lsm303dlhc.h:428
#define _lsm303dlhc_data
LSM303DLHCDriver specific data.
Definition: lsm303dlhc.h:624
lsm303dlhc_acc_odr_t
LSM303DLHC accelerometer subsystem output data rate.
Definition: lsm303dlhc.h:412
lsm303dlhc_acc_bdu_t accblockdataupdate
LSM303DLHC accelerometer subsystem block data update.
Definition: lsm303dlhc.h:564
lsm303dlhc_acc_odr_t accoutdatarate
LSM303DLHC accelerometer subsystem output data rate.
Definition: lsm303dlhc.h:551
BaseCompass comp_if
Base compass interface.
Definition: lsm303dlhc.h:656
BaseAccelerometer acc_if
Base accelerometer interface.
Definition: lsm303dlhc.h:654
Structure representing an I2C driver.
Definition: hal_i2c_lld.h:88
LSM303DLHC configuration structure.
Definition: lsm303dlhc.h:527
lsm303dlhc_acc_end_t accendianess
LSM303DLHC accelerometer endianness.
Definition: lsm303dlhc.h:568
Generic accelerometer interface header.
Generic compass interface header.
const struct LSM303DLHCVMT * vmt
Virtual Methods Table.
Definition: lsm303dlhc.h:652
lsm303dlhc_acc_bdu_t
LSM303DLHC accelerometer subsystem block data update.
Definition: lsm303dlhc.h:458
float * compsensitivity
LSM303DLHC compass initial sensitivity.
Definition: lsm303dlhc.h:573
lsm303dlhc_state_t
Driver state machine possible states.
Definition: lsm303dlhc.h:518
void lsm303dlhcStart(LSM303DLHCDriver *devp, const LSM303DLHCConfig *config)
Configures and activates LSM303DLHC Complex Driver peripheral.
Definition: lsm303dlhc.c:868
lsm303dlhc_acc_lp_t
LSM303DLHC accelerometer subsystem low power mode.
Definition: lsm303dlhc.h:442
void lsm303dlhcObjectInit(LSM303DLHCDriver *devp)
Initializes an instance.
Definition: lsm303dlhc.c:847
lsm303dlhc_comp_fs_t compfullscale
LSM303DLHC compass subsystem initial full scale.
Definition: lsm303dlhc.h:581
LSM303DLHC 6-axis accelerometer/compass class.
Definition: lsm303dlhc.h:650
lsm303dlhc_acc_fs_t
LSM303DLHC accelerometer subsystem full scale.
Definition: lsm303dlhc.h:402
LSM303DLHC virtual methods table.
Definition: lsm303dlhc.h:617
lsm303dlhc_acc_hr_t acchighresmode
LSM303DLHC accelerometer subsystem high resolution mode.
Definition: lsm303dlhc.h:560
lsm303dlhc_acc_fs_t accfullscale
LSM303DLHC accelerometer subsystem initial full scale.
Definition: lsm303dlhc.h:547
Base accelerometer class.
lsm303dlhc_comp_fs_t
LSM303DLHC compass subsystem full scale.
Definition: lsm303dlhc.h:478
lsm303dlhc_acc_hr_t
LSM303DLHC accelerometer subsystem high resolution mode.
Definition: lsm303dlhc.h:450