bme680.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /**\mainpage
  2. * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the copyright holder nor the names of the
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  23. * OR CONTRIBUTORS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  33. *
  34. * The information provided is believed to be accurate and reliable.
  35. * The copyright holder assumes no responsibility
  36. * for the consequences of use
  37. * of such information nor for any infringement of patents or
  38. * other rights of third parties which may result from its use.
  39. * No license is granted by implication or otherwise under any patent or
  40. * patent rights of the copyright holder.
  41. *
  42. * File bme680.c
  43. * @date 19 Jun 2018
  44. * @version 3.5.9
  45. *
  46. */
  47. /*! @file bme680.c
  48. @brief Sensor driver for BME680 sensor */
  49. #include "bme680.h"
  50. /*!
  51. * @brief This internal API is used to read the calibrated data from the sensor.
  52. *
  53. * This function is used to retrieve the calibration
  54. * data from the image registers of the sensor.
  55. *
  56. * @note Registers 89h to A1h for calibration data 1 to 24
  57. * from bit 0 to 7
  58. * @note Registers E1h to F0h for calibration data 25 to 40
  59. * from bit 0 to 7
  60. * @param[in] dev :Structure instance of bme680_dev.
  61. *
  62. * @return Result of API execution status.
  63. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  64. */
  65. static int8_t get_calib_data(struct bme680_dev *dev);
  66. /*!
  67. * @brief This internal API is used to set the gas configuration of the sensor.
  68. *
  69. * @param[in] dev :Structure instance of bme680_dev.
  70. *
  71. * @return Result of API execution status.
  72. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  73. */
  74. static int8_t set_gas_config(struct bme680_dev *dev);
  75. /*!
  76. * @brief This internal API is used to get the gas configuration of the sensor.
  77. * @note heatr_temp and heatr_dur values are currently register data
  78. * and not the actual values set
  79. *
  80. * @param[in] dev :Structure instance of bme680_dev.
  81. *
  82. * @return Result of API execution status.
  83. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  84. */
  85. static int8_t get_gas_config(struct bme680_dev *dev);
  86. /*!
  87. * @brief This internal API is used to calculate the Heat duration value.
  88. *
  89. * @param[in] dur :Value of the duration to be shared.
  90. *
  91. * @return uint8_t threshold duration after calculation.
  92. */
  93. static uint8_t calc_heater_dur(uint16_t dur);
  94. #ifndef BME680_FLOAT_POINT_COMPENSATION
  95. /*!
  96. * @brief This internal API is used to calculate the temperature value.
  97. *
  98. * @param[in] dev :Structure instance of bme680_dev.
  99. * @param[in] temp_adc :Contains the temperature ADC value .
  100. *
  101. * @return uint32_t calculated temperature.
  102. */
  103. static int16_t calc_temperature(uint32_t temp_adc, struct bme680_dev *dev);
  104. /*!
  105. * @brief This internal API is used to calculate the pressure value.
  106. *
  107. * @param[in] dev :Structure instance of bme680_dev.
  108. * @param[in] pres_adc :Contains the pressure ADC value .
  109. *
  110. * @return uint32_t calculated pressure.
  111. */
  112. static uint32_t calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev);
  113. /*!
  114. * @brief This internal API is used to calculate the humidity value.
  115. *
  116. * @param[in] dev :Structure instance of bme680_dev.
  117. * @param[in] hum_adc :Contains the humidity ADC value.
  118. *
  119. * @return uint32_t calculated humidity.
  120. */
  121. static uint32_t calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev);
  122. /*!
  123. * @brief This internal API is used to calculate the Gas Resistance value.
  124. *
  125. * @param[in] dev :Structure instance of bme680_dev.
  126. * @param[in] gas_res_adc :Contains the Gas Resistance ADC value.
  127. * @param[in] gas_range :Contains the range of gas values.
  128. *
  129. * @return uint32_t calculated gas resistance.
  130. */
  131. static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev);
  132. /*!
  133. * @brief This internal API is used to calculate the Heat Resistance value.
  134. *
  135. * @param[in] dev : Structure instance of bme680_dev
  136. * @param[in] temp : Contains the target temperature value.
  137. *
  138. * @return uint8_t calculated heater resistance.
  139. */
  140. static uint8_t calc_heater_res(uint16_t temp, const struct bme680_dev *dev);
  141. #else
  142. /*!
  143. * @brief This internal API is used to calculate the
  144. * temperature value value in float format
  145. *
  146. * @param[in] dev :Structure instance of bme680_dev.
  147. * @param[in] temp_adc :Contains the temperature ADC value .
  148. *
  149. * @return Calculated temperature in float
  150. */
  151. static float calc_temperature(uint32_t temp_adc, struct bme680_dev *dev);
  152. /*!
  153. * @brief This internal API is used to calculate the
  154. * pressure value value in float format
  155. *
  156. * @param[in] dev :Structure instance of bme680_dev.
  157. * @param[in] pres_adc :Contains the pressure ADC value .
  158. *
  159. * @return Calculated pressure in float.
  160. */
  161. static float calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev);
  162. /*!
  163. * @brief This internal API is used to calculate the
  164. * humidity value value in float format
  165. *
  166. * @param[in] dev :Structure instance of bme680_dev.
  167. * @param[in] hum_adc :Contains the humidity ADC value.
  168. *
  169. * @return Calculated humidity in float.
  170. */
  171. static float calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev);
  172. /*!
  173. * @brief This internal API is used to calculate the
  174. * gas resistance value value in float format
  175. *
  176. * @param[in] dev :Structure instance of bme680_dev.
  177. * @param[in] gas_res_adc :Contains the Gas Resistance ADC value.
  178. * @param[in] gas_range :Contains the range of gas values.
  179. *
  180. * @return Calculated gas resistance in float.
  181. */
  182. static float calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev);
  183. /*!
  184. * @brief This internal API is used to calculate the
  185. * heater resistance value in float format
  186. *
  187. * @param[in] temp : Contains the target temperature value.
  188. * @param[in] dev : Structure instance of bme680_dev.
  189. *
  190. * @return Calculated heater resistance in float.
  191. */
  192. static float calc_heater_res(uint16_t temp, const struct bme680_dev *dev);
  193. #endif
  194. /*!
  195. * @brief This internal API is used to calculate the field data of sensor.
  196. *
  197. * @param[out] data :Structure instance to hold the data
  198. * @param[in] dev :Structure instance of bme680_dev.
  199. *
  200. * @return int8_t result of the field data from sensor.
  201. */
  202. static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev *dev);
  203. /*!
  204. * @brief This internal API is used to set the memory page
  205. * based on register address.
  206. *
  207. * The value of memory page
  208. * value | Description
  209. * --------|--------------
  210. * 0 | BME680_PAGE0_SPI
  211. * 1 | BME680_PAGE1_SPI
  212. *
  213. * @param[in] dev :Structure instance of bme680_dev.
  214. * @param[in] reg_addr :Contains the register address array.
  215. *
  216. * @return Result of API execution status
  217. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  218. */
  219. static int8_t set_mem_page(uint8_t reg_addr, struct bme680_dev *dev);
  220. /*!
  221. * @brief This internal API is used to get the memory page based
  222. * on register address.
  223. *
  224. * The value of memory page
  225. * value | Description
  226. * --------|--------------
  227. * 0 | BME680_PAGE0_SPI
  228. * 1 | BME680_PAGE1_SPI
  229. *
  230. * @param[in] dev :Structure instance of bme680_dev.
  231. *
  232. * @return Result of API execution status
  233. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  234. */
  235. static int8_t get_mem_page(struct bme680_dev *dev);
  236. /*!
  237. * @brief This internal API is used to validate the device pointer for
  238. * null conditions.
  239. *
  240. * @param[in] dev :Structure instance of bme680_dev.
  241. *
  242. * @return Result of API execution status
  243. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  244. */
  245. static int8_t null_ptr_check(const struct bme680_dev *dev);
  246. /*!
  247. * @brief This internal API is used to check the boundary
  248. * conditions.
  249. *
  250. * @param[in] value :pointer to the value.
  251. * @param[in] min :minimum value.
  252. * @param[in] max :maximum value.
  253. * @param[in] dev :Structure instance of bme680_dev.
  254. *
  255. * @return Result of API execution status
  256. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  257. */
  258. static int8_t boundary_check(uint8_t *value, uint8_t min, uint8_t max, struct bme680_dev *dev);
  259. /****************** Global Function Definitions *******************************/
  260. /*!
  261. *@brief This API is the entry point.
  262. *It reads the chip-id and calibration data from the sensor.
  263. */
  264. int8_t bme680_init(struct bme680_dev *dev)
  265. {
  266. int8_t rslt;
  267. /* Check for null pointer in the device structure*/
  268. rslt = null_ptr_check(dev);
  269. if (rslt == BME680_OK) {
  270. /* Soft reset to restore it to default values*/
  271. rslt = bme680_soft_reset(dev);
  272. if (rslt == BME680_OK) {
  273. rslt = bme680_get_regs(BME680_CHIP_ID_ADDR, &dev->chip_id, 1, dev);
  274. if (rslt == BME680_OK) {
  275. if (dev->chip_id == BME680_CHIP_ID) {
  276. /* Get the Calibration data */
  277. rslt = get_calib_data(dev);
  278. } else {
  279. rslt = BME680_E_DEV_NOT_FOUND;
  280. }
  281. }
  282. }
  283. }
  284. return rslt;
  285. }
  286. /*!
  287. * @brief This API reads the data from the given register address of the sensor.
  288. */
  289. int8_t bme680_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, struct bme680_dev *dev)
  290. {
  291. int8_t rslt;
  292. /* Check for null pointer in the device structure*/
  293. rslt = null_ptr_check(dev);
  294. if (rslt == BME680_OK) {
  295. if (dev->intf == BME680_SPI_INTF) {
  296. /* Set the memory page */
  297. rslt = set_mem_page(reg_addr, dev);
  298. if (rslt == BME680_OK)
  299. reg_addr = reg_addr | BME680_SPI_RD_MSK;
  300. }
  301. dev->com_rslt = dev->read(dev->dev_id, reg_addr, reg_data, len);
  302. if (dev->com_rslt != 0)
  303. rslt = BME680_E_COM_FAIL;
  304. }
  305. return rslt;
  306. }
  307. /*!
  308. * @brief This API writes the given data to the register address
  309. * of the sensor.
  310. */
  311. int8_t bme680_set_regs(const uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, struct bme680_dev *dev)
  312. {
  313. int8_t rslt;
  314. /* Length of the temporary buffer is 2*(length of register)*/
  315. uint8_t tmp_buff[BME680_TMP_BUFFER_LENGTH] = { 0 };
  316. uint16_t index;
  317. /* Check for null pointer in the device structure*/
  318. rslt = null_ptr_check(dev);
  319. if (rslt == BME680_OK) {
  320. if ((len > 0) && (len < BME680_TMP_BUFFER_LENGTH / 2)) {
  321. /* Interleave the 2 arrays */
  322. for (index = 0; index < len; index++) {
  323. if (dev->intf == BME680_SPI_INTF) {
  324. /* Set the memory page */
  325. rslt = set_mem_page(reg_addr[index], dev);
  326. tmp_buff[(2 * index)] = reg_addr[index] & BME680_SPI_WR_MSK;
  327. } else {
  328. tmp_buff[(2 * index)] = reg_addr[index];
  329. }
  330. tmp_buff[(2 * index) + 1] = reg_data[index];
  331. }
  332. /* Write the interleaved array */
  333. if (rslt == BME680_OK) {
  334. dev->com_rslt = dev->write(dev->dev_id, tmp_buff[0], &tmp_buff[1], (2 * len) - 1);
  335. if (dev->com_rslt != 0)
  336. rslt = BME680_E_COM_FAIL;
  337. }
  338. } else {
  339. rslt = BME680_E_INVALID_LENGTH;
  340. }
  341. }
  342. return rslt;
  343. }
  344. /*!
  345. * @brief This API performs the soft reset of the sensor.
  346. */
  347. int8_t bme680_soft_reset(struct bme680_dev *dev)
  348. {
  349. int8_t rslt;
  350. uint8_t reg_addr = BME680_SOFT_RESET_ADDR;
  351. /* 0xb6 is the soft reset command */
  352. uint8_t soft_rst_cmd = BME680_SOFT_RESET_CMD;
  353. /* Check for null pointer in the device structure*/
  354. rslt = null_ptr_check(dev);
  355. if (rslt == BME680_OK) {
  356. if (dev->intf == BME680_SPI_INTF)
  357. rslt = get_mem_page(dev);
  358. /* Reset the device */
  359. if (rslt == BME680_OK) {
  360. rslt = bme680_set_regs(&reg_addr, &soft_rst_cmd, 1, dev);
  361. /* Wait for 5ms */
  362. dev->delay_ms(BME680_RESET_PERIOD);
  363. if (rslt == BME680_OK) {
  364. /* After reset get the memory page */
  365. if (dev->intf == BME680_SPI_INTF)
  366. rslt = get_mem_page(dev);
  367. }
  368. }
  369. }
  370. return rslt;
  371. }
  372. /*!
  373. * @brief This API is used to set the oversampling, filter and T,P,H, gas selection
  374. * settings in the sensor.
  375. */
  376. int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *dev)
  377. {
  378. int8_t rslt;
  379. uint8_t reg_addr;
  380. uint8_t data = 0;
  381. uint8_t count = 0;
  382. uint8_t reg_array[BME680_REG_BUFFER_LENGTH] = { 0 };
  383. uint8_t data_array[BME680_REG_BUFFER_LENGTH] = { 0 };
  384. uint8_t intended_power_mode = dev->power_mode; /* Save intended power mode */
  385. /* Check for null pointer in the device structure*/
  386. rslt = null_ptr_check(dev);
  387. if (rslt == BME680_OK) {
  388. if (desired_settings & BME680_GAS_MEAS_SEL)
  389. rslt = set_gas_config(dev);
  390. dev->power_mode = BME680_SLEEP_MODE;
  391. if (rslt == BME680_OK)
  392. rslt = bme680_set_sensor_mode(dev);
  393. /* Selecting the filter */
  394. if (desired_settings & BME680_FILTER_SEL) {
  395. rslt = boundary_check(&dev->tph_sett.filter, BME680_FILTER_SIZE_0, BME680_FILTER_SIZE_127, dev);
  396. reg_addr = BME680_CONF_ODR_FILT_ADDR;
  397. if (rslt == BME680_OK)
  398. rslt = bme680_get_regs(reg_addr, &data, 1, dev);
  399. if (desired_settings & BME680_FILTER_SEL)
  400. data = BME680_SET_BITS(data, BME680_FILTER, dev->tph_sett.filter);
  401. reg_array[count] = reg_addr; /* Append configuration */
  402. data_array[count] = data;
  403. count++;
  404. }
  405. /* Selecting heater control for the sensor */
  406. if (desired_settings & BME680_HCNTRL_SEL) {
  407. rslt = boundary_check(&dev->gas_sett.heatr_ctrl, BME680_ENABLE_HEATER,
  408. BME680_DISABLE_HEATER, dev);
  409. reg_addr = BME680_CONF_HEAT_CTRL_ADDR;
  410. if (rslt == BME680_OK)
  411. rslt = bme680_get_regs(reg_addr, &data, 1, dev);
  412. data = BME680_SET_BITS_POS_0(data, BME680_HCTRL, dev->gas_sett.heatr_ctrl);
  413. reg_array[count] = reg_addr; /* Append configuration */
  414. data_array[count] = data;
  415. count++;
  416. }
  417. /* Selecting heater T,P oversampling for the sensor */
  418. if (desired_settings & (BME680_OST_SEL | BME680_OSP_SEL)) {
  419. rslt = boundary_check(&dev->tph_sett.os_temp, BME680_OS_NONE, BME680_OS_16X, dev);
  420. reg_addr = BME680_CONF_T_P_MODE_ADDR;
  421. if (rslt == BME680_OK)
  422. rslt = bme680_get_regs(reg_addr, &data, 1, dev);
  423. if (desired_settings & BME680_OST_SEL)
  424. data = BME680_SET_BITS(data, BME680_OST, dev->tph_sett.os_temp);
  425. if (desired_settings & BME680_OSP_SEL)
  426. data = BME680_SET_BITS(data, BME680_OSP, dev->tph_sett.os_pres);
  427. reg_array[count] = reg_addr;
  428. data_array[count] = data;
  429. count++;
  430. }
  431. /* Selecting humidity oversampling for the sensor */
  432. if (desired_settings & BME680_OSH_SEL) {
  433. rslt = boundary_check(&dev->tph_sett.os_hum, BME680_OS_NONE, BME680_OS_16X, dev);
  434. reg_addr = BME680_CONF_OS_H_ADDR;
  435. if (rslt == BME680_OK)
  436. rslt = bme680_get_regs(reg_addr, &data, 1, dev);
  437. data = BME680_SET_BITS_POS_0(data, BME680_OSH, dev->tph_sett.os_hum);
  438. reg_array[count] = reg_addr; /* Append configuration */
  439. data_array[count] = data;
  440. count++;
  441. }
  442. /* Selecting the runGas and NB conversion settings for the sensor */
  443. if (desired_settings & (BME680_RUN_GAS_SEL | BME680_NBCONV_SEL)) {
  444. rslt = boundary_check(&dev->gas_sett.run_gas, BME680_RUN_GAS_DISABLE,
  445. BME680_RUN_GAS_ENABLE, dev);
  446. if (rslt == BME680_OK) {
  447. /* Validate boundary conditions */
  448. rslt = boundary_check(&dev->gas_sett.nb_conv, BME680_NBCONV_MIN,
  449. BME680_NBCONV_MAX, dev);
  450. }
  451. reg_addr = BME680_CONF_ODR_RUN_GAS_NBC_ADDR;
  452. if (rslt == BME680_OK)
  453. rslt = bme680_get_regs(reg_addr, &data, 1, dev);
  454. if (desired_settings & BME680_RUN_GAS_SEL)
  455. data = BME680_SET_BITS(data, BME680_RUN_GAS, dev->gas_sett.run_gas);
  456. if (desired_settings & BME680_NBCONV_SEL)
  457. data = BME680_SET_BITS_POS_0(data, BME680_NBCONV, dev->gas_sett.nb_conv);
  458. reg_array[count] = reg_addr; /* Append configuration */
  459. data_array[count] = data;
  460. count++;
  461. }
  462. if (rslt == BME680_OK)
  463. rslt = bme680_set_regs(reg_array, data_array, count, dev);
  464. /* Restore previous intended power mode */
  465. dev->power_mode = intended_power_mode;
  466. }
  467. return rslt;
  468. }
  469. /*!
  470. * @brief This API is used to get the oversampling, filter and T,P,H, gas selection
  471. * settings in the sensor.
  472. */
  473. int8_t bme680_get_sensor_settings(uint16_t desired_settings, struct bme680_dev *dev)
  474. {
  475. int8_t rslt;
  476. /* starting address of the register array for burst read*/
  477. uint8_t reg_addr = BME680_CONF_HEAT_CTRL_ADDR;
  478. uint8_t data_array[BME680_REG_BUFFER_LENGTH] = { 0 };
  479. /* Check for null pointer in the device structure*/
  480. rslt = null_ptr_check(dev);
  481. if (rslt == BME680_OK) {
  482. rslt = bme680_get_regs(reg_addr, data_array, BME680_REG_BUFFER_LENGTH, dev);
  483. if (rslt == BME680_OK) {
  484. if (desired_settings & BME680_GAS_MEAS_SEL)
  485. rslt = get_gas_config(dev);
  486. /* get the T,P,H ,Filter,ODR settings here */
  487. if (desired_settings & BME680_FILTER_SEL)
  488. dev->tph_sett.filter = BME680_GET_BITS(data_array[BME680_REG_FILTER_INDEX],
  489. BME680_FILTER);
  490. if (desired_settings & (BME680_OST_SEL | BME680_OSP_SEL)) {
  491. dev->tph_sett.os_temp = BME680_GET_BITS(data_array[BME680_REG_TEMP_INDEX], BME680_OST);
  492. dev->tph_sett.os_pres = BME680_GET_BITS(data_array[BME680_REG_PRES_INDEX], BME680_OSP);
  493. }
  494. if (desired_settings & BME680_OSH_SEL)
  495. dev->tph_sett.os_hum = BME680_GET_BITS_POS_0(data_array[BME680_REG_HUM_INDEX],
  496. BME680_OSH);
  497. /* get the gas related settings */
  498. if (desired_settings & BME680_HCNTRL_SEL)
  499. dev->gas_sett.heatr_ctrl = BME680_GET_BITS_POS_0(data_array[BME680_REG_HCTRL_INDEX],
  500. BME680_HCTRL);
  501. if (desired_settings & (BME680_RUN_GAS_SEL | BME680_NBCONV_SEL)) {
  502. dev->gas_sett.nb_conv = BME680_GET_BITS_POS_0(data_array[BME680_REG_NBCONV_INDEX],
  503. BME680_NBCONV);
  504. dev->gas_sett.run_gas = BME680_GET_BITS(data_array[BME680_REG_RUN_GAS_INDEX],
  505. BME680_RUN_GAS);
  506. }
  507. }
  508. } else {
  509. rslt = BME680_E_NULL_PTR;
  510. }
  511. return rslt;
  512. }
  513. /*!
  514. * @brief This API is used to set the power mode of the sensor.
  515. */
  516. int8_t bme680_set_sensor_mode(struct bme680_dev *dev)
  517. {
  518. int8_t rslt;
  519. uint8_t tmp_pow_mode;
  520. uint8_t pow_mode = 0;
  521. uint8_t reg_addr = BME680_CONF_T_P_MODE_ADDR;
  522. /* Check for null pointer in the device structure*/
  523. rslt = null_ptr_check(dev);
  524. if (rslt == BME680_OK) {
  525. /* Call repeatedly until in sleep */
  526. do {
  527. rslt = bme680_get_regs(BME680_CONF_T_P_MODE_ADDR, &tmp_pow_mode, 1, dev);
  528. if (rslt == BME680_OK) {
  529. /* Put to sleep before changing mode */
  530. pow_mode = (tmp_pow_mode & BME680_MODE_MSK);
  531. if (pow_mode != BME680_SLEEP_MODE) {
  532. tmp_pow_mode = tmp_pow_mode & (~BME680_MODE_MSK); /* Set to sleep */
  533. rslt = bme680_set_regs(&reg_addr, &tmp_pow_mode, 1, dev);
  534. dev->delay_ms(BME680_POLL_PERIOD_MS);
  535. }
  536. }
  537. } while (pow_mode != BME680_SLEEP_MODE);
  538. /* Already in sleep */
  539. if (dev->power_mode != BME680_SLEEP_MODE) {
  540. tmp_pow_mode = (tmp_pow_mode & ~BME680_MODE_MSK) | (dev->power_mode & BME680_MODE_MSK);
  541. if (rslt == BME680_OK)
  542. rslt = bme680_set_regs(&reg_addr, &tmp_pow_mode, 1, dev);
  543. }
  544. }
  545. return rslt;
  546. }
  547. /*!
  548. * @brief This API is used to get the power mode of the sensor.
  549. */
  550. int8_t bme680_get_sensor_mode(struct bme680_dev *dev)
  551. {
  552. int8_t rslt;
  553. uint8_t mode;
  554. /* Check for null pointer in the device structure*/
  555. rslt = null_ptr_check(dev);
  556. if (rslt == BME680_OK) {
  557. rslt = bme680_get_regs(BME680_CONF_T_P_MODE_ADDR, &mode, 1, dev);
  558. /* Masking the other register bit info*/
  559. dev->power_mode = mode & BME680_MODE_MSK;
  560. }
  561. return rslt;
  562. }
  563. /*!
  564. * @brief This API is used to set the profile duration of the sensor.
  565. */
  566. void bme680_set_profile_dur(uint16_t duration, struct bme680_dev *dev)
  567. {
  568. uint32_t tph_dur; /* Calculate in us */
  569. uint32_t meas_cycles;
  570. uint8_t os_to_meas_cycles[6] = {0, 1, 2, 4, 8, 16};
  571. meas_cycles = os_to_meas_cycles[dev->tph_sett.os_temp];
  572. meas_cycles += os_to_meas_cycles[dev->tph_sett.os_pres];
  573. meas_cycles += os_to_meas_cycles[dev->tph_sett.os_hum];
  574. /* TPH measurement duration */
  575. tph_dur = meas_cycles * UINT32_C(1963);
  576. tph_dur += UINT32_C(477 * 4); /* TPH switching duration */
  577. tph_dur += UINT32_C(477 * 5); /* Gas measurement duration */
  578. tph_dur += UINT32_C(500); /* Get it to the closest whole number.*/
  579. tph_dur /= UINT32_C(1000); /* Convert to ms */
  580. tph_dur += UINT32_C(1); /* Wake up duration of 1ms */
  581. /* The remaining time should be used for heating */
  582. dev->gas_sett.heatr_dur = duration - (uint16_t) tph_dur;
  583. }
  584. /*!
  585. * @brief This API is used to get the profile duration of the sensor.
  586. */
  587. void bme680_get_profile_dur(uint16_t *duration, const struct bme680_dev *dev)
  588. {
  589. uint32_t tph_dur; /* Calculate in us */
  590. uint32_t meas_cycles;
  591. uint8_t os_to_meas_cycles[6] = {0, 1, 2, 4, 8, 16};
  592. meas_cycles = os_to_meas_cycles[dev->tph_sett.os_temp];
  593. meas_cycles += os_to_meas_cycles[dev->tph_sett.os_pres];
  594. meas_cycles += os_to_meas_cycles[dev->tph_sett.os_hum];
  595. /* TPH measurement duration */
  596. tph_dur = meas_cycles * UINT32_C(1963);
  597. tph_dur += UINT32_C(477 * 4); /* TPH switching duration */
  598. tph_dur += UINT32_C(477 * 5); /* Gas measurement duration */
  599. tph_dur += UINT32_C(500); /* Get it to the closest whole number.*/
  600. tph_dur /= UINT32_C(1000); /* Convert to ms */
  601. tph_dur += UINT32_C(1); /* Wake up duration of 1ms */
  602. *duration = (uint16_t) tph_dur;
  603. /* Get the gas duration only when the run gas is enabled */
  604. if (dev->gas_sett.run_gas) {
  605. /* The remaining time should be used for heating */
  606. *duration += dev->gas_sett.heatr_dur;
  607. }
  608. }
  609. /*!
  610. * @brief This API reads the pressure, temperature and humidity and gas data
  611. * from the sensor, compensates the data and store it in the bme680_data
  612. * structure instance passed by the user.
  613. */
  614. int8_t bme680_get_sensor_data(struct bme680_field_data *data, struct bme680_dev *dev)
  615. {
  616. int8_t rslt;
  617. /* Check for null pointer in the device structure*/
  618. rslt = null_ptr_check(dev);
  619. if (rslt == BME680_OK) {
  620. /* Reading the sensor data in forced mode only */
  621. rslt = read_field_data(data, dev);
  622. if (rslt == BME680_OK) {
  623. if (data->status & BME680_NEW_DATA_MSK)
  624. dev->new_fields = 1;
  625. else
  626. dev->new_fields = 0;
  627. }
  628. }
  629. return rslt;
  630. }
  631. /*!
  632. * @brief This internal API is used to read the calibrated data from the sensor.
  633. */
  634. static int8_t get_calib_data(struct bme680_dev *dev)
  635. {
  636. int8_t rslt;
  637. uint8_t coeff_array[BME680_COEFF_SIZE] = { 0 };
  638. uint8_t temp_var = 0; /* Temporary variable */
  639. /* Check for null pointer in the device structure*/
  640. rslt = null_ptr_check(dev);
  641. if (rslt == BME680_OK) {
  642. rslt = bme680_get_regs(BME680_COEFF_ADDR1, coeff_array, BME680_COEFF_ADDR1_LEN, dev);
  643. /* Append the second half in the same array */
  644. if (rslt == BME680_OK)
  645. rslt = bme680_get_regs(BME680_COEFF_ADDR2, &coeff_array[BME680_COEFF_ADDR1_LEN]
  646. , BME680_COEFF_ADDR2_LEN, dev);
  647. /* Temperature related coefficients */
  648. dev->calib.par_t1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T1_MSB_REG],
  649. coeff_array[BME680_T1_LSB_REG]));
  650. dev->calib.par_t2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T2_MSB_REG],
  651. coeff_array[BME680_T2_LSB_REG]));
  652. dev->calib.par_t3 = (int8_t) (coeff_array[BME680_T3_REG]);
  653. /* Pressure related coefficients */
  654. dev->calib.par_p1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P1_MSB_REG],
  655. coeff_array[BME680_P1_LSB_REG]));
  656. dev->calib.par_p2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P2_MSB_REG],
  657. coeff_array[BME680_P2_LSB_REG]));
  658. dev->calib.par_p3 = (int8_t) coeff_array[BME680_P3_REG];
  659. dev->calib.par_p4 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P4_MSB_REG],
  660. coeff_array[BME680_P4_LSB_REG]));
  661. dev->calib.par_p5 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P5_MSB_REG],
  662. coeff_array[BME680_P5_LSB_REG]));
  663. dev->calib.par_p6 = (int8_t) (coeff_array[BME680_P6_REG]);
  664. dev->calib.par_p7 = (int8_t) (coeff_array[BME680_P7_REG]);
  665. dev->calib.par_p8 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P8_MSB_REG],
  666. coeff_array[BME680_P8_LSB_REG]));
  667. dev->calib.par_p9 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P9_MSB_REG],
  668. coeff_array[BME680_P9_LSB_REG]));
  669. dev->calib.par_p10 = (uint8_t) (coeff_array[BME680_P10_REG]);
  670. /* Humidity related coefficients */
  671. dev->calib.par_h1 = (uint16_t) (((uint16_t) coeff_array[BME680_H1_MSB_REG] << BME680_HUM_REG_SHIFT_VAL)
  672. | (coeff_array[BME680_H1_LSB_REG] & BME680_BIT_H1_DATA_MSK));
  673. dev->calib.par_h2 = (uint16_t) (((uint16_t) coeff_array[BME680_H2_MSB_REG] << BME680_HUM_REG_SHIFT_VAL)
  674. | ((coeff_array[BME680_H2_LSB_REG]) >> BME680_HUM_REG_SHIFT_VAL));
  675. dev->calib.par_h3 = (int8_t) coeff_array[BME680_H3_REG];
  676. dev->calib.par_h4 = (int8_t) coeff_array[BME680_H4_REG];
  677. dev->calib.par_h5 = (int8_t) coeff_array[BME680_H5_REG];
  678. dev->calib.par_h6 = (uint8_t) coeff_array[BME680_H6_REG];
  679. dev->calib.par_h7 = (int8_t) coeff_array[BME680_H7_REG];
  680. /* Gas heater related coefficients */
  681. dev->calib.par_gh1 = (int8_t) coeff_array[BME680_GH1_REG];
  682. dev->calib.par_gh2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_GH2_MSB_REG],
  683. coeff_array[BME680_GH2_LSB_REG]));
  684. dev->calib.par_gh3 = (int8_t) coeff_array[BME680_GH3_REG];
  685. /* Other coefficients */
  686. if (rslt == BME680_OK) {
  687. rslt = bme680_get_regs(BME680_ADDR_RES_HEAT_RANGE_ADDR, &temp_var, 1, dev);
  688. dev->calib.res_heat_range = ((temp_var & BME680_RHRANGE_MSK) / 16);
  689. if (rslt == BME680_OK) {
  690. rslt = bme680_get_regs(BME680_ADDR_RES_HEAT_VAL_ADDR, &temp_var, 1, dev);
  691. dev->calib.res_heat_val = (int8_t) temp_var;
  692. if (rslt == BME680_OK)
  693. rslt = bme680_get_regs(BME680_ADDR_RANGE_SW_ERR_ADDR, &temp_var, 1, dev);
  694. }
  695. }
  696. dev->calib.range_sw_err = ((int8_t) temp_var & (int8_t) BME680_RSERROR_MSK) / 16;
  697. }
  698. return rslt;
  699. }
  700. /*!
  701. * @brief This internal API is used to set the gas configuration of the sensor.
  702. */
  703. static int8_t set_gas_config(struct bme680_dev *dev)
  704. {
  705. int8_t rslt;
  706. /* Check for null pointer in the device structure*/
  707. rslt = null_ptr_check(dev);
  708. if (rslt == BME680_OK) {
  709. uint8_t reg_addr[2] = {0};
  710. uint8_t reg_data[2] = {0};
  711. if (dev->power_mode == BME680_FORCED_MODE) {
  712. reg_addr[0] = BME680_RES_HEAT0_ADDR;
  713. reg_data[0] = calc_heater_res(dev->gas_sett.heatr_temp, dev);
  714. reg_addr[1] = BME680_GAS_WAIT0_ADDR;
  715. reg_data[1] = calc_heater_dur(dev->gas_sett.heatr_dur);
  716. dev->gas_sett.nb_conv = 0;
  717. } else {
  718. rslt = BME680_W_DEFINE_PWR_MODE;
  719. }
  720. if (rslt == BME680_OK)
  721. rslt = bme680_set_regs(reg_addr, reg_data, 2, dev);
  722. }
  723. return rslt;
  724. }
  725. /*!
  726. * @brief This internal API is used to get the gas configuration of the sensor.
  727. * @note heatr_temp and heatr_dur values are currently register data
  728. * and not the actual values set
  729. */
  730. static int8_t get_gas_config(struct bme680_dev *dev)
  731. {
  732. int8_t rslt;
  733. /* starting address of the register array for burst read*/
  734. uint8_t reg_addr1 = BME680_ADDR_SENS_CONF_START;
  735. uint8_t reg_addr2 = BME680_ADDR_GAS_CONF_START;
  736. uint8_t reg_data = 0;
  737. /* Check for null pointer in the device structure*/
  738. rslt = null_ptr_check(dev);
  739. if (rslt == BME680_OK) {
  740. if (BME680_SPI_INTF == dev->intf) {
  741. /* Memory page switch the SPI address*/
  742. rslt = set_mem_page(reg_addr1, dev);
  743. }
  744. if (rslt == BME680_OK) {
  745. rslt = bme680_get_regs(reg_addr1, &reg_data, 1, dev);
  746. if (rslt == BME680_OK) {
  747. dev->gas_sett.heatr_temp = reg_data;
  748. rslt = bme680_get_regs(reg_addr2, &reg_data, 1, dev);
  749. if (rslt == BME680_OK) {
  750. /* Heating duration register value */
  751. dev->gas_sett.heatr_dur = reg_data;
  752. }
  753. }
  754. }
  755. }
  756. return rslt;
  757. }
  758. #ifndef BME680_FLOAT_POINT_COMPENSATION
  759. /*!
  760. * @brief This internal API is used to calculate the temperature value.
  761. */
  762. static int16_t calc_temperature(uint32_t temp_adc, struct bme680_dev *dev)
  763. {
  764. int64_t var1;
  765. int64_t var2;
  766. int64_t var3;
  767. int16_t calc_temp;
  768. var1 = ((int32_t) temp_adc >> 3) - ((int32_t) dev->calib.par_t1 << 1);
  769. var2 = (var1 * (int32_t) dev->calib.par_t2) >> 11;
  770. var3 = ((var1 >> 1) * (var1 >> 1)) >> 12;
  771. var3 = ((var3) * ((int32_t) dev->calib.par_t3 << 4)) >> 14;
  772. dev->calib.t_fine = (int32_t) (var2 + var3);
  773. calc_temp = (int16_t) (((dev->calib.t_fine * 5) + 128) >> 8);
  774. return calc_temp;
  775. }
  776. /*!
  777. * @brief This internal API is used to calculate the pressure value.
  778. */
  779. static uint32_t calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev)
  780. {
  781. int32_t var1;
  782. int32_t var2;
  783. int32_t var3;
  784. int32_t pressure_comp;
  785. var1 = (((int32_t)dev->calib.t_fine) >> 1) - 64000;
  786. var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) *
  787. (int32_t)dev->calib.par_p6) >> 2;
  788. var2 = var2 + ((var1 * (int32_t)dev->calib.par_p5) << 1);
  789. var2 = (var2 >> 2) + ((int32_t)dev->calib.par_p4 << 16);
  790. var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
  791. ((int32_t)dev->calib.par_p3 << 5)) >> 3) +
  792. (((int32_t)dev->calib.par_p2 * var1) >> 1);
  793. var1 = var1 >> 18;
  794. var1 = ((32768 + var1) * (int32_t)dev->calib.par_p1) >> 15;
  795. pressure_comp = 1048576 - pres_adc;
  796. pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125));
  797. if (pressure_comp >= BME680_MAX_OVERFLOW_VAL)
  798. pressure_comp = ((pressure_comp / var1) << 1);
  799. else
  800. pressure_comp = ((pressure_comp << 1) / var1);
  801. var1 = ((int32_t)dev->calib.par_p9 * (int32_t)(((pressure_comp >> 3) *
  802. (pressure_comp >> 3)) >> 13)) >> 12;
  803. var2 = ((int32_t)(pressure_comp >> 2) *
  804. (int32_t)dev->calib.par_p8) >> 13;
  805. var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) *
  806. (int32_t)(pressure_comp >> 8) *
  807. (int32_t)dev->calib.par_p10) >> 17;
  808. pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 +
  809. ((int32_t)dev->calib.par_p7 << 7)) >> 4);
  810. return (uint32_t)pressure_comp;
  811. }
  812. /*!
  813. * @brief This internal API is used to calculate the humidity value.
  814. */
  815. static uint32_t calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
  816. {
  817. int32_t var1;
  818. int32_t var2;
  819. int32_t var3;
  820. int32_t var4;
  821. int32_t var5;
  822. int32_t var6;
  823. int32_t temp_scaled;
  824. int32_t calc_hum;
  825. temp_scaled = (((int32_t) dev->calib.t_fine * 5) + 128) >> 8;
  826. var1 = (int32_t) (hum_adc - ((int32_t) ((int32_t) dev->calib.par_h1 * 16)))
  827. - (((temp_scaled * (int32_t) dev->calib.par_h3) / ((int32_t) 100)) >> 1);
  828. var2 = ((int32_t) dev->calib.par_h2
  829. * (((temp_scaled * (int32_t) dev->calib.par_h4) / ((int32_t) 100))
  830. + (((temp_scaled * ((temp_scaled * (int32_t) dev->calib.par_h5) / ((int32_t) 100))) >> 6)
  831. / ((int32_t) 100)) + (int32_t) (1 << 14))) >> 10;
  832. var3 = var1 * var2;
  833. var4 = (int32_t) dev->calib.par_h6 << 7;
  834. var4 = ((var4) + ((temp_scaled * (int32_t) dev->calib.par_h7) / ((int32_t) 100))) >> 4;
  835. var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
  836. var6 = (var4 * var5) >> 1;
  837. calc_hum = (((var3 + var6) >> 10) * ((int32_t) 1000)) >> 12;
  838. if (calc_hum > 100000) /* Cap at 100%rH */
  839. calc_hum = 100000;
  840. else if (calc_hum < 0)
  841. calc_hum = 0;
  842. return (uint32_t) calc_hum;
  843. }
  844. /*!
  845. * @brief This internal API is used to calculate the Gas Resistance value.
  846. */
  847. static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev)
  848. {
  849. int64_t var1;
  850. uint64_t var2;
  851. int64_t var3;
  852. uint32_t calc_gas_res;
  853. /**Look up table 1 for the possible gas range values */
  854. uint32_t lookupTable1[16] = { UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647),
  855. UINT32_C(2147483647), UINT32_C(2126008810), UINT32_C(2147483647), UINT32_C(2130303777),
  856. UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2143188679), UINT32_C(2136746228),
  857. UINT32_C(2147483647), UINT32_C(2126008810), UINT32_C(2147483647), UINT32_C(2147483647) };
  858. /**Look up table 2 for the possible gas range values */
  859. uint32_t lookupTable2[16] = { UINT32_C(4096000000), UINT32_C(2048000000), UINT32_C(1024000000), UINT32_C(512000000),
  860. UINT32_C(255744255), UINT32_C(127110228), UINT32_C(64000000), UINT32_C(32258064), UINT32_C(16016016),
  861. UINT32_C(8000000), UINT32_C(4000000), UINT32_C(2000000), UINT32_C(1000000), UINT32_C(500000),
  862. UINT32_C(250000), UINT32_C(125000) };
  863. var1 = (int64_t) ((1340 + (5 * (int64_t) dev->calib.range_sw_err)) *
  864. ((int64_t) lookupTable1[gas_range])) >> 16;
  865. var2 = (((int64_t) ((int64_t) gas_res_adc << 15) - (int64_t) (16777216)) + var1);
  866. var3 = (((int64_t) lookupTable2[gas_range] * (int64_t) var1) >> 9);
  867. calc_gas_res = (uint32_t) ((var3 + ((int64_t) var2 >> 1)) / (int64_t) var2);
  868. return calc_gas_res;
  869. }
  870. /*!
  871. * @brief This internal API is used to calculate the Heat Resistance value.
  872. */
  873. static uint8_t calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
  874. {
  875. uint8_t heatr_res;
  876. int32_t var1;
  877. int32_t var2;
  878. int32_t var3;
  879. int32_t var4;
  880. int32_t var5;
  881. int32_t heatr_res_x100;
  882. if (temp > 400) /* Cap temperature */
  883. temp = 400;
  884. var1 = (((int32_t) dev->amb_temp * dev->calib.par_gh3) / 1000) * 256;
  885. var2 = (dev->calib.par_gh1 + 784) * (((((dev->calib.par_gh2 + 154009) * temp * 5) / 100) + 3276800) / 10);
  886. var3 = var1 + (var2 / 2);
  887. var4 = (var3 / (dev->calib.res_heat_range + 4));
  888. var5 = (131 * dev->calib.res_heat_val) + 65536;
  889. heatr_res_x100 = (int32_t) (((var4 / var5) - 250) * 34);
  890. heatr_res = (uint8_t) ((heatr_res_x100 + 50) / 100);
  891. return heatr_res;
  892. }
  893. #else
  894. /*!
  895. * @brief This internal API is used to calculate the
  896. * temperature value in float format
  897. */
  898. static float calc_temperature(uint32_t temp_adc, struct bme680_dev *dev)
  899. {
  900. float var1 = 0;
  901. float var2 = 0;
  902. float calc_temp = 0;
  903. /* calculate var1 data */
  904. var1 = ((((float)temp_adc / 16384.0f) - ((float)dev->calib.par_t1 / 1024.0f))
  905. * ((float)dev->calib.par_t2));
  906. /* calculate var2 data */
  907. var2 = (((((float)temp_adc / 131072.0f) - ((float)dev->calib.par_t1 / 8192.0f)) *
  908. (((float)temp_adc / 131072.0f) - ((float)dev->calib.par_t1 / 8192.0f))) *
  909. ((float)dev->calib.par_t3 * 16.0f));
  910. /* t_fine value*/
  911. dev->calib.t_fine = (var1 + var2);
  912. /* compensated temperature data*/
  913. calc_temp = ((dev->calib.t_fine) / 5120.0f);
  914. return calc_temp;
  915. }
  916. /*!
  917. * @brief This internal API is used to calculate the
  918. * pressure value in float format
  919. */
  920. static float calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev)
  921. {
  922. float var1 = 0;
  923. float var2 = 0;
  924. float var3 = 0;
  925. float calc_pres = 0;
  926. var1 = (((float)dev->calib.t_fine / 2.0f) - 64000.0f);
  927. var2 = var1 * var1 * (((float)dev->calib.par_p6) / (131072.0f));
  928. var2 = var2 + (var1 * ((float)dev->calib.par_p5) * 2.0f);
  929. var2 = (var2 / 4.0f) + (((float)dev->calib.par_p4) * 65536.0f);
  930. var1 = (((((float)dev->calib.par_p3 * var1 * var1) / 16384.0f)
  931. + ((float)dev->calib.par_p2 * var1)) / 524288.0f);
  932. var1 = ((1.0f + (var1 / 32768.0f)) * ((float)dev->calib.par_p1));
  933. calc_pres = (1048576.0f - ((float)pres_adc));
  934. /* Avoid exception caused by division by zero */
  935. if ((int)var1 != 0) {
  936. calc_pres = (((calc_pres - (var2 / 4096.0f)) * 6250.0f) / var1);
  937. var1 = (((float)dev->calib.par_p9) * calc_pres * calc_pres) / 2147483648.0f;
  938. var2 = calc_pres * (((float)dev->calib.par_p8) / 32768.0f);
  939. var3 = ((calc_pres / 256.0f) * (calc_pres / 256.0f) * (calc_pres / 256.0f)
  940. * (dev->calib.par_p10 / 131072.0f));
  941. calc_pres = (calc_pres + (var1 + var2 + var3 + ((float)dev->calib.par_p7 * 128.0f)) / 16.0f);
  942. } else {
  943. calc_pres = 0;
  944. }
  945. return calc_pres;
  946. }
  947. /*!
  948. * @brief This internal API is used to calculate the
  949. * humidity value in float format
  950. */
  951. static float calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
  952. {
  953. float calc_hum = 0;
  954. float var1 = 0;
  955. float var2 = 0;
  956. float var3 = 0;
  957. float var4 = 0;
  958. float temp_comp;
  959. /* compensated temperature data*/
  960. temp_comp = ((dev->calib.t_fine) / 5120.0f);
  961. var1 = (float)((float)hum_adc) - (((float)dev->calib.par_h1 * 16.0f) + (((float)dev->calib.par_h3 / 2.0f)
  962. * temp_comp));
  963. var2 = var1 * ((float)(((float) dev->calib.par_h2 / 262144.0f) * (1.0f + (((float)dev->calib.par_h4 / 16384.0f)
  964. * temp_comp) + (((float)dev->calib.par_h5 / 1048576.0f) * temp_comp * temp_comp))));
  965. var3 = (float) dev->calib.par_h6 / 16384.0f;
  966. var4 = (float) dev->calib.par_h7 / 2097152.0f;
  967. calc_hum = var2 + ((var3 + (var4 * temp_comp)) * var2 * var2);
  968. if (calc_hum > 100.0f)
  969. calc_hum = 100.0f;
  970. else if (calc_hum < 0.0f)
  971. calc_hum = 0.0f;
  972. return calc_hum;
  973. }
  974. /*!
  975. * @brief This internal API is used to calculate the
  976. * gas resistance value in float format
  977. */
  978. static float calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev)
  979. {
  980. float calc_gas_res;
  981. float var1 = 0;
  982. float var2 = 0;
  983. float var3 = 0;
  984. const float lookup_k1_range[16] = {
  985. 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -0.8,
  986. 0.0, 0.0, -0.2, -0.5, 0.0, -1.0, 0.0, 0.0};
  987. const float lookup_k2_range[16] = {
  988. 0.0, 0.0, 0.0, 0.0, 0.1, 0.7, 0.0, -0.8,
  989. -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  990. var1 = (1340.0f + (5.0f * dev->calib.range_sw_err));
  991. var2 = (var1) * (1.0f + lookup_k1_range[gas_range]/100.0f);
  992. var3 = 1.0f + (lookup_k2_range[gas_range]/100.0f);
  993. calc_gas_res = 1.0f / (float)(var3 * (0.000000125f) * (float)(1 << gas_range) * (((((float)gas_res_adc)
  994. - 512.0f)/var2) + 1.0f));
  995. return calc_gas_res;
  996. }
  997. /*!
  998. * @brief This internal API is used to calculate the
  999. * heater resistance value in float format
  1000. */
  1001. static float calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
  1002. {
  1003. float var1 = 0;
  1004. float var2 = 0;
  1005. float var3 = 0;
  1006. float var4 = 0;
  1007. float var5 = 0;
  1008. float res_heat = 0;
  1009. if (temp > 400) /* Cap temperature */
  1010. temp = 400;
  1011. var1 = (((float)dev->calib.par_gh1 / (16.0f)) + 49.0f);
  1012. var2 = ((((float)dev->calib.par_gh2 / (32768.0f)) * (0.0005f)) + 0.00235f);
  1013. var3 = ((float)dev->calib.par_gh3 / (1024.0f));
  1014. var4 = (var1 * (1.0f + (var2 * (float)temp)));
  1015. var5 = (var4 + (var3 * (float)dev->amb_temp));
  1016. res_heat = (uint8_t)(3.4f * ((var5 * (4 / (4 + (float)dev->calib.res_heat_range)) *
  1017. (1/(1 + ((float) dev->calib.res_heat_val * 0.002f)))) - 25));
  1018. return res_heat;
  1019. }
  1020. #endif
  1021. /*!
  1022. * @brief This internal API is used to calculate the Heat duration value.
  1023. */
  1024. static uint8_t calc_heater_dur(uint16_t dur)
  1025. {
  1026. uint8_t factor = 0;
  1027. uint8_t durval;
  1028. if (dur >= 0xfc0) {
  1029. durval = 0xff; /* Max duration*/
  1030. } else {
  1031. while (dur > 0x3F) {
  1032. dur = dur / 4;
  1033. factor += 1;
  1034. }
  1035. durval = (uint8_t) (dur + (factor * 64));
  1036. }
  1037. return durval;
  1038. }
  1039. /*!
  1040. * @brief This internal API is used to calculate the field data of sensor.
  1041. */
  1042. static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev *dev)
  1043. {
  1044. int8_t rslt;
  1045. uint8_t buff[BME680_FIELD_LENGTH] = { 0 };
  1046. uint8_t gas_range;
  1047. uint32_t adc_temp;
  1048. uint32_t adc_pres;
  1049. uint16_t adc_hum;
  1050. uint16_t adc_gas_res;
  1051. uint8_t tries = 10;
  1052. /* Check for null pointer in the device structure*/
  1053. rslt = null_ptr_check(dev);
  1054. do {
  1055. if (rslt == BME680_OK) {
  1056. rslt = bme680_get_regs(((uint8_t) (BME680_FIELD0_ADDR)), buff, (uint16_t) BME680_FIELD_LENGTH,
  1057. dev);
  1058. data->status = buff[0] & BME680_NEW_DATA_MSK;
  1059. data->gas_index = buff[0] & BME680_GAS_INDEX_MSK;
  1060. data->meas_index = buff[1];
  1061. /* read the raw data from the sensor */
  1062. adc_pres = (uint32_t) (((uint32_t) buff[2] * 4096) | ((uint32_t) buff[3] * 16)
  1063. | ((uint32_t) buff[4] / 16));
  1064. adc_temp = (uint32_t) (((uint32_t) buff[5] * 4096) | ((uint32_t) buff[6] * 16)
  1065. | ((uint32_t) buff[7] / 16));
  1066. adc_hum = (uint16_t) (((uint32_t) buff[8] * 256) | (uint32_t) buff[9]);
  1067. adc_gas_res = (uint16_t) ((uint32_t) buff[13] * 4 | (((uint32_t) buff[14]) / 64));
  1068. gas_range = buff[14] & BME680_GAS_RANGE_MSK;
  1069. data->status |= buff[14] & BME680_GASM_VALID_MSK;
  1070. data->status |= buff[14] & BME680_HEAT_STAB_MSK;
  1071. if (data->status & BME680_NEW_DATA_MSK) {
  1072. data->temperature = calc_temperature(adc_temp, dev);
  1073. data->pressure = calc_pressure(adc_pres, dev);
  1074. data->humidity = calc_humidity(adc_hum, dev);
  1075. data->gas_resistance = calc_gas_resistance(adc_gas_res, gas_range, dev);
  1076. break;
  1077. }
  1078. /* Delay to poll the data */
  1079. dev->delay_ms(BME680_POLL_PERIOD_MS);
  1080. }
  1081. tries--;
  1082. } while (tries);
  1083. if (!tries)
  1084. rslt = BME680_W_NO_NEW_DATA;
  1085. return rslt;
  1086. }
  1087. /*!
  1088. * @brief This internal API is used to set the memory page based on register address.
  1089. */
  1090. static int8_t set_mem_page(uint8_t reg_addr, struct bme680_dev *dev)
  1091. {
  1092. int8_t rslt;
  1093. uint8_t reg;
  1094. uint8_t mem_page;
  1095. /* Check for null pointers in the device structure*/
  1096. rslt = null_ptr_check(dev);
  1097. if (rslt == BME680_OK) {
  1098. if (reg_addr > 0x7f)
  1099. mem_page = BME680_MEM_PAGE1;
  1100. else
  1101. mem_page = BME680_MEM_PAGE0;
  1102. if (mem_page != dev->mem_page) {
  1103. dev->mem_page = mem_page;
  1104. dev->com_rslt = dev->read(dev->dev_id, BME680_MEM_PAGE_ADDR | BME680_SPI_RD_MSK, &reg, 1);
  1105. if (dev->com_rslt != 0)
  1106. rslt = BME680_E_COM_FAIL;
  1107. if (rslt == BME680_OK) {
  1108. reg = reg & (~BME680_MEM_PAGE_MSK);
  1109. reg = reg | (dev->mem_page & BME680_MEM_PAGE_MSK);
  1110. dev->com_rslt = dev->write(dev->dev_id, BME680_MEM_PAGE_ADDR & BME680_SPI_WR_MSK,
  1111. &reg, 1);
  1112. if (dev->com_rslt != 0)
  1113. rslt = BME680_E_COM_FAIL;
  1114. }
  1115. }
  1116. }
  1117. return rslt;
  1118. }
  1119. /*!
  1120. * @brief This internal API is used to get the memory page based on register address.
  1121. */
  1122. static int8_t get_mem_page(struct bme680_dev *dev)
  1123. {
  1124. int8_t rslt;
  1125. uint8_t reg;
  1126. /* Check for null pointer in the device structure*/
  1127. rslt = null_ptr_check(dev);
  1128. if (rslt == BME680_OK) {
  1129. dev->com_rslt = dev->read(dev->dev_id, BME680_MEM_PAGE_ADDR | BME680_SPI_RD_MSK, &reg, 1);
  1130. if (dev->com_rslt != 0)
  1131. rslt = BME680_E_COM_FAIL;
  1132. else
  1133. dev->mem_page = reg & BME680_MEM_PAGE_MSK;
  1134. }
  1135. return rslt;
  1136. }
  1137. /*!
  1138. * @brief This internal API is used to validate the boundary
  1139. * conditions.
  1140. */
  1141. static int8_t boundary_check(uint8_t *value, uint8_t min, uint8_t max, struct bme680_dev *dev)
  1142. {
  1143. int8_t rslt = BME680_OK;
  1144. if (value != NULL) {
  1145. /* Check if value is below minimum value */
  1146. if (*value < min) {
  1147. /* Auto correct the invalid value to minimum value */
  1148. *value = min;
  1149. dev->info_msg |= BME680_I_MIN_CORRECTION;
  1150. }
  1151. /* Check if value is above maximum value */
  1152. if (*value > max) {
  1153. /* Auto correct the invalid value to maximum value */
  1154. *value = max;
  1155. dev->info_msg |= BME680_I_MAX_CORRECTION;
  1156. }
  1157. } else {
  1158. rslt = BME680_E_NULL_PTR;
  1159. }
  1160. return rslt;
  1161. }
  1162. /*!
  1163. * @brief This internal API is used to validate the device structure pointer for
  1164. * null conditions.
  1165. */
  1166. static int8_t null_ptr_check(const struct bme680_dev *dev)
  1167. {
  1168. int8_t rslt;
  1169. if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL)) {
  1170. /* Device structure pointer is not valid */
  1171. rslt = BME680_E_NULL_PTR;
  1172. } else {
  1173. /* Device structure is fine */
  1174. rslt = BME680_OK;
  1175. }
  1176. return rslt;
  1177. }