bme280_defs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * Copyright (C) 2018 - 2019 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 bme280_defs.h
  43. * @date 08 Mar 2019
  44. * @version 3.3.6
  45. * @brief
  46. *
  47. */
  48. /*! @file bme280_defs.h
  49. * @brief Sensor driver for BME280 sensor
  50. */
  51. /*!
  52. * @defgroup BME280 SENSOR API
  53. * @brief
  54. */
  55. #ifndef BME280_DEFS_H_
  56. #define BME280_DEFS_H_
  57. /********************************************************/
  58. /* header includes */
  59. #ifdef __KERNEL__
  60. #include <linux/types.h>
  61. #include <linux/kernel.h>
  62. #else
  63. #include <stdint.h>
  64. #include <stddef.h>
  65. #endif
  66. /********************************************************/
  67. /*! @name Common macros */
  68. /********************************************************/
  69. #if !defined(UINT8_C) && !defined(INT8_C)
  70. #define INT8_C(x) S8_C(x)
  71. #define UINT8_C(x) U8_C(x)
  72. #endif
  73. #if !defined(UINT16_C) && !defined(INT16_C)
  74. #define INT16_C(x) S16_C(x)
  75. #define UINT16_C(x) U16_C(x)
  76. #endif
  77. #if !defined(INT32_C) && !defined(UINT32_C)
  78. #define INT32_C(x) S32_C(x)
  79. #define UINT32_C(x) U32_C(x)
  80. #endif
  81. #if !defined(INT64_C) && !defined(UINT64_C)
  82. #define INT64_C(x) S64_C(x)
  83. #define UINT64_C(x) U64_C(x)
  84. #endif
  85. /**@}*/
  86. /**\name C standard macros */
  87. #ifndef NULL
  88. #ifdef __cplusplus
  89. #define NULL 0
  90. #else
  91. #define NULL ((void *) 0)
  92. #endif
  93. #endif
  94. /********************************************************/
  95. #ifndef BME280_FLOAT_ENABLE
  96. /* #define BME280_FLOAT_ENABLE */
  97. #endif
  98. #ifndef BME280_FLOAT_ENABLE
  99. #ifndef BME280_64BIT_ENABLE
  100. /* #define BME280_64BIT_ENABLE */
  101. #endif
  102. #endif
  103. #ifndef TRUE
  104. #define TRUE UINT8_C(1)
  105. #endif
  106. #ifndef FALSE
  107. #define FALSE UINT8_C(0)
  108. #endif
  109. /**\name I2C addresses */
  110. #define BME280_I2C_ADDR_PRIM UINT8_C(0x76 << 1)
  111. #define BME280_I2C_ADDR_SEC UINT8_C(0x77 << 1)
  112. /**\name BME280 chip identifier */
  113. #define BME280_CHIP_ID UINT8_C(0x60)
  114. /**\name Register Address */
  115. #define BME280_CHIP_ID_ADDR UINT8_C(0xD0)
  116. #define BME280_RESET_ADDR UINT8_C(0xE0)
  117. #define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88)
  118. #define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1)
  119. #define BME280_PWR_CTRL_ADDR UINT8_C(0xF4)
  120. #define BME280_CTRL_HUM_ADDR UINT8_C(0xF2)
  121. #define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4)
  122. #define BME280_CONFIG_ADDR UINT8_C(0xF5)
  123. #define BME280_DATA_ADDR UINT8_C(0xF7)
  124. /**\name API success code */
  125. #define BME280_OK INT8_C(0)
  126. /**\name API error codes */
  127. #define BME280_E_NULL_PTR INT8_C(-1)
  128. #define BME280_E_DEV_NOT_FOUND INT8_C(-2)
  129. #define BME280_E_INVALID_LEN INT8_C(-3)
  130. #define BME280_E_COMM_FAIL INT8_C(-4)
  131. #define BME280_E_SLEEP_MODE_FAIL INT8_C(-5)
  132. /**\name API warning codes */
  133. #define BME280_W_INVALID_OSR_MACRO INT8_C(1)
  134. /**\name Macros related to size */
  135. #define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26)
  136. #define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7)
  137. #define BME280_P_T_H_DATA_LEN UINT8_C(8)
  138. /**\name Sensor power modes */
  139. #define BME280_SLEEP_MODE UINT8_C(0x00)
  140. #define BME280_FORCED_MODE UINT8_C(0x01)
  141. #define BME280_NORMAL_MODE UINT8_C(0x03)
  142. /**\name Macro to combine two 8 bit data's to form a 16 bit data */
  143. #define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
  144. #define BME280_SET_BITS(reg_data, bitname, data) \
  145. ((reg_data & ~(bitname##_MSK)) | \
  146. ((data << bitname##_POS) & bitname##_MSK))
  147. #define BME280_SET_BITS_POS_0(reg_data, bitname, data) \
  148. ((reg_data & ~(bitname##_MSK)) | \
  149. (data & bitname##_MSK))
  150. #define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \
  151. (bitname##_POS))
  152. #define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))
  153. /**\name Macros for bit masking */
  154. #define BME280_SENSOR_MODE_MSK UINT8_C(0x03)
  155. #define BME280_SENSOR_MODE_POS UINT8_C(0x00)
  156. #define BME280_CTRL_HUM_MSK UINT8_C(0x07)
  157. #define BME280_CTRL_HUM_POS UINT8_C(0x00)
  158. #define BME280_CTRL_PRESS_MSK UINT8_C(0x1C)
  159. #define BME280_CTRL_PRESS_POS UINT8_C(0x02)
  160. #define BME280_CTRL_TEMP_MSK UINT8_C(0xE0)
  161. #define BME280_CTRL_TEMP_POS UINT8_C(0x05)
  162. #define BME280_FILTER_MSK UINT8_C(0x1C)
  163. #define BME280_FILTER_POS UINT8_C(0x02)
  164. #define BME280_STANDBY_MSK UINT8_C(0xE0)
  165. #define BME280_STANDBY_POS UINT8_C(0x05)
  166. /**\name Sensor component selection macros
  167. * These values are internal for API implementation. Don't relate this to
  168. * data sheet.
  169. */
  170. #define BME280_PRESS UINT8_C(1)
  171. #define BME280_TEMP UINT8_C(1 << 1)
  172. #define BME280_HUM UINT8_C(1 << 2)
  173. #define BME280_ALL UINT8_C(0x07)
  174. /**\name Settings selection macros */
  175. #define BME280_OSR_PRESS_SEL UINT8_C(1)
  176. #define BME280_OSR_TEMP_SEL UINT8_C(1 << 1)
  177. #define BME280_OSR_HUM_SEL UINT8_C(1 << 2)
  178. #define BME280_FILTER_SEL UINT8_C(1 << 3)
  179. #define BME280_STANDBY_SEL UINT8_C(1 << 4)
  180. #define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F)
  181. /**\name Oversampling macros */
  182. #define BME280_NO_OVERSAMPLING UINT8_C(0x00)
  183. #define BME280_OVERSAMPLING_1X UINT8_C(0x01)
  184. #define BME280_OVERSAMPLING_2X UINT8_C(0x02)
  185. #define BME280_OVERSAMPLING_4X UINT8_C(0x03)
  186. #define BME280_OVERSAMPLING_8X UINT8_C(0x04)
  187. #define BME280_OVERSAMPLING_16X UINT8_C(0x05)
  188. /**\name Standby duration selection macros */
  189. #define BME280_STANDBY_TIME_0_5_MS (0x00)
  190. #define BME280_STANDBY_TIME_62_5_MS (0x01)
  191. #define BME280_STANDBY_TIME_125_MS (0x02)
  192. #define BME280_STANDBY_TIME_250_MS (0x03)
  193. #define BME280_STANDBY_TIME_500_MS (0x04)
  194. #define BME280_STANDBY_TIME_1000_MS (0x05)
  195. #define BME280_STANDBY_TIME_10_MS (0x06)
  196. #define BME280_STANDBY_TIME_20_MS (0x07)
  197. /**\name Filter coefficient selection macros */
  198. #define BME280_FILTER_COEFF_OFF (0x00)
  199. #define BME280_FILTER_COEFF_2 (0x01)
  200. #define BME280_FILTER_COEFF_4 (0x02)
  201. #define BME280_FILTER_COEFF_8 (0x03)
  202. #define BME280_FILTER_COEFF_16 (0x04)
  203. /*!
  204. * @brief Interface selection Enums
  205. */
  206. enum bme280_intf {
  207. /*! SPI interface */
  208. BME280_SPI_INTF,
  209. /*! I2C interface */
  210. BME280_I2C_INTF
  211. };
  212. /*!
  213. * @brief Type definitions
  214. */
  215. typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len);
  216. typedef void (*bme280_delay_fptr_t)(uint32_t period);
  217. /*!
  218. * @brief Calibration data
  219. */
  220. struct bme280_calib_data
  221. {
  222. /**
  223. * @ Trim Variables
  224. */
  225. /**@{*/
  226. uint16_t dig_T1;
  227. int16_t dig_T2;
  228. int16_t dig_T3;
  229. uint16_t dig_P1;
  230. int16_t dig_P2;
  231. int16_t dig_P3;
  232. int16_t dig_P4;
  233. int16_t dig_P5;
  234. int16_t dig_P6;
  235. int16_t dig_P7;
  236. int16_t dig_P8;
  237. int16_t dig_P9;
  238. uint8_t dig_H1;
  239. int16_t dig_H2;
  240. uint8_t dig_H3;
  241. int16_t dig_H4;
  242. int16_t dig_H5;
  243. int8_t dig_H6;
  244. int32_t t_fine;
  245. /**@}*/
  246. };
  247. /*!
  248. * @brief bme280 sensor structure which comprises of temperature, pressure and
  249. * humidity data
  250. */
  251. #ifdef BME280_FLOAT_ENABLE
  252. struct bme280_data
  253. {
  254. /*! Compensated pressure */
  255. double pressure;
  256. /*! Compensated temperature */
  257. double temperature;
  258. /*! Compensated humidity */
  259. double humidity;
  260. };
  261. #else
  262. struct bme280_data
  263. {
  264. /*! Compensated pressure */
  265. uint32_t pressure;
  266. /*! Compensated temperature */
  267. int32_t temperature;
  268. /*! Compensated humidity */
  269. uint32_t humidity;
  270. };
  271. #endif /* BME280_USE_FLOATING_POINT */
  272. /*!
  273. * @brief bme280 sensor structure which comprises of uncompensated temperature,
  274. * pressure and humidity data
  275. */
  276. struct bme280_uncomp_data
  277. {
  278. /*! un-compensated pressure */
  279. uint32_t pressure;
  280. /*! un-compensated temperature */
  281. uint32_t temperature;
  282. /*! un-compensated humidity */
  283. uint32_t humidity;
  284. };
  285. /*!
  286. * @brief bme280 sensor settings structure which comprises of mode,
  287. * oversampling and filter settings.
  288. */
  289. struct bme280_settings
  290. {
  291. /*! pressure oversampling */
  292. uint8_t osr_p;
  293. /*! temperature oversampling */
  294. uint8_t osr_t;
  295. /*! humidity oversampling */
  296. uint8_t osr_h;
  297. /*! filter coefficient */
  298. uint8_t filter;
  299. /*! standby time */
  300. uint8_t standby_time;
  301. };
  302. /*!
  303. * @brief bme280 device structure
  304. */
  305. struct bme280_dev
  306. {
  307. /*! Chip Id */
  308. uint8_t chip_id;
  309. /*! Device Id */
  310. uint8_t dev_id;
  311. /*! SPI/I2C interface */
  312. enum bme280_intf intf;
  313. /*! Read function pointer */
  314. bme280_com_fptr_t read;
  315. /*! Write function pointer */
  316. bme280_com_fptr_t write;
  317. /*! Delay function pointer */
  318. bme280_delay_fptr_t delay_ms;
  319. /*! Trim data */
  320. struct bme280_calib_data calib_data;
  321. /*! Sensor settings */
  322. struct bme280_settings settings;
  323. };
  324. #endif /* BME280_DEFS_H_ */
  325. /** @}*/
  326. /** @}*/