Skip to content

Commit 0e9d47d

Browse files
authored
Merge pull request #75 from ramboerik/feature-stm32f4
Added support for stm32f4
2 parents 06c0eab + 64791f8 commit 0e9d47d

23 files changed

+248
-56
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Windows image file caches
1+
# Windows image file caches
22
Thumbs.db
33
ehthumbs.db
44

@@ -52,3 +52,7 @@ $RECYCLE.BIN/
5252
Network Trash Folder
5353
Temporary Items
5454
.apdisk
55+
56+
57+
.pio
58+
.vscode

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Please note: This is version 2 of the library. This version has a new user inter
77

88

99
## Purpose of the Library
10-
**TeensyStep** is an efficient Arduino library compatible with Teensy 3.0, 3.1, 3.2, 3.5 and 3.6. The library is able to handle synchronous and independent movement and continuous rotation of steppers with pulse rates of up to 300'000 steps per second. The following table shows a summary of the **TeensyStep** specification:
10+
**TeensyStep** is an efficient Arduino library compatible with Teensy 3.0, 3.1, 3.2, 3.5, 3.6 and STM32F4. The library is able to handle synchronous and independent movement and continuous rotation of steppers with pulse rates of up to 300'000 steps per second. The following table shows a summary of the **TeensyStep** specification:
1111

1212
| Description | Specification | Default |
1313
|:-------------------------------------------|:-------------------------:|:----------------:|

library.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "TeensyStep",
3-
"keywords": "teensy, stepper, motor, high-speed",
4-
"description": "High speed stepper driver for teensy boards (T3.0 - T3.6)",
3+
"keywords": "teensy, stm32f4, stepper, motor, high-speed",
4+
"description": "High speed stepper driver for teensy boards (T3.0 - T3.6) and STM32F4",
55
"repository":
66
{
77
"type": "git",
@@ -17,5 +17,5 @@
1717
"homepage": "https://luni64.github.io/TeensyStep",
1818
"version": "2.1",
1919
"frameworks": "arduino",
20-
"platforms": "Teensy"
20+
"platforms": ["Teensy", "stm32"]
2121
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name=TeensyStep
22
version=2.1
33
author=Lutz Niggl <lutz.niggl@lunoptics.com>
44
maintainer=Lutz Niggl <lutz.niggl@lunoptics.com>
5-
sentence=High speed stepper driver for PJRC Teensy boards (T3.0 - T3.6)
5+
sentence=High speed stepper driver for PJRC Teensy boards (T3.0 - T3.6) and STM32F4
66
paragraph= Step rates up to 300000stp/sec. Accelerated and synchronized movement of up to 10 steppers. Due to the low processor load it can easily be used togehter with sensors, displays, serial communication ...
77
category=Device Control
88
url=https://luni64.github.io/TeensyStep/

src/ErrorHandler.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#include "ErrorHandler.h"
21
#include <Arduino.h>
2+
#include "ErrorHandler.h"
33

44
namespace TeensyStep
5-
{
6-
static Stream* stream;
5+
{
6+
static Stream* stream;
77

88
static void vHandler(int module, int code)
9-
{
9+
{
1010
const char* mod;
1111
const char* txt;
1212

@@ -20,16 +20,16 @@ namespace TeensyStep
2020
case pitErr::argErr: txt = "BUG, argument error"; break;
2121
case pitErr::notAllocated:txt = "BUG, timer not allocated"; break;
2222
default: txt = "unknown"; break;
23-
}
24-
break;
23+
}
24+
break;
2525

2626
case errModule::MC: //------------------------------------------
2727
mod = "CTRL";
2828
switch ((mcErr)code)
2929
{
3030
case mcErr::alrdyMoving: txt = "Started while moving"; break;
3131
default: txt = "unknown"; break;
32-
}
32+
}
3333
break;
3434

3535
default: //------------------------------------------------------
@@ -41,7 +41,7 @@ namespace TeensyStep
4141

4242
while (true)
4343
{
44-
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
44+
digitalToggle(LED_BUILTIN);
4545
delay(50);
4646
}
4747
}
@@ -53,4 +53,4 @@ namespace TeensyStep
5353
}
5454

5555
errCallback_t* ErrorHandler::callback = nullptr;
56-
}
56+
}

src/ErrorHandler.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace TeensyStep
1212
{
1313
public:
1414
static int error(errModule module, int code)
15-
{
15+
{
1616
if (callback != nullptr) callback((int)module, code);
1717
return code;
1818
}
@@ -27,19 +27,19 @@ namespace TeensyStep
2727
enum class errModule {
2828
PIT = 1,
2929
MC,
30-
RB
30+
RB
3131
};
3232

3333
enum class pitErr {
3434
OK,
3535
argErr,
3636
notAllocated,
3737
outOfTimers,
38-
38+
3939
};
4040

4141
enum class mcErr {
4242
OK,
43-
alrdyMoving,
43+
alrdyMoving,
4444
};
45-
}
45+
}

src/RotateControlBase.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include "MotorControlBase.h"
4-
#include "core_pins.h"
53
#include <algorithm>
4+
#include <Arduino.h>
5+
#include "MotorControlBase.h"
66

77
namespace TeensyStep
88
{
@@ -166,4 +166,4 @@ namespace TeensyStep
166166
delay(1);
167167
}
168168
}
169-
}
169+
}

src/Stepper.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
#include <Arduino.h>
12
#include "Stepper.h"
2-
#include "core_pins.h"
33

44
namespace TeensyStep
55
{
@@ -18,6 +18,7 @@ namespace TeensyStep
1818

1919
Stepper& Stepper::setStepPinPolarity(int polarity)
2020
{
21+
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
2122
// Calculate adresses of bitbanded pin-set and pin-clear registers
2223
uint32_t pinRegAddr = (uint32_t)digital_pin_to_info_PGM[stepPin].reg; //GPIO_PDOR
2324
uint32_t* pinSetReg = (uint32_t*)(pinRegAddr + 4 * 32); //GPIO_PSOR = GPIO_PDOR + 4
@@ -33,12 +34,18 @@ namespace TeensyStep
3334
stepPinActiveReg = pinSetReg;
3435
stepPinInactiveReg = pinClearReg;
3536
}
37+
#else
38+
this->polarity = polarity;
39+
#endif
40+
clearStepPin(); // set step pin to inactive state
41+
return *this;
3642
clearStepPin(); // set step pin to inactive state
3743
return *this;
3844
}
3945

4046
Stepper& Stepper::setInverseRotation(bool reverse)
4147
{
48+
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
4249
// Calculate adresses of bitbanded pin-set and pin-clear registers
4350
uint32_t pinRegAddr = (uint32_t)digital_pin_to_info_PGM[dirPin].reg; //GPIO_PDOR
4451
uint32_t* pinSetReg = (uint32_t*)(pinRegAddr + 4 * 32); //GPIO_PSOR = GPIO_PDOR + 4
@@ -53,6 +60,9 @@ namespace TeensyStep
5360
dirPinCwReg = pinSetReg;
5461
dirPinCcwReg = pinClearReg;
5562
}
63+
#else
64+
this->reverse = reverse;
65+
#endif
5666
return *this;
5767
}
5868

src/Stepper.h

+27-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ namespace TeensyStep
5555
static bool cmpVmax(const Stepper* a, const Stepper* b) { return std::abs(a->vMax) > std::abs(b->vMax); }
5656

5757
// Pin & Dir registers
58+
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
5859
volatile uint32_t* stepPinActiveReg;
5960
volatile uint32_t* stepPinInactiveReg;
6061
volatile uint32_t* dirPinCwReg;
6162
volatile uint32_t* dirPinCcwReg;
63+
#else
64+
volatile uint8_t polarity;
65+
volatile uint8_t reverse;
66+
#endif
6267
const int stepPin, dirPin;
6368

6469
// Friends
@@ -73,12 +78,13 @@ namespace TeensyStep
7378
};
7479

7580
// Inline implementation -----------------------------------------
76-
81+
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
7782
void Stepper::doStep()
7883
{
7984
*stepPinActiveReg = 1;
8085
current += dir;
8186
}
87+
8288
void Stepper::clearStepPin() const
8389
{
8490
*stepPinInactiveReg = 1;
@@ -87,11 +93,29 @@ namespace TeensyStep
8793
void Stepper::setDir(int d)
8894
{
8995
dir = d;
90-
dir == 1 ? * dirPinCwReg = 1 : * dirPinCcwReg = 1;
96+
dir == 1 ? *dirPinCwReg = 1 : *dirPinCcwReg = 1;
97+
}
98+
#else
99+
void Stepper::doStep()
100+
{
101+
digitalWrite(stepPin, polarity);
102+
current += dir;
103+
}
104+
105+
void Stepper::clearStepPin() const
106+
{
107+
digitalWrite(stepPin, !polarity);
108+
}
109+
110+
void Stepper::setDir(int d)
111+
{
112+
dir = d;
113+
digitalWrite(dirPin, dir == 1 ? reverse : !reverse);
91114
}
115+
#endif
92116

93117
void Stepper::toggleDir()
94118
{
95119
setDir(-dir);
96120
}
97-
}
121+
}

src/TeensyStep.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
//STM32 ====================================================================================================
2424

25-
#elif defined(__STM32_TBD__)
26-
#include "timers/STM32/TimerField.h"
25+
#elif defined(STM32F4xx)
26+
#include "timer/stm32/TimerField.h"
2727

2828
//Some other hardware ======================================================================================
2929

src/accelerators/LinStepAccelerator.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int32_t LinStepAccelerator::prepareMovement(int32_t currentPos, int32_t targetPo
6868
// hack, call some error callback instead
6969
while (1)
7070
{
71-
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
71+
digitalToggle(LED_BUILTIN);
7272
delay(25);
7373
}
7474
}
@@ -123,4 +123,4 @@ uint32_t LinStepAccelerator::initiateStopping(int32_t curPos)
123123
{
124124
return ds - stepsDone; // return steps to go
125125
}
126-
}
126+
}

src/accelerators/SinRotAccelerator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SinRotAccelerator
2929

3030
inline float signed_sqrt(int32_t x) // signed square root
3131
{
32-
return x > 0 ? sqrtf(x) : -sqrtf(-x);
32+
return x > 0 ? sqrt(x) : -sqrt(-x);
3333
}
3434
};
3535

src/timer/TimerFieldBase.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class ITimerField
44
{
55
public:
6-
ITimerField(TF_Handler *);
6+
ITimerField(TeensyStep::TF_Handler *);
77

88
virtual bool begin()=0;
99
virtual void end()=0;
@@ -19,4 +19,4 @@ class ITimerField
1919

2020
virtual void setPulseWidth(unsigned delay)=0;
2121
virtual void triggerDelay()=0;
22-
};
22+
};

src/timer/generic/TickTimer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ void std::__throw_bad_function_call()
55
while(1);
66
}
77

8+
float PeriodicTimer::minFrequency = (float)F_CPU / std::numeric_limits<uint32_t>::max();
89
TimerBase* TimerControl::firstTimer = nullptr;
9-
TimerBase* TimerControl::lastTimer = nullptr;
10+
TimerBase* TimerControl::lastTimer = nullptr;

0 commit comments

Comments
 (0)