bmp085.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. ChibiOS - Copyright (C) 2016..2017 Theodore Ateba
  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 bmp085.c
  17. * @brief BMP085 Digital pressure sensor interface module code.
  18. *
  19. * @addtogroup BMP085
  20. * @ingroup EX_BOSCH
  21. * @{
  22. */
  23. #include "hal.h"
  24. #include "bmp085.h"
  25. /*==========================================================================*/
  26. /* Driver local definitions. */
  27. /*==========================================================================*/
  28. #define BMP085_SAD 0x77
  29. #define BMP085_CR_P_VAL0 0x34
  30. #define BMP085_CR_P_VAL1 0x74
  31. #define BMP085_CR_P_VAL2 0xB4
  32. #define BMP085_CR_P_VAL3 0xF4
  33. #define BMP085_CR_T_VAL 0x2E
  34. /*==========================================================================*/
  35. /* Driver exported variables. */
  36. /*==========================================================================*/
  37. /*==========================================================================*/
  38. /* Driver local variables and types. */
  39. /*==========================================================================*/
  40. /*==========================================================================*/
  41. /* Driver local functions. */
  42. /*==========================================================================*/
  43. #if (BMP085_USE_I2C) || defined(__DOXYGEN__)
  44. /**
  45. * @brief Reads registers value using I2C.
  46. * @pre The I2C interface must be initialized and the driver started.
  47. *
  48. * @param[in] i2cp pointer to the I2C interface
  49. * @param[in] reg first sub-register address
  50. * @param[out] rxbufp pointer to an output buffer
  51. * @param[in] n number of consecutive register to read
  52. *
  53. * @return the operation status
  54. * @notapi
  55. */
  56. msg_t bmp085I2CReadRegister(I2CDriver *i2cp, uint8_t reg, uint8_t *rxbufp,
  57. size_t n) {
  58. uint8_t txbuf = reg;
  59. return i2cMasterTransmitTimeout(i2cp, BMP085_SAD, &txbuf, 1, rxbufp, 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] txbufp buffer containing sub-address value in first position
  68. * and values to write
  69. * @param[in] n size of txbuf less one (not considering the first
  70. * element)
  71. *
  72. * @return the operation status
  73. * @notapi
  74. */
  75. msg_t bmp085I2CWriteRegister(I2CDriver *i2cp, uint8_t *txbufp, size_t n) {
  76. return i2cMasterTransmitTimeout(i2cp, BMP085_SAD, txbufp, n + 1, NULL, 0,
  77. TIME_INFINITE);
  78. }
  79. #endif /* BMP085_USE_I2C */
  80. /**
  81. * @brief Read all the calibration data from the BMP085 EEPROM.
  82. * @pre The I2C interface must be initialized and the driver started.
  83. *
  84. * @param[in] devp pointer to the BMP085 device driver sensor
  85. * @param[in] reg first calibration coefficient register to read
  86. *
  87. * @return msg the operation status
  88. */
  89. static msg_t bmp085ReadCoefficient(BMP085Driver *devp, uint8_t reg) {
  90. uint8_t rxbuf[22];
  91. #if BMP085_SHARED_I2C
  92. i2cAcquireBus(devp->config->i2cp);
  93. #endif /* BMP085_SHARED_I2C */
  94. msg_t msg = bmp085I2CReadRegister(devp->config->i2cp, reg, rxbuf, 22);
  95. #if BMP085_SHARED_I2C
  96. i2cReleaseBus(devp->config->i2cp);
  97. #endif /* BMP085_SHARED_I2C */
  98. if (msg == MSG_OK) {
  99. devp->calibrationdata.ac1 = ((rxbuf[ 0] << 8) | rxbuf[ 1]);
  100. devp->calibrationdata.ac2 = ((rxbuf[ 2] << 8) | rxbuf[ 3]);
  101. devp->calibrationdata.ac3 = ((rxbuf[ 4] << 8) | rxbuf[ 5]);
  102. devp->calibrationdata.ac4 = ((rxbuf[ 6] << 8) | rxbuf[ 7]);
  103. devp->calibrationdata.ac5 = ((rxbuf[ 8] << 8) | rxbuf[ 9]);
  104. devp->calibrationdata.ac6 = ((rxbuf[10] << 8) | rxbuf[11]);
  105. devp->calibrationdata.b1 = ((rxbuf[12] << 8) | rxbuf[13]);
  106. devp->calibrationdata.b2 = ((rxbuf[14] << 8) | rxbuf[15]);
  107. devp->calibrationdata.mb = ((rxbuf[16] << 8) | rxbuf[17]);
  108. devp->calibrationdata.mc = ((rxbuf[18] << 8) | rxbuf[19]);
  109. devp->calibrationdata.md = ((rxbuf[20] << 8) | rxbuf[21]);
  110. }
  111. return msg;
  112. }
  113. /**
  114. * @brief Calcul the true temperature.
  115. *
  116. * @param[in] devp pointer to the BMP085 device driver sensor
  117. * @param[in] ut uncompensated temperature
  118. * @param[out] ctp pointer of the compensated temperature
  119. */
  120. static void calcul_t(BMP085Driver *devp, int32_t ut, float *ctp) {
  121. int32_t x1, x2;
  122. /* Converting the temperature value. */
  123. x1 = ((ut - devp->calibrationdata.ac6) * devp->calibrationdata.ac5) >> 15;
  124. x2 = (devp->calibrationdata.mc << 11) / (x1 + devp->calibrationdata.md);
  125. devp->calibrationdata.b5 = x1 + x2;
  126. *ctp = (float)((devp->calibrationdata.b5 + 8) >> 4)*BMP085_T_RES;
  127. }
  128. /**
  129. * @brief Calcul the true pressure.
  130. *
  131. * @param[in] devp pointer to the BMP085 device driver sensor
  132. * @param[in] up uncompensated pressure
  133. * @param[in] oss over sampling setting
  134. * @param[out] cpp pointer of the compensated pressure
  135. */
  136. static void calcul_p(BMP085Driver *devp, int32_t up, uint8_t oss, float *cpp) {
  137. int32_t press;
  138. int32_t x1,x2,x3;
  139. int32_t b3,b6;
  140. uint32_t b4,b7;
  141. /* Converting the pressure value. */
  142. b6 = devp->calibrationdata.b5 - 4000;
  143. x1 = (devp->calibrationdata.b2 * ((b6 * b6) >> 12)) >> 11;
  144. x2 = (devp->calibrationdata.ac2 * b6) >> 11;
  145. x3 = x1 + x2;
  146. b3 = ((((int32_t)devp->calibrationdata.ac1 * 4 + x3) << oss) + 2) >> 2;
  147. x1 = ((devp->calibrationdata.ac3)*b6) >> 13;
  148. x2 = (devp->calibrationdata.b1 * (b6*b6 >> 12)) >> 16;
  149. x3 = ((x1 + x2) + 2) >> 2;
  150. b4 = devp->calibrationdata.ac4 * (uint32_t)(x3 + 32768) >> 15;
  151. b7 = ((uint32_t)up - b3)*(50000 >> oss);
  152. if (b7 < 0x80000000)
  153. press = (b7*2)/b4;
  154. else
  155. press = (b7/b4)*2;
  156. x1 = (press >> 8)*(press >> 8);
  157. x1 = (x1*3038) >> 16;
  158. x2 = (-7357*press) >> 16;
  159. *cpp =(float)((press + ((x1 + x2 + 3791) >> 4))*BMP085_P_RES);
  160. }
  161. /**
  162. * @brief Start temperature measurement.
  163. *
  164. * @param[in] devp pointer to the BMP085 device driver
  165. *
  166. * @return the operation status
  167. */
  168. static msg_t start_t_measurement(BMP085Driver *devp) {
  169. uint8_t txbuf[2] = {BMP085_AD_CR, BMP085_CR_T_VAL};
  170. i2cAcquireBus(devp->config->i2cp);
  171. msg_t msg = bmp085I2CWriteRegister(devp->config->i2cp, txbuf, 2);
  172. i2cReleaseBus(devp->config->i2cp);
  173. /* Conversion time for the temperature. */
  174. chThdSleepMilliseconds(BMP085_THERMO_CT_LOW);
  175. //chThdSleepMilliseconds(devp->config.tct); // TODO: use this instead of the top line:
  176. return msg;
  177. }
  178. /**
  179. * @brief Start the pressure measurment.
  180. *
  181. * @param[in] devp pointer to the BMP085 driver
  182. * @return msg the operation status
  183. */
  184. static msg_t start_p_measurement(BMP085Driver *devp) {
  185. uint8_t oss, delay;
  186. uint8_t txbuf[2];
  187. oss = devp->config->oss;
  188. txbuf[0] = BMP085_AD_CR;
  189. /* Check the oss according the bmp085 mode. */
  190. if (oss == BMP085_BARO_OSS_0) {
  191. txbuf[1] = BMP085_CR_P_VAL0 + (oss << 6);
  192. delay = BMP085_BARO_CT_LOW;
  193. }
  194. else if (oss == BMP085_BARO_OSS_1) {
  195. txbuf[1] = BMP085_CR_P_VAL1 + (oss << 6);
  196. delay = BMP085_BARO_CT_STD;
  197. }
  198. else if (oss == BMP085_BARO_OSS_2) {
  199. txbuf[1] = BMP085_CR_P_VAL2 + (oss << 6);
  200. delay = BMP085_BARO_CT_HR;
  201. }
  202. else {
  203. txbuf[1] = BMP085_CR_P_VAL3 + (oss << 6);
  204. delay = BMP085_BARO_CT_LUHR;
  205. }
  206. /* Start the sensor for sampling. */
  207. #if BMP085_SHARED_I2C
  208. i2cAcquireBus(devp->config->i2cp);
  209. #endif /* BMP085_SHARED_I2C */
  210. msg_t msg = bmp085I2CWriteRegister(devp->config->i2cp, txbuf, 2);
  211. #if BMP085_SHARED_I2C
  212. i2cReleaseBus(devp->config->i2cp);
  213. #endif /* BMP085_SHARED_I2C */
  214. /* Conversion time for the pressure, max time for the moment. */
  215. chThdSleepMilliseconds(delay);
  216. return msg;
  217. }
  218. /**
  219. * @brief Read the uncompensated temperature from the BMP085 register.
  220. *
  221. * @param[in] devp pointer to the BMP085 driver
  222. * @param[out] utemp uncompensated temperature read from the sensor register
  223. *
  224. * @return msg the operation status
  225. */
  226. static msg_t acquire_ut(BMP085Driver *devp, int32_t *utemp) {
  227. uint8_t rxbuf[2];
  228. msg_t msg;
  229. /* Start the temperature measurement. */
  230. start_t_measurement(devp);
  231. /* Start the sensor for sampling. */
  232. #if BMP085_SHARED_I2C
  233. i2cAcquireBus(devp->config->i2cp);
  234. #endif /* BMP085_SHARED_I2C */
  235. /* Get the temperature. */
  236. msg = bmp085I2CReadRegister(devp->config->i2cp, BMP085_AD_T_DR_MSB, rxbuf,
  237. 2);
  238. #if BMP085_SHARED_I2C
  239. i2cReleaseBus(devp->config->i2cp);
  240. #endif /* BMP085_SHARED_I2C */
  241. if(msg == MSG_OK){
  242. /* Building the uncompensated temperature value. */
  243. *utemp = (int32_t)((rxbuf[0] << 8) | rxbuf[1]);
  244. }
  245. return msg;
  246. }
  247. /**
  248. * @brief Read the uncompensated pressure from the BMP085 register.
  249. *
  250. * @param[in] devp pointer to the BMP085 driver
  251. * @param[out] upress uncompensated pressure read from the sensor register
  252. *
  253. * @return msg the operation status
  254. */
  255. static msg_t acquire_up(BMP085Driver *devp, int32_t *upress) {
  256. uint8_t rxbuf[3];
  257. uint8_t oss;
  258. msg_t msg;
  259. /* Get the oversampling setting from the driver configuratioin. */
  260. oss = devp->config->oss;
  261. /* Start the pressure measurement. */
  262. start_p_measurement(devp);
  263. /* Start the sensor for sampling. */
  264. #if BMP085_SHARED_I2C
  265. i2cAcquireBus(devp->config->i2cp);
  266. #endif /* BMP085_SHARED_I2C */
  267. /* Get the pressure */
  268. msg = bmp085I2CReadRegister(devp->config->i2cp, BMP085_AD_P_DR_MSB, rxbuf,
  269. 3);
  270. #if BMP085_SHARED_I2C
  271. i2cReleaseBus(devp->config->i2cp);
  272. #endif /* BMP085_SHARED_I2C */
  273. if (msg == MSG_OK) {
  274. /* Building the uncompensated pressure value. */
  275. *upress = (int32_t)((rxbuf[0] << 16)|(rxbuf[1] << 8)|rxbuf[2]);
  276. *upress = *upress >> (8-oss);
  277. }
  278. return msg;
  279. }
  280. /*==========================================================================*/
  281. /* Interface implementation. */
  282. /*==========================================================================*/
  283. /**
  284. * @brief Get the barometer number of axes.
  285. *
  286. * @param[in] ip interface pointer of the BMP085 sensor
  287. *
  288. * @return barometer number of axes
  289. */
  290. static size_t baro_get_axes_number(void *ip) {
  291. osalDbgCheck(ip != NULL);
  292. return BMP085_BARO_NUMBER_OF_AXES;
  293. }
  294. /**
  295. * @brief Get the thermometer number of axes.
  296. *
  297. * @param[in] ip interface pointer of the BMP085 sensor
  298. *
  299. * @return thermometer number of axes
  300. */
  301. static size_t thermo_get_axes_number(void *ip) {
  302. osalDbgCheck(ip != NULL);
  303. return BMP085_THERMO_NUMBER_OF_AXES;
  304. }
  305. /**
  306. * @brief Get the sensor number of axes.
  307. *
  308. * @param[in] ip interface pointer of the BMP085 sensor
  309. *
  310. * @return sensor number of axes
  311. */
  312. static size_t sens_get_axes_number(void *ip) {
  313. osalDbgCheck(ip != NULL);
  314. return (baro_get_axes_number(ip) + thermo_get_axes_number(ip));
  315. }
  316. /**
  317. * @brief Read baromether raw data.
  318. *
  319. * @param[in] ip interface pointer of the sensor
  320. * @param[in] axes buffer for various axes data
  321. *
  322. * @return msg the result of the reading operation
  323. */
  324. static msg_t baro_read_raw(void *ip, int32_t axes[]) {
  325. osalDbgCheck((ip != NULL) && (axes != NULL));
  326. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  327. "baro_read_raw(), invalid state");
  328. #if BMP085_USE_I2C
  329. osalDbgAssert((((BMP085Driver *)ip)->config->i2cp->state == I2C_READY),
  330. "baro_read_raw(), channel not ready");
  331. #if BMP085_SHARED_I2C
  332. i2cAcquireBus(((BMP085Driver *)ip)->config->i2cp);
  333. i2cStart(((BMP085Driver *)ip)->config->i2cp,
  334. ((BMP085Driver *)ip)->config->i2ccfg);
  335. #endif /* BMP085_SHARED_I2C. */
  336. /* Measure the uncompensated pressure. */
  337. msg_t msg = acquire_up(((BMP085Driver *)ip), axes);
  338. #if BMP085_SHARED_I2C
  339. i2cReleaseBus(((BMP085Driver *)ip)->config->i2cp);
  340. #endif /* BMP085_SHARED_I2C. */
  341. #endif /* BMP085_USE_I2C. */
  342. return msg;
  343. }
  344. /**
  345. * @brief Read thermometer raw data.
  346. *
  347. * @param[in] ip interface pointer of the BMP085 sensor
  348. * @param[in] axes buffer for various axes data
  349. *
  350. * @return msg the result of the reading operation
  351. */
  352. static msg_t thermo_read_raw(void *ip, int32_t axes[]) {
  353. osalDbgCheck((ip != NULL) && (axes != NULL));
  354. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  355. "thermo_read_raw(), invalid state");
  356. #if BMP085_USE_I2C
  357. osalDbgAssert((((BMP085Driver *)ip)->config->i2cp->state == I2C_READY),
  358. "thermo_read_raw(), channel not ready");
  359. #if BMP085_SHARED_I2C
  360. i2cAcquireBus(((BMP085Driver *)ip)->config->i2cp);
  361. i2cStart(((BMP085Driver *)ip)->config->i2cp,
  362. ((BMP085Driver *)ip)->config->i2ccfg);
  363. #endif /* BMP085_SHARED_I2C. */
  364. /* Measure the uncompensated temperature. */
  365. msg_t msg = acquire_ut(((BMP085Driver *)ip), axes);
  366. #if BMP085_SHARED_I2C
  367. i2cReleaseBus(((LSM303DLHCDriver *)ip)->config->i2cp);
  368. #endif /* BMP085_SHARED_I2C. */
  369. #endif /* BMP085_USE_I2C. */
  370. return msg;
  371. }
  372. /**
  373. * @brief Read BMP085 sensor raw data.
  374. *
  375. * @param[in] ip interface pointer of the BMP085 sensor
  376. * @param[in] axes buffer for various axes data
  377. *
  378. * @return msg the result of the reading operation
  379. */
  380. static msg_t sens_read_raw(void *ip, int32_t axes[]) {
  381. int32_t* bp = axes;
  382. msg_t msg;
  383. msg = baro_read_raw(ip, bp);
  384. if (msg != MSG_OK)
  385. return msg;
  386. bp += BMP085_BARO_NUMBER_OF_AXES;
  387. msg = thermo_read_raw(ip, bp);
  388. return msg;
  389. }
  390. /**
  391. * @brief Read barometer cooked data.
  392. *
  393. * @param[in] ip interface pointer of the BMP085 sensor
  394. * @param[in] axes buffer for various axes data
  395. *
  396. * @return msg the result of the reading operation
  397. */
  398. static msg_t baro_read_cooked(void *ip, float axes[]) {
  399. uint32_t i;
  400. int32_t raw[BMP085_BARO_NUMBER_OF_AXES];
  401. msg_t msg;
  402. uint8_t oss;
  403. osalDbgCheck((ip != NULL) && (axes != NULL));
  404. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  405. "baro_read_cooked(), invalid state");
  406. msg = baro_read_raw(ip, raw);
  407. oss = ((BMP085Driver *)ip)->config->oss;
  408. for (i = 0; i < BMP085_BARO_NUMBER_OF_AXES; i++)
  409. calcul_p(ip, raw[i], oss, &axes[i]);
  410. return msg;
  411. }
  412. /**
  413. * @brief Read thermometer cooked data.
  414. *
  415. * @param[in] ip interface pointer of the BMP085 sensor
  416. * @param[in] axes buffer for various axes data
  417. *
  418. * @return msg the result of the reading operation
  419. */
  420. static msg_t thermo_read_cooked(void *ip, float axes[]) {
  421. uint32_t i;
  422. int32_t raw[BMP085_THERMO_NUMBER_OF_AXES];
  423. msg_t msg;
  424. osalDbgCheck(((ip != NULL) && (axes != NULL)));
  425. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  426. "thermo_read_cooked(), invalid state");
  427. msg = thermo_read_raw(ip, raw);
  428. for (i = 0; i < BMP085_THERMO_NUMBER_OF_AXES; i++)
  429. calcul_t(ip, raw[i], &axes[i]);
  430. return msg;
  431. }
  432. /**
  433. * @brief Read BMP085 sensor cooked data.
  434. *
  435. * @param[in] ip interface pointer of the BMP085 sensor
  436. * @param[in] axes buffer for various axes data
  437. *
  438. * @return msg the result of the reading operation
  439. */
  440. static msg_t sens_read_cooked(void *ip, float axes[]) {
  441. float* bp = axes;
  442. msg_t msg;
  443. msg = baro_read_cooked(ip, bp);
  444. if (msg != MSG_OK)
  445. return msg;
  446. bp += BMP085_BARO_NUMBER_OF_AXES;
  447. msg = thermo_read_cooked(ip, bp);
  448. return msg;
  449. }
  450. /**
  451. * @brief Set the barometer bias.
  452. *
  453. * @param[in] ip interface pointer of the BMP085 sensor
  454. * @param[in] bp pointer to the bias value
  455. *
  456. * @return msg the result of the setting operation
  457. */
  458. static msg_t baro_set_bias(void *ip, float *bp) {
  459. osalDbgCheck((ip != NULL) && (bp !=NULL));
  460. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY) ||
  461. (((BMP085Driver *)ip)->state == BMP085_STOP),
  462. "baro_set_bias(), invalid state");
  463. return MSG_OK;
  464. }
  465. /**
  466. * @brief Set the thermometer bias.
  467. *
  468. * @param[in] ip interface pointer of the BMP085 sensor
  469. * @param[in] bp pointer to the bias value
  470. *
  471. * @return msg the result of the setting operation
  472. */
  473. static msg_t thermo_set_bias(void *ip, float *bp) {
  474. osalDbgCheck((ip != NULL) && (bp !=NULL));
  475. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY) ||
  476. (((BMP085Driver *)ip)->state == BMP085_STOP),
  477. "thermo_set_bias(), invalid state");
  478. return MSG_OK;
  479. }
  480. /**
  481. * @brief Reset the barometer bias.
  482. *
  483. * @param[in] ip interface pointer of the BMP085 sensor
  484. *
  485. * @return msg the result of the reset operation
  486. */
  487. static msg_t baro_reset_bias(void *ip) {
  488. osalDbgCheck(ip != NULL);
  489. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY) ||
  490. (((BMP085Driver *)ip)->state == BMP085_STOP),
  491. "baro_reset_bias(), invalid state");
  492. return MSG_OK;
  493. }
  494. /**
  495. * @brief Reset the thermometer bias.
  496. *
  497. * @param[in] ip interface pointer of the BMP085 sensor
  498. *
  499. * @return msg the result of the reset operation
  500. */
  501. static msg_t thermo_reset_bias(void *ip) {
  502. osalDbgCheck(ip != NULL);
  503. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY) ||
  504. (((BMP085Driver *)ip)->state == BMP085_STOP),
  505. "thermo_reset_bias(), invalid state");
  506. return MSG_OK;
  507. }
  508. /**
  509. * @brief Set the barometer sensivity.
  510. *
  511. * @param[in] ip interface pointer of the BMP085 sensor
  512. * @param[in] sp pointer to the sensivity value
  513. *
  514. * @return msg the result of the setting operation
  515. */
  516. static msg_t baro_set_sensivity(void *ip, float *sp) {
  517. osalDbgCheck((ip != NULL) && (sp !=NULL));
  518. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  519. "baro_set_sensivity(), invalid state");
  520. return MSG_OK;
  521. }
  522. /**
  523. * @brief Set the thermometer sensivity.
  524. *
  525. * @param[in] ip interface pointer of the BMP085 sensor
  526. * @param[in] sp pointer to the sensivity value
  527. *
  528. * @return msg the result of the setting operation
  529. */
  530. static msg_t thermo_set_sensivity(void *ip, float *sp) {
  531. osalDbgCheck((ip != NULL) && (sp !=NULL));
  532. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  533. "thermo_set_sensivity(), invalid state");
  534. return MSG_OK;
  535. }
  536. /**
  537. * @brief Reset the barometer sensivity.
  538. *
  539. * @param[in] ip interface pointer of the BMP085 sensor
  540. *
  541. * @return msg the result of the reset operation
  542. */
  543. static msg_t baro_reset_sensivity(void *ip) {
  544. osalDbgCheck(ip != NULL);
  545. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  546. "baro_reset_sensivity(), invalid state");
  547. return MSG_OK;
  548. }
  549. /**
  550. * @brief Reset the thermometer sensivity.
  551. *
  552. * @param[in] ip interface pointer of the BMP085 sensor
  553. *
  554. * @return msg the result of the reset operation
  555. */
  556. static msg_t thermo_reset_sensivity(void *ip) {
  557. osalDbgCheck(ip != NULL);
  558. osalDbgAssert((((BMP085Driver *)ip)->state == BMP085_READY),
  559. "thermo_reset_sensivity(), invalid state");
  560. return MSG_OK;
  561. }
  562. static const struct BaseSensorVMT vmt_basesensor = {
  563. sens_get_axes_number, sens_read_raw, sens_read_cooked
  564. };
  565. static const struct BaseBarometerVMT vmt_basebarometer = {
  566. baro_get_axes_number, baro_read_raw, baro_read_cooked,
  567. baro_set_bias, baro_reset_bias,
  568. baro_set_sensivity, baro_reset_sensivity
  569. };
  570. static const struct BaseThermometerVMT vmt_basethermometer = {
  571. thermo_get_axes_number, thermo_read_raw, thermo_read_cooked,
  572. thermo_set_bias, thermo_reset_bias,
  573. thermo_set_sensivity, thermo_reset_sensivity
  574. };
  575. /*==========================================================================*/
  576. /* Driver exported functions. */
  577. /*==========================================================================*/
  578. /**
  579. * @brief Initializes an instance.
  580. *
  581. * @param[out] devp pointer to the @p BMP085Driver object
  582. *
  583. * @init
  584. */
  585. void bmp085ObjectInit(BMP085Driver *devp) {
  586. devp->vmt_basesensor = &vmt_basesensor;
  587. devp->vmt_basebarometer = &vmt_basebarometer;
  588. devp->vmt_basethermometer = &vmt_basethermometer;
  589. devp->config = NULL;
  590. devp->state = BMP085_STOP;
  591. }
  592. /**
  593. * @brief Configures and activates BMP085 Complex Driver peripheral.
  594. *
  595. * @param[in] devp pointer to the @p BMP085Driver object
  596. * @param[in] config pointer to the @p BMP085Config object
  597. *
  598. * @api
  599. */
  600. void bmp085Start(BMP085Driver *devp, const BMP085Config *config) {
  601. osalDbgCheck((devp != NULL) && (config != NULL));
  602. osalDbgAssert((devp->state == BMP085_STOP) ||
  603. (devp->state == BMP085_READY),
  604. "bmp085cStart(), invalid state");
  605. devp->config = config;
  606. #if BMP085_USE_I2C
  607. #if BMP085_SHARED_I2C
  608. i2cAcquireBus((devp)->config->i2cp);
  609. #endif /* BMP085_SHARED_I2C. */
  610. /* Read the Calibrations data. */
  611. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  612. bmp085ReadCoefficient(devp, BMP085_AD_CC_AC1_MSB);
  613. #if BMP085_SHARED_I2C
  614. i2cReleaseBus((devp)->config->i2cp);
  615. #endif /* BMP085_SHARED_I2C. */
  616. #endif /* BMP085_USE_I2C. */
  617. if(devp->state != BMP085_READY)
  618. devp->state = BMP085_READY;
  619. }
  620. /**
  621. * @brief Deactivates the BMP085 Complex Driver peripheral.
  622. *
  623. * @param[in] devp pointer to the @p BMP085Driver object
  624. *
  625. * @api
  626. */
  627. void bmp085Stop(BMP085Driver *devp) {
  628. osalDbgCheck(devp != NULL);
  629. osalDbgAssert((devp->state == BMP085_STOP) ||
  630. (devp->state == BMP085_READY),
  631. "bmp085Stop(), invalid state");
  632. #if (BMP085_USE_I2C)
  633. if (devp->state == BMP085_STOP) {
  634. #if BMP085_SHARED_I2C
  635. i2cAcquireBus((devp)->config->i2cp);
  636. i2cStart((devp)->config->i2cp, (devp)->config->i2ccfg);
  637. #endif /* BMP085_SHARED_I2C. */
  638. #if BMP085_SHARED_I2C
  639. i2cReleaseBus((devp)->config->i2cp);
  640. #endif /* BMP085_SHARED_I2C. */
  641. }
  642. #endif /* BMP085_USE_I2C. */
  643. if (devp->state != BMP085_STOP)
  644. devp->state = BMP085_STOP;
  645. }
  646. /** @} */