ChibiOS/EX  1.1.2
lsm303agr.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 lsm303agr.h
23  * @brief LSM303AGR MEMS interface module header.
24  *
25  * @addtogroup LSM303AGR
26  * @ingroup EX_ST
27  * @{
28  */
29 #ifndef _LSM303AGR_H_
30 #define _LSM303AGR_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 LSM303AGR driver version string.
45  */
46 #define EX_LSM303AGR_VERSION "1.0.1"
47 
48 /**
49  * @brief LSM303AGR driver version major number.
50  */
51 #define EX_LSM303AGR_MAJOR 1
52 
53 /**
54  * @brief LSM303AGR driver version minor number.
55  */
56 #define EX_LSM303AGR_MINOR 0
57 
58 /**
59  * @brief LSM303AGR driver version patch number.
60  */
61 #define EX_LSM303AGR_PATCH 1
62 /** @} */
63 
64 /**
65  * @brief LSM303AGR 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 LSM303AGR_ACC_NUMBER_OF_AXES 3U
73 
74 #define LSM303AGR_ACC_2G 2.0f
75 #define LSM303AGR_ACC_4G 4.0f
76 #define LSM303AGR_ACC_8G 8.0f
77 #define LSM303AGR_ACC_16G 16.0f
78 
79 #define LSM303AGR_ACC_SENS_2G 0.060f
80 #define LSM303AGR_ACC_SENS_4G 0.120f
81 #define LSM303AGR_ACC_SENS_8G 0.240f
82 #define LSM303AGR_ACC_SENS_16G 0.750f
83 
84 #define LSM303AGR_ACC_BIAS 0.0f
85 /** @} */
86 
87 /**
88  * @brief LSM303AGR 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 LSM303AGR_COMP_NUMBER_OF_AXES 3U
95 
96 #define LSM303AGR_COMP_50GA 50.0f
97 
98 #define LSM303AGR_COMP_SENS_50GA 0.00015f
99 
100 #define LSM303AGR_COMP_BIAS 0.0f
101 /** @} */
102 
103 /**
104  * @name LSM303AGR communication interfaces related bit masks
105  * @{
106  */
107 #define LSM303AGR_DI_MASK 0xFF
108 #define LSM303AGR_DI(n) (1 << n)
109 #define LSM303AGR_AD_MASK 0x7F
110 #define LSM303AGR_AD(n) (1 << n)
111 #define LSM303AGR_MS (1 << 7)
112 /** @} */
113 
114 /**
115  * @name LSM303AGR register addresses
116  * @{
117  */
118 #define LSM303AGR_AD_STATUS_REG_AUX_A 0x07
119 #define LSM303AGR_AD_OUT_TEMP_L_A 0x0C
120 #define LSM303AGR_AD_OUT_TEMP_H_A 0x0D
121 #define LSM303AGR_AD_INT_COUNTER_REG_A 0x0E
122 #define LSM303AGR_AD_WHO_AM_I_A 0x0F
123 #define LSM303AGR_AD_TEMP_CFG_REG_A 0x1F
124 #define LSM303AGR_AD_CTRL_REG1_A 0x20
125 #define LSM303AGR_AD_CTRL_REG2_A 0x21
126 #define LSM303AGR_AD_CTRL_REG3_A 0x22
127 #define LSM303AGR_AD_CTRL_REG4_A 0x23
128 #define LSM303AGR_AD_CTRL_REG5_A 0x24
129 #define LSM303AGR_AD_CTRL_REG6_A 0x25
130 #define LSM303AGR_AD_REFERENCE_A 0x26
131 #define LSM303AGR_AD_STATUS_REG_A 0x27
132 #define LSM303AGR_AD_OUT_X_L_A 0x28
133 #define LSM303AGR_AD_OUT_X_H_A 0x29
134 #define LSM303AGR_AD_OUT_Y_L_A 0x2A
135 #define LSM303AGR_AD_OUT_Y_H_A 0x2B
136 #define LSM303AGR_AD_OUT_Z_L_A 0x2C
137 #define LSM303AGR_AD_OUT_Z_H_A 0x2D
138 #define LSM303AGR_AD_FIFO_CTRL_REG_A 0x2E
139 #define LSM303AGR_AD_FIFO_SRC_REG_A 0x2F
140 #define LSM303AGR_AD_INT1_CFG_A 0x30
141 #define LSM303AGR_AD_INT1_SRC_A 0x31
142 #define LSM303AGR_AD_INT1_THS_A 0x32
143 #define LSM303AGR_AD_INT1_DURATION_A 0x33
144 #define LSM303AGR_AD_INT2_CFG_A 0x34
145 #define LSM303AGR_AD_INT2_SRC_A 0x35
146 #define LSM303AGR_AD_INT2_THS_A 0x36
147 #define LSM303AGR_AD_INT2_DURATION_A 0x37
148 #define LSM303AGR_AD_CLICK_CFG_A 0x38
149 #define LSM303AGR_AD_CLICK_SRC_A 0x39
150 #define LSM303AGR_AD_CLICK_THS_A 0x3A
151 #define LSM303AGR_AD_TIME_LIMIT_A 0x3B
152 #define LSM303AGR_AD_TIME_LATENCY_A 0x3C
153 #define LSM303AGR_AD_TIME_WINDOW_A 0x3D
154 #define LSM303AGR_AD_ACT_THS_A 0x3E
155 #define LSM303AGR_AD_ACT_DUR_A 0x3F
156 #define LSM303AGR_AD_OFFSET_X_REG_L_M 0x45
157 #define LSM303AGR_AD_OFFSET_X_REG_H_M 0x46
158 #define LSM303AGR_AD_OFFSET_Y_REG_L_M 0x47
159 #define LSM303AGR_AD_OFFSET_Y_REG_H_M 0x48
160 #define LSM303AGR_AD_OFFSET_Z_REG_L_M 0x49
161 #define LSM303AGR_AD_OFFSET_Z_REG_H_M 0x4A
162 #define LSM303AGR_AD_WHO_AM_I_M 0x4F
163 #define LSM303AGR_AD_CFG_REG_A_M 0x60
164 #define LSM303AGR_AD_CFG_REG_B_M 0x61
165 #define LSM303AGR_AD_CFG_REG_C_M 0x62
166 #define LSM303AGR_AD_INT_CRTL_REG_M 0x63
167 #define LSM303AGR_AD_INT_SOURCE_REG_M 0x64
168 #define LSM303AGR_AD_INT_THS_L_REG_M 0x65
169 #define LSM303AGR_AD_INT_THS_H_REG_M 0x66
170 #define LSM303AGR_AD_STATUS_REG_M 0x67
171 #define LSM303AGR_AD_OUTX_L_REG_M 0x68
172 #define LSM303AGR_AD_OUTX_H_REG_M 0x69
173 #define LSM303AGR_AD_OUTY_L_REG_M 0x6A
174 #define LSM303AGR_AD_OUTY_H_REG_M 0x6B
175 #define LSM303AGR_AD_OUTZ_L_REG_M 0x6C
176 #define LSM303AGR_AD_OUTZ_H_REG_M 0x6D
177 /** @} */
178 
179 /**
180  * @name LSM303AGR_TEMP_CFG_REG_A register bits definitions
181  * @{
182  */
183 #define LSM303AGR_TEMP_CFG_REG_A_TEMP_EN0 (1 << 0)
184 #define LSM303AGR_TEMP_CFG_REG_A_TEMP_EN1 (1 << 0)
185 /** @} */
186 
187 /**
188  * @name LSM303AGR_CTRL_REG1_A register bits definitions
189  * @{
190  */
191 #define LSM303AGR_CTRL_REG1_A_XEN (1 << 0)
192 #define LSM303AGR_CTRL_REG1_A_YEN (1 << 1)
193 #define LSM303AGR_CTRL_REG1_A_ZEN (1 << 2)
194 #define LSM303AGR_CTRL_REG1_A_LPEN (1 << 3)
195 #define LSM303AGR_CTRL_REG1_A_ODR0 (1 << 4)
196 #define LSM303AGR_CTRL_REG1_A_ODR1 (1 << 5)
197 #define LSM303AGR_CTRL_REG1_A_ODR2 (1 << 6)
198 #define LSM303AGR_CTRL_REG1_A_ODR3 (1 << 7)
199 /** @} */
200 
201 /**
202  * @name LSM303AGR_CTRL_REG2_A register bits definitions
203  * @{
204  */
205 #define LSM303AGR_CTRL_REG2_A_HPIS1 (1 << 0)
206 #define LSM303AGR_CTRL_REG2_A_HPIS2 (1 << 1)
207 #define LSM303AGR_CTRL_REG2_A_HPCLICK (1 << 2)
208 #define LSM303AGR_CTRL_REG2_A_FDS (1 << 3)
209 #define LSM303AGR_CTRL_REG2_A_HPCF1 (1 << 4)
210 #define LSM303AGR_CTRL_REG2_A_HPCF2 (1 << 5)
211 #define LSM303AGR_CTRL_REG2_A_HPM0 (1 << 6)
212 #define LSM303AGR_CTRL_REG2_A_HPM1 (1 << 7)
213 /** @} */
214 
215 /**
216  * @name LSM303AGR_CTRL_REG3_A register bits definitions
217  * @{
218  */
219 #define LSM303AGR_CTRL_REG3_A_I1_OVERRUN (1 << 1)
220 #define LSM303AGR_CTRL_REG3_A_I1_WTM (1 << 2)
221 #define LSM303AGR_CTRL_REG3_A_I1_DRDY2 (1 << 3)
222 #define LSM303AGR_CTRL_REG3_A_I1_DRDY1 (1 << 4)
223 #define LSM303AGR_CTRL_REG3_A_I1_AOI2 (1 << 5)
224 #define LSM303AGR_CTRL_REG3_A_I1_AOI1 (1 << 6)
225 #define LSM303AGR_CTRL_REG3_A_I1_CLICK (1 << 7)
226 /** @} */
227 
228 /**
229  * @name LSM303AGR_CTRL_REG4_A register bits definitions
230  * @{
231  */
232 #define LSM303AGR_CTRL_REG4_A_SPI_ENABLE (1 << 0)
233 #define LSM303AGR_CTRL_REG4_A_ST0 (1 << 1)
234 #define LSM303AGR_CTRL_REG4_A_ST1 (1 << 2)
235 #define LSM303AGR_CTRL_REG4_A_HR (1 << 3)
236 #define LSM303AGR_CTRL_REG4_A_FS_MASK 0x30
237 #define LSM303AGR_CTRL_REG4_A_FS0 (1 << 4)
238 #define LSM303AGR_CTRL_REG4_A_FS1 (1 << 5)
239 #define LSM303AGR_CTRL_REG4_A_BLE (1 << 6)
240 #define LSM303AGR_CTRL_REG4_A_BDU (1 << 7)
241 /** @} */
242 
243 /**
244  * @name LSM303AGR_CTRL_REG5_A register bits definitions
245  * @{
246  */
247 #define LSM303AGR_CTRL_REG5_A_D4D_INT2 (1 << 0)
248 #define LSM303AGR_CTRL_REG5_A_LIR_INT2 (1 << 1)
249 #define LSM303AGR_CTRL_REG5_A_D4D_INT1 (1 << 2)
250 #define LSM303AGR_CTRL_REG5_A_LIR_INT1 (1 << 3)
251 #define LSM303AGR_CTRL_REG5_A_FIFO_EN (1 << 6)
252 #define LSM303AGR_CTRL_REG5_A_BOOT (1 << 7)
253 /** @} */
254 
255 /**
256  * @name LSM303AGR_CTRL_REG6_A register bits definitions
257  * @{
258  */
259 #define LSM303AGR_CTRL_REG6_A_H_LACTIVE (1 << 1)
260 #define LSM303AGR_CTRL_REG6_A_P2_ACT (1 << 3)
261 #define LSM303AGR_CTRL_REG6_A_BOOT_I2 (1 << 4)
262 #define LSM303AGR_CTRL_REG6_A_I2_INT2 (1 << 5)
263 #define LSM303AGR_CTRL_REG6_A_I2_INT1 (1 << 6)
264 #define LSM303AGR_CTRL_REG6_A_I2_CLICKEN (1 << 7)
265 /** @} */
266 
267 /**
268  * @name LSM303AGR_CFG_REG_A register bits definitions
269  * @{
270  */
271 #define LSM303AGR_CFG_REG_A_M_MD0 (1 << 0)
272 #define LSM303AGR_CFG_REG_A_M_MD1 (1 << 1)
273 #define LSM303AGR_CFG_REG_A_M_ODR0 (1 << 2)
274 #define LSM303AGR_CFG_REG_A_M_ODR1 (1 << 3)
275 #define LSM303AGR_CFG_REG_A_M_LP (1 << 4)
276 #define LSM303AGR_CFG_REG_A_M_SOFT_RST (1 << 5)
277 #define LSM303AGR_CFG_REG_A_M_REBOOT (1 << 6)
278 #define LSM303AGR_CFG_REG_A_M_COMP_TEMP_EN (1 << 7)
279 /** @} */
280 
281 /**
282  * @name LSM303AGR_CFG_REG_B register bits definitions
283  * @{
284  */
285 #define LSM303AGR_CFG_REG_B_M_LPF (1 << 0)
286 #define LSM303AGR_CFG_REG_B_M_OFF_CANC (1 << 1)
287 #define LSM303AGR_CFG_REG_B_M_SET_FREQ (1 << 2)
288 #define LSM303AGR_CFG_REG_B_M_INT_ON (1 << 3)
289 #define LSM303AGR_CFG_REG_B_M_OFF_CANC_OS (1 << 4)
290 /** @} */
291 
292 /**
293  * @name LSM303AGR_CFG_REG_C register bits definitions
294  * @{
295  */
296 #define LSM303AGR_CFG_REG_C_M_INT_MAG (1 << 0)
297 #define LSM303AGR_CFG_REG_C_M_SELF_TEST (1 << 1)
298 #define LSM303AGR_CFG_REG_C_M_BLE (1 << 3)
299 #define LSM303AGR_CFG_REG_C_M_BDU (1 << 4)
300 #define LSM303AGR_CFG_REG_C_M_I2C_DIS (1 << 5)
301 #define LSM303AGR_CFG_REG_C_M_INT_MAG_PIN (1 << 6)
302 /** @} */
303 
304 /*===========================================================================*/
305 /* Driver pre-compile time settings. */
306 /*===========================================================================*/
307 
308 /**
309  * @name Configuration options
310  * @{
311  */
312 /**
313  * @brief LSM303AGR SPI interface switch.
314  * @details If set to @p TRUE the support for SPI is included.
315  * @note The default is @p FALSE.
316  */
317 #if !defined(LSM303AGR_USE_SPI) || defined(__DOXYGEN__)
318 #define LSM303AGR_USE_SPI FALSE
319 #endif
320 
321 /**
322  * @brief LSM303AGR shared SPI switch.
323  * @details If set to @p TRUE the device acquires SPI bus ownership
324  * on each transaction.
325  * @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION.
326  */
327 #if !defined(LSM303AGR_SHARED_SPI) || defined(__DOXYGEN__)
328 #define LSM303AGR_SHARED_SPI FALSE
329 #endif
330 
331 /**
332  * @brief LSM303AGR I2C interface switch.
333  * @details If set to @p TRUE the support for I2C is included.
334  * @note The default is @p TRUE.
335  */
336 #if !defined(LSM303AGR_USE_I2C) || defined(__DOXYGEN__)
337 #define LSM303AGR_USE_I2C TRUE
338 #endif
339 
340 /**
341  * @brief LSM303AGR shared I2C switch.
342  * @details If set to @p TRUE the device acquires I2C bus ownership
343  * on each transaction.
344  * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
345  */
346 #if !defined(LSM303AGR_SHARED_I2C) || defined(__DOXYGEN__)
347 #define LSM303AGR_SHARED_I2C FALSE
348 #endif
349 
350 /**
351  * @brief LSM303AGR advanced configurations switch.
352  * @details If set to @p TRUE more configurations are available.
353  * @note The default is @p FALSE.
354  */
355 #if !defined(LSM303AGR_USE_ADVANCED) || defined(__DOXYGEN__)
356 #define LSM303AGR_USE_ADVANCED FALSE
357 #endif
358 /** @} */
359 
360 /*===========================================================================*/
361 /* Derived constants and error checks. */
362 /*===========================================================================*/
363 
364 #if !(LSM303AGR_USE_SPI ^ LSM303AGR_USE_I2C)
365 #error "LSM303AGR_USE_SPI and LSM303AGR_USE_I2C cannot be both true or both false"
366 #endif
367 
368 #if LSM303AGR_USE_SPI && !HAL_USE_SPI
369 #error "LSM303AGR_USE_SPI requires HAL_USE_SPI"
370 #endif
371 
372 #if LSM303AGR_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
373 #error "LSM303AGR_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
374 #endif
375 
376 #if LSM303AGR_USE_I2C && !HAL_USE_I2C
377 #error "LSM303AGR_USE_I2C requires HAL_USE_I2C"
378 #endif
379 
380 #if LSM303AGR_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
381 #error "LSM303AGR_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
382 #endif
383 
384 /**
385  * @todo Add support for LSM303AGR over SPI.
386  */
387 #if LSM303AGR_USE_SPI
388 #error "LSM303AGR over SPI still not supported"
389 #endif
390 
391 /*===========================================================================*/
392 /* Driver data structures and types. */
393 /*===========================================================================*/
394 
395 /**
396  * @name LSM303AGR accelerometer subsystem data structures and types.
397  * @{
398  */
399 /**
400  * @brief Structure representing a LSM303AGR driver.
401  */
403 
404 /**
405  * @brief LSM303AGR accelerometer subsystem full scale.
406  */
407 typedef enum {
408  LSM303AGR_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
409  LSM303AGR_ACC_FS_4G = 0x10, /**< Full scale ±4g. */
410  LSM303AGR_ACC_FS_8G = 0x20, /**< Full scale ±8g. */
411  LSM303AGR_ACC_FS_16G = 0x30 /**< Full scale ±16g. */
413 
414 /**
415  * @brief LSM303AGR accelerometer subsystem output data rate.
416  */
417 typedef enum {
418  LSM303AGR_ACC_ODR_PD = 0x00, /**< Power down */
419  LSM303AGR_ACC_ODR_1Hz = 0x10, /**< ODR 1 Hz */
420  LSM303AGR_ACC_ODR_10Hz = 0x20, /**< ODR 10 Hz */
421  LSM303AGR_ACC_ODR_25Hz = 0x30, /**< ODR 25 Hz */
422  LSM303AGR_ACC_ODR_50Hz = 0x40, /**< ODR 50 Hz */
423  LSM303AGR_ACC_ODR_100Hz = 0x50, /**< ODR 100 Hz */
424  LSM303AGR_ACC_ODR_200Hz = 0x60, /**< ODR 200 Hz */
425  LSM303AGR_ACC_ODR_400Hz = 0x70, /**< ODR 400 Hz */
426  LSM303AGR_ACC_ODR_1620Hz = 0x80, /**< ODR 1620 Hz (LP only) */
427  LSM303AGR_ACC_ODR_1344Hz = 0x90 /**< ODR 1344 Hz or 5376 Hz in LP */
429 
430 /**
431  * @brief LSM303AGR accelerometer subsystem axes enabling.
432  */
433 typedef enum {
434  LSM303AGR_ACC_AE_DISABLED = 0x00,/**< All axes disabled. */
435  LSM303AGR_ACC_AE_X = 0x01, /**< Only X-axis enabled. */
436  LSM303AGR_ACC_AE_Y = 0x02, /**< Only Y-axis enabled. */
437  LSM303AGR_ACC_AE_XY = 0x03, /**< X and Y axes enabled. */
438  LSM303AGR_ACC_AE_Z = 0x04, /**< Only Z-axis enabled. */
439  LSM303AGR_ACC_AE_XZ = 0x05, /**< X and Z axes enabled. */
440  LSM303AGR_ACC_AE_YZ = 0x06, /**< Y and Z axes enabled. */
441  LSM303AGR_ACC_AE_XYZ = 0x07 /**< All axes enabled. */
443 
444 /**
445  * @brief LSM303AGR accelerometer subsystem operation mode.
446  */
447 typedef enum {
448  LSM303AGR_ACC_MODE_NORM = 0, /**< Normal mode. */
449  LSM303AGR_ACC_MODE_LPOW = 1, /**< Low power mode. */
450  LSM303AGR_ACC_MODE_HRES = 2 /**< High resolution mode. */
452 
453 /**
454  * @brief LSM303AGR accelerometer subsystem block data update.
455  */
456 typedef enum {
457  LSM303AGR_ACC_BDU_CONT = 0x00, /**< Continuous update */
458  LSM303AGR_ACC_BDU_BLOCK = 0x80 /**< Update blocked */
460 
461 /**
462  * @brief LSM303AGR accelerometer endianness.
463  */
464 typedef enum {
465  LSM303AGR_ACC_END_LITTLE = 0x00, /**< Little Endian */
466  LSM303AGR_ACC_END_BIG = 0x40 /**< Big Endian */
468 
469 /**
470  * @name LSM303AGR compass subsystem data structures and types.
471  * @{
472  */
473 /**
474  * @brief LSM303AGR compass subsystem output data rate.
475  */
476 typedef enum {
477  LSM303AGR_COMP_ODR_10HZ = 0x00, /**< ODR 10 Hz */
478  LSM303AGR_COMP_ODR_20HZ = 0x04, /**< ODR 20 Hz */
479  LSM303AGR_COMP_ODR_50HZ = 0x08, /**< ODR 50 Hz */
480  LSM303AGR_COMP_ODR_100HZ = 0x0C /**< ODR 100 Hz */
482 
483 /**
484  * @brief LSM303AGR compass subsystem working mode.
485  */
486 typedef enum {
487  LSM303AGR_COMP_MODE_NORM = 0x00, /**< Continuous-Conversion Mode */
488  LSM303AGR_COMP_MODE_SINGLE = 0x01,/**< Single-Conversion Mode */
489  LSM303AGR_COMP_MODE_IDLE = 0x02 /**< Sleep Mode */
491 
492 /**
493  * @brief LSM303AGR compass subsystem working mode.
494  */
495 typedef enum {
496  LSM303AGR_COMP_LPOW_DIS = 0x00, /**< High Resolution Mode */
497  LSM303AGR_COMP_LPOW_EN = 0x10 /**< Low Power Mode */
499 
500 /**
501  * @name LSM303AGR main system data structures and types.
502  * @{
503  */
504 /**
505  * @brief Driver state machine possible states.
506  */
507 typedef enum {
508  LSM303AGR_UNINIT = 0, /**< Not initialized. */
509  LSM303AGR_STOP = 1, /**< Stopped. */
510  LSM303AGR_READY = 2, /**< Ready. */
512 
513 /**
514  * @brief LSM303AGR configuration structure.
515  */
516 typedef struct {
517  /**
518  * @brief I2C driver associated to this LSM303AGR.
519  */
520  I2CDriver *i2cp;
521  /**
522  * @brief I2C configuration associated to this LSM303AGR.
523  */
524  const I2CConfig *i2ccfg;
525  /**
526  * @brief LSM303AGR accelerometer subsystem initial sensitivity.
527  */
529  /**
530  * @brief LSM303AGR accelerometer subsystem initial bias.
531  */
532  float *accbias;
533  /**
534  * @brief LSM303AGR accelerometer subsystem initial full scale.
535  */
537  /**
538  * @brief LSM303AGR accelerometer subsystem output data rate.
539  */
541 #if LSM303AGR_USE_ADVANCED || defined(__DOXYGEN__)
542  /**
543  * @brief LSM303AGR accelerometer subsystem mode.
544  */
546  /**
547  * @brief LSM303AGR accelerometer subsystem block data update.
548  */
550  /**
551  * @brief LSM303AGR accelerometer endianness.
552  */
554 #endif
555  /**
556  * @brief LSM303AGR compass initial sensitivity.
557  */
559  /**
560  * @brief LSM303AGR compass initial bias.
561  */
562  float *compbias;
563  /**
564  * @brief LSM303AGR compass subsystem output data rate.
565  */
567 #if LSM303AGR_USE_ADVANCED || defined(__DOXYGEN__)
568  /**
569  * @brief LSM303AGR compass subsystem working mode.
570  */
572  /**
573  * @brief LSM303AGR compass subsystem lowpower mode.
574  */
576 #endif
578 
579 /**
580  * @brief @p LSM303AGR specific methods.
581  */
582 #define _lsm303agr_methods_alone \
583  /* Change full scale value of LSM303AGR accelerometer subsystem.*/ \
584  msg_t (*acc_set_full_scale)(LSM303AGRDriver *devp, \
585  lsm303agr_acc_fs_t fs);
586 
587 /**
588  * @brief @p LSM303AGR specific methods with inherited ones.
589  */
590 #define _lsm303agr_methods \
591  _base_object_methods \
592  _lsm303agr_methods_alone
593 
594 /**
595  * @extends BaseObjectVMT
596  *
597  * @brief @p LSM303AGR virtual methods table.
598  */
599 struct LSM303AGRVMT {
601 };
602 
603 /**
604  * @brief @p LSM303AGRDriver specific data.
605  */
606 #define _lsm303agr_data \
607  _base_sensor_data \
608  /* Driver state.*/ \
609  lsm303agr_state_t state; \
610  /* Current configuration data.*/ \
611  const LSM303AGRConfig *config; \
612  /* Accelerometer subsystem axes number.*/ \
613  size_t accaxes; \
614  /* Accelerometer subsystem current sensitivity.*/ \
615  float accsensitivity[LSM303AGR_ACC_NUMBER_OF_AXES]; \
616  /* Accelerometer subsystem current bias .*/ \
617  float accbias[LSM303AGR_ACC_NUMBER_OF_AXES]; \
618  /* Accelerometer subsystem current full scale value.*/ \
619  float accfullscale; \
620  /* Compass subsystem axes number.*/ \
621  size_t compaxes; \
622  /* Compass subsystem current sensitivity.*/ \
623  float compsensitivity[LSM303AGR_COMP_NUMBER_OF_AXES]; \
624  /* Compass subsystem current bias.*/ \
625  float compbias[LSM303AGR_COMP_NUMBER_OF_AXES]; \
626  /* Compass subsystem current full scale value.*/ \
627  float compfullscale;
628 
629 /**
630  * @brief LSM303AGR 6-axis accelerometer/compass class.
631  */
633  /** @brief Virtual Methods Table.*/
634  const struct LSM303AGRVMT *vmt;
635  /** @brief Base accelerometer interface.*/
637  /** @brief Base compass interface.*/
640 };
641 /** @} */
642 
643 /*===========================================================================*/
644 /* Driver macros. */
645 /*===========================================================================*/
646 
647 /**
648  * @brief Return the number of axes of the BaseAccelerometer.
649  *
650  * @param[in] devp pointer to @p LSM303AGRDriver.
651  *
652  * @return the number of axes.
653  *
654  * @api
655  */
656 #define lsm303agrAccelerometerGetAxesNumber(devp) \
657  accelerometerGetAxesNumber(&((devp)->acc_if))
658 
659 /**
660  * @brief Retrieves raw data from the BaseAccelerometer.
661  * @note This data is retrieved from MEMS register without any algebraical
662  * manipulation.
663  * @note The axes array must be at least the same size of the
664  * BaseAccelerometer axes number.
665  *
666  * @param[in] devp pointer to @p LSM303AGRDriver.
667  * @param[out] axes a buffer which would be filled with raw data.
668  *
669  * @return The operation status.
670  * @retval MSG_OK if the function succeeded.
671  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
672  * be retrieved using @p i2cGetErrors().
673  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
674  *
675  * @api
676  */
677 #define lsm303agrAccelerometerReadRaw(devp, axes) \
678  accelerometerReadRaw(&((devp)->acc_if), axes)
679 
680 /**
681  * @brief Retrieves cooked data from the BaseAccelerometer.
682  * @note This data is manipulated according to the formula
683  * cooked = (raw * sensitivity) - bias.
684  * @note Final data is expressed as milli-G.
685  * @note The axes array must be at least the same size of the
686  * BaseAccelerometer axes number.
687  *
688  * @param[in] devp pointer to @p LSM303AGRDriver.
689  * @param[out] axes a buffer which would be filled with cooked data.
690  *
691  * @return The operation status.
692  * @retval MSG_OK if the function succeeded.
693  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
694  * be retrieved using @p i2cGetErrors().
695  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
696  *
697  * @api
698  */
699 #define lsm303agrAccelerometerReadCooked(devp, axes) \
700  accelerometerReadCooked(&((devp)->acc_if), axes)
701 
702 /**
703  * @brief Set bias values for the BaseAccelerometer.
704  * @note Bias must be expressed as milli-G.
705  * @note The bias buffer must be at least the same size of the
706  * BaseAccelerometer axes number.
707  *
708  * @param[in] devp pointer to @p LSM303AGRDriver.
709  * @param[in] bp a buffer which contains biases.
710  *
711  * @return The operation status.
712  * @retval MSG_OK if the function succeeded.
713  *
714  * @api
715  */
716 #define lsm303agrAccelerometerSetBias(devp, bp) \
717  accelerometerSetBias(&((devp)->acc_if), bp)
718 
719 /**
720  * @brief Reset bias values for the BaseAccelerometer.
721  * @note Default biases value are obtained from device datasheet when
722  * available otherwise they are considered zero.
723  *
724  * @param[in] devp pointer to @p LSM303AGRDriver.
725  *
726  * @return The operation status.
727  * @retval MSG_OK if the function succeeded.
728  *
729  * @api
730  */
731 #define lsm303agrAccelerometerResetBias(devp) \
732  accelerometerResetBias(&((devp)->acc_if))
733 
734 /**
735  * @brief Set sensitivity values for the BaseAccelerometer.
736  * @note Sensitivity must be expressed as milli-G/LSB.
737  * @note The sensitivity buffer must be at least the same size of the
738  * BaseAccelerometer axes number.
739  *
740  * @param[in] devp pointer to @p LSM303AGRDriver.
741  * @param[in] sp a buffer which contains sensitivities.
742  *
743  * @return The operation status.
744  * @retval MSG_OK if the function succeeded.
745  *
746  * @api
747  */
748 #define lsm303agrAccelerometerSetSensitivity(devp, sp) \
749  accelerometerSetSensitivity(&((devp)->acc_if), sp)
750 
751 /**
752  * @brief Reset sensitivity values for the BaseAccelerometer.
753  * @note Default sensitivities value are obtained from device datasheet.
754  *
755  * @param[in] devp pointer to @p LSM303AGRDriver.
756  *
757  * @return The operation status.
758  * @retval MSG_OK if the function succeeded.
759  * @retval MSG_RESET otherwise.
760  *
761  * @api
762  */
763 #define lsm303agrAccelerometerResetSensitivity(devp) \
764  accelerometerResetSensitivity(&((devp)->acc_if))
765 
766 /**
767  * @brief Changes the LSM303AGRDriver accelerometer fullscale value.
768  * @note This function also rescale sensitivities and biases based on
769  * previous and next fullscale value.
770  * @note A recalibration is highly suggested after calling this function.
771  *
772  * @param[in] devp pointer to @p LSM303AGRDriver.
773  * @param[in] fs new fullscale value.
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 lsm303agrAccelerometerSetFullScale(devp, fs) \
782  (devp)->vmt->acc_set_full_scale(devp, fs)
783 
784 /**
785  * @brief Return the number of axes of the BaseCompass.
786  *
787  * @param[in] devp pointer to @p LSM303AGRDriver.
788  *
789  * @return the number of axes.
790  *
791  * @api
792  */
793 #define lsm303agrCompassGetAxesNumber(devp) \
794  compassGetAxesNumber(&((devp)->comp_if))
795 
796 /**
797  * @brief Retrieves raw data from the BaseCompass.
798  * @note This data is retrieved from MEMS register without any algebraical
799  * manipulation.
800  * @note The axes array must be at least the same size of the
801  * BaseCompass axes number.
802  *
803  * @param[in] devp pointer to @p BaseCompass interface.
804  * @param[out] axes a buffer which would be filled with raw data.
805  *
806  * @return The operation status.
807  * @retval MSG_OK if the function succeeded.
808  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
809  * be retrieved using @p i2cGetErrors().
810  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
811  *
812  * @api
813  */
814 #define lsm303agrCompassReadRaw(devp, axes) \
815  compassReadRaw(&((devp)->comp_if), axes)
816 
817 /**
818  * @brief Retrieves cooked data from the BaseCompass.
819  * @note This data is manipulated according to the formula
820  * cooked = (raw * sensitivity) - bias.
821  * @note Final data is expressed as G.
822  * @note The axes array must be at least the same size of the
823  * BaseCompass axes number.
824  *
825  * @param[in] devp pointer to @p BaseCompass interface.
826  * @param[out] axes a buffer which would be filled with cooked data.
827  *
828  * @return The operation status.
829  * @retval MSG_OK if the function succeeded.
830  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
831  * be retrieved using @p i2cGetErrors().
832  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
833  *
834  * @api
835  */
836 #define lsm303agrCompassReadCooked(devp, axes) \
837  compassReadCooked(&((devp)->comp_if), axes)
838 
839 /**
840  * @brief Set bias values for the BaseCompass.
841  * @note Bias must be expressed as G.
842  * @note The bias buffer must be at least the same size of the
843  * BaseCompass axes number.
844  *
845  * @param[in] devp pointer to @p BaseCompass interface.
846  * @param[in] bp a buffer which contains biases.
847  *
848  * @return The operation status.
849  * @retval MSG_OK if the function succeeded.
850  *
851  * @api
852  */
853 #define lsm303agrCompassSetBias(devp, bp) \
854  compassSetBias(&((devp)->comp_if), bp)
855 
856 /**
857  * @brief Reset bias values for the BaseCompass.
858  * @note Default biases value are obtained from device datasheet when
859  * available otherwise they are considered zero.
860  *
861  * @param[in] devp pointer to @p LSM303AGRDriver.
862  *
863  * @return The operation status.
864  * @retval MSG_OK if the function succeeded.
865  *
866  * @api
867  */
868 #define lsm303agrCompassResetBias(devp) \
869  compassResetBias(&((devp)->comp_if))
870 
871 /**
872  * @brief Set sensitivity values for the BaseCompass.
873  * @note Sensitivity must be expressed as G/LSB.
874  * @note The sensitivity buffer must be at least the same size of the
875  * BaseCompass axes number.
876  *
877  * @param[in] devp pointer to @p LSM303AGRDriver.
878  * @param[in] sp a buffer which contains sensitivities.
879  *
880  * @return The operation status.
881  * @retval MSG_OK if the function succeeded.
882  *
883  * @api
884  */
885 #define lsm303agrCompassSetSensitivity(devp, sp) \
886  compassSetSensitivity(&((devp)->comp_if), sp)
887 
888 /**
889  * @brief Reset sensitivity values for the BaseCompass.
890  * @note Default sensitivities value are obtained from device datasheet.
891  *
892  * @param[in] devp pointer to @p LSM303AGRDriver.
893  *
894  * @return The operation status.
895  * @retval MSG_OK if the function succeeded.
896  * @retval MSG_RESET otherwise.
897  *
898  * @api
899  */
900 #define lsm303agrCompassResetSensitivity(devp) \
901  compassResetSensitivity(&((devp)->comp_if))
902 
903 /*===========================================================================*/
904 /* External declarations. */
905 /*===========================================================================*/
906 
907 #ifdef __cplusplus
908 extern "C" {
909 #endif
911  void lsm303agrStart(LSM303AGRDriver *devp, const LSM303AGRConfig *config);
912  void lsm303agrStop(LSM303AGRDriver *devp);
913 #ifdef __cplusplus
914 }
915 #endif
916 
917 #endif /* _LSM303AGR_H_ */
918 
919 /** @} */
void lsm303agrStop(LSM303AGRDriver *devp)
Deactivates the LSM303AGR Complex Driver peripheral.
Definition: lsm303agr.c:873
const struct LSM303AGRVMT * vmt
Virtual Methods Table.
Definition: lsm303agr.h:634
lsm303agr_acc_end_t
LSM303AGR accelerometer endianness.
Definition: lsm303agr.h:464
lsm303agr_comp_mode_t compmode
LSM303AGR compass subsystem working mode.
Definition: lsm303agr.h:571
float * compbias
LSM303AGR compass initial bias.
Definition: lsm303agr.h:562
void lsm303agrObjectInit(LSM303AGRDriver *devp)
Initializes an instance.
Definition: lsm303agr.c:690
lsm303agr_acc_mode_t
LSM303AGR accelerometer subsystem operation mode.
Definition: lsm303agr.h:447
Base compass class.
Definition: hal_compass.h:86
lsm303agr_comp_lpow_t complp
LSM303AGR compass subsystem lowpower mode.
Definition: lsm303agr.h:575
lsm303agr_comp_odr_t compoutputdatarate
LSM303AGR compass subsystem output data rate.
Definition: lsm303agr.h:566
float * accbias
LSM303AGR accelerometer subsystem initial bias.
Definition: lsm303agr.h:532
lsm303agr_acc_bdu_t accblockdataupdate
LSM303AGR accelerometer subsystem block data update.
Definition: lsm303agr.h:549
float * compsensitivity
LSM303AGR compass initial sensitivity.
Definition: lsm303agr.h:558
lsm303agr_acc_odr_t
LSM303AGR accelerometer subsystem output data rate.
Definition: lsm303agr.h:417
LSM303AGR 6-axis accelerometer/compass class.
Definition: lsm303agr.h:632
lsm303agr_comp_lpow_t
LSM303AGR compass subsystem working mode.
Definition: lsm303agr.h:495
#define _lsm303agr_methods
LSM303AGR specific methods with inherited ones.
Definition: lsm303agr.h:590
lsm303agr_comp_odr_t
LSM303AGR compass subsystem output data rate.
Definition: lsm303agr.h:476
lsm303agr_comp_mode_t
LSM303AGR compass subsystem working mode.
Definition: lsm303agr.h:486
lsm303agr_acc_ae_t
LSM303AGR accelerometer subsystem axes enabling.
Definition: lsm303agr.h:433
BaseAccelerometer acc_if
Base accelerometer interface.
Definition: lsm303agr.h:636
const I2CConfig * i2ccfg
I2C configuration associated to this LSM303AGR.
Definition: lsm303agr.h:524
LSM303AGR virtual methods table.
Definition: lsm303agr.h:599
lsm303agr_acc_mode_t accmode
LSM303AGR accelerometer subsystem mode.
Definition: lsm303agr.h:545
lsm303agr_acc_odr_t accoutdatarate
LSM303AGR accelerometer subsystem output data rate.
Definition: lsm303agr.h:540
Generic accelerometer interface header.
lsm303agr_acc_fs_t
LSM303AGR accelerometer subsystem full scale.
Definition: lsm303agr.h:407
lsm303agr_acc_bdu_t
LSM303AGR accelerometer subsystem block data update.
Definition: lsm303agr.h:456
float * accsensitivity
LSM303AGR accelerometer subsystem initial sensitivity.
Definition: lsm303agr.h:528
lsm303agr_acc_end_t accendianess
LSM303AGR accelerometer endianness.
Definition: lsm303agr.h:553
Generic compass interface header.
lsm303agr_state_t
Driver state machine possible states.
Definition: lsm303agr.h:507
lsm303agr_acc_fs_t accfullscale
LSM303AGR accelerometer subsystem initial full scale.
Definition: lsm303agr.h:536
LSM303AGR configuration structure.
Definition: lsm303agr.h:516
void lsm303agrStart(LSM303AGRDriver *devp, const LSM303AGRConfig *config)
Configures and activates LSM303AGR Complex Driver peripheral.
Definition: lsm303agr.c:711
#define _lsm303agr_data
LSM303AGRDriver specific data.
Definition: lsm303agr.h:606
BaseCompass comp_if
Base compass interface.
Definition: lsm303agr.h:638
Base accelerometer class.
I2CDriver * i2cp
I2C driver associated to this LSM303AGR.
Definition: lsm303agr.h:520