Skip to content

Commit

Permalink
Merge pull request #17 from bdring/Devt
Browse files Browse the repository at this point in the history
Devt
  • Loading branch information
bdring authored Sep 16, 2021
2 parents 3181d6b + 994ab09 commit 33bacf5
Show file tree
Hide file tree
Showing 25 changed files with 325 additions and 345 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;
};
}
15 changes: 10 additions & 5 deletions FluidNC/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "MotionControl.h"
#include "Platform.h"

#include "WebUI/TelnetServer.h"
#include "WebUI/Serial2Socket.h"
#include "WebUI/InputBuffer.h"

#ifdef ENABLE_WIFI
Expand All @@ -38,6 +40,10 @@ void setup() {
WiFi.mode(WIFI_OFF);
#endif

// Setup input polling loop after loading the configuration,
// because the polling may depend on the config
client_init();

display_init();

// Load settings from non-volatile storage
Expand All @@ -53,10 +59,6 @@ void setup() {
bool configOkay = config->load(config_filename->get());
make_user_commands();

// Setup input polling loop after loading the configuration,
// because the polling may depend on the config
client_init();

if (configOkay) {
log_info("Machine " << config->_name);
log_info("Board " << config->_board);
Expand Down Expand Up @@ -119,16 +121,19 @@ void setup() {

#ifdef ENABLE_WIFI
WebUI::wifi_config.begin();
register_client(&WebUI::Serial2Socket);
register_client(&WebUI::telnet_server);
#endif
#ifdef ENABLE_BLUETOOTH
if (config->_comms->_bluetoothConfig) {
config->_comms->_bluetoothConfig->begin();
register_client(&WebUI::SerialBT);
}
#endif
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
Loading

0 comments on commit 33bacf5

Please sign in to comment.