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

optimisation: stepper: make dda_isteps_t an array #4427

Merged
merged 1 commit into from
Nov 29, 2023
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
76 changes: 37 additions & 39 deletions Firmware/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ block_t *current_block; // A pointer to the block currently being traced
//static makes it inpossible to be called from outside of this file by extern.!

// Variables used by The Stepper Driver Interrupt
static unsigned char out_bits; // The next stepping-bits to be output
static dda_isteps_t
counter_x, // Counter variables for the bresenham line tracer
counter_y,
counter_z,
counter_e;
static unsigned char out_bits; // The next stepping-bits to be output
static dda_isteps_t counter[NUM_AXIS]; // Counter variables for the bresenham line tracer
volatile dda_usteps_t step_events_completed; // The number of step events executed in the current block
static uint32_t acceleration_time, deceleration_time;
static uint16_t acc_step_rate; // needed for deccelaration start point
Expand Down Expand Up @@ -393,18 +389,20 @@ FORCE_INLINE void stepper_next_block()
}

if (current_block->flag & BLOCK_FLAG_DDA_LOWRES) {
counter_x.lo = -(current_block->step_event_count.lo >> 1);
counter_y.lo = counter_x.lo;
counter_z.lo = counter_x.lo;
counter_e.lo = counter_x.lo;
const int16_t value = -(current_block->step_event_count.lo >> 1);
for (uint8_t axis = 0; axis < NUM_AXIS; axis++)
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can even iterate from the back which saves 2-4 bytes more

for (uint8_t axis = NUM_AXIS-1; axis >=0 ; --axis){
    counter[axis].lo = value;
}

Caches and memory ordering is not an issue on the AVR IMHO

Copy link

@petrubecheru petrubecheru Nov 29, 2023

Choose a reason for hiding this comment

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

what about this, using postdecrementation :
for (uint8_t axis = NUM_AXIS; axis-- ; ){
counter[axis].lo = value;
} ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@petrubecheru Thanks but compiling with PF-build.sh the usage of Flash and RAM are the same before and after.

{
counter[axis].lo = value;
}
#ifdef LIN_ADVANCE
e_extruding = current_block->steps[E_AXIS].lo != 0;
#endif
} else {
counter_x.wide = -(current_block->step_event_count.wide >> 1);
counter_y.wide = counter_x.wide;
counter_z.wide = counter_x.wide;
counter_e.wide = counter_x.wide;
const int32_t value = -(current_block->step_event_count.wide >> 1);
for (uint8_t axis = 0; axis < NUM_AXIS; axis++)
{
counter[axis].wide = value;
}
#ifdef LIN_ADVANCE
e_extruding = current_block->steps[E_AXIS].wide != 0;
#endif
Expand Down Expand Up @@ -643,48 +641,48 @@ FORCE_INLINE void stepper_tick_lowres()
for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
MSerial.checkRx(); // Check for serial chars.
// Step in X axis
counter_x.lo += current_block->steps[X_AXIS].lo;
if (counter_x.lo > 0) {
counter[X_AXIS].lo += current_block->steps[X_AXIS].lo;
if (counter[X_AXIS].lo > 0) {
STEP_NC_HI(X_AXIS);
#ifdef DEBUG_XSTEP_DUP_PIN
STEP_NC_HI(X_DUP_AXIS);
#endif //DEBUG_XSTEP_DUP_PIN
counter_x.lo -= current_block->step_event_count.lo;
counter[X_AXIS].lo -= current_block->step_event_count.lo;
count_position[X_AXIS]+=count_direction[X_AXIS];
STEP_NC_LO(X_AXIS);
#ifdef DEBUG_XSTEP_DUP_PIN
STEP_NC_LO(X_DUP_AXIS);
#endif //DEBUG_XSTEP_DUP_PIN
}
// Step in Y axis
counter_y.lo += current_block->steps[Y_AXIS].lo;
if (counter_y.lo > 0) {
counter[Y_AXIS].lo += current_block->steps[Y_AXIS].lo;
if (counter[Y_AXIS].lo > 0) {
STEP_NC_HI(Y_AXIS);
#ifdef DEBUG_YSTEP_DUP_PIN
STEP_NC_HI(Y_DUP_AXIS);
#endif //DEBUG_YSTEP_DUP_PIN
counter_y.lo -= current_block->step_event_count.lo;
counter[Y_AXIS].lo -= current_block->step_event_count.lo;
count_position[Y_AXIS]+=count_direction[Y_AXIS];
STEP_NC_LO(Y_AXIS);
#ifdef DEBUG_YSTEP_DUP_PIN
STEP_NC_LO(Y_DUP_AXIS);
#endif //DEBUG_YSTEP_DUP_PIN
}
// Step in Z axis
counter_z.lo += current_block->steps[Z_AXIS].lo;
if (counter_z.lo > 0) {
counter[Z_AXIS].lo += current_block->steps[Z_AXIS].lo;
if (counter[Z_AXIS].lo > 0) {
STEP_NC_HI(Z_AXIS);
counter_z.lo -= current_block->step_event_count.lo;
counter[Z_AXIS].lo -= current_block->step_event_count.lo;
count_position[Z_AXIS]+=count_direction[Z_AXIS];
STEP_NC_LO(Z_AXIS);
}
// Step in E axis
counter_e.lo += current_block->steps[E_AXIS].lo;
if (counter_e.lo > 0) {
counter[E_AXIS].lo += current_block->steps[E_AXIS].lo;
if (counter[E_AXIS].lo > 0) {
#ifndef LIN_ADVANCE
STEP_NC_HI(E_AXIS);
#endif /* LIN_ADVANCE */
counter_e.lo -= current_block->step_event_count.lo;
counter[E_AXIS].lo -= current_block->step_event_count.lo;
count_position[E_AXIS] += count_direction[E_AXIS];
#ifdef LIN_ADVANCE
e_steps += count_direction[E_AXIS];
Expand All @@ -705,49 +703,49 @@ FORCE_INLINE void stepper_tick_highres()
for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
MSerial.checkRx(); // Check for serial chars.
// Step in X axis
counter_x.wide += current_block->steps[X_AXIS].wide;
if (counter_x.wide > 0) {
counter[X_AXIS].wide += current_block->steps[X_AXIS].wide;
if (counter[X_AXIS].wide > 0) {
STEP_NC_HI(X_AXIS);
#ifdef DEBUG_XSTEP_DUP_PIN
STEP_NC_HI(X_DUP_AXIS);
#endif //DEBUG_XSTEP_DUP_PIN
counter_x.wide -= current_block->step_event_count.wide;
counter[X_AXIS].wide -= current_block->step_event_count.wide;
count_position[X_AXIS]+=count_direction[X_AXIS];
STEP_NC_LO(X_AXIS);
#ifdef DEBUG_XSTEP_DUP_PIN
STEP_NC_LO(X_DUP_AXIS);
#endif //DEBUG_XSTEP_DUP_PIN
}
// Step in Y axis
counter_y.wide += current_block->steps[Y_AXIS].wide;
if (counter_y.wide > 0) {
counter[Y_AXIS].wide += current_block->steps[Y_AXIS].wide;
if (counter[Y_AXIS].wide > 0) {
STEP_NC_HI(Y_AXIS);
#ifdef DEBUG_YSTEP_DUP_PIN
STEP_NC_HI(Y_DUP_AXIS);
#endif //DEBUG_YSTEP_DUP_PIN
counter_y.wide -= current_block->step_event_count.wide;
counter[Y_AXIS].wide -= current_block->step_event_count.wide;
count_position[Y_AXIS]+=count_direction[Y_AXIS];
STEP_NC_LO(Y_AXIS);
#ifdef DEBUG_YSTEP_DUP_PIN
STEP_NC_LO(Y_DUP_AXIS);
#endif //DEBUG_YSTEP_DUP_PIN
}
// Step in Z axis
counter_z.wide += current_block->steps[Z_AXIS].wide;
if (counter_z.wide > 0) {
counter[Z_AXIS].wide += current_block->steps[Z_AXIS].wide;
if (counter[Z_AXIS].wide > 0) {
STEP_NC_HI(Z_AXIS);
counter_z.wide -= current_block->step_event_count.wide;
counter[Z_AXIS].wide -= current_block->step_event_count.wide;
count_position[Z_AXIS]+=count_direction[Z_AXIS];
STEP_NC_LO(Z_AXIS);
}
// Step in E axis
counter_e.wide += current_block->steps[E_AXIS].wide;
if (counter_e.wide > 0) {
counter[E_AXIS].wide += current_block->steps[E_AXIS].wide;
if (counter[E_AXIS].wide > 0) {
#ifndef LIN_ADVANCE
STEP_NC_HI(E_AXIS);
#endif /* LIN_ADVANCE */
counter_e.wide -= current_block->step_event_count.wide;
count_position[E_AXIS]+=count_direction[E_AXIS];
counter[E_AXIS].wide -= current_block->step_event_count.wide;
count_position[E_AXIS] += count_direction[E_AXIS];
#ifdef LIN_ADVANCE
e_steps += count_direction[E_AXIS];
#else
Expand Down
Loading