ChibiOS  0.0.0
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.1"
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 1
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 accelerometer subsystem advanced configurations
347  * switch.
348  * @details If set to @p TRUE more configurations are available.
349  * @note The default is @p FALSE.
350  */
351 #if !defined(LSM303DLHC_ACC_USE_ADVANCED) || defined(__DOXYGEN__)
352 #define LSM303DLHC_ACC_USE_ADVANCED FALSE
353 #endif
354 
355 /**
356  * @brief LSM303DLHC compass subsystem advanced configurations
357  * switch.
358  * @details If set to @p TRUE more configurations are available.
359  * @note The default is @p FALSE.
360  */
361 #if !defined(LSM303DLHC_COMP_USE_ADVANCED) || defined(__DOXYGEN__)
362 #define LSM303DLHC_COMP_USE_ADVANCED FALSE
363 #endif
364 /** @} */
365 
366 /*===========================================================================*/
367 /* Derived constants and error checks. */
368 /*===========================================================================*/
369 
370 #if !(LSM303DLHC_USE_SPI ^ LSM303DLHC_USE_I2C)
371 #error "LSM303DLHC_USE_SPI and LSM303DLHC_USE_I2C cannot be both true or both false"
372 #endif
373 
374 #if LSM303DLHC_USE_SPI && !HAL_USE_SPI
375 #error "LSM303DLHC_USE_SPI requires HAL_USE_SPI"
376 #endif
377 
378 #if LSM303DLHC_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
379 #error "LSM303DLHC_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
380 #endif
381 
382 #if LSM303DLHC_USE_I2C && !HAL_USE_I2C
383 #error "LSM303DLHC_USE_I2C requires HAL_USE_I2C"
384 #endif
385 
386 #if LSM303DLHC_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
387 #error "LSM303DLHC_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
388 #endif
389 
390 /**
391  * @todo Add support for LSM303DLHC over SPI.
392  */
393 #if LSM303DLHC_USE_SPI
394 #error "LSM303DLHC over SPI still not supported"
395 #endif
396 
397 /*===========================================================================*/
398 /* Driver data structures and types. */
399 /*===========================================================================*/
400 
401 /**
402  * @name LSM303DLHC accelerometer subsystem data structures and types.
403  * @{
404  */
405 /**
406  * @brief Structure representing a LSM303DLHC driver.
407  */
409 
410 /**
411  * @brief LSM303DLHC accelerometer subsystem full scale.
412  */
413 typedef enum {
414  LSM303DLHC_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
415  LSM303DLHC_ACC_FS_4G = 0x10, /**< Full scale ±4g. */
416  LSM303DLHC_ACC_FS_8G = 0x20, /**< Full scale ±8g. */
417  LSM303DLHC_ACC_FS_16G = 0x30 /**< Full scale ±16g. */
419 
420 /**
421  * @brief LSM303DLHC accelerometer subsystem output data rate.
422  */
423 typedef enum {
424  LSM303DLHC_ACC_ODR_PD = 0x00, /**< Power down */
425  LSM303DLHC_ACC_ODR_1Hz = 0x10, /**< ODR 1 Hz */
426  LSM303DLHC_ACC_ODR_10Hz = 0x20, /**< ODR 10 Hz */
427  LSM303DLHC_ACC_ODR_25Hz = 0x30, /**< ODR 25 Hz */
428  LSM303DLHC_ACC_ODR_50Hz = 0x40, /**< ODR 50 Hz */
429  LSM303DLHC_ACC_ODR_100Hz = 0x50, /**< ODR 100 Hz */
430  LSM303DLHC_ACC_ODR_200Hz = 0x60, /**< ODR 200 Hz */
431  LSM303DLHC_ACC_ODR_400Hz = 0x70, /**< ODR 400 Hz */
432  LSM303DLHC_ACC_ODR_1620Hz = 0x80, /**< ODR 1620 Hz (LP only) */
433  LSM303DLHC_ACC_ODR_1344Hz = 0x90 /**< ODR 1344 Hz or 5376 Hz in LP */
435 
436 /**
437  * @brief LSM303DLHC accelerometer subsystem axes enabling.
438  */
439 typedef enum {
440  LSM303DLHC_ACC_AE_DISABLED = 0x00,/**< All axes disabled. */
441  LSM303DLHC_ACC_AE_X = 0x01, /**< Only X-axis enabled. */
442  LSM303DLHC_ACC_AE_Y = 0x02, /**< Only Y-axis enabled. */
443  LSM303DLHC_ACC_AE_XY = 0x03, /**< X and Y axes enabled. */
444  LSM303DLHC_ACC_AE_Z = 0x04, /**< Only Z-axis enabled. */
445  LSM303DLHC_ACC_AE_XZ = 0x05, /**< X and Z axes enabled. */
446  LSM303DLHC_ACC_AE_YZ = 0x06, /**< Y and Z axes enabled. */
447  LSM303DLHC_ACC_AE_XYZ = 0x07 /**< All axes enabled. */
449 
450 /**
451  * @brief LSM303DLHC accelerometer subsystem low power mode.
452  */
453 typedef enum {
454  LSM303DLHC_ACC_LP_DISABLED = 0x00,/**< Low power mode disabled. */
455  LSM303DLHC_ACC_LP_ENABLED = 0x40 /**< Low power mode enabled. */
457 
458 /**
459  * @brief LSM303DLHC accelerometer subsystem high resolution mode.
460  */
461 typedef enum {
462  LSM303DLHC_ACC_HR_DISABLED = 0x00,/**< High resolution mode disabled. */
463  LSM303DLHC_ACC_HR_ENABLED = 0x08 /**< High resolution mode enabled. */
465 
466 /**
467  * @brief LSM303DLHC accelerometer subsystem block data update.
468  */
469 typedef enum {
470  LSM303DLHC_ACC_BDU_CONT = 0x00, /**< Continuous update */
471  LSM303DLHC_ACC_BDU_BLOCK = 0x80 /**< Update blocked */
473 
474 /**
475  * @brief LSM303DLHC accelerometer endianness.
476  */
477 typedef enum {
478  LSM303DLHC_ACC_END_LITTLE = 0x00, /**< Little Endian */
479  LSM303DLHC_ACC_END_BIG = 0x40 /**< Big Endian */
481 
482 /**
483  * @name LSM303DLHC compass subsystem data structures and types.
484  * @{
485  */
486 /**
487  * @brief LSM303DLHC compass subsystem full scale.
488  */
489 typedef enum {
490  LSM303DLHC_COMP_FS_1P3GA = 0x20, /**< Full scale ±1.3 Gauss */
491  LSM303DLHC_COMP_FS_1P9GA = 0x40, /**< Full scale ±1.9 Gauss */
492  LSM303DLHC_COMP_FS_2P5GA = 0x60, /**< Full scale ±2.5 Gauss */
493  LSM303DLHC_COMP_FS_4P0GA = 0x80, /**< Full scale ±4.0 Gauss */
494  LSM303DLHC_COMP_FS_4P7GA = 0xA0, /**< Full scale ±4.7 Gauss */
495  LSM303DLHC_COMP_FS_5P6GA = 0xC0, /**< Full scale ±5.6 Gauss */
496  LSM303DLHC_COMP_FS_8P1GA = 0xE0 /**< Full scale ±8.1 Gauss */
498 
499 /**
500  * @brief LSM303DLHC compass subsystem output data rate.
501  */
502 typedef enum {
503  LSM303DLHC_COMP_ODR_0P75HZ = 0x00,/**< ODR 0.75 Hz */
504  LSM303DLHC_COMP_ODR_1P5HZ = 0x04, /**< ODR 1.5 Hz */
505  LSM303DLHC_COMP_ODR_3P0HZ = 0x08, /**< ODR 3 Hz */
506  LSM303DLHC_COMP_ODR_7P5HZ = 0x0C, /**< ODR 7.5 Hz */
507  LSM303DLHC_COMP_ODR_15HZ = 0x10, /**< ODR 15 Hz */
508  LSM303DLHC_COMP_ODR_30HZ = 0x14, /**< ODR 30 Hz */
509  LSM303DLHC_COMP_ODR_75HZ = 0x18, /**< ODR 75 Hz */
510  LSM303DLHC_COMP_ODR_220HZ = 0x1C /**< ODR 220 Hz */
512 
513 /**
514  * @brief LSM303DLHC compass subsystem working mode.
515  */
516 typedef enum {
517  LSM303DLHC_COMP_MD_CONT = 0x00, /**< Continuous-Conversion Mode */
518  LSM303DLHC_COMP_MD_BLOCK = 0x01, /**< Single-Conversion Mode */
519  LSM303DLHC_COMP_MD_SLEEP = 0x02 /**< Sleep Mode */
521 
522 /**
523  * @name LSM303DLHC main system data structures and types.
524  * @{
525  */
526 /**
527  * @brief Driver state machine possible states.
528  */
529 typedef enum {
530  LSM303DLHC_UNINIT = 0, /**< Not initialized. */
531  LSM303DLHC_STOP = 1, /**< Stopped. */
532  LSM303DLHC_READY = 2, /**< Ready. */
534 
535 /**
536  * @brief LSM303DLHC configuration structure.
537  */
538 typedef struct {
539  /**
540  * @brief I2C driver associated to this LSM303DLHC.
541  */
543  /**
544  * @brief I2C configuration associated to this LSM303DLHC.
545  */
547  /**
548  * @brief LSM303DLHC accelerometer subsystem initial sensitivity.
549  */
551  /**
552  * @brief LSM303DLHC accelerometer subsystem initial bias.
553  */
554  float *accbias;
555  /**
556  * @brief LSM303DLHC accelerometer subsystem initial full scale.
557  */
559  /**
560  * @brief LSM303DLHC accelerometer subsystem output data rate.
561  */
563 #if LSM303DLHC_ACC_USE_ADVANCED || defined(__DOXYGEN__)
564  /**
565  * @brief LSM303DLHC accelerometer subsystem low power mode.
566  */
568  /**
569  * @brief LSM303DLHC accelerometer subsystem high resolution mode.
570  */
572  /**
573  * @brief LSM303DLHC accelerometer subsystem block data update.
574  */
576  /**
577  * @brief LSM303DLHC accelerometer endianness.
578  */
580 #endif
581  /**
582  * @brief LSM303DLHC compass initial sensitivity.
583  */
585  /**
586  * @brief LSM303DLHC compass initial bias.
587  */
588  float *compbias;
589  /**
590  * @brief LSM303DLHC compass subsystem initial full scale.
591  */
593  /**
594  * @brief LSM303DLHC compass subsystem output data rate.
595  */
597 #if LSM303DLHC_COMP_USE_ADVANCED || defined(__DOXYGEN__)
598  /**
599  * @brief LSM303DLHC compass subsystem working mode.
600  */
602 #endif
604 
605 /**
606  * @brief @p LSM303DLHC specific methods.
607  */
608 #define _lsm303dlhc_methods_alone \
609  /* Change full scale value of LSM303DLHC accelerometer subsystem.*/ \
610  msg_t (*acc_set_full_scale)(LSM303DLHCDriver *devp, \
611  lsm303dlhc_acc_fs_t fs); \
612  /* Change full scale value of LSM303DLHC compass subsystem.*/ \
613  msg_t (*comp_set_full_scale)(LSM303DLHCDriver *devp, \
614  lsm303dlhc_comp_fs_t fs); \
615 
616 /**
617  * @brief @p LSM303DLHC specific methods with inherited ones.
618  */
619 #define _lsm303dlhc_methods \
620  _base_object_methods \
621  _lsm303dlhc_methods_alone
622 
623 /**
624  * @extends BaseObjectVMT
625  *
626  * @brief @p LSM303DLHC virtual methods table.
627  */
630 };
631 
632 /**
633  * @brief @p LSM303DLHCDriver specific data.
634  */
635 #define _lsm303dlhc_data \
636  _base_sensor_data \
637  /* Driver state.*/ \
638  lsm303dlhc_state_t state; \
639  /* Current configuration data.*/ \
640  const LSM303DLHCConfig *config; \
641  /* Accelerometer subsystem axes number.*/ \
642  size_t accaxes; \
643  /* Accelerometer subsystem current sensitivity.*/ \
644  float accsensitivity[LSM303DLHC_ACC_NUMBER_OF_AXES]; \
645  /* Accelerometer subsystem current bias .*/ \
646  float accbias[LSM303DLHC_ACC_NUMBER_OF_AXES]; \
647  /* Accelerometer subsystem current full scale value.*/ \
648  float accfullscale; \
649  /* Compass subsystem axes number.*/ \
650  size_t compaxes; \
651  /* Compass subsystem current sensitivity.*/ \
652  float compsensitivity[LSM303DLHC_COMP_NUMBER_OF_AXES];\
653  /* Compass subsystem current bias.*/ \
654  float compbias[LSM303DLHC_COMP_NUMBER_OF_AXES]; \
655  /* Compass subsystem current full scale value.*/ \
656  float compfullscale;
657 
658 /**
659  * @brief LSM303DLHC 6-axis accelerometer/compass class.
660  */
662  /** @brief Virtual Methods Table.*/
663  const struct LSM303DLHCVMT *vmt;
664  /** @brief Base accelerometer interface.*/
666  /** @brief Base compass interface.*/
669 };
670 /** @} */
671 
672 /*===========================================================================*/
673 /* Driver macros. */
674 /*===========================================================================*/
675 
676 /**
677  * @brief Return the number of axes of the BaseAccelerometer.
678  *
679  * @param[in] devp pointer to @p LSM303DLHCDriver.
680  *
681  * @return the number of axes.
682  *
683  * @api
684  */
685 #define lsm303dlhcAccelerometerGetAxesNumber(devp) \
686  accelerometerGetAxesNumber(&((devp)->acc_if))
687 
688 /**
689  * @brief Retrieves raw data from the BaseAccelerometer.
690  * @note This data is retrieved from MEMS register without any algebraical
691  * manipulation.
692  * @note The axes array must be at least the same size of the
693  * BaseAccelerometer axes number.
694  *
695  * @param[in] devp pointer to @p LSM303DLHCDriver.
696  * @param[out] axes a buffer which would be filled with raw data.
697  *
698  * @return The operation status.
699  * @retval MSG_OK if the function succeeded.
700  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
701  * be retrieved using @p i2cGetErrors().
702  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
703  *
704  * @api
705  */
706 #define lsm303dlhcAccelerometerReadRaw(devp, axes) \
707  accelerometerReadRaw(&((devp)->acc_if), axes)
708 
709 /**
710  * @brief Retrieves cooked data from the BaseAccelerometer.
711  * @note This data is manipulated according to the formula
712  * cooked = (raw * sensitivity) - bias.
713  * @note Final data is expressed as milli-G.
714  * @note The axes array must be at least the same size of the
715  * BaseAccelerometer axes number.
716  *
717  * @param[in] devp pointer to @p LSM303DLHCDriver.
718  * @param[out] axes a buffer which would be filled with cooked data.
719  *
720  * @return The operation status.
721  * @retval MSG_OK if the function succeeded.
722  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
723  * be retrieved using @p i2cGetErrors().
724  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
725  *
726  * @api
727  */
728 #define lsm303dlhcAccelerometerReadCooked(devp, axes) \
729  accelerometerReadCooked(&((devp)->acc_if), axes)
730 
731 /**
732  * @brief Set bias values for the BaseAccelerometer.
733  * @note Bias must be expressed as milli-G.
734  * @note The bias buffer must be at least the same size of the
735  * BaseAccelerometer axes number.
736  *
737  * @param[in] devp pointer to @p LSM303DLHCDriver.
738  * @param[in] bp a buffer which contains biases.
739  *
740  * @return The operation status.
741  * @retval MSG_OK if the function succeeded.
742  *
743  * @api
744  */
745 #define lsm303dlhcAccelerometerSetBias(devp, bp) \
746  accelerometerSetBias(&((devp)->acc_if), bp)
747 
748 /**
749  * @brief Reset bias values for the BaseAccelerometer.
750  * @note Default biases value are obtained from device datasheet when
751  * available otherwise they are considered zero.
752  *
753  * @param[in] devp pointer to @p LSM303DLHCDriver.
754  *
755  * @return The operation status.
756  * @retval MSG_OK if the function succeeded.
757  *
758  * @api
759  */
760 #define lsm303dlhcAccelerometerResetBias(devp) \
761  accelerometerResetBias(&((devp)->acc_if))
762 
763 /**
764  * @brief Set sensitivity values for the BaseAccelerometer.
765  * @note Sensitivity must be expressed as milli-G/LSB.
766  * @note The sensitivity buffer must be at least the same size of the
767  * BaseAccelerometer axes number.
768  *
769  * @param[in] devp pointer to @p LSM303DLHCDriver.
770  * @param[in] sp a buffer which contains sensitivities.
771  *
772  * @return The operation status.
773  * @retval MSG_OK if the function succeeded.
774  *
775  * @api
776  */
777 #define lsm303dlhcAccelerometerSetSensitivity(devp, sp) \
778  accelerometerSetSensitivity(&((devp)->acc_if), sp)
779 
780 /**
781  * @brief Reset sensitivity values for the BaseAccelerometer.
782  * @note Default sensitivities value are obtained from device datasheet.
783  *
784  * @param[in] devp pointer to @p LSM303DLHCDriver.
785  *
786  * @return The operation status.
787  * @retval MSG_OK if the function succeeded.
788  * @retval MSG_RESET otherwise.
789  *
790  * @api
791  */
792 #define lsm303dlhcAccelerometerResetSensitivity(devp) \
793  accelerometerResetSensitivity(&((devp)->acc_if))
794 
795 /**
796  * @brief Changes the LSM303DLHCDriver accelerometer fullscale value.
797  * @note This function also rescale sensitivities and biases based on
798  * previous and next fullscale value.
799  * @note A recalibration is highly suggested after calling this function.
800  *
801  * @param[in] devp pointer to @p LSM303DLHCDriver.
802  * @param[in] fs new fullscale value.
803  *
804  * @return The operation status.
805  * @retval MSG_OK if the function succeeded.
806  * @retval MSG_RESET otherwise.
807  *
808  * @api
809  */
810 #define lsm303dlhcAccelerometerSetFullScale(devp, fs) \
811  (devp)->vmt->acc_set_full_scale(devp, fs)
812 
813 /**
814  * @brief Return the number of axes of the BaseCompass.
815  *
816  * @param[in] devp pointer to @p LSM303DLHCDriver.
817  *
818  * @return the number of axes.
819  *
820  * @api
821  */
822 #define lsm303dlhcCompassGetAxesNumber(devp) \
823  compassGetAxesNumber(&((devp)->comp_if))
824 
825 /**
826  * @brief Retrieves raw data from the BaseCompass.
827  * @note This data is retrieved from MEMS register without any algebraical
828  * manipulation.
829  * @note The axes array must be at least the same size of the
830  * BaseCompass axes number.
831  *
832  * @param[in] devp pointer to @p BaseCompass interface.
833  * @param[out] axes a buffer which would be filled with raw data.
834  *
835  * @return The operation status.
836  * @retval MSG_OK if the function succeeded.
837  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
838  * be retrieved using @p i2cGetErrors().
839  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
840  *
841  * @api
842  */
843 #define lsm303dlhcCompassReadRaw(devp, axes) \
844  compassReadRaw(&((devp)->comp_if), axes)
845 
846 /**
847  * @brief Retrieves cooked data from the BaseCompass.
848  * @note This data is manipulated according to the formula
849  * cooked = (raw * sensitivity) - bias.
850  * @note Final data is expressed as G.
851  * @note The axes array must be at least the same size of the
852  * BaseCompass axes number.
853  *
854  * @param[in] devp pointer to @p BaseCompass interface.
855  * @param[out] axes a buffer which would be filled with cooked data.
856  *
857  * @return The operation status.
858  * @retval MSG_OK if the function succeeded.
859  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
860  * be retrieved using @p i2cGetErrors().
861  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
862  *
863  * @api
864  */
865 #define lsm303dlhcCompassReadCooked(devp, axes) \
866  compassReadCooked(&((devp)->comp_if), axes)
867 
868 /**
869  * @brief Set bias values for the BaseCompass.
870  * @note Bias must be expressed as G.
871  * @note The bias buffer must be at least the same size of the
872  * BaseCompass axes number.
873  *
874  * @param[in] devp pointer to @p BaseCompass interface.
875  * @param[in] bp a buffer which contains biases.
876  *
877  * @return The operation status.
878  * @retval MSG_OK if the function succeeded.
879  *
880  * @api
881  */
882 #define lsm303dlhcCompassSetBias(devp, bp) \
883  compassSetBias(&((devp)->comp_if), bp)
884 
885 /**
886  * @brief Reset bias values for the BaseCompass.
887  * @note Default biases value are obtained from device datasheet when
888  * available otherwise they are considered zero.
889  *
890  * @param[in] devp pointer to @p LSM303DLHCDriver.
891  *
892  * @return The operation status.
893  * @retval MSG_OK if the function succeeded.
894  *
895  * @api
896  */
897 #define lsm303dlhcCompassResetBias(devp) \
898  compassResetBias(&((devp)->comp_if))
899 
900 /**
901  * @brief Set sensitivity values for the BaseCompass.
902  * @note Sensitivity must be expressed as G/LSB.
903  * @note The sensitivity buffer must be at least the same size of the
904  * BaseCompass axes number.
905  *
906  * @param[in] devp pointer to @p LSM303DLHCDriver.
907  * @param[in] sp a buffer which contains sensitivities.
908  *
909  * @return The operation status.
910  * @retval MSG_OK if the function succeeded.
911  *
912  * @api
913  */
914 #define lsm303dlhcCompassSetSensitivity(devp, sp) \
915  compassSetSensitivity(&((devp)->comp_if), sp)
916 
917 /**
918  * @brief Reset sensitivity values for the BaseCompass.
919  * @note Default sensitivities value are obtained from device datasheet.
920  *
921  * @param[in] devp pointer to @p LSM303DLHCDriver.
922  *
923  * @return The operation status.
924  * @retval MSG_OK if the function succeeded.
925  * @retval MSG_RESET otherwise.
926  *
927  * @api
928  */
929 #define lsm303dlhcCompassResetSensitivity(devp) \
930  compassResetSensitivity(&((devp)->comp_if))
931 
932 /**
933  * @brief Changes the LSM303DLHCDriver compass fullscale value.
934  * @note This function also rescale sensitivities and biases based on
935  * previous and next fullscale value.
936  * @note A recalibration is highly suggested after calling this function.
937  *
938  * @param[in] devp pointer to @p LSM303DLHCDriver.
939  * @param[in] fs new fullscale value.
940  *
941  * @return The operation status.
942  * @retval MSG_OK if the function succeeded.
943  * @retval MSG_RESET otherwise.
944  *
945  * @api
946  */
947 #define lsm303dlhcCompassSetFullScale(devp, fs) \
948  (devp)->vmt->comp_set_full_scale(devp, fs)
949 
950 /*===========================================================================*/
951 /* External declarations. */
952 /*===========================================================================*/
953 
954 #ifdef __cplusplus
955 extern "C" {
956 #endif
958  void lsm303dlhcStart(LSM303DLHCDriver *devp, const LSM303DLHCConfig *config);
959  void lsm303dlhcStop(LSM303DLHCDriver *devp);
960 #ifdef __cplusplus
961 }
962 #endif
963 
964 #endif /* _LSM303DLHC_H_ */
965 
966 /** @} */
#define _lsm303dlhc_methods
LSM303DLHC specific methods with inherited ones.
Definition: lsm303dlhc.h:619
lsm303dlhc_comp_md_t
LSM303DLHC compass subsystem working mode.
Definition: lsm303dlhc.h:516
lsm303dlhc_acc_lp_t acclowpower
LSM303DLHC accelerometer subsystem low power mode.
Definition: lsm303dlhc.h:567
float * compbias
LSM303DLHC compass initial bias.
Definition: lsm303dlhc.h:588
lsm303dlhc_acc_end_t
LSM303DLHC accelerometer endianness.
Definition: lsm303dlhc.h:477
lsm303dlhc_comp_md_t compmode
LSM303DLHC compass subsystem working mode.
Definition: lsm303dlhc.h:601
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:550
float * accbias
LSM303DLHC accelerometer subsystem initial bias.
Definition: lsm303dlhc.h:554
lsm303dlhc_comp_odr_t
LSM303DLHC compass subsystem output data rate.
Definition: lsm303dlhc.h:502
lsm303dlhc_comp_odr_t compoutputdatarate
LSM303DLHC compass subsystem output data rate.
Definition: lsm303dlhc.h:596
const I2CConfig * i2ccfg
I2C configuration associated to this LSM303DLHC.
Definition: lsm303dlhc.h:546
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:542
lsm303dlhc_acc_ae_t
LSM303DLHC accelerometer subsystem axes enabling.
Definition: lsm303dlhc.h:439
#define _lsm303dlhc_data
LSM303DLHCDriver specific data.
Definition: lsm303dlhc.h:635
lsm303dlhc_acc_odr_t
LSM303DLHC accelerometer subsystem output data rate.
Definition: lsm303dlhc.h:423
lsm303dlhc_acc_bdu_t accblockdataupdate
LSM303DLHC accelerometer subsystem block data update.
Definition: lsm303dlhc.h:575
lsm303dlhc_acc_odr_t accoutdatarate
LSM303DLHC accelerometer subsystem output data rate.
Definition: lsm303dlhc.h:562
BaseCompass comp_if
Base compass interface.
Definition: lsm303dlhc.h:667
BaseAccelerometer acc_if
Base accelerometer interface.
Definition: lsm303dlhc.h:665
Structure representing an I2C driver.
Definition: hal_i2c_lld.h:88
LSM303DLHC configuration structure.
Definition: lsm303dlhc.h:538
lsm303dlhc_acc_end_t accendianess
LSM303DLHC accelerometer endianness.
Definition: lsm303dlhc.h:579
Generic accelerometer interface header.
Generic compass interface header.
const struct LSM303DLHCVMT * vmt
Virtual Methods Table.
Definition: lsm303dlhc.h:663
lsm303dlhc_acc_bdu_t
LSM303DLHC accelerometer subsystem block data update.
Definition: lsm303dlhc.h:469
float * compsensitivity
LSM303DLHC compass initial sensitivity.
Definition: lsm303dlhc.h:584
lsm303dlhc_state_t
Driver state machine possible states.
Definition: lsm303dlhc.h:529
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:453
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:592
LSM303DLHC 6-axis accelerometer/compass class.
Definition: lsm303dlhc.h:661
lsm303dlhc_acc_fs_t
LSM303DLHC accelerometer subsystem full scale.
Definition: lsm303dlhc.h:413
LSM303DLHC virtual methods table.
Definition: lsm303dlhc.h:628
lsm303dlhc_acc_hr_t acchighresmode
LSM303DLHC accelerometer subsystem high resolution mode.
Definition: lsm303dlhc.h:571
lsm303dlhc_acc_fs_t accfullscale
LSM303DLHC accelerometer subsystem initial full scale.
Definition: lsm303dlhc.h:558
Base accelerometer class.
lsm303dlhc_comp_fs_t
LSM303DLHC compass subsystem full scale.
Definition: lsm303dlhc.h:489
lsm303dlhc_acc_hr_t
LSM303DLHC accelerometer subsystem high resolution mode.
Definition: lsm303dlhc.h:461