Skip to content

Commit

Permalink
disable TIMER1 - causes problems - ISR is stuck after a certain time
Browse files Browse the repository at this point in the history
state not needed anymore - ISR is creating entire pulse instead of each flank at a time
  • Loading branch information
sq7bti committed Aug 27, 2014
1 parent 1edb83e commit 8c2bf8d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
67 changes: 35 additions & 32 deletions iAccelStepper.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
#include "iAccelStepper.h"
#include "driverlib/timer.h"

volatile boolean state[3];
static iAccelStepper* me[3];
static unsigned int all_instances;
static unsigned long ulPeriod;

void iAccelStepper::ISR(void) {
TimerIntClear(g_ulTIMERBase[id], TIMER_TIMA_TIMEOUT);
digitalWrite(_pin[0], state[id]);
if(state[id]) {
if(_direction == DIRECTION_CW)
// Clockwise
++_currentPos;
else
// Anticlockwise
--_currentPos;
// prepare for the next period
computeNewSpeed();
digitalWrite(_pin[1], _direction);
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, ulPeriod);
TimerEnable(g_ulTIMERBase[id], TIMER_A);

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

// prepare for the next period
computeNewSpeed();
digitalWrite(_pin[1], _direction);

// switch off timer at the falling edge
if((_stepInterval == 0) || (abs(distanceToGo()) < 1)) {
TimerDisable(g_ulTIMERBase[id], TIMER_A);
running = false;
} else {
// switch off timer at the falling edge
if(_stepInterval == 0) {
TimerDisable(g_ulTIMERBase[id], TIMER_A);
running = false;
} else {
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, _stepInterval - ulPeriod);
TimerEnable(g_ulTIMERBase[id], TIMER_A);
}
digitalWrite(_pin[0], true);
delayMicroseconds(ulPeriod);
digitalWrite(_pin[0], false);

TimerLoadSet(g_ulTIMERBase[id], TIMER_A, _stepInterval - ulPeriod);
TimerEnable(g_ulTIMERBase[id], TIMER_A);
}
state[id] ^= 1;
}

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[3] = { timerISR0, timerISR1, timerISR2 };
ISR_ptr_t timerISR_ptr[2] = { timerISR0, timerISR1 };

void iAccelStepper::begin(uint8_t pin1, uint8_t pin2, uint8_t pin3)
{
Expand All @@ -52,9 +51,10 @@ void iAccelStepper::begin(uint8_t pin1, uint8_t pin2, uint8_t pin3)
AccelStepper::setEnablePin(pin3);
AccelStepper::setPinsInverted(false, false, false, false, true);

ulPeriod = 2 * clockCyclesPerMicrosecond();
// ulPeriod = 2 * clockCyclesPerMicrosecond();
ulPeriod = 1;

if(all_instances < 3) {
if(all_instances < 2) {
id = all_instances;
// Configure timer
SysCtlPeripheralEnable(g_ulTIMERPeriph[id]);
Expand All @@ -64,7 +64,6 @@ void iAccelStepper::begin(uint8_t pin1, uint8_t pin2, uint8_t pin3)
TimerIntRegister(g_ulTIMERBase[id], TIMER_A, timerISR_ptr[id]);

me[id] = this;
state[id] = false;
running = false;
++all_instances;
}
Expand All @@ -83,17 +82,21 @@ void iAccelStepper::moveTo(long absolute)
running = true;
// enable driver
// enableOutputs();
digitalWrite(_pin[1], _direction);
computeNewSpeed();
state[id] = true;
digitalWrite(_pin[0], true);
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, ulPeriod);
digitalWrite(_pin[1], _direction);

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

digitalWrite(_pin[0], true);
delayMicroseconds(ulPeriod);
digitalWrite(_pin[0], false);

TimerLoadSet(g_ulTIMERBase[id], TIMER_A, _stepInterval - ulPeriod);
TimerEnable(g_ulTIMERBase[id], TIMER_A);
}
}
9 changes: 5 additions & 4 deletions iAccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
// The list of Timer peripherals.
//
//*****************************************************************************
static const unsigned long g_ulTIMERPeriph[3] =
static const unsigned long g_ulTIMERPeriph[2] =
{
#if defined(PART_TM4C1233H6PM) || defined(PART_LM4F120H5QR)
// SYSCTL_PERIPH_TIMER0, // wiring_analog.c analogWrite()
SYSCTL_PERIPH_TIMER1,
// SYSCTL_PERIPH_TIMER1,
SYSCTL_PERIPH_TIMER2,
SYSCTL_PERIPH_TIMER3
// SYSCTL_PERIPH_TIMER4, // Tone.c
Expand All @@ -27,11 +27,11 @@ static const unsigned long g_ulTIMERPeriph[3] =
#endif
};

static const unsigned long g_ulTIMERBase[3] =
static const unsigned long g_ulTIMERBase[2] =
{
#if defined(PART_TM4C1233H6PM) || defined(PART_LM4F120H5QR)
// TIMER0_BASE, // wiring_analog.c analogWrite()
TIMER1_BASE,
// TIMER1_BASE,
TIMER2_BASE,
TIMER3_BASE
// TIMER4_BASE, // Tone.c
Expand All @@ -51,6 +51,7 @@ class iAccelStepper : public AccelStepper
void moveTo(long absolute);
void move(long relative);
boolean run(void) { return running; };
unsigned long stepInterval() { return _stepInterval; };
void ISR(void);
private:
volatile boolean running;
Expand Down

0 comments on commit 8c2bf8d

Please sign in to comment.