Skip to content

Commit

Permalink
Merge pull request #16 from bdring/SD_Card_cs
Browse files Browse the repository at this point in the history
Move SD cs to the sdcard: section
  • Loading branch information
bdring authored Sep 16, 2021
2 parents af40e77 + 12cf000 commit 994ab09
Show file tree
Hide file tree
Showing 23 changed files with 301 additions and 319 deletions.
2 changes: 1 addition & 1 deletion FluidNC/src/Configuration/ParserHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace Configuration {
}

if (_parser.token_.state == TokenState::Matching) {
log_error("Ignored key " << _parser.key().str());
log_warn("Ignored key " << _parser.key().str());
}
#ifdef DEBUG_CHATTY_YAML_PARSER
if (_parser.token_.state == Configuration::TokenState::Matched) {
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/Machine/MachineConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ namespace Machine {
handler.section("axes", _axes);
handler.section("i2so", _i2so);
handler.section("spi", _spi);
handler.section("sdcard", _sdCard);
handler.section("control", _control);
handler.section("coolant", _coolant);
handler.section("probe", _probe);
handler.section("comms", _comms);
handler.section("macros", _macros);

handler.section("user_outputs", _userOutputs);
handler.section("sdcard", _sdCard);
handler.item("software_debounce_ms", _softwareDebounceMs);
// TODO: Consider putting these under a gcode: hierarchy level? Or motion control?
handler.item("laser_mode", _laserMode);
Expand Down
41 changes: 20 additions & 21 deletions FluidNC/src/Machine/SPIBus.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2021 - Stefan de Bruijn
// Copyright (c) 2021 - Mitch Bradley
// Copyright (c) 2021 - Bart Dring
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file.

#include "SPIBus.h"
Expand All @@ -8,52 +9,50 @@

namespace Machine {
void SPIBus::validate() const {
if (_cs.defined() || _miso.defined() || _mosi.defined() || _sck.defined()) {
Assert(_cs.defined(), "SPI CS pin should be configured once");
if (_miso.defined() || _mosi.defined() || _sck.defined()) {
Assert(_miso.defined(), "SPI MISO pin should be configured once");
Assert(_mosi.defined(), "SPI MOSI pin should be configured once");
Assert(_sck.defined(), "SPI SCK pin should be configured once");
}
}

void SPIBus::init() {
if (_cs.defined()) { // validation ensures the rest is also defined.
log_info("SPI SCK:" << _sck.name() << " MOSI:" << _mosi.name() << " MISO:" << _miso.name() << " CS:" << _cs.name());
if (_miso.defined() || _mosi.defined() || _sck.defined()) { // validation ensures the rest is also defined.
log_info("SPI SCK:" << _sck.name() << " MOSI:" << _mosi.name() << " MISO:" << _miso.name());

_cs.setAttr(Pin::Attr::Output);

auto csPin = _cs.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);

// Start the SPI bus with the pins defined here. Once it has been started,
// those pins "stick" and subsequent attempts to restart it with defaults
// for the miso, mosi, and sck pins are ignored
SPI.begin(sckPin, misoPin, mosiPin, csPin);
SPI.begin(sckPin, misoPin, mosiPin); // CS is defined by each device
_defined = true;
} else {
_defined = false;
log_info("SPI not defined");
}
}

void SPIBus::group(Configuration::HandlerBase& handler) {
handler.item("cs", _cs);
handler.item("miso", _miso);
handler.item("mosi", _mosi);
handler.item("sck", _sck);
}

// XXX it would be nice to have some way to turn off SPI entirely
void SPIBus::afterParse() {
if (_cs.undefined()) {
_cs = Pin::create("gpio.5");
}
if (_miso.undefined()) {
_miso = Pin::create("gpio.19");
}
if (_mosi.undefined()) {
_mosi = Pin::create("gpio.23");
}
if (_sck.undefined()) {
_sck = Pin::create("gpio.18");
}
// if (_miso.undefined()) {
// _miso = Pin::create("gpio.19");
// }
// if (_mosi.undefined()) {
// _mosi = Pin::create("gpio.23");
// }
// if (_sck.undefined()) {
// _sck = Pin::create("gpio.18");
// }
}

bool SPIBus::defined() { return _defined; }
}
5 changes: 4 additions & 1 deletion FluidNC/src/Machine/SPIBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Machine {
public:
SPIBus() = default;

Pin _cs;
Pin _miso;
Pin _mosi;
Pin _sck;
Expand All @@ -22,6 +21,10 @@ namespace Machine {

void init();

bool defined();

~SPIBus() = default;
private:
bool _defined = false;
};
}
2 changes: 1 addition & 1 deletion FluidNC/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void setup() {
WebUI::inputBuffer.begin();
} catch (const AssertionFailed& ex) {
// This means something is terribly broken:
log_info("Critical error in main_init: " << ex.what());
log_error("Critical error in main_init: " << ex.what());
sys.state = State::ConfigAlarm;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
This is used for Trinamic SPI controlled stepper motor drivers.
*/

#include "TrinamicDriver.h"
#include "TrinamicSpiDriver.h"

#include "../Machine/MachineConfig.h"

#include <TMCStepper.h> // https://github.com/teemuatlut/TMCStepper
#include <atomic>

namespace MotorDrivers {
TrinamicDriver::TrinamicDriver(uint16_t driver_part_number, int8_t spi_index) :
TrinamicSpiDriver::TrinamicSpiDriver(uint16_t driver_part_number, int8_t spi_index) :
TrinamicBase(driver_part_number), _spi_index(spi_index) {}

uint8_t daisy_chain_cs = -1;

void TrinamicDriver::init() {
_has_errors = false;
void TrinamicSpiDriver::init() {
_has_errors = false;
auto spiConfig = config->_spi;

Assert(spiConfig->defined(), "SPI bus is not configured. Cannot initialize TMC driver.");

_cs_pin.setAttr(Pin::Attr::Output | Pin::Attr::InitialOn);
_cs_mapping = PinMapper(_cs_pin);
Expand Down Expand Up @@ -57,8 +60,7 @@ namespace MotorDrivers {

config_message();

auto spiConfig = config->_spi;
if (spiConfig != nullptr) {
if (spiConfig != nullptr || !spiConfig->defined()) {
auto csPin = _cs_pin.getNative(Pin::Capabilities::Output);
auto mosiPin = spiConfig->_mosi.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native);
auto sckPin = spiConfig->_sck.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native);
Expand Down Expand Up @@ -95,13 +97,12 @@ namespace MotorDrivers {
/*
This is the startup message showing the basic definition
*/
void TrinamicDriver::config_message() {
log_info(" Trinamic TMC" << _driver_part_number << " Step:" << _step_pin.name() << " Dir:" << _dir_pin.name()
<< " CS:" << _cs_pin.name() << " Disable:" << _disable_pin.name() << " Index:" << _spi_index
<< " R:" << _r_sense);
void TrinamicSpiDriver::config_message() {
log_info(" Trinamic TMC" << _driver_part_number << " Step:" << _step_pin.name() << " Dir:" << _dir_pin.name() << " CS:"
<< _cs_pin.name() << " Disable:" << _disable_pin.name() << " Index:" << _spi_index << " R:" << _r_sense);
}

bool TrinamicDriver::test() {
bool TrinamicSpiDriver::test() {
if (_has_errors) {
return false;
}
Expand Down Expand Up @@ -151,7 +152,7 @@ namespace MotorDrivers {
uint16_t run (mA)
float hold (as a percentage of run)
*/
void TrinamicDriver::read_settings() {
void TrinamicSpiDriver::read_settings() {
if (_has_errors) {
return;
}
Expand All @@ -172,7 +173,7 @@ namespace MotorDrivers {
tmcstepper->rms_current(run_i_ma, hold_i_percent);
}

bool TrinamicDriver::set_homing_mode(bool isHoming) {
bool TrinamicSpiDriver::set_homing_mode(bool isHoming) {
set_mode(isHoming);
return true;
}
Expand All @@ -182,7 +183,7 @@ namespace MotorDrivers {
Many people will want quiet and stallguard homing. Stallguard only run in
Coolstep mode, so it will need to switch to Coolstep when homing
*/
void TrinamicDriver::set_mode(bool isHoming) {
void TrinamicSpiDriver::set_mode(bool isHoming) {
if (_has_errors) {
return;
}
Expand Down Expand Up @@ -231,7 +232,7 @@ namespace MotorDrivers {
/*
This is the stallguard tuning info. It is call debug, so it could be generic across all classes.
*/
void TrinamicDriver::debug_message() {
void TrinamicSpiDriver::debug_message() {
if (_has_errors) {
return;
}
Expand Down Expand Up @@ -262,7 +263,7 @@ namespace MotorDrivers {

// this can use the enable feature over SPI. The dedicated pin must be in the enable mode,
// but that can be hardwired that way.
void IRAM_ATTR TrinamicDriver::set_disable(bool disable) {
void IRAM_ATTR TrinamicSpiDriver::set_disable(bool disable) {
if (_has_errors) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TMC2130Stepper; // Forward declaration

namespace MotorDrivers {

class TrinamicDriver : public TrinamicBase {
class TrinamicSpiDriver : public TrinamicBase {
private:
const int _spi_freq = 100000;

Expand All @@ -39,9 +39,9 @@ namespace MotorDrivers {
void config_message() override;

public:
TrinamicDriver(uint16_t driver_part_number) : TrinamicDriver(driver_part_number, -1) {}
TrinamicSpiDriver(uint16_t driver_part_number) : TrinamicSpiDriver(driver_part_number, -1) {}

TrinamicDriver(uint16_t driver_part_number, int8_t spi_index);
TrinamicSpiDriver(uint16_t driver_part_number, int8_t spi_index);

// Overrides for inherited methods
void init() override;
Expand All @@ -67,17 +67,17 @@ namespace MotorDrivers {
const char* name() const override { return "trinamic_spi"; }
};

class TMC2130 : public TrinamicDriver {
class TMC2130 : public TrinamicSpiDriver {
public:
TMC2130() : TrinamicDriver(2130) {}
TMC2130() : TrinamicSpiDriver(2130) {}

// Name of the configurable. Must match the name registered in the cpp file.
const char* name() const override { return "tmc_2130"; }
};

class TMC5160 : public TrinamicDriver {
class TMC5160 : public TrinamicSpiDriver {
public:
TMC5160() : TrinamicDriver(5160) {}
TMC5160() : TrinamicSpiDriver(5160) {}

// Name of the configurable. Must match the name registered in the cpp file.
const char* name() const override { return "tmc_5160"; }
Expand Down
16 changes: 15 additions & 1 deletion FluidNC/src/Report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,21 @@ void report_feedback_message(Message message) { // ok to send to all clients
#include "Uart.h"
// Welcome message
void report_init_message(Print& client) {
client << "\r\nGrbl " << GRBL_VERSION << " [FluidNC " << GIT_TAG << GIT_REV << " '$' for help]\n";
client << "\r\nGrbl " << GRBL_VERSION << " [FluidNC " << GIT_TAG << GIT_REV << " (";

#if defined(ENABLE_WIFI) || defined(ENABLE_BLUETOOTH)
# ifdef ENABLE_WIFI
client << "wifi";
# endif

# ifdef ENABLE_BLUETOOTH
client << "bt";
# endif
#else
client << "noradio";
#endif

client << ") '$' for help]\n";
}

// Prints current probe parameters. Upon a probe command, these parameters are updated upon a
Expand Down
32 changes: 26 additions & 6 deletions FluidNC/src/SDCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ uint32_t SDCard::lineNumber() {
SDCard::State SDCard::test_or_open(bool refresh) {
auto spiConfig = config->_spi;

if (spiConfig == nullptr) {
if (spiConfig == nullptr || !spiConfig->defined()) {
//log_debug("SPI not defined");
return SDCard::State::NotPresent;
}

auto csPin = spiConfig->_cs.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native);
if (spiConfig == nullptr || _cs.undefined()) {
//log_debug("SD cs not defined");
return SDCard::State::NotPresent;
}

//no need to go further if SD detect is not correct
if (config->_sdCard->_cardDetect.defined() && !config->_sdCard->_cardDetect.read()) {
if (_cardDetect.defined() && !_cardDetect.read()) {
_state = SDCard::State::NotPresent;
return _state;
}
Expand All @@ -149,6 +153,8 @@ SDCard::State SDCard::test_or_open(bool refresh) {

_state = SDCard::State::NotPresent;

auto csPin = _cs.getNative(Pin::Capabilities::Output | Pin::Capabilities::Native);

//refresh content if card was removed
if (SD.begin(csPin, SPI, SPIfreq, "/sd", 2)) {
if (SD.cardSize() > 0) {
Expand Down Expand Up @@ -182,14 +188,28 @@ const char* SDCard::filename() {
void SDCard::init() {
static bool init_message = true; // used to show messages only once.

if (init_message) {
_cardDetect.report("SD Card Detect");
init_message = false;
if (_cs.defined()) {
if (!config->_spi->defined()) {
log_error("SD needs SPI defined");
} else {
if (init_message) {
_cardDetect.report("SD Card Detect");
init_message = false;
}
log_info("SD Card cs:" << _cs.name() << " dectect:" << _cardDetect.name());
}
}

_cs.setAttr(Pin::Attr::Output);
_cardDetect.setAttr(Pin::Attr::Output);
}

void SDCard::afterParse() {
// if (_cs.undefined()) {
// _cs = Pin::create("gpio.5");
// }
}

SDCard::~SDCard() {
delete _pImpl;
}
7 changes: 6 additions & 1 deletion FluidNC/src/SDCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SDCard : public Configuration::Configurable {

State _state;
Pin _cardDetect;
Pin _cs;
SDCard::State test_or_open(bool refresh);
Print& _client;
WebUI::AuthenticationLevel _auth_level;
Expand All @@ -78,6 +79,7 @@ class SDCard : public Configuration::Configurable {
Error readFileLine(char* line, int len);
float report_perc_complete();
uint32_t lineNumber();
void afterParse() override;

Print& getClient() { return _client; }
WebUI::AuthenticationLevel getAuthLevel() { return _auth_level; }
Expand All @@ -88,7 +90,10 @@ class SDCard : public Configuration::Configurable {
void init();

// Configuration handlers.
void group(Configuration::HandlerBase& handler) override { handler.item("card_detect", _cardDetect); }
void group(Configuration::HandlerBase& handler) override {
handler.item("cs", _cs);
handler.item("card_detect", _cardDetect);
}

~SDCard();
};
Loading

0 comments on commit 994ab09

Please sign in to comment.