diff --git a/stepper.c b/stepper.c index 467fed5..d03c026 100644 --- a/stepper.c +++ b/stepper.c @@ -144,9 +144,8 @@ SIGNAL(TIMER1_COMPA_vect) // If there is no current block, attempt to pop one from the buffer if (current_block == NULL) { // Anything in the buffer? - if (block_buffer_head != block_buffer_tail) { - // Retrieve a new line and get ready to step it - current_block = &block_buffer[block_buffer_tail]; + current_block = plan_get_current_block(); + if (current_block != NULL) { trapezoid_generator_reset(); counter_x = -(current_block->step_event_count >> 1); counter_y = counter_x; @@ -184,8 +183,7 @@ SIGNAL(TIMER1_COMPA_vect) // printInteger(current_block->rate_delta); // printString(" <-- delta\n\r"); current_block = NULL; - // move the block buffer tail to the next instruction - block_buffer_tail = (block_buffer_tail + 1) % BLOCK_BUFFER_SIZE; + plan_discard_current_block(); } } else { out_bits = 0; @@ -252,7 +250,7 @@ void st_init() // Block until all buffered steps are executed void st_synchronize() { - while(block_buffer_tail != block_buffer_head) { sleep_mode(); } + while(plan_get_current_block()) { sleep_mode(); } } // Configures the prescaler and ceiling of timer 1 to produce the given rate as accurately as possible. diff --git a/stepper_plan.c b/stepper_plan.c index 0d972f9..7d0de45 100644 --- a/stepper_plan.c +++ b/stepper_plan.c @@ -351,6 +351,17 @@ int plan_is_acceleration_manager_enabled() { return(acceleration_manager_enabled); } +inline void plan_discard_current_block() { + if (block_buffer_head != block_buffer_tail) { + block_buffer_tail = (block_buffer_tail + 1) % BLOCK_BUFFER_SIZE; + } +} + +inline block_t *plan_get_current_block() { + if (block_buffer_head == block_buffer_tail) { return(NULL); } + return(&block_buffer[block_buffer_tail]); +} + // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration // calculation the caller must also provide the physical length of the line in millimeters. diff --git a/stepper_plan.h b/stepper_plan.h index 0d04c1c..1f71d7e 100644 --- a/stepper_plan.h +++ b/stepper_plan.h @@ -56,9 +56,6 @@ typedef struct { uint32_t exit_rate; } block_t; -extern block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instructions -extern volatile int block_buffer_head; // Index of the next block to be pushed -extern volatile int block_buffer_tail; // Index of the block to process now extern int32_t position[3]; // Initialize the motion plan subsystem @@ -71,6 +68,13 @@ void plan_init(); // calculation the caller must also provide the physical length of the line in millimeters. void plan_buffer_line(double x, double y, double z, double feed_rate, int invert_feed_rate); +// Call when the current block is no longer needed. Discards the block and makes the memory +// availible for new blocks. +inline void plan_discard_current_block(); + +// Gets the current block. Returns NULL if buffer empty +inline block_t *plan_get_current_block(); + // Enables acceleration-management for upcoming blocks void plan_set_acceleration_manager_enabled(int enabled); // Is acceleration-management currently enabled?