lsm303agr.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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.c
  17. * @brief LSM303AGR MEMS interface module code.
  18. *
  19. * @addtogroup LSM303AGR
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #include "hal.h"
  24. #include "lsm303agr.h"
  25. /*===========================================================================*/
  26. /* Driver local definitions. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Driver exported variables. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Driver local variables and types. */
  33. /*===========================================================================*/
  34. /**
  35. * @brief Accelerometer and Compass Slave Address.
  36. */
  37. typedef enum {
  38. LSM303AGR_SAD_ACC = 0x19, /**< SAD for accelerometer. */
  39. LSM303AGR_SAD_COMP = 0x1E /**< SAD for compass. */
  40. } lsm303agr_sad_t;
  41. /*===========================================================================*/
  42. /* Driver local functions. */
  43. /*===========================================================================*/
  44. /**
  45. * @brief Reads registers value using I2C.
  46. * @pre The I2C interface must be initialized and the driver started.
  47. * @note IF_ADD_INC bit must be 1 in CTRL_REG8.
  48. *
  49. * @param[in] i2cp pointer to the I2C interface.
  50. * @param[in] sad slave address without R bit.
  51. * @param[in] reg first sub-register address.
  52. * @param[in] rxbuf receiving buffer.
  53. * @param[in] n size of rxbuf.
  54. * @return the operation status.
  55. */
  56. static msg_t lsm303agrI2CReadRegister(I2CDriver *i2cp, lsm303agr_sad_t sad,
  57. uint8_t reg, uint8_t *rxbuf, size_t n) {
  58. uint8_t txbuf = reg | LSM303AGR_MS;
  59. return i2cMasterTransmitTimeout(i2cp, sad, &txbuf, 1, rxbuf, n,
  60. TIME_INFINITE);
  61. }
  62. /**
  63. * @brief Writes a value into a register using I2C.
  64. * @pre The I2C interface must be initialized and the driver started.
  65. *
  66. * @param[in] i2cp pointer to the I2C interface.
  67. * @param[in] sad slave address without R bit.
  68. * @param[in] txbuf buffer containing sub-address value in first position
  69. * and values to write.
  70. * @param[in] n size of txbuf less one (not considering the first
  71. * element).
  72. * @return the operation status.
  73. */
  74. static msg_t lsm303agrI2CWriteRegister(I2CDriver *i2cp, lsm303agr_sad_t sad,
  75. uint8_t *txbuf, size_t n) {
  76. if (n != 1)
  77. *txbuf |= LSM303AGR_MS;
  78. return i2cMasterTransmitTimeout(i2cp, sad, txbuf, n + 1, NULL, 0,
  79. TIME_INFINITE);
  80. }
  81. /**
  82. * @brief Return the number of axes of the BaseAccelerometer.
  83. *
  84. * @param[in] ip pointer to @p BaseAccelerometer interface.
  85. *
  86. * @return the number of axes.
  87. */
  88. static size_t acc_get_axes_number(void *ip) {
  89. (void)ip;
  90. return LSM303AGR_ACC_NUMBER_OF_AXES;
  91. }
  92. /**
  93. * @brief Retrieves raw data from the BaseAccelerometer.
  94. * @note This data is retrieved from MEMS register without any algebraical
  95. * manipulation.
  96. * @note The axes array must be at least the same size of the
  97. * BaseAccelerometer axes number.
  98. *
  99. * @param[in] ip pointer to @p BaseAccelerometer interface.
  100. * @param[out] axes a buffer which would be filled with raw data.
  101. *
  102. * @return The operation status.
  103. * @retval MSG_OK if the function succeeded.
  104. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  105. * be retrieved using @p i2cGetErrors().
  106. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  107. */
  108. static msg_t acc_read_raw(void *ip, int32_t axes[]) {
  109. LSM303AGRDriver* devp;
  110. uint8_t buff [LSM303AGR_ACC_NUMBER_OF_AXES * 2], i;
  111. int16_t tmp;
  112. msg_t msg;
  113. osalDbgCheck((ip != NULL) && (axes != NULL));
  114. /* Getting parent instance pointer.*/
  115. devp = objGetInstance(LSM303AGRDriver*, (BaseAccelerometer*)ip);
  116. osalDbgAssert((devp->state == LSM303AGR_READY),
  117. "acc_read_raw(), invalid state");
  118. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  119. "acc_read_raw(), channel not ready");
  120. #if LSM303AGR_SHARED_I2C
  121. i2cAcquireBus(devp->config->i2cp);
  122. i2cStart(devp->config->i2cp,
  123. devp->config->i2ccfg);
  124. #endif /* LSM303AGR_SHARED_I2C */
  125. msg = lsm303agrI2CReadRegister(devp->config->i2cp, LSM303AGR_SAD_ACC,
  126. LSM303AGR_AD_OUT_X_L_A, buff,
  127. LSM303AGR_ACC_NUMBER_OF_AXES * 2);
  128. #if LSM303AGR_SHARED_I2C
  129. i2cReleaseBus(devp->config->i2cp);
  130. #endif /* LSM303AGR_SHARED_I2C */
  131. if(msg == MSG_OK)
  132. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  133. tmp = buff[2 * i] + (buff[2 * i + 1] << 8);
  134. axes[i] = (int32_t)tmp;
  135. }
  136. return msg;
  137. }
  138. /**
  139. * @brief Retrieves cooked data from the BaseAccelerometer.
  140. * @note This data is manipulated according to the formula
  141. * cooked = (raw * sensitivity) - bias.
  142. * @note Final data is expressed as milli-G.
  143. * @note The axes array must be at least the same size of the
  144. * BaseAccelerometer axes number.
  145. *
  146. * @param[in] ip pointer to @p BaseAccelerometer interface.
  147. * @param[out] axes a buffer which would be filled with cooked data.
  148. *
  149. * @return The operation status.
  150. * @retval MSG_OK if the function succeeded.
  151. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  152. * be retrieved using @p i2cGetErrors().
  153. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  154. */
  155. static msg_t acc_read_cooked(void *ip, float axes[]) {
  156. LSM303AGRDriver* devp;
  157. uint32_t i;
  158. int32_t raw[LSM303AGR_ACC_NUMBER_OF_AXES];
  159. msg_t msg;
  160. osalDbgCheck((ip != NULL) && (axes != NULL));
  161. /* Getting parent instance pointer.*/
  162. devp = objGetInstance(LSM303AGRDriver*, (BaseAccelerometer*)ip);
  163. osalDbgAssert((devp->state == LSM303AGR_READY),
  164. "acc_read_cooked(), invalid state");
  165. msg = acc_read_raw(ip, raw);
  166. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  167. axes[i] = (raw[i] * devp->accsensitivity[i]) - devp->accbias[i];
  168. }
  169. return msg;
  170. }
  171. /**
  172. * @brief Set bias values for the BaseAccelerometer.
  173. * @note Bias must be expressed as milli-G.
  174. * @note The bias buffer must be at least the same size of the
  175. * BaseAccelerometer axes number.
  176. *
  177. * @param[in] ip pointer to @p BaseAccelerometer interface.
  178. * @param[in] bp a buffer which contains biases.
  179. *
  180. * @return The operation status.
  181. * @retval MSG_OK if the function succeeded.
  182. */
  183. static msg_t acc_set_bias(void *ip, float *bp) {
  184. LSM303AGRDriver* devp;
  185. uint32_t i;
  186. msg_t msg = MSG_OK;
  187. osalDbgCheck((ip != NULL) && (bp != NULL));
  188. /* Getting parent instance pointer.*/
  189. devp = objGetInstance(LSM303AGRDriver*, (BaseAccelerometer*)ip);
  190. osalDbgAssert((devp->state == LSM303AGR_READY),
  191. "acc_set_bias(), invalid state");
  192. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  193. devp->accbias[i] = bp[i];
  194. }
  195. return msg;
  196. }
  197. /**
  198. * @brief Reset bias values for the BaseAccelerometer.
  199. * @note Default biases value are obtained from device datasheet when
  200. * available otherwise they are considered zero.
  201. *
  202. * @param[in] ip pointer to @p BaseAccelerometer interface.
  203. *
  204. * @return The operation status.
  205. * @retval MSG_OK if the function succeeded.
  206. */
  207. static msg_t acc_reset_bias(void *ip) {
  208. LSM303AGRDriver* devp;
  209. uint32_t i;
  210. msg_t msg = MSG_OK;
  211. osalDbgCheck(ip != NULL);
  212. /* Getting parent instance pointer.*/
  213. devp = objGetInstance(LSM303AGRDriver*, (BaseAccelerometer*)ip);
  214. osalDbgAssert((devp->state == LSM303AGR_READY),
  215. "acc_reset_bias(), invalid state");
  216. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++)
  217. devp->accbias[i] = LSM303AGR_ACC_BIAS;
  218. return msg;
  219. }
  220. /**
  221. * @brief Set sensitivity values for the BaseAccelerometer.
  222. * @note Sensitivity must be expressed as milli-G/LSB.
  223. * @note The sensitivity buffer must be at least the same size of the
  224. * BaseAccelerometer axes number.
  225. *
  226. * @param[in] ip pointer to @p BaseAccelerometer interface.
  227. * @param[in] sp a buffer which contains sensitivities.
  228. *
  229. * @return The operation status.
  230. * @retval MSG_OK if the function succeeded.
  231. */
  232. static msg_t acc_set_sensivity(void *ip, float *sp) {
  233. LSM303AGRDriver* devp;
  234. uint32_t i;
  235. msg_t msg = MSG_OK;
  236. /* Getting parent instance pointer.*/
  237. devp = objGetInstance(LSM303AGRDriver*, (BaseAccelerometer*)ip);
  238. osalDbgCheck((ip != NULL) && (sp != NULL));
  239. osalDbgAssert((devp->state == LSM303AGR_READY),
  240. "acc_set_sensivity(), invalid state");
  241. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  242. devp->accsensitivity[i] = sp[i];
  243. }
  244. return msg;
  245. }
  246. /**
  247. * @brief Reset sensitivity values for the BaseAccelerometer.
  248. * @note Default sensitivities value are obtained from device datasheet.
  249. *
  250. * @param[in] ip pointer to @p BaseAccelerometer interface.
  251. *
  252. * @return The operation status.
  253. * @retval MSG_OK if the function succeeded.
  254. * @retval MSG_RESET otherwise.
  255. */
  256. static msg_t acc_reset_sensivity(void *ip) {
  257. LSM303AGRDriver* devp;
  258. uint32_t i;
  259. msg_t msg = MSG_OK;
  260. osalDbgCheck(ip != NULL);
  261. /* Getting parent instance pointer.*/
  262. devp = objGetInstance(LSM303AGRDriver*, (BaseAccelerometer*)ip);
  263. osalDbgAssert((devp->state == LSM303AGR_READY),
  264. "acc_reset_sensivity(), invalid state");
  265. if(devp->config->accfullscale == LSM303AGR_ACC_FS_2G) {
  266. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  267. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_2G;
  268. }
  269. }
  270. else if(devp->config->accfullscale == LSM303AGR_ACC_FS_4G) {
  271. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  272. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_4G;
  273. }
  274. }
  275. else if(devp->config->accfullscale == LSM303AGR_ACC_FS_8G) {
  276. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  277. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_8G;
  278. }
  279. }
  280. else if(devp->config->accfullscale == LSM303AGR_ACC_FS_16G) {
  281. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  282. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_16G;
  283. }
  284. }
  285. else {
  286. osalDbgAssert(FALSE, "reset_sensivity(), accelerometer full scale issue");
  287. msg = MSG_RESET;
  288. }
  289. return msg;
  290. }
  291. /**
  292. * @brief Changes the LSM303AGRDriver accelerometer fullscale value.
  293. * @note This function also rescale sensitivities and biases based on
  294. * previous and next fullscale value.
  295. * @note A recalibration is highly suggested after calling this function.
  296. *
  297. * @param[in] devp pointer to @p LSM303AGRDriver interface.
  298. * @param[in] fs new fullscale value.
  299. *
  300. * @return The operation status.
  301. * @retval MSG_OK if the function succeeded.
  302. * @retval MSG_RESET otherwise.
  303. */
  304. static msg_t acc_set_full_scale(LSM303AGRDriver *devp,
  305. lsm303agr_acc_fs_t fs) {
  306. float newfs, scale;
  307. uint8_t i, buff[2];
  308. msg_t msg;
  309. osalDbgCheck(devp != NULL);
  310. osalDbgAssert((devp->state == LSM303AGR_READY),
  311. "acc_set_full_scale(), invalid state");
  312. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  313. "acc_set_full_scale(), channel not ready");
  314. /* Computing new fullscale value.*/
  315. if(fs == LSM303AGR_ACC_FS_2G) {
  316. newfs = LSM303AGR_ACC_2G;
  317. }
  318. else if(fs == LSM303AGR_ACC_FS_4G) {
  319. newfs = LSM303AGR_ACC_4G;
  320. }
  321. else if(fs == LSM303AGR_ACC_FS_8G) {
  322. newfs = LSM303AGR_ACC_8G;
  323. }
  324. else if(fs == LSM303AGR_ACC_FS_16G) {
  325. newfs = LSM303AGR_ACC_16G;
  326. }
  327. else {
  328. msg = MSG_RESET;
  329. return msg;
  330. }
  331. if(newfs != devp->accfullscale) {
  332. /* Computing scale value.*/
  333. scale = newfs / devp->accfullscale;
  334. devp->accfullscale = newfs;
  335. #if LSM303AGR_SHARED_I2C
  336. i2cAcquireBus(devp->config->i2cp);
  337. i2cStart(devp->config->i2cp,
  338. devp->config->i2ccfg);
  339. #endif /* LSM303AGR_SHARED_I2C */
  340. /* Updating register.*/
  341. msg = lsm303agrI2CReadRegister(devp->config->i2cp,
  342. LSM303AGR_SAD_ACC,
  343. LSM303AGR_AD_CTRL_REG4_A,
  344. &buff[1], 1);
  345. #if LSM303AGR_SHARED_I2C
  346. i2cReleaseBus(devp->config->i2cp);
  347. #endif /* LSM303AGR_SHARED_I2C */
  348. if(msg != MSG_OK)
  349. return msg;
  350. buff[1] &= ~(LSM303AGR_CTRL_REG4_A_FS_MASK);
  351. buff[1] |= fs;
  352. buff[0] = LSM303AGR_AD_CTRL_REG4_A;
  353. #if LSM303AGR_SHARED_I2C
  354. i2cAcquireBus(devp->config->i2cp);
  355. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  356. #endif /* LSM303AGR_SHARED_I2C */
  357. msg = lsm303agrI2CWriteRegister(devp->config->i2cp,
  358. LSM303AGR_SAD_ACC, buff, 1);
  359. #if LSM303AGR_SHARED_I2C
  360. i2cReleaseBus(devp->config->i2cp);
  361. #endif /* LSM303AGR_SHARED_I2C */
  362. if(msg != MSG_OK)
  363. return msg;
  364. /* Scaling sensitivity and bias. Re-calibration is suggested anyway.*/
  365. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  366. devp->accsensitivity[i] *= scale;
  367. devp->accbias[i] *= scale;
  368. }
  369. }
  370. return msg;
  371. }
  372. /**
  373. * @brief Return the number of axes of the BaseCompass.
  374. *
  375. * @param[in] ip pointer to @p BaseCompass interface
  376. *
  377. * @return the number of axes.
  378. */
  379. static size_t comp_get_axes_number(void *ip) {
  380. osalDbgCheck(ip != NULL);
  381. return LSM303AGR_COMP_NUMBER_OF_AXES;
  382. }
  383. /**
  384. * @brief Retrieves raw data from the BaseCompass.
  385. * @note This data is retrieved from MEMS register without any algebraical
  386. * manipulation.
  387. * @note The axes array must be at least the same size of the
  388. * BaseCompass axes number.
  389. *
  390. * @param[in] ip pointer to @p BaseCompass interface.
  391. * @param[out] axes a buffer which would be filled with raw data.
  392. *
  393. * @return The operation status.
  394. * @retval MSG_OK if the function succeeded.
  395. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  396. * be retrieved using @p i2cGetErrors().
  397. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  398. */
  399. static msg_t comp_read_raw(void *ip, int32_t axes[]) {
  400. LSM303AGRDriver* devp;
  401. uint8_t buff [LSM303AGR_COMP_NUMBER_OF_AXES * 2], i;
  402. int16_t tmp;
  403. msg_t msg;
  404. osalDbgCheck((ip != NULL) && (axes != NULL));
  405. /* Getting parent instance pointer.*/
  406. devp = objGetInstance(LSM303AGRDriver*, (BaseCompass*)ip);
  407. osalDbgAssert((devp->state == LSM303AGR_READY),
  408. "comp_read_raw(), invalid state");
  409. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  410. "comp_read_raw(), channel not ready");
  411. #if LSM303AGR_SHARED_I2C
  412. i2cAcquireBus(devp->config->i2cp);
  413. i2cStart(devp->config->i2cp,
  414. devp->config->i2ccfg);
  415. #endif /* LSM303AGR_SHARED_I2C */
  416. msg = lsm303agrI2CReadRegister(devp->config->i2cp, LSM303AGR_SAD_COMP,
  417. LSM303AGR_AD_OUTX_L_REG_M, buff,
  418. LSM303AGR_COMP_NUMBER_OF_AXES * 2);
  419. #if LSM303AGR_SHARED_I2C
  420. i2cReleaseBus(devp->config->i2cp);
  421. #endif /* LSM303AGR_SHARED_I2C */
  422. if(msg == MSG_OK)
  423. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) {
  424. tmp = buff[2 * i] + (buff[2 * i + 1] << 8);
  425. axes[i] = (int32_t)tmp;
  426. }
  427. return msg;
  428. }
  429. /**
  430. * @brief Retrieves cooked data from the BaseCompass.
  431. * @note This data is manipulated according to the formula
  432. * cooked = (raw * sensitivity) - bias.
  433. * @note Final data is expressed as G.
  434. * @note The axes array must be at least the same size of the
  435. * BaseCompass axes number.
  436. *
  437. * @param[in] ip pointer to @p BaseCompass interface.
  438. * @param[out] axes a buffer which would be filled with cooked data.
  439. *
  440. * @return The operation status.
  441. * @retval MSG_OK if the function succeeded.
  442. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  443. * be retrieved using @p i2cGetErrors().
  444. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  445. */
  446. static msg_t comp_read_cooked(void *ip, float axes[]) {
  447. LSM303AGRDriver* devp;
  448. uint32_t i;
  449. int32_t raw[LSM303AGR_COMP_NUMBER_OF_AXES];
  450. msg_t msg;
  451. osalDbgCheck((ip != NULL) && (axes != NULL));
  452. /* Getting parent instance pointer.*/
  453. devp = objGetInstance(LSM303AGRDriver*, (BaseCompass*)ip);
  454. osalDbgAssert((devp->state == LSM303AGR_READY),
  455. "comp_read_cooked(), invalid state");
  456. msg = comp_read_raw(ip, raw);
  457. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES ; i++) {
  458. axes[i] = (raw[i] * devp->compsensitivity[i]) - devp->compbias[i];
  459. }
  460. return msg;
  461. }
  462. /**
  463. * @brief Set bias values for the BaseCompass.
  464. * @note Bias must be expressed as G.
  465. * @note The bias buffer must be at least the same size of the
  466. * BaseCompass axes number.
  467. *
  468. * @param[in] ip pointer to @p BaseCompass interface.
  469. * @param[in] bp a buffer which contains biases.
  470. *
  471. * @return The operation status.
  472. * @retval MSG_OK if the function succeeded.
  473. */
  474. static msg_t comp_set_bias(void *ip, float *bp) {
  475. LSM303AGRDriver* devp;
  476. uint32_t i;
  477. msg_t msg = MSG_OK;
  478. osalDbgCheck((ip != NULL) && (bp != NULL));
  479. /* Getting parent instance pointer.*/
  480. devp = objGetInstance(LSM303AGRDriver*, (BaseCompass*)ip);
  481. osalDbgAssert((devp->state == LSM303AGR_READY),
  482. "comp_set_bias(), invalid state");
  483. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) {
  484. devp->compbias[i] = bp[i];
  485. }
  486. return msg;
  487. }
  488. /**
  489. * @brief Reset bias values for the BaseCompass.
  490. * @note Default biases value are obtained from device datasheet when
  491. * available otherwise they are considered zero.
  492. *
  493. * @param[in] ip pointer to @p BaseCompass interface.
  494. *
  495. * @return The operation status.
  496. * @retval MSG_OK if the function succeeded.
  497. */
  498. static msg_t comp_reset_bias(void *ip) {
  499. LSM303AGRDriver* devp;
  500. uint32_t i;
  501. msg_t msg = MSG_OK;
  502. osalDbgCheck(ip != NULL);
  503. /* Getting parent instance pointer.*/
  504. devp = objGetInstance(LSM303AGRDriver*, (BaseCompass*)ip);
  505. osalDbgAssert((devp->state == LSM303AGR_READY),
  506. "comp_reset_bias(), invalid state");
  507. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++)
  508. devp->compbias[i] = LSM303AGR_COMP_BIAS;
  509. return msg;
  510. }
  511. /**
  512. * @brief Set sensitivity values for the BaseCompass.
  513. * @note Sensitivity must be expressed as G/LSB.
  514. * @note The sensitivity buffer must be at least the same size of the
  515. * BaseCompass axes number.
  516. *
  517. * @param[in] ip pointer to @p BaseCompass interface.
  518. * @param[in] sp a buffer which contains sensitivities.
  519. *
  520. * @return The operation status.
  521. * @retval MSG_OK if the function succeeded.
  522. */
  523. static msg_t comp_set_sensivity(void *ip, float *sp) {
  524. LSM303AGRDriver* devp;
  525. uint32_t i;
  526. msg_t msg = MSG_OK;
  527. /* Getting parent instance pointer.*/
  528. devp = objGetInstance(LSM303AGRDriver*, (BaseCompass*)ip);
  529. osalDbgCheck((ip != NULL) && (sp != NULL));
  530. osalDbgAssert((devp->state == LSM303AGR_READY),
  531. "comp_set_sensivity(), invalid state");
  532. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) {
  533. devp->compsensitivity[i] = sp[i];
  534. }
  535. return msg;
  536. }
  537. /**
  538. * @brief Reset sensitivity values for the BaseCompass.
  539. * @note Default sensitivities value are obtained from device datasheet.
  540. *
  541. * @param[in] ip pointer to @p BaseCompass interface.
  542. *
  543. * @return The operation status.
  544. * @retval MSG_OK if the function succeeded.
  545. * @retval MSG_RESET otherwise.
  546. */
  547. static msg_t comp_reset_sensivity(void *ip) {
  548. LSM303AGRDriver* devp;
  549. uint32_t i;
  550. msg_t msg = MSG_OK;
  551. osalDbgCheck(ip != NULL);
  552. /* Getting parent instance pointer.*/
  553. devp = objGetInstance(LSM303AGRDriver*, (BaseCompass*)ip);
  554. osalDbgAssert((devp->state == LSM303AGR_READY),
  555. "comp_reset_sensivity(), invalid state");
  556. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++)
  557. devp->compsensitivity[i] = LSM303AGR_COMP_SENS_50GA;
  558. return msg;
  559. }
  560. static const struct LSM303AGRVMT vmt_device = {
  561. (size_t)0,
  562. acc_set_full_scale
  563. };
  564. static const struct BaseAccelerometerVMT vmt_accelerometer = {
  565. sizeof(struct LSM303AGRVMT*),
  566. acc_get_axes_number, acc_read_raw, acc_read_cooked,
  567. acc_set_bias, acc_reset_bias, acc_set_sensivity, acc_reset_sensivity
  568. };
  569. static const struct BaseCompassVMT vmt_compass = {
  570. sizeof(struct LSM303AGRVMT*) + sizeof(BaseAccelerometer),
  571. comp_get_axes_number, comp_read_raw, comp_read_cooked,
  572. comp_set_bias, comp_reset_bias, comp_set_sensivity, comp_reset_sensivity
  573. };
  574. /*===========================================================================*/
  575. /* Driver exported functions. */
  576. /*===========================================================================*/
  577. /**
  578. * @brief Initializes an instance.
  579. *
  580. * @param[out] devp pointer to the @p LSM303AGRDriver object
  581. *
  582. * @init
  583. */
  584. void lsm303agrObjectInit(LSM303AGRDriver *devp) {
  585. devp->vmt = &vmt_device;
  586. devp->acc_if.vmt = &vmt_accelerometer;
  587. devp->comp_if.vmt = &vmt_compass;
  588. devp->config = NULL;
  589. devp->accaxes = LSM303AGR_ACC_NUMBER_OF_AXES;
  590. devp->compaxes = LSM303AGR_COMP_NUMBER_OF_AXES;
  591. devp->state = LSM303AGR_STOP;
  592. }
  593. /**
  594. * @brief Configures and activates LSM303AGR Complex Driver peripheral.
  595. *
  596. * @param[in] devp pointer to the @p LSM303AGRDriver object
  597. * @param[in] config pointer to the @p LSM303AGRConfig object
  598. *
  599. * @api
  600. */
  601. void lsm303agrStart(LSM303AGRDriver *devp, const LSM303AGRConfig *config) {
  602. uint32_t i;
  603. uint8_t cr[6];
  604. osalDbgCheck((devp != NULL) && (config != NULL));
  605. osalDbgAssert((devp->state == LSM303AGR_STOP) ||
  606. (devp->state == LSM303AGR_READY),
  607. "lsm303agrStart(), invalid state");
  608. devp->config = config;
  609. /* Configuring Accelerometer subsystem.*/
  610. /* Multiple write starting address.*/
  611. cr[0] = LSM303AGR_AD_CTRL_REG1_A;
  612. /* Control register 1 configuration block.*/
  613. {
  614. cr[1] = LSM303AGR_ACC_AE_XYZ | devp->config->accoutdatarate;
  615. #if LSM303AGR_USE_ADVANCED || defined(__DOXYGEN__)
  616. if(devp->config->accmode == LSM303AGR_ACC_MODE_LPOW)
  617. cr[1] |= LSM303AGR_CTRL_REG1_A_LPEN;
  618. #endif
  619. }
  620. /* Control register 2 configuration block.*/
  621. {
  622. cr[2] = 0;
  623. }
  624. /* Control register 3 configuration block.*/
  625. {
  626. cr[3] = 0;
  627. }
  628. /* Control register 4 configuration block.*/
  629. {
  630. cr[4] = devp->config->accfullscale;
  631. #if LSM303AGR_USE_ADVANCED || defined(__DOXYGEN__)
  632. cr[4] |= devp->config->accendianess |
  633. devp->config->accblockdataupdate;
  634. if(devp->config->accmode == LSM303AGR_ACC_MODE_HRES)
  635. cr[4] |= LSM303AGR_CTRL_REG4_A_HR;
  636. #endif
  637. }
  638. /* Storing sensitivity according to user settings */
  639. if(devp->config->accfullscale == LSM303AGR_ACC_FS_2G) {
  640. devp->accfullscale = LSM303AGR_ACC_2G;
  641. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  642. if(devp->config->accsensitivity == NULL)
  643. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_2G;
  644. else
  645. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  646. }
  647. }
  648. else if(devp->config->accfullscale == LSM303AGR_ACC_FS_4G) {
  649. devp->accfullscale = LSM303AGR_ACC_4G;
  650. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  651. if(devp->config->accsensitivity == NULL)
  652. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_4G;
  653. else
  654. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  655. }
  656. }
  657. else if(devp->config->accfullscale == LSM303AGR_ACC_FS_8G) {
  658. devp->accfullscale = LSM303AGR_ACC_8G;
  659. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  660. if(devp->config->accsensitivity == NULL)
  661. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_8G;
  662. else
  663. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  664. }
  665. }
  666. else if(devp->config->accfullscale == LSM303AGR_ACC_FS_16G) {
  667. devp->accfullscale = LSM303AGR_ACC_16G;
  668. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  669. if(devp->config->accsensitivity == NULL)
  670. devp->accsensitivity[i] = LSM303AGR_ACC_SENS_16G;
  671. else
  672. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  673. }
  674. }
  675. else
  676. osalDbgAssert(FALSE, "lsm303dlhcStart(), accelerometer full scale issue");
  677. /* Storing bias information */
  678. if(devp->config->accbias != NULL)
  679. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++)
  680. devp->accbias[i] = devp->config->accbias[i];
  681. else
  682. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++)
  683. devp->accbias[i] = LSM303AGR_ACC_BIAS;
  684. #if LSM303AGR_SHARED_I2C
  685. i2cAcquireBus((devp)->config->i2cp);
  686. #endif /* LSM303AGR_SHARED_I2C */
  687. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  688. lsm303agrI2CWriteRegister(devp->config->i2cp, LSM303AGR_SAD_ACC, cr, 4);
  689. #if LSM303AGR_SHARED_I2C
  690. i2cReleaseBus((devp)->config->i2cp);
  691. #endif /* LSM303AGR_SHARED_I2C */
  692. /* Configuring Compass subsystem */
  693. /* Multiple write starting address.*/
  694. cr[0] = LSM303AGR_AD_CFG_REG_A_M;
  695. /* Control register A configuration block.*/
  696. {
  697. cr[1] = devp->config->compoutputdatarate;
  698. #if LSM303AGR_USE_ADVANCED || defined(__DOXYGEN__)
  699. cr[1] |= devp->config->compmode | devp->config->complp;
  700. #endif
  701. }
  702. /* Control register B configuration block.*/
  703. {
  704. cr[2] = 0;
  705. }
  706. /* Control register C configuration block.*/
  707. {
  708. cr[3] = 0;
  709. }
  710. #if LSM303AGR_SHARED_I2C
  711. i2cAcquireBus((devp)->config->i2cp);
  712. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  713. #endif /* LSM303AGR_SHARED_I2C */
  714. lsm303agrI2CWriteRegister(devp->config->i2cp, LSM303AGR_SAD_COMP,
  715. cr, 3);
  716. #if LSM303AGR_SHARED_I2C
  717. i2cReleaseBus((devp)->config->i2cp);
  718. #endif /* LSM303AGR_SHARED_I2C */
  719. devp->compfullscale = LSM303AGR_COMP_50GA;
  720. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) {
  721. if(devp->config->compsensitivity == NULL) {
  722. devp->compsensitivity[i] = LSM303AGR_COMP_SENS_50GA;
  723. }
  724. else {
  725. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  726. }
  727. }
  728. /* This is the MEMS transient recovery time */
  729. osalThreadSleepMilliseconds(5);
  730. devp->state = LSM303AGR_READY;
  731. }
  732. /**
  733. * @brief Deactivates the LSM303AGR Complex Driver peripheral.
  734. *
  735. * @param[in] devp pointer to the @p LSM303AGRDriver object
  736. *
  737. * @api
  738. */
  739. void lsm303agrStop(LSM303AGRDriver *devp) {
  740. uint8_t cr[2];
  741. osalDbgCheck(devp != NULL);
  742. osalDbgAssert((devp->state == LSM303AGR_STOP) ||
  743. (devp->state == LSM303AGR_READY),
  744. "lsm303agrStop(), invalid state");
  745. if (devp->state == LSM303AGR_READY) {
  746. #if LSM303AGR_SHARED_I2C
  747. i2cAcquireBus((devp)->config->i2cp);
  748. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  749. #endif /* LSM303AGR_SHARED_I2C */
  750. /* Disabling accelerometer. */
  751. cr[0] = LSM303AGR_AD_CTRL_REG1_A;
  752. cr[1] = LSM303AGR_ACC_AE_DISABLED | LSM303AGR_ACC_ODR_PD;
  753. lsm303agrI2CWriteRegister(devp->config->i2cp, LSM303AGR_SAD_ACC,
  754. cr, 1);
  755. /* Disabling compass. */
  756. cr[0] = LSM303AGR_AD_CFG_REG_A_M;
  757. cr[1] = LSM303AGR_COMP_MODE_IDLE;
  758. lsm303agrI2CWriteRegister(devp->config->i2cp, LSM303AGR_SAD_COMP,
  759. cr, 1);
  760. i2cStop((devp)->config->i2cp);
  761. #if LSM303AGR_SHARED_I2C
  762. i2cReleaseBus((devp)->config->i2cp);
  763. #endif /* LSM303AGR_SHARED_I2C */
  764. }
  765. devp->state = LSM303AGR_STOP;
  766. }
  767. /** @} */