sensor_max31875.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-1-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(NU_PKG_USING_MAX31875)
  14. #include <sys/time.h>
  15. #include "sensor.h"
  16. #include "max31875_c.h"
  17. #define DBG_ENABLE
  18. #define DBG_LEVEL DBG_LOG
  19. #define DBG_SECTION_NAME "sensor.max31875"
  20. #define DBG_COLOR
  21. #include <rtdbg.h>
  22. static S_MAX31875 g_sMax31875;
  23. static struct rt_i2c_bus_device *i2c_bus_dev;
  24. static int max31875_i2c_write_reg(int address, const char *data, int length)
  25. {
  26. struct rt_i2c_msg msg;
  27. msg.addr = address; /* Slave address */
  28. msg.flags = RT_I2C_WR; /* Write flag */
  29. msg.buf = (rt_uint8_t *)data; /* Slave register address */
  30. msg.len = length; /* Number of bytes sent */
  31. if (rt_i2c_transfer(i2c_bus_dev, &msg, 1) != 1)
  32. {
  33. return -RT_ERROR;
  34. }
  35. return RT_EOK;
  36. }
  37. static int max31875_i2c_read_reg(int address, const char *reg, int reg_length, char *data, int length)
  38. {
  39. struct rt_i2c_msg msgs[2];
  40. msgs[0].addr = address; /* Slave address */
  41. msgs[0].flags = RT_I2C_WR; /* Write flag */
  42. msgs[0].buf = (rt_uint8_t *)reg; /* Slave register address */
  43. msgs[0].len = reg_length; /* Number of bytes sent */
  44. msgs[1].addr = address; /* Slave address */
  45. msgs[1].flags = RT_I2C_RD; /* Read flag without READ_ACK */
  46. msgs[1].buf = (rt_uint8_t *)data; /* Read data pointer */
  47. msgs[1].len = length; /* Number of bytes read */
  48. if (rt_i2c_transfer(i2c_bus_dev, &msgs[0], 2) != 2)
  49. {
  50. return -RT_ERROR;
  51. }
  52. return RT_EOK;
  53. }
  54. static rt_size_t max31875_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
  55. {
  56. struct rt_sensor_data *data = (struct rt_sensor_data *)buf;
  57. if (sensor->info.type == RT_SENSOR_CLASS_TEMP)
  58. {
  59. float temp_value;
  60. temp_value = max31875_read_reg_as_temperature(MAX31875_REG_TEMPERATURE, &g_sMax31875);
  61. data->type = RT_SENSOR_CLASS_TEMP;
  62. data->data.temp = (rt_int32_t)(temp_value * 10);
  63. data->timestamp = rt_sensor_get_ts();
  64. }
  65. return 1;
  66. }
  67. static rt_err_t max31875_control(struct rt_sensor_device *sensor, int cmd, void *args)
  68. {
  69. return -RT_ERROR;
  70. }
  71. static struct rt_sensor_ops sensor_ops =
  72. {
  73. max31875_fetch_data,
  74. max31875_control
  75. };
  76. int rt_hw_max31875_temp_init(const char *name, struct rt_sensor_config *cfg)
  77. {
  78. rt_int8_t result;
  79. rt_sensor_t sensor = RT_NULL;
  80. sensor = rt_calloc(1, sizeof(struct rt_sensor_device));
  81. if (sensor == RT_NULL)
  82. return -(RT_ENOMEM);
  83. sensor->info.type = RT_SENSOR_CLASS_TEMP;
  84. sensor->info.vendor = RT_SENSOR_VENDOR_UNKNOWN;
  85. sensor->info.model = "max31875_temp";
  86. sensor->info.unit = RT_SENSOR_UNIT_DCELSIUS;
  87. sensor->info.intf_type = RT_SENSOR_INTF_I2C;
  88. sensor->info.range_max = 70;
  89. sensor->info.range_min = 0;
  90. sensor->info.period_min = 100; //100ms
  91. rt_memcpy(&sensor->config, cfg, sizeof(struct rt_sensor_config));
  92. sensor->ops = &sensor_ops;
  93. result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDWR, RT_NULL);
  94. if (result != RT_EOK)
  95. {
  96. LOG_E("device register: %d", result);
  97. rt_free(sensor);
  98. return -RT_ERROR;
  99. }
  100. return RT_EOK;
  101. }
  102. int rt_hw_max31875_init(const char *name, struct rt_sensor_config *cfg)
  103. {
  104. struct rt_sensor_intf *intf;
  105. rt_err_t ret = RT_ERROR;
  106. RT_ASSERT(name != NULL);
  107. RT_ASSERT(cfg != NULL);
  108. intf = &cfg->intf;
  109. /* Find I2C bus */
  110. i2c_bus_dev = (struct rt_i2c_bus_device *)rt_device_find(intf->dev_name);
  111. if (i2c_bus_dev == RT_NULL)
  112. {
  113. goto exit_rt_hw_max31875_init;
  114. }
  115. g_sMax31875.read = max31875_i2c_read_reg;
  116. g_sMax31875.write = max31875_i2c_write_reg;
  117. g_sMax31875.address = (uint32_t)(intf->user_data) & 0xff;
  118. if ((ret = max31875_init(g_sMax31875.address)) != RT_EOK)
  119. {
  120. LOG_E("Init..!\n");
  121. }
  122. else if ((ret = max31875_write_cfg(MAX31875_CFG_CONV_RATE_8 | MAX31875_CFG_RESOLUTION_12BIT, &g_sMax31875)) != RT_EOK)
  123. {
  124. LOG_E("Write_cfg..!\n");
  125. }
  126. else
  127. return rt_hw_max31875_temp_init(name, cfg);
  128. exit_rt_hw_max31875_init:
  129. return -(ret);
  130. }
  131. #endif //#if defined(NU_PKG_USING_MAX31875)