lis3dsh.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. ChibiOS - Copyright (C) 2016..2018 Rocco Marco Guglielmi
  3. This file is part of ChibiOS.
  4. ChibiOS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. ChibiOS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file lis3dsh.c
  17. * @brief LIS3DSH MEMS interface module code.
  18. *
  19. * @addtogroup LIS3DSH
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #include "hal.h"
  24. #include "lis3dsh.h"
  25. /*===========================================================================*/
  26. /* Driver local definitions. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Driver exported variables. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Driver local variables and types. */
  33. /*===========================================================================*/
  34. /*===========================================================================*/
  35. /* Driver local functions. */
  36. /*===========================================================================*/
  37. #if (LIS3DSH_USE_SPI) || defined(__DOXYGEN__)
  38. /**
  39. * @brief Reads a generic register value using SPI.
  40. * @pre The SPI interface must be initialized and the driver started.
  41. * @note Multiple write/read requires proper settings in CTRL_REG6.
  42. *
  43. * @param[in] spip pointer to the SPI interface
  44. * @param[in] reg starting register address
  45. * @param[in] n number of adjacent registers to write
  46. * @param[in] b pointer to a buffer.
  47. */
  48. static void lis3dshSPIReadRegister(SPIDriver *spip, uint8_t reg, size_t n,
  49. uint8_t* b) {
  50. uint8_t cmd;
  51. cmd = reg | LIS3DSH_RW;
  52. spiSelect(spip);
  53. spiSend(spip, 1, &cmd);
  54. spiReceive(spip, n, b);
  55. spiUnselect(spip);
  56. }
  57. /**
  58. * @brief Writes a value into a generic register using SPI.
  59. * @pre The SPI interface must be initialized and the driver started.
  60. * @note Multiple write/read requires proper settings in CTRL_REG6.
  61. *
  62. * @param[in] spip pointer to the SPI interface
  63. * @param[in] reg starting register address
  64. * @param[in] n number of adjacent registers to write
  65. * @param[in] b pointer to a buffer of values.
  66. */
  67. static void lis3dshSPIWriteRegister(SPIDriver *spip, uint8_t reg, size_t n,
  68. uint8_t* b) {
  69. uint8_t cmd;
  70. cmd = reg;
  71. spiSelect(spip);
  72. spiSend(spip, 1, &cmd);
  73. spiSend(spip, n, b);
  74. spiUnselect(spip);
  75. }
  76. #endif /* LIS3DSH_USE_SPI */
  77. /**
  78. * @brief Return the number of axes of the BaseAccelerometer.
  79. *
  80. * @param[in] ip pointer to @p BaseAccelerometer interface.
  81. *
  82. * @return the number of axes.
  83. */
  84. static size_t acc_get_axes_number(void *ip) {
  85. (void)ip;
  86. return LIS3DSH_ACC_NUMBER_OF_AXES;
  87. }
  88. /**
  89. * @brief Retrieves raw data from the BaseAccelerometer.
  90. * @note This data is retrieved from MEMS register without any algebraical
  91. * manipulation.
  92. * @note The axes array must be at least the same size of the
  93. * BaseAccelerometer axes number.
  94. *
  95. * @param[in] ip pointer to @p BaseAccelerometer interface.
  96. * @param[out] axes a buffer which would be filled with raw data.
  97. *
  98. * @return The operation status.
  99. * @retval MSG_OK if the function succeeded.
  100. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  101. * be retrieved using @p i2cGetErrors().
  102. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  103. */
  104. static msg_t acc_read_raw(void *ip, int32_t axes[]) {
  105. LIS3DSHDriver* devp;
  106. uint8_t buff [LIS3DSH_ACC_NUMBER_OF_AXES * 2], i;
  107. int16_t tmp;
  108. msg_t msg = MSG_OK;
  109. osalDbgCheck((ip != NULL) && (axes != NULL));
  110. /* Getting parent instance pointer.*/
  111. devp = objGetInstance(LIS3DSHDriver*, (BaseAccelerometer*)ip);
  112. osalDbgAssert((devp->state == LIS3DSH_READY),
  113. "acc_read_raw(), invalid state");
  114. #if LIS3DSH_USE_SPI
  115. #if LIS3DSH_SHARED_SPI
  116. osalDbgAssert((devp->config->spip->state == SPI_READY),
  117. "acc_read_raw(), channel not ready");
  118. spiAcquireBus(devp->config->spip);
  119. spiStart(devp->config->spip,
  120. devp->config->spicfg);
  121. #endif /* LIS3DSH_SHARED_SPI */
  122. lis3dshSPIReadRegister(devp->config->spip, LIS3DSH_AD_OUT_X_L,
  123. LIS3DSH_ACC_NUMBER_OF_AXES * 2, buff);
  124. #if LIS3DSH_SHARED_SPI
  125. spiReleaseBus(devp->config->spip);
  126. #endif /* LIS3DSH_SHARED_SPI */
  127. #endif /* LIS3DSH_USE_SPI */
  128. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++) {
  129. tmp = buff[2 * i] + (buff[2 * i + 1] << 8);
  130. axes[i] = (int32_t)tmp;
  131. }
  132. return msg;
  133. }
  134. /**
  135. * @brief Retrieves cooked data from the BaseAccelerometer.
  136. * @note This data is manipulated according to the formula
  137. * cooked = (raw * sensitivity) - bias.
  138. * @note Final data is expressed as milli-G.
  139. * @note The axes array must be at least the same size of the
  140. * BaseAccelerometer axes number.
  141. *
  142. * @param[in] ip pointer to @p BaseAccelerometer interface.
  143. * @param[out] axes a buffer which would be filled with cooked data.
  144. *
  145. * @return The operation status.
  146. * @retval MSG_OK if the function succeeded.
  147. * @retval MSG_RESET if one or more I2C errors occurred, the errors can
  148. * be retrieved using @p i2cGetErrors().
  149. * @retval MSG_TIMEOUT if a timeout occurred before operation end.
  150. */
  151. static msg_t acc_read_cooked(void *ip, float axes[]) {
  152. LIS3DSHDriver* devp;
  153. uint32_t i;
  154. int32_t raw[LIS3DSH_ACC_NUMBER_OF_AXES];
  155. msg_t msg;
  156. osalDbgCheck((ip != NULL) && (axes != NULL));
  157. /* Getting parent instance pointer.*/
  158. devp = objGetInstance(LIS3DSHDriver*, (BaseAccelerometer*)ip);
  159. osalDbgAssert((devp->state == LIS3DSH_READY),
  160. "acc_read_cooked(), invalid state");
  161. msg = acc_read_raw(ip, raw);
  162. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++) {
  163. axes[i] = (raw[i] * devp->accsensitivity[i]) - devp->accbias[i];
  164. }
  165. return msg;
  166. }
  167. /**
  168. * @brief Set bias values for the BaseAccelerometer.
  169. * @note Bias must be expressed as milli-G.
  170. * @note The bias buffer must be at least the same size of the
  171. * BaseAccelerometer axes number.
  172. *
  173. * @param[in] ip pointer to @p BaseAccelerometer interface.
  174. * @param[in] bp a buffer which contains biases.
  175. *
  176. * @return The operation status.
  177. * @retval MSG_OK if the function succeeded.
  178. */
  179. static msg_t acc_set_bias(void *ip, float *bp) {
  180. LIS3DSHDriver* devp;
  181. uint32_t i;
  182. msg_t msg = MSG_OK;
  183. osalDbgCheck((ip != NULL) && (bp != NULL));
  184. /* Getting parent instance pointer.*/
  185. devp = objGetInstance(LIS3DSHDriver*, (BaseAccelerometer*)ip);
  186. osalDbgAssert((devp->state == LIS3DSH_READY),
  187. "acc_set_bias(), invalid state");
  188. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++) {
  189. devp->accbias[i] = bp[i];
  190. }
  191. return msg;
  192. }
  193. /**
  194. * @brief Reset bias values for the BaseAccelerometer.
  195. * @note Default biases value are obtained from device datasheet when
  196. * available otherwise they are considered zero.
  197. *
  198. * @param[in] ip pointer to @p BaseAccelerometer interface.
  199. *
  200. * @return The operation status.
  201. * @retval MSG_OK if the function succeeded.
  202. */
  203. static msg_t acc_reset_bias(void *ip) {
  204. LIS3DSHDriver* devp;
  205. uint32_t i;
  206. msg_t msg = MSG_OK;
  207. osalDbgCheck(ip != NULL);
  208. /* Getting parent instance pointer.*/
  209. devp = objGetInstance(LIS3DSHDriver*, (BaseAccelerometer*)ip);
  210. osalDbgAssert((devp->state == LIS3DSH_READY),
  211. "acc_reset_bias(), invalid state");
  212. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  213. devp->accbias[i] = LIS3DSH_ACC_BIAS;
  214. return msg;
  215. }
  216. /**
  217. * @brief Set sensitivity values for the BaseAccelerometer.
  218. * @note Sensitivity must be expressed as milli-G/LSB.
  219. * @note The sensitivity buffer must be at least the same size of the
  220. * BaseAccelerometer axes number.
  221. *
  222. * @param[in] ip pointer to @p BaseAccelerometer interface.
  223. * @param[in] sp a buffer which contains sensitivities.
  224. *
  225. * @return The operation status.
  226. * @retval MSG_OK if the function succeeded.
  227. */
  228. static msg_t acc_set_sensivity(void *ip, float *sp) {
  229. LIS3DSHDriver* devp;
  230. uint32_t i;
  231. msg_t msg = MSG_OK;
  232. /* Getting parent instance pointer.*/
  233. devp = objGetInstance(LIS3DSHDriver*, (BaseAccelerometer*)ip);
  234. osalDbgCheck((ip != NULL) && (sp != NULL));
  235. osalDbgAssert((devp->state == LIS3DSH_READY),
  236. "acc_set_sensivity(), invalid state");
  237. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++) {
  238. devp->accsensitivity[i] = sp[i];
  239. }
  240. return msg;
  241. }
  242. /**
  243. * @brief Reset sensitivity values for the BaseAccelerometer.
  244. * @note Default sensitivities value are obtained from device datasheet.
  245. *
  246. * @param[in] ip pointer to @p BaseAccelerometer interface.
  247. *
  248. * @return The operation status.
  249. * @retval MSG_OK if the function succeeded.
  250. * @retval MSG_RESET otherwise.
  251. */
  252. static msg_t acc_reset_sensivity(void *ip) {
  253. LIS3DSHDriver* devp;
  254. uint32_t i;
  255. msg_t msg = MSG_OK;
  256. osalDbgCheck(ip != NULL);
  257. /* Getting parent instance pointer.*/
  258. devp = objGetInstance(LIS3DSHDriver*, (BaseAccelerometer*)ip);
  259. osalDbgAssert((devp->state == LIS3DSH_READY),
  260. "acc_reset_sensivity(), invalid state");
  261. if(devp->config->accfullscale == LIS3DSH_ACC_FS_2G)
  262. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  263. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_2G;
  264. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_4G)
  265. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  266. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_4G;
  267. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_6G)
  268. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  269. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_6G;
  270. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_8G)
  271. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  272. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_8G;
  273. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_16G)
  274. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  275. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_16G;
  276. else {
  277. osalDbgAssert(FALSE,
  278. "acc_reset_sensivity(), accelerometer full scale issue");
  279. return MSG_RESET;
  280. }
  281. return msg;
  282. }
  283. /**
  284. * @brief Changes the LIS3DSHDriver 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 LIS3DSHDriver 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(LIS3DSHDriver *devp, lis3dsh_acc_fs_t fs) {
  297. float newfs, scale;
  298. uint8_t i, cr;
  299. msg_t msg;
  300. osalDbgCheck(devp != NULL);
  301. osalDbgAssert((devp->state == LIS3DSH_READY),
  302. "acc_set_full_scale(), invalid state");
  303. osalDbgAssert((devp->config->spip->state == SPI_READY),
  304. "acc_set_full_scale(), channel not ready");
  305. /* Computing new fullscale value.*/
  306. if(fs == LIS3DSH_ACC_FS_2G) {
  307. newfs = LIS3DSH_ACC_2G;
  308. }
  309. else if(fs == LIS3DSH_ACC_FS_4G) {
  310. newfs = LIS3DSH_ACC_4G;
  311. }
  312. else if(fs == LIS3DSH_ACC_FS_6G) {
  313. newfs = LIS3DSH_ACC_6G;
  314. }
  315. else if(fs == LIS3DSH_ACC_FS_8G) {
  316. newfs = LIS3DSH_ACC_8G;
  317. }
  318. else if(fs == LIS3DSH_ACC_FS_16G) {
  319. newfs = LIS3DSH_ACC_16G;
  320. }
  321. else {
  322. msg = MSG_RESET;
  323. return msg;
  324. }
  325. if(newfs != devp->accfullscale) {
  326. /* Computing scale value.*/
  327. scale = newfs / devp->accfullscale;
  328. devp->accfullscale = newfs;
  329. #if LIS3DSH_USE_SPI
  330. #if LIS3DSH_SHARED_SPI
  331. spiAcquireBus(devp->config->spip);
  332. spiStart(devp->config->spip,
  333. devp->config->spicfg);
  334. #endif /* LIS3DSH_SHARED_SPI */
  335. /* Getting data from register.*/
  336. lis3dshSPIReadRegister(devp->config->spip, LIS3DSH_AD_CTRL_REG5, 1, &cr);
  337. #if LIS3DSH_SHARED_SPI
  338. spiReleaseBus(devp->config->spip);
  339. #endif /* LIS3DSH_SHARED_SPI */
  340. #endif /* LIS3DSH_USE_SPI */
  341. cr &= ~(LIS3DSH_CTRL_REG5_FS_MASK);
  342. cr |= fs;
  343. #if LIS3DSH_USE_SPI
  344. #if LIS3DSH_SHARED_SPI
  345. spiAcquireBus(devp->config->spip);
  346. spiStart(devp->config->spip,
  347. devp->config->spicfg);
  348. #endif /* LIS3DSH_SHARED_SPI */
  349. /* Getting data from register.*/
  350. lis3dshSPIWriteRegister(devp->config->spip, LIS3DSH_AD_CTRL_REG5, 1, &cr);
  351. #if LIS3DSH_SHARED_SPI
  352. spiReleaseBus(devp->config->spip);
  353. #endif /* LIS3DSH_SHARED_SPI */
  354. #endif /* LIS3DSH_USE_SPI */
  355. /* Scaling sensitivity and bias. Re-calibration is suggested anyway. */
  356. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++) {
  357. devp->accsensitivity[i] *= scale;
  358. devp->accbias[i] *= scale;
  359. }
  360. }
  361. return msg;
  362. }
  363. static const struct LIS3DSHVMT vmt_device = {
  364. (size_t)0,
  365. acc_set_full_scale
  366. };
  367. static const struct BaseAccelerometerVMT vmt_accelerometer = {
  368. sizeof(struct LIS3DSHVMT*),
  369. acc_get_axes_number, acc_read_raw, acc_read_cooked,
  370. acc_set_bias, acc_reset_bias, acc_set_sensivity, acc_reset_sensivity
  371. };
  372. /*===========================================================================*/
  373. /* Driver exported functions. */
  374. /*===========================================================================*/
  375. /**
  376. * @brief Initializes an instance.
  377. *
  378. * @param[out] devp pointer to the @p LIS3DSHDriver object
  379. *
  380. * @init
  381. */
  382. void lis3dshObjectInit(LIS3DSHDriver *devp) {
  383. devp->vmt = &vmt_device;
  384. devp->acc_if.vmt = &vmt_accelerometer;
  385. devp->config = NULL;
  386. devp->accaxes = LIS3DSH_ACC_NUMBER_OF_AXES;
  387. devp->state = LIS3DSH_STOP;
  388. }
  389. /**
  390. * @brief Configures and activates LIS3DSH Complex Driver peripheral.
  391. *
  392. * @param[in] devp pointer to the @p LIS3DSHDriver object
  393. * @param[in] config pointer to the @p LIS3DSHConfig object
  394. *
  395. * @api
  396. */
  397. void lis3dshStart(LIS3DSHDriver *devp, const LIS3DSHConfig *config) {
  398. uint32_t i;
  399. uint8_t cr;
  400. osalDbgCheck((devp != NULL) && (config != NULL));
  401. osalDbgAssert((devp->state == LIS3DSH_STOP) ||
  402. (devp->state == LIS3DSH_READY),
  403. "lis3dshStart(), invalid state");
  404. devp->config = config;
  405. /* Control register 4 configuration block.*/
  406. {
  407. cr = LIS3DSH_CTRL_REG4_XEN | LIS3DSH_CTRL_REG4_YEN | LIS3DSH_CTRL_REG4_ZEN |
  408. devp->config->accoutputdatarate;
  409. #if LIS3DSH_USE_ADVANCED || defined(__DOXYGEN__)
  410. cr |= devp->config->accblockdataupdate;
  411. #endif
  412. }
  413. #if LIS3DSH_USE_SPI
  414. #if LIS3DSH_SHARED_SPI
  415. spiAcquireBus(devp->config->spip);
  416. #endif /* LIS3DSH_SHARED_SPI */
  417. spiStart(devp->config->spip, devp->config->spicfg);
  418. lis3dshSPIWriteRegister(devp->config->spip, LIS3DSH_AD_CTRL_REG4, 1, &cr);
  419. #if LIS3DSH_SHARED_SPI
  420. spiReleaseBus(devp->config->spip);
  421. #endif /* LIS3DSH_SHARED_SPI */
  422. #endif /* LIS3DSH_USE_SPI */
  423. /* Control register 5 configuration block.*/
  424. {
  425. cr = devp->config->accfullscale;
  426. #if LIS3DSH_USE_ADVANCED || defined(__DOXYGEN__)
  427. cr |= devp->config->accantialiasing;
  428. #endif
  429. }
  430. #if LIS3DSH_USE_SPI
  431. #if LIS3DSH_SHARED_SPI
  432. spiAcquireBus(devp->config->spip);
  433. spiStart(devp->config->spip, devp->config->spicfg);
  434. #endif /* LIS3DSH_SHARED_SPI */
  435. lis3dshSPIWriteRegister(devp->config->spip, LIS3DSH_AD_CTRL_REG5, 1, &cr);
  436. #if LIS3DSH_SHARED_SPI
  437. spiReleaseBus(devp->config->spip);
  438. #endif /* LIS3DSH_SHARED_SPI */
  439. #endif /* LIS3DSH_USE_SPI */
  440. /* Control register 6 configuration block.*/
  441. {
  442. cr = LIS3DSH_CTRL_REG6_ADD_INC;
  443. #if LIS3DSH_USE_ADVANCED || defined(__DOXYGEN__)
  444. cr |= devp->config->accblockdataupdate;
  445. #endif
  446. }
  447. #if LIS3DSH_USE_SPI
  448. #if LIS3DSH_SHARED_SPI
  449. spiAcquireBus(devp->config->spip);
  450. spiStart(devp->config->spip, devp->config->spicfg);
  451. #endif /* LIS3DSH_SHARED_SPI */
  452. lis3dshSPIWriteRegister(devp->config->spip, LIS3DSH_AD_CTRL_REG6, 1, &cr);
  453. #if LIS3DSH_SHARED_SPI
  454. spiReleaseBus(devp->config->spip);
  455. #endif /* LIS3DSH_SHARED_SPI */
  456. #endif /* LIS3DSH_USE_SPI */
  457. /* Storing sensitivity information according to user setting */
  458. if(devp->config->accfullscale == LIS3DSH_ACC_FS_2G) {
  459. devp->accfullscale = LIS3DSH_ACC_2G;
  460. if(devp->config->accsensitivity == NULL)
  461. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  462. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_2G;
  463. else
  464. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  465. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  466. }
  467. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_4G) {
  468. devp->accfullscale = LIS3DSH_ACC_4G;
  469. if(devp->config->accsensitivity == NULL)
  470. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  471. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_4G;
  472. else
  473. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  474. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  475. }
  476. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_6G) {
  477. devp->accfullscale = LIS3DSH_ACC_6G;
  478. if(devp->config->accsensitivity == NULL)
  479. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  480. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_6G;
  481. else
  482. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  483. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  484. }
  485. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_8G) {
  486. devp->accfullscale = LIS3DSH_ACC_8G;
  487. if(devp->config->accsensitivity == NULL)
  488. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  489. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_8G;
  490. else
  491. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  492. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  493. }
  494. else if(devp->config->accfullscale == LIS3DSH_ACC_FS_16G) {
  495. devp->accfullscale = LIS3DSH_ACC_16G;
  496. if(devp->config->accsensitivity == NULL)
  497. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  498. devp->accsensitivity[i] = LIS3DSH_ACC_SENS_16G;
  499. else
  500. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  501. devp->accsensitivity[i] = devp->config->accsensitivity[i];
  502. }
  503. else {
  504. osalDbgAssert(FALSE, "lis3dshStart(), accelerometer full scale issue");
  505. }
  506. /* Storing bias information according to user setting */
  507. if(devp->config->accbias != NULL)
  508. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  509. devp->accbias[i] = devp->config->accbias[i];
  510. else
  511. for(i = 0; i < LIS3DSH_ACC_NUMBER_OF_AXES; i++)
  512. devp->accbias[i] = LIS3DSH_ACC_BIAS;
  513. /* This is the Accelerometer transient recovery time */
  514. osalThreadSleepMilliseconds(10);
  515. devp->state = LIS3DSH_READY;
  516. }
  517. /**
  518. * @brief Deactivates the LIS3DSH Complex Driver peripheral.
  519. *
  520. * @param[in] devp pointer to the @p LIS3DSHDriver object
  521. *
  522. * @api
  523. */
  524. void lis3dshStop(LIS3DSHDriver *devp) {
  525. uint8_t cr4;
  526. osalDbgCheck(devp != NULL);
  527. osalDbgAssert((devp->state == LIS3DSH_STOP) ||
  528. (devp->state == LIS3DSH_READY),
  529. "lis3dshStop(), invalid state");
  530. if (devp->state == LIS3DSH_READY) {
  531. #if (LIS3DSH_USE_SPI)
  532. #if LIS3DSH_SHARED_SPI
  533. spiAcquireBus(devp->config->spip);
  534. spiStart(devp->config->spip,
  535. devp->config->spicfg);
  536. #endif /* LIS3DSH_SHARED_SPI */
  537. /* Disabling all axes and enabling power down mode.*/
  538. cr4 = 0;
  539. lis3dshSPIWriteRegister(devp->config->spip, LIS3DSH_AD_CTRL_REG4,
  540. 1, &cr4);
  541. spiStop(devp->config->spip);
  542. #if LIS3DSH_SHARED_SPI
  543. spiReleaseBus(devp->config->spip);
  544. #endif /* LIS3DSH_SHARED_SPI */
  545. #endif /* LIS3DSH_USE_SPI */
  546. }
  547. devp->state = LIS3DSH_STOP;
  548. }
  549. /** @} */