lis3dsh.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 lis3dsh.h
  17. * @brief LIS3DSH MEMS interface module header.
  18. *
  19. * @addtogroup LIS3DSH
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #ifndef _LIS3DSH_H_
  24. #define _LIS3DSH_H_
  25. #include "hal_accelerometer.h"
  26. /*===========================================================================*/
  27. /* Driver constants. */
  28. /*===========================================================================*/
  29. /**
  30. * @name Version identification
  31. * @{
  32. */
  33. /**
  34. * @brief LIS3DSH driver version string.
  35. */
  36. #define EX_LIS3DSH_VERSION "1.1.2"
  37. /**
  38. * @brief LIS3DSH driver version major number.
  39. */
  40. #define EX_LIS3DSH_MAJOR 1
  41. /**
  42. * @brief LIS3DSH driver version minor number.
  43. */
  44. #define EX_LIS3DSH_MINOR 1
  45. /**
  46. * @brief LIS3DSH driver version patch number.
  47. */
  48. #define EX_LIS3DSH_PATCH 2
  49. /** @} */
  50. /**
  51. * @brief LIS3DSH 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 LIS3DSH_ACC_NUMBER_OF_AXES 3U
  59. #define LIS3DSH_ACC_2G 2.0f
  60. #define LIS3DSH_ACC_4G 4.0f
  61. #define LIS3DSH_ACC_6G 6.0f
  62. #define LIS3DSH_ACC_8G 8.0f
  63. #define LIS3DSH_ACC_16G 16.0f
  64. #define LIS3DSH_ACC_SENS_2G 0.06f
  65. #define LIS3DSH_ACC_SENS_4G 0.12f
  66. #define LIS3DSH_ACC_SENS_6G 0.18f
  67. #define LIS3DSH_ACC_SENS_8G 0.24f
  68. #define LIS3DSH_ACC_SENS_16G 0.73f
  69. #define LIS3DSH_ACC_BIAS 0.0f
  70. /** @} */
  71. /**
  72. * @name LIS3DSH communication interfaces related bit masks
  73. * @{
  74. */
  75. #define LIS3DSH_DI_MASK 0xFF
  76. #define LIS3DSH_DI(n) (1 << n)
  77. #define LIS3DSH_AD_MASK 0x3F
  78. #define LIS3DSH_AD(n) (1 << n)
  79. #define LIS3DSH_MS (1 << 6)
  80. #define LIS3DSH_RW (1 << 7)
  81. /** @} */
  82. /**
  83. * @name LIS3DSH register addresses
  84. * @{
  85. */
  86. #define LIS3DSH_AD_OUT_T 0x0C
  87. #define LIS3DSH_AD_INFO1 0x0D
  88. #define LIS3DSH_AD_INFO2 0x0E
  89. #define LIS3DSH_AD_WHO_AM_I 0x0F
  90. #define LIS3DSH_AD_OFF_X 0x10
  91. #define LIS3DSH_AD_OFF_Y 0x11
  92. #define LIS3DSH_AD_OFF_Z 0x12
  93. #define LIS3DSH_AD_CS_X 0x13
  94. #define LIS3DSH_AD_CS_Y 0x14
  95. #define LIS3DSH_AD_CS_Z 0x15
  96. #define LIS3DSH_AD_LC_L 0x16
  97. #define LIS3DSH_AD_LC_H 0x17
  98. #define LIS3DSH_AD_STAT 0x18
  99. #define LIS3DSH_AD_PEAK1 0x19
  100. #define LIS3DSH_AD_PEAK2 0x1A
  101. #define LIS3DSH_AD_VFC_1 0x1B
  102. #define LIS3DSH_AD_VFC_2 0x1C
  103. #define LIS3DSH_AD_VFC_3 0x1D
  104. #define LIS3DSH_AD_VFC_4 0x1E
  105. #define LIS3DSH_AD_THRS3 0x1F
  106. #define LIS3DSH_AD_CTRL_REG4 0x20
  107. #define LIS3DSH_AD_CTRL_REG1 0x21
  108. #define LIS3DSH_AD_CTRL_REG2 0x22
  109. #define LIS3DSH_AD_CTRL_REG3 0x23
  110. #define LIS3DSH_AD_CTRL_REG5 0x24
  111. #define LIS3DSH_AD_CTRL_REG6 0x25
  112. #define LIS3DSH_AD_STATUS 0x27
  113. #define LIS3DSH_AD_OUT_X_L 0x28
  114. #define LIS3DSH_AD_OUT_X_H 0x29
  115. #define LIS3DSH_AD_OUT_Y_L 0x2A
  116. #define LIS3DSH_AD_OUT_Y_H 0x2B
  117. #define LIS3DSH_AD_OUT_Z_L 0x2C
  118. #define LIS3DSH_AD_OUT_Z_H 0x2D
  119. #define LIS3DSH_AD_FIFO_CTRL 0x2E
  120. #define LIS3DSH_AD_FIFO_SRC 0x2F
  121. #define LIS3DSH_AD_ST1_0 0x40
  122. #define LIS3DSH_AD_ST1_1 0x41
  123. #define LIS3DSH_AD_ST1_2 0x42
  124. #define LIS3DSH_AD_ST1_3 0x43
  125. #define LIS3DSH_AD_ST1_4 0x44
  126. #define LIS3DSH_AD_ST1_5 0x45
  127. #define LIS3DSH_AD_ST1_6 0x46
  128. #define LIS3DSH_AD_ST1_7 0x47
  129. #define LIS3DSH_AD_ST1_8 0x48
  130. #define LIS3DSH_AD_ST1_9 0x49
  131. #define LIS3DSH_AD_ST1_A 0x4A
  132. #define LIS3DSH_AD_ST1_B 0x4B
  133. #define LIS3DSH_AD_ST1_C 0x4C
  134. #define LIS3DSH_AD_ST1_D 0x4D
  135. #define LIS3DSH_AD_ST1_E 0x4E
  136. #define LIS3DSH_AD_ST1_F 0x4F
  137. #define LIS3DSH_AD_TIM4_1 0x50
  138. #define LIS3DSH_AD_TIM3_1 0x51
  139. #define LIS3DSH_AD_TIM2_1_L 0x52
  140. #define LIS3DSH_AD_TIM2_1_H 0x53
  141. #define LIS3DSH_AD_TIM1_1_L 0x54
  142. #define LIS3DSH_AD_TIM1_1_H 0x55
  143. #define LIS3DSH_AD_THRS2_1 0x56
  144. #define LIS3DSH_AD_THRS1_1 0x57
  145. #define LIS3DSH_AD_MASK1_B 0x59
  146. #define LIS3DSH_AD_MASK1_A 0x5A
  147. #define LIS3DSH_AD_SETT1 0x5B
  148. #define LIS3DSH_AD_PR1 0x5C
  149. #define LIS3DSH_AD_TC1_L 0x5D
  150. #define LIS3DSH_AD_TC1_H 0x5E
  151. #define LIS3DSH_AD_OUTS1 0x5F
  152. #define LIS3DSH_AD_ST2_0 0x60
  153. #define LIS3DSH_AD_ST2_1 0x61
  154. #define LIS3DSH_AD_ST2_2 0x62
  155. #define LIS3DSH_AD_ST2_3 0x63
  156. #define LIS3DSH_AD_ST2_4 0x64
  157. #define LIS3DSH_AD_ST2_5 0x65
  158. #define LIS3DSH_AD_ST2_6 0x66
  159. #define LIS3DSH_AD_ST2_7 0x67
  160. #define LIS3DSH_AD_ST2_8 0x68
  161. #define LIS3DSH_AD_ST2_9 0x69
  162. #define LIS3DSH_AD_ST2_A 0x6A
  163. #define LIS3DSH_AD_ST2_B 0x6B
  164. #define LIS3DSH_AD_ST2_C 0x6C
  165. #define LIS3DSH_AD_ST2_D 0x6D
  166. #define LIS3DSH_AD_ST2_E 0x6E
  167. #define LIS3DSH_AD_ST2_F 0x6F
  168. #define LIS3DSH_AD_TIM4_2 0x70
  169. #define LIS3DSH_AD_TIM3_2 0x71
  170. #define LIS3DSH_AD_TIM2_2_L 0x72
  171. #define LIS3DSH_AD_TIM2_2_H 0x73
  172. #define LIS3DSH_AD_TIM1_2_L 0x74
  173. #define LIS3DSH_AD_TIM1_2_H 0x75
  174. #define LIS3DSH_AD_THRS2_2 0x76
  175. #define LIS3DSH_AD_THRS1_2 0x77
  176. #define LIS3DSH_AD_DES2 0x78
  177. #define LIS3DSH_AD_MASK2_B 0x79
  178. #define LIS3DSH_AD_MASK2_A 0x7A
  179. #define LIS3DSH_AD_SETT2 0x7B
  180. #define LIS3DSH_AD_PR2 0x7C
  181. #define LIS3DSH_AD_TC2_L 0x7D
  182. #define LIS3DSH_AD_TC2_H 0x7E
  183. #define LIS3DSH_AD_OUTS2 0x7F
  184. /** @} */
  185. /**
  186. * @name LIS3DSH_CTRL_REG1 register bits definitions
  187. * @{
  188. */
  189. #define LIS3DSH_CTRL_REG1_MASK 0xE9
  190. #define LIS3DSH_CTRL_REG1_SM1_EN (1 << 0)
  191. #define LIS3DSH_CTRL_REG1_SM1_PIN (1 << 3)
  192. #define LIS3DSH_CTRL_REG1_HYST0_1 (1 << 5)
  193. #define LIS3DSH_CTRL_REG1_HYST1_1 (1 << 6)
  194. #define LIS3DSH_CTRL_REG1_HYST2_1 (1 << 7)
  195. /** @} */
  196. /**
  197. * @name LIS3DSH_CTRL_REG2 register bits definitions
  198. * @{
  199. */
  200. #define LIS3DSH_CTRL_REG2_MASK 0xE9
  201. #define LIS3DSH_CTRL_REG2_SM2_EN (1 << 0)
  202. #define LIS3DSH_CTRL_REG2_SM2_PIN (1 << 3)
  203. #define LIS3DSH_CTRL_REG2_HYST0_2 (1 << 5)
  204. #define LIS3DSH_CTRL_REG2_HYST1_2 (1 << 6)
  205. #define LIS3DSH_CTRL_REG2_HYST2_2 (1 << 7)
  206. /** @} */
  207. /**
  208. * @name LIS3DSH_CTRL_REG3 register bits definitions
  209. * @{
  210. */
  211. #define LIS3DSH_CTRL_REG3_MASK 0xFF
  212. #define LIS3DSH_CTRL_REG3_STRT (1 << 0)
  213. #define LIS3DSH_CTRL_REG3_VFILT (1 << 2)
  214. #define LIS3DSH_CTRL_REG3_INT1_EN (1 << 3)
  215. #define LIS3DSH_CTRL_REG3_INT2_EN (1 << 4)
  216. #define LIS3DSH_CTRL_REG3_IEL (1 << 5)
  217. #define LIS3DSH_CTRL_REG3_IEA (1 << 6)
  218. #define LIS3DSH_CTRL_REG3_DR_EN (1 << 7)
  219. /** @} */
  220. /**
  221. * @name LIS3DSH_CTRL_REG4 register bits definitions
  222. * @{
  223. */
  224. #define LIS3DSH_CTRL_REG4_MASK 0xFF
  225. #define LIS3DSH_CTRL_REG4_XEN (1 << 0)
  226. #define LIS3DSH_CTRL_REG4_YEN (1 << 1)
  227. #define LIS3DSH_CTRL_REG4_ZEN (1 << 2)
  228. #define LIS3DSH_CTRL_REG4_BDU (1 << 3)
  229. #define LIS3DSH_CTRL_REG4_ODR_0 (1 << 4)
  230. #define LIS3DSH_CTRL_REG4_ODR_1 (1 << 5)
  231. #define LIS3DSH_CTRL_REG4_ODR_2 (1 << 6)
  232. #define LIS3DSH_CTRL_REG4_ODR_3 (1 << 7)
  233. /** @} */
  234. /**
  235. * @name LIS3DSH_CTRL_REG5 register bits definitions
  236. * @{
  237. */
  238. #define LIS3DSH_CTRL_REG5_MASK 0xFF
  239. #define LIS3DSH_CTRL_REG5_SIM (1 << 0)
  240. #define LIS3DSH_CTRL_REG5_ST1 (1 << 1)
  241. #define LIS3DSH_CTRL_REG5_ST2 (1 << 2)
  242. #define LIS3DSH_CTRL_REG5_FS_MASK 0x38
  243. #define LIS3DSH_CTRL_REG5_FS0 (1 << 3)
  244. #define LIS3DSH_CTRL_REG5_FS1 (1 << 4)
  245. #define LIS3DSH_CTRL_REG5_FS2 (1 << 5)
  246. #define LIS3DSH_CTRL_REG5_BW1 (1 << 6)
  247. #define LIS3DSH_CTRL_REG5_BW2 (1 << 7)
  248. /** @} */
  249. /**
  250. * @name LIS3DSH_CTRL_REG6 register bits definitions
  251. * @{
  252. */
  253. #define LIS3DSH_CTRL_REG6_MASK 0xFF
  254. #define LIS3DSH_CTRL_REG6_P2_BOOT (1 << 0)
  255. #define LIS3DSH_CTRL_REG6_P1_OVRUN (1 << 1)
  256. #define LIS3DSH_CTRL_REG6_P1_WTM (1 << 2)
  257. #define LIS3DSH_CTRL_REG6_P1_EMPTY (1 << 3)
  258. #define LIS3DSH_CTRL_REG6_ADD_INC (1 << 4)
  259. #define LIS3DSH_CTRL_REG6_WTM_EN (1 << 5)
  260. #define LIS3DSH_CTRL_REG6_FIFO_EN (1 << 6)
  261. #define LIS3DSH_CTRL_REG6_BOOT (1 << 7)
  262. /** @} */
  263. /*===========================================================================*/
  264. /* Driver pre-compile time settings. */
  265. /*===========================================================================*/
  266. /**
  267. * @name Configuration options
  268. * @{
  269. */
  270. /**
  271. * @brief LIS3DSH SPI interface switch.
  272. * @details If set to @p TRUE the support for SPI is included.
  273. * @note The default is @p TRUE.
  274. */
  275. #if !defined(LIS3DSH_USE_SPI) || defined(__DOXYGEN__)
  276. #define LIS3DSH_USE_SPI TRUE
  277. #endif
  278. /**
  279. * @brief LIS3DSH shared SPI switch.
  280. * @details If set to @p TRUE the device acquires SPI bus ownership
  281. * on each transaction.
  282. * @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION.
  283. */
  284. #if !defined(LIS3DSH_SHARED_SPI) || defined(__DOXYGEN__)
  285. #define LIS3DSH_SHARED_SPI FALSE
  286. #endif
  287. /**
  288. * @brief LIS3DSH I2C interface switch.
  289. * @details If set to @p TRUE the support for I2C is included.
  290. * @note The default is @p FALSE.
  291. */
  292. #if !defined(LIS3DSH_USE_I2C) || defined(__DOXYGEN__)
  293. #define LIS3DSH_USE_I2C FALSE
  294. #endif
  295. /**
  296. * @brief LIS3DSH shared I2C switch.
  297. * @details If set to @p TRUE the device acquires I2C bus ownership
  298. * on each transaction.
  299. * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
  300. */
  301. #if !defined(LIS3DSH_SHARED_I2C) || defined(__DOXYGEN__)
  302. #define LIS3DSH_SHARED_I2C FALSE
  303. #endif
  304. /**
  305. * @brief LIS3DSH advanced configurations switch.
  306. * @details If set to @p TRUE more configurations are available.
  307. * @note The default is @p FALSE.
  308. */
  309. #if !defined(LIS3DSH_USE_ADVANCED) || defined(__DOXYGEN__)
  310. #define LIS3DSH_USE_ADVANCED FALSE
  311. #endif
  312. /** @} */
  313. /*===========================================================================*/
  314. /* Derived constants and error checks. */
  315. /*===========================================================================*/
  316. #if !(LIS3DSH_USE_SPI ^ LIS3DSH_USE_I2C)
  317. #error "LIS3DSH_USE_SPI and LIS3DSH_USE_I2C cannot be both true or both false"
  318. #endif
  319. #if LIS3DSH_USE_SPI && !HAL_USE_SPI
  320. #error "LIS3DSH_USE_SPI requires HAL_USE_SPI"
  321. #endif
  322. #if LIS3DSH_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
  323. #error "LIS3DSH_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
  324. #endif
  325. #if LIS3DSH_USE_I2C && !HAL_USE_I2C
  326. #error "LIS3DSH_USE_I2C requires HAL_USE_I2C"
  327. #endif
  328. #if LIS3DSH_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
  329. #error "LIS3DSH_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
  330. #endif
  331. /**
  332. * @todo Add support for LIS3DSH over I2C.
  333. */
  334. #if LIS3DSH_USE_I2C
  335. #error "LIS3DSH over I2C still not supported"
  336. #endif
  337. /*===========================================================================*/
  338. /* Driver data structures and types. */
  339. /*===========================================================================*/
  340. /**
  341. * @name LIS3DSH data structures and types
  342. * @{
  343. */
  344. /**
  345. * @brief Structure representing a LIS3DSH driver.
  346. */
  347. typedef struct LIS3DSHDriver LIS3DSHDriver;
  348. /**
  349. * @brief LIS3DSH full scale.
  350. */
  351. typedef enum {
  352. LIS3DSH_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
  353. LIS3DSH_ACC_FS_4G = 0x08, /**< Full scale ±4g. */
  354. LIS3DSH_ACC_FS_6G = 0x10, /**< Full scale ±6g. */
  355. LIS3DSH_ACC_FS_8G = 0x18, /**< Full scale ±8g. */
  356. LIS3DSH_ACC_FS_16G = 0x20 /**< Full scale ±16g. */
  357. }lis3dsh_acc_fs_t;
  358. /**
  359. * @brief LIS3DSH output data rate.
  360. */
  361. typedef enum {
  362. LIS3DSH_ACC_ODR_PD = 0x00, /**< ODR 100 Hz. */
  363. LIS3DSH_ACC_ODR_3_125HZ = 0x10, /**< ODR 3.125 Hz. */
  364. LIS3DSH_ACC_ODR_6_25HZ = 0x20, /**< ODR 6.25 Hz. */
  365. LIS3DSH_ACC_ODR_12_5HZ = 0x30, /**< ODR 12.5 Hz. */
  366. LIS3DSH_ACC_ODR_25HZ = 0x40, /**< ODR 25 Hz. */
  367. LIS3DSH_ACC_ODR_50HZ = 0x50, /**< ODR 50 Hz. */
  368. LIS3DSH_ACC_ODR_100HZ = 0x60, /**< ODR 100 Hz. */
  369. LIS3DSH_ACC_ODR_400HZ = 0x70, /**< ODR 400 Hz. */
  370. LIS3DSH_ACC_ODR_800HZ = 0x80, /**< ODR 800 Hz. */
  371. LIS3DSH_ACC_ODR_1600HZ = 0x90 /**< ODR 1600 Hz. */
  372. }lis3dsh_acc_odr_t;
  373. /**
  374. * @brief LIS3DSH anti-aliasing bandwidth.
  375. */
  376. typedef enum {
  377. LIS3DSH_ACC_BW_800HZ = 0x00, /**< AA filter BW 800Hz. */
  378. LIS3DSH_ACC_BW_200HZ = 0x40, /**< AA filter BW 200Hz. */
  379. LIS3DSH_ACC_BW_400HZ = 0x80, /**< AA filter BW 400Hz. */
  380. LIS3DSH_ACC_BW_50HZ = 0xC0 /**< AA filter BW 50Hz. */
  381. }lis3dsh_acc_bw_t;
  382. /**
  383. * @brief LIS3DSH block data update.
  384. */
  385. typedef enum {
  386. LIS3DSH_ACC_BDU_CONTINUOUS = 0x00,/**< Block data continuously updated. */
  387. LIS3DSH_ACC_BDU_BLOCKED = 0x80 /**< Block data updated after reading. */
  388. } lis3dsh_acc_bdu_t;
  389. /**
  390. * @brief Driver state machine possible states.
  391. */
  392. typedef enum {
  393. LIS3DSH_UNINIT = 0, /**< Not initialized. */
  394. LIS3DSH_STOP = 1, /**< Stopped. */
  395. LIS3DSH_READY = 2, /**< Ready. */
  396. } lis3dsh_state_t;
  397. /**
  398. * @brief LIS3DSH configuration structure.
  399. */
  400. typedef struct {
  401. #if (LIS3DSH_USE_SPI) || defined(__DOXYGEN__)
  402. /**
  403. * @brief SPI driver associated to this LIS3DSH.
  404. */
  405. SPIDriver *spip;
  406. /**
  407. * @brief SPI configuration associated to this LIS3DSH.
  408. */
  409. const SPIConfig *spicfg;
  410. #endif /* LIS3DSH_USE_SPI */
  411. #if (LIS3DSH_USE_I2C) || defined(__DOXYGEN__)
  412. /**
  413. * @brief I2C driver associated to this LIS3DSH.
  414. */
  415. I2CDriver *i2cp;
  416. /**
  417. * @brief I2C configuration associated to this LIS3DSH.
  418. */
  419. const I2CConfig *i2ccfg;
  420. #endif /* LIS3DSH_USE_I2C */
  421. /**
  422. * @brief LIS3DSH accelerometer subsystem initial sensitivity.
  423. */
  424. float *accsensitivity;
  425. /**
  426. * @brief LIS3DSH accelerometer subsystem initial bias.
  427. */
  428. float *accbias;
  429. /**
  430. * @brief LIS3DSH accelerometer subsystem initial full scale.
  431. */
  432. lis3dsh_acc_fs_t accfullscale;
  433. /**
  434. * @brief LIS3DSH output data rate selection.
  435. */
  436. lis3dsh_acc_odr_t accoutputdatarate;
  437. #if LIS3DSH_USE_ADVANCED || defined(__DOXYGEN__)
  438. /**
  439. * @brief LIS3DSH anti-aliasing bandwidth.
  440. */
  441. lis3dsh_acc_bw_t accantialiasing;
  442. /**
  443. * @brief LIS3DSH block data update.
  444. */
  445. lis3dsh_acc_bdu_t accblockdataupdate;
  446. #endif
  447. } LIS3DSHConfig;
  448. /**
  449. * @brief @p LIS3DSH specific methods.
  450. */
  451. #define _lis3dsh_methods_alone \
  452. /* Change full scale value of LIS3DSH accelerometer subsystem.*/ \
  453. msg_t (*acc_set_full_scale)(LIS3DSHDriver *devp, lis3dsh_acc_fs_t fs);
  454. /**
  455. * @brief @p LIS3DSH specific methods with inherited ones.
  456. */
  457. #define _lis3dsh_methods \
  458. _base_object_methods \
  459. _lis3dsh_methods_alone
  460. /**
  461. * @extends BaseObjectVMT
  462. *
  463. * @brief @p LIS3DSH virtual methods table.
  464. */
  465. struct LIS3DSHVMT {
  466. _lis3dsh_methods
  467. };
  468. /**
  469. * @brief @p LIS3DSHDriver specific data.
  470. */
  471. #define _lis3dsh_data \
  472. _base_sensor_data \
  473. /* Driver state.*/ \
  474. lis3dsh_state_t state; \
  475. /* Current configuration data.*/ \
  476. const LIS3DSHConfig *config; \
  477. /* Accelerometer subsystem axes number.*/ \
  478. size_t accaxes; \
  479. /* Accelerometer subsystem current sensitivity.*/ \
  480. float accsensitivity[LIS3DSH_ACC_NUMBER_OF_AXES]; \
  481. /* Accelerometer subsystem current bias .*/ \
  482. float accbias[LIS3DSH_ACC_NUMBER_OF_AXES]; \
  483. /* Accelerometer subsystem current full scale value.*/ \
  484. float accfullscale;
  485. /**
  486. * @brief LIS3DSH 3-axis accelerometer class.
  487. */
  488. struct LIS3DSHDriver {
  489. /** @brief Virtual Methods Table.*/
  490. const struct LIS3DSHVMT *vmt;
  491. /** @brief Base accelerometer interface.*/
  492. BaseAccelerometer acc_if;
  493. _lis3dsh_data
  494. };
  495. /** @} */
  496. /*===========================================================================*/
  497. /* Driver macros. */
  498. /*===========================================================================*/
  499. /**
  500. * @brief Return the number of axes of the BaseAccelerometer.
  501. *
  502. * @param[in] devp pointer to @p LIS3DSHDriver.
  503. *
  504. * @return the number of axes.
  505. *
  506. * @api
  507. */
  508. #define lis3dshAccelerometerGetAxesNumber(devp) \
  509. accelerometerGetAxesNumber(&((devp)->acc_if))
  510. /**
  511. * @brief Retrieves raw data from the BaseAccelerometer.
  512. * @note This data is retrieved from MEMS register without any algebraical
  513. * manipulation.
  514. * @note The axes array must be at least the same size of the
  515. * BaseAccelerometer axes number.
  516. *
  517. * @param[in] devp pointer to @p LIS3DSHDriver.
  518. * @param[out] axes a buffer which would be filled with raw data.
  519. *
  520. * @return The operation status.
  521. * @retval MSG_OK if the function succeeded.
  522. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  523. * be retrieved using @p i2cGetErrors().
  524. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  525. *
  526. * @api
  527. */
  528. #define lis3dshAccelerometerReadRaw(devp, axes) \
  529. accelerometerReadRaw(&((devp)->acc_if), axes)
  530. /**
  531. * @brief Retrieves cooked data from the BaseAccelerometer.
  532. * @note This data is manipulated according to the formula
  533. * cooked = (raw * sensitivity) - bias.
  534. * @note Final data is expressed as milli-G.
  535. * @note The axes array must be at least the same size of the
  536. * BaseAccelerometer axes number.
  537. *
  538. * @param[in] devp pointer to @p LIS3DSHDriver.
  539. * @param[out] axes a buffer which would be filled with cooked data.
  540. *
  541. * @return The operation status.
  542. * @retval MSG_OK if the function succeeded.
  543. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  544. * be retrieved using @p i2cGetErrors().
  545. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  546. *
  547. * @api
  548. */
  549. #define lis3dshAccelerometerReadCooked(devp, axes) \
  550. accelerometerReadCooked(&((devp)->acc_if), axes)
  551. /**
  552. * @brief Set bias values for the BaseAccelerometer.
  553. * @note Bias must be expressed as milli-G.
  554. * @note The bias buffer must be at least the same size of the
  555. * BaseAccelerometer axes number.
  556. *
  557. * @param[in] devp pointer to @p LIS3DSHDriver.
  558. * @param[in] bp a buffer which contains biases.
  559. *
  560. * @return The operation status.
  561. * @retval MSG_OK if the function succeeded.
  562. *
  563. * @api
  564. */
  565. #define lis3dshAccelerometerSetBias(devp, bp) \
  566. accelerometerSetBias(&((devp)->acc_if), bp)
  567. /**
  568. * @brief Reset bias values for the BaseAccelerometer.
  569. * @note Default biases value are obtained from device datasheet when
  570. * available otherwise they are considered zero.
  571. *
  572. * @param[in] devp pointer to @p LIS3DSHDriver.
  573. *
  574. * @return The operation status.
  575. * @retval MSG_OK if the function succeeded.
  576. *
  577. * @api
  578. */
  579. #define lis3dshAccelerometerResetBias(devp) \
  580. accelerometerResetBias(&((devp)->acc_if))
  581. /**
  582. * @brief Set sensitivity values for the BaseAccelerometer.
  583. * @note Sensitivity must be expressed as milli-G/LSB.
  584. * @note The sensitivity buffer must be at least the same size of the
  585. * BaseAccelerometer axes number.
  586. *
  587. * @param[in] devp pointer to @p LIS3DSHDriver.
  588. * @param[in] sp a buffer which contains sensitivities.
  589. *
  590. * @return The operation status.
  591. * @retval MSG_OK if the function succeeded.
  592. *
  593. * @api
  594. */
  595. #define lis3dshAccelerometerSetSensitivity(devp, sp) \
  596. accelerometerSetSensitivity(&((devp)->acc_if), sp)
  597. /**
  598. * @brief Reset sensitivity values for the BaseAccelerometer.
  599. * @note Default sensitivities value are obtained from device datasheet.
  600. *
  601. * @param[in] devp pointer to @p LIS3DSHDriver.
  602. *
  603. * @return The operation status.
  604. * @retval MSG_OK if the function succeeded.
  605. * @retval MSG_RESET otherwise.
  606. *
  607. * @api
  608. */
  609. #define lis3dshAccelerometerResetSensitivity(devp) \
  610. accelerometerResetSensitivity(&((devp)->acc_if))
  611. /**
  612. * @brief Changes the LIS3DSHDriver accelerometer fullscale value.
  613. * @note This function also rescale sensitivities and biases based on
  614. * previous and next fullscale value.
  615. * @note A recalibration is highly suggested after calling this function.
  616. *
  617. * @param[in] devp pointer to @p LIS3DSHDriver.
  618. * @param[in] fs new fullscale value.
  619. *
  620. * @return The operation status.
  621. * @retval MSG_OK if the function succeeded.
  622. * @retval MSG_RESET otherwise.
  623. *
  624. * @api
  625. */
  626. #define lis3dshAccelerometerSetFullScale(devp, fs) \
  627. (devp)->vmt->acc_set_full_scale(devp, fs)
  628. /*===========================================================================*/
  629. /* External declarations. */
  630. /*===========================================================================*/
  631. #ifdef __cplusplus
  632. extern "C" {
  633. #endif
  634. void lis3dshObjectInit(LIS3DSHDriver *devp);
  635. void lis3dshStart(LIS3DSHDriver *devp, const LIS3DSHConfig *config);
  636. void lis3dshStop(LIS3DSHDriver *devp);
  637. #ifdef __cplusplus
  638. }
  639. #endif
  640. #endif /* _LIS3DSH_H_ */
  641. /** @} */