sensor_ti_hdc1000.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "sensor_ti_hdc1000.h"
  2. #include "string.h"
  3. #define DBG_ENABLE
  4. #define DBG_LEVEL DBG_LOG
  5. #define DBG_SECTION_NAME "sensor.ti.hdc1000"
  6. #define DBG_COLOR
  7. #include <rtdbg.h>
  8. static int hdc1000_selftest(void);
  9. /*********** Common *****************/
  10. static struct hdc1000_device *hdc1000_dev;
  11. static rt_err_t _hdc1000_init(struct rt_sensor_intf *intf)
  12. {
  13. rt_uint8_t i2c_addr = (rt_uint32_t)(intf->user_data) & 0xff;
  14. hdc1000_dev = hdc1000_init(intf->dev_name, i2c_addr);
  15. if (hdc1000_dev == RT_NULL)
  16. {
  17. return -RT_ERROR;
  18. }
  19. return RT_EOK;
  20. }
  21. static RT_SIZE_TYPE hdc1000_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
  22. {
  23. struct rt_sensor_data *data = (struct rt_sensor_data *)buf;
  24. rt_uint16_t temp_value, humi_value;
  25. hdc1000_get_temperature_and_humidity(hdc1000_dev, &temp_value, &humi_value);
  26. if (sensor->info.type == RT_SENSOR_CLASS_TEMP)
  27. {
  28. data->type = RT_SENSOR_CLASS_TEMP;
  29. data->data.temp = (( float )( temp_value * HDC1000_TEMP_CONST_14BIT ) - 40 ) * 10; // x10
  30. data->timestamp = rt_sensor_get_ts();
  31. }
  32. else if (sensor->info.type == RT_SENSOR_CLASS_HUMI)
  33. {
  34. data->type = RT_SENSOR_CLASS_HUMI;
  35. data->data.humi = (( float )humi_value * HDC1000_HUMI_CONST_14BIT ) * 10; // x10
  36. data->timestamp = rt_sensor_get_ts();
  37. }
  38. return 1;
  39. }
  40. static rt_err_t hdc1000_control(struct rt_sensor_device *sensor, int cmd, void *args)
  41. {
  42. rt_err_t result = RT_EOK;
  43. switch (cmd)
  44. {
  45. case RT_SENSOR_CTRL_GET_ID:
  46. hdc1000_read_device_id(hdc1000_dev, args);
  47. break;
  48. case RT_SENSOR_CTRL_SET_RANGE:
  49. return -RT_ERROR;
  50. case RT_SENSOR_CTRL_SELF_TEST:
  51. hdc1000_selftest();
  52. break;
  53. default:
  54. return -RT_ERROR;
  55. }
  56. return result;
  57. }
  58. static struct rt_sensor_ops sensor_ops =
  59. {
  60. hdc1000_fetch_data,
  61. hdc1000_control
  62. };
  63. int rt_hw_hdc1000_init(const char *name, struct rt_sensor_config *cfg)
  64. {
  65. rt_int8_t result;
  66. rt_sensor_t sensor_temp = RT_NULL, sensor_humi = RT_NULL;
  67. /* Termperture sensor register */
  68. sensor_temp = rt_calloc(1, sizeof(struct rt_sensor_device));
  69. if (sensor_temp == RT_NULL)
  70. return -1;
  71. sensor_temp->info.type = RT_SENSOR_CLASS_TEMP;
  72. sensor_temp->info.vendor = RT_SENSOR_VENDOR_STM;
  73. sensor_temp->info.model = "hdc1000_temp";
  74. sensor_temp->info.unit = RT_SENSOR_UNIT_DCELSIUS;
  75. sensor_temp->info.intf_type = RT_SENSOR_INTF_I2C;
  76. sensor_temp->info.range_max = 125;
  77. sensor_temp->info.range_min = -40;
  78. sensor_temp->info.period_min = 80;
  79. rt_memcpy(&sensor_temp->config, cfg, sizeof(struct rt_sensor_config));
  80. sensor_temp->ops = &sensor_ops;
  81. result = rt_hw_sensor_register(sensor_temp, name, RT_DEVICE_FLAG_RDWR, RT_NULL);
  82. if (result != RT_EOK)
  83. {
  84. LOG_E("temp sensor register err code: %d", result);
  85. goto __exit;
  86. }
  87. LOG_I("temp sensor init success");
  88. /* humidity sensor register */
  89. sensor_humi = rt_calloc(1, sizeof(struct rt_sensor_device));
  90. if (sensor_humi == RT_NULL)
  91. return -1;
  92. sensor_humi->info.type = RT_SENSOR_CLASS_HUMI;
  93. sensor_humi->info.vendor = RT_SENSOR_VENDOR_STM;
  94. sensor_humi->info.model = "hdc1000_humi";
  95. sensor_humi->info.unit = RT_SENSOR_UNIT_PERMILLAGE;
  96. sensor_humi->info.intf_type = RT_SENSOR_INTF_I2C;
  97. sensor_humi->info.range_max = 100;
  98. sensor_humi->info.range_min = 0;
  99. sensor_humi->info.period_min = 80;
  100. rt_memcpy(&sensor_humi->config, cfg, sizeof(struct rt_sensor_config));
  101. sensor_humi->ops = &sensor_ops;
  102. result = rt_hw_sensor_register(sensor_humi, name, RT_DEVICE_FLAG_RDWR, RT_NULL);
  103. if (result != RT_EOK)
  104. {
  105. LOG_E("humi sensor register err code: %d", result);
  106. goto __exit;
  107. }
  108. LOG_I("humi sensor init success");
  109. /* hdc1000 init */
  110. result = _hdc1000_init(&cfg->intf);
  111. if (result != RT_EOK)
  112. {
  113. LOG_E("hdc1000 init err code: %d", result);
  114. goto __exit;
  115. }
  116. LOG_I("hdc1000 init success");
  117. return result;
  118. __exit:
  119. if (sensor_temp)
  120. rt_free(sensor_temp);
  121. if (sensor_humi)
  122. rt_free(sensor_humi);
  123. if (hdc1000_dev)
  124. hdc1000_deinit(hdc1000_dev);
  125. return -RT_ERROR;
  126. }
  127. /* SelfTest function */
  128. static int hdc1000_selftest(void)
  129. {
  130. struct hdc1000_device *dev;
  131. rt_uint16_t device_id,manufacturer_id,temp_value,humi_value;
  132. int i;
  133. /* Initialize hdc1000, The parameter is RT_NULL, means auto probing for i2c*/
  134. dev = hdc1000_init("i2c1",RT_NULL);
  135. if (dev == RT_NULL)
  136. {
  137. rt_kprintf("hdc1000 init failed\n");
  138. return -1;
  139. }
  140. rt_kprintf("hdc1000 init succeed\n");
  141. hdc1000_read_device_id(hdc1000_dev, &device_id);
  142. hdc1000_read_manufacturer_id(hdc1000_dev, &manufacturer_id);
  143. for (i = 0; i < 5; i++)
  144. {
  145. hdc1000_get_temperature_and_humidity(hdc1000_dev, &temp_value, &humi_value);
  146. temp_value = (int16_t)(((float)temp_value/65536*165-40)); // C
  147. humi_value = (int16_t)(((float)humi_value/65536)*100); // %RH
  148. rt_kprintf("device_id = %X, manufacturer_id = %X, temp = %d C, humi = %d%RH\r\n", device_id,manufacturer_id,temp_value,humi_value);
  149. rt_thread_mdelay(100);
  150. }
  151. hdc1000_deinit(dev);
  152. return 0;
  153. }
  154. MSH_CMD_EXPORT(hdc1000_selftest, hdc1000 sensor sleftest function);