LM75.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "LM75.h"
  2. #include "../interfaces/I2CSensor.h"
  3. #define LM75_REG_TEMP 0x00
  4. #define LM75_REG_CONFIG 0x01
  5. #define LM75_REG_THYST 0x02
  6. #define LM75_REG_TOS 0x03
  7. #define LM75_CONFIG_SHUTDOWN 0b00000001
  8. #define LM75_CONFIG_INTERRUPT 0b00000010
  9. #define LM75_CONFIG_OSPOLARITY_HIGH 0b00000100
  10. #define LM75_CONFIG_FAULTQUEUE_1 0b00000000
  11. #define LM75_CONFIG_FAULTQUEUE_2 0b00001000
  12. #define LM75_CONFIG_FAULTQUEUE_4 0b00010000
  13. #define LM75_CONFIG_FAULTQUEUE_6 0b00011000
  14. const SensorType LM75 = {
  15. .typename = "LM75",
  16. .interface = &I2C,
  17. .pollingInterval = 500,
  18. .allocator = unitemp_LM75_alloc,
  19. .mem_releaser = unitemp_LM75_free,
  20. .initializer = unitemp_LM75_init,
  21. .deinitializer = unitemp_LM75_deinit,
  22. .updater = unitemp_LM75_update};
  23. bool unitemp_LM75_alloc(Sensor* sensor, char* args) {
  24. UNUSED(args);
  25. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  26. //Адреса на шине I2C (7 бит)
  27. i2c_sensor->minI2CAdr = 0b1001000;
  28. i2c_sensor->maxI2CAdr = 0b1001111;
  29. return true;
  30. }
  31. bool unitemp_LM75_free(Sensor* sensor) {
  32. //Нечего высвобождать, так как ничего не было выделено
  33. UNUSED(sensor);
  34. return true;
  35. }
  36. bool unitemp_LM75_init(Sensor* sensor) {
  37. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  38. //Выход если не удалось записать значение в датчик
  39. if(!unitemp_i2c_writeReg(i2c_sensor, LM75_REG_CONFIG, LM75_CONFIG_FAULTQUEUE_1)) return false;
  40. return true;
  41. }
  42. bool unitemp_LM75_deinit(Sensor* sensor) {
  43. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  44. if(!unitemp_i2c_writeReg(
  45. i2c_sensor, LM75_REG_CONFIG, LM75_CONFIG_FAULTQUEUE_1 | LM75_CONFIG_SHUTDOWN))
  46. return false;
  47. return true;
  48. }
  49. UnitempStatus unitemp_LM75_update(Sensor* sensor) {
  50. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  51. uint8_t buff[2];
  52. if(!unitemp_i2c_readRegArray(i2c_sensor, LM75_REG_TEMP, 2, buff)) return UT_TIMEOUT;
  53. int16_t raw = ((((uint16_t)buff[0] << 8) | buff[1]) >> 7);
  54. if(FURI_BIT(raw, 8)) {
  55. FURI_BIT_CLEAR(raw, 8);
  56. raw = (int8_t)raw;
  57. }
  58. sensor->temp = (float)raw / 2.0f;
  59. return UT_OK;
  60. }