Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the simple 4 axis stepper control respect the axis inversion settings in Configuration_prusa.h #1263

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions Firmware/sm4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this is a non-constant variable that is declared globally, the compiler manages to optimize it completely because it is never written to it. So it works just as well as a define


sm4_stop_cb_t sm4_stop_cb = 0;

sm4_update_pos_cb_t sm4_update_pos_cb = 0;
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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;
}
Expand All @@ -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
Expand Down