From 579cbcc76382204c981f1f078b97d0d87df24aa6 Mon Sep 17 00:00:00 2001 From: Stefan de Bruijn Date: Sat, 19 Jun 2021 22:52:21 +0200 Subject: [PATCH] Fixed SPI and SD initialization. --- Grbl_Esp32/src/Grbl.cpp | 13 ++++++++++++- Grbl_Esp32/src/MachineCommon.h | 13 ++----------- Grbl_Esp32/src/MachineConfig.cpp | 11 +++++++++++ Grbl_Esp32/src/MachineConfig.h | 4 +++- Grbl_Esp32/src/SDCard.cpp | 11 ++++------- Grbl_Esp32/src/System.cpp | 5 ----- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Grbl_Esp32/src/Grbl.cpp b/Grbl_Esp32/src/Grbl.cpp index cabcc738..1cdb74b7 100644 --- a/Grbl_Esp32/src/Grbl.cpp +++ b/Grbl_Esp32/src/Grbl.cpp @@ -61,6 +61,16 @@ void grbl_init() { // 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 @@ -74,7 +84,8 @@ void grbl_init() { memset(sys_position, 0, sizeof(sys_position)); // Clear machine position. machine_init(); // user supplied function for special initialization - // Initialize system state. + + // Initialize system state. if (sys.state != State::ConfigAlarm) { #ifdef FORCE_INITIALIZATION_ALARM // Force Grbl into an ALARM state upon a power-cycle or hard reset. diff --git a/Grbl_Esp32/src/MachineCommon.h b/Grbl_Esp32/src/MachineCommon.h index 0cef956d..d6cd8cb2 100644 --- a/Grbl_Esp32/src/MachineCommon.h +++ b/Grbl_Esp32/src/MachineCommon.h @@ -3,18 +3,9 @@ // Grbl setting that are common to all machines // It should not be necessary to change anything herein -#ifndef GRBL_SPI_FREQ -// You can override these by defining them in a board file. -// To override, you must set all of them -//-1 means use the default board pin -# define GRBL_SPI_SS -1 -# define GRBL_SPI_MOSI -1 -# define GRBL_SPI_MISO -1 -# define GRBL_SPI_SCK -1 -# define GRBL_SPI_FREQ 4000000 -#endif +const int32_t GRBL_SPI_FREQ = 4000000; -// ESP32 CPU Settings + // ESP32 CPU Settings const uint32_t fTimers = 80000000; // a reference to the speed of ESP32 timers // =============== Don't change or comment these out ====================== diff --git a/Grbl_Esp32/src/MachineConfig.cpp b/Grbl_Esp32/src/MachineConfig.cpp index 18f0879f..a32f54d2 100644 --- a/Grbl_Esp32/src/MachineConfig.cpp +++ b/Grbl_Esp32/src/MachineConfig.cpp @@ -362,6 +362,17 @@ void SPIBus::validate() const { } } +void SPIBus::init() { + if (_ss.defined()) { // validation ensures the rest is also defined. + auto ssPin = _ss.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native); + auto mosiPin = _mosi.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native); + auto sckPin = _sck.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native); + auto misoPin = _miso.getNative(Pin::Capabilities::Input | Pin::Capabilities::Native); + + SPI.begin(sckPin, misoPin, mosiPin, ssPin); + } +} + void SPIBus::group(Configuration::HandlerBase& handler) { handler.item("ss", _ss); handler.item("miso", _miso); diff --git a/Grbl_Esp32/src/MachineConfig.h b/Grbl_Esp32/src/MachineConfig.h index b677cf1a..949222c3 100644 --- a/Grbl_Esp32/src/MachineConfig.h +++ b/Grbl_Esp32/src/MachineConfig.h @@ -203,6 +203,8 @@ public: void group(Configuration::HandlerBase& handler) override; void afterParse() override; + void init(); + ~SPIBus() = default; }; @@ -370,7 +372,7 @@ public: // before initialization. If it detects a problem and the hard limits setting is enabled, Grbl will // simply message the user to check the limits and enter an alarm state, rather than idle. Grbl will // not throw an alarm message. - bool _checkLimitsAtInit = true; + bool _checkLimitsAtInit = true; // If your machine has two limits switches wired in parallel to one axis, you will need to enable // this feature. Since the two switches are sharing a single pin, there is no way for Grbl to tell diff --git a/Grbl_Esp32/src/SDCard.cpp b/Grbl_Esp32/src/SDCard.cpp index 548cfcc4..3927d2a2 100644 --- a/Grbl_Esp32/src/SDCard.cpp +++ b/Grbl_Esp32/src/SDCard.cpp @@ -142,12 +142,7 @@ SDCard::State SDCard::get_state(bool refresh) { if (spiConfig != nullptr) { auto ssPin = spiConfig->_ss.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native); - auto mosiPin = spiConfig->_mosi.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native); - auto sckPin = spiConfig->_sck.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native); - auto misoPin = spiConfig->_miso.getNative(Pin::Capabilities::Input | Pin::Capabilities::Native); - - SPI.begin(sckPin, misoPin, mosiPin, ssPin); // this will get called for each motor, but does not seem to hurt anything - + //no need to go further if SD detect is not correct if (config->_sdCard->_cardDetect.defined() && !config->_sdCard->_cardDetect.read()) { _state = SDCard::State::NotPresent; @@ -164,10 +159,12 @@ SDCard::State SDCard::get_state(bool refresh) { //SD is idle or not detected, let see if still the case SD.end(); + _state = SDCard::State::NotPresent; + //using default value for speed ? should be parameter //refresh content if card was removed - if (SD.begin((GRBL_SPI_SS == -1) ? SS : GRBL_SPI_SS, SPI, GRBL_SPI_FREQ, "/sd", 2)) { + if (SD.begin(ssPin, SPI, GRBL_SPI_FREQ, "/sd", 2)) { if (SD.cardSize() > 0) { _state = SDCard::State::Idle; } diff --git a/Grbl_Esp32/src/System.cpp b/Grbl_Esp32/src/System.cpp index 8045e546..c4a08b01 100644 --- a/Grbl_Esp32/src/System.cpp +++ b/Grbl_Esp32/src/System.cpp @@ -69,11 +69,6 @@ void system_reset() { } void init_output_pins() { - //customize pin definition if needed -#if (GRBL_SPI_SS != -1) || (GRBL_SPI_MISO != -1) || (GRBL_SPI_MOSI != -1) || (GRBL_SPI_SCK != -1) - SPI.begin(GRBL_SPI_SCK, GRBL_SPI_MISO, GRBL_SPI_MOSI, GRBL_SPI_SS); -#endif - auto userOutputs = config->_userOutputs; // Setup M62,M63,M64,M65 pins