1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-12-05 02:19:14 +00:00
MarlinFirmware/Marlin/src/feature/tmc_util.h

294 lines
9.2 KiB
C
Raw Normal View History

2017-12-15 21:03:14 +00:00
/**
* Marlin 3D Printer Firmware
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
2017-12-15 21:03:14 +00:00
2018-10-03 07:48:49 +00:00
#include "../inc/MarlinConfig.h"
#include "../lcd/ultralcd.h"
2018-10-03 07:48:49 +00:00
#if HAS_TRINAMIC
#include <TMCStepper.h>
2018-03-13 07:01:41 +00:00
#endif
#if HAS_LCD_MENU
#include "../module/planner.h"
#endif
2018-03-13 07:01:41 +00:00
2018-10-03 07:48:49 +00:00
#define TMC_X_LABEL 'X', '0'
#define TMC_Y_LABEL 'Y', '0'
#define TMC_Z_LABEL 'Z', '0'
#define TMC_X2_LABEL 'X', '2'
#define TMC_Y2_LABEL 'Y', '2'
#define TMC_Z2_LABEL 'Z', '2'
#define TMC_Z3_LABEL 'Z', '3'
#define TMC_E0_LABEL 'E', '0'
#define TMC_E1_LABEL 'E', '1'
#define TMC_E2_LABEL 'E', '2'
#define TMC_E3_LABEL 'E', '3'
#define TMC_E4_LABEL 'E', '4'
#define TMC_E5_LABEL 'E', '5'
#define CHOPPER_DEFAULT_12V { 3, -1, 1 }
#define CHOPPER_DEFAULT_19V { 4, 1, 1 }
#define CHOPPER_DEFAULT_24V { 4, 2, 1 }
#define CHOPPER_DEFAULT_36V { 5, 2, 4 }
#define CHOPPER_PRUSAMK3_24V { 4, 1, 4 }
#define CHOPPER_MARLIN_119 { 5, 2, 3 }
constexpr uint16_t _tmc_thrs(const uint16_t msteps, const int32_t thrs, const uint32_t spmm) {
return 12650000UL * msteps / (256 * thrs * spmm);
}
2018-10-03 07:48:49 +00:00
template<char AXIS_LETTER, char DRIVER_ID>
class TMCStorage {
protected:
// Only a child class has access to constructor => Don't create on its own! "Poor man's abstract class"
TMCStorage() {}
public:
2018-10-03 07:48:49 +00:00
uint16_t val_mA = 0;
#if ENABLED(MONITOR_DRIVER_STATUS)
uint8_t otpw_count = 0,
error_count = 0;
2018-10-03 07:48:49 +00:00
bool flag_otpw = false;
inline bool getOTPW() { return flag_otpw; }
inline void clear_otpw() { flag_otpw = 0; }
2018-10-03 07:48:49 +00:00
#endif
inline uint16_t getMilliamps() { return val_mA; }
2017-12-15 21:03:14 +00:00
inline void printLabel() {
2018-10-03 07:48:49 +00:00
SERIAL_CHAR(AXIS_LETTER);
if (DRIVER_ID > '0') SERIAL_CHAR(DRIVER_ID);
}
struct {
#if HAS_STEALTHCHOP
bool stealthChop_enabled = false;
#endif
#if ENABLED(HYBRID_THRESHOLD)
uint8_t hybrid_thrs = 0;
#endif
#if ENABLED(SENSORLESS_HOMING)
int8_t homing_thrs = 0;
#endif
} stored;
2018-10-03 07:48:49 +00:00
};
template<class TMC, char AXIS_LETTER, char DRIVER_ID>
class TMCMarlin : public TMC, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
public:
TMCMarlin(uint16_t cs_pin, float RS) :
TMC(cs_pin, RS)
{}
TMCMarlin(uint16_t CS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK) :
TMC(CS, RS, pinMOSI, pinMISO, pinSCK)
{}
inline uint16_t rms_current() { return TMC::rms_current(); }
inline void rms_current(uint16_t mA) {
2018-10-03 07:48:49 +00:00
this->val_mA = mA;
TMC::rms_current(mA);
}
inline void rms_current(uint16_t mA, float mult) {
2018-10-03 07:48:49 +00:00
this->val_mA = mA;
TMC::rms_current(mA, mult);
}
#if HAS_STEALTHCHOP
inline void refresh_stepping_mode() { this->en_pwm_mode(this->stored.stealthChop_enabled); }
inline bool get_stealthChop_status() { return this->en_pwm_mode(); }
#endif
#if HAS_LCD_MENU
inline void init_lcd_variables(const AxisEnum spmm_id) {
#if ENABLED(HYBRID_THRESHOLD)
this->stored.hybrid_thrs = _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[spmm_id]);
#endif
#if ENABLED(SENSORLESS_HOMING)
this->stored.homing_thrs = this->sgt();
#endif
}
inline void refresh_stepper_current() { rms_current(this->val_mA); }
#if ENABLED(HYBRID_THRESHOLD)
inline void refresh_hybrid_thrs(float spmm) { this->TPWMTHRS(_tmc_thrs(this->microsteps(), this->stored.hybrid_thrs, spmm)); }
#endif
#if ENABLED(SENSORLESS_HOMING)
inline void refresh_homing_thrs() { this->sgt(this->stored.homing_thrs); }
#endif
#endif
2018-10-03 07:48:49 +00:00
};
template<char AXIS_LETTER, char DRIVER_ID>
class TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID> : public TMC2208Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
public:
TMCMarlin(Stream * SerialPort, float RS, bool has_rx=true) :
TMC2208Stepper(SerialPort, RS, has_rx=true)
{}
TMCMarlin(uint16_t RX, uint16_t TX, float RS, bool has_rx=true) :
TMC2208Stepper(RX, TX, RS, has_rx=true)
{}
uint16_t rms_current() { return TMC2208Stepper::rms_current(); }
inline void rms_current(uint16_t mA) {
2018-10-03 07:48:49 +00:00
this->val_mA = mA;
TMC2208Stepper::rms_current(mA);
}
inline void rms_current(uint16_t mA, float mult) {
2018-10-03 07:48:49 +00:00
this->val_mA = mA;
TMC2208Stepper::rms_current(mA, mult);
}
#if HAS_STEALTHCHOP
inline void refresh_stepping_mode() { en_spreadCycle(!this->stored.stealthChop_enabled); }
2019-01-18 00:48:29 +00:00
inline bool get_stealthChop_status() { return !this->en_spreadCycle(); }
#endif
#if HAS_LCD_MENU
inline void init_lcd_variables(const AxisEnum spmm_id) {
#if ENABLED(HYBRID_THRESHOLD)
this->stored.hybrid_thrs = _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[spmm_id]);
#endif
}
inline void refresh_stepper_current() { rms_current(this->val_mA); }
#if ENABLED(HYBRID_THRESHOLD)
inline void refresh_hybrid_thrs(float spmm) { this->TPWMTHRS(_tmc_thrs(this->microsteps(), this->stored.hybrid_thrs, spmm)); }
#endif
#endif
};
template<char AXIS_LETTER, char DRIVER_ID>
class TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID> : public TMC2660Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
public:
TMCMarlin(uint16_t cs_pin, float RS) :
TMC2660Stepper(cs_pin, RS)
{}
TMCMarlin(uint16_t CS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK) :
TMC2660Stepper(CS, RS, pinMOSI, pinMISO, pinSCK)
{}
inline uint16_t rms_current() { return TMC2660Stepper::rms_current(); }
inline void rms_current(uint16_t mA) {
this->val_mA = mA;
TMC2660Stepper::rms_current(mA);
}
2017-12-15 21:03:14 +00:00
#if HAS_LCD_MENU
inline void init_lcd_variables(const AxisEnum spmm_id) {
#if ENABLED(SENSORLESS_HOMING)
this->stored.homing_thrs = this->sgt();
#endif
}
inline void refresh_stepper_current() { rms_current(this->val_mA); }
#if ENABLED(SENSORLESS_HOMING)
inline void refresh_homing_thrs() { this->sgt(this->stored.homing_thrs); }
#endif
#endif
};
2018-01-10 01:14:07 +00:00
2017-12-15 21:03:14 +00:00
template<typename TMC>
2018-10-03 07:48:49 +00:00
void tmc_get_current(TMC &st) {
st.printLabel();
SERIAL_ECHOLNPAIR(" driver current: ", st.getMilliamps());
2017-12-29 19:38:08 +00:00
}
2017-12-15 21:03:14 +00:00
template<typename TMC>
2018-05-10 05:05:15 +00:00
void tmc_set_current(TMC &st, const int mA) {
2018-10-03 07:48:49 +00:00
st.rms_current(mA);
2017-12-29 19:38:08 +00:00
}
2018-10-03 07:48:49 +00:00
#if ENABLED(MONITOR_DRIVER_STATUS)
template<typename TMC>
void tmc_report_otpw(TMC &st) {
st.printLabel();
SERIAL_ECHOPGM(" temperature prewarn triggered: ");
serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false"));
SERIAL_EOL();
}
template<typename TMC>
void tmc_clear_otpw(TMC &st) {
st.clear_otpw();
st.printLabel();
SERIAL_ECHOLNPGM(" prewarn flag cleared");
}
#endif
2017-12-15 21:03:14 +00:00
template<typename TMC>
2018-10-03 07:48:49 +00:00
void tmc_get_pwmthrs(TMC &st, const uint16_t spmm) {
st.printLabel();
SERIAL_ECHOLNPAIR(" stealthChop max speed: ", _tmc_thrs(st.microsteps(), st.TPWMTHRS(), spmm));
2017-12-29 19:38:08 +00:00
}
2017-12-15 21:03:14 +00:00
template<typename TMC>
2018-05-10 05:05:15 +00:00
void tmc_set_pwmthrs(TMC &st, const int32_t thrs, const uint32_t spmm) {
2018-01-10 01:14:07 +00:00
st.TPWMTHRS(_tmc_thrs(st.microsteps(), thrs, spmm));
2017-12-29 19:38:08 +00:00
}
2017-12-15 21:03:14 +00:00
template<typename TMC>
2018-10-03 07:48:49 +00:00
void tmc_get_sgt(TMC &st) {
st.printLabel();
SERIAL_ECHOPGM(" homing sensitivity: ");
SERIAL_PRINTLN(st.sgt(), DEC);
2017-12-29 19:38:08 +00:00
}
2017-12-15 21:03:14 +00:00
template<typename TMC>
2018-05-10 05:05:15 +00:00
void tmc_set_sgt(TMC &st, const int8_t sgt_val) {
2017-12-29 19:38:08 +00:00
st.sgt(sgt_val);
}
2017-12-15 21:03:14 +00:00
void monitor_tmc_driver();
void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e);
2017-12-15 21:03:14 +00:00
2018-01-10 01:14:07 +00:00
#if ENABLED(TMC_DEBUG)
2018-10-06 22:18:10 +00:00
#if ENABLED(MONITOR_DRIVER_STATUS)
void tmc_set_report_status(const bool status);
#endif
void tmc_report_all(const bool print_x, const bool print_y, const bool print_z, const bool print_e);
void tmc_get_registers(const bool print_x, const bool print_y, const bool print_z, const bool print_e);
2018-01-10 01:14:07 +00:00
#endif
#if HAS_LCD_MENU
void init_tmc_section();
#endif
2017-12-15 21:03:14 +00:00
/**
* TMC2130 specific sensorless homing using stallGuard2.
* stallGuard2 only works when in spreadCycle mode.
* spreadCycle and stealthChop are mutually exclusive.
*
* Defined here because of limitations with templates and headers.
*/
2018-09-09 19:59:12 +00:00
#if USE_SENSORLESS
// Track enabled status of stealthChop and only re-enable where applicable
struct sensorless_t {
bool x, y, z, x2, y2, z2, z3;
};
bool tmc_enable_stallguard(TMC2130Stepper &st);
void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth);
bool tmc_enable_stallguard(TMC2660Stepper);
void tmc_disable_stallguard(TMC2660Stepper, const bool);
2017-12-15 21:03:14 +00:00
#endif
2018-10-06 22:18:10 +00:00
#if TMC_HAS_SPI
void tmc_init_cs_pins();
#endif