LM75.c 2.3 KB

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