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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // http://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
  70. // constants for calculating temperature from the measured RTD resistance.
  71. #define RTD_Z1 -0.0039083
  72. #define RTD_Z2 0.00001758480889
  73. #define RTD_Z3 -0.0000000231
  74. #define RTD_Z4 -0.000001155
  75. typedef enum max31865_numwires {
  76. MAX31865_2WIRE = 0,
  77. MAX31865_3WIRE = 1,
  78. MAX31865_4WIRE = 0
  79. } max31865_numwires_t;
  80. #if DISABLED(MAX31865_USE_AUTO_MODE)
  81. typedef enum one_shot_event : uint8_t {
  82. SETUP_BIAS_VOLTAGE,
  83. SETUP_1_SHOT_MODE,
  84. READ_RTD_REG
  85. } one_shot_event_t;
  86. #endif
  87. /* Interface class for the MAX31865 RTD Sensor reader */
  88. class MAX31865 {
  89. private:
  90. static SPISettings spiConfig;
  91. TERN(LARGE_PINMAP, uint32_t, uint8_t) sclkPin, misoPin, mosiPin, cselPin;
  92. uint16_t spiDelay;
  93. float zeroRes, refRes, wireRes;
  94. #if ENABLED(MAX31865_USE_READ_ERROR_DETECTION)
  95. millis_t lastReadStamp = 0;
  96. #endif
  97. uint16_t lastRead = 0;
  98. uint8_t lastFault = 0;
  99. #if DISABLED(MAX31865_USE_AUTO_MODE)
  100. millis_t nextEventStamp;
  101. one_shot_event_t nextEvent;
  102. #endif
  103. #ifdef MAX31865_IGNORE_INITIAL_FAULTY_READS
  104. uint8_t ignore_faults = MAX31865_IGNORE_INITIAL_FAULTY_READS;
  105. uint16_t fixFault(uint16_t rtd);
  106. #endif
  107. uint8_t stdFlags = 0;
  108. void setConfig(uint8_t config, bool enable);
  109. void readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n);
  110. uint8_t readRegister8(uint8_t addr);
  111. uint16_t readRegister16(uint8_t addr);
  112. void writeRegister8(uint8_t addr, uint8_t reg);
  113. void writeRegister16(uint8_t addr, uint16_t reg);
  114. void softSpiInit();
  115. void spiBeginTransaction();
  116. uint8_t spiTransfer(uint8_t addr);
  117. void spiEndTransaction();
  118. void initFixedFlags(max31865_numwires_t wires);
  119. void enable50HzFilter(bool b);
  120. void enableBias();
  121. void oneShot();
  122. void resetFlags();
  123. uint16_t readRawImmediate();
  124. void runAutoFaultDetectionCycle();
  125. public:
  126. #if ENABLED(LARGE_PINMAP)
  127. MAX31865(uint32_t spi_cs, uint8_t pin_mapping);
  128. MAX31865(uint32_t spi_cs, uint32_t spi_mosi, uint32_t spi_miso,
  129. uint32_t spi_clk, uint8_t pin_mapping);
  130. #else
  131. MAX31865(int8_t spi_cs);
  132. MAX31865(int8_t spi_cs, int8_t spi_mosi, int8_t spi_miso,
  133. int8_t spi_clk);
  134. #endif
  135. void begin(max31865_numwires_t wires, float zero_res, float ref_res, float wire_res);
  136. uint8_t readFault();
  137. void clearFault();
  138. uint16_t readRaw();
  139. float readResistance();
  140. float temperature();
  141. float temperature(uint16_t adc_val);
  142. float temperature(float rtd_res);
  143. };