sht3x.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 write 2-byte cmd to SHT3x
  66. *
  67. * @param dev the pointer of device driver structure
  68. * @param cmd the 16bit command to be sent to SHT3x
  69. *
  70. * @return the cmd transfer status, RT_EOK reprensents setting successfully.
  71. */
  72. static rt_err_t write_cmd(sht3x_device_t dev, rt_uint16_t cmd);
  73. /**
  74. * This function read len bytes from dev and store the bytes in to buf
  75. *
  76. * @param dev the pointer of device driver structure
  77. * @param buf the pointer to buffer array
  78. * @param len the number of bytes to be read
  79. *
  80. * @return the i2c read status, RT_EOK represents success.
  81. */
  82. static rt_err_t read_bytes(sht3x_device_t dev, rt_uint8_t * buf, rt_uint8_t len);
  83. /**
  84. * This function read temperature and humidity by single shot mode
  85. * Attention:
  86. * - rt_thread_mdelay() is called to wait for SHT3x to be ready to read
  87. * - the temperature and humidity is stored in the device driver structure
  88. *
  89. * @param dev the pointer of device driver structure
  90. *
  91. * @return the status of read data from SHT3x, RT_EOK means success.
  92. */
  93. rt_err_t sht3x_read_singleshot(sht3x_device_t dev);
  94. /**
  95. * This function resets all parameter with default
  96. *
  97. * @param dev the pointer of device driver structure
  98. *
  99. * @return the softreset status,RT_EOK reprensents setting successfully.
  100. */
  101. rt_err_t sht3x_softreset(sht3x_device_t dev);
  102. /**
  103. * This function clear the status register in SHT3x
  104. *
  105. * @param dev the pointer of device driver structure
  106. *
  107. * @return the command transfer status, RT_EOK means success.
  108. */
  109. rt_err_t sht3x_clear_status(sht3x_device_t dev);
  110. /**
  111. * This function read the status register from SHT3x
  112. * Attention:
  113. * - the status word is stored in device driver structure
  114. *
  115. * @param dev the pointer of device driver structure
  116. *
  117. * @return the command transfer status, RT_EOK means success.
  118. */
  119. rt_err_t sht3x_read_status(sht3x_device_t dev);
  120. /**
  121. * This function enable heater
  122. * @param dev the pointer of device driver structure
  123. *
  124. * @return the command transfer status, RT_EOK means success.
  125. */
  126. rt_err_t sht3x_enable_heater(sht3x_device_t dev);
  127. /**
  128. * This function disable heater
  129. * @param dev the pointer of device driver structure
  130. *
  131. * @return the command transfer status, RT_EOK means success.
  132. */
  133. rt_err_t sht3x_disable_heater(sht3x_device_t dev);
  134. /**
  135. * This function write accelerated response time command to SHT3x
  136. * @param dev the pointer of device driver structure
  137. *
  138. * @return the command transfer status, RT_EOK means success.
  139. */
  140. rt_err_t sht3x_acc_resp_time(sht3x_device_t dev);
  141. /**
  142. * This function write break command to SHT3x to break out of continuous readout mode
  143. * @param dev the pointer of device driver structure
  144. *
  145. * @return the command transfer status, RT_EOK means success.
  146. */
  147. rt_err_t sht3x_break(sht3x_device_t dev);
  148. /**
  149. * This function initializes sht3x registered device driver
  150. *
  151. * @param dev the name of sht3x device
  152. *
  153. * @return the sht3x device.
  154. */
  155. sht3x_device_t sht3x_init(const char *i2c_bus_name, rt_uint8_t sht3x_addr);
  156. /**
  157. * This function releases memory and deletes mutex lock
  158. *
  159. * @param dev the pointer of device driver structure
  160. */
  161. void sht3x_deinit(sht3x_device_t dev);
  162. #endif /* _SHT30_H__ */