ChibiOS  0.0.0
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.0"
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 0
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 accelerometer subsystem advanced configurations
218  * switch.
219  * @details If set to @p TRUE more configurations are available.
220  * @note The default is @p FALSE.
221  */
222 #if !defined(LIS302DL_ACC_USE_ADVANCED) || defined(__DOXYGEN__)
223 #define LIS302DL_ACC_USE_ADVANCED FALSE
224 #endif
225 /** @} */
226 
227 /*===========================================================================*/
228 /* Derived constants and error checks. */
229 /*===========================================================================*/
230 
231 #if !(LIS302DL_USE_SPI ^ LIS302DL_USE_I2C)
232 #error "LIS302DL_USE_SPI and LIS302DL_USE_I2C cannot be both true or both false"
233 #endif
234 
235 #if LIS302DL_USE_SPI && !HAL_USE_SPI
236 #error "LIS302DL_USE_SPI requires HAL_USE_SPI"
237 #endif
238 
239 #if LIS302DL_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
240 #error "LIS302DL_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
241 #endif
242 
243 #if LIS302DL_USE_I2C && !HAL_USE_I2C
244 #error "LIS302DL_USE_I2C requires HAL_USE_I2C"
245 #endif
246 
247 #if LIS302DL_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
248 #error "LIS302DL_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
249 #endif
250 
251 /**
252  * @todo Add support for LIS302DL over I2C.
253  */
254 #if LIS302DL_USE_I2C
255 #error "LIS302DL over I2C still not supported"
256 #endif
257 
258 /*===========================================================================*/
259 /* Driver data structures and types. */
260 /*===========================================================================*/
261 
262 /**
263  * @name LIS302DL data structures and types
264  * @{
265  */
266 /**
267  * @brief Structure representing a LIS302DL driver.
268  */
270 
271 /**
272  * @brief LIS302DL full scale.
273  */
274 typedef enum {
275  LIS302DL_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
276  LIS302DL_ACC_FS_8G = 0x20 /**< Full scale ±8g. */
278 
279 /**
280  * @brief LIS302DL output data rate and bandwidth.
281  */
282 typedef enum {
283  LIS302DL_ACC_ODR_100HZ = 0x00, /**< ODR 100 Hz. */
284  LIS302DL_ACC_ODR_400HZ = 0x80 /**< ODR 400 Hz. */
286 
287 /**
288  * @brief LIS302DL high pass filtering.
289  */
290 typedef enum {
291  LIS302DL_ACC_HP_DISABLED = 0x00, /**< HP bypassed. */
292  LIS302DL_ACC_HP_0 = 0x10, /**< HP cutoff 2Hz (ODR 100Hz) or 8Hz */
293  LIS302DL_ACC_HP_1 = 0x11, /**< HP cutoff 1Hz or 4Hz */
294  LIS302DL_ACC_HP_2 = 0x12, /**< HP cutoff 0.5Hz or 2Hz */
295  LIS302DL_ACC_HP_3 = 0x13 /**< HP cutoff 0.25Hz or 1Hz */
297 
298 /**
299  * @brief Driver state machine possible states.
300  */
301 typedef enum {
302  LIS302DL_UNINIT = 0, /**< Not initialized. */
303  LIS302DL_STOP = 1, /**< Stopped. */
304  LIS302DL_READY = 2, /**< Ready. */
306 
307 /**
308  * @brief LIS302DL configuration structure.
309  */
310 typedef struct {
311 
312 #if (LIS302DL_USE_SPI) || defined(__DOXYGEN__)
313  /**
314  * @brief SPI driver associated to this LIS302DL.
315  */
317  /**
318  * @brief SPI configuration associated to this LIS302DL.
319  */
321 #endif /* LIS302DL_USE_SPI */
322 #if (LIS302DL_USE_I2C) || defined(__DOXYGEN__)
323  /**
324  * @brief I2C driver associated to this LIS302DL.
325  */
327  /**
328  * @brief I2C configuration associated to this LIS302DL.
329  */
331 #endif /* LIS302DL_USE_I2C */
332  /**
333  * @brief LIS302DL accelerometer subsystem initial sensitivity.
334  */
336  /**
337  * @brief LIS302DL accelerometer subsystem initial bias.
338  */
339  float *accbias;
340  /**
341  * @brief LIS302DL accelerometer subsystem initial full scale.
342  */
344  /**
345  * @brief LIS302DL output data rate selection.
346  */
348 #if LIS302DL_USE_ADVANCED || defined(__DOXYGEN__)
349  /**
350  * @brief LIS302DL high pass filtering.
351  */
353 #endif
355 
356 /**
357  * @brief @p LIS302DL specific methods.
358  */
359 #define _lis302dl_methods_alone \
360  /* Change full scale value of LIS302DL .*/ \
361  msg_t (*set_full_scale)(LIS302DLDriver *devp, lis302dl_acc_fs_t fs);
362 
363 
364 /**
365  * @brief @p LIS302DL specific methods with inherited ones.
366  */
367 #define _lis302dl_methods \
368  _base_object_methods \
369  _lis302dl_methods_alone
370 
371 /**
372  * @extends BaseObjectVMT
373  *
374  * @brief @p LIS302DL accelerometer virtual methods table.
375  */
376 struct LIS302DLVMT {
378 };
379 
380 /**
381  * @brief @p LIS302DLDriver specific data.
382  */
383 #define _lis302dl_data \
384  /* Driver state.*/ \
385  lis302dl_state_t state; \
386  /* Current configuration data.*/ \
387  const LIS302DLConfig *config; \
388  /* Accelerometer subsystem axes number.*/ \
389  size_t accaxes; \
390  /* Current sensitivity.*/ \
391  float accsensitivity[LIS302DL_ACC_NUMBER_OF_AXES]; \
392  /* Bias data.*/ \
393  int32_t accbias[LIS302DL_ACC_NUMBER_OF_AXES]; \
394  /* Current full scale value.*/ \
395  float accfullscale;
396 
397 /**
398  * @brief LIS302DL 3-axis accelerometer class.
399  */
401  /** @brief Virtual Methods Table.*/
402  const struct LIS302DLVMT *vmt;
403  /** @brief Base accelerometer interface.*/
406 };
407 /** @} */
408 
409 /*===========================================================================*/
410 /* Driver macros. */
411 /*===========================================================================*/
412 
413 /**
414  * @brief Return the number of axes of the BaseAccelerometer.
415  *
416  * @param[in] devp pointer to @p LIS302DLDriver.
417  *
418  * @return the number of axes.
419  *
420  * @api
421  */
422 #define lis302dlAccelerometerGetAxesNumber(devp) \
423  accelerometerGetAxesNumber(&((devp)->acc_if))
424 
425 /**
426  * @brief Retrieves raw data from the BaseAccelerometer.
427  * @note This data is retrieved from MEMS register without any algebraical
428  * manipulation.
429  * @note The axes array must be at least the same size of the
430  * BaseAccelerometer axes number.
431  *
432  * @param[in] devp pointer to @p LIS302DLDriver.
433  * @param[out] axes a buffer which would be filled with raw data.
434  *
435  * @return The operation status.
436  * @retval MSG_OK if the function succeeded.
437  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
438  * be retrieved using @p i2cGetErrors().
439  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
440  *
441  * @api
442  */
443 #define lis302dlAccelerometerReadRaw(devp, axes) \
444  accelerometerReadRaw(&((devp)->acc_if), axes)
445 
446 /**
447  * @brief Retrieves cooked data from the BaseAccelerometer.
448  * @note This data is manipulated according to the formula
449  * cooked = (raw * sensitivity) - bias.
450  * @note Final data is expressed as milli-G.
451  * @note The axes array must be at least the same size of the
452  * BaseAccelerometer axes number.
453  *
454  * @param[in] devp pointer to @p LIS302DLDriver.
455  * @param[out] axes a buffer which would be filled with cooked data.
456  *
457  * @return The operation status.
458  * @retval MSG_OK if the function succeeded.
459  * @retval MSG_RESET if one or more I2C errors occurred, the errors can
460  * be retrieved using @p i2cGetErrors().
461  * @retval MSG_TIMEOUT if a timeout occurred before operation end.
462  *
463  * @api
464  */
465 #define lis302dlAccelerometerReadCooked(devp, axes) \
466  accelerometerReadCooked(&((devp)->acc_if), axes)
467 
468 /**
469  * @brief Set bias values for the BaseAccelerometer.
470  * @note Bias must be expressed as milli-G.
471  * @note The bias buffer must be at least the same size of the
472  * BaseAccelerometer axes number.
473  *
474  * @param[in] devp pointer to @p LIS302DLDriver.
475  * @param[in] bp a buffer which contains biases.
476  *
477  * @return The operation status.
478  * @retval MSG_OK if the function succeeded.
479  *
480  * @api
481  */
482 #define lis302dlAccelerometerSetBias(devp, bp) \
483  accelerometerSetBias(&((devp)->acc_if), bp)
484 
485 /**
486  * @brief Reset bias values for the BaseAccelerometer.
487  * @note Default biases value are obtained from device datasheet when
488  * available otherwise they are considered zero.
489  *
490  * @param[in] devp pointer to @p LIS302DLDriver.
491  *
492  * @return The operation status.
493  * @retval MSG_OK if the function succeeded.
494  *
495  * @api
496  */
497 #define lis302dlAccelerometerResetBias(devp) \
498  accelerometerResetBias(&((devp)->acc_if))
499 
500 /**
501  * @brief Set sensitivity values for the BaseAccelerometer.
502  * @note Sensitivity must be expressed as milli-G/LSB.
503  * @note The sensitivity buffer must be at least the same size of the
504  * BaseAccelerometer axes number.
505  *
506  * @param[in] devp pointer to @p LIS302DLDriver.
507  * @param[in] sp a buffer which contains sensitivities.
508  *
509  * @return The operation status.
510  * @retval MSG_OK if the function succeeded.
511  *
512  * @api
513  */
514 #define lis302dlAccelerometerSetSensitivity(devp, sp) \
515  accelerometerSetSensitivity(&((devp)->acc_if), sp)
516 
517 /**
518  * @brief Reset sensitivity values for the BaseAccelerometer.
519  * @note Default sensitivities value are obtained from device datasheet.
520  *
521  * @param[in] devp pointer to @p LIS302DLDriver.
522  *
523  * @return The operation status.
524  * @retval MSG_OK if the function succeeded.
525  * @retval MSG_RESET otherwise.
526  *
527  * @api
528  */
529 #define lis302dlAccelerometerResetSensitivity(devp) \
530  accelerometerResetSensitivity(&((devp)->acc_if))
531 
532 /**
533  * @brief Changes the LIS302DLDriver accelerometer fullscale value.
534  * @note This function also rescale sensitivities and biases based on
535  * previous and next fullscale value.
536  * @note A recalibration is highly suggested after calling this function.
537  *
538  * @param[in] devp pointer to @p LIS302DLDriver.
539  * @param[in] fs new fullscale value.
540  *
541  * @return The operation status.
542  * @retval MSG_OK if the function succeeded.
543  * @retval MSG_RESET otherwise.
544  *
545  * @api
546  */
547 #define lis302dlAccelerometerSetFullScale(devp, fs) \
548  (devp)->vmt->acc_set_full_scale(devp, fs)
549 
550 /*===========================================================================*/
551 /* External declarations. */
552 /*===========================================================================*/
553 
554 #ifdef __cplusplus
555 extern "C" {
556 #endif
558  void lis302dlStart(LIS302DLDriver *devp, const LIS302DLConfig *config);
559  void lis302dlStop(LIS302DLDriver *devp);
560 #ifdef __cplusplus
561 }
562 #endif
563 
564 #endif /* _LIS302DL_H_ */
565 
566 /** @} */
567 
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:301
float * accbias
LIS302DL accelerometer subsystem initial bias.
Definition: lis302dl.h:339
lis302dl_acc_odr_t
LIS302DL output data rate and bandwidth.
Definition: lis302dl.h:282
SPIDriver * spip
SPI driver associated to this LIS302DL.
Definition: lis302dl.h:316
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:404
const struct LIS302DLVMT * vmt
Virtual Methods Table.
Definition: lis302dl.h:402
float * accsensitivity
LIS302DL accelerometer subsystem initial sensitivity.
Definition: lis302dl.h:335
Structure representing an I2C driver.
Definition: hal_i2c_lld.h:88
lis302dl_acc_odr_t accoutputdatarate
LIS302DL output data rate selection.
Definition: lis302dl.h:347
Driver configuration structure.
Definition: hal_spi_lld.h:83
LIS302DL 3-axis accelerometer class.
Definition: lis302dl.h:400
lis302dl_acc_fs_t
LIS302DL full scale.
Definition: lis302dl.h:274
lis302dl_acc_hp_t
LIS302DL high pass filtering.
Definition: lis302dl.h:290
Generic accelerometer interface header.
LIS302DL configuration structure.
Definition: lis302dl.h:310
I2CDriver * i2cp
I2C driver associated to this LIS302DL.
Definition: lis302dl.h:326
const SPIConfig * spicfg
SPI configuration associated to this LIS302DL.
Definition: lis302dl.h:320
lis302dl_acc_hp_t acchighpass
LIS302DL high pass filtering.
Definition: lis302dl.h:352
#define _lis302dl_methods
LIS302DL specific methods with inherited ones.
Definition: lis302dl.h:367
#define _lis302dl_data
LIS302DLDriver specific data.
Definition: lis302dl.h:383
lis302dl_acc_fs_t accfullscale
LIS302DL accelerometer subsystem initial full scale.
Definition: lis302dl.h:343
void lis302dlObjectInit(LIS302DLDriver *devp)
Initializes an instance.
Definition: lis302dl.c:425
const I2CConfig * i2ccfg
I2C configuration associated to this LIS302DL.
Definition: lis302dl.h:330
Base accelerometer class.
LIS302DL accelerometer virtual methods table.
Definition: lis302dl.h:376
Structure representing an SPI driver.
Definition: hal_spi_lld.h:128