MAX6675.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "MAX6675.h"
  16. const SensorType MAX6675 = {
  17. .typename = "MAX6675",
  18. .altname = "MAX6675 (Thermocouple)",
  19. .interface = &SPI,
  20. .datatype = UT_TEMPERATURE,
  21. .pollingInterval = 500,
  22. .allocator = unitemp_MAX6675_alloc,
  23. .mem_releaser = unitemp_MAX6675_free,
  24. .initializer = unitemp_MAX6675_init,
  25. .deinitializer = unitemp_MAX6675_deinit,
  26. .updater = unitemp_MAX6675_update};
  27. bool unitemp_MAX6675_alloc(Sensor* sensor, char* args) {
  28. UNUSED(sensor);
  29. UNUSED(args);
  30. return true;
  31. }
  32. bool unitemp_MAX6675_free(Sensor* sensor) {
  33. UNUSED(sensor);
  34. return true;
  35. }
  36. bool unitemp_MAX6675_init(Sensor* sensor) {
  37. SPISensor* instance = sensor->instance;
  38. furi_hal_spi_bus_handle_init(instance->spi);
  39. return true;
  40. }
  41. bool unitemp_MAX6675_deinit(Sensor* sensor) {
  42. SPISensor* instance = sensor->instance;
  43. furi_hal_spi_bus_handle_deinit(instance->spi);
  44. return true;
  45. }
  46. UnitempStatus unitemp_MAX6675_update(Sensor* sensor) {
  47. SPISensor* instance = sensor->instance;
  48. furi_hal_spi_acquire(instance->spi);
  49. furi_hal_gpio_write(instance->CS_pin->pin, false);
  50. uint8_t buff[2] = {0};
  51. furi_hal_spi_bus_rx(instance->spi, buff, 2, 0xFF);
  52. furi_hal_spi_release(instance->spi);
  53. uint32_t raw = (buff[0] << 8) | buff[1];
  54. if(raw == 0xFFFFFFFF || raw == 0) return UT_SENSORSTATUS_TIMEOUT;
  55. //Определение состояния термопары
  56. uint8_t state = raw & 0b100;
  57. //Обрыв
  58. if(state == 0b100) {
  59. UNITEMP_DEBUG("%s has thermocouple open circuit", sensor->name);
  60. return UT_SENSORSTATUS_ERROR;
  61. }
  62. sensor->temp = (int16_t)(raw) / 32.0f;
  63. return UT_SENSORSTATUS_OK;
  64. }