lis302dl.h 18 KB

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