diff --git a/src/hal/avr/tmc2130.cpp b/src/hal/avr/tmc2130.cpp index 44e7196..8c17e85 100644 --- a/src/hal/avr/tmc2130.cpp +++ b/src/hal/avr/tmc2130.cpp @@ -74,7 +74,7 @@ void TMC2130::SetCurrents(const MotorParams ¶ms, const MotorCurrents ¤ } void TMC2130::SetEnabled(const MotorParams ¶ms, bool enabled) { - hal::shr16::shr16.SetTMCDir(params.idx, enabled); + hal::shr16::shr16.SetTMCEnabled(params.idx, enabled); if (this->enabled != enabled) ClearStallguard(params); this->enabled = enabled; diff --git a/tests/unit/modules/motion/test_motion.cpp b/tests/unit/modules/motion/test_motion.cpp index 788644c..936e077 100644 --- a/tests/unit/modules/motion/test_motion.cpp +++ b/tests/unit/modules/motion/test_motion.cpp @@ -3,6 +3,17 @@ using namespace modules::motion; +namespace hal { +namespace shr16 { +extern uint8_t shr16_tmc_ena; +} // namespace shr16 +} // namespace hal + +// Conveniently read the enable state set into the lower-level shift register +bool getTMCEnabled(const MotorParams &mp) { + return (hal::shr16::shr16_tmc_ena & (1 << mp.idx)); +} + // Perform Step() until all moves are completed, returning the number of steps performed. // Ensure the move doesn't run forever, making the test fail reliably. ssize_t stepUntilDone(size_t maxSteps = 100000) { @@ -34,19 +45,23 @@ TEST_CASE("motion::basic", "[motion]") { TEST_CASE("motion::auto_axis_enable", "[motion]") { // by default the axis should start disabled REQUIRE(motion.Enabled(Pulley) == false); + REQUIRE(getTMCEnabled(axisParams[Pulley].params) == false); // enable manually the axis motion.SetEnabled(Pulley, true); REQUIRE(motion.Enabled(Pulley) == true); + REQUIRE(getTMCEnabled(axisParams[Pulley].params) == true); // now disable motion.SetEnabled(Pulley, false); REQUIRE(motion.Enabled(Pulley) == false); + REQUIRE(getTMCEnabled(axisParams[Pulley].params) == false); // planning a move should enable the axis automatically REQUIRE(motion.QueueEmpty()); motion.PlanMove(1.0_mm, 100.0_mm_s); REQUIRE(motion.Enabled(Pulley) == true); + REQUIRE(getTMCEnabled(axisParams[Pulley].params) == true); } TEST_CASE("motion::unit", "[motion]") { diff --git a/tests/unit/modules/stubs/stub_shr16.cpp b/tests/unit/modules/stubs/stub_shr16.cpp index 97d179f..839fadf 100644 --- a/tests/unit/modules/stubs/stub_shr16.cpp +++ b/tests/unit/modules/stubs/stub_shr16.cpp @@ -7,10 +7,12 @@ SHR16 shr16; uint16_t shr16_v_copy; uint8_t shr16_tmc_dir; +uint8_t shr16_tmc_ena; void SHR16::Init() { shr16_v_copy = 0; shr16_tmc_dir = 0; + shr16_tmc_ena = 0; } void SHR16::SetLED(uint16_t led) { @@ -18,7 +20,11 @@ void SHR16::SetLED(uint16_t led) { } void SHR16::SetTMCEnabled(uint8_t index, bool ena) { - // do nothing right now + // this is using another array for testing convenience + if (ena) + shr16_tmc_ena |= (1 << index); + else + shr16_tmc_ena &= ~(1 << index); } void SHR16::SetTMCDir(uint8_t index, bool dir) {