Browse Source

Merge pull request #1263 from metacollin/calfix

Make the simple 4 axis stepper control respect the axis inversion settings in Configuration_prusa.h
DRracer 2 years ago
parent
commit
e641d6e1e1
1 changed files with 27 additions and 20 deletions
  1. 27 20
      Firmware/sm4.c

+ 27 - 20
Firmware/sm4.c

@@ -36,6 +36,13 @@
 //#define E0_STEP_PIN  34 //PC3 (+)
 
 
+#define XDIR INVERT_X_DIR:!INVERT_X_DIR
+#define YDIR INVERT_Y_DIR:!INVERT_Y_DIR
+#define ZDIR INVERT_Z_DIR:!INVERT_Z_DIR
+#define EDIR INVERT_E0_DIR:!INVERT_E0_DIR
+
+uint8_t dir_mask = 0x0F^(INVERT_X_DIR | (INVERT_Y_DIR << 1) | (INVERT_Z_DIR << 2) | (INVERT_E0_DIR << 3));
+
 sm4_stop_cb_t sm4_stop_cb = 0;
 
 sm4_update_pos_cb_t sm4_update_pos_cb = 0;
@@ -50,15 +57,15 @@ uint8_t sm4_get_dir(uint8_t axis)
 	switch (axis)
 	{
 #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
-	case 0: return (PORTL & 2)?0:1;
-	case 1: return (PORTL & 1)?0:1;
-	case 2: return (PORTL & 4)?0:1;
-	case 3: return (PORTL & 64)?1:0;
+	case 0: return (PORTL & 2)?XDIR;
+	case 1: return (PORTL & 1)?YDIR;
+	case 2: return (PORTL & 4)?ZDIR;
+	case 3: return (PORTL & 64)?EDIR;
 #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
-	case 0: return (PORTL & 1)?1:0;
-	case 1: return (PORTL & 2)?0:1;
-	case 2: return (PORTL & 4)?1:0;
-	case 3: return (PORTL & 64)?0:1;
+	case 0: return (PORTL & 1)?XDIR;
+	case 1: return (PORTL & 2)?YDIR;
+	case 2: return (PORTL & 4)?ZDIR;
+	case 3: return (PORTL & 64)?EDIR;
 #endif
 	}
 	return 0;
@@ -69,15 +76,15 @@ void sm4_set_dir(uint8_t axis, uint8_t dir)
 	switch (axis)
 	{
 #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
-	case 0: if (!dir) PORTL |= 2; else PORTL &= ~2; break;
-	case 1: if (!dir) PORTL |= 1; else PORTL &= ~1; break;
-	case 2: if (!dir) PORTL |= 4; else PORTL &= ~4; break;
-	case 3: if (dir) PORTL |= 64; else PORTL &= ~64; break;
+	case 0: if (dir == INVERT_X_DIR) PORTL |= 2; else PORTL &= ~2; break;
+	case 1: if (dir == INVERT_Y_DIR) PORTL |= 1; else PORTL &= ~1; break;
+	case 2: if (dir == INVERT_Z_DIR) PORTL |= 4; else PORTL &= ~4; break;
+	case 3: if (dir == INVERT_E0_DIR) PORTL |= 64; else PORTL &= ~64; break;
 #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
-	case 0: if (dir) PORTL |= 1; else PORTL &= ~1; break;
-	case 1: if (!dir) PORTL |= 2; else PORTL &= ~2; break;
-	case 2: if (dir) PORTL |= 4; else PORTL &= ~4; break;
-	case 3: if (!dir) PORTL |= 64; else PORTL &= ~64; break;
+	case 0: if (dir == INVERT_X_DIR) PORTL |= 1; else PORTL &= ~1; break;
+	case 1: if (dir == INVERT_Y_DIR) PORTL |= 2; else PORTL &= ~2; break;
+	case 2: if (dir == INVERT_Z_DIR) PORTL |= 4; else PORTL &= ~4; break;
+	case 3: if (dir == INVERT_E0_DIR) PORTL |= 64; else PORTL &= ~64; break;
 #endif
 	}
 	asm("nop");
@@ -93,13 +100,13 @@ uint8_t sm4_get_dir_bits(void)
 	if (portL & 1) dir_bits |= 2;
 	if (portL & 4) dir_bits |= 4;
 	if (portL & 64) dir_bits |= 8;
-	dir_bits ^= 0x07; //invert XYZ, do not invert E
+	dir_bits ^= dir_mask;
 #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
 	if (portL & 1) dir_bits |= 1;
 	if (portL & 2) dir_bits |= 2;
 	if (portL & 4) dir_bits |= 4;
 	if (portL & 64) dir_bits |= 8;
-	dir_bits ^= 0x0a; //invert YE, do not invert XZ
+	dir_bits ^= dir_mask; 
 #endif
 	return dir_bits;
 }
@@ -110,13 +117,13 @@ void sm4_set_dir_bits(uint8_t dir_bits)
 	portL &= 0xb8; //set direction bits to zero
 	//TODO -optimize in asm
 #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
-	dir_bits ^= 0x07; //invert XYZ, do not invert E
+	dir_bits ^= dir_mask;
 	if (dir_bits & 1) portL |= 2;  //set X direction bit
 	if (dir_bits & 2) portL |= 1;  //set Y direction bit
 	if (dir_bits & 4) portL |= 4;  //set Z direction bit
 	if (dir_bits & 8) portL |= 64; //set E direction bit
 #elif ((MOTHERBOARD == BOARD_EINSY_1_0a))
-	dir_bits ^= 0x0a; //invert YE, do not invert XZ
+	dir_bits ^= dir_mask; 
 	if (dir_bits & 1) portL |= 1;  //set X direction bit
 	if (dir_bits & 2) portL |= 2;  //set Y direction bit
 	if (dir_bits & 4) portL |= 4;  //set Z direction bit