2009-01-28 23:48:21 +01:00
|
|
|
/*
|
|
|
|
stepper.c - stepper motor interface
|
|
|
|
Part of Grbl
|
|
|
|
|
|
|
|
Copyright (c) 2009 Simen Svale Skogsrud
|
|
|
|
|
|
|
|
Grbl is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Grbl is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-29 09:58:29 +01:00
|
|
|
/* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
|
2010-03-14 23:45:31 +01:00
|
|
|
and Philipp Tiefenbacher. The ring buffer implementation gleaned from the wiring_serial library
|
2009-02-03 09:56:45 +01:00
|
|
|
by David A. Mellis */
|
2009-01-29 09:58:29 +01:00
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
#include "stepper.h"
|
|
|
|
#include "config.h"
|
2009-02-11 00:37:33 +01:00
|
|
|
#include <math.h>
|
2010-03-03 00:26:48 +01:00
|
|
|
#include <stdlib.h>
|
2010-02-27 19:55:09 +01:00
|
|
|
#include <util/delay.h>
|
2009-01-28 23:48:21 +01:00
|
|
|
#include "nuts_bolts.h"
|
2010-06-28 23:29:58 +02:00
|
|
|
#include "acceleration.h"
|
2009-01-28 23:48:21 +01:00
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
2009-02-03 09:56:45 +01:00
|
|
|
#include "wiring_serial.h"
|
|
|
|
|
2010-03-14 23:45:31 +01:00
|
|
|
// Pick a suitable line-buffer size
|
|
|
|
#ifdef __AVR_ATmega328P__
|
|
|
|
#define LINE_BUFFER_SIZE 40 // Atmega 328 has one full kilobyte of extra RAM!
|
|
|
|
#else
|
2010-03-03 13:04:51 +01:00
|
|
|
#define LINE_BUFFER_SIZE 10
|
2010-03-14 23:45:31 +01:00
|
|
|
#endif
|
2010-03-02 21:46:51 +01:00
|
|
|
|
2010-12-20 14:01:38 +01:00
|
|
|
<<<<<<< Updated upstream
|
2010-03-02 21:46:51 +01:00
|
|
|
struct Line {
|
|
|
|
uint32_t steps_x, steps_y, steps_z;
|
2010-03-03 00:26:48 +01:00
|
|
|
int32_t maximum_steps;
|
2010-03-02 21:46:51 +01:00
|
|
|
uint8_t direction_bits;
|
2010-06-28 23:29:58 +02:00
|
|
|
double average_millimeters_per_step_event;
|
|
|
|
uin32_t ideal_rate; // in step-events/minute
|
|
|
|
uin32_t exit_rate;
|
|
|
|
uin32_t brake_point; // the point where braking starts measured in step-events from end point
|
|
|
|
uint32_t rate; // in cpu-ticks pr. step
|
2010-03-03 00:26:48 +01:00
|
|
|
};
|
2009-01-28 23:48:21 +01:00
|
|
|
|
2010-03-03 00:26:48 +01:00
|
|
|
struct Line line_buffer[LINE_BUFFER_SIZE]; // A buffer for step instructions
|
2010-03-02 21:46:51 +01:00
|
|
|
volatile int line_buffer_head = 0;
|
|
|
|
volatile int line_buffer_tail = 0;
|
2010-06-28 23:29:58 +02:00
|
|
|
volatile int moving = FALSE;
|
2009-02-08 22:08:27 +01:00
|
|
|
|
2010-03-02 21:46:51 +01:00
|
|
|
// Variables used by SIG_OUTPUT_COMPARE1A
|
2010-06-28 23:29:58 +02:00
|
|
|
uint8_t out_bits; // The next stepping-bits to be output
|
2010-03-04 21:18:55 +01:00
|
|
|
struct Line *current_line; // A pointer to the line currently being traced
|
2010-06-28 23:29:58 +02:00
|
|
|
volatile int32_t counter_x,
|
|
|
|
counter_y, counter_z; // counter variables for the bresenham line tracer
|
|
|
|
uint32_t iterations; // The number of iterations left to complete the current_line
|
|
|
|
volatile int busy; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
|
2009-01-28 23:48:21 +01:00
|
|
|
|
2010-06-28 23:29:58 +02:00
|
|
|
void set_step_events_per_minute(uint32_t steps_per_minute);
|
|
|
|
|
|
|
|
uint32_t mm_per_minute_to_step_events_pr_minute(struct Line* line, double mm_per_minute) {
|
|
|
|
return(mm_per_minute/line->average_millimeters_per_step_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_accelleration_plan() {
|
|
|
|
// Store the current
|
|
|
|
int initial_buffer_tail = line_buffer_tail;
|
|
|
|
|
|
|
|
}
|
2010-12-20 14:01:38 +01:00
|
|
|
=======
|
|
|
|
#define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
|
|
|
|
#define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
|
|
|
|
|
|
|
|
#define CYCLES_PER_ACCELLERATION_TICK (F_CPU)
|
|
|
|
|
|
|
|
// This record is used to buffer the setup for each linear movement
|
|
|
|
struct Block {
|
|
|
|
uint32_t steps_x, steps_y, steps_z; // Step count along each axis
|
|
|
|
double rate_x, rate_y, rate_z; // Nominal steps/minute for each axis
|
|
|
|
int32_t maximum_steps; // The largest stepcount of any axis for this block
|
|
|
|
uint8_t direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
|
|
|
|
uint32_t rate; // The nominal step rate for this block in microseconds/step
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Block block_buffer[BLOCK_BUFFER_SIZE]; // A buffer for motion instructions
|
|
|
|
volatile int block_buffer_head = 0; // Index of the next block to be pushed
|
|
|
|
volatile int block_buffer_tail = 0; // Index of the block to process now
|
|
|
|
|
|
|
|
// Variables used by The Stepper Driver Interrupt
|
|
|
|
uint8_t out_bits; // The next stepping-bits to be output
|
|
|
|
struct Block *current_block; // A pointer to the block currently being traced
|
|
|
|
int32_t counter_x,
|
|
|
|
counter_y,
|
|
|
|
counter_z; // counter variables for the bresenham line tracer
|
|
|
|
uint32_t iterations; // The number of iterations left to complete the current_block
|
|
|
|
volatile int busy; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
|
|
|
|
|
|
|
|
// Variables used by the accelleration manager
|
|
|
|
float rate_multiplier; // The current rate multiplier. at 1.0 nominal rates equals actual rates
|
|
|
|
float rate_change_rate; // The amount the rate_multiplier changes each
|
|
|
|
uint32_t rate_ramp_iterations; // The accelleration iterations for which the current rate ramp is valid
|
|
|
|
|
|
|
|
void config_step_timer(uint32_t microseconds);
|
|
|
|
>>>>>>> Stashed changes
|
2009-02-08 20:40:24 +01:00
|
|
|
|
2010-03-03 01:39:44 +01:00
|
|
|
// Add a new linear movement to the buffer. steps_x, _y and _z is the signed, relative motion in
|
2010-06-28 23:29:58 +02:00
|
|
|
// steps. Microseconds specify how many microseconds the move should take to perform. To aid accelleration
|
|
|
|
// calculation the caller must also provide the physical length of the line in millimeters.
|
|
|
|
void st_buffer_line(int32_t steps_x, int32_t steps_y, int32_t steps_z, uint32_t microseconds, double millimeters) {
|
2010-03-02 21:46:51 +01:00
|
|
|
// Calculate the buffer head after we push this byte
|
|
|
|
int next_buffer_head = (line_buffer_head + 1) % LINE_BUFFER_SIZE;
|
|
|
|
// If the buffer is full: good! That means we are well ahead of the robot.
|
2010-12-20 14:01:38 +01:00
|
|
|
<<<<<<< Updated upstream
|
2010-03-02 21:46:51 +01:00
|
|
|
// Nap until there is room in the buffer.
|
|
|
|
while(line_buffer_tail == next_buffer_head) { sleep_mode(); }
|
2010-03-04 21:18:55 +01:00
|
|
|
// Setup line record
|
2010-03-02 21:46:51 +01:00
|
|
|
struct Line *line = &line_buffer[line_buffer_head];
|
|
|
|
line->steps_x = labs(steps_x);
|
|
|
|
line->steps_y = labs(steps_y);
|
2010-03-03 00:26:48 +01:00
|
|
|
line->steps_z = labs(steps_z);
|
2010-03-02 21:46:51 +01:00
|
|
|
line->maximum_steps = max(line->steps_x, max(line->steps_y, line->steps_z));
|
2010-03-03 00:26:48 +01:00
|
|
|
// Bail if this is a zero-length line
|
|
|
|
if (line->maximum_steps == 0) { return; };
|
2010-06-28 23:29:58 +02:00
|
|
|
line->rate = (TICKS_PER_MICROSECOND*microseconds)/line->maximum_steps;
|
2010-12-20 14:01:38 +01:00
|
|
|
=======
|
|
|
|
// Rest here until there is room in the buffer.
|
|
|
|
while(block_buffer_tail == next_buffer_head) { sleep_mode(); }
|
|
|
|
// Prepare to set up new block
|
|
|
|
struct Block *block = &block_buffer[block_buffer_head];
|
|
|
|
// Number of steps for each axis
|
|
|
|
block->steps_x = labs(steps_x);
|
|
|
|
block->steps_y = labs(steps_y);
|
|
|
|
block->steps_z = labs(steps_z);
|
|
|
|
block->maximum_steps = max(block->steps_x, max(block->steps_y, block->steps_z));
|
|
|
|
// Bail if this is a zero-length block
|
|
|
|
if (block->maximum_steps == 0) { return; };
|
|
|
|
// Rate in steps/second for each axis
|
|
|
|
double rate_multiplier = 60.0*1000000.0/microseconds;
|
|
|
|
block->rate_x = round(block->steps_x*rate_multiplier);
|
|
|
|
block->rate_y = round(block->steps_y*rate_multiplier);
|
|
|
|
block->rate_z = round(block->steps_z*rate_multiplier);
|
|
|
|
block->rate = microseconds/block->maximum_steps;
|
|
|
|
// Compute direction bits for this block
|
|
|
|
>>>>>>> Stashed changes
|
2010-03-02 21:46:51 +01:00
|
|
|
uint8_t direction_bits = 0;
|
|
|
|
if (steps_x < 0) { direction_bits |= (1<<X_DIRECTION_BIT); }
|
|
|
|
if (steps_y < 0) { direction_bits |= (1<<Y_DIRECTION_BIT); }
|
|
|
|
if (steps_z < 0) { direction_bits |= (1<<Z_DIRECTION_BIT); }
|
|
|
|
line->direction_bits = direction_bits;
|
2010-06-28 23:29:58 +02:00
|
|
|
line->average_millimeters_per_step_event = millimeters/line->maximum_steps
|
2010-03-02 21:46:51 +01:00
|
|
|
// Move buffer head
|
2010-12-20 14:01:38 +01:00
|
|
|
<<<<<<< Updated upstream
|
2010-03-02 21:46:51 +01:00
|
|
|
line_buffer_head = next_buffer_head;
|
2010-03-03 00:26:48 +01:00
|
|
|
// enable stepper interrupt
|
|
|
|
TIMSK1 |= (1<<OCIE1A);
|
2010-03-07 23:10:41 +01:00
|
|
|
|
2010-03-02 21:46:51 +01:00
|
|
|
}
|
|
|
|
|
2010-03-04 21:18:55 +01:00
|
|
|
// This timer interrupt is executed at the rate set with config_step_timer. It pops one instruction from
|
2010-03-02 21:46:51 +01:00
|
|
|
// the line_buffer, executes it. Then it starts timer2 in order to reset the motor port after
|
2009-01-28 23:48:21 +01:00
|
|
|
// five microseconds.
|
2010-12-20 14:01:38 +01:00
|
|
|
=======
|
|
|
|
block_buffer_head = next_buffer_head;
|
|
|
|
// Ensure that block processing is running by enabling The Stepper Driver Interrupt
|
|
|
|
ENABLE_STEPPER_DRIVER_INTERRUPT();
|
|
|
|
}
|
|
|
|
|
|
|
|
// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Grbl. It is executed at the rate set with
|
|
|
|
// config_step_timer. It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
|
|
|
|
// It is supported by The Stepper Port Reset Interrupt which it uses to reset the stepper port after each pulse.
|
|
|
|
>>>>>>> Stashed changes
|
2010-03-14 23:45:31 +01:00
|
|
|
#ifdef TIMER1_COMPA_vect
|
|
|
|
SIGNAL(TIMER1_COMPA_vect)
|
|
|
|
#else
|
2009-01-28 23:48:21 +01:00
|
|
|
SIGNAL(SIG_OUTPUT_COMPARE1A)
|
2010-03-14 23:45:31 +01:00
|
|
|
#endif
|
2009-01-28 23:48:21 +01:00
|
|
|
{
|
2010-03-04 21:18:55 +01:00
|
|
|
if(busy){ return; } // The busy-flag is used to avoid reentering this interrupt
|
2010-03-03 23:18:39 +01:00
|
|
|
|
2010-03-03 00:26:48 +01:00
|
|
|
PORTD |= (1<<3);
|
2010-03-02 21:46:51 +01:00
|
|
|
// Set the direction pins a cuple of nanoseconds before we step the steppers
|
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~DIRECTION_MASK) | (out_bits & DIRECTION_MASK);
|
|
|
|
// Then pulse the stepping pins
|
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | out_bits;
|
2010-12-20 14:01:38 +01:00
|
|
|
// Reset step pulse reset timer so that The Stepper Port Reset Interrupt can reset the signal after
|
2010-03-07 20:29:18 +01:00
|
|
|
// exactly settings.pulse_microseconds microseconds.
|
|
|
|
TCNT2 = -(((settings.pulse_microseconds-2)*TICKS_PER_MICROSECOND)/8);
|
2010-03-03 23:18:39 +01:00
|
|
|
|
2010-03-03 22:55:45 +01:00
|
|
|
busy = TRUE;
|
2010-03-04 21:18:55 +01:00
|
|
|
sei(); // Re enable interrupts (normally disabled while inside an interrupt handler)
|
2010-03-03 23:18:39 +01:00
|
|
|
// We re-enable interrupts in order for SIG_OVERFLOW2 to be able to be triggered
|
2010-03-04 21:18:55 +01:00
|
|
|
// at exactly the right time even if we occasionally spend a lot of time inside this handler.
|
2010-03-02 21:46:51 +01:00
|
|
|
|
|
|
|
// If there is no current line, attempt to pop one from the buffer
|
|
|
|
if (current_line == NULL) {
|
2010-03-03 00:26:48 +01:00
|
|
|
PORTD &= ~(1<<4);
|
2010-03-02 21:46:51 +01:00
|
|
|
// Anything in the buffer?
|
|
|
|
if (line_buffer_head != line_buffer_tail) {
|
2010-03-03 00:26:48 +01:00
|
|
|
PORTD ^= (1<<5);
|
2010-03-02 21:46:51 +01:00
|
|
|
// Retrieve a new line and get ready to step it
|
|
|
|
current_line = &line_buffer[line_buffer_tail];
|
2010-03-03 13:04:51 +01:00
|
|
|
config_step_timer(current_line->rate);
|
2010-03-04 21:18:55 +01:00
|
|
|
counter_x = -(current_line->maximum_steps >> 1);
|
2010-03-02 21:46:51 +01:00
|
|
|
counter_y = counter_x;
|
|
|
|
counter_z = counter_x;
|
2010-03-03 00:26:48 +01:00
|
|
|
iterations = current_line->maximum_steps;
|
2010-06-28 23:29:58 +02:00
|
|
|
moving = TRUE;
|
2010-03-03 00:26:48 +01:00
|
|
|
} else {
|
|
|
|
// disable this interrupt until there is something to handle
|
2010-06-28 23:29:58 +02:00
|
|
|
moving = FALSE;
|
2010-03-03 00:26:48 +01:00
|
|
|
TIMSK1 &= ~(1<<OCIE1A);
|
|
|
|
PORTD |= (1<<4);
|
|
|
|
}
|
|
|
|
}
|
2010-03-02 21:46:51 +01:00
|
|
|
|
|
|
|
if (current_line != NULL) {
|
|
|
|
out_bits = current_line->direction_bits;
|
|
|
|
counter_x += current_line->steps_x;
|
|
|
|
if (counter_x > 0) {
|
|
|
|
out_bits |= (1<<X_STEP_BIT);
|
|
|
|
counter_x -= current_line->maximum_steps;
|
|
|
|
}
|
2010-03-03 00:26:48 +01:00
|
|
|
counter_y += current_line->steps_y;
|
2010-03-02 21:46:51 +01:00
|
|
|
if (counter_y > 0) {
|
|
|
|
out_bits |= (1<<Y_STEP_BIT);
|
|
|
|
counter_y -= current_line->maximum_steps;
|
|
|
|
}
|
2010-03-03 00:26:48 +01:00
|
|
|
counter_z += current_line->steps_z;
|
2010-03-02 21:46:51 +01:00
|
|
|
if (counter_z > 0) {
|
|
|
|
out_bits |= (1<<Z_STEP_BIT);
|
|
|
|
counter_z -= current_line->maximum_steps;
|
|
|
|
}
|
|
|
|
// If current line is finished, reset pointer
|
2010-03-03 00:26:48 +01:00
|
|
|
iterations -= 1;
|
|
|
|
if (iterations <= 0) {
|
2010-03-02 21:46:51 +01:00
|
|
|
current_line = NULL;
|
2010-03-03 00:26:48 +01:00
|
|
|
// move the line buffer tail to the next instruction
|
|
|
|
line_buffer_tail = (line_buffer_tail + 1) % LINE_BUFFER_SIZE;
|
2009-02-08 20:40:24 +01:00
|
|
|
}
|
2010-03-02 08:19:21 +01:00
|
|
|
} else {
|
2010-03-02 21:46:51 +01:00
|
|
|
out_bits = 0;
|
2009-02-03 09:56:45 +01:00
|
|
|
}
|
2010-03-07 20:29:18 +01:00
|
|
|
out_bits ^= settings.invert_mask;
|
2010-03-03 22:55:45 +01:00
|
|
|
busy=FALSE;
|
2010-03-03 00:26:48 +01:00
|
|
|
PORTD &= ~(1<<3);
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This interrupt is set up by SIG_OUTPUT_COMPARE1A when it sets the motor port bits. It resets
|
2010-03-07 20:29:18 +01:00
|
|
|
// the motor port after a short period (settings.pulse_microseconds) completing one step cycle.
|
2010-03-14 23:45:31 +01:00
|
|
|
#ifdef TIMER2_OVF_vect
|
|
|
|
SIGNAL(TIMER2_OVF_vect)
|
|
|
|
#else
|
2010-02-27 19:55:09 +01:00
|
|
|
SIGNAL(SIG_OVERFLOW2)
|
2010-03-14 23:45:31 +01:00
|
|
|
#endif
|
2009-01-28 23:48:21 +01:00
|
|
|
{
|
2010-02-27 19:55:09 +01:00
|
|
|
// reset stepping pins (leave the direction pins)
|
2010-03-07 20:29:18 +01:00
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | (settings.invert_mask & STEP_MASK);
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize and start the stepper motor subsystem
|
|
|
|
void st_init()
|
|
|
|
{
|
|
|
|
// Configure directions of interface pins
|
2009-02-03 09:56:45 +01:00
|
|
|
STEPPING_DDR |= STEPPING_MASK;
|
2010-03-07 20:29:18 +01:00
|
|
|
STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | settings.invert_mask;
|
2009-01-28 23:48:21 +01:00
|
|
|
LIMIT_DDR &= ~(LIMIT_MASK);
|
|
|
|
STEPPERS_ENABLE_DDR |= 1<<STEPPERS_ENABLE_BIT;
|
2010-02-27 19:55:09 +01:00
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
// waveform generation = 0100 = CTC
|
|
|
|
TCCR1B &= ~(1<<WGM13);
|
|
|
|
TCCR1B |= (1<<WGM12);
|
|
|
|
TCCR1A &= ~(1<<WGM11);
|
|
|
|
TCCR1A &= ~(1<<WGM10);
|
|
|
|
|
|
|
|
// output mode = 00 (disconnected)
|
|
|
|
TCCR1A &= ~(3<<COM1A0);
|
|
|
|
TCCR1A &= ~(3<<COM1B0);
|
|
|
|
|
|
|
|
// Configure Timer 2
|
|
|
|
TCCR2A = 0; // Normal operation
|
2010-02-27 19:55:09 +01:00
|
|
|
TCCR2B = (1<<CS21); // Full speed, 1/8 prescaler
|
2010-03-03 13:04:51 +01:00
|
|
|
TIMSK2 |= (1<<TOIE2);
|
2010-03-03 22:11:50 +01:00
|
|
|
|
2010-12-20 14:01:38 +01:00
|
|
|
// Just set the step_timer to something serviceably lazy
|
2010-03-03 22:11:50 +01:00
|
|
|
config_step_timer(20000);
|
|
|
|
// set enable pin
|
2010-03-03 13:04:51 +01:00
|
|
|
STEPPERS_ENABLE_PORT |= 1<<STEPPERS_ENABLE_BIT;
|
2009-01-28 23:48:21 +01:00
|
|
|
|
2009-01-30 11:26:08 +01:00
|
|
|
sei();
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Block until all buffered steps are executed
|
|
|
|
void st_synchronize()
|
|
|
|
{
|
2010-03-03 13:04:51 +01:00
|
|
|
while(line_buffer_tail != line_buffer_head) { sleep_mode(); }
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
2010-03-03 13:04:51 +01:00
|
|
|
// Cancel all buffered steps
|
2009-01-28 23:48:21 +01:00
|
|
|
void st_flush()
|
|
|
|
{
|
2009-01-30 11:26:08 +01:00
|
|
|
cli();
|
2010-03-02 21:46:51 +01:00
|
|
|
line_buffer_tail = line_buffer_head;
|
|
|
|
current_line = NULL;
|
2009-01-30 11:26:08 +01:00
|
|
|
sei();
|
2009-01-28 23:48:21 +01:00
|
|
|
}
|
|
|
|
|
2010-03-04 21:18:55 +01:00
|
|
|
// Configures the prescaler and ceiling of timer 1 to produce the given rate as accurately as possible.
|
2010-06-28 23:29:58 +02:00
|
|
|
void config_step_timer(uint32_t ticks)
|
2009-01-28 23:48:21 +01:00
|
|
|
{
|
|
|
|
uint16_t ceiling;
|
|
|
|
uint16_t prescaler;
|
2009-02-03 23:36:04 +01:00
|
|
|
if (ticks <= 0xffffL) {
|
2009-01-28 23:48:21 +01:00
|
|
|
ceiling = ticks;
|
|
|
|
prescaler = 0; // prescaler: 0
|
|
|
|
} else if (ticks <= 0x7ffffL) {
|
|
|
|
ceiling = ticks >> 3;
|
|
|
|
prescaler = 1; // prescaler: 8
|
|
|
|
} else if (ticks <= 0x3fffffL) {
|
|
|
|
ceiling = ticks >> 6;
|
|
|
|
prescaler = 2; // prescaler: 64
|
|
|
|
} else if (ticks <= 0xffffffL) {
|
|
|
|
ceiling = (ticks >> 8);
|
|
|
|
prescaler = 3; // prescaler: 256
|
|
|
|
} else if (ticks <= 0x3ffffffL) {
|
|
|
|
ceiling = (ticks >> 10);
|
|
|
|
prescaler = 4; // prescaler: 1024
|
|
|
|
} else {
|
2009-01-30 11:26:08 +01:00
|
|
|
// Okay, that was slower than we actually go. Just set the slowest speed
|
2009-01-28 23:48:21 +01:00
|
|
|
ceiling = 0xffff;
|
|
|
|
prescaler = 4;
|
|
|
|
}
|
|
|
|
// Set prescaler
|
|
|
|
TCCR1B = (TCCR1B & ~(0x07<<CS10)) | ((prescaler+1)<<CS10);
|
|
|
|
// Set ceiling
|
|
|
|
OCR1A = ceiling;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:29:58 +02:00
|
|
|
void set_step_events_per_minute(uint32_t steps_per_minute) {
|
|
|
|
config_step_timer((TICKS_PER_MICROSECOND*1000000*60)/steps_per_minute);
|
|
|
|
}
|
|
|
|
|
2009-01-28 23:48:21 +01:00
|
|
|
void st_go_home()
|
|
|
|
{
|
|
|
|
// Todo: Perform the homing cycle
|
|
|
|
}
|