From 3f73f5214bf1f7c947ea34ffe5ce77890ade68ff Mon Sep 17 00:00:00 2001 From: Stefan de Bruijn Date: Thu, 17 Dec 2020 21:52:22 +0100 Subject: [PATCH] - Fixed initialization bug in trinamic drivers - Added better error handling for validation of pin changes --- Grbl_Esp32/src/Motors/TrinamicDriver.cpp | 10 ++++++++-- Grbl_Esp32/src/Pin.cpp | 12 +++++++++++- Grbl_Esp32/src/StackTrace/AssertionFailed.cpp | 2 +- Grbl_Esp32/src/StackTrace/AssertionFailed.h | 7 ++++--- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Grbl_Esp32/src/Motors/TrinamicDriver.cpp b/Grbl_Esp32/src/Motors/TrinamicDriver.cpp index d3e1a799..22384e3c 100644 --- a/Grbl_Esp32/src/Motors/TrinamicDriver.cpp +++ b/Grbl_Esp32/src/Motors/TrinamicDriver.cpp @@ -202,6 +202,14 @@ namespace Motors { */ void TrinamicDriver::read_settings() { + // When 'test' is called and no actual trinamic is there, _has_errors will evaluate to 'true'. The + // result of that is that the check below will fail. And the result of that is that the step/dir pin + // is not initialized if we don't do it here. Unstep actually uses that pin -- and then you will get + // an assertion for using a pin that's not initialized. + // + // So... I moved init_step_dir_pins here, which basically solves that. + init_step_dir_pins(); + if (_has_errors) { return; } @@ -218,8 +226,6 @@ namespace Motors { } tmcstepper->microsteps(axis_settings[_axis_index]->microsteps->get()); tmcstepper->rms_current(run_i_ma, hold_i_percent); - - init_step_dir_pins(); } bool TrinamicDriver::set_homing_mode(bool isHoming) { diff --git a/Grbl_Esp32/src/Pin.cpp b/Grbl_Esp32/src/Pin.cpp index 60ad03d7..fdff63b4 100644 --- a/Grbl_Esp32/src/Pin.cpp +++ b/Grbl_Esp32/src/Pin.cpp @@ -165,7 +165,17 @@ Pin Pin::create(const String& str) { bool Pin::validate(const String& str) { Pins::PinDetail* pinImplementation; - auto valid = parse(str, pinImplementation); + bool valid = false; + try { + valid = parse(str, pinImplementation); + } catch (const AssertionFailed& e) { + // If an assertion happens, it's definitely invalid. +#ifdef ESP32 + grbl_msg_sendf(CLIENT_ALL, MsgLevel::Warning, "Validation for pin spec %s failed: %s", str.c_str(), e.error.c_str()); +#endif + valid = false; + } + if (pinImplementation) { delete pinImplementation; } diff --git a/Grbl_Esp32/src/StackTrace/AssertionFailed.cpp b/Grbl_Esp32/src/StackTrace/AssertionFailed.cpp index 6c914ebb..58f85be1 100644 --- a/Grbl_Esp32/src/StackTrace/AssertionFailed.cpp +++ b/Grbl_Esp32/src/StackTrace/AssertionFailed.cpp @@ -23,7 +23,7 @@ AssertionFailed AssertionFailed::create(const char* condition, const char* msg, st += " at: "; st += esp_backtrace_print(10); - return AssertionFailed(st); + return AssertionFailed(st, tmp); } #else diff --git a/Grbl_Esp32/src/StackTrace/AssertionFailed.h b/Grbl_Esp32/src/StackTrace/AssertionFailed.h index d8766690..270c9ab2 100644 --- a/Grbl_Esp32/src/StackTrace/AssertionFailed.h +++ b/Grbl_Esp32/src/StackTrace/AssertionFailed.h @@ -6,11 +6,12 @@ class AssertionFailed { public: String stackTrace; + String error; - AssertionFailed(String st) : stackTrace(st) {} + AssertionFailed(String st, String err) : stackTrace(st), error(err) {} static AssertionFailed create(const char* condition) { - return create(condition, "Assertion failed"); + return create(condition, "Unknown error (assertion failed)."); } static AssertionFailed create(const char* condition, const char* msg, ...); @@ -24,7 +25,7 @@ public: String stackTrace; static std::exception create(const char* condition) { - return create(condition, "Assertion failed"); + return create(condition, "Unknown error (assertion failed)."); } static std::exception create(const char* condition, const char* msg, ...); };