Skip to content

Commit

Permalink
reverted to the method with _state: false during the LOW of the durat…
Browse files Browse the repository at this point in the history
…ion of _stepInterval and HIGH for the duration of step pulse
  • Loading branch information
sq7bti committed Aug 28, 2014
1 parent 6ca77c8 commit fce2db2
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions iAccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,45 @@ static uint8_t _pin_step[2];
static uint32_t _port_dir[2];
static uint8_t _pin_dir[2];
static boolean direction[2];
static boolean _state[2];
static unsigned int all_instances;
static unsigned long ulPeriod;

void iAccelStepper::ISR(void) {
TimerIntClear(g_ulTIMERBase[id], TIMER_TIMA_TIMEOUT);

if(_direction == DIRECTION_CW)
// Clockwise
++_currentPos;
else
// Anticlockwise
--_currentPos;

// prepare for the next period
computeNewSpeed();

// either fire the timer again for another period or switch it off when the move is finished
if((_stepInterval == 0) || (abs(distanceToGo()) < 1)) {
TimerDisable(g_ulTIMERBase[id], TIMER_A);
running = false;
} else {
HWREG(_port_step[id]) = _pin_step[id];
// TODO: check if ISR can handle delayMicroseconds(int)
// delayMicroseconds(ulPeriod);
if(direction[id] != _direction) {
direction[id] = _direction;
HWREG(_port_dir[id]) = _direction?_pin_dir[id]:0;
}

// falling edge produce space for length _stepInterval
if(_state[id]) {
_state[id] = false;
HWREG(_port_step[id]) = 0;
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, _stepInterval - ulPeriod);
TimerEnable(g_ulTIMERBase[id], TIMER_A);
} else {
// prepare for the next period
// rising edge - calculate everything necessary and calculate _stepInterval
computeNewSpeed();

HWREG(_port_step[id]) = 0;
if(_direction == DIRECTION_CW)
// Clockwise
++_currentPos;
else
// Anticlockwise
--_currentPos;

// either fire the timer again for another period or switch it off when the move is finished
if((_stepInterval == 0) || (abs(distanceToGo()) < 1)) {
TimerDisable(g_ulTIMERBase[id], TIMER_A);
running = false;
} else {
_state[id] = true;
HWREG(_port_step[id]) = _pin_step[id];
if(direction[id] != _direction) {
direction[id] = _direction;
HWREG(_port_dir[id]) = _direction?_pin_dir[id]:0;
}
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, ulPeriod);
TimerEnable(g_ulTIMERBase[id], TIMER_A);
}
}
}

Expand All @@ -63,8 +69,8 @@ void iAccelStepper::begin(uint8_t pin1, uint8_t pin2, uint8_t pin3)
AccelStepper::setPinsInverted(false, false, false, false, true);

// specs of DRV8825 requires 2us, A3967 and A4988 requires atleast 1us step pulse
// ulPeriod = 2 * clockCyclesPerMicrosecond();
ulPeriod = 1;
ulPeriod = clockCyclesPerMicrosecond();
// ulPeriod = 1;

if(all_instances < 2) {
id = all_instances;
Expand All @@ -83,6 +89,7 @@ void iAccelStepper::begin(uint8_t pin1, uint8_t pin2, uint8_t pin3)
_port_dir[id] = (uint32_t)portBASERegister(digitalPinToPort(pin2)) + (GPIO_O_DATA + (digitalPinToBitMask(pin2) << 2));
_pin_dir[id] = (uint8_t)digitalPinToBitMask(pin2);
direction[id] = false;
_state[id] = false;
}
}

Expand Down Expand Up @@ -113,10 +120,9 @@ void iAccelStepper::moveTo(long absolute)
--_currentPos;

HWREG(_port_step[id]) = _pin_step[id];
delayMicroseconds(ulPeriod);
HWREG(_port_step[id]) = 0;
_state[id] = true;

TimerLoadSet(g_ulTIMERBase[id], TIMER_A, _stepInterval - ulPeriod);
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, ulPeriod);
TimerEnable(g_ulTIMERBase[id], TIMER_A);
}
}

0 comments on commit fce2db2

Please sign in to comment.