-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8d01b84
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "iAccelStepper.h" | ||
|
||
#include "driverlib/timer.h" | ||
|
||
volatile boolean state[3]; | ||
static iAccelStepper* me[3]; | ||
static unsigned int all_instances; | ||
|
||
void iAccelStepper::ISR(void) { | ||
TimerIntClear(g_ulTIMERBase[id], TIMER_TIMA_TIMEOUT); | ||
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, 1); | ||
TimerEnable(g_ulTIMERBase[id], TIMER_A); | ||
} else { | ||
// switch off timer at the falling edge | ||
if(_stepInterval == 0) { | ||
TimerDisable(g_ulTIMERBase[id], TIMER_A); | ||
digitalWrite(GREEN_LED, 0); | ||
running = false; | ||
} else { | ||
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, (80*_stepInterval)-1); | ||
TimerEnable(g_ulTIMERBase[id], TIMER_A); | ||
} | ||
} | ||
digitalWrite(_pin[0], state[id]); | ||
state[id] ^= 1; | ||
} | ||
|
||
void timerISR0(void) { me[0]->ISR(); } | ||
void timerISR1(void) { me[1]->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 }; | ||
|
||
void iAccelStepper::begin(uint8_t pin1, uint8_t pin2) | ||
{ | ||
// STEP DIR | ||
AccelStepper::begin(AccelStepper::DRIVER, pin1, pin2); | ||
|
||
if(all_instances < 3) { | ||
id = all_instances; | ||
Serial.println("configure timer"); | ||
// Configure timer | ||
SysCtlPeripheralEnable(g_ulTIMERPeriph[id]); | ||
Serial.println("configure sysctl"); | ||
|
||
TimerConfigure(g_ulTIMERBase[id], TIMER_CFG_ONE_SHOT); | ||
Serial.println("configure timerconfigure"); | ||
|
||
TimerIntEnable(g_ulTIMERBase[id], TIMER_TIMA_TIMEOUT); | ||
Serial.println("configure timerintenable"); | ||
|
||
TimerIntRegister(g_ulTIMERBase[id], TIMER_A, timerISR_ptr[id]); | ||
|
||
me[id] = this; | ||
state[id] = false; | ||
running = false; | ||
++all_instances; | ||
} | ||
} | ||
|
||
void iAccelStepper::move(long relative) | ||
{ | ||
moveTo(_currentPos + relative); | ||
} | ||
|
||
void iAccelStepper::moveTo(long absolute) | ||
{ | ||
AccelStepper::moveTo(absolute); | ||
|
||
if(!running && (distanceToGo() != 0)) { | ||
digitalWrite(_pin[1], _direction); | ||
computeNewSpeed(); | ||
state[id] = true; | ||
digitalWrite(_pin[0], state[id]); | ||
running = true; | ||
TimerLoadSet(g_ulTIMERBase[id], TIMER_A, 1); | ||
TimerEnable(g_ulTIMERBase[id], TIMER_A); | ||
digitalWrite(GREEN_LED, 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef iAccelStepper_h | ||
#define iAccelStepper_h | ||
|
||
#include <Energia.h> | ||
#include "AccelStepper.h" | ||
|
||
#include "driverlib/sysctl.h" | ||
|
||
//***************************************************************************** | ||
// | ||
// The list of Timer peripherals. | ||
// | ||
//***************************************************************************** | ||
static const unsigned long g_ulTIMERPeriph[3] = | ||
{ | ||
#if defined(PART_TM4C1233H6PM) || defined(PART_LM4F120H5QR) | ||
// SYSCTL_PERIPH_TIMER0, // wiring_analog.c analogWrite() | ||
SYSCTL_PERIPH_TIMER1, | ||
SYSCTL_PERIPH_TIMER2, | ||
SYSCTL_PERIPH_TIMER3 | ||
// SYSCTL_PERIPH_TIMER4, // Tone.c | ||
// SYSCTL_PERIPH_TIMER5, // wiring.c - millis() micros() | ||
// SYSCTL_PERIPH_TIMER6, | ||
// SYSCTL_PERIPH_TIMER7 | ||
#else | ||
#error "**** No PART defined or unsupported PART ****" | ||
#endif | ||
}; | ||
|
||
static const unsigned long g_ulTIMERBase[3] = | ||
{ | ||
#if defined(PART_TM4C1233H6PM) || defined(PART_LM4F120H5QR) | ||
// TIMER0_BASE, // wiring_analog.c analogWrite() | ||
TIMER1_BASE, | ||
TIMER2_BASE, | ||
TIMER3_BASE | ||
// TIMER4_BASE, // Tone.c | ||
// TIMER5_BASE, // wiring.c - millis() micros() | ||
// TIMER6_BASE, | ||
// TIMER7_BASE | ||
#else | ||
#error "**** No PART defined or unsupported PART ****" | ||
#endif | ||
}; | ||
|
||
class iAccelStepper : public AccelStepper | ||
{ | ||
public: | ||
iAccelStepper() : AccelStepper() {}; | ||
void begin(uint8_t pin1 = 2, uint8_t pin2 = 3); | ||
void moveTo(long absolute); | ||
void move(long relative); | ||
boolean run(void) { return running; }; | ||
void ISR(void); | ||
private: | ||
boolean running; | ||
unsigned int id; | ||
}; | ||
|
||
#endif |