ChibiOS  19.1.4
lis302dl.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 lis302dl.h
23  * @brief LIS302DL MEMS interface module header.
24  *
25  * @addtogroup LIS302DL
26  * @ingroup EX_ST
27  * @{
28  */
29 
30 #ifndef _LIS302DL_H_
31 #define _LIS302DL_H_
32 
33 #include "hal_accelerometer.h"
34 
35 /*===========================================================================*/
36 /* Driver constants. */
37 /*===========================================================================*/
38 
39 /**
40  * @name Version identification
41  * @{
42  */
43 /**
44  * @brief LIS302DL driver version string.
45  */
46 #define EX_LIS302DL_VERSION "1.1.1"
47 
48 /**
49  * @brief LIS302DL driver version major number.
50  */
51 #define EX_LIS302DL_MAJOR 1
52 
53 /**
54  * @brief LIS302DL driver version minor number.
55  */
56 #define EX_LIS302DL_MINOR 1
57 
58 /**
59  * @brief LIS302DL driver version patch number.
60  */
61 #define EX_LIS302DL_PATCH 1
62 /** @} */
63 
64 /**
65  * @brief LIS302DL 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 LIS302DL_ACC_NUMBER_OF_AXES 3U
73 
74 #define LIS302DL_ACC_2G 2.0f
75 #define LIS302DL_ACC_8G 8.0f
76 
77 #define LIS302DL_ACC_SENS_2G 18.0f
78 #define LIS302DL_ACC_SENS_8G 72.0f
79 
80 #define LIS302DL_ACC_BIAS 0.0f
81 /** @} */
82 
83 /**
84  * @name LIS302DL communication interfaces related bit masks
85  * @{
86  */
87 #define LIS302DL_DI_MASK 0xFF
88 #define LIS302DL_DI(n) (1 << n)
89 #define LIS302DL_AD_MASK 0x3F
90 #define LIS302DL_AD(n) (1 << n)
91 #define LIS302DL_MS (1 << 6)
92 #define LIS302DL_RW (1 << 7)
93 /** @} */
94 
95 /**
96  * @name LIS302DL register addresses
97  * @{
98  */
99 #define LIS302DL_AD_WHO_AM_I 0x0F
100 #define LIS302DL_AD_CTRL_REG1 0x20
101 #define LIS302DL_AD_CTRL_REG2 0x21
102 #define LIS302DL_AD_CTRL_REG3 0x22
103 #define LIS302DL_AD_HP_FILER_RESET 0x23
104 #define LIS302DL_AD_STATUS_REG 0x27
105 #define LIS302DL_AD_OUT_X 0x29
106 #define LIS302DL_AD_OUT_Y 0x2B
107 #define LIS302DL_AD_OUT_Z 0x2D
108 #define LIS302DL_AD_FF_WU_CFG_1 0x30
109 #define LIS302DL_AD_FF_WU_SRC_1 0x31
110 #define LIS302DL_AD_FF_WU_THS_1 0x32
111 #define LIS302DL_AD_FF_WU_DURATION_1 0x33
112 #define LIS302DL_AD_FF_WU_CFG_2 0x34
113 #define LIS302DL_AD_FF_WU_SRC_2 0x35
114 #define LIS302DL_AD_FF_WU_THS_2 0x36
115 #define LIS302DL_AD_FF_WU_DURATION_2 0x37
116 #define LIS302DL_AD_CLICK_CFG 0x38
117 #define LIS302DL_AD_CLICK_SRC 0x39
118 #define LIS302DL_AD_CLICK_THSY_X 0x3B
119 #define LIS302DL_AD_CLICK_THSZ 0x3C
120 #define LIS302DL_AD_CLICK_TIME_LIMIT 0x3D
121 #define LIS302DL_AD_CLICK_LATENCY 0x3E
122 #define LIS302DL_AD_CLICK_WINDOW 0x3F
123 /** @} */
124 
125 /**
126  * @name LIS302DL_CTRL_REG1 register bits definitions
127  * @{
128  */
129 #define LIS302DL_CTRL_REG1_MASK 0xFF
130 #define LIS302DL_CTRL_REG1_XEN (1 << 0)
131 #define LIS302DL_CTRL_REG1_YEN (1 << 1)
132 #define LIS302DL_CTRL_REG1_ZEN (1 << 2)
133 #define LIS302DL_CTRL_REG1_STM (1 << 3)
134 #define LIS302DL_CTRL_REG1_STP (1 << 4)
135 #define LIS302DL_CTRL_REG1_FS_MASK 0x20
136 #define LIS302DL_CTRL_REG1_FS (1 << 5)
137 #define LIS302DL_CTRL_REG1_PD (1 << 6)
138 #define LIS302DL_CTRL_REG1_DR (1 << 7)
139 /** @} */
140 
141 /**
142  * @name LIS302DL_CTRL_REG2 register bits definitions
143  * @{
144  */
145 #define LIS302DL_CTRL_REG2_MASK 0xDF
146 #define LIS302DL_CTRL_REG2_HPCF1 (1 << 0)
147 #define LIS302DL_CTRL_REG2_HPCF2 (1 << 1)
148 #define LIS302DL_CTRL_REG2_HPFFWU1 (1 << 2)
149 #define LIS302DL_CTRL_REG2_HPFFWU2 (1 << 3)
150 #define LIS302DL_CTRL_REG2_FDS (1 << 4)
151 #define LIS302DL_CTRL_REG2_BOOT (1 << 6)
152 #define LIS302DL_CTRL_REG2_SIM (1 << 7)
153 /** @} */
154 
155 /**
156  * @name LIS302DL_CTRL_REG3 register bits definitions
157  * @{
158  */
159 #define LIS302DL_CTRL_REG3_MASK 0xFF
160 #define LIS302DL_CTRL_REG3_I1CFG0 (1 << 0)
161 #define LIS302DL_CTRL_REG3_I1CFG1 (1 << 1)
162 #define LIS302DL_CTRL_REG3_I1CFG2 (1 << 2)
163 #define LIS302DL_CTRL_REG3_I2CFG0 (1 << 3)
164 #define LIS302DL_CTRL_REG3_I2CFG1 (1 << 4)
165 #define LIS302DL_CTRL_REG3_I2CFG2 (1 << 5)
166 #define LIS302DL_CTRL_REG3_PP_OD (1 << 6)
167 #define LIS302DL_CTRL_REG3_IHL (1 << 7)
168 /** @} */
169 
170 /*===========================================================================*/
171 /* Driver pre-compile time settings. */
172 /*===========================================================================*/
173 
174 /**
175  * @name Configuration options
176  * @{
177  */
178 /**
179  * @brief LIS302DL SPI interface switch.
180  * @details If set to @p TRUE the support for SPI is included.
181  * @note The default is @p TRUE.
182  */
183 #if !defined(LIS302DL_USE_SPI) || defined(__DOXYGEN__)
184 #define LIS302DL_USE_SPI TRUE
185 #endif
186 
187 /**
188  * @brief LIS302DL shared SPI switch.
189  * @details If set to @p TRUE the device acquires SPI bus ownership
190  * on each transaction.
191  * @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION.
192  */
193 #if !defined(LIS302DL_SHARED_SPI) || defined(__DOXYGEN__)
194 #define LIS302DL_SHARED_SPI FALSE
195 #endif
196 
197 /**
198  * @brief LIS302DL I2C interface switch.
199  * @details If set to @p TRUE the support for I2C is included.
200  * @note The default is @p FALSE.
201  */
202 #if !defined(LIS302DL_USE_I2C) || defined(__DOXYGEN__)
203 #define LIS302DL_USE_I2C FALSE
204 #endif
205 
206 /**
207  * @brief LIS302DL shared I2C switch.
208  * @details If set to @p TRUE the device acquires I2C bus ownership
209  * on each transaction.
210  * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
211  */
212 #if !defined(LIS302DL_SHARED_I2C) || defined(__DOXYGEN__)
213 #define LIS302DL_SHARED_I2C FALSE
214 #endif
215 
216 /**
217  * @brief LIS302DL advanced configurations switch.
218  * @details If set to @p TRUE more configurations are available.
219  * @note The default is @p FALSE.
220  */
221 #if !defined(LIS302DL_USE_ADVANCED) || defined(__DOXYGEN__)
222 #define LIS302DL_USE_ADVANCED FALSE
223 #endif
224 /** @} */
225 
226 /*===========================================================================*/
227 /* Derived constants and error checks. */
228 /*===========================================================================*/
229 
230 #if !(LIS302DL_USE_SPI ^ LIS302DL_USE_I2C)
231 #error "LIS302DL_USE_SPI and LIS302DL_USE_I2C cannot be both true or both false"
232 #endif
233 
234 #if LIS302DL_USE_SPI && !HAL_USE_SPI
235 #error "LIS302DL_USE_SPI requires HAL_USE_SPI"
236 #endif
237 
238 #if LIS302DL_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
239 #error "LIS302DL_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
240 #endif
241 
242 #if LIS302DL_USE_I2C && !HAL_USE_I2C
243 #error "LIS302DL_USE_I2C requires HAL_USE_I2C"
244 #endif
245 
246 #if LIS302DL_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
247 #error "LIS302DL_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
248 #endif
249 
250 /**
251  * @todo Add support for LIS302DL over I2C.
252  */
253 #if LIS302DL_USE_I2C
254 #error "LIS302DL over I2C still not supported"
255 #endif
256 
257 /*===========================================================================*/
258 /* Driver data structures and types. */
259 /*===========================================================================*/
260 
261 /**
262  * @name LIS302DL data structures and types
263  * @{
264  */
265 /**
266  * @brief Structure representing a LIS302DL driver.
267  */
269 
270 /**
271  * @brief LIS302DL full scale.
272  */
273 typedef enum {
274  LIS302DL_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
275  LIS302DL_ACC_FS_8G = 0x20 /**< Full scale ±8g. */
277 
278 /**
279  * @brief LIS302DL output data rate and bandwidth.
280  */
281 typedef enum {
282  LIS302DL_ACC_ODR_100HZ = 0x00, /**< ODR 100 Hz. */
283  LIS302DL_ACC_ODR_400HZ = 0x80 /**< ODR 400 Hz. */
285 
286 /**
287  * @brief LIS302DL high pass filtering.
288  */
289 typedef enum {
290  LIS302DL_ACC_HP_DISABLED = 0x00, /**< HP bypassed. */
291  LIS302DL_ACC_HP_0 = 0x10, /**< HP cutoff 2Hz (ODR 100Hz) or 8Hz */
292  LIS302DL_ACC_HP_1 = 0x11, /**< HP cutoff 1Hz or 4Hz */
293  LIS302DL_ACC_HP_2 = 0x12, /**< HP cutoff 0.5Hz or 2Hz */
294  LIS302DL_ACC_HP_3 = 0x13 /**< HP cutoff 0.25Hz or 1Hz */
296 
297 /**
298  * @brief Driver state machine possible states.
299  */
300 typedef enum {
301  LIS302DL_UNINIT = 0, /**< Not initialized. */
302  LIS302DL_STOP = 1, /**< Stopped. */
303  LIS302DL_READY = 2, /**< Ready. */
305 
306 /**
307  * @brief LIS302DL configuration structure.
308  */
309 typedef struct {
310 
311 #if (LIS302DL_USE_SPI) || defined(__DOXYGEN__)
312  /**
313  * @brief SPI driver associated to this LIS302DL.
314  */
316  /**
317  * @brief SPI configuration associated to this LIS302DL.
318  */
320 #endif /* LIS302DL_USE_SPI */
321 #if (LIS302DL_USE_I2C) || defined(__DOXYGEN__)
322  /**
323  * @brief I2C driver associated to this LIS302DL.
324  */
326  /**
327  * @brief I2C configuration associated to this LIS302DL.
328  */
330 #endif /* LIS302DL_USE_I2C */
331  /**
332  * @brief LIS302DL accelerometer subsystem initial sensitivity.
333  */
335  /**
336  * @brief LIS302DL accelerometer subsystem initial bias.
337  */
338  float *accbias;
339  /**
340  * @brief LIS302DL accelerometer subsystem initial full scale.
341  */
343  /**
344  * @brief LIS302DL output data rate selection.
345  */
347 #if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__)
348  /**
349  * @brief LIS302DL high pass filtering.
350  */
352 #endif
354 
355 /**
356  * @brief @p LIS302DL specific methods.
357  */
358 #define _lis302dl_methods_alone \
359  /* Change full scale value of LIS302DL .*/ \
360  msg_t (*set_full_scale)(LIS302DLDriver *devp, lis302dl_acc_fs_t fs);
361 
362 
363 /**
364  * @brief @p LIS302DL specific methods with inherited ones.
365  */
366 #define _lis302dl_methods \
367  _base_object_methods \
368  _lis302dl_methods_alone
369 
370 /**
371  * @extends BaseObjectVMT
372  *
373  * @brief @p LIS302DL accelerometer virtual methods table.
374  */
375 struct LIS302DLVMT {
377 };
378 
379 /**
380  * @brief @p LIS302DLDriver specific data.
381  */
382 #define _lis302dl_data \
383  /* Driver state.*/ \
384  lis302dl_state_t state; \
385  /* Current configuration data.*/ \
386  const LIS302DLConfig *config; \
387  /* Accelerometer subsystem axes number.*/ \
388  size_t accaxes; \
389  /* Current sensitivity.*/ \
390  float accsensitivity[LIS302DL_ACC_NUMBER_OF_AXES]; \
391  /* Bias data.*/ \
392  int32_t accbias[LIS302DL_ACC_NUMBER_OF_AXES]; \
393  /* Current full scale value.*/ \
394  float accfullscale;
395 
396 /**
397  * @brief LIS302DL 3-axis accelerometer class.
398  */
400  /** @brief Virtual Methods Table.*/
401  const struct LIS302DLVMT *vmt;
402  /** @brief Base accelerometer interface.*/
405 };
406 /** @} */
407 
408 /*===========================================================================*/
409 /* Driver macros. */
410 /*===========================================================================*/
411 
412 /**
413  * @brief Return the number of axes of the BaseAccelerometer.
414  *
415  * @param[in] devp pointer to @p LIS302DLDriver.
416  *
417  * @return the number of axes.
418  *
419  * @api
420  */
421 #define lis302dlAccelerometerGetAxesNumber(devp) \
422  accelerometerGetAxesNumber(&((devp)->acc_if))
423 
424 /**
425  * @brief Retrieves raw data from the BaseAccelerometer.
426  * @note This data is retrieved from MEMS register without any algebraical
427  * manipulation.
428  * @note The axes array must be at least the same size of the
429  * BaseAccelerometer axes number.
430  *
431  * @param[in] devp pointer to @p LIS302DLDriver.
432  * @param[out] axes a buffer which would be filled with raw data.
433  *
434  * @return The operation status.
435  * @retval MSG_OK if the function succeeded.
436  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
437  * be retrieved using @p i2cGetErrors().
438  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
439  *
440  * @api
441  */
442 #define lis302dlAccelerometerReadRaw(devp, axes) \
443  accelerometerReadRaw(&((devp)->acc_if), axes)
444 
445 /**
446  * @brief Retrieves cooked data from the BaseAccelerometer.
447  * @note This data is manipulated according to the formula
448  * cooked = (raw * sensitivity) - bias.
449  * @note Final data is expressed as milli-G.
450  * @note The axes array must be at least the same size of the
451  * BaseAccelerometer axes number.
452  *
453  * @param[in] devp pointer to @p LIS302DLDriver.
454  * @param[out] axes a buffer which would be filled with cooked data.
455  *
456  * @return The operation status.
457  * @retval MSG_OK if the function succeeded.
458  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
459  * be retrieved using @p i2cGetErrors().
460  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
461  *
462  * @api
463  */
464 #define lis302dlAccelerometerReadCooked(devp, axes) \
465  accelerometerReadCooked(&((devp)->acc_if), axes)
466 
467 /**
468  * @brief Set bias values for the BaseAccelerometer.
469  * @note Bias must be expressed as milli-G.
470  * @note The bias buffer must be at least the same size of the
471  * BaseAccelerometer axes number.
472  *
473  * @param[in] devp pointer to @p LIS302DLDriver.
474  * @param[in] bp a buffer which contains biases.
475  *
476  * @return The operation status.
477  * @retval MSG_OK if the function succeeded.
478  *
479  * @api
480  */
481 #define lis302dlAccelerometerSetBias(devp, bp) \
482  accelerometerSetBias(&((devp)->acc_if), bp)
483 
484 /**
485  * @brief Reset bias values for the BaseAccelerometer.
486  * @note Default biases value are obtained from device datasheet when
487  * available otherwise they are considered zero.
488  *
489  * @param[in] devp pointer to @p LIS302DLDriver.
490  *
491  * @return The operation status.
492  * @retval MSG_OK if the function succeeded.
493  *
494  * @api
495  */
496 #define lis302dlAccelerometerResetBias(devp) \
497  accelerometerResetBias(&((devp)->acc_if))
498 
499 /**
500  * @brief Set sensitivity values for the BaseAccelerometer.
501  * @note Sensitivity must be expressed as milli-G/LSB.
502  * @note The sensitivity buffer must be at least the same size of the
503  * BaseAccelerometer axes number.
504  *
505  * @param[in] devp pointer to @p LIS302DLDriver.
506  * @param[in] sp a buffer which contains sensitivities.
507  *
508  * @return The operation status.
509  * @retval MSG_OK if the function succeeded.
510  *
511  * @api
512  */
513 #define lis302dlAccelerometerSetSensitivity(devp, sp) \
514  accelerometerSetSensitivity(&((devp)->acc_if), sp)
515 
516 /**
517  * @brief Reset sensitivity values for the BaseAccelerometer.
518  * @note Default sensitivities value are obtained from device datasheet.
519  *
520  * @param[in] devp pointer to @p LIS302DLDriver.
521  *
522  * @return The operation status.
523  * @retval MSG_OK if the function succeeded.
524  * @retval MSG_RESET otherwise.
525  *
526  * @api
527  */
528 #define lis302dlAccelerometerResetSensitivity(devp) \
529  accelerometerResetSensitivity(&((devp)->acc_if))
530 
531 /**
532  * @brief Changes the LIS302DLDriver accelerometer fullscale value.
533  * @note This function also rescale sensitivities and biases based on
534  * previous and next fullscale value.
535  * @note A recalibration is highly suggested after calling this function.
536  *
537  * @param[in] devp pointer to @p LIS302DLDriver.
538  * @param[in] fs new fullscale value.
539  *
540  * @return The operation status.
541  * @retval MSG_OK if the function succeeded.
542  * @retval MSG_RESET otherwise.
543  *
544  * @api
545  */
546 #define lis302dlAccelerometerSetFullScale(devp, fs) \
547  (devp)->vmt->acc_set_full_scale(devp, fs)
548 
549 /*===========================================================================*/
550 /* External declarations. */
551 /*===========================================================================*/
552 
553 #ifdef __cplusplus
554 extern "C" {
555 #endif
557  void lis302dlStart(LIS302DLDriver *devp, const LIS302DLConfig *config);
558  void lis302dlStop(LIS302DLDriver *devp);
559 #ifdef __cplusplus
560 }
561 #endif
562 
563 #endif /* _LIS302DL_H_ */
564 
565 /** @} */
566 
void lis302dlStop(LIS302DLDriver *devp)
Deactivates the LIS302DL Complex Driver peripheral.
Definition: lis302dl.c:528
Type of I2C driver configuration structure.
Definition: hal_i2c_lld.h:75
lis302dl_state_t
Driver state machine possible states.
Definition: lis302dl.h:300
float * accbias
LIS302DL accelerometer subsystem initial bias.
Definition: lis302dl.h:338
lis302dl_acc_odr_t
LIS302DL output data rate and bandwidth.
Definition: lis302dl.h:281
SPIDriver * spip
SPI driver associated to this LIS302DL.
Definition: lis302dl.h:315
void lis302dlStart(LIS302DLDriver *devp, const LIS302DLConfig *config)
Configures and activates LIS302DL Complex Driver peripheral.
Definition: lis302dl.c:444
BaseAccelerometer acc_if
Base accelerometer interface.
Definition: lis302dl.h:403
const struct LIS302DLVMT * vmt
Virtual Methods Table.
Definition: lis302dl.h:401
float * accsensitivity
LIS302DL accelerometer subsystem initial sensitivity.
Definition: lis302dl.h:334
Structure representing an I2C driver.
Definition: hal_i2c_lld.h:88
lis302dl_acc_odr_t accoutputdatarate
LIS302DL output data rate selection.
Definition: lis302dl.h:346
Structure representing an SPI driver.
Definition: hal_spi.h:192
LIS302DL 3-axis accelerometer class.
Definition: lis302dl.h:399
lis302dl_acc_fs_t
LIS302DL full scale.
Definition: lis302dl.h:273
lis302dl_acc_hp_t
LIS302DL high pass filtering.
Definition: lis302dl.h:289
Generic accelerometer interface header.
LIS302DL configuration structure.
Definition: lis302dl.h:309
I2CDriver * i2cp
I2C driver associated to this LIS302DL.
Definition: lis302dl.h:325
const SPIConfig * spicfg
SPI configuration associated to this LIS302DL.
Definition: lis302dl.h:319
lis302dl_acc_hp_t acchighpass
LIS302DL high pass filtering.
Definition: lis302dl.h:351
Driver configuration structure.
Definition: hal_spi.h:148
#define _lis302dl_methods
LIS302DL specific methods with inherited ones.
Definition: lis302dl.h:366
#define _lis302dl_data
LIS302DLDriver specific data.
Definition: lis302dl.h:382
lis302dl_acc_fs_t accfullscale
LIS302DL accelerometer subsystem initial full scale.
Definition: lis302dl.h:342
void lis302dlObjectInit(LIS302DLDriver *devp)
Initializes an instance.
Definition: lis302dl.c:425
const I2CConfig * i2ccfg
I2C configuration associated to this LIS302DL.
Definition: lis302dl.h:329
Base accelerometer class.
LIS302DL accelerometer virtual methods table.
Definition: lis302dl.h:375