From a0e5e5dcab2d3ade90ddb324b657c744e88b66b1 Mon Sep 17 00:00:00 2001
From: Kelvin Ly <kelvin.ly1618@gmail.com>
Date: Thu, 23 Sep 2021 22:13:48 -0400
Subject: [PATCH] Use normal TMC2590 configuration, separate enable pins,
 disable FTP because there's no SD card right now

---
 grblHAL_Teensy4/src/init_tmc2590.c   | 33 ++++++++++++++++++++++++----
 grblHAL_Teensy4/src/init_tmc2590.h   |  2 +-
 grblHAL_Teensy4/src/my_machine.h     |  4 ++--
 grblHAL_Teensy4/src/my_machine_map.h |  4 +++-
 4 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/grblHAL_Teensy4/src/init_tmc2590.c b/grblHAL_Teensy4/src/init_tmc2590.c
index b10f8e1..d511f81 100644
--- a/grblHAL_Teensy4/src/init_tmc2590.c
+++ b/grblHAL_Teensy4/src/init_tmc2590.c
@@ -1,6 +1,7 @@
 #include "init_tmc2590.h"
 
 #include "grbl/hal.h"
+#include "grbl/protocol.h"
 
 #define TMC_SCK_PIN (27)
 #define TMC_CSN_PIN (28)
@@ -30,8 +31,14 @@ const static struct tmc2590_config {
     { DRVCONF, REPEAT3(SLPH(3)|SLPL(3)|SLP2(0)|VSENSE(1)|EN_PFD(1)|EN_S2VS(1)) },
     // no step interpolation
     // rising edge only
-    // 1/64 microstepping
-    { DRVCTRL_SDOFF0, REPEAT3(MRES(2)) }
+    // 1/16 microstepping
+    { DRVCTRL_SDOFF0, REPEAT3(MRES(4)) }
+  /*
+    {CHOPCONF, REPEAT3(0x901b4)},
+    {SGCSCONF, REPEAT3(0xd001f)},
+    {DRVCONF, REPEAT3(0xef013)},
+    {DRVCTRL_SDOFF0, REPEAT3(0x0000)},
+  */
 };
 
 static uint32_t tmc2590_mask_addr(uint32_t v, enum tmc2590_reg reg) {
@@ -63,6 +70,8 @@ static uint32_t tmc2590_mask_addr(uint32_t v, enum tmc2590_reg reg) {
 }
 
 static void tmc2590_csn_enable(void) {
+  digitalWrite(TMC_CSN_PIN, HIGH);
+  delayMicroseconds(1);
   digitalWrite(TMC_SCK_PIN, HIGH);
   delayMicroseconds(TMC2590_TCC_US);
 
@@ -79,12 +88,12 @@ static void tmc2590_csn_disable(void) {
 }
 
 static void tmc2590_send_bit(uint32_t bit) {
-  digitalWrite(TMC_SCK_PIN, LOW);
   if (bit) {
     digitalWrite(TMC_SDI_PIN, HIGH);
   } else {
     digitalWrite(TMC_SDI_PIN, LOW);
   }
+  digitalWrite(TMC_SCK_PIN, LOW);
   delayMicroseconds(TMC2590_CLK_US);
   digitalWrite(TMC_SCK_PIN, HIGH);
   delayMicroseconds(TMC2590_CLK_US);
@@ -103,14 +112,26 @@ static void tmc2590_write_reg(const struct tmc2590_config* cfg) {
   tmc2590_csn_disable();
 }
 
+static void test_message(uint_fast16_t state) {
+  report_message("TMC inited", Message_Warning);
+}
+
+
 static void tmc2590_init(void) {
+  digitalWrite(TMC_SCK_PIN, HIGH);
+  digitalWrite(TMC_SDI_PIN, HIGH);
+  digitalWrite(TMC_CSN_PIN, HIGH);
+
   pinMode(TMC_SCK_PIN, OUTPUT);
   pinMode(TMC_SDI_PIN, OUTPUT);
   pinMode(TMC_CSN_PIN, OUTPUT);
+  delayMicroseconds(1);
 
   for (int i = 0; i < sizeof(tmc_config)/sizeof(tmc_config[0]); i++) {
     tmc2590_write_reg(&tmc_config[i]);
   }
+
+  protocol_enqueue_rt_command(test_message);
 }
 
 static driver_reset_ptr on_reset;
@@ -120,11 +141,15 @@ static void tmc2590_on_reset(void) {
   on_reset();
 }
 
-void my_plugin(void) {
+void my_plugin_init(void) {
+  // 100 ms delay to let the stepper controllers get situated
+  delay(100);
   // TODO add message
   tmc2590_init();
 
   // hook into soft reset
   on_reset = hal.driver_reset;
   hal.driver_reset = tmc2590_on_reset;
+
+  //protocol_enqueue_rt_command(test_message);
 }
diff --git a/grblHAL_Teensy4/src/init_tmc2590.h b/grblHAL_Teensy4/src/init_tmc2590.h
index e371f7e..9dda115 100644
--- a/grblHAL_Teensy4/src/init_tmc2590.h
+++ b/grblHAL_Teensy4/src/init_tmc2590.h
@@ -72,7 +72,7 @@ enum tmc2590_reg {
 #define SEMIN_SIZE    (4)
 
 #define SEIMIN_SHIFT  (15)
-#define SEDN_SHIFT    (2)
+#define SEDN_SHIFT    (13)
 #define SEMAX_SHIFT   (8)
 #define SEUP_SHIFT    (5)
 #define SEMIN_SHIFT   (0)
diff --git a/grblHAL_Teensy4/src/my_machine.h b/grblHAL_Teensy4/src/my_machine.h
index 307c7d0..26b7c12 100644
--- a/grblHAL_Teensy4/src/my_machine.h
+++ b/grblHAL_Teensy4/src/my_machine.h
@@ -54,7 +54,7 @@ N_AXIS has a default value of 3, edit grbl\config.h to increase.
 //#define BLUETOOTH_ENABLE    1 // Set to 1 for HC-05 module. Requires Bluetooth plugin.
 //#define SPINDLE_HUANYANG    1 // Set to 1 or 2 for Huanyang VFD spindle. Requires spindle plugin.
 //#define QEI_ENABLE          1 // Enable quadrature encoder interfaces. Max value is 1. Requires encoder plugin.
-//#define ETHERNET_ENABLE     1 // Ethernet streaming. Requires networking plugin.
+#define ETHERNET_ENABLE     1 // Ethernet streaming. Requires networking plugin.
 //#define SDCARD_ENABLE       1 // Run gcode programs from SD card, requires sdcard plugin.
 //#define KEYPAD_ENABLE       1 // I2C keypad for jogging etc., requires keypad plugin.
 //#define PLASMA_ENABLE       1 // Plasma/THC plugin. To be completed.
@@ -87,7 +87,7 @@ N_AXIS has a default value of 3, edit grbl\config.h to increase.
 
 #if ETHERNET_ENABLE > 0
 #define TELNET_ENABLE           1 // Telnet daemon - requires Ethernet streaming enabled.
-#define FTP_ENABLE              1 // Ftp daemon - requires SD card enabled.
+#define FTP_ENABLE              0 // Ftp daemon - requires SD card enabled.
 #define WEBSOCKET_ENABLE        1 // Websocket daemon - requires Ethernet streaming enabled.
 #define NETWORK_HOSTNAME        "GRBL"
 #define NETWORK_IPMODE          1 // 0 = static, 1 = DHCP, 2 = AutoIP
diff --git a/grblHAL_Teensy4/src/my_machine_map.h b/grblHAL_Teensy4/src/my_machine_map.h
index dae9e3f..b4c98bb 100644
--- a/grblHAL_Teensy4/src/my_machine_map.h
+++ b/grblHAL_Teensy4/src/my_machine_map.h
@@ -25,7 +25,9 @@
 #define Z_DIRECTION_PIN     (7u)
 
 // Define stepper driver enable/disable output pin(s).
-#define STEPPERS_ENABLE_PIN (10u)
+#define X_ENABLE_PIN (10u)
+#define Y_ENABLE_PIN (40u)
+#define Z_ENABLE_PIN (39u)
 
 // Define homing/hard limit switch input pins.
 #define X_LIMIT_PIN         (20u)