l3gd20.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 l3gd20.c
  17. * @brief L3GD20 MEMS interface module code.
  18. *
  19. * @addtogroup L3GD20
  20. * @ingroup EX_ST
  21. * @{
  22. */
  23. #include "hal.h"
  24. #include "l3gd20.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 (L3GD20_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. *
  42. * @param[in] spip pointer to the SPI interface
  43. * @param[in] reg starting register address
  44. * @param[in] n number of consecutive registers to read
  45. * @param[in] b pointer to an output buffer.
  46. */
  47. static void l3gd20SPIReadRegister(SPIDriver *spip, uint8_t reg, size_t n,
  48. uint8_t* b) {
  49. uint8_t cmd;
  50. (n == 1) ? (cmd = reg | L3GD20_RW) : (cmd = reg | L3GD20_RW | L3GD20_MS);
  51. spiSelect(spip);
  52. spiSend(spip, 1, &cmd);
  53. spiReceive(spip, n, b);
  54. spiUnselect(spip);
  55. }
  56. /**
  57. * @brief Writes a value into a generic register using SPI.
  58. * @pre The SPI interface must be initialized and the driver started.
  59. *
  60. * @param[in] spip pointer to the SPI interface
  61. * @param[in] reg starting register address
  62. * @param[in] n number of adjacent registers to write
  63. * @param[in] b pointer to a buffer of values.
  64. */
  65. static void l3gd20SPIWriteRegister(SPIDriver *spip, uint8_t reg, size_t n,
  66. uint8_t* b) {
  67. uint8_t cmd;
  68. (n == 1) ? (cmd = reg) : (cmd = reg | L3GD20_MS);
  69. spiSelect(spip);
  70. spiSend(spip, 1, &cmd);
  71. spiSend(spip, n, b);
  72. spiUnselect(spip);
  73. }
  74. #endif /* L3GD20_USE_SPI */
  75. /**
  76. * @brief Return the number of axes of the BaseGyroscope.
  77. *
  78. * @param[in] ip pointer to @p BaseGyroscope interface.
  79. *
  80. * @return the number of axes.
  81. */
  82. static size_t gyro_get_axes_number(void *ip) {
  83. (void)ip;
  84. return L3GD20_GYRO_NUMBER_OF_AXES;
  85. }
  86. /**
  87. * @brief Retrieves raw data from the BaseGyroscope.
  88. * @note This data is retrieved from MEMS register without any algebraical
  89. * manipulation.
  90. * @note The axes array must be at least the same size of the
  91. * BaseGyroscope axes number.
  92. *
  93. * @param[in] ip pointer to @p BaseGyroscope interface.
  94. * @param[out] axes a buffer which would be filled with raw data.
  95. *
  96. * @return The operation status.
  97. * @retval MSG_OK if the function succeeded.
  98. */
  99. static msg_t gyro_read_raw(void *ip, int32_t axes[L3GD20_GYRO_NUMBER_OF_AXES]) {
  100. L3GD20Driver* devp;
  101. int16_t tmp;
  102. uint8_t i, buff [2 * L3GD20_GYRO_NUMBER_OF_AXES];
  103. msg_t msg = MSG_OK;
  104. osalDbgCheck((ip != NULL) && (axes != NULL));
  105. /* Getting parent instance pointer.*/
  106. devp = objGetInstance(L3GD20Driver*, (BaseGyroscope*)ip);
  107. osalDbgAssert((devp->state == L3GD20_READY),
  108. "gyro_read_raw(), invalid state");
  109. #if L3GD20_USE_SPI
  110. osalDbgAssert((devp->config->spip->state == SPI_READY),
  111. "gyro_read_raw(), channel not ready");
  112. #if L3GD20_SHARED_SPI
  113. spiAcquireBus(devp->config->spip);
  114. spiStart(devp->config->spip,
  115. devp->config->spicfg);
  116. #endif /* L3GD20_SHARED_SPI */
  117. l3gd20SPIReadRegister(devp->config->spip, L3GD20_AD_OUT_X_L,
  118. L3GD20_GYRO_NUMBER_OF_AXES * 2, buff);
  119. #if L3GD20_SHARED_SPI
  120. spiReleaseBus(devp->config->spip);
  121. #endif /* L3GD20_SHARED_SPI */
  122. #endif /* L3GD20_USE_SPI */
  123. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  124. tmp = buff[2 * i] + (buff[2 * i + 1] << 8);
  125. axes[i] = (int32_t)tmp;
  126. }
  127. return msg;
  128. }
  129. /**
  130. * @brief Retrieves cooked data from the BaseGyroscope.
  131. * @note This data is manipulated according to the formula
  132. * cooked = (raw * sensitivity) - bias.
  133. * @note Final data is expressed as DPS.
  134. * @note The axes array must be at least the same size of the
  135. * BaseGyroscope axes number.
  136. *
  137. * @param[in] ip pointer to @p BaseGyroscope interface.
  138. * @param[out] axes a buffer which would be filled with cooked data.
  139. *
  140. * @return The operation status.
  141. * @retval MSG_OK if the function succeeded.
  142. */
  143. static msg_t gyro_read_cooked(void *ip, float axes[]) {
  144. L3GD20Driver* devp;
  145. uint32_t i;
  146. int32_t raw[L3GD20_GYRO_NUMBER_OF_AXES];
  147. msg_t msg;
  148. osalDbgCheck((ip != NULL) && (axes != NULL));
  149. /* Getting parent instance pointer.*/
  150. devp = objGetInstance(L3GD20Driver*, (BaseGyroscope*)ip);
  151. osalDbgAssert((devp->state == L3GD20_READY),
  152. "gyro_read_cooked(), invalid state");
  153. msg = gyro_read_raw(ip, raw);
  154. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++){
  155. axes[i] = (raw[i] * devp->gyrosensitivity[i]) - devp->gyrobias[i];
  156. }
  157. return msg;
  158. }
  159. /**
  160. * @brief Samples bias values for the BaseGyroscope.
  161. * @note The L3GD20 shall not be moved during the whole procedure.
  162. * @note After this function internal bias is automatically updated.
  163. * @note The behavior of this function depends on @p L3GD20_BIAS_ACQ_TIMES
  164. * and @p L3GD20_BIAS_SETTLING_US.
  165. *
  166. * @param[in] ip pointer to @p BaseGyroscope interface.
  167. *
  168. * @return The operation status.
  169. * @retval MSG_OK if the function succeeded.
  170. */
  171. static msg_t gyro_sample_bias(void *ip) {
  172. L3GD20Driver* devp;
  173. uint32_t i, j;
  174. int32_t raw[L3GD20_GYRO_NUMBER_OF_AXES];
  175. int32_t buff[L3GD20_GYRO_NUMBER_OF_AXES] = {0, 0, 0};
  176. msg_t msg;
  177. osalDbgCheck(ip != NULL);
  178. /* Getting parent instance pointer.*/
  179. devp = objGetInstance(L3GD20Driver*, (BaseGyroscope*)ip);
  180. osalDbgAssert((devp->state == L3GD20_READY),
  181. "gyro_sample_bias(), invalid state");
  182. #if L3GD20_USE_SPI
  183. osalDbgAssert((devp->config->spip->state == SPI_READY),
  184. "gyro_sample_bias(), channel not ready");
  185. #endif
  186. for(i = 0; i < L3GD20_BIAS_ACQ_TIMES; i++){
  187. msg = gyro_read_raw(ip, raw);
  188. if(msg != MSG_OK)
  189. return msg;
  190. for(j = 0; j < L3GD20_GYRO_NUMBER_OF_AXES; j++){
  191. buff[j] += raw[j];
  192. }
  193. osalThreadSleepMicroseconds(L3GD20_BIAS_SETTLING_US);
  194. }
  195. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++){
  196. devp->gyrobias[i] = (buff[i] / L3GD20_BIAS_ACQ_TIMES);
  197. devp->gyrobias[i] *= devp->gyrosensitivity[i];
  198. }
  199. return msg;
  200. }
  201. /**
  202. * @brief Set bias values for the BaseGyroscope.
  203. * @note Bias must be expressed as DPS.
  204. * @note The bias buffer must be at least the same size of the BaseGyroscope
  205. * axes number.
  206. *
  207. * @param[in] ip pointer to @p BaseGyroscope interface.
  208. * @param[in] bp a buffer which contains biases.
  209. *
  210. * @return The operation status.
  211. * @retval MSG_OK if the function succeeded.
  212. */
  213. static msg_t gyro_set_bias(void *ip, float *bp) {
  214. L3GD20Driver* devp;
  215. uint32_t i;
  216. msg_t msg = MSG_OK;
  217. osalDbgCheck((ip != NULL) && (bp != NULL));
  218. /* Getting parent instance pointer.*/
  219. devp = objGetInstance(L3GD20Driver*, (BaseGyroscope*)ip);
  220. osalDbgAssert((devp->state == L3GD20_READY),
  221. "gyro_set_bias(), invalid state");
  222. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  223. devp->gyrobias[i] = bp[i];
  224. }
  225. return msg;
  226. }
  227. /**
  228. * @brief Reset bias values for the BaseGyroscope.
  229. * @note Default biases value are obtained from device datasheet when
  230. * available otherwise they are considered zero.
  231. *
  232. * @param[in] ip pointer to @p BaseGyroscope interface.
  233. *
  234. * @return The operation status.
  235. * @retval MSG_OK if the function succeeded.
  236. */
  237. static msg_t gyro_reset_bias(void *ip) {
  238. L3GD20Driver* devp;
  239. uint32_t i;
  240. msg_t msg = MSG_OK;
  241. osalDbgCheck(ip != NULL);
  242. /* Getting parent instance pointer.*/
  243. devp = objGetInstance(L3GD20Driver*, (BaseGyroscope*)ip);
  244. osalDbgAssert((devp->state == L3GD20_READY),
  245. "gyro_reset_bias(), invalid state");
  246. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++)
  247. devp->gyrobias[i] = L3GD20_GYRO_BIAS;
  248. return msg;
  249. }
  250. /**
  251. * @brief Set sensitivity values for the BaseGyroscope.
  252. * @note Sensitivity must be expressed as DPS/LSB.
  253. * @note The sensitivity buffer must be at least the same size of the
  254. * BaseGyroscope axes number.
  255. *
  256. * @param[in] ip pointer to @p BaseGyroscope interface.
  257. * @param[in] sp a buffer which contains sensitivities.
  258. *
  259. * @return The operation status.
  260. * @retval MSG_OK if the function succeeded.
  261. */
  262. static msg_t gyro_set_sensivity(void *ip, float *sp) {
  263. L3GD20Driver* devp;
  264. uint32_t i;
  265. msg_t msg = MSG_OK;
  266. osalDbgCheck((ip != NULL) && (sp !=NULL));
  267. /* Getting parent instance pointer.*/
  268. devp = objGetInstance(L3GD20Driver*, (BaseGyroscope*)ip);
  269. osalDbgAssert((devp->state == L3GD20_READY),
  270. "gyro_set_sensivity(), invalid state");
  271. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  272. devp->gyrosensitivity[i] = sp[i];
  273. }
  274. return msg;
  275. }
  276. /**
  277. * @brief Reset sensitivity values for the BaseGyroscope.
  278. * @note Default sensitivities value are obtained from device datasheet.
  279. *
  280. * @param[in] ip pointer to @p BaseGyroscope interface.
  281. *
  282. * @return The operation status.
  283. * @retval MSG_OK if the function succeeded.
  284. * @retval MSG_RESET otherwise.
  285. */
  286. static msg_t gyro_reset_sensivity(void *ip) {
  287. L3GD20Driver* devp;
  288. uint32_t i;
  289. msg_t msg = MSG_OK;
  290. osalDbgCheck(ip != NULL);
  291. /* Getting parent instance pointer.*/
  292. devp = objGetInstance(L3GD20Driver*, (BaseGyroscope*)ip);
  293. osalDbgAssert((devp->state == L3GD20_READY),
  294. "gyro_reset_sensivity(), invalid state");
  295. if(devp->config->gyrofullscale == L3GD20_FS_250DPS)
  296. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++)
  297. devp->gyrosensitivity[i] = L3GD20_GYRO_SENS_250DPS;
  298. else if(devp->config->gyrofullscale == L3GD20_FS_500DPS)
  299. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++)
  300. devp->gyrosensitivity[i] = L3GD20_GYRO_SENS_500DPS;
  301. else if(devp->config->gyrofullscale == L3GD20_FS_2000DPS)
  302. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++)
  303. devp->gyrosensitivity[i] = L3GD20_GYRO_SENS_2000DPS;
  304. else {
  305. osalDbgAssert(FALSE, "gyro_reset_sensivity(), full scale issue");
  306. return MSG_RESET;
  307. }
  308. return msg;
  309. }
  310. /**
  311. * @brief Changes the L3GD20Driver gyroscope fullscale value.
  312. * @note This function also rescale sensitivities and biases based on
  313. * previous and next fullscale value.
  314. * @note A recalibration is highly suggested after calling this function.
  315. *
  316. * @param[in] devp pointer to @p BaseGyroscope interface.
  317. * @param[in] fs new fullscale value.
  318. *
  319. * @return The operation status.
  320. * @retval MSG_OK if the function succeeded.
  321. * @retval MSG_RESET otherwise.
  322. */
  323. static msg_t gyro_set_full_scale(L3GD20Driver *devp, l3gd20_fs_t fs) {
  324. float newfs, scale;
  325. uint8_t i, cr;
  326. msg_t msg = MSG_OK;
  327. osalDbgCheck(devp != NULL);
  328. osalDbgAssert((devp->state == L3GD20_READY),
  329. "gyro_set_full_scale(), invalid state");
  330. #if L3GD20_USE_SPI
  331. osalDbgAssert((devp->config->spip->state == SPI_READY),
  332. "gyro_set_full_scale(), channel not ready");
  333. #endif
  334. if(fs == L3GD20_FS_250DPS) {
  335. newfs = L3GD20_250DPS;
  336. }
  337. else if(fs == L3GD20_FS_500DPS) {
  338. newfs = L3GD20_500DPS;
  339. }
  340. else if(fs == L3GD20_FS_2000DPS) {
  341. newfs = L3GD20_2000DPS;
  342. }
  343. else {
  344. return MSG_RESET;
  345. }
  346. if(newfs != devp->gyrofullscale) {
  347. scale = newfs / devp->gyrofullscale;
  348. devp->gyrofullscale = newfs;
  349. #if L3GD20_USE_SPI
  350. #if L3GD20_SHARED_SPI
  351. spiAcquireBus(devp->config->spip);
  352. spiStart(devp->config->spip,
  353. devp->config->spicfg);
  354. #endif /* L3GD20_SHARED_SPI */
  355. /* Updating register.*/
  356. l3gd20SPIReadRegister(devp->config->spip,
  357. L3GD20_AD_CTRL_REG4, 1, &cr);
  358. #if L3GD20_SHARED_SPI
  359. spiReleaseBus(devp->config->spip);
  360. #endif /* L3GD20_SHARED_SPI */
  361. #endif /* L3GD20_USE_SPI */
  362. cr &= ~(L3GD20_CTRL_REG4_FS_MASK);
  363. cr |= fs;
  364. #if L3GD20_USE_SPI
  365. #if L3GD20_SHARED_SPI
  366. spiAcquireBus(devp->config->spip);
  367. spiStart(devp->config->spip,
  368. devp->config->spicfg);
  369. #endif /* L3GD20_SHARED_SPI */
  370. l3gd20SPIWriteRegister(devp->config->spip,
  371. L3GD20_AD_CTRL_REG4, 1, &cr);
  372. #if L3GD20_SHARED_SPI
  373. spiReleaseBus(devp->config->spip);
  374. #endif /* L3GD20_SHARED_SPI */
  375. #endif /* L3GD20_USE_SPI */
  376. /* Scaling sensitivity and bias. Re-calibration is suggested anyway. */
  377. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  378. devp->gyrosensitivity[i] *= scale;
  379. devp->gyrobias[i] *= scale;
  380. }
  381. }
  382. return msg;
  383. }
  384. static const struct L3GD20VMT vmt_device = {
  385. (size_t)0,
  386. gyro_set_full_scale
  387. };
  388. static const struct BaseGyroscopeVMT vmt_gyroscope = {
  389. sizeof(struct L3GD20VMT*),
  390. gyro_get_axes_number, gyro_read_raw, gyro_read_cooked,
  391. gyro_sample_bias, gyro_set_bias, gyro_reset_bias,
  392. gyro_set_sensivity, gyro_reset_sensivity
  393. };
  394. /*===========================================================================*/
  395. /* Driver exported functions. */
  396. /*===========================================================================*/
  397. /**
  398. * @brief Initializes an instance.
  399. *
  400. * @param[out] devp pointer to the @p L3GD20Driver object
  401. *
  402. * @init
  403. */
  404. void l3gd20ObjectInit(L3GD20Driver *devp) {
  405. devp->vmt = &vmt_device;
  406. devp->gyro_if.vmt = &vmt_gyroscope;
  407. devp->config = NULL;
  408. devp->state = L3GD20_STOP;
  409. }
  410. /**
  411. * @brief Configures and activates L3GD20 Complex Driver peripheral.
  412. *
  413. * @param[in] devp pointer to the @p L3GD20Driver object
  414. * @param[in] config pointer to the @p L3GD20Config object
  415. *
  416. * @api
  417. */
  418. void l3gd20Start(L3GD20Driver *devp, const L3GD20Config *config) {
  419. uint32_t i;
  420. uint8_t cr[5] = {0, 0, 0, 0, 0};
  421. osalDbgCheck((devp != NULL) && (config != NULL));
  422. osalDbgAssert((devp->state == L3GD20_STOP) || (devp->state == L3GD20_READY),
  423. "l3gd20Start(), invalid state");
  424. devp->config = config;
  425. /* Control register 1 configuration block.*/
  426. {
  427. cr[0] = L3GD20_CTRL_REG1_XEN | L3GD20_CTRL_REG1_YEN |
  428. L3GD20_CTRL_REG1_ZEN | L3GD20_CTRL_REG1_PD |
  429. devp->config->gyrooutputdatarate;
  430. #if L3GD20_USE_ADVANCED || defined(__DOXYGEN__)
  431. cr[0] |= devp->config->gyrobandwidth;
  432. #endif
  433. }
  434. /* Control register 2 configuration block.*/
  435. {
  436. #if L3GD20_USE_ADVANCED || defined(__DOXYGEN__)
  437. if(devp->config->gyrohpmode != L3GD20_HPM_BYPASSED)
  438. cr[1] = devp->config->gyrohpmode | devp->config->gyrohpconfiguration;
  439. #endif
  440. }
  441. /* Control register 4 configuration block.*/
  442. {
  443. cr[3] = devp->config->gyrofullscale;
  444. #if L3GD20_USE_ADVANCED || defined(__DOXYGEN__)
  445. cr[3] |= devp->config->gyroblockdataupdate |
  446. devp->config->gyroendianness;
  447. #endif
  448. }
  449. /* Control register 5 configuration block.*/
  450. {
  451. #if L3GD20_USE_ADVANCED || defined(__DOXYGEN__)
  452. if((devp->config->gyrohpmode != L3GD20_HPM_BYPASSED)) {
  453. cr[4] = L3GD20_CTRL_REG5_HPEN;
  454. if(devp->config->gyrolp2mode != L3GD20_LP2M_BYPASSED) {
  455. cr[4] |= L3GD20_CTRL_REG5_INT1_SEL1 |
  456. L3GD20_CTRL_REG5_OUT_SEL1;
  457. }
  458. else {
  459. cr[4] |= L3GD20_CTRL_REG5_INT1_SEL0 |
  460. L3GD20_CTRL_REG5_OUT_SEL0;
  461. }
  462. }
  463. #endif
  464. }
  465. #if L3GD20_USE_SPI
  466. #if L3GD20_SHARED_SPI
  467. spiAcquireBus(devp->config->spip);
  468. #endif /* L3GD20_SHARED_SPI */
  469. spiStart(devp->config->spip,
  470. devp->config->spicfg);
  471. l3gd20SPIWriteRegister(devp->config->spip, L3GD20_AD_CTRL_REG1,
  472. 5, cr);
  473. #if L3GD20_SHARED_SPI
  474. spiReleaseBus(devp->config->spip);
  475. #endif /* L3GD20_SHARED_SPI */
  476. #endif /* L3GD20_USE_SPI */
  477. /* Storing sensitivity information according to full scale.*/
  478. if(devp->config->gyrofullscale == L3GD20_FS_250DPS) {
  479. devp->gyrofullscale = L3GD20_250DPS;
  480. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  481. if (devp->config->gyrosensitivity == NULL)
  482. devp->gyrosensitivity[i] = L3GD20_GYRO_SENS_250DPS;
  483. else
  484. devp->gyrosensitivity[i] = devp->config->gyrosensitivity[i];
  485. }
  486. }
  487. else if(devp->config->gyrofullscale == L3GD20_FS_500DPS) {
  488. devp->gyrofullscale = L3GD20_500DPS;
  489. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  490. if (devp->config->gyrosensitivity == NULL)
  491. devp->gyrosensitivity[i] = L3GD20_GYRO_SENS_500DPS;
  492. else
  493. devp->gyrosensitivity[i] = devp->config->gyrosensitivity[i];
  494. }
  495. }
  496. else if(devp->config->gyrofullscale == L3GD20_FS_2000DPS) {
  497. devp->gyrofullscale = L3GD20_2000DPS;
  498. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  499. if (devp->config->gyrosensitivity == NULL)
  500. devp->gyrosensitivity[i] = L3GD20_GYRO_SENS_2000DPS;
  501. else
  502. devp->gyrosensitivity[i] = devp->config->gyrosensitivity[i];
  503. }
  504. }
  505. else
  506. osalDbgAssert(FALSE, "l3gd20Start(), full scale issue");
  507. /* Storing bias information.*/
  508. if(devp->config->gyrobias != NULL) {
  509. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  510. devp->gyrobias[i] = devp->config->gyrobias[i];
  511. }
  512. }
  513. else {
  514. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++)
  515. devp->gyrobias[i] = L3GD20_GYRO_BIAS;
  516. }
  517. /* This is the Gyroscope transient recovery time.*/
  518. osalThreadSleepMilliseconds(10);
  519. devp->state = L3GD20_READY;
  520. }
  521. /**
  522. * @brief Deactivates the L3GD20 Complex Driver peripheral.
  523. *
  524. * @param[in] devp pointer to the @p L3GD20Driver object
  525. *
  526. * @api
  527. */
  528. void l3gd20Stop(L3GD20Driver *devp) {
  529. uint8_t cr1;
  530. osalDbgCheck(devp != NULL);
  531. osalDbgAssert((devp->state == L3GD20_STOP) || (devp->state == L3GD20_READY),
  532. "l3gd20Stop(), invalid state");
  533. if (devp->state == L3GD20_READY) {
  534. /* Disabling all axes and enabling power down mode.*/
  535. cr1 = 0;
  536. #if L3GD20_USE_SPI
  537. #if L3GD20_SHARED_SPI
  538. spiAcquireBus(devp->config->spip);
  539. spiStart(devp->config->spip,
  540. devp->config->spicfg);
  541. #endif /* L3GD20_SHARED_SPI */
  542. l3gd20SPIWriteRegister(devp->config->spip, L3GD20_AD_CTRL_REG1,
  543. 1, &cr1);
  544. spiStop(devp->config->spip);
  545. #if L3GD20_SHARED_SPI
  546. spiReleaseBus(devp->config->spip);
  547. #endif /* L3GD20_SHARED_SPI */
  548. #endif /* L3GD20_USE_SPI */
  549. }
  550. devp->state = L3GD20_STOP;
  551. }
  552. /** @} */