lsm303agr.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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 lsm303agr.h
  17. * @brief LSM303AGR MEMS interface module header.
  18. *
  19. * @addtogroup LSM303AGR
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #ifndef _LSM303AGR_H_
  24. #define _LSM303AGR_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 LSM303AGR driver version string.
  36. */
  37. #define EX_LSM303AGR_VERSION "1.0.1"
  38. /**
  39. * @brief LSM303AGR driver version major number.
  40. */
  41. #define EX_LSM303AGR_MAJOR 1
  42. /**
  43. * @brief LSM303AGR driver version minor number.
  44. */
  45. #define EX_LSM303AGR_MINOR 0
  46. /**
  47. * @brief LSM303AGR driver version patch number.
  48. */
  49. #define EX_LSM303AGR_PATCH 1
  50. /** @} */
  51. /**
  52. * @brief LSM303AGR 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 LSM303AGR_ACC_NUMBER_OF_AXES 3U
  60. #define LSM303AGR_ACC_2G 2.0f
  61. #define LSM303AGR_ACC_4G 4.0f
  62. #define LSM303AGR_ACC_8G 8.0f
  63. #define LSM303AGR_ACC_16G 16.0f
  64. #define LSM303AGR_ACC_SENS_2G 0.060f
  65. #define LSM303AGR_ACC_SENS_4G 0.120f
  66. #define LSM303AGR_ACC_SENS_8G 0.240f
  67. #define LSM303AGR_ACC_SENS_16G 0.750f
  68. #define LSM303AGR_ACC_BIAS 0.0f
  69. /** @} */
  70. /**
  71. * @brief LSM303AGR 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 LSM303AGR_COMP_NUMBER_OF_AXES 3U
  78. #define LSM303AGR_COMP_50GA 50.0f
  79. #define LSM303AGR_COMP_SENS_50GA 0.00015f
  80. #define LSM303AGR_COMP_BIAS 0.0f
  81. /** @} */
  82. /**
  83. * @name LSM303AGR communication interfaces related bit masks
  84. * @{
  85. */
  86. #define LSM303AGR_DI_MASK 0xFF
  87. #define LSM303AGR_DI(n) (1 << n)
  88. #define LSM303AGR_AD_MASK 0x7F
  89. #define LSM303AGR_AD(n) (1 << n)
  90. #define LSM303AGR_MS (1 << 7)
  91. /** @} */
  92. /**
  93. * @name LSM303AGR register addresses
  94. * @{
  95. */
  96. #define LSM303AGR_AD_STATUS_REG_AUX_A 0x07
  97. #define LSM303AGR_AD_OUT_TEMP_L_A 0x0C
  98. #define LSM303AGR_AD_OUT_TEMP_H_A 0x0D
  99. #define LSM303AGR_AD_INT_COUNTER_REG_A 0x0E
  100. #define LSM303AGR_AD_WHO_AM_I_A 0x0F
  101. #define LSM303AGR_AD_TEMP_CFG_REG_A 0x1F
  102. #define LSM303AGR_AD_CTRL_REG1_A 0x20
  103. #define LSM303AGR_AD_CTRL_REG2_A 0x21
  104. #define LSM303AGR_AD_CTRL_REG3_A 0x22
  105. #define LSM303AGR_AD_CTRL_REG4_A 0x23
  106. #define LSM303AGR_AD_CTRL_REG5_A 0x24
  107. #define LSM303AGR_AD_CTRL_REG6_A 0x25
  108. #define LSM303AGR_AD_REFERENCE_A 0x26
  109. #define LSM303AGR_AD_STATUS_REG_A 0x27
  110. #define LSM303AGR_AD_OUT_X_L_A 0x28
  111. #define LSM303AGR_AD_OUT_X_H_A 0x29
  112. #define LSM303AGR_AD_OUT_Y_L_A 0x2A
  113. #define LSM303AGR_AD_OUT_Y_H_A 0x2B
  114. #define LSM303AGR_AD_OUT_Z_L_A 0x2C
  115. #define LSM303AGR_AD_OUT_Z_H_A 0x2D
  116. #define LSM303AGR_AD_FIFO_CTRL_REG_A 0x2E
  117. #define LSM303AGR_AD_FIFO_SRC_REG_A 0x2F
  118. #define LSM303AGR_AD_INT1_CFG_A 0x30
  119. #define LSM303AGR_AD_INT1_SRC_A 0x31
  120. #define LSM303AGR_AD_INT1_THS_A 0x32
  121. #define LSM303AGR_AD_INT1_DURATION_A 0x33
  122. #define LSM303AGR_AD_INT2_CFG_A 0x34
  123. #define LSM303AGR_AD_INT2_SRC_A 0x35
  124. #define LSM303AGR_AD_INT2_THS_A 0x36
  125. #define LSM303AGR_AD_INT2_DURATION_A 0x37
  126. #define LSM303AGR_AD_CLICK_CFG_A 0x38
  127. #define LSM303AGR_AD_CLICK_SRC_A 0x39
  128. #define LSM303AGR_AD_CLICK_THS_A 0x3A
  129. #define LSM303AGR_AD_TIME_LIMIT_A 0x3B
  130. #define LSM303AGR_AD_TIME_LATENCY_A 0x3C
  131. #define LSM303AGR_AD_TIME_WINDOW_A 0x3D
  132. #define LSM303AGR_AD_ACT_THS_A 0x3E
  133. #define LSM303AGR_AD_ACT_DUR_A 0x3F
  134. #define LSM303AGR_AD_OFFSET_X_REG_L_M 0x45
  135. #define LSM303AGR_AD_OFFSET_X_REG_H_M 0x46
  136. #define LSM303AGR_AD_OFFSET_Y_REG_L_M 0x47
  137. #define LSM303AGR_AD_OFFSET_Y_REG_H_M 0x48
  138. #define LSM303AGR_AD_OFFSET_Z_REG_L_M 0x49
  139. #define LSM303AGR_AD_OFFSET_Z_REG_H_M 0x4A
  140. #define LSM303AGR_AD_WHO_AM_I_M 0x4F
  141. #define LSM303AGR_AD_CFG_REG_A_M 0x60
  142. #define LSM303AGR_AD_CFG_REG_B_M 0x61
  143. #define LSM303AGR_AD_CFG_REG_C_M 0x62
  144. #define LSM303AGR_AD_INT_CRTL_REG_M 0x63
  145. #define LSM303AGR_AD_INT_SOURCE_REG_M 0x64
  146. #define LSM303AGR_AD_INT_THS_L_REG_M 0x65
  147. #define LSM303AGR_AD_INT_THS_H_REG_M 0x66
  148. #define LSM303AGR_AD_STATUS_REG_M 0x67
  149. #define LSM303AGR_AD_OUTX_L_REG_M 0x68
  150. #define LSM303AGR_AD_OUTX_H_REG_M 0x69
  151. #define LSM303AGR_AD_OUTY_L_REG_M 0x6A
  152. #define LSM303AGR_AD_OUTY_H_REG_M 0x6B
  153. #define LSM303AGR_AD_OUTZ_L_REG_M 0x6C
  154. #define LSM303AGR_AD_OUTZ_H_REG_M 0x6D
  155. /** @} */
  156. /**
  157. * @name LSM303AGR_TEMP_CFG_REG_A register bits definitions
  158. * @{
  159. */
  160. #define LSM303AGR_TEMP_CFG_REG_A_TEMP_EN0 (1 << 0)
  161. #define LSM303AGR_TEMP_CFG_REG_A_TEMP_EN1 (1 << 0)
  162. /** @} */
  163. /**
  164. * @name LSM303AGR_CTRL_REG1_A register bits definitions
  165. * @{
  166. */
  167. #define LSM303AGR_CTRL_REG1_A_XEN (1 << 0)
  168. #define LSM303AGR_CTRL_REG1_A_YEN (1 << 1)
  169. #define LSM303AGR_CTRL_REG1_A_ZEN (1 << 2)
  170. #define LSM303AGR_CTRL_REG1_A_LPEN (1 << 3)
  171. #define LSM303AGR_CTRL_REG1_A_ODR0 (1 << 4)
  172. #define LSM303AGR_CTRL_REG1_A_ODR1 (1 << 5)
  173. #define LSM303AGR_CTRL_REG1_A_ODR2 (1 << 6)
  174. #define LSM303AGR_CTRL_REG1_A_ODR3 (1 << 7)
  175. /** @} */
  176. /**
  177. * @name LSM303AGR_CTRL_REG2_A register bits definitions
  178. * @{
  179. */
  180. #define LSM303AGR_CTRL_REG2_A_HPIS1 (1 << 0)
  181. #define LSM303AGR_CTRL_REG2_A_HPIS2 (1 << 1)
  182. #define LSM303AGR_CTRL_REG2_A_HPCLICK (1 << 2)
  183. #define LSM303AGR_CTRL_REG2_A_FDS (1 << 3)
  184. #define LSM303AGR_CTRL_REG2_A_HPCF1 (1 << 4)
  185. #define LSM303AGR_CTRL_REG2_A_HPCF2 (1 << 5)
  186. #define LSM303AGR_CTRL_REG2_A_HPM0 (1 << 6)
  187. #define LSM303AGR_CTRL_REG2_A_HPM1 (1 << 7)
  188. /** @} */
  189. /**
  190. * @name LSM303AGR_CTRL_REG3_A register bits definitions
  191. * @{
  192. */
  193. #define LSM303AGR_CTRL_REG3_A_I1_OVERRUN (1 << 1)
  194. #define LSM303AGR_CTRL_REG3_A_I1_WTM (1 << 2)
  195. #define LSM303AGR_CTRL_REG3_A_I1_DRDY2 (1 << 3)
  196. #define LSM303AGR_CTRL_REG3_A_I1_DRDY1 (1 << 4)
  197. #define LSM303AGR_CTRL_REG3_A_I1_AOI2 (1 << 5)
  198. #define LSM303AGR_CTRL_REG3_A_I1_AOI1 (1 << 6)
  199. #define LSM303AGR_CTRL_REG3_A_I1_CLICK (1 << 7)
  200. /** @} */
  201. /**
  202. * @name LSM303AGR_CTRL_REG4_A register bits definitions
  203. * @{
  204. */
  205. #define LSM303AGR_CTRL_REG4_A_SPI_ENABLE (1 << 0)
  206. #define LSM303AGR_CTRL_REG4_A_ST0 (1 << 1)
  207. #define LSM303AGR_CTRL_REG4_A_ST1 (1 << 2)
  208. #define LSM303AGR_CTRL_REG4_A_HR (1 << 3)
  209. #define LSM303AGR_CTRL_REG4_A_FS_MASK 0x30
  210. #define LSM303AGR_CTRL_REG4_A_FS0 (1 << 4)
  211. #define LSM303AGR_CTRL_REG4_A_FS1 (1 << 5)
  212. #define LSM303AGR_CTRL_REG4_A_BLE (1 << 6)
  213. #define LSM303AGR_CTRL_REG4_A_BDU (1 << 7)
  214. /** @} */
  215. /**
  216. * @name LSM303AGR_CTRL_REG5_A register bits definitions
  217. * @{
  218. */
  219. #define LSM303AGR_CTRL_REG5_A_D4D_INT2 (1 << 0)
  220. #define LSM303AGR_CTRL_REG5_A_LIR_INT2 (1 << 1)
  221. #define LSM303AGR_CTRL_REG5_A_D4D_INT1 (1 << 2)
  222. #define LSM303AGR_CTRL_REG5_A_LIR_INT1 (1 << 3)
  223. #define LSM303AGR_CTRL_REG5_A_FIFO_EN (1 << 6)
  224. #define LSM303AGR_CTRL_REG5_A_BOOT (1 << 7)
  225. /** @} */
  226. /**
  227. * @name LSM303AGR_CTRL_REG6_A register bits definitions
  228. * @{
  229. */
  230. #define LSM303AGR_CTRL_REG6_A_H_LACTIVE (1 << 1)
  231. #define LSM303AGR_CTRL_REG6_A_P2_ACT (1 << 3)
  232. #define LSM303AGR_CTRL_REG6_A_BOOT_I2 (1 << 4)
  233. #define LSM303AGR_CTRL_REG6_A_I2_INT2 (1 << 5)
  234. #define LSM303AGR_CTRL_REG6_A_I2_INT1 (1 << 6)
  235. #define LSM303AGR_CTRL_REG6_A_I2_CLICKEN (1 << 7)
  236. /** @} */
  237. /**
  238. * @name LSM303AGR_CFG_REG_A register bits definitions
  239. * @{
  240. */
  241. #define LSM303AGR_CFG_REG_A_M_MD0 (1 << 0)
  242. #define LSM303AGR_CFG_REG_A_M_MD1 (1 << 1)
  243. #define LSM303AGR_CFG_REG_A_M_ODR0 (1 << 2)
  244. #define LSM303AGR_CFG_REG_A_M_ODR1 (1 << 3)
  245. #define LSM303AGR_CFG_REG_A_M_LP (1 << 4)
  246. #define LSM303AGR_CFG_REG_A_M_SOFT_RST (1 << 5)
  247. #define LSM303AGR_CFG_REG_A_M_REBOOT (1 << 6)
  248. #define LSM303AGR_CFG_REG_A_M_COMP_TEMP_EN (1 << 7)
  249. /** @} */
  250. /**
  251. * @name LSM303AGR_CFG_REG_B register bits definitions
  252. * @{
  253. */
  254. #define LSM303AGR_CFG_REG_B_M_LPF (1 << 0)
  255. #define LSM303AGR_CFG_REG_B_M_OFF_CANC (1 << 1)
  256. #define LSM303AGR_CFG_REG_B_M_SET_FREQ (1 << 2)
  257. #define LSM303AGR_CFG_REG_B_M_INT_ON (1 << 3)
  258. #define LSM303AGR_CFG_REG_B_M_OFF_CANC_OS (1 << 4)
  259. /** @} */
  260. /**
  261. * @name LSM303AGR_CFG_REG_C register bits definitions
  262. * @{
  263. */
  264. #define LSM303AGR_CFG_REG_C_M_INT_MAG (1 << 0)
  265. #define LSM303AGR_CFG_REG_C_M_SELF_TEST (1 << 1)
  266. #define LSM303AGR_CFG_REG_C_M_BLE (1 << 3)
  267. #define LSM303AGR_CFG_REG_C_M_BDU (1 << 4)
  268. #define LSM303AGR_CFG_REG_C_M_I2C_DIS (1 << 5)
  269. #define LSM303AGR_CFG_REG_C_M_INT_MAG_PIN (1 << 6)
  270. /** @} */
  271. /*===========================================================================*/
  272. /* Driver pre-compile time settings. */
  273. /*===========================================================================*/
  274. /**
  275. * @name Configuration options
  276. * @{
  277. */
  278. /**
  279. * @brief LSM303AGR SPI interface switch.
  280. * @details If set to @p TRUE the support for SPI is included.
  281. * @note The default is @p FALSE.
  282. */
  283. #if !defined(LSM303AGR_USE_SPI) || defined(__DOXYGEN__)
  284. #define LSM303AGR_USE_SPI FALSE
  285. #endif
  286. /**
  287. * @brief LSM303AGR shared SPI switch.
  288. * @details If set to @p TRUE the device acquires SPI bus ownership
  289. * on each transaction.
  290. * @note The default is @p FALSE. Requires SPI_USE_MUTUAL_EXCLUSION.
  291. */
  292. #if !defined(LSM303AGR_SHARED_SPI) || defined(__DOXYGEN__)
  293. #define LSM303AGR_SHARED_SPI FALSE
  294. #endif
  295. /**
  296. * @brief LSM303AGR I2C interface switch.
  297. * @details If set to @p TRUE the support for I2C is included.
  298. * @note The default is @p TRUE.
  299. */
  300. #if !defined(LSM303AGR_USE_I2C) || defined(__DOXYGEN__)
  301. #define LSM303AGR_USE_I2C TRUE
  302. #endif
  303. /**
  304. * @brief LSM303AGR shared I2C switch.
  305. * @details If set to @p TRUE the device acquires I2C bus ownership
  306. * on each transaction.
  307. * @note The default is @p FALSE. Requires I2C_USE_MUTUAL_EXCLUSION.
  308. */
  309. #if !defined(LSM303AGR_SHARED_I2C) || defined(__DOXYGEN__)
  310. #define LSM303AGR_SHARED_I2C FALSE
  311. #endif
  312. /**
  313. * @brief LSM303AGR advanced configurations switch.
  314. * @details If set to @p TRUE more configurations are available.
  315. * @note The default is @p FALSE.
  316. */
  317. #if !defined(LSM303AGR_USE_ADVANCED) || defined(__DOXYGEN__)
  318. #define LSM303AGR_USE_ADVANCED FALSE
  319. #endif
  320. /** @} */
  321. /*===========================================================================*/
  322. /* Derived constants and error checks. */
  323. /*===========================================================================*/
  324. #if !(LSM303AGR_USE_SPI ^ LSM303AGR_USE_I2C)
  325. #error "LSM303AGR_USE_SPI and LSM303AGR_USE_I2C cannot be both true or both false"
  326. #endif
  327. #if LSM303AGR_USE_SPI && !HAL_USE_SPI
  328. #error "LSM303AGR_USE_SPI requires HAL_USE_SPI"
  329. #endif
  330. #if LSM303AGR_SHARED_SPI && !SPI_USE_MUTUAL_EXCLUSION
  331. #error "LSM303AGR_SHARED_SPI requires SPI_USE_MUTUAL_EXCLUSION"
  332. #endif
  333. #if LSM303AGR_USE_I2C && !HAL_USE_I2C
  334. #error "LSM303AGR_USE_I2C requires HAL_USE_I2C"
  335. #endif
  336. #if LSM303AGR_SHARED_I2C && !I2C_USE_MUTUAL_EXCLUSION
  337. #error "LSM303AGR_SHARED_I2C requires I2C_USE_MUTUAL_EXCLUSION"
  338. #endif
  339. /**
  340. * @todo Add support for LSM303AGR over SPI.
  341. */
  342. #if LSM303AGR_USE_SPI
  343. #error "LSM303AGR over SPI still not supported"
  344. #endif
  345. /*===========================================================================*/
  346. /* Driver data structures and types. */
  347. /*===========================================================================*/
  348. /**
  349. * @name LSM303AGR accelerometer subsystem data structures and types.
  350. * @{
  351. */
  352. /**
  353. * @brief Structure representing a LSM303AGR driver.
  354. */
  355. typedef struct LSM303AGRDriver LSM303AGRDriver;
  356. /**
  357. * @brief LSM303AGR accelerometer subsystem full scale.
  358. */
  359. typedef enum {
  360. LSM303AGR_ACC_FS_2G = 0x00, /**< Full scale ±2g. */
  361. LSM303AGR_ACC_FS_4G = 0x10, /**< Full scale ±4g. */
  362. LSM303AGR_ACC_FS_8G = 0x20, /**< Full scale ±8g. */
  363. LSM303AGR_ACC_FS_16G = 0x30 /**< Full scale ±16g. */
  364. } lsm303agr_acc_fs_t;
  365. /**
  366. * @brief LSM303AGR accelerometer subsystem output data rate.
  367. */
  368. typedef enum {
  369. LSM303AGR_ACC_ODR_PD = 0x00, /**< Power down */
  370. LSM303AGR_ACC_ODR_1Hz = 0x10, /**< ODR 1 Hz */
  371. LSM303AGR_ACC_ODR_10Hz = 0x20, /**< ODR 10 Hz */
  372. LSM303AGR_ACC_ODR_25Hz = 0x30, /**< ODR 25 Hz */
  373. LSM303AGR_ACC_ODR_50Hz = 0x40, /**< ODR 50 Hz */
  374. LSM303AGR_ACC_ODR_100Hz = 0x50, /**< ODR 100 Hz */
  375. LSM303AGR_ACC_ODR_200Hz = 0x60, /**< ODR 200 Hz */
  376. LSM303AGR_ACC_ODR_400Hz = 0x70, /**< ODR 400 Hz */
  377. LSM303AGR_ACC_ODR_1620Hz = 0x80, /**< ODR 1620 Hz (LP only) */
  378. LSM303AGR_ACC_ODR_1344Hz = 0x90 /**< ODR 1344 Hz or 5376 Hz in LP */
  379. } lsm303agr_acc_odr_t;
  380. /**
  381. * @brief LSM303AGR accelerometer subsystem axes enabling.
  382. */
  383. typedef enum {
  384. LSM303AGR_ACC_AE_DISABLED = 0x00,/**< All axes disabled. */
  385. LSM303AGR_ACC_AE_X = 0x01, /**< Only X-axis enabled. */
  386. LSM303AGR_ACC_AE_Y = 0x02, /**< Only Y-axis enabled. */
  387. LSM303AGR_ACC_AE_XY = 0x03, /**< X and Y axes enabled. */
  388. LSM303AGR_ACC_AE_Z = 0x04, /**< Only Z-axis enabled. */
  389. LSM303AGR_ACC_AE_XZ = 0x05, /**< X and Z axes enabled. */
  390. LSM303AGR_ACC_AE_YZ = 0x06, /**< Y and Z axes enabled. */
  391. LSM303AGR_ACC_AE_XYZ = 0x07 /**< All axes enabled. */
  392. } lsm303agr_acc_ae_t;
  393. /**
  394. * @brief LSM303AGR accelerometer subsystem operation mode.
  395. */
  396. typedef enum {
  397. LSM303AGR_ACC_MODE_NORM = 0, /**< Normal mode. */
  398. LSM303AGR_ACC_MODE_LPOW = 1, /**< Low power mode. */
  399. LSM303AGR_ACC_MODE_HRES = 2 /**< High resolution mode. */
  400. } lsm303agr_acc_mode_t;
  401. /**
  402. * @brief LSM303AGR accelerometer subsystem block data update.
  403. */
  404. typedef enum {
  405. LSM303AGR_ACC_BDU_CONT = 0x00, /**< Continuous update */
  406. LSM303AGR_ACC_BDU_BLOCK = 0x80 /**< Update blocked */
  407. } lsm303agr_acc_bdu_t;
  408. /**
  409. * @brief LSM303AGR accelerometer endianness.
  410. */
  411. typedef enum {
  412. LSM303AGR_ACC_END_LITTLE = 0x00, /**< Little Endian */
  413. LSM303AGR_ACC_END_BIG = 0x40 /**< Big Endian */
  414. } lsm303agr_acc_end_t;
  415. /**
  416. * @name LSM303AGR compass subsystem data structures and types.
  417. * @{
  418. */
  419. /**
  420. * @brief LSM303AGR compass subsystem output data rate.
  421. */
  422. typedef enum {
  423. LSM303AGR_COMP_ODR_10HZ = 0x00, /**< ODR 10 Hz */
  424. LSM303AGR_COMP_ODR_20HZ = 0x04, /**< ODR 20 Hz */
  425. LSM303AGR_COMP_ODR_50HZ = 0x08, /**< ODR 50 Hz */
  426. LSM303AGR_COMP_ODR_100HZ = 0x0C /**< ODR 100 Hz */
  427. } lsm303agr_comp_odr_t;
  428. /**
  429. * @brief LSM303AGR compass subsystem working mode.
  430. */
  431. typedef enum {
  432. LSM303AGR_COMP_MODE_NORM = 0x00, /**< Continuous-Conversion Mode */
  433. LSM303AGR_COMP_MODE_SINGLE = 0x01,/**< Single-Conversion Mode */
  434. LSM303AGR_COMP_MODE_IDLE = 0x02 /**< Sleep Mode */
  435. } lsm303agr_comp_mode_t;
  436. /**
  437. * @brief LSM303AGR compass subsystem working mode.
  438. */
  439. typedef enum {
  440. LSM303AGR_COMP_LPOW_DIS = 0x00, /**< High Resolution Mode */
  441. LSM303AGR_COMP_LPOW_EN = 0x10 /**< Low Power Mode */
  442. } lsm303agr_comp_lpow_t;
  443. /**
  444. * @name LSM303AGR main system data structures and types.
  445. * @{
  446. */
  447. /**
  448. * @brief Driver state machine possible states.
  449. */
  450. typedef enum {
  451. LSM303AGR_UNINIT = 0, /**< Not initialized. */
  452. LSM303AGR_STOP = 1, /**< Stopped. */
  453. LSM303AGR_READY = 2, /**< Ready. */
  454. } lsm303agr_state_t;
  455. /**
  456. * @brief LSM303AGR configuration structure.
  457. */
  458. typedef struct {
  459. /**
  460. * @brief I2C driver associated to this LSM303AGR.
  461. */
  462. I2CDriver *i2cp;
  463. /**
  464. * @brief I2C configuration associated to this LSM303AGR.
  465. */
  466. const I2CConfig *i2ccfg;
  467. /**
  468. * @brief LSM303AGR accelerometer subsystem initial sensitivity.
  469. */
  470. float *accsensitivity;
  471. /**
  472. * @brief LSM303AGR accelerometer subsystem initial bias.
  473. */
  474. float *accbias;
  475. /**
  476. * @brief LSM303AGR accelerometer subsystem initial full scale.
  477. */
  478. lsm303agr_acc_fs_t accfullscale;
  479. /**
  480. * @brief LSM303AGR accelerometer subsystem output data rate.
  481. */
  482. lsm303agr_acc_odr_t accoutdatarate;
  483. #if LSM303AGR_USE_ADVANCED || defined(__DOXYGEN__)
  484. /**
  485. * @brief LSM303AGR accelerometer subsystem mode.
  486. */
  487. lsm303agr_acc_mode_t accmode;
  488. /**
  489. * @brief LSM303AGR accelerometer subsystem block data update.
  490. */
  491. lsm303agr_acc_bdu_t accblockdataupdate;
  492. /**
  493. * @brief LSM303AGR accelerometer endianness.
  494. */
  495. lsm303agr_acc_end_t accendianess;
  496. #endif
  497. /**
  498. * @brief LSM303AGR compass initial sensitivity.
  499. */
  500. float *compsensitivity;
  501. /**
  502. * @brief LSM303AGR compass initial bias.
  503. */
  504. float *compbias;
  505. /**
  506. * @brief LSM303AGR compass subsystem output data rate.
  507. */
  508. lsm303agr_comp_odr_t compoutputdatarate;
  509. #if LSM303AGR_USE_ADVANCED || defined(__DOXYGEN__)
  510. /**
  511. * @brief LSM303AGR compass subsystem working mode.
  512. */
  513. lsm303agr_comp_mode_t compmode;
  514. /**
  515. * @brief LSM303AGR compass subsystem lowpower mode.
  516. */
  517. lsm303agr_comp_lpow_t complp;
  518. #endif
  519. } LSM303AGRConfig;
  520. /**
  521. * @brief @p LSM303AGR specific methods.
  522. */
  523. #define _lsm303agr_methods_alone \
  524. /* Change full scale value of LSM303AGR accelerometer subsystem.*/ \
  525. msg_t (*acc_set_full_scale)(LSM303AGRDriver *devp, \
  526. lsm303agr_acc_fs_t fs);
  527. /**
  528. * @brief @p LSM303AGR specific methods with inherited ones.
  529. */
  530. #define _lsm303agr_methods \
  531. _base_object_methods \
  532. _lsm303agr_methods_alone
  533. /**
  534. * @extends BaseObjectVMT
  535. *
  536. * @brief @p LSM303AGR virtual methods table.
  537. */
  538. struct LSM303AGRVMT {
  539. _lsm303agr_methods
  540. };
  541. /**
  542. * @brief @p LSM303AGRDriver specific data.
  543. */
  544. #define _lsm303agr_data \
  545. _base_sensor_data \
  546. /* Driver state.*/ \
  547. lsm303agr_state_t state; \
  548. /* Current configuration data.*/ \
  549. const LSM303AGRConfig *config; \
  550. /* Accelerometer subsystem axes number.*/ \
  551. size_t accaxes; \
  552. /* Accelerometer subsystem current sensitivity.*/ \
  553. float accsensitivity[LSM303AGR_ACC_NUMBER_OF_AXES]; \
  554. /* Accelerometer subsystem current bias .*/ \
  555. float accbias[LSM303AGR_ACC_NUMBER_OF_AXES]; \
  556. /* Accelerometer subsystem current full scale value.*/ \
  557. float accfullscale; \
  558. /* Compass subsystem axes number.*/ \
  559. size_t compaxes; \
  560. /* Compass subsystem current sensitivity.*/ \
  561. float compsensitivity[LSM303AGR_COMP_NUMBER_OF_AXES]; \
  562. /* Compass subsystem current bias.*/ \
  563. float compbias[LSM303AGR_COMP_NUMBER_OF_AXES]; \
  564. /* Compass subsystem current full scale value.*/ \
  565. float compfullscale;
  566. /**
  567. * @brief LSM303AGR 6-axis accelerometer/compass class.
  568. */
  569. struct LSM303AGRDriver {
  570. /** @brief Virtual Methods Table.*/
  571. const struct LSM303AGRVMT *vmt;
  572. /** @brief Base accelerometer interface.*/
  573. BaseAccelerometer acc_if;
  574. /** @brief Base compass interface.*/
  575. BaseCompass comp_if;
  576. _lsm303agr_data
  577. };
  578. /** @} */
  579. /*===========================================================================*/
  580. /* Driver macros. */
  581. /*===========================================================================*/
  582. /**
  583. * @brief Return the number of axes of the BaseAccelerometer.
  584. *
  585. * @param[in] devp pointer to @p LSM303AGRDriver.
  586. *
  587. * @return the number of axes.
  588. *
  589. * @api
  590. */
  591. #define lsm303agrAccelerometerGetAxesNumber(devp) \
  592. accelerometerGetAxesNumber(&((devp)->acc_if))
  593. /**
  594. * @brief Retrieves raw data from the BaseAccelerometer.
  595. * @note This data is retrieved from MEMS register without any algebraical
  596. * manipulation.
  597. * @note The axes array must be at least the same size of the
  598. * BaseAccelerometer axes number.
  599. *
  600. * @param[in] devp pointer to @p LSM303AGRDriver.
  601. * @param[out] axes a buffer which would be filled with raw data.
  602. *
  603. * @return The operation status.
  604. * @retval MSG_OK if the function succeeded.
  605. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  606. * be retrieved using @p i2cGetErrors().
  607. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  608. *
  609. * @api
  610. */
  611. #define lsm303agrAccelerometerReadRaw(devp, axes) \
  612. accelerometerReadRaw(&((devp)->acc_if), axes)
  613. /**
  614. * @brief Retrieves cooked data from the BaseAccelerometer.
  615. * @note This data is manipulated according to the formula
  616. * cooked = (raw * sensitivity) - bias.
  617. * @note Final data is expressed as milli-G.
  618. * @note The axes array must be at least the same size of the
  619. * BaseAccelerometer axes number.
  620. *
  621. * @param[in] devp pointer to @p LSM303AGRDriver.
  622. * @param[out] axes a buffer which would be filled with cooked data.
  623. *
  624. * @return The operation status.
  625. * @retval MSG_OK if the function succeeded.
  626. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  627. * be retrieved using @p i2cGetErrors().
  628. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  629. *
  630. * @api
  631. */
  632. #define lsm303agrAccelerometerReadCooked(devp, axes) \
  633. accelerometerReadCooked(&((devp)->acc_if), axes)
  634. /**
  635. * @brief Set bias values for the BaseAccelerometer.
  636. * @note Bias must be expressed as milli-G.
  637. * @note The bias buffer must be at least the same size of the
  638. * BaseAccelerometer axes number.
  639. *
  640. * @param[in] devp pointer to @p LSM303AGRDriver.
  641. * @param[in] bp a buffer which contains biases.
  642. *
  643. * @return The operation status.
  644. * @retval MSG_OK if the function succeeded.
  645. *
  646. * @api
  647. */
  648. #define lsm303agrAccelerometerSetBias(devp, bp) \
  649. accelerometerSetBias(&((devp)->acc_if), bp)
  650. /**
  651. * @brief Reset bias values for the BaseAccelerometer.
  652. * @note Default biases value are obtained from device datasheet when
  653. * available otherwise they are considered zero.
  654. *
  655. * @param[in] devp pointer to @p LSM303AGRDriver.
  656. *
  657. * @return The operation status.
  658. * @retval MSG_OK if the function succeeded.
  659. *
  660. * @api
  661. */
  662. #define lsm303agrAccelerometerResetBias(devp) \
  663. accelerometerResetBias(&((devp)->acc_if))
  664. /**
  665. * @brief Set sensitivity values for the BaseAccelerometer.
  666. * @note Sensitivity must be expressed as milli-G/LSB.
  667. * @note The sensitivity buffer must be at least the same size of the
  668. * BaseAccelerometer axes number.
  669. *
  670. * @param[in] devp pointer to @p LSM303AGRDriver.
  671. * @param[in] sp a buffer which contains sensitivities.
  672. *
  673. * @return The operation status.
  674. * @retval MSG_OK if the function succeeded.
  675. *
  676. * @api
  677. */
  678. #define lsm303agrAccelerometerSetSensitivity(devp, sp) \
  679. accelerometerSetSensitivity(&((devp)->acc_if), sp)
  680. /**
  681. * @brief Reset sensitivity values for the BaseAccelerometer.
  682. * @note Default sensitivities value are obtained from device datasheet.
  683. *
  684. * @param[in] devp pointer to @p LSM303AGRDriver.
  685. *
  686. * @return The operation status.
  687. * @retval MSG_OK if the function succeeded.
  688. * @retval MSG_RESET otherwise.
  689. *
  690. * @api
  691. */
  692. #define lsm303agrAccelerometerResetSensitivity(devp) \
  693. accelerometerResetSensitivity(&((devp)->acc_if))
  694. /**
  695. * @brief Changes the LSM303AGRDriver accelerometer fullscale value.
  696. * @note This function also rescale sensitivities and biases based on
  697. * previous and next fullscale value.
  698. * @note A recalibration is highly suggested after calling this function.
  699. *
  700. * @param[in] devp pointer to @p LSM303AGRDriver.
  701. * @param[in] fs new fullscale value.
  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 lsm303agrAccelerometerSetFullScale(devp, fs) \
  710. (devp)->vmt->acc_set_full_scale(devp, fs)
  711. /**
  712. * @brief Return the number of axes of the BaseCompass.
  713. *
  714. * @param[in] devp pointer to @p LSM303AGRDriver.
  715. *
  716. * @return the number of axes.
  717. *
  718. * @api
  719. */
  720. #define lsm303agrCompassGetAxesNumber(devp) \
  721. compassGetAxesNumber(&((devp)->comp_if))
  722. /**
  723. * @brief Retrieves raw data from the BaseCompass.
  724. * @note This data is retrieved from MEMS register without any algebraical
  725. * manipulation.
  726. * @note The axes array must be at least the same size of the
  727. * BaseCompass axes number.
  728. *
  729. * @param[in] devp pointer to @p BaseCompass interface.
  730. * @param[out] axes a buffer which would be filled with raw data.
  731. *
  732. * @return The operation status.
  733. * @retval MSG_OK if the function succeeded.
  734. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  735. * be retrieved using @p i2cGetErrors().
  736. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  737. *
  738. * @api
  739. */
  740. #define lsm303agrCompassReadRaw(devp, axes) \
  741. compassReadRaw(&((devp)->comp_if), axes)
  742. /**
  743. * @brief Retrieves cooked data from the BaseCompass.
  744. * @note This data is manipulated according to the formula
  745. * cooked = (raw * sensitivity) - bias.
  746. * @note Final data is expressed as G.
  747. * @note The axes array must be at least the same size of the
  748. * BaseCompass axes number.
  749. *
  750. * @param[in] devp pointer to @p BaseCompass interface.
  751. * @param[out] axes a buffer which would be filled with cooked data.
  752. *
  753. * @return The operation status.
  754. * @retval MSG_OK if the function succeeded.
  755. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  756. * be retrieved using @p i2cGetErrors().
  757. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  758. *
  759. * @api
  760. */
  761. #define lsm303agrCompassReadCooked(devp, axes) \
  762. compassReadCooked(&((devp)->comp_if), axes)
  763. /**
  764. * @brief Set bias values for the BaseCompass.
  765. * @note Bias must be expressed as G.
  766. * @note The bias buffer must be at least the same size of the
  767. * BaseCompass axes number.
  768. *
  769. * @param[in] devp pointer to @p BaseCompass interface.
  770. * @param[in] bp a buffer which contains biases.
  771. *
  772. * @return The operation status.
  773. * @retval MSG_OK if the function succeeded.
  774. *
  775. * @api
  776. */
  777. #define lsm303agrCompassSetBias(devp, bp) \
  778. compassSetBias(&((devp)->comp_if), bp)
  779. /**
  780. * @brief Reset bias values for the BaseCompass.
  781. * @note Default biases value are obtained from device datasheet when
  782. * available otherwise they are considered zero.
  783. *
  784. * @param[in] devp pointer to @p LSM303AGRDriver.
  785. *
  786. * @return The operation status.
  787. * @retval MSG_OK if the function succeeded.
  788. *
  789. * @api
  790. */
  791. #define lsm303agrCompassResetBias(devp) \
  792. compassResetBias(&((devp)->comp_if))
  793. /**
  794. * @brief Set sensitivity values for the BaseCompass.
  795. * @note Sensitivity must be expressed as G/LSB.
  796. * @note The sensitivity buffer must be at least the same size of the
  797. * BaseCompass axes number.
  798. *
  799. * @param[in] devp pointer to @p LSM303AGRDriver.
  800. * @param[in] sp a buffer which contains sensitivities.
  801. *
  802. * @return The operation status.
  803. * @retval MSG_OK if the function succeeded.
  804. *
  805. * @api
  806. */
  807. #define lsm303agrCompassSetSensitivity(devp, sp) \
  808. compassSetSensitivity(&((devp)->comp_if), sp)
  809. /**
  810. * @brief Reset sensitivity values for the BaseCompass.
  811. * @note Default sensitivities value are obtained from device datasheet.
  812. *
  813. * @param[in] devp pointer to @p LSM303AGRDriver.
  814. *
  815. * @return The operation status.
  816. * @retval MSG_OK if the function succeeded.
  817. * @retval MSG_RESET otherwise.
  818. *
  819. * @api
  820. */
  821. #define lsm303agrCompassResetSensitivity(devp) \
  822. compassResetSensitivity(&((devp)->comp_if))
  823. /*===========================================================================*/
  824. /* External declarations. */
  825. /*===========================================================================*/
  826. #ifdef __cplusplus
  827. extern "C" {
  828. #endif
  829. void lsm303agrObjectInit(LSM303AGRDriver *devp);
  830. void lsm303agrStart(LSM303AGRDriver *devp, const LSM303AGRConfig *config);
  831. void lsm303agrStop(LSM303AGRDriver *devp);
  832. #ifdef __cplusplus
  833. }
  834. #endif
  835. #endif /* _LSM303AGR_H_ */
  836. /** @} */