sht3x.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef __SHT3X_H__
  2. #define __SHT3X_H__
  3. #include <rthw.h>
  4. #include <rtthread.h>
  5. #include <rtdevice.h>
  6. #define SHT3X_REPETABILITY_LOW 0
  7. #define SHT3X_REPETABILITY_MEDIUM 1
  8. #define SHT3X_REPETABILITY_HIGH 2
  9. /* sht3x commands define */
  10. // read serial number:
  11. #define CMD_READ_SERIALNBR 0x3780
  12. // read status register:
  13. #define CMD_READ_STATUS 0xF32D
  14. // clear status register:
  15. #define CMD_CLEAR_STATUS 0x3041
  16. // enabled heater:
  17. #define CMD_HEATER_ENABLE 0x306D
  18. // disable heater
  19. #define CMD_HEATER_DISABLE 0x3066
  20. // soft reset
  21. #define CMD_SOFT_RESET 0x30A2
  22. // accelerated response time
  23. #define CMD_ART 0x2B32
  24. // break, stop periodic data acquisition mode
  25. #define CMD_BREAK 0x3093
  26. // measurement: polling, high repeatability
  27. #define CMD_MEAS_POLLING_H 0x2400
  28. // measurement: polling, medium repeatability
  29. #define CMD_MEAS_POLLING_M 0x240B
  30. // measurement: polling, low repeatability
  31. #define CMD_MEAS_POLLING_L 0x2416
  32. #define SHT3X_ADDR_PD 0x44 // addr pin pulled down: 0x44
  33. #define SHT3X_ADDR_PU 0x45 // addr pin pulled down: 0x45
  34. typedef union sht3x_status_word
  35. {
  36. rt_uint16_t status_word;
  37. struct status_bits
  38. {
  39. rt_uint16_t checksum_ok : 1 ;
  40. rt_uint16_t command_ok : 1 ;
  41. rt_uint16_t reserved_3 : 2 ;
  42. rt_uint16_t reset_detected : 1 ;
  43. rt_uint16_t reserved_2 : 5 ;
  44. rt_uint16_t T_tracking_alert : 1 ;
  45. rt_uint16_t RH_tracking_alert : 1 ;
  46. rt_uint16_t reserved_1 : 1 ;
  47. rt_uint16_t heater : 1 ;
  48. rt_uint16_t reserved_0 : 1 ;
  49. rt_uint16_t alert_pending: 1 ;
  50. } bits;
  51. }sht3x_status;
  52. struct sht3x_device
  53. {
  54. struct rt_i2c_bus_device *i2c;
  55. rt_uint8_t sht3x_addr ;
  56. rt_mutex_t lock;
  57. float temperature ;
  58. float humidity ;
  59. /* the command for data readout: change it as you need */
  60. rt_uint16_t cmd_readout;
  61. sht3x_status status ;
  62. };
  63. typedef struct sht3x_device *sht3x_device_t;
  64. /**
  65. * This function read temperature and humidity by single shot mode
  66. * Attention:
  67. * - rt_thread_mdelay() is called to wait for SHT3x to be ready to read
  68. * - the temperature and humidity is stored in the device driver structure
  69. *
  70. * @param dev the pointer of device driver structure
  71. *
  72. * @return the status of read data from SHT3x, RT_EOK means success.
  73. */
  74. rt_err_t sht3x_read_singleshot(sht3x_device_t dev);
  75. /**
  76. * This function resets all parameter with default
  77. *
  78. * @param dev the pointer of device driver structure
  79. *
  80. * @return the softreset status,RT_EOK reprensents setting successfully.
  81. */
  82. rt_err_t sht3x_softreset(sht3x_device_t dev);
  83. /**
  84. * This function clear the status register in SHT3x
  85. *
  86. * @param dev the pointer of device driver structure
  87. *
  88. * @return the command transfer status, RT_EOK means success.
  89. */
  90. rt_err_t sht3x_clear_status(sht3x_device_t dev);
  91. /**
  92. * This function read the status register from SHT3x
  93. * Attention:
  94. * - the status word is stored in device driver structure
  95. *
  96. * @param dev the pointer of device driver structure
  97. *
  98. * @return the command transfer status, RT_EOK means success.
  99. */
  100. rt_err_t sht3x_read_status(sht3x_device_t dev);
  101. /**
  102. * This function enable heater
  103. * @param dev the pointer of device driver structure
  104. *
  105. * @return the command transfer status, RT_EOK means success.
  106. */
  107. rt_err_t sht3x_enable_heater(sht3x_device_t dev);
  108. /**
  109. * This function disable heater
  110. * @param dev the pointer of device driver structure
  111. *
  112. * @return the command transfer status, RT_EOK means success.
  113. */
  114. rt_err_t sht3x_disable_heater(sht3x_device_t dev);
  115. /**
  116. * This function write accelerated response time command to SHT3x
  117. * @param dev the pointer of device driver structure
  118. *
  119. * @return the command transfer status, RT_EOK means success.
  120. */
  121. rt_err_t sht3x_acc_resp_time(sht3x_device_t dev);
  122. /**
  123. * This function write break command to SHT3x to break out of continuous readout mode
  124. * @param dev the pointer of device driver structure
  125. *
  126. * @return the command transfer status, RT_EOK means success.
  127. */
  128. rt_err_t sht3x_break(sht3x_device_t dev);
  129. /**
  130. * This function initializes sht3x registered device driver
  131. *
  132. * @param dev the name of sht3x device
  133. *
  134. * @return the sht3x device.
  135. */
  136. sht3x_device_t sht3x_init(const char *i2c_bus_name, rt_uint8_t sht3x_addr);
  137. /**
  138. * This function releases memory and deletes mutex lock
  139. *
  140. * @param dev the pointer of device driver structure
  141. */
  142. void sht3x_deinit(sht3x_device_t dev);
  143. #endif /* _SHT30_H__ */