diff --git a/src/config/config.h b/src/config/config.h index cf73064..759031b 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -180,7 +180,7 @@ static constexpr AxisConfig idler = { .iHold = 23, /// 398mA .stealth = false, .stepsPerUnit = (200 * 16 / 360.), - .sg_thrs = 5, + .sg_thrs = 6, }; /// Idler motion limits diff --git a/src/hal/tmc2130.cpp b/src/hal/tmc2130.cpp index 89951b6..61900a8 100644 --- a/src/hal/tmc2130.cpp +++ b/src/hal/tmc2130.cpp @@ -1,7 +1,7 @@ /// @file tmc2130.cpp #include "tmc2130.h" #include "../config/config.h" - +#include "../modules/permanent_storage.h" #include "../debug.h" namespace hal { @@ -44,8 +44,7 @@ bool TMC2130::Init(const MotorParams ¶ms, const MotorCurrents ¤ts, Mot WriteRegister(params, Registers::TPOWERDOWN, 0); ///Stallguard parameters - uint32_t tmc2130_coolConf = (((uint32_t)params.sg_thrs) << 16U); - WriteRegister(params, Registers::COOLCONF, tmc2130_coolConf); + SetSGTHRS(params, mps::AxisSGTHRS::get((mm::Axis)params.axis)); WriteRegister(params, Registers::TCOOLTHRS, config::tmc2130_coolStepThreshold); ///Write stealth mode config and setup diag0 output @@ -80,6 +79,11 @@ void TMC2130::SetCurrents(const MotorParams ¶ms, const MotorCurrents ¤ WriteRegister(params, Registers::IHOLD_IRUN, ihold_irun); } +void TMC2130::SetSGTHRS(const MotorParams ¶ms, uint8_t sgthrs) { + uint32_t tmc2130_coolConf = (((uint32_t)sgthrs) << 16U); + WriteRegister(params, Registers::COOLCONF, tmc2130_coolConf); +} + void TMC2130::SetEnabled(const MotorParams ¶ms, bool enabled) { hal::shr16::shr16.SetTMCEnabled(params.idx, enabled); if (this->enabled != enabled) diff --git a/src/hal/tmc2130.h b/src/hal/tmc2130.h index 3592bd8..1f73e2a 100644 --- a/src/hal/tmc2130.h +++ b/src/hal/tmc2130.h @@ -26,6 +26,7 @@ struct MotorParams { gpio::GPIO_pin sgPin; ///< stallguard pin config::MRes mRes; ///< microstep resolution int8_t sg_thrs; + uint8_t axis; }; struct MotorCurrents { @@ -91,6 +92,9 @@ public: /// Set the current motor currents void SetCurrents(const MotorParams ¶ms, const MotorCurrents ¤ts); + /// Set stallguard threshold + void SetSGTHRS(const MotorParams ¶ms, uint8_t sgthrs); + /// Return enabled state const bool Enabled() const { return enabled; diff --git a/src/modules/motion.h b/src/modules/motion.h index 15c3ec5..550f795 100644 --- a/src/modules/motion.h +++ b/src/modules/motion.h @@ -42,7 +42,7 @@ static AxisParams axisParams[NUM_AXIS] = { // Pulley { .name = 'P', - .params = { .spi = hal::spi::TmcSpiBus, .idx = Pulley, .dirOn = config::pulley.dirOn, .csPin = PULLEY_CS_PIN, .stepPin = PULLEY_STEP_PIN, .sgPin = PULLEY_SG_PIN, .mRes = config::pulley.mRes, .sg_thrs = config::pulley.sg_thrs }, + .params = { .spi = hal::spi::TmcSpiBus, .idx = Pulley, .dirOn = config::pulley.dirOn, .csPin = PULLEY_CS_PIN, .stepPin = PULLEY_STEP_PIN, .sgPin = PULLEY_SG_PIN, .mRes = config::pulley.mRes, .sg_thrs = config::pulley.sg_thrs, Axis::Pulley }, .currents = { .vSense = config::pulley.vSense, .iRun = config::pulley.iRun, .iHold = config::pulley.iHold }, .mode = DefaultMotorMode(config::pulley), .jerk = unitToSteps(config::pulleyLimits.jerk), @@ -51,7 +51,7 @@ static AxisParams axisParams[NUM_AXIS] = { // Selector { .name = 'S', - .params = { .spi = hal::spi::TmcSpiBus, .idx = Selector, .dirOn = config::selector.dirOn, .csPin = SELECTOR_CS_PIN, .stepPin = SELECTOR_STEP_PIN, .sgPin = SELECTOR_SG_PIN, .mRes = config::selector.mRes, .sg_thrs = config::selector.sg_thrs }, + .params = { .spi = hal::spi::TmcSpiBus, .idx = Selector, .dirOn = config::selector.dirOn, .csPin = SELECTOR_CS_PIN, .stepPin = SELECTOR_STEP_PIN, .sgPin = SELECTOR_SG_PIN, .mRes = config::selector.mRes, .sg_thrs = config::selector.sg_thrs, Axis::Selector }, .currents = { .vSense = config::selector.vSense, .iRun = config::selector.iRun, .iHold = config::selector.iHold }, .mode = DefaultMotorMode(config::selector), .jerk = unitToSteps(config::selectorLimits.jerk), @@ -60,7 +60,7 @@ static AxisParams axisParams[NUM_AXIS] = { // Idler { .name = 'I', - .params = { .spi = hal::spi::TmcSpiBus, .idx = Idler, .dirOn = config::idler.dirOn, .csPin = IDLER_CS_PIN, .stepPin = IDLER_STEP_PIN, .sgPin = IDLER_SG_PIN, .mRes = config::idler.mRes, .sg_thrs = config::idler.sg_thrs }, + .params = { .spi = hal::spi::TmcSpiBus, .idx = Idler, .dirOn = config::idler.dirOn, .csPin = IDLER_CS_PIN, .stepPin = IDLER_STEP_PIN, .sgPin = IDLER_SG_PIN, .mRes = config::idler.mRes, .sg_thrs = config::idler.sg_thrs, Axis::Idler }, .currents = { .vSense = config::idler.vSense, .iRun = config::idler.iRun, .iHold = config::idler.iHold }, .mode = DefaultMotorMode(config::idler), .jerk = unitToSteps(config::idlerLimits.jerk), @@ -336,7 +336,7 @@ public: void AbortPlannedMoves(bool halt = true); /// @returns the TMC213 driver associated with the particular axis - inline const hal::tmc2130::TMC2130 &DriverForAxis(Axis axis) const { + inline hal::tmc2130::TMC2130 &DriverForAxis(Axis axis) { return axisData[axis].drv; } diff --git a/src/modules/permanent_storage.cpp b/src/modules/permanent_storage.cpp index 3cf3247..6323374 100644 --- a/src/modules/permanent_storage.cpp +++ b/src/modules/permanent_storage.cpp @@ -25,6 +25,7 @@ struct eeprom_t { uint8_t eepromFilament[800]; ///< Top nibble status, bottom nibble last filament loaded uint8_t eepromDriveErrorCountH; uint8_t eepromDriveErrorCountL[2]; + uint8_t sg_thrs[3]; } __attribute__((packed)); static_assert(sizeof(eeprom_t) - 2 <= hal::eeprom::EEPROM::End(), "eeprom_t doesn't fit into EEPROM available."); @@ -359,5 +360,16 @@ void DriveError::setH(uint8_t highByte) { ee::EEPROM::UpdateByte(EEOFFSET(eepromBase->eepromDriveErrorCountH), highByte - 1); } +uint8_t AxisSGTHRS::get(mm::Axis axis) { + uint8_t sg_thrs = ee::EEPROM::ReadByte(EEOFFSET(eepromBase->sg_thrs[axis])); + if (sg_thrs & 0x80) + sg_thrs = mm::axisParams[axis].params.sg_thrs; + return sg_thrs; +} + +void AxisSGTHRS::set(mm::Axis axis, uint8_t val) { + ee::EEPROM::UpdateByte(EEOFFSET(eepromBase->sg_thrs[axis]), val); +} + } // namespace permanent_storage } // namespace modules diff --git a/src/modules/permanent_storage.h b/src/modules/permanent_storage.h index da8004a..df6a177 100644 --- a/src/modules/permanent_storage.h +++ b/src/modules/permanent_storage.h @@ -1,6 +1,7 @@ /// @file permanent_storage.h #pragma once #include "../hal/eeprom.h" +#include "motion.h" namespace modules { @@ -98,6 +99,16 @@ private: static void setH(uint8_t highByte); }; +/// @brief Read and increment drive errors +/// +/// (Motor power rail voltage loss) +class AxisSGTHRS { +public: + static uint8_t get(mm::Axis axis); + static void set(mm::Axis axis, uint8_t val); +}; + + } // namespace permanent_storage } // namespace modules diff --git a/src/registers.cpp b/src/registers.cpp index 4a595e1..e4743f2 100644 --- a/src/registers.cpp +++ b/src/registers.cpp @@ -14,6 +14,7 @@ #include "modules/idler.h" #include "modules/pulley.h" #include "modules/selector.h" +#include "modules/permanent_storage.h" /** @defgroup register_table Register Table * @@ -145,9 +146,9 @@ | 0x14h 20 | uint16 | Pulley_slow_feedrate | 0000h 0 | 0014h 20 | unit mm/s | Read / Write | M707 A0x14 | M708 A0x14 Xnnnn | 0x15h 21 | uint16 | Selector_homing_feedrate | 0000h 0 | 001eh 30 | unit mm/s | Read (Write) | M707 A0x15 | (M708 A0x15 Xnnnn) | 0x16h 22 | uint16 | Idler_homing_feedrate | 0000h 0 | 0109h 265 | unit deg/s | Read (Write) | M707 A0x16 | (M708 A0x16 Xnnnn) -| 0x17h 23 | uint16 | Pulley_sg_thrs__R | 0000h 0 | 0008h 8 | | Read (Write) | M707 A0x17 | (M708 A0x17 Xnnnn) -| 0x18h 24 | uint16 | Selector_sg_thrs_R | 0000h 0 | 0003h 3 | | Read (Write) | M707 A0x18 | (M708 A0x18 Xnnnn) -| 0x19h 25 | uint16 | Idler_sg_thrs_R | 0000h 0 | 0005h 5 | | Read (Write) | M707 A0x19 | (M708 A0x19 Xnnnn) +| 0x17h 23 | uint8 | Pulley_sg_thrs__R | 00h 0 | 08h 8 | | Read (Write) | M707 A0x17 | M708 A0x17 Xnn +| 0x18h 24 | uint8 | Selector_sg_thrs_R | 00h 0 | 03h 3 | | Read (Write) | M707 A0x18 | M708 A0x18 Xnn +| 0x19h 25 | uint8 | Idler_sg_thrs_R | 00h 0 | 05h 6 | | Read (Write) | M707 A0x19 | M708 A0x19 Xnn | 0x1ah 26 | uint16 | Get Pulley position | 0000h 0 | ffffh 65535 | unit mm | Read only | M707 A0x1a | N/A | 0x1bh 27 | uint16 | Set/Get_Selector_slot | 0000h 0 | ffffh 65535 | unit slot [0-4/5] 5=park pos | Read / Write | M707 A0x1b | M708 A0x1b Xn | 0x1ch 28 | uint16 | Set/Get_Idler_slot | 0000h 0 | ffffh 65535 | unit slot [0-4/5] 5=disengaged | Read / Write | M707 A0x1c | M708 A0x1c Xn @@ -333,19 +334,19 @@ static const RegisterRec registers[] /*PROGMEM*/ = { // 0x17 Pulley sg_thrs threshold RW RegisterRec( - []() -> uint16_t { return config::pulley.sg_thrs; }, - //@@TODO please update documentation as well - 2), + []() -> uint16_t { return mps::AxisSGTHRS::get(mm::Axis::Pulley); }, + [](uint16_t d) { mm::motion.DriverForAxis(mm::Axis::Pulley).SetSGTHRS(mm::axisParams[mm::Axis::Pulley].params, d); mps::AxisSGTHRS::set(mm::Axis::Pulley, d); }, + 1), // 0x18 Selector sg_thrs RW RegisterRec( - []() -> uint16_t { return config::selector.sg_thrs; }, - //@@TODO please update documentation as well - 2), + []() -> uint16_t { return mps::AxisSGTHRS::get(mm::Axis::Selector); }, + [](uint16_t d) { mm::motion.DriverForAxis(mm::Axis::Selector).SetSGTHRS(mm::axisParams[mm::Axis::Selector].params, d); mps::AxisSGTHRS::set(mm::Axis::Selector, d); }, + 1), // 0x19 Idler sg_thrs RW RegisterRec( - []() -> uint16_t { return config::idler.sg_thrs; }, - //@@TODO please update documentation as well - 2), + []() -> uint16_t { return mps::AxisSGTHRS::get(mm::Axis::Idler); }, + [](uint16_t d) { mm::motion.DriverForAxis(mm::Axis::Idler).SetSGTHRS(mm::axisParams[mm::Axis::Idler].params, d); mps::AxisSGTHRS::set(mm::Axis::Idler, d); }, + 1), // 0x1a Get Pulley position [mm] R RegisterRec(