updates
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 3 Jan 2016 07:58:15 +0000 (23:58 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 3 Jan 2016 07:58:15 +0000 (23:58 -0800)
src/canonical_machine.c
src/hardware.h
src/main.c
src/settings/settings_default.h
src/stepper.c
src/tmc2660.c
src/tmc2660.h

index cc82c0f56a22d5bcb9dc87d1b36edb03b2b5a6c7..29a650d0e9f5510bd0d8bf64a9544ab41b838296 100644 (file)
@@ -189,16 +189,19 @@ void cm_set_spindle_mode(GCodeState_t *gcode_state, uint8_t spindle_mode) {gcode
 void cm_set_spindle_speed_parameter(GCodeState_t *gcode_state, float speed) {gcode_state->spindle_speed = speed;}
 void cm_set_tool_number(GCodeState_t *gcode_state, uint8_t tool) {gcode_state->tool = tool;}
 
+
 void cm_set_absolute_override(GCodeState_t *gcode_state, uint8_t absolute_override) {
   gcode_state->absolute_override = absolute_override;
   cm_set_work_offsets(MODEL);                // must reset offsets if you change absolute override
 }
 
+
 void cm_set_model_linenum(uint32_t linenum) {
   cm.gm.linenum = linenum;                // you must first set the model line number,
   nv_add_object((const char_t *)"n");    // then add the line number to the nv list
 }
 
+
 /***********************************************************************************
  * COORDINATE SYSTEMS AND OFFSETS
  * Functions to get, set and report coordinate systems and work offsets
index 941418a207d63c50f5ac8b0519c9f309e5140c26..24a6ae32e4f66bd71c138dff4729dc91414d1fa5 100644 (file)
@@ -41,9 +41,9 @@
  *    LO    Real time clock interrupt            (set in xmega_rtc.h)
  *
  *    (*) The TX cannot run at LO level or exception reports and other prints
- *        called from a LO interrupt (as in prep_line()) will kill the system in a
- *        permanent sleep_mode() call in usart_putc() (usart.c) as no interrupt
- *        can release the sleep mode.
+ *        called from a LO interrupt (as in prep_line()) will kill the system
+ *        in a permanent sleep_mode() call in usart_putc() (usart.c) as no
+ *        interrupt can release the sleep mode.
  */
 
 #ifndef HARDWARE_H_ONCE
 
 enum hwPlatform {
   HM_PLATFORM_NONE = 0,
-  HW_PLATFORM_TINYG_XMEGA,    // TinyG code base on Xmega boards.
+  HW_PLATFORM_TINYG_XMEGA,       // TinyG code base on Xmega boards.
 };
 
 #define HW_VERSION_TINYGV6 6
 #define HW_VERSION_TINYGV7 7
 #define HW_VERSION_TINYGV8 8
 
-#include "config.h"                        // needed for the stat_t typedef
+#include "config.h"              // needed for the stat_t typedef
 #include <avr/interrupt.h>
-#include "xmega/xmega_rtc.h"            // Xmega only. Goes away with RTC refactoring
+#include "xmega/xmega_rtc.h"
 
-#define MILLISECONDS_PER_TICK 1            // MS for system tick (systick * N)
-#define SYS_ID_LEN 12                    // length of system ID string from sys_get_id()
+#define MILLISECONDS_PER_TICK 1  // MS for system tick (systick * N)
+#define SYS_ID_LEN 12            // length of system ID string from sys_get_id()
 
 
 // Clock Crystal Config. Pick one:
-//#define __CLOCK_INTERNAL_32MHZ TRUE    // use internal oscillator
-//#define __CLOCK_EXTERNAL_8MHZ    TRUE    // uses PLL to provide 32 MHz system clock
-#define __CLOCK_EXTERNAL_16MHZ TRUE        // uses PLL to provide 32 MHz system clock
-
-/*** Motor, output bit & switch port assignments ***
- *** These are not all the same, and must line up in multiple places in gpio.h ***
- * Sorry if this is confusing - it's a board routing issue
- */
-#define PORT_MOTOR_1     PORTA            // motors mapped to ports
+//#define __CLOCK_INTERNAL_32MHZ TRUE // use internal oscillator
+//#define __CLOCK_EXTERNAL_8MHZ TRUE  // uses PLL to provide 32 MHz system clock
+#define __CLOCK_EXTERNAL_16MHZ TRUE   // uses PLL to provide 32 MHz system clock
+
+// Motor, output bit & switch port assignments
+// These are not all the same, and must line up in multiple places in gpio.h
+// Sorry if this is confusing - it's a board routing issue
+#define PORT_MOTOR_1     PORTA        // motors mapped to ports
 #define PORT_MOTOR_2     PORTF
 #define PORT_MOTOR_3     PORTE
 #define PORT_MOTOR_4     PORTD
 
-#define PORT_SWITCH_X    PORTA            // Switch axes mapped to ports
+#define PORT_SWITCH_X    PORTA        // Switch axes mapped to ports
 #define PORT_SWITCH_Y    PORTD
 #define PORT_SWITCH_Z    PORTE
 #define PORT_SWITCH_A    PORTF
 
-#define PORT_OUT_V7_X    PORTA            // v7 mapping - Output bits mapped to ports
+#define PORT_OUT_V7_X    PORTA        // v7 mapping
 #define PORT_OUT_V7_Y    PORTF
 #define PORT_OUT_V7_Z    PORTD
 #define PORT_OUT_V7_A    PORTE
 
-#define PORT_OUT_V6_X    PORTA            // v6 and earlier mapping - Output bits mapped to ports
-#define PORT_OUT_V6_Y    PORTF
-#define PORT_OUT_V6_Z    PORTE
-#define PORT_OUT_V6_A    PORTD
-
 // These next four must be changed when the PORT_MOTOR_* definitions change!
 #define PORTCFG_VP0MAP_PORT_MOTOR_1_gc PORTCFG_VP02MAP_PORTA_gc
 #define PORTCFG_VP1MAP_PORT_MOTOR_2_gc PORTCFG_VP13MAP_PORTF_gc
@@ -114,10 +108,11 @@ enum hwPlatform {
  *    b3    (out) chip select
  *    b4    (in)  fault
  *    b5    (out) output bit for GPIO port1
- *    b6    (in)  min limit switch on GPIO 2 (note: motor controls and GPIO2 port mappings are not the same)
- *    b7    (in)  max limit switch on GPIO 2 (note: motor controls and GPIO2 port mappings are not the same)
+ *    b6    (in)  min limit switch on GPIO 2 *
+ *    b7    (in)  max limit switch on GPIO 2 *
+ *  * motor controls and GPIO2 port mappings are not the same
  */
-#define MOTOR_PORT_DIR_gm 0x3F    // dir settings: lower 6 out, upper 2 in
+#define MOTOR_PORT_DIR_gm 0x2f // pin dir settings
 
 enum cfgPortBits {        // motor control port bit positions
   STEP_BIT_bp = 0,        // bit 0
@@ -135,27 +130,26 @@ enum cfgPortBits {        // motor control port bit positions
 #define MOTOR_ENABLE_BIT_bm   (1 << MOTOR_ENABLE_BIT_bp)
 #define CHIP_SELECT_BIT_bm    (1 << CHIP_SELECT_BIT_bp)
 #define FAULT_BIT_bm          (1 << FAULT_BIT_bp)
-#define GPIO1_OUT_BIT_bm      (1 << GPIO1_OUT_BIT_bp)    // spindle and coolant output bits
-#define SW_MIN_BIT_bm         (1 << SW_MIN_BIT_bp)       // minimum switch inputs
-#define SW_MAX_BIT_bm         (1 << SW_MAX_BIT_bp)       // maximum switch inputs
-
-/* Bit assignments for GPIO1_OUTs for spindle, PWM and coolant */
+#define GPIO1_OUT_BIT_bm      (1 << GPIO1_OUT_BIT_bp)   // spindle and coolant
+#define SW_MIN_BIT_bm         (1 << SW_MIN_BIT_bp)      // minimum switch inputs
+#define SW_MAX_BIT_bm         (1 << SW_MAX_BIT_bp)      // maximum switch inputs
 
+// Bit assignments for GPIO1_OUTs for spindle, PWM and coolant
 #define SPINDLE_BIT            0x08        // spindle on/off
 #define SPINDLE_DIR            0x04        // spindle direction, 1=CW, 0=CCW
 #define SPINDLE_PWM            0x02        // spindle PWMs output bit
-#define MIST_COOLANT_BIT       0x01        // coolant on/off - these are the same due to limited ports
-#define FLOOD_COOLANT_BIT      0x01        // coolant on/off
+#define MIST_COOLANT_BIT       0x01        // coolant on/off (same as flood)
+#define FLOOD_COOLANT_BIT      0x01        // coolant on/off (same as mist)
 
 #define SPINDLE_LED            0
 #define SPINDLE_DIR_LED        1
 #define SPINDLE_PWM_LED        2
 #define COOLANT_LED            3
 
-#define INDICATOR_LED        SPINDLE_DIR_LED    // can use the spindle direction as an indicator LED
-
-/* Timer assignments - see specific modules for details) */
+// Can use the spindle direction as an indicator LED
+#define INDICATOR_LED        SPINDLE_DIR_LED
 
+// Timer assignments - see specific modules for details)
 #define TIMER_DDA            TCC0        // DDA timer     (see stepper.h)
 #define TIMER_DWELL          TCD0        // Dwell timer   (see stepper.h)
 #define TIMER_LOAD           TCE0        // Loader time   (see stepper.h)
@@ -164,8 +158,7 @@ enum cfgPortBits {        // motor control port bit positions
 #define TIMER_PWM1           TCD1        // PWM timer #1  (see pwm.c)
 #define TIMER_PWM2           TCE1        // PWM timer #2  (see pwm.c)
 
-/* Timer setup for stepper and dwells */
-
+// Timer setup for stepper and dwells
 #define FREQUENCY_DDA          (float)50000    // DDA frequency in hz.
 #define FREQUENCY_DWELL        (float)10000    // Dwell count frequency in hz.
 #define LOAD_TIMER_PERIOD      100             // cycles you have to shut off SW interrupt
@@ -200,27 +193,30 @@ enum cfgPortBits {        // motor control port bit positions
 #define TIMER_EXEC_INTLVL      TIMER_OVFINTLVL_LO
 
 
-/**** Device singleton - global structure to allow iteration through similar devices ****/
 /*
+  Device singleton - global structure to allow iteration through similar devices
+
   Ports are shared between steppers and GPIO so we need a global struct.
   Each xmega port has 3 bindings; motors, switches and the output bit
 
   The initialization sequence is important. the order is:
   - sys_init()    binds all ports to the device struct
-  - st_init()     sets IO directions and sets stepper VPORTS and stepper specific functions
+  - st_init()     sets IO directions and sets stepper VPORTS and stepper
+                  specific functions
 
-  Care needs to be taken in routines that use ports not to write to bits that are
-  not assigned to the designated function - ur unpredicatable results will occur
+  Care needs to be taken in routines that use ports not to write to bits that
+  are not assigned to the designated function - ur unpredicatable results will
+  occur.
 */
 
-typedef struct hmSingleton {
-  PORT_t *st_port[MOTORS];        // bindings for stepper motor ports (stepper.c)
-  PORT_t *sw_port[MOTORS];        // bindings for switch ports (GPIO2)
-  PORT_t *out_port[MOTORS];       // bindings for output ports (GPIO1)
+typedef struct {
+  PORT_t *st_port[MOTORS];       // bindings for stepper motor ports (stepper.c)
+  PORT_t *sw_port[MOTORS];       // bindings for switch ports (GPIO2)
+  PORT_t *out_port[MOTORS];      // bindings for output ports (GPIO1)
 } hwSingleton_t;
 hwSingleton_t hw;
 
-void hardware_init();             // master hardware init
+void hardware_init();            // master hardware init
 void hw_request_hard_reset();
 void hw_hard_reset();
 stat_t hw_hard_reset_handler();
index 0696c06ce320adf8ec6de69a1f5c5de52a82fca0..c2351acce1005623f08c6ad74dc4f08c608bd63f 100644 (file)
@@ -78,7 +78,7 @@ int main() {
   init();
 
   // main loop
-  while (true) controller_run();    // single pass through the controller
+  while (true) controller_run();  // single pass through the controller
 
   return 0;
 }
index 3fdc98f875fe10ad90905a628b6cf510a7fe6024..1f6ef74cf1be9fbc2e7d314f0e1be34b19145da0 100644 (file)
@@ -49,7 +49,7 @@
 #define M1_MOTOR_MAP             AXIS_X              // 1ma
 #define M1_STEP_ANGLE            1.8                 // 1sa
 #define M1_TRAVEL_PER_REV        1.25                // 1tr
-#define M1_MICROSTEPS            8                   // 1mi        1,2,4,8
+#define M1_MICROSTEPS            256                 // 1mi        1,2,4,8
 #define M1_POLARITY              0                   // 1po        0=normal, 1=reversed
 #define M1_POWER_MODE            MOTOR_POWER_MODE    // 1pm        standard
 #define M1_POWER_LEVEL           MOTOR_POWER_LEVEL   // 1mp
@@ -57,7 +57,7 @@
 #define M2_MOTOR_MAP             AXIS_Y
 #define M2_STEP_ANGLE            1.8
 #define M2_TRAVEL_PER_REV        1.25
-#define M2_MICROSTEPS            8
+#define M2_MICROSTEPS            256
 #define M2_POLARITY              0
 #define M2_POWER_MODE            MOTOR_POWER_MODE
 #define M2_POWER_LEVEL           MOTOR_POWER_LEVEL
@@ -65,7 +65,7 @@
 #define M3_MOTOR_MAP             AXIS_Z
 #define M3_STEP_ANGLE            1.8
 #define M3_TRAVEL_PER_REV        1.25
-#define M3_MICROSTEPS            8
+#define M3_MICROSTEPS            256
 #define M3_POLARITY              0
 #define M3_POWER_MODE            MOTOR_POWER_MODE
 #define M3_POWER_LEVEL           MOTOR_POWER_LEVEL
@@ -73,7 +73,7 @@
 #define M4_MOTOR_MAP             AXIS_A
 #define M4_STEP_ANGLE            1.8
 #define M4_TRAVEL_PER_REV        360            // degrees moved per motor rev
-#define M4_MICROSTEPS            8
+#define M4_MICROSTEPS            256
 #define M4_POLARITY              0
 #define M4_POWER_MODE            MOTOR_POWER_MODE
 #define M4_POWER_LEVEL           MOTOR_POWER_LEVEL
@@ -89,7 +89,7 @@
 #define M6_MOTOR_MAP             AXIS_C
 #define M6_STEP_ANGLE            1.8
 #define M6_TRAVEL_PER_REV        360            // degrees moved per motor rev
-#define M6_MICROSTEPS            8
+#define M6_MICROSTEPS            256
 #define M6_POLARITY              0
 #define M6_POWER_MODE            MOTOR_POWER_MODE
 #define M6_POWER_LEVEL           MOTOR_POWER_LEVEL
index 2270095c2ed9e40f41012883ed9caabc65825817..4d8ddbb156462a2ae2aabff62fa09f86280627b2 100644 (file)
@@ -54,7 +54,7 @@ static void _request_load_move();
 
 
 /*
- * stepper_init() - initialize stepper motor subsystem
+ * Initialize stepper motor subsystem
  *
  *    Notes:
  *      - This init requires sys_init() to be run beforehand
@@ -62,20 +62,8 @@ static void _request_load_move();
  *      - motor polarity is setup during config_init()
  *      - high level interrupts must be enabled in main() once all inits are complete
  */
-
-/*    NOTE: This is the bare code that the Motate timer calls replace.
- *    NB: requires: #include <component_tc.h>
- *
- *    REG_TC1_WPMR = 0x54494D00;            // enable write to registers
- *    TC_Configure(TC_BLOCK_DDA, TC_CHANNEL_DDA, TC_CMR_DDA);
- *    REG_RC_DDA = TC_RC_DDA;                // set frequency
- *    REG_IER_DDA = TC_IER_DDA;            // enable interrupts
- *    NVIC_EnableIRQ(TC_IRQn_DDA);
- *    pmc_enable_periph_clk(TC_ID_DDA);
- *    TC_Start(TC_BLOCK_DDA, TC_CHANNEL_DDA);
- */
 void stepper_init() {
-  memset(&st_run, 0, sizeof(st_run));            // clear all values, pointers and status
+  memset(&st_run, 0, sizeof(st_run));          // clear all values, pointers and status
   stepper_init_assertions();
 
   // Configure virtual ports
@@ -84,8 +72,8 @@ void stepper_init() {
 
   // setup ports and data structures
   for (uint8_t i = 0; i < MOTORS; i++) {
-    hw.st_port[i]->DIR = MOTOR_PORT_DIR_gm;    // sets outputs for motors & GPIO1, and GPIO2 inputs
-    hw.st_port[i]->OUT = MOTOR_ENABLE_BIT_bm;  // zero port bits AND disable motor
+    hw.st_port[i]->DIR = MOTOR_PORT_DIR_gm;      // sets outputs for motors & GPIO1, and GPIO2 inputs
+    hw.st_port[i]->OUTSET = MOTOR_ENABLE_BIT_bm; // disable motor
   }
 
   // setup DDA timer
@@ -111,22 +99,19 @@ void stepper_init() {
   TIMER_EXEC.PER = EXEC_TIMER_PERIOD;          // set period
 
   st_pre.buffer_state = PREP_BUFFER_OWNED_BY_EXEC;
-  st_reset();                                    // reset steppers to known state
+  st_reset();                                  // reset steppers to known state
 }
 
 
-/*
- * stepper_init_assertions() - test assertions, return error code if violation exists
- * stepper_test_assertions() - test assertions, return error code if violation exists
- */
 void stepper_init_assertions() {
-  st_run.magic_end = MAGICNUM;
+  st_run.magic_end   = MAGICNUM;
   st_run.magic_start = MAGICNUM;
-  st_pre.magic_end = MAGICNUM;
+  st_pre.magic_end   = MAGICNUM;
   st_pre.magic_start = MAGICNUM;
 }
 
 
+/// Test assertions, return error code if violation exists
 stat_t stepper_test_assertions() {
   if (st_run.magic_end   != MAGICNUM) return STAT_STEPPER_ASSERTION_FAILURE;
   if (st_run.magic_start != MAGICNUM) return STAT_STEPPER_ASSERTION_FAILURE;
@@ -138,7 +123,7 @@ stat_t stepper_test_assertions() {
 
 
 /*
- * st_runtime_isbusy() - return TRUE if runtime is busy:
+ * return TRUE if runtime is busy:
  *
  *    Busy conditions:
  *    - motors are running
@@ -161,13 +146,13 @@ void st_reset() {
 }
 
 
-/// Clear counters
+/// Clear diagnostic counters, reset stepper prep
 stat_t st_clc(nvObj_t *nv) {
-  // clear diagnostic counters, reset stepper prep
   st_reset();
   return STAT_OK;
 }
 
+
 /*
  * Motor power management functions
  *
@@ -186,10 +171,11 @@ static uint8_t _motor_is_enabled(uint8_t motor) {
   case (MOTOR_2): port = PORT_MOTOR_2_VPORT.OUT; break;
   case (MOTOR_3): port = PORT_MOTOR_3_VPORT.OUT; break;
   case (MOTOR_4): port = PORT_MOTOR_4_VPORT.OUT; break;
-  default: port = 0xff;    // defaults to disabled for bad motor input value
+  default: port = 0xff; // defaults to disabled for bad motor input value
   }
 
-  return port & MOTOR_ENABLE_BIT_bm ? 0 : 1;    // returns 1 if motor is enabled (motor is actually active low)
+  // returns 1 if motor is enabled (motor is actually active low)
+  return port & MOTOR_ENABLE_BIT_bm ? 0 : 1;
 }
 
 
@@ -237,11 +223,10 @@ void st_deenergize_motors() {
 
 
 /*
- * st_motor_power_callback() - callback to manage motor power sequencing
- *
- *    Handles motor power-down timing, low-power idle, and adaptive motor power
+ * Callback to manage motor power sequencing
+ * Handles motor power-down timing, low-power idle, and adaptive motor power
  */
-stat_t st_motor_power_callback() {    // called by controller
+stat_t st_motor_power_callback() { // called by controller
   // manage power for each motor individually
   for (uint8_t m = MOTOR_1; m < MOTORS; m++) {
 
@@ -265,14 +250,10 @@ stat_t st_motor_power_callback() {    // called by controller
     }
 
     // do not process countdown if in a feedhold
-    if (cm_get_combined_state() == COMBINED_HOLD) {
-      continue;
-    }
+    if (cm_get_combined_state() == COMBINED_HOLD) continue;
 
     // do not process countdown if in a feedhold
-    if (cm_get_combined_state() == COMBINED_HOLD) {
-      continue;
-    }
+    if (cm_get_combined_state() == COMBINED_HOLD) continue;
 
     // run the countdown if you are in a countdown
     if (st_run.mot[m].power_state == MOTOR_POWER_TIMEOUT_COUNTDOWN) {
@@ -321,10 +302,10 @@ ISR(TIMER_DDA_ISR_vect) {
   }
 
   // pulse stretching for using external drivers.- turn step bits off
-  PORT_MOTOR_1_VPORT.OUT &= ~STEP_BIT_bm;                // ~ 5 uSec pulse width
-  PORT_MOTOR_2_VPORT.OUT &= ~STEP_BIT_bm;                // ~ 4 uSec
-  PORT_MOTOR_3_VPORT.OUT &= ~STEP_BIT_bm;                // ~ 3 uSec
-  PORT_MOTOR_4_VPORT.OUT &= ~STEP_BIT_bm;                // ~ 2 uSec
+  PORT_MOTOR_1_VPORT.OUT &= ~STEP_BIT_bm;              // ~ 5 uSec pulse width
+  PORT_MOTOR_2_VPORT.OUT &= ~STEP_BIT_bm;              // ~ 4 uSec
+  PORT_MOTOR_3_VPORT.OUT &= ~STEP_BIT_bm;              // ~ 3 uSec
+  PORT_MOTOR_4_VPORT.OUT &= ~STEP_BIT_bm;              // ~ 2 uSec
 
   if (--st_run.dda_ticks_downcount != 0) return;
 
@@ -334,7 +315,7 @@ ISR(TIMER_DDA_ISR_vect) {
 
 
 /// DDA timer interrupt routine - service ticks from DDA timer
-ISR(TIMER_DWELL_ISR_vect) {                                // DWELL timer interrupt
+ISR(TIMER_DWELL_ISR_vect) {                            // DWELL timer interrupt
   if (--st_run.dda_ticks_downcount == 0) {
     TIMER_DWELL.CTRLA = STEP_TIMER_DISABLE;            // disable DWELL timer
     _load_move();
@@ -343,9 +324,9 @@ ISR(TIMER_DWELL_ISR_vect) {                                // DWELL timer interr
 
 
 /****************************************************************************************
- * Exec sequencing code        - computes and prepares next load segment
+ * Exec sequencing code      - computes and prepares next load segment
  * st_request_exec_move()    - SW interrupt to request to execute a move
- * exec_timer interrupt        - interrupt handler for calling exec function
+ * exec_timer interrupt      - interrupt handler for calling exec function
  */
 void st_request_exec_move() {
   if (st_pre.buffer_state == PREP_BUFFER_OWNED_BY_EXEC) {// bother interrupting
@@ -355,8 +336,8 @@ void st_request_exec_move() {
 }
 
 
-ISR(TIMER_EXEC_ISR_vect) {                                // exec move SW interrupt
-  TIMER_EXEC.CTRLA = EXEC_TIMER_DISABLE;                // disable SW interrupt timer
+ISR(TIMER_EXEC_ISR_vect) {                               // exec move SW interrupt
+  TIMER_EXEC.CTRLA = EXEC_TIMER_DISABLE;                 // disable SW interrupt timer
 
   // exec_move
   if (st_pre.buffer_state == PREP_BUFFER_OWNED_BY_EXEC) {
@@ -386,8 +367,8 @@ static void _request_load_move() {
 }
 
 
-ISR(TIMER_LOAD_ISR_vect) {                                      // load steppers SW interrupt
-  TIMER_LOAD.CTRLA = LOAD_TIMER_DISABLE;                        // disable SW interrupt timer
+ISR(TIMER_LOAD_ISR_vect) {                                   // load steppers SW interrupt
+  TIMER_LOAD.CTRLA = LOAD_TIMER_DISABLE;                     // disable SW interrupt timer
   _load_move();
 }
 
index 502d5e974bd4cb05525c78f6bd4fdb71c7b989c4..21b8c7565b41e82a041fb044d7d95d05398e5dce 100644 (file)
@@ -146,7 +146,7 @@ void spi_next() {
     spi_send();
 
     // Next state
-    spi_delay = 3;
+    spi_delay = 4;
     spi_state = SPI_STATE_READ;
     break;
 
@@ -213,42 +213,35 @@ void tmc2660_init() {
     drivers[i].state = TMC2660_STATE_CONFIG;
     drivers[i].reg = 0;
 
-    drivers[i].regs[TMC2660_DRVCTRL] = 0;
-    drivers[i].regs[TMC2660_CHOPCONF] = 0x14557;
-    drivers[i].regs[TMC2660_SMARTEN] = 0x8202;
-    drivers[i].regs[TMC2660_SGCSCONF] = 0x1001f;
-    drivers[i].regs[TMC2660_DRVCONF] = 0x10;
+    drivers[i].regs[TMC2660_DRVCTRL] = TMC2660_DRVCTRL_MRES_256;
+    //drivers[i].regs[TMC2660_CHOPCONF] = TMC2660_CHOPCONF_TBL_36 | TMC2660_CHOPCONF_HEND(0) | TMC2660_CHOPCONF_HSTART(4) |
+    //  TMC2660_CHOPCONF_TOFF(4);
+    drivers[i].regs[TMC2660_CHOPCONF] = TMC2660_CHOPCONF_TBL_36 | TMC2660_CHOPCONF_CHM | TMC2660_CHOPCONF_HEND(7) |
+      TMC2660_CHOPCONF_HSTART(6) | TMC2660_CHOPCONF_TOFF(7);
+    drivers[i].regs[TMC2660_SMARTEN] = TMC2660_SMARTEN_SEIMIN | TMC2660_SMARTEN_MAX(2) | TMC2660_SMARTEN_MIN(2);
+    drivers[i].regs[TMC2660_SGCSCONF] = TMC2660_SGCSCONF_SFILT | TMC2660_SGCSCONF_CS_NONE;
+    drivers[i].regs[TMC2660_DRVCONF] = TMC2660_DRVCONF_RDSEL_MSTEP;
   }
 
   // Setup pins
-  TMC2660_SPI_PORT.OUTSET = 1 << 4;  // High
-  TMC2660_SPI_PORT.DIRSET = 1 << 4;  // Output
+  TMC2660_SPI_PORT.OUTSET = 1 << TMC2660_SPI_SS_PIN;   // High
+  TMC2660_SPI_PORT.DIRSET = 1 << TMC2660_SPI_SS_PIN;   // Output (Why is it necessary to set the SS pin?)
   TMC2660_SPI_PORT.OUTSET = 1 << TMC2660_SPI_SCK_PIN;  // High
   TMC2660_SPI_PORT.DIRSET = 1 << TMC2660_SPI_SCK_PIN;  // Output
   TMC2660_SPI_PORT.DIRCLR = 1 << TMC2660_SPI_MISO_PIN; // Input
   TMC2660_SPI_PORT.OUTSET = 1 << TMC2660_SPI_MOSI_PIN; // High
   TMC2660_SPI_PORT.DIRSET = 1 << TMC2660_SPI_MOSI_PIN; // Output
 
-#if TMC2660_NUM_DRIVERS > 0
   TMC2660_SPI_SSX_PORT.OUTSET = 1 << TMC2660_SPI_SSX_PIN; // High
   TMC2660_SPI_SSX_PORT.DIRSET = 1 << TMC2660_SPI_SSX_PIN; // Output
-#endif
-#if TMC2660_NUM_DRIVERS > 1
   TMC2660_SPI_SSY_PORT.OUTSET = 1 << TMC2660_SPI_SSY_PIN; // High
   TMC2660_SPI_SSY_PORT.DIRSET = 1 << TMC2660_SPI_SSY_PIN; // Output
-#endif
-#if TMC2660_NUM_DRIVERS > 2
   TMC2660_SPI_SSZ_PORT.OUTSET = 1 << TMC2660_SPI_SSZ_PIN; // High
   TMC2660_SPI_SSZ_PORT.DIRSET = 1 << TMC2660_SPI_SSZ_PIN; // Output
-#endif
-#if TMC2660_NUM_DRIVERS > 3
   TMC2660_SPI_SSA_PORT.OUTSET = 1 << TMC2660_SPI_SSA_PIN; // High
   TMC2660_SPI_SSA_PORT.DIRSET = 1 << TMC2660_SPI_SSA_PIN; // Output
-#endif
-#if TMC2660_NUM_DRIVERS > 4
   TMC2660_SPI_SSB_PORT.OUTSET = 1 << TMC2660_SPI_SSB_PIN; // High
   TMC2660_SPI_SSB_PORT.DIRSET = 1 << TMC2660_SPI_SSB_PIN; // Output
-#endif
 
   // Configure SPI
   PR.PRPC &= ~PR_SPI_bm; // Disable power reduction
@@ -260,7 +253,7 @@ void tmc2660_init() {
   // Configure timer
   PR.PRPC &= ~PR_TC1_bm; // Disable power reduction
   TMC2660_TIMER.PER = F_CPU / 1024 / 10; // Set timer period
-  TMC2660_TIMER.INTCTRLA = TC_OVFINTLVL_LO_gc; // Low priority overflow int
+  TMC2660_TIMER.INTCTRLA = TC_OVFINTLVL_MED_gc; // overflow interupt level
   TMC2660_TIMER.CTRLA = TC_CLKSEL_DIV1024_gc; // enable, clock/1024
 }
 
index ad6f2d2148b373c498d5764bcf7610884f2e8802..07212a2dc0ea85010ca4de36483a1a67eddec074 100644 (file)
@@ -35,6 +35,7 @@
 #include <stdint.h>
 
 #define TMC2660_SPI_PORT PORTC
+#define TMC2660_SPI_SS_PIN 4
 #define TMC2660_SPI_SCK_PIN 5
 #define TMC2660_SPI_MISO_PIN 6
 #define TMC2660_SPI_MOSI_PIN 7
@@ -116,7 +117,7 @@ int tmc2660_all_ready();
 
 #define TMC2660_SGCSCONF            3
 #define TMC2660_SGCSCONF_ADDR       (6UL << 17)
-#define TMC2660_SGCSCONF_SFILT      (1UL << 17)
+#define TMC2660_SGCSCONF_SFILT      (1UL << 16)
 #define TMC2660_SGCSCONF_THRESH(x)  (((int32_t)x & 0x7f) << 8)
 #define TMC2660_SGCSCONF_CS(x)      (((int32_t)x & 0x1f) << 0)
 #define TMC2660_SGCSCONF_CS_NONE    (31UL << 0)