Skip to content

Commit

Permalink
when initiated from idle - start with a low state to allow a change o…
Browse files Browse the repository at this point in the history
…f direction to commit
  • Loading branch information
sq7bti committed Sep 16, 2014
1 parent 36c5fec commit 493ae13
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
79 changes: 39 additions & 40 deletions iAccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include "inc/hw_gpio.h"
#include "inc/hw_timer.h"

static iAccelStepper* me[2];
static uint32_t _port_step[2];
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 iAccelStepper* me[MAX_INST];
static uint32_t _port_step[MAX_INST];
static uint8_t _pin_step[MAX_INST];
static uint32_t _port_dir[MAX_INST];
static uint8_t _pin_dir[MAX_INST];
static boolean direction[MAX_INST];
static boolean _state[MAX_INST];
static unsigned char all_instances;
static uint32_t ulPeriod;

Expand All @@ -20,22 +20,23 @@ void iAccelStepper::ISR(void) {
// falling edge produce space for length _stepInterval
if(_state[id]) {
_state[id] = false;

// prepare for the next period
// rising edge - calculate everything necessary and calculate _stepInterval
computeNewSpeed();

if(direction[id] != _direction) {
direction[id] = _direction;
HWREG(_port_dir[id]) = _direction?_pin_dir[id]:0;
}

HWREG(_port_step[id]) = 0;
//TimerLoadSet(g_ulTIMERBase[id], TIMER_A, _stepInterval - ulPeriod);
HWREG(g_ulTIMERBase[id] + TIMER_O_TAILR) = _stepInterval - ulPeriod;
//TimerEnable(g_ulTIMERBase[id], TIMER_A);
HWREG(g_ulTIMERBase[id] + TIMER_O_CTL) |= TIMER_A & (TIMER_CTL_TAEN | TIMER_CTL_TBEN);
} else {
// prepare for the next period
// rising edge - calculate everything necessary and calculate _stepInterval
computeNewSpeed();

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

// either fire the timer again for another period or switch it off when the move is finished
if((_stepInterval == 0) || (abs(distanceToGo()) < 1)) {
Expand All @@ -44,13 +45,17 @@ void iAccelStepper::ISR(void) {
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;
}

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

//TimerLoadSet(g_ulTIMERBase[id], TIMER_A, ulPeriod);
HWREG(g_ulTIMERBase[id] + TIMER_O_TAILR) = ulPeriod;
HWREG(_port_step[id]) = _pin_step[id];
//TimerEnable(g_ulTIMERBase[id], TIMER_A);
HWREG(g_ulTIMERBase[id] + TIMER_O_CTL) |= TIMER_A & (TIMER_CTL_TAEN | TIMER_CTL_TBEN);
}
Expand All @@ -59,27 +64,27 @@ void iAccelStepper::ISR(void) {

void timerISR0(void) { me[0]->ISR(); }
void timerISR1(void) { me[1]->ISR(); }
//void timerISR2(void) { me[2]->ISR(); }
void timerISR2(void) { me[2]->ISR(); }
//void timerISR3(void) { me[3]->ISR(); }
//void timerISR4(void) { me[4]->ISR(); }

typedef void (*ISR_ptr_t)(void);
//ISR_ptr_t timerISR_ptr[5] = { &timerISR0, &timerISR1, timerISR2, timerISR3, timerISR4 };
ISR_ptr_t timerISR_ptr[2] = { timerISR0, timerISR1 };
ISR_ptr_t timerISR_ptr[MAX_INST] = { timerISR0, timerISR1 };

void iAccelStepper::begin(uint8_t pin1, uint8_t pin2, uint8_t pin3)
{
// STEP DIR
AccelStepper::begin(AccelStepper::DRIVER, pin1, pin2);
// ENABLE
AccelStepper::setEnablePin(pin3);
AccelStepper::setPinsInverted(false, false, false, false, true);
AccelStepper::setPinsInverted(false, false, true);

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

if(all_instances < 2) {
if(all_instances < MAX_INST) {
id = all_instances;
// Configure timer
SysCtlPeripheralEnable(g_ulTIMERPeriph[id]);
Expand Down Expand Up @@ -112,26 +117,20 @@ void iAccelStepper::moveTo(long absolute)

if(!running && (distanceToGo() != 0)) {
running = true;
// enable driver
// enableOutputs();

computeNewSpeed();

if(direction[id] != _direction) {
direction[id] = _direction;
HWREG(_port_dir[id]) = _direction?_pin_dir[id]:0;
}

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

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

//TimerLoadSet(g_ulTIMERBase[id], TIMER_A, ulPeriod);
HWREG(g_ulTIMERBase[id] + TIMER_O_TAILR) = ulPeriod;
HWREG(g_ulTIMERBase[id] + TIMER_O_TAILR) = _stepInterval - ulPeriod;
// HWREG(g_ulTIMERBase[id] + TIMER_O_TAILR) = ulPeriod;
//TimerEnable(g_ulTIMERBase[id], TIMER_A);
HWREG(g_ulTIMERBase[id] + TIMER_O_CTL) |= TIMER_A & (TIMER_CTL_TAEN | TIMER_CTL_TBEN);
}
Expand Down
6 changes: 4 additions & 2 deletions iAccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

#include "driverlib/sysctl.h"

#define MAX_INST 2

//*****************************************************************************
//
// The list of Timer peripherals.
//
//*****************************************************************************
static const unsigned long g_ulTIMERPeriph[2] =
static const unsigned long g_ulTIMERPeriph[MAX_INST] =
{
#if defined(PART_TM4C1233H6PM) || defined(PART_LM4F120H5QR)
// SYSCTL_PERIPH_TIMER0, // wiring_analog.c analogWrite()
Expand All @@ -27,7 +29,7 @@ static const unsigned long g_ulTIMERPeriph[2] =
#endif
};

static const unsigned long g_ulTIMERBase[2] =
static const unsigned long g_ulTIMERBase[MAX_INST] =
{
#if defined(PART_TM4C1233H6PM) || defined(PART_LM4F120H5QR)
// TIMER0_BASE, // wiring_analog.c analogWrite()
Expand Down

0 comments on commit 493ae13

Please sign in to comment.