sht3x.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef __SHT3X_H__
  2. #define __SHT3X_H__
  3. #include <rthw.h>
  4. #include <rtthread.h>
  5. #include <rthw.h>
  6. #include <rtdevice.h>
  7. #define SHT3X_REPETABILITY_LOW 0
  8. #define SHT3X_REPETABILITY_MEDIUM 1
  9. #define SHT3X_REPETABILITY_HIGH 2
  10. /* sht3x commands define */
  11. // read serial number:
  12. #define CMD_READ_SERIALNBR 0x3780
  13. // read status register:
  14. #define CMD_READ_STATUS 0xF32D
  15. // clear status register:
  16. #define CMD_CLEAR_STATUS 0x3041
  17. // enabled heater:
  18. #define CMD_HEATER_ENABLE 0x306D
  19. // disable heater
  20. #define CMD_HEATER_DISABLE 0x3066
  21. // soft reset
  22. #define CMD_SOFT_RESET 0x30A2
  23. // accelerated response time
  24. #define CMD_ART 0x2B32
  25. // break, stop periodic data acquisition mode
  26. #define CMD_BREAK 0x3093
  27. // measurement: polling, high repeatability
  28. #define CMD_MEAS_POLLING_H 0x2400
  29. // measurement: polling, medium repeatability
  30. #define CMD_MEAS_POLLING_M 0x240B
  31. // measurement: polling, low repeatability
  32. #define CMD_MEAS_POLLING_L 0x2416
  33. #define SHT3X_ADDR_PD 0x44 // addr pin pulled down: 0x44
  34. #define SHT3X_ADDR_PU 0x45 // addr pin pulled down: 0x45
  35. typedef union sht3x_status_word
  36. {
  37. rt_uint16_t status_word;
  38. struct status_bits
  39. {
  40. // rt_uint16_t alert_pending: 1 ;
  41. // rt_uint16_t reserved_0 : 1 ;
  42. // rt_uint16_t heater : 1 ;
  43. // rt_uint16_t reserved_1 : 1 ;
  44. // rt_uint16_t RH_tracking_alert : 1 ;
  45. // rt_uint16_t T_tracking_alert : 1 ;
  46. // rt_uint16_t reserved_2 : 5 ;
  47. // rt_uint16_t reset_detected : 1 ;
  48. // rt_uint16_t reserved_3 : 2 ;
  49. // rt_uint16_t command_ok : 1 ;
  50. // rt_uint16_t checksum_ok : 1 ;
  51. rt_uint16_t checksum_ok : 1 ;
  52. rt_uint16_t command_ok : 1 ;
  53. rt_uint16_t reserved_3 : 2 ;
  54. rt_uint16_t reset_detected : 1 ;
  55. rt_uint16_t reserved_2 : 5 ;
  56. rt_uint16_t T_tracking_alert : 1 ;
  57. rt_uint16_t RH_tracking_alert : 1 ;
  58. rt_uint16_t reserved_1 : 1 ;
  59. rt_uint16_t heater : 1 ;
  60. rt_uint16_t reserved_0 : 1 ;
  61. rt_uint16_t alert_pending: 1 ;
  62. } bits;
  63. }sht3x_status;
  64. struct sht3x_device
  65. {
  66. struct rt_i2c_bus_device *i2c;
  67. rt_uint8_t sht3x_addr ;
  68. rt_mutex_t lock;
  69. float temperature ;
  70. float humidity ;
  71. /* the command for data readout: change it as you need */
  72. rt_uint16_t cmd_readout;
  73. sht3x_status status ;
  74. };
  75. typedef struct sht3x_device *sht3x_device_t;
  76. /**
  77. * This function resets all parameter with default
  78. *
  79. * @param dev the pointer of device driver structure
  80. *
  81. * @return the softreset status,RT_EOK reprensents setting successfully.
  82. */
  83. rt_err_t sht3x_softreset(sht3x_device_t dev);
  84. /**
  85. * This function initializes sht3x registered device driver
  86. *
  87. * @param dev the name of sht3x device
  88. *
  89. * @return the sht3x device.
  90. */
  91. sht3x_device_t sht3x_init(const char *i2c_bus_name, rt_uint8_t sht3x_addr);
  92. /**
  93. * This function releases memory and deletes mutex lock
  94. *
  95. * @param dev the pointer of device driver structure
  96. */
  97. void sht3x_deinit(sht3x_device_t dev);
  98. /**
  99. * This function reads temperature and humidity by sht3x sensor measurement using single shot mode
  100. *
  101. * @param dev the pointer of device driver structure
  102. *
  103. * @return the relative temperature converted to float data.
  104. */
  105. rt_err_t sht3x_read_singleshot(sht3x_device_t dev);
  106. #endif /* _SHT30_H__ */