My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MAX31865.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Based on Adafruit MAX31865 library:
  24. *
  25. * This is a library for the Adafruit PT100/P1000 RTD Sensor w/MAX31865
  26. * Designed specifically to work with the Adafruit RTD Sensor
  27. * https://www.adafruit.com/products/3328
  28. *
  29. * This sensor uses SPI to communicate, 4 pins are required to interface.
  30. *
  31. * Adafruit invests time and resources providing this open source code,
  32. * please support Adafruit and open-source hardware by purchasing
  33. * products from Adafruit!
  34. *
  35. * Written by Limor Fried/Ladyada for Adafruit Industries.
  36. *
  37. * Modifications by JoAnn Manges (@GadgetAngel)
  38. * Copyright (c) 2020, JoAnn Manges
  39. * All rights reserved.
  40. */
  41. #pragma once
  42. //#define DEBUG_MAX31865
  43. #include "../inc/MarlinConfig.h"
  44. #include "../HAL/shared/Delay.h"
  45. #include HAL_PATH(../HAL, MarlinSPI.h)
  46. #define MAX31865_CONFIG_REG 0x00
  47. #define MAX31865_CONFIG_BIAS 0x80
  48. #define MAX31865_CONFIG_MODEAUTO 0x40
  49. #define MAX31865_CONFIG_MODEOFF 0x00
  50. #define MAX31865_CONFIG_1SHOT 0x20
  51. #define MAX31865_CONFIG_3WIRE 0x10
  52. #define MAX31865_CONFIG_24WIRE 0x00
  53. #define MAX31865_CONFIG_FAULTSTAT 0x02
  54. #define MAX31865_CONFIG_FILT50HZ 0x01
  55. #define MAX31865_CONFIG_FILT60HZ 0x00
  56. #define MAX31865_RTDMSB_REG 0x01
  57. #define MAX31865_RTDLSB_REG 0x02
  58. #define MAX31865_HFAULTMSB_REG 0x03
  59. #define MAX31865_HFAULTLSB_REG 0x04
  60. #define MAX31865_LFAULTMSB_REG 0x05
  61. #define MAX31865_LFAULTLSB_REG 0x06
  62. #define MAX31865_FAULTSTAT_REG 0x07
  63. #define MAX31865_FAULT_HIGHTHRESH 0x80 // D7
  64. #define MAX31865_FAULT_LOWTHRESH 0x40 // D6
  65. #define MAX31865_FAULT_REFINLOW 0x20 // D5
  66. #define MAX31865_FAULT_REFINHIGH 0x10 // D4
  67. #define MAX31865_FAULT_RTDINLOW 0x08 // D3
  68. #define MAX31865_FAULT_OVUV 0x04 // D2
  69. typedef enum max31865_numwires {
  70. MAX31865_2WIRE = 0,
  71. MAX31865_3WIRE = 1,
  72. MAX31865_4WIRE = 0
  73. } max31865_numwires_t;
  74. #if DISABLED(MAX31865_USE_AUTO_MODE)
  75. typedef enum one_shot_event : uint8_t {
  76. SETUP_BIAS_VOLTAGE,
  77. SETUP_1_SHOT_MODE,
  78. READ_RTD_REG
  79. } one_shot_event_t;
  80. #endif
  81. /* Interface class for the MAX31865 RTD Sensor reader */
  82. class MAX31865 {
  83. private:
  84. static SPISettings spiConfig;
  85. TERN(LARGE_PINMAP, uint32_t, uint8_t) sclkPin, misoPin, mosiPin, cselPin;
  86. uint16_t spiDelay;
  87. float resNormalizer, refRes, wireRes;
  88. #if ENABLED(MAX31865_USE_READ_ERROR_DETECTION)
  89. millis_t lastReadStamp = 0;
  90. #endif
  91. uint16_t lastRead = 0;
  92. uint8_t lastFault = 0;
  93. #if DISABLED(MAX31865_USE_AUTO_MODE)
  94. millis_t nextEventStamp;
  95. one_shot_event_t nextEvent;
  96. #endif
  97. #ifdef MAX31865_IGNORE_INITIAL_FAULTY_READS
  98. uint8_t ignore_faults = MAX31865_IGNORE_INITIAL_FAULTY_READS;
  99. uint16_t fixFault(uint16_t rtd);
  100. #endif
  101. uint8_t stdFlags = 0;
  102. void setConfig(uint8_t config, bool enable);
  103. void readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n);
  104. uint8_t readRegister8(uint8_t addr);
  105. uint16_t readRegister16(uint8_t addr);
  106. void writeRegister8(uint8_t addr, uint8_t reg);
  107. void writeRegister16(uint8_t addr, uint16_t reg);
  108. void softSpiInit();
  109. void spiBeginTransaction();
  110. uint8_t spiTransfer(uint8_t addr);
  111. void spiEndTransaction();
  112. void initFixedFlags(max31865_numwires_t wires);
  113. void enable50HzFilter(bool b);
  114. void enableBias();
  115. void oneShot();
  116. void resetFlags();
  117. uint16_t readRawImmediate();
  118. void runAutoFaultDetectionCycle();
  119. public:
  120. #if ENABLED(LARGE_PINMAP)
  121. MAX31865(uint32_t spi_cs, uint8_t pin_mapping);
  122. MAX31865(uint32_t spi_cs, uint32_t spi_mosi, uint32_t spi_miso,
  123. uint32_t spi_clk, uint8_t pin_mapping);
  124. #else
  125. MAX31865(int8_t spi_cs);
  126. MAX31865(int8_t spi_cs, int8_t spi_mosi, int8_t spi_miso,
  127. int8_t spi_clk);
  128. #endif
  129. void begin(max31865_numwires_t wires, const_float_t zero_res, const_float_t ref_res, const_float_t wire_res);
  130. uint8_t readFault();
  131. void clearFault();
  132. uint16_t readRaw();
  133. float readResistance();
  134. float temperature();
  135. float temperature(const uint16_t adc_val);
  136. float temperature(float rtd_res);
  137. };