/*
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 .
*/
/* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
and Philipp Tiefenbacher. The circle buffer implementation by the wiring_serial library by David A. Mellis */
#include "stepper.h"
#include "config.h"
#include "nuts_bolts.h"
#include
#define TICKS_PER_MICROSECOND F_CPU/1000000
#define STEP_BUFFER_SIZE 100
volatile uint8_t step_buffer[STEP_BUFFER_SIZE]; // A buffer for step instructions
volatile int step_buffer_head = 0;
volatile int step_buffer_tail = 0;
uint8_t stepper_mode = STEPPER_MODE_STOPPED;
// This timer interrupt is executed at the pace set with set_pace. It pops one instruction from
// the step_buffer, executes it. Then it starts timer2 in order to reset the motor port after
// five microseconds.
SIGNAL(SIG_OUTPUT_COMPARE1A)
{
if (step_buffer_head != step_buffer_tail) {
// Set the stepper port according to the instructions
MOTORS_PORT = (MOTORS_PORT & ~MOTORS_MASK) | step_buffer[step_buffer_tail];
// Reset and start timer 2 which will reset the motor port after 5 microsecond
TCNT2 = 0; // reset counter
OCR2A = 5*TICKS_PER_MICROSECOND; // set the time
TIMSK2 |= OCIE2A; // enable interrupt
// move the step buffer tail to the next instruction
step_buffer_tail = (step_buffer_tail + 1) % STEP_BUFFER_SIZE;
}
}
// This interrupt is set up by SIG_OUTPUT_COMPARE1A when it sets the motor port bits. It resets
// the motor port after a short period (5us) completing one step cycle.
SIGNAL(SIG_OUTPUT_COMPARE2A)
{
MOTORS_PORT = MOTORS_PORT & ~MOTORS_MASK; // reset stepper pins
TIMSK2 &= ~OCIE2A; // disable this timer interrupt until next time
}
// Initialize and start the stepper motor subsystem
void st_init()
{
// Configure directions of interface pins
MOTORS_DDR |= MOTORS_MASK;
LIMIT_DDR &= ~(LIMIT_MASK);
STEPPERS_ENABLE_DDR |= 1<> 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 {
// Okay, that was slower than we actually go. Just set the slowest speed
ceiling = 0xffff;
prescaler = 4;
}
// Set prescaler
TCCR1B = (TCCR1B & ~(0x07<