lsm303dlhc.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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 lsm303dlhc.h
  17. * @brief LSM303DLHC MEMS interface module header.
  18. *
  19. * @addtogroup LSM303DLHC
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #ifndef _LSM303DLHC_H_
  24. #define _LSM303DLHC_H_
  25. #include "hal_accelerometer.h"
  26. #include "hal_compass.h"
  27. /*===========================================================================*/
  28. /* Driver constants. */
  29. /*===========================================================================*/
  30. /**
  31. * @name Version identification
  32. * @{
  33. */
  34. /**
  35. * @brief LSM303DLHC driver version string.
  36. */
  37. #define EX_LSM303DLHC_VERSION "1.1.2"
  38. /**
  39. * @brief LSM303DLHC driver version major number.
  40. */
  41. #define EX_LSM303DLHC_MAJOR 1
  42. /**
  43. * @brief LSM303DLHC driver version minor number.
  44. */
  45. #define EX_LSM303DLHC_MINOR 1
  46. /**
  47. * @brief LSM303DLHC driver version patch number.
  48. */
  49. #define EX_LSM303DLHC_PATCH 2
  50. /** @} */
  51. /**
  52. * @brief LSM303DLHC accelerometer subsystem characteristics.
  53. * @note Sensitivity is expressed as milli-G/LSB whereas
  54. * 1 milli-G = 0.00980665 m/s^2.
  55. * @note Bias is expressed as milli-G.
  56. *
  57. * @{
  58. */
  59. #define LSM303DLHC_ACC_NUMBER_OF_AXES 3U
  60. #define LSM303DLHC_ACC_2G 2.0f
  61. #define LSM303DLHC_ACC_4G 4.0f
  62. #define LSM303DLHC_ACC_8G 8.0f
  63. #define LSM303DLHC_ACC_16G 16.0f
  64. #define LSM303DLHC_ACC_SENS_2G 0.0610f
  65. #define LSM303DLHC_ACC_SENS_4G 0.1221f
  66. #define LSM303DLHC_ACC_SENS_8G 0.2442f
  67. #define LSM303DLHC_ACC_SENS_16G 0.4884f
  68. #define LSM303DLHC_ACC_BIAS 0.0f
  69. /** @} */
  70. /**
  71. * @brief LSM303DLHC compass subsystem characteristics.
  72. * @note Sensitivity is expressed as G/LSB whereas G stands for Gauss.
  73. * @note Bias is expressed as G.
  74. *
  75. * @{
  76. */
  77. #define LSM303DLHC_COMP_NUMBER_OF_AXES 3U
  78. #define LSM303DLHC_COMP_1P3GA 1.3f
  79. #define LSM303DLHC_COMP_1P9GA 1.9f
  80. #define LSM303DLHC_COMP_2P5GA 2.5f
  81. #define LSM303DLHC_COMP_4P0GA 4.0f
  82. #define LSM303DLHC_COMP_4P7GA 4.7f
  83. #define LSM303DLHC_COMP_5P6GA 5.6f
  84. #define LSM303DLHC_COMP_8P1GA 8.1f
  85. #define LSM303DLHC_COMP_SENS_XY_1P3GA 0.000909f
  86. #define LSM303DLHC_COMP_SENS_XY_1P9GA 0.001169f
  87. #define LSM303DLHC_COMP_SENS_XY_2P5GA 0.0014925f
  88. #define LSM303DLHC_COMP_SENS_XY_4P0GA 0.0022222f
  89. #define LSM303DLHC_COMP_SENS_XY_4P7GA 0.0025000f
  90. #define LSM303DLHC_COMP_SENS_XY_5P6GA 0.0030303f
  91. #define LSM303DLHC_COMP_SENS_XY_8P1GA 0.0043478f
  92. #define LSM303DLHC_COMP_SENS_Z_1P3GA 0.0010204f
  93. #define LSM303DLHC_COMP_SENS_Z_1P9GA 0.0013071f
  94. #define LSM303DLHC_COMP_SENS_Z_2P5GA 0.0016666f
  95. #define LSM303DLHC_COMP_SENS_Z_4P0GA 0.0025000f
  96. #define LSM303DLHC_COMP_SENS_Z_4P7GA 0.0028169f
  97. #define LSM303DLHC_COMP_SENS_Z_5P6GA 0.0033898f
  98. #define LSM303DLHC_COMP_SENS_Z_8P1GA 0.0048780f
  99. #define LSM303DLHC_COMP_BIAS 0.0f
  100. /** @} */
  101. /**
  102. * @name LSM303DLHC communication interfaces related bit masks
  103. * @{
  104. */
  105. #define LSM303DLHC_DI_MASK 0xFF
  106. #define LSM303DLHC_DI(n) (1 << n)
  107. #define LSM303DLHC_AD_MASK 0x7F
  108. #define LSM303DLHC_AD(n) (1 << n)
  109. #define LSM303DLHC_MS (1 << 7)
  110. /** @} */
  111. /**
  112. * @name LSM303DLHC register addresses
  113. * @{
  114. */
  115. #define LSM303DLHC_AD_ACC_CTRL_REG1 0x20
  116. #define LSM303DLHC_AD_ACC_CTRL_REG2 0x21
  117. #define LSM303DLHC_AD_ACC_CTRL_REG3 0x22
  118. #define LSM303DLHC_AD_ACC_CTRL_REG4 0x23
  119. #define LSM303DLHC_AD_ACC_CTRL_REG5 0x24
  120. #define LSM303DLHC_AD_ACC_CTRL_REG6 0x25
  121. #define LSM303DLHC_AD_ACC_REFERENCE 0x26
  122. #define LSM303DLHC_AD_ACC_STATUS_REG 0x27
  123. #define LSM303DLHC_AD_ACC_OUT_X_L 0x28
  124. #define LSM303DLHC_AD_ACC_OUT_X_H 0x29
  125. #define LSM303DLHC_AD_ACC_OUT_Y_L 0x2A
  126. #define LSM303DLHC_AD_ACC_OUT_Y_H 0x2B
  127. #define LSM303DLHC_AD_ACC_OUT_Z_L 0x2C
  128. #define LSM303DLHC_AD_ACC_OUT_Z_H 0x2D
  129. #define LSM303DLHC_AD_ACC_FIFO_CTRL_REG 0x2E
  130. #define LSM303DLHC_AD_ACC_FIFO_SRC_REG 0x2F
  131. #define LSM303DLHC_AD_ACC_INT1_CFG 0x30
  132. #define LSM303DLHC_AD_ACC_INT1_SRC 0x31
  133. #define LSM303DLHC_AD_ACC_INT1_THS 0x32
  134. #define LSM303DLHC_AD_ACC_INT1_DURATION 0x33
  135. #define LSM303DLHC_AD_ACC_INT2_CFG 0x34
  136. #define LSM303DLHC_AD_ACC_INT2_SRC 0x35
  137. #define LSM303DLHC_AD_ACC_INT2_THS 0x36
  138. #define LSM303DLHC_AD_ACC_INT2_DURATION 0x37
  139. #define LSM303DLHC_AD_ACC_CLICK_CFG 0x38
  140. #define LSM303DLHC_AD_ACC_CLICK_SRC 0x39
  141. #define LSM303DLHC_AD_ACC_CLICK_THS 0x3A
  142. #define LSM303DLHC_AD_ACC_TIME_LIMIT 0x3B
  143. #define LSM303DLHC_AD_ACC_TIME_LATENCY 0x3C
  144. #define LSM303DLHC_AD_ACC_TIME_WINDOW 0x3D
  145. #define LSM303DLHC_AD_COMP_CRA_REG 0x00
  146. #define LSM303DLHC_AD_COMP_CRB_REG 0x01
  147. #define LSM303DLHC_AD_COMP_MR_REG 0x02
  148. #define LSM303DLHC_AD_COMP_OUT_X_H 0x03
  149. #define LSM303DLHC_AD_COMP_OUT_X_L 0x04
  150. #define LSM303DLHC_AD_COMP_OUT_Z_H 0x05
  151. #define LSM303DLHC_AD_COMP_OUT_Z_L 0x06
  152. #define LSM303DLHC_AD_COMP_OUT_Y_H 0x07
  153. #define LSM303DLHC_AD_COMP_OUT_Y_L 0x08
  154. #define LSM303DLHC_AD_COMP_SR_REG 0x09
  155. #define LSM303DLHC_AD_COMP_IRA_REG 0x0A
  156. #define LSM303DLHC_AD_COMP_IRB_REG 0x0B
  157. #define LSM303DLHC_AD_COMP_IRC_REG 0x0C
  158. #define LSM303DLHC_AD_COMP_TEMP_OUT_H 0x31
  159. #define LSM303DLHC_AD_COMP_TEMP_OUT_L 0x32
  160. /** @} */
  161. /**
  162. * @name LSM303DLHC_CTRL_REG1_A register bits definitions
  163. * @{
  164. */
  165. #define LSM303DLHC_CTRL_REG1_A_MASK 0xFF
  166. #define LSM303DLHC_CTRL_REG1_A_XEN (1 << 0)
  167. #define LSM303DLHC_CTRL_REG1_A_YEN (1 << 1)
  168. #define LSM303DLHC_CTRL_REG1_A_ZEN (1 << 2)
  169. #define LSM303DLHC_CTRL_REG1_A_LPEN (1 << 3)
  170. #define LSM303DLHC_CTRL_REG1_A_ODR0 (1 << 4)
  171. #define LSM303DLHC_CTRL_REG1_A_ODR1 (1 << 5)
  172. #define LSM303DLHC_CTRL_REG1_A_ODR2 (1 << 6)
  173. #define LSM303DLHC_CTRL_REG1_A_ODR3 (1 << 7)
  174. /** @} */
  175. /**
  176. * @name LSM303DLHC_CTRL_REG2_A register bits definitions
  177. * @{
  178. */
  179. #define LSM303DLHC_CTRL_REG2_A_MASK 0xFF
  180. #define LSM303DLHC_CTRL_REG2_A_HPIS1 (1 << 0)
  181. #define LSM303DLHC_CTRL_REG2_A_HPIS2 (1 << 1)
  182. #define LSM303DLHC_CTRL_REG2_A_HPCLICK (1 << 2)
  183. #define LSM303DLHC_CTRL_REG2_A_FDS (1 << 3)
  184. #define LSM303DLHC_CTRL_REG2_A_HPCF1 (1 << 4)
  185. #define LSM303DLHC_CTRL_REG2_A_HPCF2 (1 << 5)
  186. #define LSM303DLHC_CTRL_REG2_A_HPM0 (1 << 6)
  187. #define LSM303DLHC_CTRL_REG2_A_HPM1 (1 << 7)
  188. /** @} */
  189. /**
  190. * @name LSM303DLHC_CTRL_REG3_A register bits definitions
  191. * @{
  192. */
  193. #define LSM303DLHC_CTRL_REG3_A_MASK 0xFD
  194. #define LSM303DLHC_CTRL_REG3_A_I1_OVERRUN (1 << 1)
  195. #define LSM303DLHC_CTRL_REG3_A_I1_WTM (1 << 2)
  196. #define LSM303DLHC_CTRL_REG3_A_I1_DRDY2 (1 << 3)
  197. #define LSM303DLHC_CTRL_REG3_A_I1_DRDY1 (1 << 4)
  198. #define LSM303DLHC_CTRL_REG3_A_I1_AOI2 (1 << 5)
  199. #define LSM303DLHC_CTRL_REG3_A_I1_AOI1 (1 << 6)
  200. #define LSM303DLHC_CTRL_REG3_A_I1_CLICK (1 << 7)
  201. /** @} */
  202. /**
  203. * @name LSM303DLHC_CTRL_REG4_A register bits definitions
  204. * @{
  205. */
  206. #define LSM303DLHC_CTRL_REG4_A_MASK 0xF9
  207. #define LSM303DLHC_CTRL_REG4_A_SIM (1 << 0)
  208. #define LSM303DLHC_CTRL_REG4_A_HR (1 << 3)
  209. #define LSM303DLHC_CTRL_REG4_A_FS_MASK 0x30
  210. #define LSM303DLHC_CTRL_REG4_A_FS0 (1 << 4)
  211. #define LSM303DLHC_CTRL_REG4_A_FS1 (1 << 5)
  212. #define LSM303DLHC_CTRL_REG4_A_BLE (1 << 6)
  213. #define LSM303DLHC_CTRL_REG4_A_BDU (1 << 7)
  214. /** @} */
  215. /**
  216. * @name LSM303DLHC_CTRL_REG5_A register bits definitions
  217. * @{
  218. */
  219. #define LSM303DLHC_CTRL_REG5_A_MASK 0xCF
  220. #define LSM303DLHC_CTRL_REG5_A_D4D_INT2 (1 << 0)
  221. #define LSM303DLHC_CTRL_REG5_A_LIR_INT2 (1 << 1)
  222. #define LSM303DLHC_CTRL_REG5_A_D4D_INT1 (1 << 2)
  223. #define LSM303DLHC_CTRL_REG5_A_LIR_INT1 (1 << 3)
  224. #define LSM303DLHC_CTRL_REG5_A_FIFO_EN (1 << 6)
  225. #define LSM303DLHC_CTRL_REG5_A_BOOT (1 << 7)
  226. /** @} */
  227. /**
  228. * @name LSM303DLHC_CTRL_REG6_A register bits definitions
  229. * @{
  230. */
  231. #define LSM303DLHC_CTRL_REG6_A_MASK 0xFA
  232. #define LSM303DLHC_CTRL_REG6_A_H_LACTIVE (1 << 1)
  233. #define LSM303DLHC_CTRL_REG6_A_P2_ACT (1 << 3)
  234. #define LSM303DLHC_CTRL_REG6_A_BOOT_I1 (1 << 4)
  235. #define LSM303DLHC_CTRL_REG6_A_I2_INT2 (1 << 5)
  236. #define LSM303DLHC_CTRL_REG6_A_I2_INT1 (1 << 6)
  237. #define LSM303DLHC_CTRL_REG6_A_I2_CLICKEN (1 << 7)
  238. /** @} */
  239. /**
  240. * @name LSM303DLHC_CRA_REG_M register bits definitions
  241. * @{
  242. */
  243. #define LSM303DLHC_CRA_REG_M_MASK 0x9C
  244. #define LSM303DLHC_CRA_REG_M_DO0 (1 << 2)
  245. #define LSM303DLHC_CRA_REG_M_DO1 (1 << 3)
  246. #define LSM303DLHC_CRA_REG_M_DO2 (1 << 4)
  247. #define LSM303DLHC_CRA_REG_M_TEMP_EN (1 << 7)
  248. /** @} */
  249. /**
  250. * @name LSM303DLHC_CRB_REG_M register bits definitions
  251. * @{
  252. */
  253. #define LSM303DLHC_CRB_REG_M_MASK 0xE0
  254. #define LSM303DLHC_CRB_REG_M_GN_MASK 0xE0
  255. #define LSM303DLHC_CRB_REG_M_GN0 (1 << 5)
  256. #define LSM303DLHC_CRB_REG_M_GN1 (1 << 6)
  257. #define LSM303DLHC_CRB_REG_M_GN2 (1 << 7)
  258. /**
  259. * @name LSM303DLHC_CRB_REG_M register bits definitions
  260. * @{
  261. */
  262. #define LSM303DLHC_MR_REG_M_MASK 0x03
  263. #define LSM303DLHC_MR_REG_M_MD0 (1 << 0)
  264. #define LSM303DLHC_MR_REG_M_MD1 (1 << 1)
  265. /** @} */
  266. /*===========================================================================*/
  267. /* Driver pre-compile time settings. */
  268. /*===========================================================================*/
  269. /**
  270. * @name Configuration options
  271. * @{
  272. */
  273. /**
  274. * @brief LSM303DLHC SPI interface switch.
  275. * @details If set to @p TRUE the support for SPI is included.
  276. * @note The default is @p FALSE.
  277. */
  278. #if !defined(LSM303DLHC_USE_SPI) || defined(__DOXYGEN__)
  279. #define LSM303DLHC_USE_SPI FALSE
  280. #endif
  281. /**
  282. * @brief LSM303DLHC shared SPI switch.
  283. * @details If set to @p TRUE the device acquires SPI bus ownership
  284. * on each transaction.
  285. * @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION.
  286. */
  287. #if !defined(LSM303DLHC_SHARED_SPI) || defined(__DOXYGEN__)
  288. #define LSM303DLHC_SHARED_SPI FALSE
  289. #endif
  290. /**
  291. * @brief LSM303DLHC I2C interface switch.
  292. * @details If set to @p TRUE the support for I2C is included.
  293. * @note The default is @p TRUE.
  294. */
  295. #if !defined(LSM303DLHC_USE_I2C) || defined(__DOXYGEN__)
  296. #define LSM303DLHC_USE_I2C TRUE
  297. #endif
  298. /**
  299. * @brief LSM303DLHC shared I2C switch.
  300. * @details If set to @p TRUE the device acquires I2C bus ownership
  301. * on each transaction.
  302. * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
  303. */
  304. #if !defined(LSM303DLHC_SHARED_I2C) || defined(__DOXYGEN__)
  305. #define LSM303DLHC_SHARED_I2C FALSE
  306. #endif
  307. /**
  308. * @brief LSM303DLHC advanced configurations switch.
  309. * @details If set to @p TRUE more configurations are available.
  310. * @note The default is @p FALSE.
  311. */
  312. #if !defined(LSM303DLHC_USE_ADVANCED) || defined(__DOXYGEN__)
  313. #define LSM303DLHC_USE_ADVANCED FALSE
  314. #endif
  315. /** @} */
  316. /*===========================================================================*/
  317. /* Derived constants and error checks. */
  318. /*===========================================================================*/
  319. #if !(LSM303DLHC_USE_SPI ^ LSM303DLHC_USE_I2C)
  320. #error "LSM303DLHC_USE_SPI and LSM303DLHC_USE_I2C cannot be both true or both false"
  321. #endif
  322. #if LSM303DLHC_USE_SPI && !HAL_USE_SPI
  323. #error "LSM303DLHC_USE_SPI requires HAL_USE_SPI"
  324. #endif
  325. #if LSM303DLHC_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
  326. #error "LSM303DLHC_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
  327. #endif
  328. #if LSM303DLHC_USE_I2C && !HAL_USE_I2C
  329. #error "LSM303DLHC_USE_I2C requires HAL_USE_I2C"
  330. #endif
  331. #if LSM303DLHC_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
  332. #error "LSM303DLHC_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
  333. #endif
  334. /**
  335. * @todo Add support for LSM303DLHC over SPI.
  336. */
  337. #if LSM303DLHC_USE_SPI
  338. #error "LSM303DLHC over SPI still not supported"
  339. #endif
  340. /*===========================================================================*/
  341. /* Driver data structures and types. */
  342. /*===========================================================================*/
  343. /**
  344. * @name LSM303DLHC accelerometer subsystem data structures and types.
  345. * @{
  346. */
  347. /**
  348. * @brief Structure representing a LSM303DLHC driver.
  349. */
  350. typedef struct LSM303DLHCDriver LSM303DLHCDriver;
  351. /**
  352. * @brief LSM303DLHC accelerometer subsystem full scale.
  353. */
  354. typedef enum {
  355. LSM303DLHC_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
  356. LSM303DLHC_ACC_FS_4G = 0x10, /**< Full scale ±4g. */
  357. LSM303DLHC_ACC_FS_8G = 0x20, /**< Full scale ±8g. */
  358. LSM303DLHC_ACC_FS_16G = 0x30 /**< Full scale ±16g. */
  359. } lsm303dlhc_acc_fs_t;
  360. /**
  361. * @brief LSM303DLHC accelerometer subsystem output data rate.
  362. */
  363. typedef enum {
  364. LSM303DLHC_ACC_ODR_PD = 0x00, /**< Power down */
  365. LSM303DLHC_ACC_ODR_1Hz = 0x10, /**< ODR 1 Hz */
  366. LSM303DLHC_ACC_ODR_10Hz = 0x20, /**< ODR 10 Hz */
  367. LSM303DLHC_ACC_ODR_25Hz = 0x30, /**< ODR 25 Hz */
  368. LSM303DLHC_ACC_ODR_50Hz = 0x40, /**< ODR 50 Hz */
  369. LSM303DLHC_ACC_ODR_100Hz = 0x50, /**< ODR 100 Hz */
  370. LSM303DLHC_ACC_ODR_200Hz = 0x60, /**< ODR 200 Hz */
  371. LSM303DLHC_ACC_ODR_400Hz = 0x70, /**< ODR 400 Hz */
  372. LSM303DLHC_ACC_ODR_1620Hz = 0x80, /**< ODR 1620 Hz (LP only) */
  373. LSM303DLHC_ACC_ODR_1344Hz = 0x90 /**< ODR 1344 Hz or 5376 Hz in LP */
  374. } lsm303dlhc_acc_odr_t;
  375. /**
  376. * @brief LSM303DLHC accelerometer subsystem axes enabling.
  377. */
  378. typedef enum {
  379. LSM303DLHC_ACC_AE_DISABLED = 0x00,/**< All axes disabled. */
  380. LSM303DLHC_ACC_AE_X = 0x01, /**< Only X-axis enabled. */
  381. LSM303DLHC_ACC_AE_Y = 0x02, /**< Only Y-axis enabled. */
  382. LSM303DLHC_ACC_AE_XY = 0x03, /**< X and Y axes enabled. */
  383. LSM303DLHC_ACC_AE_Z = 0x04, /**< Only Z-axis enabled. */
  384. LSM303DLHC_ACC_AE_XZ = 0x05, /**< X and Z axes enabled. */
  385. LSM303DLHC_ACC_AE_YZ = 0x06, /**< Y and Z axes enabled. */
  386. LSM303DLHC_ACC_AE_XYZ = 0x07 /**< All axes enabled. */
  387. } lsm303dlhc_acc_ae_t;
  388. /**
  389. * @brief LSM303DLHC accelerometer subsystem low power mode.
  390. */
  391. typedef enum {
  392. LSM303DLHC_ACC_LP_DISABLED = 0x00,/**< Low power mode disabled. */
  393. LSM303DLHC_ACC_LP_ENABLED = 0x40 /**< Low power mode enabled. */
  394. } lsm303dlhc_acc_lp_t;
  395. /**
  396. * @brief LSM303DLHC accelerometer subsystem high resolution mode.
  397. */
  398. typedef enum {
  399. LSM303DLHC_ACC_HR_DISABLED = 0x00,/**< High resolution mode disabled. */
  400. LSM303DLHC_ACC_HR_ENABLED = 0x08 /**< High resolution mode enabled. */
  401. } lsm303dlhc_acc_hr_t;
  402. /**
  403. * @brief LSM303DLHC accelerometer subsystem block data update.
  404. */
  405. typedef enum {
  406. LSM303DLHC_ACC_BDU_CONT = 0x00, /**< Continuous update */
  407. LSM303DLHC_ACC_BDU_BLOCK = 0x80 /**< Update blocked */
  408. } lsm303dlhc_acc_bdu_t;
  409. /**
  410. * @brief LSM303DLHC accelerometer endianness.
  411. */
  412. typedef enum {
  413. LSM303DLHC_ACC_END_LITTLE = 0x00, /**< Little Endian */
  414. LSM303DLHC_ACC_END_BIG = 0x40 /**< Big Endian */
  415. } lsm303dlhc_acc_end_t;
  416. /**
  417. * @name LSM303DLHC compass subsystem data structures and types.
  418. * @{
  419. */
  420. /**
  421. * @brief LSM303DLHC compass subsystem full scale.
  422. */
  423. typedef enum {
  424. LSM303DLHC_COMP_FS_1P3GA = 0x20, /**< Full scale ±1.3 Gauss */
  425. LSM303DLHC_COMP_FS_1P9GA = 0x40, /**< Full scale ±1.9 Gauss */
  426. LSM303DLHC_COMP_FS_2P5GA = 0x60, /**< Full scale ±2.5 Gauss */
  427. LSM303DLHC_COMP_FS_4P0GA = 0x80, /**< Full scale ±4.0 Gauss */
  428. LSM303DLHC_COMP_FS_4P7GA = 0xA0, /**< Full scale ±4.7 Gauss */
  429. LSM303DLHC_COMP_FS_5P6GA = 0xC0, /**< Full scale ±5.6 Gauss */
  430. LSM303DLHC_COMP_FS_8P1GA = 0xE0 /**< Full scale ±8.1 Gauss */
  431. } lsm303dlhc_comp_fs_t;
  432. /**
  433. * @brief LSM303DLHC compass subsystem output data rate.
  434. */
  435. typedef enum {
  436. LSM303DLHC_COMP_ODR_0P75HZ = 0x00,/**< ODR 0.75 Hz */
  437. LSM303DLHC_COMP_ODR_1P5HZ = 0x04, /**< ODR 1.5 Hz */
  438. LSM303DLHC_COMP_ODR_3P0HZ = 0x08, /**< ODR 3 Hz */
  439. LSM303DLHC_COMP_ODR_7P5HZ = 0x0C, /**< ODR 7.5 Hz */
  440. LSM303DLHC_COMP_ODR_15HZ = 0x10, /**< ODR 15 Hz */
  441. LSM303DLHC_COMP_ODR_30HZ = 0x14, /**< ODR 30 Hz */
  442. LSM303DLHC_COMP_ODR_75HZ = 0x18, /**< ODR 75 Hz */
  443. LSM303DLHC_COMP_ODR_220HZ = 0x1C /**< ODR 220 Hz */
  444. } lsm303dlhc_comp_odr_t;
  445. /**
  446. * @brief LSM303DLHC compass subsystem working mode.
  447. */
  448. typedef enum {
  449. LSM303DLHC_COMP_MD_CONT = 0x00, /**< Continuous-Conversion Mode */
  450. LSM303DLHC_COMP_MD_BLOCK = 0x01, /**< Single-Conversion Mode */
  451. LSM303DLHC_COMP_MD_SLEEP = 0x02 /**< Sleep Mode */
  452. } lsm303dlhc_comp_md_t;
  453. /**
  454. * @name LSM303DLHC main system data structures and types.
  455. * @{
  456. */
  457. /**
  458. * @brief Driver state machine possible states.
  459. */
  460. typedef enum {
  461. LSM303DLHC_UNINIT = 0, /**< Not initialized. */
  462. LSM303DLHC_STOP = 1, /**< Stopped. */
  463. LSM303DLHC_READY = 2, /**< Ready. */
  464. } lsm303dlhc_state_t;
  465. /**
  466. * @brief LSM303DLHC configuration structure.
  467. */
  468. typedef struct {
  469. /**
  470. * @brief I2C driver associated to this LSM303DLHC.
  471. */
  472. I2CDriver *i2cp;
  473. /**
  474. * @brief I2C configuration associated to this LSM303DLHC.
  475. */
  476. const I2CConfig *i2ccfg;
  477. /**
  478. * @brief LSM303DLHC accelerometer subsystem initial sensitivity.
  479. */
  480. float *accsensitivity;
  481. /**
  482. * @brief LSM303DLHC accelerometer subsystem initial bias.
  483. */
  484. float *accbias;
  485. /**
  486. * @brief LSM303DLHC accelerometer subsystem initial full scale.
  487. */
  488. lsm303dlhc_acc_fs_t accfullscale;
  489. /**
  490. * @brief LSM303DLHC accelerometer subsystem output data rate.
  491. */
  492. lsm303dlhc_acc_odr_t accoutdatarate;
  493. #if LSM303DLHC_ACC_USE_ADVANCED || defined(__DOXYGEN__)
  494. /**
  495. * @brief LSM303DLHC accelerometer subsystem low power mode.
  496. */
  497. lsm303dlhc_acc_lp_t acclowpower;
  498. /**
  499. * @brief LSM303DLHC accelerometer subsystem high resolution mode.
  500. */
  501. lsm303dlhc_acc_hr_t acchighresmode;
  502. /**
  503. * @brief LSM303DLHC accelerometer subsystem block data update.
  504. */
  505. lsm303dlhc_acc_bdu_t accblockdataupdate;
  506. /**
  507. * @brief LSM303DLHC accelerometer endianness.
  508. */
  509. lsm303dlhc_acc_end_t accendianess;
  510. #endif
  511. /**
  512. * @brief LSM303DLHC compass initial sensitivity.
  513. */
  514. float *compsensitivity;
  515. /**
  516. * @brief LSM303DLHC compass initial bias.
  517. */
  518. float *compbias;
  519. /**
  520. * @brief LSM303DLHC compass subsystem initial full scale.
  521. */
  522. lsm303dlhc_comp_fs_t compfullscale;
  523. /**
  524. * @brief LSM303DLHC compass subsystem output data rate.
  525. */
  526. lsm303dlhc_comp_odr_t compoutputdatarate;
  527. #if LSM303DLHC_COMP_USE_ADVANCED || defined(__DOXYGEN__)
  528. /**
  529. * @brief LSM303DLHC compass subsystem working mode.
  530. */
  531. lsm303dlhc_comp_md_t compmode;
  532. #endif
  533. } LSM303DLHCConfig;
  534. /**
  535. * @brief @p LSM303DLHC specific methods.
  536. */
  537. #define _lsm303dlhc_methods_alone \
  538. /* Change full scale value of LSM303DLHC accelerometer subsystem.*/ \
  539. msg_t (*acc_set_full_scale)(LSM303DLHCDriver *devp, \
  540. lsm303dlhc_acc_fs_t fs); \
  541. /* Change full scale value of LSM303DLHC compass subsystem.*/ \
  542. msg_t (*comp_set_full_scale)(LSM303DLHCDriver *devp, \
  543. lsm303dlhc_comp_fs_t fs); \
  544. /**
  545. * @brief @p LSM303DLHC specific methods with inherited ones.
  546. */
  547. #define _lsm303dlhc_methods \
  548. _base_object_methods \
  549. _lsm303dlhc_methods_alone
  550. /**
  551. * @extends BaseObjectVMT
  552. *
  553. * @brief @p LSM303DLHC virtual methods table.
  554. */
  555. struct LSM303DLHCVMT {
  556. _lsm303dlhc_methods
  557. };
  558. /**
  559. * @brief @p LSM303DLHCDriver specific data.
  560. */
  561. #define _lsm303dlhc_data \
  562. _base_sensor_data \
  563. /* Driver state.*/ \
  564. lsm303dlhc_state_t state; \
  565. /* Current configuration data.*/ \
  566. const LSM303DLHCConfig *config; \
  567. /* Accelerometer subsystem axes number.*/ \
  568. size_t accaxes; \
  569. /* Accelerometer subsystem current sensitivity.*/ \
  570. float accsensitivity[LSM303DLHC_ACC_NUMBER_OF_AXES]; \
  571. /* Accelerometer subsystem current bias .*/ \
  572. float accbias[LSM303DLHC_ACC_NUMBER_OF_AXES]; \
  573. /* Accelerometer subsystem current full scale value.*/ \
  574. float accfullscale; \
  575. /* Compass subsystem axes number.*/ \
  576. size_t compaxes; \
  577. /* Compass subsystem current sensitivity.*/ \
  578. float compsensitivity[LSM303DLHC_COMP_NUMBER_OF_AXES];\
  579. /* Compass subsystem current bias.*/ \
  580. float compbias[LSM303DLHC_COMP_NUMBER_OF_AXES]; \
  581. /* Compass subsystem current full scale value.*/ \
  582. float compfullscale;
  583. /**
  584. * @brief LSM303DLHC 6-axis accelerometer/compass class.
  585. */
  586. struct LSM303DLHCDriver {
  587. /** @brief Virtual Methods Table.*/
  588. const struct LSM303DLHCVMT *vmt;
  589. /** @brief Base accelerometer interface.*/
  590. BaseAccelerometer acc_if;
  591. /** @brief Base compass interface.*/
  592. BaseCompass comp_if;
  593. _lsm303dlhc_data
  594. };
  595. /** @} */
  596. /*===========================================================================*/
  597. /* Driver macros. */
  598. /*===========================================================================*/
  599. /**
  600. * @brief Return the number of axes of the BaseAccelerometer.
  601. *
  602. * @param[in] devp pointer to @p LSM303DLHCDriver.
  603. *
  604. * @return the number of axes.
  605. *
  606. * @api
  607. */
  608. #define lsm303dlhcAccelerometerGetAxesNumber(devp) \
  609. accelerometerGetAxesNumber(&((devp)->acc_if))
  610. /**
  611. * @brief Retrieves raw data from the BaseAccelerometer.
  612. * @note This data is retrieved from MEMS register without any algebraical
  613. * manipulation.
  614. * @note The axes array must be at least the same size of the
  615. * BaseAccelerometer axes number.
  616. *
  617. * @param[in] devp pointer to @p LSM303DLHCDriver.
  618. * @param[out] axes a buffer which would be filled with raw data.
  619. *
  620. * @return The operation status.
  621. * @retval MSG_OK if the function succeeded.
  622. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  623. * be retrieved using @p i2cGetErrors().
  624. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  625. *
  626. * @api
  627. */
  628. #define lsm303dlhcAccelerometerReadRaw(devp, axes) \
  629. accelerometerReadRaw(&((devp)->acc_if), axes)
  630. /**
  631. * @brief Retrieves cooked data from the BaseAccelerometer.
  632. * @note This data is manipulated according to the formula
  633. * cooked = (raw * sensitivity) - bias.
  634. * @note Final data is expressed as milli-G.
  635. * @note The axes array must be at least the same size of the
  636. * BaseAccelerometer axes number.
  637. *
  638. * @param[in] devp pointer to @p LSM303DLHCDriver.
  639. * @param[out] axes a buffer which would be filled with cooked data.
  640. *
  641. * @return The operation status.
  642. * @retval MSG_OK if the function succeeded.
  643. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  644. * be retrieved using @p i2cGetErrors().
  645. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  646. *
  647. * @api
  648. */
  649. #define lsm303dlhcAccelerometerReadCooked(devp, axes) \
  650. accelerometerReadCooked(&((devp)->acc_if), axes)
  651. /**
  652. * @brief Set bias values for the BaseAccelerometer.
  653. * @note Bias must be expressed as milli-G.
  654. * @note The bias buffer must be at least the same size of the
  655. * BaseAccelerometer axes number.
  656. *
  657. * @param[in] devp pointer to @p LSM303DLHCDriver.
  658. * @param[in] bp a buffer which contains biases.
  659. *
  660. * @return The operation status.
  661. * @retval MSG_OK if the function succeeded.
  662. *
  663. * @api
  664. */
  665. #define lsm303dlhcAccelerometerSetBias(devp, bp) \
  666. accelerometerSetBias(&((devp)->acc_if), bp)
  667. /**
  668. * @brief Reset bias values for the BaseAccelerometer.
  669. * @note Default biases value are obtained from device datasheet when
  670. * available otherwise they are considered zero.
  671. *
  672. * @param[in] devp pointer to @p LSM303DLHCDriver.
  673. *
  674. * @return The operation status.
  675. * @retval MSG_OK if the function succeeded.
  676. *
  677. * @api
  678. */
  679. #define lsm303dlhcAccelerometerResetBias(devp) \
  680. accelerometerResetBias(&((devp)->acc_if))
  681. /**
  682. * @brief Set sensitivity values for the BaseAccelerometer.
  683. * @note Sensitivity must be expressed as milli-G/LSB.
  684. * @note The sensitivity buffer must be at least the same size of the
  685. * BaseAccelerometer axes number.
  686. *
  687. * @param[in] devp pointer to @p LSM303DLHCDriver.
  688. * @param[in] sp a buffer which contains sensitivities.
  689. *
  690. * @return The operation status.
  691. * @retval MSG_OK if the function succeeded.
  692. *
  693. * @api
  694. */
  695. #define lsm303dlhcAccelerometerSetSensitivity(devp, sp) \
  696. accelerometerSetSensitivity(&((devp)->acc_if), sp)
  697. /**
  698. * @brief Reset sensitivity values for the BaseAccelerometer.
  699. * @note Default sensitivities value are obtained from device datasheet.
  700. *
  701. * @param[in] devp pointer to @p LSM303DLHCDriver.
  702. *
  703. * @return The operation status.
  704. * @retval MSG_OK if the function succeeded.
  705. * @retval MSG_RESET otherwise.
  706. *
  707. * @api
  708. */
  709. #define lsm303dlhcAccelerometerResetSensitivity(devp) \
  710. accelerometerResetSensitivity(&((devp)->acc_if))
  711. /**
  712. * @brief Changes the LSM303DLHCDriver accelerometer fullscale value.
  713. * @note This function also rescale sensitivities and biases based on
  714. * previous and next fullscale value.
  715. * @note A recalibration is highly suggested after calling this function.
  716. *
  717. * @param[in] devp pointer to @p LSM303DLHCDriver.
  718. * @param[in] fs new fullscale value.
  719. *
  720. * @return The operation status.
  721. * @retval MSG_OK if the function succeeded.
  722. * @retval MSG_RESET otherwise.
  723. *
  724. * @api
  725. */
  726. #define lsm303dlhcAccelerometerSetFullScale(devp, fs) \
  727. (devp)->vmt->acc_set_full_scale(devp, fs)
  728. /**
  729. * @brief Return the number of axes of the BaseCompass.
  730. *
  731. * @param[in] devp pointer to @p LSM303DLHCDriver.
  732. *
  733. * @return the number of axes.
  734. *
  735. * @api
  736. */
  737. #define lsm303dlhcCompassGetAxesNumber(devp) \
  738. compassGetAxesNumber(&((devp)->comp_if))
  739. /**
  740. * @brief Retrieves raw data from the BaseCompass.
  741. * @note This data is retrieved from MEMS register without any algebraical
  742. * manipulation.
  743. * @note The axes array must be at least the same size of the
  744. * BaseCompass axes number.
  745. *
  746. * @param[in] devp pointer to @p BaseCompass interface.
  747. * @param[out] axes a buffer which would be filled with raw data.
  748. *
  749. * @return The operation status.
  750. * @retval MSG_OK if the function succeeded.
  751. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  752. * be retrieved using @p i2cGetErrors().
  753. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  754. *
  755. * @api
  756. */
  757. #define lsm303dlhcCompassReadRaw(devp, axes) \
  758. compassReadRaw(&((devp)->comp_if), axes)
  759. /**
  760. * @brief Retrieves cooked data from the BaseCompass.
  761. * @note This data is manipulated according to the formula
  762. * cooked = (raw * sensitivity) - bias.
  763. * @note Final data is expressed as G.
  764. * @note The axes array must be at least the same size of the
  765. * BaseCompass axes number.
  766. *
  767. * @param[in] devp pointer to @p BaseCompass interface.
  768. * @param[out] axes a buffer which would be filled with cooked data.
  769. *
  770. * @return The operation status.
  771. * @retval MSG_OK if the function succeeded.
  772. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  773. * be retrieved using @p i2cGetErrors().
  774. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  775. *
  776. * @api
  777. */
  778. #define lsm303dlhcCompassReadCooked(devp, axes) \
  779. compassReadCooked(&((devp)->comp_if), axes)
  780. /**
  781. * @brief Set bias values for the BaseCompass.
  782. * @note Bias must be expressed as G.
  783. * @note The bias buffer must be at least the same size of the
  784. * BaseCompass axes number.
  785. *
  786. * @param[in] devp pointer to @p BaseCompass interface.
  787. * @param[in] bp a buffer which contains biases.
  788. *
  789. * @return The operation status.
  790. * @retval MSG_OK if the function succeeded.
  791. *
  792. * @api
  793. */
  794. #define lsm303dlhcCompassSetBias(devp, bp) \
  795. compassSetBias(&((devp)->comp_if), bp)
  796. /**
  797. * @brief Reset bias values for the BaseCompass.
  798. * @note Default biases value are obtained from device datasheet when
  799. * available otherwise they are considered zero.
  800. *
  801. * @param[in] devp pointer to @p LSM303DLHCDriver.
  802. *
  803. * @return The operation status.
  804. * @retval MSG_OK if the function succeeded.
  805. *
  806. * @api
  807. */
  808. #define lsm303dlhcCompassResetBias(devp) \
  809. compassResetBias(&((devp)->comp_if))
  810. /**
  811. * @brief Set sensitivity values for the BaseCompass.
  812. * @note Sensitivity must be expressed as G/LSB.
  813. * @note The sensitivity buffer must be at least the same size of the
  814. * BaseCompass axes number.
  815. *
  816. * @param[in] devp pointer to @p LSM303DLHCDriver.
  817. * @param[in] sp a buffer which contains sensitivities.
  818. *
  819. * @return The operation status.
  820. * @retval MSG_OK if the function succeeded.
  821. *
  822. * @api
  823. */
  824. #define lsm303dlhcCompassSetSensitivity(devp, sp) \
  825. compassSetSensitivity(&((devp)->comp_if), sp)
  826. /**
  827. * @brief Reset sensitivity values for the BaseCompass.
  828. * @note Default sensitivities value are obtained from device datasheet.
  829. *
  830. * @param[in] devp pointer to @p LSM303DLHCDriver.
  831. *
  832. * @return The operation status.
  833. * @retval MSG_OK if the function succeeded.
  834. * @retval MSG_RESET otherwise.
  835. *
  836. * @api
  837. */
  838. #define lsm303dlhcCompassResetSensitivity(devp) \
  839. compassResetSensitivity(&((devp)->comp_if))
  840. /**
  841. * @brief Changes the LSM303DLHCDriver compass fullscale value.
  842. * @note This function also rescale sensitivities and biases based on
  843. * previous and next fullscale value.
  844. * @note A recalibration is highly suggested after calling this function.
  845. *
  846. * @param[in] devp pointer to @p LSM303DLHCDriver.
  847. * @param[in] fs new fullscale value.
  848. *
  849. * @return The operation status.
  850. * @retval MSG_OK if the function succeeded.
  851. * @retval MSG_RESET otherwise.
  852. *
  853. * @api
  854. */
  855. #define lsm303dlhcCompassSetFullScale(devp, fs) \
  856. (devp)->vmt->comp_set_full_scale(devp, fs)
  857. /*===========================================================================*/
  858. /* External declarations. */
  859. /*===========================================================================*/
  860. #ifdef __cplusplus
  861. extern "C" {
  862. #endif
  863. void lsm303dlhcObjectInit(LSM303DLHCDriver *devp);
  864. void lsm303dlhcStart(LSM303DLHCDriver *devp, const LSM303DLHCConfig *config);
  865. void lsm303dlhcStop(LSM303DLHCDriver *devp);
  866. #ifdef __cplusplus
  867. }
  868. #endif
  869. #endif /* _LSM303DLHC_H_ */
  870. /** @} */