Repo for ESP32 Weather Station Development
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
11KB

  1. /**
  2. * Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
  3. *
  4. * BSD-3-Clause
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * 3. Neither the name of the copyright holder nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  29. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  30. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * @file bme280_defs.h
  34. * @date 21/01/2020
  35. * @version 3.4.2
  36. *
  37. */
  38. /*! @file bme280_defs.h
  39. * @brief Sensor driver for BME280 sensor
  40. */
  41. /*!
  42. * @defgroup BME280 SENSOR API
  43. * @brief
  44. */
  45. #ifndef BME280_DEFS_H_
  46. #define BME280_DEFS_H_
  47. /********************************************************/
  48. /* header includes */
  49. #ifdef __KERNEL__
  50. #include <linux/types.h>
  51. #include <linux/kernel.h>
  52. #else
  53. #include <stdint.h>
  54. #include <stddef.h>
  55. #endif
  56. /********************************************************/
  57. /*! @name Common macros */
  58. /********************************************************/
  59. #if !defined(UINT8_C) && !defined(INT8_C)
  60. #define INT8_C(x) S8_C(x)
  61. #define UINT8_C(x) U8_C(x)
  62. #endif
  63. #if !defined(UINT16_C) && !defined(INT16_C)
  64. #define INT16_C(x) S16_C(x)
  65. #define UINT16_C(x) U16_C(x)
  66. #endif
  67. #if !defined(INT32_C) && !defined(UINT32_C)
  68. #define INT32_C(x) S32_C(x)
  69. #define UINT32_C(x) U32_C(x)
  70. #endif
  71. #if !defined(INT64_C) && !defined(UINT64_C)
  72. #define INT64_C(x) S64_C(x)
  73. #define UINT64_C(x) U64_C(x)
  74. #endif
  75. /**@}*/
  76. /**\name C standard macros */
  77. #ifndef NULL
  78. #ifdef __cplusplus
  79. #define NULL 0
  80. #else
  81. #define NULL ((void *) 0)
  82. #endif
  83. #endif
  84. /********************************************************/
  85. #ifndef BME280_FLOAT_ENABLE
  86. /* #define BME280_FLOAT_ENABLE */
  87. #endif
  88. #ifndef BME280_FLOAT_ENABLE
  89. #ifndef BME280_64BIT_ENABLE
  90. #define BME280_64BIT_ENABLE
  91. #endif
  92. #endif
  93. #ifndef TRUE
  94. #define TRUE UINT8_C(1)
  95. #endif
  96. #ifndef FALSE
  97. #define FALSE UINT8_C(0)
  98. #endif
  99. /**\name I2C addresses */
  100. #define BME280_I2C_ADDR_PRIM UINT8_C(0x76)
  101. #define BME280_I2C_ADDR_SEC UINT8_C(0x77)
  102. /**\name BME280 chip identifier */
  103. #define BME280_CHIP_ID UINT8_C(0x60)
  104. /**\name Register Address */
  105. #define BME280_CHIP_ID_ADDR UINT8_C(0xD0)
  106. #define BME280_RESET_ADDR UINT8_C(0xE0)
  107. #define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88)
  108. #define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1)
  109. #define BME280_PWR_CTRL_ADDR UINT8_C(0xF4)
  110. #define BME280_CTRL_HUM_ADDR UINT8_C(0xF2)
  111. #define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4)
  112. #define BME280_CONFIG_ADDR UINT8_C(0xF5)
  113. #define BME280_DATA_ADDR UINT8_C(0xF7)
  114. /**\name API success code */
  115. #define BME280_OK INT8_C(0)
  116. /**\name API error codes */
  117. #define BME280_E_NULL_PTR INT8_C(-1)
  118. #define BME280_E_DEV_NOT_FOUND INT8_C(-2)
  119. #define BME280_E_INVALID_LEN INT8_C(-3)
  120. #define BME280_E_COMM_FAIL INT8_C(-4)
  121. #define BME280_E_SLEEP_MODE_FAIL INT8_C(-5)
  122. #define BME280_E_NVM_COPY_FAILED INT8_C(-6)
  123. /**\name API warning codes */
  124. #define BME280_W_INVALID_OSR_MACRO INT8_C(1)
  125. /**\name Macros related to size */
  126. #define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26)
  127. #define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7)
  128. #define BME280_P_T_H_DATA_LEN UINT8_C(8)
  129. /**\name Sensor power modes */
  130. #define BME280_SLEEP_MODE UINT8_C(0x00)
  131. #define BME280_FORCED_MODE UINT8_C(0x01)
  132. #define BME280_NORMAL_MODE UINT8_C(0x03)
  133. /**\name Macro to combine two 8 bit data's to form a 16 bit data */
  134. #define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
  135. #define BME280_SET_BITS(reg_data, bitname, data) \
  136. ((reg_data & ~(bitname##_MSK)) | \
  137. ((data << bitname##_POS) & bitname##_MSK))
  138. #define BME280_SET_BITS_POS_0(reg_data, bitname, data) \
  139. ((reg_data & ~(bitname##_MSK)) | \
  140. (data & bitname##_MSK))
  141. #define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \
  142. (bitname##_POS))
  143. #define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))
  144. /**\name Macros for bit masking */
  145. #define BME280_SENSOR_MODE_MSK UINT8_C(0x03)
  146. #define BME280_SENSOR_MODE_POS UINT8_C(0x00)
  147. #define BME280_CTRL_HUM_MSK UINT8_C(0x07)
  148. #define BME280_CTRL_HUM_POS UINT8_C(0x00)
  149. #define BME280_CTRL_PRESS_MSK UINT8_C(0x1C)
  150. #define BME280_CTRL_PRESS_POS UINT8_C(0x02)
  151. #define BME280_CTRL_TEMP_MSK UINT8_C(0xE0)
  152. #define BME280_CTRL_TEMP_POS UINT8_C(0x05)
  153. #define BME280_FILTER_MSK UINT8_C(0x1C)
  154. #define BME280_FILTER_POS UINT8_C(0x02)
  155. #define BME280_STANDBY_MSK UINT8_C(0xE0)
  156. #define BME280_STANDBY_POS UINT8_C(0x05)
  157. /**\name Sensor component selection macros
  158. * These values are internal for API implementation. Don't relate this to
  159. * data sheet.
  160. */
  161. #define BME280_PRESS UINT8_C(1)
  162. #define BME280_TEMP UINT8_C(1 << 1)
  163. #define BME280_HUM UINT8_C(1 << 2)
  164. #define BME280_ALL UINT8_C(0x07)
  165. /**\name Settings selection macros */
  166. #define BME280_OSR_PRESS_SEL UINT8_C(1)
  167. #define BME280_OSR_TEMP_SEL UINT8_C(1 << 1)
  168. #define BME280_OSR_HUM_SEL UINT8_C(1 << 2)
  169. #define BME280_FILTER_SEL UINT8_C(1 << 3)
  170. #define BME280_STANDBY_SEL UINT8_C(1 << 4)
  171. #define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F)
  172. /**\name Oversampling macros */
  173. #define BME280_NO_OVERSAMPLING UINT8_C(0x00)
  174. #define BME280_OVERSAMPLING_1X UINT8_C(0x01)
  175. #define BME280_OVERSAMPLING_2X UINT8_C(0x02)
  176. #define BME280_OVERSAMPLING_4X UINT8_C(0x03)
  177. #define BME280_OVERSAMPLING_8X UINT8_C(0x04)
  178. #define BME280_OVERSAMPLING_16X UINT8_C(0x05)
  179. /**\name Measurement delay calculation macros */
  180. #define BME280_MEAS_OFFSET UINT16_C(1250)
  181. #define BME280_MEAS_DUR UINT16_C(2300)
  182. #define BME280_PRES_HUM_MEAS_OFFSET UINT16_C(575)
  183. #define BME280_MEAS_SCALING_FACTOR UINT16_C(1000)
  184. /**\name Standby duration selection macros */
  185. #define BME280_STANDBY_TIME_0_5_MS (0x00)
  186. #define BME280_STANDBY_TIME_62_5_MS (0x01)
  187. #define BME280_STANDBY_TIME_125_MS (0x02)
  188. #define BME280_STANDBY_TIME_250_MS (0x03)
  189. #define BME280_STANDBY_TIME_500_MS (0x04)
  190. #define BME280_STANDBY_TIME_1000_MS (0x05)
  191. #define BME280_STANDBY_TIME_10_MS (0x06)
  192. #define BME280_STANDBY_TIME_20_MS (0x07)
  193. /**\name Filter coefficient selection macros */
  194. #define BME280_FILTER_COEFF_OFF (0x00)
  195. #define BME280_FILTER_COEFF_2 (0x01)
  196. #define BME280_FILTER_COEFF_4 (0x02)
  197. #define BME280_FILTER_COEFF_8 (0x03)
  198. #define BME280_FILTER_COEFF_16 (0x04)
  199. #define BME280_STATUS_REG_ADDR (0xF3)
  200. #define BME280_SOFT_RESET_COMMAND (0xB6)
  201. #define BME280_STATUS_IM_UPDATE (0x01)
  202. /*!
  203. * @brief Interface selection Enums
  204. */
  205. enum bme280_intf {
  206. /*! SPI interface */
  207. BME280_SPI_INTF,
  208. /*! I2C interface */
  209. BME280_I2C_INTF
  210. };
  211. /*!
  212. * @brief Type definitions
  213. */
  214. typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len);
  215. typedef void (*bme280_delay_fptr_t)(uint32_t period);
  216. /*!
  217. * @brief Calibration data
  218. */
  219. struct bme280_calib_data
  220. {
  221. /**
  222. * @ Trim Variables
  223. */
  224. /**@{*/
  225. uint16_t dig_t1;
  226. int16_t dig_t2;
  227. int16_t dig_t3;
  228. uint16_t dig_p1;
  229. int16_t dig_p2;
  230. int16_t dig_p3;
  231. int16_t dig_p4;
  232. int16_t dig_p5;
  233. int16_t dig_p6;
  234. int16_t dig_p7;
  235. int16_t dig_p8;
  236. int16_t dig_p9;
  237. uint8_t dig_h1;
  238. int16_t dig_h2;
  239. uint8_t dig_h3;
  240. int16_t dig_h4;
  241. int16_t dig_h5;
  242. int8_t dig_h6;
  243. int32_t t_fine;
  244. /**@}*/
  245. };
  246. /*!
  247. * @brief bme280 sensor structure which comprises of temperature, pressure and
  248. * humidity data
  249. */
  250. #ifdef BME280_FLOAT_ENABLE
  251. struct bme280_data
  252. {
  253. /*! Compensated pressure */
  254. double pressure;
  255. /*! Compensated temperature */
  256. double temperature;
  257. /*! Compensated humidity */
  258. double humidity;
  259. };
  260. #else
  261. struct bme280_data
  262. {
  263. /*! Compensated pressure */
  264. uint32_t pressure;
  265. /*! Compensated temperature */
  266. int32_t temperature;
  267. /*! Compensated humidity */
  268. uint32_t humidity;
  269. };
  270. #endif /* BME280_USE_FLOATING_POINT */
  271. /*!
  272. * @brief bme280 sensor structure which comprises of uncompensated temperature,
  273. * pressure and humidity data
  274. */
  275. struct bme280_uncomp_data
  276. {
  277. /*! un-compensated pressure */
  278. uint32_t pressure;
  279. /*! un-compensated temperature */
  280. uint32_t temperature;
  281. /*! un-compensated humidity */
  282. uint32_t humidity;
  283. };
  284. /*!
  285. * @brief bme280 sensor settings structure which comprises of mode,
  286. * oversampling and filter settings.
  287. */
  288. struct bme280_settings
  289. {
  290. /*! pressure oversampling */
  291. uint8_t osr_p;
  292. /*! temperature oversampling */
  293. uint8_t osr_t;
  294. /*! humidity oversampling */
  295. uint8_t osr_h;
  296. /*! filter coefficient */
  297. uint8_t filter;
  298. /*! standby time */
  299. uint8_t standby_time;
  300. };
  301. /*!
  302. * @brief bme280 device structure
  303. */
  304. struct bme280_dev
  305. {
  306. /*! Chip Id */
  307. uint8_t chip_id;
  308. /*! Device Id */
  309. uint8_t dev_id;
  310. /*! SPI/I2C interface */
  311. enum bme280_intf intf;
  312. /*! Read function pointer */
  313. bme280_com_fptr_t read;
  314. /*! Write function pointer */
  315. bme280_com_fptr_t write;
  316. /*! Delay function pointer */
  317. bme280_delay_fptr_t delay_ms;
  318. /*! Trim data */
  319. struct bme280_calib_data calib_data;
  320. /*! Sensor settings */
  321. struct bme280_settings settings;
  322. };
  323. #endif /* BME280_DEFS_H_ */
  324. /** @}*/
  325. /** @}*/