lsm303dlhc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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.c
  17. * @brief LSM303DLHC MEMS interface module code.
  18. *
  19. * @addtogroup LSM303DLHC
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #include "hal.h"
  24. #include "lsm303dlhc.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. LSM303DLHC_SAD_ACC = 0x19, /**< SAD for accelerometer. */
  39. LSM303DLHC_SAD_COMP = 0x1E /**< SAD for compass. */
  40. } lsm303dlhc_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 lsm303dlhcI2CReadRegister(I2CDriver *i2cp, lsm303dlhc_sad_t sad,
  57. uint8_t reg, uint8_t *rxbuf, size_t n) {
  58. uint8_t txbuf = reg | LSM303DLHC_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 lsm303dlhcI2CWriteRegister(I2CDriver *i2cp, lsm303dlhc_sad_t sad,
  75. uint8_t *txbuf, size_t n) {
  76. if (n != 1)
  77. *txbuf |= LSM303DLHC_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 LSM303DLHC_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. LSM303DLHCDriver* devp;
  110. uint8_t buff [LSM303DLHC_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(LSM303DLHCDriver*, (BaseAccelerometer*)ip);
  116. osalDbgAssert((devp->state == LSM303DLHC_READY),
  117. "acc_read_raw(), invalid state");
  118. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  119. "acc_read_raw(), channel not ready");
  120. #if LSM303DLHC_SHARED_I2C
  121. i2cAcquireBus(devp->config->i2cp);
  122. i2cStart(devp->config->i2cp,
  123. devp->config->i2ccfg);
  124. #endif /* LSM303DLHC_SHARED_I2C */
  125. msg = lsm303dlhcI2CReadRegister(devp->config->i2cp, LSM303DLHC_SAD_ACC,
  126. LSM303DLHC_AD_ACC_OUT_X_L, buff,
  127. LSM303DLHC_ACC_NUMBER_OF_AXES * 2);
  128. #if LSM303DLHC_SHARED_I2C
  129. i2cReleaseBus(devp->config->i2cp);
  130. #endif /* LSM303DLHC_SHARED_I2C */
  131. if(msg == MSG_OK)
  132. for(i = 0; i < LSM303DLHC_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. LSM303DLHCDriver* devp;
  157. uint32_t i;
  158. int32_t raw[LSM303DLHC_ACC_NUMBER_OF_AXES];
  159. msg_t msg;
  160. osalDbgCheck((ip != NULL) && (axes != NULL));
  161. /* Getting parent instance pointer.*/
  162. devp = objGetInstance(LSM303DLHCDriver*, (BaseAccelerometer*)ip);
  163. osalDbgAssert((devp->state == LSM303DLHC_READY),
  164. "acc_read_cooked(), invalid state");
  165. msg = acc_read_raw(ip, raw);
  166. for(i = 0; i < LSM303DLHC_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. LSM303DLHCDriver* 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(LSM303DLHCDriver*, (BaseAccelerometer*)ip);
  190. osalDbgAssert((devp->state == LSM303DLHC_READY),
  191. "acc_set_bias(), invalid state");
  192. for(i = 0; i < LSM303DLHC_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. LSM303DLHCDriver* devp;
  209. uint32_t i;
  210. msg_t msg = MSG_OK;
  211. osalDbgCheck(ip != NULL);
  212. /* Getting parent instance pointer.*/
  213. devp = objGetInstance(LSM303DLHCDriver*, (BaseAccelerometer*)ip);
  214. osalDbgAssert((devp->state == LSM303DLHC_READY),
  215. "acc_reset_bias(), invalid state");
  216. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
  217. devp->accbias[i] = LSM303DLHC_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. LSM303DLHCDriver* devp;
  234. uint32_t i;
  235. msg_t msg = MSG_OK;
  236. /* Getting parent instance pointer.*/
  237. devp = objGetInstance(LSM303DLHCDriver*, (BaseAccelerometer*)ip);
  238. osalDbgCheck((ip != NULL) && (sp != NULL));
  239. osalDbgAssert((devp->state == LSM303DLHC_READY),
  240. "acc_set_sensivity(), invalid state");
  241. for(i = 0; i < LSM303DLHC_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. LSM303DLHCDriver* devp;
  258. uint32_t i;
  259. msg_t msg = MSG_OK;
  260. osalDbgCheck(ip != NULL);
  261. /* Getting parent instance pointer.*/
  262. devp = objGetInstance(LSM303DLHCDriver*, (BaseAccelerometer*)ip);
  263. osalDbgAssert((devp->state == LSM303DLHC_READY),
  264. "acc_reset_sensivity(), invalid state");
  265. if(devp->config->accfullscale == LSM303DLHC_ACC_FS_2G)
  266. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
  267. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_2G;
  268. else if(devp->config->accfullscale == LSM303DLHC_ACC_FS_4G)
  269. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
  270. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_4G;
  271. else if(devp->config->accfullscale == LSM303DLHC_ACC_FS_8G)
  272. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
  273. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_8G;
  274. else if(devp->config->accfullscale == LSM303DLHC_ACC_FS_16G)
  275. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
  276. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_16G;
  277. else {
  278. osalDbgAssert(FALSE, "acc_reset_sensivity(), accelerometer full scale issue");
  279. msg = MSG_RESET;
  280. }
  281. return msg;
  282. }
  283. /**
  284. * @brief Changes the LSM303DLHCDriver accelerometer fullscale value.
  285. * @note This function also rescale sensitivities and biases based on
  286. * previous and next fullscale value.
  287. * @note A recalibration is highly suggested after calling this function.
  288. *
  289. * @param[in] devp pointer to @p LSM303DLHCDriver interface.
  290. * @param[in] fs new fullscale value.
  291. *
  292. * @return The operation status.
  293. * @retval MSG_OK if the function succeeded.
  294. * @retval MSG_RESET otherwise.
  295. */
  296. static msg_t acc_set_full_scale(LSM303DLHCDriver *devp,
  297. lsm303dlhc_acc_fs_t fs) {
  298. float newfs, scale;
  299. uint8_t i, buff[2];
  300. msg_t msg;
  301. osalDbgCheck(devp != NULL);
  302. osalDbgAssert((devp->state == LSM303DLHC_READY),
  303. "acc_set_full_scale(), invalid state");
  304. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  305. "acc_set_full_scale(), channel not ready");
  306. /* Computing new fullscale value.*/
  307. if(fs == LSM303DLHC_ACC_FS_2G) {
  308. newfs = LSM303DLHC_ACC_2G;
  309. }
  310. else if(fs == LSM303DLHC_ACC_FS_4G) {
  311. newfs = LSM303DLHC_ACC_4G;
  312. }
  313. else if(fs == LSM303DLHC_ACC_FS_8G) {
  314. newfs = LSM303DLHC_ACC_8G;
  315. }
  316. else if(fs == LSM303DLHC_ACC_FS_16G) {
  317. newfs = LSM303DLHC_ACC_16G;
  318. }
  319. else {
  320. msg = MSG_RESET;
  321. return msg;
  322. }
  323. if(newfs != devp->accfullscale) {
  324. /* Computing scale value.*/
  325. scale = newfs / devp->accfullscale;
  326. devp->accfullscale = newfs;
  327. #if LSM303DLHC_SHARED_I2C
  328. i2cAcquireBus(devp->config->i2cp);
  329. i2cStart(devp->config->i2cp,
  330. devp->config->i2ccfg);
  331. #endif /* LSM303DLHC_SHARED_I2C */
  332. /* Updating register.*/
  333. msg = lsm303dlhcI2CReadRegister(devp->config->i2cp,
  334. LSM303DLHC_SAD_ACC,
  335. LSM303DLHC_AD_ACC_CTRL_REG4,
  336. &buff[1], 1);
  337. #if LSM303DLHC_SHARED_I2C
  338. i2cReleaseBus(devp->config->i2cp);
  339. #endif /* LSM303DLHC_SHARED_I2C */
  340. if(msg != MSG_OK)
  341. return msg;
  342. buff[1] &= ~(LSM303DLHC_CTRL_REG4_A_FS_MASK);
  343. buff[1] |= fs;
  344. buff[0] = LSM303DLHC_AD_ACC_CTRL_REG4;
  345. #if LSM303DLHC_SHARED_I2C
  346. i2cAcquireBus(devp->config->i2cp);
  347. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  348. #endif /* LSM303DLHC_SHARED_I2C */
  349. msg = lsm303dlhcI2CWriteRegister(devp->config->i2cp,
  350. LSM303DLHC_SAD_ACC, buff, 1);
  351. #if LSM303DLHC_SHARED_I2C
  352. i2cReleaseBus(devp->config->i2cp);
  353. #endif /* LSM303DLHC_SHARED_I2C */
  354. if(msg != MSG_OK)
  355. return msg;
  356. /* Scaling sensitivity and bias. Re-calibration is suggested anyway.*/
  357. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  358. devp->accsensitivity[i] *= scale;
  359. devp->accbias[i] *= scale;
  360. }
  361. }
  362. return msg;
  363. }
  364. /**
  365. * @brief Return the number of axes of the BaseCompass.
  366. *
  367. * @param[in] ip pointer to @p BaseCompass interface
  368. *
  369. * @return the number of axes.
  370. */
  371. static size_t comp_get_axes_number(void *ip) {
  372. osalDbgCheck(ip != NULL);
  373. return LSM303DLHC_COMP_NUMBER_OF_AXES;
  374. }
  375. /**
  376. * @brief Retrieves raw data from the BaseCompass.
  377. * @note This data is retrieved from MEMS register without any algebraical
  378. * manipulation.
  379. * @note The axes array must be at least the same size of the
  380. * BaseCompass axes number.
  381. *
  382. * @param[in] ip pointer to @p BaseCompass interface.
  383. * @param[out] axes a buffer which would be filled with raw data.
  384. *
  385. * @return The operation status.
  386. * @retval MSG_OK if the function succeeded.
  387. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  388. * be retrieved using @p i2cGetErrors().
  389. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  390. */
  391. static msg_t comp_read_raw(void *ip, int32_t axes[]) {
  392. LSM303DLHCDriver* devp;
  393. uint8_t buff [LSM303DLHC_COMP_NUMBER_OF_AXES * 2], i;
  394. int16_t tmp;
  395. msg_t msg;
  396. osalDbgCheck((ip != NULL) && (axes != NULL));
  397. /* Getting parent instance pointer.*/
  398. devp = objGetInstance(LSM303DLHCDriver*, (BaseCompass*)ip);
  399. osalDbgAssert((devp->state == LSM303DLHC_READY),
  400. "comp_read_raw(), invalid state");
  401. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  402. "comp_read_raw(), channel not ready");
  403. #if LSM303DLHC_SHARED_I2C
  404. i2cAcquireBus(devp->config->i2cp);
  405. i2cStart(devp->config->i2cp,
  406. devp->config->i2ccfg);
  407. #endif /* LSM303DLHC_SHARED_I2C */
  408. msg = lsm303dlhcI2CReadRegister(devp->config->i2cp, LSM303DLHC_SAD_COMP,
  409. LSM303DLHC_AD_COMP_OUT_X_L, buff,
  410. LSM303DLHC_COMP_NUMBER_OF_AXES * 2);
  411. #if LSM303DLHC_SHARED_I2C
  412. i2cReleaseBus(devp->config->i2cp);
  413. #endif /* LSM303DLHC_SHARED_I2C */
  414. if(msg == MSG_OK)
  415. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  416. tmp = buff[2 * i] + (buff[2 * i + 1] << 8);
  417. axes[i] = (int32_t)tmp;
  418. }
  419. return msg;
  420. }
  421. /**
  422. * @brief Retrieves cooked data from the BaseCompass.
  423. * @note This data is manipulated according to the formula
  424. * cooked = (raw * sensitivity) - bias.
  425. * @note Final data is expressed as G.
  426. * @note The axes array must be at least the same size of the
  427. * BaseCompass axes number.
  428. *
  429. * @param[in] ip pointer to @p BaseCompass interface.
  430. * @param[out] axes a buffer which would be filled with cooked data.
  431. *
  432. * @return The operation status.
  433. * @retval MSG_OK if the function succeeded.
  434. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  435. * be retrieved using @p i2cGetErrors().
  436. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  437. */
  438. static msg_t comp_read_cooked(void *ip, float axes[]) {
  439. LSM303DLHCDriver* devp;
  440. uint32_t i;
  441. int32_t raw[LSM303DLHC_COMP_NUMBER_OF_AXES];
  442. msg_t msg;
  443. osalDbgCheck((ip != NULL) && (axes != NULL));
  444. /* Getting parent instance pointer.*/
  445. devp = objGetInstance(LSM303DLHCDriver*, (BaseCompass*)ip);
  446. osalDbgAssert((devp->state == LSM303DLHC_READY),
  447. "comp_read_cooked(), invalid state");
  448. msg = comp_read_raw(ip, raw);
  449. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES ; i++) {
  450. axes[i] = (raw[i] * devp->compsensitivity[i]) - devp->compbias[i];
  451. }
  452. return msg;
  453. }
  454. /**
  455. * @brief Set bias values for the BaseCompass.
  456. * @note Bias must be expressed as G.
  457. * @note The bias buffer must be at least the same size of the
  458. * BaseCompass axes number.
  459. *
  460. * @param[in] ip pointer to @p BaseCompass interface.
  461. * @param[in] bp a buffer which contains biases.
  462. *
  463. * @return The operation status.
  464. * @retval MSG_OK if the function succeeded.
  465. */
  466. static msg_t comp_set_bias(void *ip, float *bp) {
  467. LSM303DLHCDriver* devp;
  468. uint32_t i;
  469. msg_t msg = MSG_OK;
  470. osalDbgCheck((ip != NULL) && (bp != NULL));
  471. /* Getting parent instance pointer.*/
  472. devp = objGetInstance(LSM303DLHCDriver*, (BaseCompass*)ip);
  473. osalDbgAssert((devp->state == LSM303DLHC_READY),
  474. "comp_set_bias(), invalid state");
  475. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  476. devp->compbias[i] = bp[i];
  477. }
  478. return msg;
  479. }
  480. /**
  481. * @brief Reset bias values for the BaseCompass.
  482. * @note Default biases value are obtained from device datasheet when
  483. * available otherwise they are considered zero.
  484. *
  485. * @param[in] ip pointer to @p BaseCompass interface.
  486. *
  487. * @return The operation status.
  488. * @retval MSG_OK if the function succeeded.
  489. */
  490. static msg_t comp_reset_bias(void *ip) {
  491. LSM303DLHCDriver* devp;
  492. uint32_t i;
  493. msg_t msg = MSG_OK;
  494. osalDbgCheck(ip != NULL);
  495. /* Getting parent instance pointer.*/
  496. devp = objGetInstance(LSM303DLHCDriver*, (BaseCompass*)ip);
  497. osalDbgAssert((devp->state == LSM303DLHC_READY),
  498. "comp_reset_bias(), invalid state");
  499. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++)
  500. devp->compbias[i] = LSM303DLHC_COMP_BIAS;
  501. return msg;
  502. }
  503. /**
  504. * @brief Set sensitivity values for the BaseCompass.
  505. * @note Sensitivity must be expressed as G/LSB.
  506. * @note The sensitivity buffer must be at least the same size of the
  507. * BaseCompass axes number.
  508. *
  509. * @param[in] ip pointer to @p BaseCompass interface.
  510. * @param[in] sp a buffer which contains sensitivities.
  511. *
  512. * @return The operation status.
  513. * @retval MSG_OK if the function succeeded.
  514. */
  515. static msg_t comp_set_sensivity(void *ip, float *sp) {
  516. LSM303DLHCDriver* devp;
  517. uint32_t i;
  518. msg_t msg = MSG_OK;
  519. /* Getting parent instance pointer.*/
  520. devp = objGetInstance(LSM303DLHCDriver*, (BaseCompass*)ip);
  521. osalDbgCheck((ip != NULL) && (sp != NULL));
  522. osalDbgAssert((devp->state == LSM303DLHC_READY),
  523. "comp_set_sensivity(), invalid state");
  524. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  525. devp->compsensitivity[i] = sp[i];
  526. }
  527. return msg;
  528. }
  529. /**
  530. * @brief Reset sensitivity values for the BaseCompass.
  531. * @note Default sensitivities value are obtained from device datasheet.
  532. *
  533. * @param[in] ip pointer to @p BaseCompass interface.
  534. *
  535. * @return The operation status.
  536. * @retval MSG_OK if the function succeeded.
  537. * @retval MSG_RESET otherwise.
  538. */
  539. static msg_t comp_reset_sensivity(void *ip) {
  540. LSM303DLHCDriver* devp;
  541. uint32_t i;
  542. msg_t msg = MSG_OK;
  543. osalDbgCheck(ip != NULL);
  544. /* Getting parent instance pointer.*/
  545. devp = objGetInstance(LSM303DLHCDriver*, (BaseCompass*)ip);
  546. osalDbgAssert((devp->state == LSM303DLHC_READY),
  547. "comp_reset_sensivity(), invalid state");
  548. if(devp->config->compfullscale == LSM303DLHC_COMP_FS_1P3GA)
  549. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  550. if(i != 2) {
  551. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_1P3GA;
  552. }
  553. else {
  554. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_1P3GA;
  555. }
  556. }
  557. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_1P9GA)
  558. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  559. if(i != 2) {
  560. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_1P9GA;
  561. }
  562. else {
  563. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_1P9GA;
  564. }
  565. }
  566. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_2P5GA)
  567. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  568. if(i != 2) {
  569. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_2P5GA;
  570. }
  571. else {
  572. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_2P5GA;
  573. }
  574. }
  575. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_4P0GA)
  576. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  577. if(i != 2) {
  578. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_4P0GA;
  579. }
  580. else {
  581. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_4P0GA;
  582. }
  583. }
  584. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_4P7GA)
  585. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  586. if(i != 2) {
  587. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_4P7GA;
  588. }
  589. else {
  590. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_4P7GA;
  591. }
  592. }
  593. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_5P6GA)
  594. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  595. if(i != 2) {
  596. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_5P6GA;
  597. }
  598. else {
  599. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_5P6GA;
  600. }
  601. }
  602. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_8P1GA)
  603. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  604. if(i != 2) {
  605. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_8P1GA;
  606. }
  607. else {
  608. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_8P1GA;
  609. }
  610. }
  611. else {
  612. osalDbgAssert(FALSE, "comp_reset_sensivity(), compass full scale issue");
  613. msg = MSG_RESET;
  614. }
  615. return msg;
  616. }
  617. /**
  618. * @brief Changes the LSM303DLHCDriver compass fullscale value.
  619. * @note This function also rescale sensitivities and biases based on
  620. * previous and next fullscale value.
  621. * @note A recalibration is highly suggested after calling this function.
  622. *
  623. * @param[in] devp pointer to @p LSM303DLHCDriver interface.
  624. * @param[in] fs new fullscale value.
  625. *
  626. * @return The operation status.
  627. * @retval MSG_OK if the function succeeded.
  628. * @retval MSG_RESET otherwise.
  629. */
  630. static msg_t comp_set_full_scale(LSM303DLHCDriver *devp,
  631. lsm303dlhc_comp_fs_t fs) {
  632. float newfs, scale;
  633. uint8_t i, buff[2];
  634. msg_t msg;
  635. osalDbgCheck(devp != NULL);
  636. osalDbgAssert((devp->state == LSM303DLHC_READY),
  637. "comp_set_full_scale(), invalid state");
  638. osalDbgAssert((devp->config->i2cp->state == I2C_READY),
  639. "comp_set_full_scale(), channel not ready");
  640. /* Computing new fullscale value.*/
  641. if(fs == LSM303DLHC_COMP_FS_1P3GA) {
  642. newfs = LSM303DLHC_COMP_1P3GA;
  643. }
  644. else if(fs == LSM303DLHC_COMP_FS_1P9GA) {
  645. newfs = LSM303DLHC_COMP_1P9GA;
  646. }
  647. else if(fs == LSM303DLHC_COMP_FS_2P5GA) {
  648. newfs = LSM303DLHC_COMP_2P5GA;
  649. }
  650. else if(fs == LSM303DLHC_COMP_FS_4P0GA) {
  651. newfs = LSM303DLHC_COMP_4P0GA;
  652. }
  653. else if(fs == LSM303DLHC_COMP_FS_4P7GA) {
  654. newfs = LSM303DLHC_COMP_4P7GA;
  655. }
  656. else if(fs == LSM303DLHC_COMP_FS_5P6GA) {
  657. newfs = LSM303DLHC_COMP_5P6GA;
  658. }
  659. else if(fs == LSM303DLHC_COMP_FS_8P1GA) {
  660. newfs = LSM303DLHC_COMP_8P1GA;
  661. }
  662. else {
  663. msg = MSG_RESET;
  664. return msg;
  665. }
  666. if(newfs != devp->compfullscale) {
  667. /* Computing scale value.*/
  668. scale = newfs / devp->compfullscale;
  669. devp->compfullscale = newfs;
  670. #if LSM303DLHC_SHARED_I2C
  671. i2cAcquireBus(devp->config->i2cp);
  672. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  673. #endif /* LSM303DLHC_SHARED_I2C */
  674. /* Updating register.*/
  675. msg = lsm303dlhcI2CReadRegister(devp->config->i2cp, LSM303DLHC_SAD_COMP,
  676. LSM303DLHC_AD_COMP_CRB_REG, &buff[1], 1);
  677. #if LSM303DLHC_SHARED_I2C
  678. i2cReleaseBus(devp->config->i2cp);
  679. #endif /* LSM303DLHC_SHARED_I2C */
  680. if(msg != MSG_OK)
  681. return msg;
  682. buff[1] &= ~(LSM303DLHC_CRB_REG_M_GN_MASK);
  683. buff[1] |= fs;
  684. buff[0] = LSM303DLHC_AD_COMP_CRB_REG;
  685. #if LSM303DLHC_SHARED_I2C
  686. i2cAcquireBus(devp->config->i2cp);
  687. i2cStart(devp->config->i2cp, devp->config->i2ccfg);
  688. #endif /* LSM303DLHC_SHARED_I2C */
  689. msg = lsm303dlhcI2CWriteRegister(devp->config->i2cp, LSM303DLHC_SAD_COMP,
  690. buff, 1);
  691. #if LSM303DLHC_SHARED_I2C
  692. i2cReleaseBus(devp->config->i2cp);
  693. #endif /* LSM303DLHC_SHARED_I2C */
  694. if(msg != MSG_OK)
  695. return msg;
  696. /* Scaling sensitivity and bias. Re-calibration is suggested anyway.*/
  697. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  698. devp->compsensitivity[i] *= scale;
  699. devp->compbias[i] *= scale;
  700. }
  701. }
  702. return msg;
  703. }
  704. static const struct LSM303DLHCVMT vmt_device = {
  705. (size_t)0,
  706. acc_set_full_scale, comp_set_full_scale
  707. };
  708. static const struct BaseAccelerometerVMT vmt_accelerometer = {
  709. sizeof(struct LSM303DLHCVMT*),
  710. acc_get_axes_number, acc_read_raw, acc_read_cooked,
  711. acc_set_bias, acc_reset_bias, acc_set_sensivity, acc_reset_sensivity
  712. };
  713. static const struct BaseCompassVMT vmt_compass = {
  714. sizeof(struct LSM303DLHCVMT*) + sizeof(BaseAccelerometer),
  715. comp_get_axes_number, comp_read_raw, comp_read_cooked,
  716. comp_set_bias, comp_reset_bias, comp_set_sensivity, comp_reset_sensivity
  717. };
  718. /*===========================================================================*/
  719. /* Driver exported functions. */
  720. /*===========================================================================*/
  721. /**
  722. * @brief Initializes an instance.
  723. *
  724. * @param[out] devp pointer to the @p LSM303DLHCDriver object
  725. *
  726. * @init
  727. */
  728. void lsm303dlhcObjectInit(LSM303DLHCDriver *devp) {
  729. devp->vmt = &vmt_device;
  730. devp->acc_if.vmt = &vmt_accelerometer;
  731. devp->comp_if.vmt = &vmt_compass;
  732. devp->config = NULL;
  733. devp->accaxes = LSM303DLHC_ACC_NUMBER_OF_AXES;
  734. devp->compaxes = LSM303DLHC_COMP_NUMBER_OF_AXES;
  735. devp->state = LSM303DLHC_STOP;
  736. }
  737. /**
  738. * @brief Configures and activates LSM303DLHC Complex Driver peripheral.
  739. *
  740. * @param[in] devp pointer to the @p LSM303DLHCDriver object
  741. * @param[in] config pointer to the @p LSM303DLHCConfig object
  742. *
  743. * @api
  744. */
  745. void lsm303dlhcStart(LSM303DLHCDriver *devp, const LSM303DLHCConfig *config) {
  746. uint32_t i;
  747. uint8_t cr[6];
  748. osalDbgCheck((devp != NULL) && (config != NULL));
  749. osalDbgAssert((devp->state == LSM303DLHC_STOP) ||
  750. (devp->state == LSM303DLHC_READY),
  751. "lsm303dlhcStart(), invalid state");
  752. devp->config = config;
  753. /* Configuring Accelerometer subsystem.*/
  754. /* Multiple write starting address.*/
  755. cr[0] = LSM303DLHC_AD_ACC_CTRL_REG1;
  756. /* Control register 1 configuration block.*/
  757. {
  758. cr[1] = LSM303DLHC_CTRL_REG1_A_XEN | LSM303DLHC_CTRL_REG1_A_YEN |
  759. LSM303DLHC_CTRL_REG1_A_ZEN | devp->config->accoutdatarate;
  760. #if LSM303DLHC_USE_ADVANCED || defined(__DOXYGEN__)
  761. cr[1] |= devp->config->acclowpower;
  762. #endif
  763. }
  764. /* Control register 2 configuration block.*/
  765. {
  766. cr[2] = 0;
  767. }
  768. /* Control register 3 configuration block.*/
  769. {
  770. cr[3] = 0;
  771. }
  772. /* Control register 4 configuration block.*/
  773. {
  774. cr[4] = devp->config->accfullscale;
  775. #if LSM303DLHC_USE_ADVANCED || defined(__DOXYGEN__)
  776. cr[4] |= devp->config->accendianess |
  777. devp->config->accblockdataupdate |
  778. devp->config->acchighresmode;
  779. #endif
  780. }
  781. #if LSM303DLHC_SHARED_I2C
  782. i2cAcquireBus((devp)->config->i2cp);
  783. #endif /* LSM303DLHC_SHARED_I2C */
  784. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  785. lsm303dlhcI2CWriteRegister(devp->config->i2cp, LSM303DLHC_SAD_ACC, cr, 4);
  786. #if LSM303DLHC_SHARED_I2C
  787. i2cReleaseBus((devp)->config->i2cp);
  788. #endif /* LSM303DLHC_SHARED_I2C */
  789. /* Storing sensitivity according to user settings */
  790. if(devp->config->accfullscale == LSM303DLHC_ACC_FS_2G) {
  791. devp->accfullscale = LSM303DLHC_ACC_2G;
  792. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  793. if(devp->config->accsensitivity == NULL)
  794. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_2G;
  795. else
  796. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  797. }
  798. }
  799. else if(devp->config->accfullscale == LSM303DLHC_ACC_FS_4G) {
  800. devp->accfullscale = LSM303DLHC_ACC_4G;
  801. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  802. if(devp->config->accsensitivity == NULL)
  803. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_4G;
  804. else
  805. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  806. }
  807. }
  808. else if(devp->config->accfullscale == LSM303DLHC_ACC_FS_8G) {
  809. devp->accfullscale = LSM303DLHC_ACC_8G;
  810. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  811. if(devp->config->accsensitivity == NULL)
  812. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_8G;
  813. else
  814. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  815. }
  816. }
  817. else if(devp->config->accfullscale == LSM303DLHC_ACC_FS_16G) {
  818. devp->accfullscale = LSM303DLHC_ACC_16G;
  819. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  820. if(devp->config->accsensitivity == NULL)
  821. devp->accsensitivity[i] = LSM303DLHC_ACC_SENS_16G;
  822. else
  823. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  824. }
  825. }
  826. else
  827. osalDbgAssert(FALSE, "lsm303dlhcStart(), accelerometer full scale issue");
  828. /* Storing bias information */
  829. if(devp->config->accbias != NULL)
  830. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
  831. devp->accbias[i] = devp->config->accbias[i];
  832. else
  833. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
  834. devp->accbias[i] = LSM303DLHC_ACC_BIAS;
  835. /* Configuring Compass subsystem */
  836. /* Multiple write starting address.*/
  837. cr[0] = LSM303DLHC_AD_COMP_CRA_REG;
  838. /* Control register A configuration block.*/
  839. {
  840. cr[1] = devp->config->compoutputdatarate;
  841. }
  842. /* Control register B configuration block.*/
  843. {
  844. cr[2] = devp->config->compfullscale;
  845. }
  846. /* Mode register configuration block.*/
  847. {
  848. cr[3] = 0;
  849. #if LSM303DLHC_USE_ADVANCED || defined(__DOXYGEN__)
  850. cr[3] |= devp->config->compmode;
  851. #endif
  852. }
  853. #if LSM303DLHC_SHARED_I2C
  854. i2cAcquireBus((devp)->config->i2cp);
  855. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  856. #endif /* LSM303DLHC_SHARED_I2C */
  857. lsm303dlhcI2CWriteRegister(devp->config->i2cp, LSM303DLHC_SAD_COMP,
  858. cr, 3);
  859. #if LSM303DLHC_SHARED_I2C
  860. i2cReleaseBus((devp)->config->i2cp);
  861. #endif /* LSM303DLHC_SHARED_I2C */
  862. if(devp->config->compfullscale == LSM303DLHC_COMP_FS_1P3GA) {
  863. devp->compfullscale = LSM303DLHC_COMP_1P3GA;
  864. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  865. if(devp->config->compsensitivity == NULL) {
  866. if(i != 2) {
  867. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_1P3GA;
  868. }
  869. else {
  870. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_1P3GA;
  871. }
  872. }
  873. else {
  874. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  875. }
  876. }
  877. }
  878. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_1P9GA) {
  879. devp->compfullscale = LSM303DLHC_COMP_1P9GA;
  880. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  881. if(devp->config->compsensitivity == NULL) {
  882. if(i != 2) {
  883. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_1P9GA;
  884. }
  885. else {
  886. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_1P9GA;
  887. }
  888. }
  889. else {
  890. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  891. }
  892. }
  893. }
  894. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_2P5GA) {
  895. devp->compfullscale = LSM303DLHC_COMP_2P5GA;
  896. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  897. if(devp->config->compsensitivity == NULL) {
  898. if(i != 2) {
  899. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_2P5GA;
  900. }
  901. else {
  902. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_2P5GA;
  903. }
  904. }
  905. else {
  906. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  907. }
  908. }
  909. }
  910. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_4P0GA) {
  911. devp->compfullscale = LSM303DLHC_COMP_4P0GA;
  912. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  913. if(devp->config->compsensitivity == NULL) {
  914. if(i != 2) {
  915. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_4P0GA;
  916. }
  917. else {
  918. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_4P0GA;
  919. }
  920. }
  921. else {
  922. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  923. }
  924. }
  925. }
  926. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_4P7GA) {
  927. devp->compfullscale = LSM303DLHC_COMP_4P7GA;
  928. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  929. if(devp->config->compsensitivity == NULL) {
  930. if(i != 2) {
  931. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_4P7GA;
  932. }
  933. else {
  934. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_4P7GA;
  935. }
  936. }
  937. else {
  938. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  939. }
  940. }
  941. }
  942. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_5P6GA) {
  943. devp->compfullscale = LSM303DLHC_COMP_5P6GA;
  944. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  945. if(devp->config->compsensitivity == NULL) {
  946. if(i != 2) {
  947. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_5P6GA;
  948. }
  949. else {
  950. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_5P6GA;
  951. }
  952. }
  953. else {
  954. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  955. }
  956. }
  957. }
  958. else if(devp->config->compfullscale == LSM303DLHC_COMP_FS_8P1GA) {
  959. devp->compfullscale = LSM303DLHC_COMP_8P1GA;
  960. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  961. if(devp->config->compsensitivity == NULL) {
  962. if(i != 2) {
  963. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_XY_8P1GA;
  964. }
  965. else {
  966. devp->compsensitivity[i] = LSM303DLHC_COMP_SENS_Z_8P1GA;
  967. }
  968. }
  969. else {
  970. devp->compsensitivity[i] = devp->config->compsensitivity[i];
  971. }
  972. }
  973. }
  974. else
  975. osalDbgAssert(FALSE, "lsm303dlhcStart(), compass full scale issue");
  976. /* Storing bias information */
  977. if(devp->config->compbias != NULL)
  978. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++)
  979. devp->compbias[i] = devp->config->compbias[i];
  980. else
  981. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++)
  982. devp->compbias[i] = LSM303DLHC_COMP_BIAS;
  983. /* This is the MEMS transient recovery time */
  984. osalThreadSleepMilliseconds(5);
  985. devp->state = LSM303DLHC_READY;
  986. }
  987. /**
  988. * @brief Deactivates the LSM303DLHC Complex Driver peripheral.
  989. *
  990. * @param[in] devp pointer to the @p LSM303DLHCDriver object
  991. *
  992. * @api
  993. */
  994. void lsm303dlhcStop(LSM303DLHCDriver *devp) {
  995. uint8_t cr[2];
  996. osalDbgCheck(devp != NULL);
  997. osalDbgAssert((devp->state == LSM303DLHC_STOP) ||
  998. (devp->state == LSM303DLHC_READY),
  999. "lsm303dlhcStop(), invalid state");
  1000. if (devp->state == LSM303DLHC_READY) {
  1001. #if LSM303DLHC_SHARED_I2C
  1002. i2cAcquireBus((devp)->config->i2cp);
  1003. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  1004. #endif /* LSM303DLHC_SHARED_I2C */
  1005. /* Disabling accelerometer. */
  1006. cr[0] = LSM303DLHC_AD_ACC_CTRL_REG1;
  1007. cr[1] = LSM303DLHC_ACC_AE_DISABLED | LSM303DLHC_ACC_ODR_PD;
  1008. lsm303dlhcI2CWriteRegister(devp->config->i2cp, LSM303DLHC_SAD_ACC,
  1009. cr, 1);
  1010. /* Disabling compass. */
  1011. cr[0] = LSM303DLHC_AD_COMP_MR_REG;
  1012. cr[1] = LSM303DLHC_COMP_MD_SLEEP;
  1013. lsm303dlhcI2CWriteRegister(devp->config->i2cp, LSM303DLHC_SAD_COMP,
  1014. cr, 1);
  1015. i2cStop((devp)->config->i2cp);
  1016. #if LSM303DLHC_SHARED_I2C
  1017. i2cReleaseBus((devp)->config->i2cp);
  1018. #endif /* LSM303DLHC_SHARED_I2C */
  1019. }
  1020. devp->state = LSM303DLHC_STOP;
  1021. }
  1022. /** @} */