hdc1000_reg.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define DBG_TAG "hdc1000"
  6. #define DBG_LVL DBG_INFO
  7. #include <rtdbg.h>
  8. #include "hdc1000_reg.h"
  9. #define HDC1000_DELAY_MS(x) rt_thread_mdelay(x)
  10. /**
  11. * @brief This function config the value of the register for hdc1000
  12. *
  13. * @param dev the pointer of device driver structure
  14. * @param reg the register for hdc1000
  15. * @param data value to write
  16. *
  17. * @return the writing status, RT_EOK reprensents writing the value of the register successfully.
  18. */
  19. static rt_err_t hdc1000_write_reg(struct hdc1000_device *dev,rt_uint8_t reg, rt_uint16_t data)
  20. {
  21. rt_int8_t res = 0;
  22. struct rt_i2c_msg msgs;
  23. rt_uint8_t buf[3] = {0};
  24. buf[ 0 ] = reg;
  25. // MSB
  26. buf[ 1 ] = ( data >> 8 ) & 0xFF;
  27. buf[ 2 ] = data & 0xFF;
  28. if (dev->bus->type == RT_Device_Class_I2CBUS)
  29. {
  30. msgs.addr = dev->i2c_addr; /* slave address */
  31. msgs.flags = RT_I2C_WR; /* write flag */
  32. msgs.buf = buf; /* Send data pointer */
  33. msgs.len = 3;
  34. if (rt_i2c_transfer((struct rt_i2c_bus_device *)dev->bus, &msgs, 1) == 1)
  35. {
  36. res = RT_EOK;
  37. }
  38. else
  39. {
  40. res = -RT_ERROR;
  41. }
  42. }
  43. return res;
  44. }
  45. /**
  46. * @brief This function read the value of register for hdc1000
  47. *
  48. * @param dev the pointer of device driver structure
  49. * @param reg the register for hdc1000
  50. * @param buf read data pointer
  51. *
  52. * @return the reading status, RT_EOK reprensents reading the value of registers successfully.
  53. */
  54. static rt_err_t hdc1000_read_reg(struct hdc1000_device *dev, rt_uint8_t reg, rt_uint16_t *data)
  55. {
  56. rt_int8_t res = 0;
  57. struct rt_i2c_msg msgs[2];
  58. rt_uint8_t buffer[2] = {0};
  59. if (dev->bus->type == RT_Device_Class_I2CBUS)
  60. {
  61. msgs[0].addr = dev->i2c_addr; /* Slave address */
  62. msgs[0].flags = RT_I2C_WR; /* Write flag */
  63. msgs[0].buf = &reg; /* Slave register address */
  64. msgs[0].len = 1; /* Number of bytes sent */
  65. msgs[1].addr = dev->i2c_addr; /* Slave address */
  66. msgs[1].flags = RT_I2C_RD; /* Read flag */
  67. msgs[1].buf = buffer; /* Read data pointer */
  68. msgs[1].len = 2; /* Number of bytes read */
  69. if (rt_i2c_transfer((struct rt_i2c_bus_device *)dev->bus, msgs, 2) == 2)
  70. {
  71. res = RT_EOK;
  72. *data = (buffer[0] << 8) | buffer[1];
  73. }
  74. else
  75. {
  76. res = -RT_ERROR;
  77. }
  78. }
  79. return res;
  80. }
  81. /**
  82. * @brief This function reset the hdc1000 device.
  83. *
  84. * @param dev_name the name of transfer device
  85. * @param param the i2c device address for i2c communication, RT_NULL for spi
  86. *
  87. * @return the pointer of device driver structure, RT_NULL reprensents initialization failed.
  88. */
  89. static rt_err_t hdc1000_soft_reset(struct hdc1000_device *dev)
  90. {
  91. return ( hdc1000_write_reg(dev, HDC1000_CONFIGURATION_REG,HDC1000_SOFT_RESET) );
  92. }
  93. /**
  94. * @brief Get the HTS221 temperature value
  95. *
  96. * @param pObj the device pObj
  97. * @param Value pointer where the temperature value is written
  98. *
  99. * @retval 0 in case of success, an error code otherwise
  100. */
  101. static rt_err_t hdc1000_setup_mode(struct hdc1000_device *dev,uint16_t mode)
  102. {
  103. return ( hdc1000_write_reg(dev,HDC1000_CONFIGURATION_REG, mode) );
  104. }
  105. /**
  106. * @brief Get the HDC1000 humidity value
  107. *
  108. * @param dev the pointer of device driver structure
  109. * @param raw pointer where the temperature and humidity value is written
  110. *
  111. * @retval RT_EOK in case of success, an error code otherwise
  112. */
  113. rt_err_t hdc1000_get_temperature_and_humidity(struct hdc1000_device *dev,uint16_t *temp_raw,uint16_t *humi_raw)
  114. {
  115. rt_int8_t res = 0;
  116. struct rt_i2c_msg msgs[2];
  117. rt_uint8_t buffer[4] = { HDC1000_TEMPERATURE_REG };// 0x00 or HDC1000_HUMIDITY_REG(01)
  118. /* Trigger a measurement (executing a pointer write transaction with the address point 0x00 or 0x01) */
  119. {
  120. msgs[0].addr = dev->i2c_addr; /* Slave address */
  121. msgs[0].flags = RT_I2C_WR; /* Write flag */
  122. msgs[0].buf = buffer; /* Slave register address,buffer[0] is the address point*/
  123. msgs[0].len = 1; /* Number of bytes sent */
  124. if (rt_i2c_transfer((struct rt_i2c_bus_device *)dev->bus, msgs, 1) != 1)
  125. {
  126. res = -RT_ERROR;
  127. }
  128. }
  129. /* wait for the measurements to complete or wait for the assertion of DRDYn
  130. - Temperture sensor conversion time ~6.35ms @ 14 bit resolution
  131. - Humidity sensor conversion time ~6.5ms @ 14 bit resolution
  132. */
  133. HDC1000_DELAY_MS(15);
  134. /* read the temperture and humidity raw value */
  135. {
  136. //msgs[0].addr = dev->i2c_addr; /* Slave address */
  137. msgs[0].flags = RT_I2C_RD; /* Read flag */
  138. msgs[0].buf = buffer; /* Read data pointer */
  139. msgs[0].len = 4; /* Number of bytes read */
  140. if (rt_i2c_transfer((struct rt_i2c_bus_device *)dev->bus, msgs, 1) == 1)
  141. {
  142. res = RT_EOK;
  143. *temp_raw = (buffer[0] << 8) | buffer[1];
  144. *humi_raw = (buffer[2] << 8) | buffer[3];
  145. }
  146. else
  147. {
  148. res = -RT_ERROR;
  149. }
  150. }
  151. return res;
  152. }
  153. /**
  154. * @brief read the HDC1000 configuration register
  155. *
  156. * @param dev the pointer of device driver structure
  157. * @param raw pointer where the configuration register is written
  158. *
  159. * @retval RT_EOK in case of success, an error code otherwise
  160. */
  161. rt_err_t hdc1000_read_configuration(struct hdc1000_device *dev,rt_uint16_t *config)
  162. {
  163. return ( hdc1000_read_reg(dev,HDC1000_CONFIGURATION_REG, config) );
  164. }
  165. /**
  166. * @brief Get the HDC1000 manufacturer id
  167. *
  168. * @param dev the pointer of device driver structure
  169. * @param raw pointer where the manufacturer id is written
  170. *
  171. * @retval RT_EOK in case of success, an error code otherwise
  172. */
  173. rt_err_t hdc1000_read_manufacturer_id(struct hdc1000_device *dev,rt_uint16_t *manufacturer_id)
  174. {
  175. return( hdc1000_read_reg(dev, HDC1000_MANUFACTURER_ID_REG, manufacturer_id) );
  176. }
  177. /**
  178. * @brief Get the HDC1000 device id
  179. *
  180. * @param dev the pointer of device driver structure
  181. * @param raw pointer where the device id is written
  182. *
  183. * @retval RT_EOK in case of success, an error code otherwise
  184. */
  185. rt_err_t hdc1000_read_device_id(struct hdc1000_device *dev,rt_uint16_t *dev_id)
  186. {
  187. return ( hdc1000_read_reg(dev, HDC1000_DEVICE_ID_REG, dev_id) );
  188. }
  189. /**
  190. * @brief This function initialize the hdc1000 device.
  191. *
  192. * @param dev_name the name of transfer device
  193. * @param i2c_addr the i2c device address for i2c communication
  194. * @param config hdc1000 configuaration
  195. *
  196. * @return the pointer of device driver structure, RT_NULL reprensents initialization failed.
  197. */
  198. struct hdc1000_device *hdc1000_init(const char *dev_name, rt_uint8_t i2c_addr)
  199. {
  200. struct hdc1000_device *dev = RT_NULL;
  201. rt_uint16_t device_id = 0xFFFF, res = RT_EOK;
  202. RT_ASSERT(dev_name);
  203. dev = rt_calloc(1, sizeof(struct hdc1000_device));
  204. if (dev == RT_NULL)
  205. {
  206. LOG_E("Can't allocate memory for hdc1000 device on '%s' ", dev_name);
  207. goto __exit;
  208. }
  209. dev->bus = rt_device_find(dev_name);
  210. if (dev->bus == RT_NULL)
  211. {
  212. LOG_E("Can't find device:'%s'", dev_name);
  213. goto __exit;
  214. }
  215. if (dev->bus->type == RT_Device_Class_I2CBUS)
  216. {
  217. if (i2c_addr != RT_NULL)
  218. {
  219. dev->i2c_addr = i2c_addr;
  220. }
  221. else
  222. {
  223. /* find hdc1000 device at address */
  224. dev->i2c_addr = HDC1000_DEVICE_IIC_BUS_ADDRESS;
  225. if (hdc1000_read_device_id(dev, &device_id) != RT_EOK)
  226. {
  227. LOG_E("Can't find device at '%s'!", dev_name);
  228. LOG_E("Failed to read device id!");
  229. goto __exit;
  230. }
  231. LOG_D("Device i2c address is:'0x%x'!", dev->i2c_addr);
  232. }
  233. }
  234. else
  235. {
  236. LOG_E("Unsupported device:'%s'!", dev_name);
  237. goto __exit;
  238. }
  239. res += hdc1000_soft_reset(dev);
  240. HDC1000_DELAY_MS(15); //15ms
  241. res += hdc1000_setup_mode(dev,HDC1000_ACQUISITION_MODE_IN_SEQUENCE);
  242. res += hdc1000_read_configuration(dev, &dev->config.configuration);
  243. if (res == RT_EOK)
  244. {
  245. LOG_I("Device init succeed!");
  246. }
  247. else
  248. {
  249. LOG_W("Error in device initialization!");
  250. goto __exit;
  251. }
  252. return dev;
  253. __exit:
  254. if (dev != RT_NULL)
  255. {
  256. rt_free(dev);
  257. }
  258. return RT_NULL;
  259. }
  260. /**
  261. * This function releases memory
  262. *
  263. * @param dev the pointer of device driver structure
  264. */
  265. void hdc1000_deinit(struct hdc1000_device *dev)
  266. {
  267. RT_ASSERT(dev);
  268. rt_free(dev);
  269. }
  270. //----------------------------------------------------------------------------------