This commit is contained in:
Hakan Bastedt
2024-02-05 21:22:23 +01:00
parent dbb4d0f34e
commit 43854ca4d0
3 changed files with 62 additions and 51 deletions

View File

@@ -6,19 +6,17 @@
class StepGen2
{
private:
volatile uint8_t timerIsRunning;
volatile int32_t timerStepPosition;
volatile int32_t timerStepDirection;
volatile int32_t timerStepPositionAtEnd;
volatile int32_t timerNewEndStepPosition;
volatile uint32_t timerNewCycleTime;
volatile double_t actualPosition;
volatile double_t requestedPosition;
volatile double_t oldPosition;
volatile int32_t oldStepPosition;
volatile uint8_t enabled;
volatile int32_t nSteps;
volatile float Tstart;
volatile float Tstop;
volatile float Tstep;
HardwareTimer *MyTim;
HardwareTimer *MyTim2;
HardwareTimer *MyTim2; // 10,11,13,14
int16_t stepsPerMM;
uint8_t dirPin;
PinName stepPin;
@@ -34,10 +32,11 @@ public:
static uint32_t sync0CycleTime;
volatile uint32_t lcncCycleTime; // Linuxcnc nominal cycle time (1 ms often)
StepGen2(TIM_TypeDef *Timer, TIM_TypeDef *Timer2, uint32_t _timerChannel, PinName _stepPin, uint8_t _dirPin, void irq(void));
StepGen2(TIM_TypeDef *Timer, uint32_t _timerChannel, PinName _stepPin, uint8_t _dirPin, void irq(void), TIM_TypeDef *Timer2, void irq2(void));
uint32_t handleStepper(void);
void timerCB();
void timer2CB();
void enable(uint8_t yes);
void reqPos(double_t pos) { requestedPosition = pos; };

View File

@@ -2,13 +2,8 @@
#include <stdio.h>
#include "StepGen2.h"
StepGen2::StepGen2(TIM_TypeDef *Timer, TIM_TypeDef *Timer2, uint32_t _timerChannel, PinName _stepPin, uint8_t _dirPin, void irq(void))
StepGen2::StepGen2(TIM_TypeDef *Timer, uint32_t _timerChannel, PinName _stepPin, uint8_t _dirPin, void irq(void), TIM_TypeDef *Timer2, void irq2(void))
{
timerIsRunning = 0;
timerStepPosition = 0;
timerStepDirection = 0;
timerStepPositionAtEnd = 0;
timerNewEndStepPosition = 0;
actualPosition = 0;
requestedPosition = 0;
oldPosition = 0;
@@ -23,6 +18,7 @@ StepGen2::StepGen2(TIM_TypeDef *Timer, TIM_TypeDef *Timer2, uint32_t _timerChann
MyTim->attachInterrupt(irq);
pinMode(dirPin, OUTPUT);
MyTim2 = new HardwareTimer(Timer2);
MyTim2->attachInterrupt(irq2);
}
uint32_t StepGen2::handleStepper(void)
@@ -66,7 +62,20 @@ uint32_t StepGen2::handleStepper(void)
if (Tstart > lcncCycleTime) // Not enough movement to make a step
return updatePosAndReturn(stepPosStop, 6); // Check this again
// Tstart, Tstep and Tstop defines the coming pwm-sequence.
return 0; // Always do one pulse at Tstart when we come here. Next Tstart+Tstep and so on until Tstop.
//
MyTim2->setOverflow(Tstart + Tjitter, MICROSEC_FORMAT); // All handled by irqs
MyTim2->resume();
return updatePosAndReturn(stepPosStop, 0);
}
void StepGen2::timer2CB()
{
MyTim2->pause(); // Once is enough.
MyTim->setMode(timerChan, TIMER_OUTPUT_COMPARE_PWM2, stepPin);
MyTim->setOverflow(floor(1e6 / Tstep), HERTZ_FORMAT); // 100000 microseconds = 100 milliseconds
MyTim->setCaptureCompare(timerChan, 50, PERCENT_COMPARE_FORMAT); // 50%
nSteps = round((Tstop - Tstart) / Tstep + 1);
if (nSteps > 0)
MyTim->resume();
}
void StepGen2::timerCB()
{
@@ -102,6 +111,7 @@ void StepGen2::timerCB()
}
#endif
}
uint32_t StepGen2::updatePosAndReturn(int32_t stepPosStop, uint32_t i)
{ //
oldPos(reqPos()); // Save the numeric position for next step

View File

@@ -34,8 +34,10 @@ void timerCallbackStep2(void)
#endif
#include "StepGen2.h"
void timerCallbackStep(void);
StepGen2 Step(TIM1, TIM10, 4, PA_11, PA12, timerCallbackStep);
void timerCallbackStepStart(void);
StepGen2 Step(TIM1, 4, PA_11, PA12, timerCallbackStep, TIM10, timerCallbackStepStart);
void timerCallbackStep(void) { Step.timerCB(); }
void timerCallbackStepStart(void) { Step.timer2CB(); }
CircularBuffer<uint32_t, 200> Tim;
volatile uint64_t nowTime = 0, thenTime = 0;