Skip to content

Commit

Permalink
Merge pull request #75 from ramboerik/feature-stm32f4
Browse files Browse the repository at this point in the history
Added support for stm32f4
  • Loading branch information
luni64 authored Oct 5, 2020
2 parents 06c0eab + 64791f8 commit 0e9d47d
Show file tree
Hide file tree
Showing 23 changed files with 248 additions and 56 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Windows image file caches
# Windows image file caches
Thumbs.db
ehthumbs.db

Expand Down Expand Up @@ -52,3 +52,7 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk


.pio
.vscode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Please note: This is version 2 of the library. This version has a new user inter


## Purpose of the Library
**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:
**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:

| Description | Specification | Default |
|:-------------------------------------------|:-------------------------:|:----------------:|
Expand Down
6 changes: 3 additions & 3 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "TeensyStep",
"keywords": "teensy, stepper, motor, high-speed",
"description": "High speed stepper driver for teensy boards (T3.0 - T3.6)",
"keywords": "teensy, stm32f4, stepper, motor, high-speed",
"description": "High speed stepper driver for teensy boards (T3.0 - T3.6) and STM32F4",
"repository":
{
"type": "git",
Expand All @@ -17,5 +17,5 @@
"homepage": "https://luni64.github.io/TeensyStep",
"version": "2.1",
"frameworks": "arduino",
"platforms": "Teensy"
"platforms": ["Teensy", "stm32"]
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name=TeensyStep
version=2.1
author=Lutz Niggl <[email protected]>
maintainer=Lutz Niggl <[email protected]>
sentence=High speed stepper driver for PJRC Teensy boards (T3.0 - T3.6)
sentence=High speed stepper driver for PJRC Teensy boards (T3.0 - T3.6) and STM32F4
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 ...
category=Device Control
url=https://luni64.github.io/TeensyStep/
Expand Down
18 changes: 9 additions & 9 deletions src/ErrorHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "ErrorHandler.h"
#include <Arduino.h>
#include "ErrorHandler.h"

namespace TeensyStep
{
static Stream* stream;
{
static Stream* stream;

static void vHandler(int module, int code)
{
{
const char* mod;
const char* txt;

Expand All @@ -20,16 +20,16 @@ namespace TeensyStep
case pitErr::argErr: txt = "BUG, argument error"; break;
case pitErr::notAllocated:txt = "BUG, timer not allocated"; break;
default: txt = "unknown"; break;
}
break;
}
break;

case errModule::MC: //------------------------------------------
mod = "CTRL";
switch ((mcErr)code)
{
case mcErr::alrdyMoving: txt = "Started while moving"; break;
default: txt = "unknown"; break;
}
}
break;

default: //------------------------------------------------------
Expand All @@ -41,7 +41,7 @@ namespace TeensyStep

while (true)
{
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
digitalToggle(LED_BUILTIN);
delay(50);
}
}
Expand All @@ -53,4 +53,4 @@ namespace TeensyStep
}

errCallback_t* ErrorHandler::callback = nullptr;
}
}
10 changes: 5 additions & 5 deletions src/ErrorHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace TeensyStep
{
public:
static int error(errModule module, int code)
{
{
if (callback != nullptr) callback((int)module, code);
return code;
}
Expand All @@ -27,19 +27,19 @@ namespace TeensyStep
enum class errModule {
PIT = 1,
MC,
RB
RB
};

enum class pitErr {
OK,
argErr,
notAllocated,
outOfTimers,

};

enum class mcErr {
OK,
alrdyMoving,
alrdyMoving,
};
}
}
6 changes: 3 additions & 3 deletions src/RotateControlBase.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "MotorControlBase.h"
#include "core_pins.h"
#include <algorithm>
#include <Arduino.h>
#include "MotorControlBase.h"

namespace TeensyStep
{
Expand Down Expand Up @@ -166,4 +166,4 @@ namespace TeensyStep
delay(1);
}
}
}
}
12 changes: 11 additions & 1 deletion src/Stepper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <Arduino.h>
#include "Stepper.h"
#include "core_pins.h"

namespace TeensyStep
{
Expand All @@ -18,6 +18,7 @@ namespace TeensyStep

Stepper& Stepper::setStepPinPolarity(int polarity)
{
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
// Calculate adresses of bitbanded pin-set and pin-clear registers
uint32_t pinRegAddr = (uint32_t)digital_pin_to_info_PGM[stepPin].reg; //GPIO_PDOR
uint32_t* pinSetReg = (uint32_t*)(pinRegAddr + 4 * 32); //GPIO_PSOR = GPIO_PDOR + 4
Expand All @@ -33,12 +34,18 @@ namespace TeensyStep
stepPinActiveReg = pinSetReg;
stepPinInactiveReg = pinClearReg;
}
#else
this->polarity = polarity;
#endif
clearStepPin(); // set step pin to inactive state
return *this;
clearStepPin(); // set step pin to inactive state
return *this;
}

Stepper& Stepper::setInverseRotation(bool reverse)
{
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
// Calculate adresses of bitbanded pin-set and pin-clear registers
uint32_t pinRegAddr = (uint32_t)digital_pin_to_info_PGM[dirPin].reg; //GPIO_PDOR
uint32_t* pinSetReg = (uint32_t*)(pinRegAddr + 4 * 32); //GPIO_PSOR = GPIO_PDOR + 4
Expand All @@ -53,6 +60,9 @@ namespace TeensyStep
dirPinCwReg = pinSetReg;
dirPinCcwReg = pinClearReg;
}
#else
this->reverse = reverse;
#endif
return *this;
}

Expand Down
30 changes: 27 additions & 3 deletions src/Stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ namespace TeensyStep
static bool cmpVmax(const Stepper* a, const Stepper* b) { return std::abs(a->vMax) > std::abs(b->vMax); }

// Pin & Dir registers
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
volatile uint32_t* stepPinActiveReg;
volatile uint32_t* stepPinInactiveReg;
volatile uint32_t* dirPinCwReg;
volatile uint32_t* dirPinCcwReg;
#else
volatile uint8_t polarity;
volatile uint8_t reverse;
#endif
const int stepPin, dirPin;

// Friends
Expand All @@ -73,12 +78,13 @@ namespace TeensyStep
};

// Inline implementation -----------------------------------------

#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
void Stepper::doStep()
{
*stepPinActiveReg = 1;
current += dir;
}

void Stepper::clearStepPin() const
{
*stepPinInactiveReg = 1;
Expand All @@ -87,11 +93,29 @@ namespace TeensyStep
void Stepper::setDir(int d)
{
dir = d;
dir == 1 ? * dirPinCwReg = 1 : * dirPinCcwReg = 1;
dir == 1 ? *dirPinCwReg = 1 : *dirPinCcwReg = 1;
}
#else
void Stepper::doStep()
{
digitalWrite(stepPin, polarity);
current += dir;
}

void Stepper::clearStepPin() const
{
digitalWrite(stepPin, !polarity);
}

void Stepper::setDir(int d)
{
dir = d;
digitalWrite(dirPin, dir == 1 ? reverse : !reverse);
}
#endif

void Stepper::toggleDir()
{
setDir(-dir);
}
}
}
4 changes: 2 additions & 2 deletions src/TeensyStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

//STM32 ====================================================================================================

#elif defined(__STM32_TBD__)
#include "timers/STM32/TimerField.h"
#elif defined(STM32F4xx)
#include "timer/stm32/TimerField.h"

//Some other hardware ======================================================================================

Expand Down
4 changes: 2 additions & 2 deletions src/accelerators/LinStepAccelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int32_t LinStepAccelerator::prepareMovement(int32_t currentPos, int32_t targetPo
// hack, call some error callback instead
while (1)
{
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
digitalToggle(LED_BUILTIN);
delay(25);
}
}
Expand Down Expand Up @@ -123,4 +123,4 @@ uint32_t LinStepAccelerator::initiateStopping(int32_t curPos)
{
return ds - stepsDone; // return steps to go
}
}
}
2 changes: 1 addition & 1 deletion src/accelerators/SinRotAccelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SinRotAccelerator

inline float signed_sqrt(int32_t x) // signed square root
{
return x > 0 ? sqrtf(x) : -sqrtf(-x);
return x > 0 ? sqrt(x) : -sqrt(-x);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/timer/TimerFieldBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class ITimerField
{
public:
ITimerField(TF_Handler *);
ITimerField(TeensyStep::TF_Handler *);

virtual bool begin()=0;
virtual void end()=0;
Expand All @@ -19,4 +19,4 @@ class ITimerField

virtual void setPulseWidth(unsigned delay)=0;
virtual void triggerDelay()=0;
};
};
3 changes: 2 additions & 1 deletion src/timer/generic/TickTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ void std::__throw_bad_function_call()
while(1);
}

float PeriodicTimer::minFrequency = (float)F_CPU / std::numeric_limits<uint32_t>::max();
TimerBase* TimerControl::firstTimer = nullptr;
TimerBase* TimerControl::lastTimer = nullptr;
TimerBase* TimerControl::lastTimer = nullptr;
Loading

0 comments on commit 0e9d47d

Please sign in to comment.