diff --git a/Grbl_Esp32/src/Grbl.cpp b/Grbl_Esp32/src/Grbl.cpp index d3e97cd1..7869e012 100644 --- a/Grbl_Esp32/src/Grbl.cpp +++ b/Grbl_Esp32/src/Grbl.cpp @@ -64,45 +64,47 @@ void grbl_init() { // Load Grbl settings from non-volatile storage debug_serial("Initializing settings..."); settings_init(); // requires config - config->load(config_filename->get()); + bool configOkay = config->load(config_filename->get()); make_grbl_commands(); // Setup input polling loop after loading the configuration, // because the polling may depend on the config client_init(); - report_machine_type(CLIENT_SERIAL); - info_serial("Board: %s", config->_board.c_str()); + if (configOkay) { + report_machine_type(CLIENT_SERIAL); + info_serial("Board: %s", config->_board.c_str()); - if (config->_i2so) { - info_serial("Initializing I2SO..."); - // The I2S out must be initialized before it can access the expanded GPIO port. Must be initialized _after_ settings! - i2s_out_init(); - } - if (config->_spi) { - info_serial("Initializing SPI..."); - // The SPI must be initialized before we can use it. - config->_spi->init(); - - // Initialize SD card after SPI: - if (config->_sdCard != nullptr) { - config->_sdCard->init(); + if (config->_i2so) { + info_serial("Initializing I2SO..."); + // The I2S out must be initialized before it can access the expanded GPIO port. Must be initialized _after_ settings! + i2s_out_init(); } + if (config->_spi) { + info_serial("Initializing SPI..."); + // The SPI must be initialized before we can use it. + config->_spi->init(); + + // Initialize SD card after SPI: + if (config->_sdCard != nullptr) { + config->_sdCard->init(); + } + } + + info_serial("Initializing steppers..."); + stepper_init(); // Configure stepper pins and interrupt timers + + info_serial("Initializing axes..."); + config->_axes->read_settings(); + config->_axes->init(); + + config->_control->init(); + init_output_pins(); // Configure pinout pins and pin-change interrupt (Renamed due to conflict with esp32 files) + memset(sys_position, 0, sizeof(sys_position)); // Clear machine position. + + machine_init(); // user supplied function for special initialization } - info_serial("Initializing steppers..."); - stepper_init(); // Configure stepper pins and interrupt timers - - info_serial("Initializing axes..."); - config->_axes->read_settings(); - config->_axes->init(); - - config->_control->init(); - init_output_pins(); // Configure pinout pins and pin-change interrupt (Renamed due to conflict with esp32 files) - memset(sys_position, 0, sizeof(sys_position)); // Clear machine position. - - machine_init(); // user supplied function for special initialization - // Initialize system state. if (sys.state != State::ConfigAlarm) { if (FORCE_INITIALIZATION_ALARM) { @@ -166,6 +168,7 @@ static void reset_variables() { } void run_once() { + static int tries = 0; try { reset_variables(); // Start Grbl main loop. Processes program inputs and executes them. @@ -188,6 +191,9 @@ void run_once() { error_all("Critical error in run_once: %s", ex.msg.c_str()); sys.state = State::ConfigAlarm; } + if (++tries > 1) { + while (1) {} + } // This is inside a loop in Grbl_Esp32.ino } diff --git a/Grbl_Esp32/src/Motors/StandardStepper.h b/Grbl_Esp32/src/Motors/StandardStepper.h index a733f1b3..f1a391e6 100644 --- a/Grbl_Esp32/src/Motors/StandardStepper.h +++ b/Grbl_Esp32/src/Motors/StandardStepper.h @@ -1,5 +1,7 @@ #pragma once +#include "../Machine/MachineConfig.h" // config->_stepType +#include "../Stepper.h" // ST_I2S_* #include "Motor.h" #include @@ -33,8 +35,11 @@ namespace Motors { // Configuration handlers: void validate() const override { - Assert(!_step_pin.undefined(), "Step pin should be configured."); - Assert(!_dir_pin.undefined(), "Direction pin should be configured."); + Assert(_step_pin.defined(), "Step pin should be configured."); + Assert(_dir_pin.defined(), "Direction pin should be configured."); + bool isI2SO = config->_stepType == ST_I2S_STREAM || config->_stepType == ST_I2S_STATIC; + Assert(!isI2SO || _step_pin.name().startsWith("I2SO"), "Step pin must be an I2SO pin"); + Assert(!isI2SO || _dir_pin.name().startsWith("I2SO"), "Direction pin must be an I2SO pin"); } void group(Configuration::HandlerBase& handler) override { diff --git a/Grbl_Esp32/src/Pins/GPIOPinDetail.cpp b/Grbl_Esp32/src/Pins/GPIOPinDetail.cpp index ecf29c44..ee6cfa10 100644 --- a/Grbl_Esp32/src/Pins/GPIOPinDetail.cpp +++ b/Grbl_Esp32/src/Pins/GPIOPinDetail.cpp @@ -21,6 +21,7 @@ #include "GPIOPinDetail.h" #include "../Assert.h" +#include "../Logging.h" extern "C" void __pinMode(uint8_t pin, uint8_t mode); extern "C" int __digitalRead(uint8_t pin); @@ -133,7 +134,10 @@ namespace Pins { PinCapabilities GPIOPinDetail::capabilities() const { return _capabilities; } void GPIOPinDetail::write(int high) { - Assert(_attributes.has(PinAttributes::Output), "Pin has no output attribute defined. Cannot write to it."); + if (!_attributes.has(PinAttributes::Output)) { + log_error(toString()); + } + Assert(_attributes.has(PinAttributes::Output), "Pin %s cannot be written", toString()); int value = _readWriteMask ^ high; __digitalWrite(_index, value); } @@ -149,9 +153,11 @@ namespace Pins { // Check the attributes first: Assert(value.validateWith(this->_capabilities) || _index == 1 || _index == 3, - "The requested attributes don't match the pin capabilities"); + "The requested attributes don't match the capabilities for %s", + toString()); Assert(!_attributes.conflictsWith(value) || _index == 1 || _index == 3, - "Attributes on this pin have been set before, and there's a conflict."); + "The requested attributes on %s conflict with previous settings", + toString()); _attributes = _attributes | value; @@ -180,14 +186,12 @@ namespace Pins { } void GPIOPinDetail::attachInterrupt(void (*callback)(void*), void* arg, int mode) { - Assert(_attributes.has(PinAttributes::ISR), - "Pin has no ISR attribute, which means 'setAttr' was not set, or the pin doesn't support ISR's. Cannot bind ISR."); + Assert(_attributes.has(PinAttributes::ISR), "Pin %s does not support interrupts", toString()); ::attachInterruptArg(_index, callback, arg, mode); } void GPIOPinDetail::detachInterrupt() { - Assert(_attributes.has(PinAttributes::ISR), - "Pin has no ISR attribute, which means 'setAttr' was not set, or the pin doesn't support ISR's. Cannot unbind ISR."); + Assert(_attributes.has(PinAttributes::ISR), "Pin %s does not support interrupts"); ::detachInterrupt(_index); }