Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp8266: add an ISR-driven example to protect from WiFi operations #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/Bounce-ESP8266-ISR/Bounce-ESP8266-ISR.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Bounce.pde
// -*- mode: C++ -*-
//
// Make a single stepper bounce from one limit to another
//
// Copyright (C) 2012 Mike McCauley
// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

// Define a stepper and the pins it will use
#define DIR 12 // esp8266 gpio
#define STEP 13 // esp8266 gpio
AccelStepper stepper(AccelStepper::DRIVER, STEP, DIR);

IRAM_ATTR
void stepperRun ()
{
stepper.run();
}

void setup()
{
// Change these to suit your stepper if you want
stepper.setMaxSpeed(10000);
stepper.setAcceleration(10000);
stepper.moveTo(500);

// initialize ISR
timer1_isr_init();
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_attachInterrupt(stepperRun);
timer1_write(microsecondsToClockCycles(100));
}

void loop()
{
// cli() / sei() barriers are necessary when timer is enabled
cli();

// If at the end of travel go to the other end
if (stepper.distanceToGo() == 0)
stepper.moveTo(-stepper.currentPosition());

sei(); // barrier ends

delay(1000); // ISR does not mind long delays or WiFi operations
}
13 changes: 13 additions & 0 deletions src/AccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void AccelStepper::move(long relative)
// Implements steps according to the current step interval
// You must call this at least once per step
// returns true if a step occurred
ISR_ATTR
boolean AccelStepper::runSpeed()
{
// Dont do anything unless we actually have a step interval
Expand Down Expand Up @@ -69,6 +70,7 @@ boolean AccelStepper::runSpeed()
}
}

ISR_ATTR
long AccelStepper::distanceToGo()
{
return _targetPos - _currentPos;
Expand All @@ -95,6 +97,7 @@ void AccelStepper::setCurrentPosition(long position)
}

// Subclasses can override
ISR_ATTR
unsigned long AccelStepper::computeNewSpeed()
{
long distanceTo = distanceToGo(); // +ve is clockwise from curent location
Expand Down Expand Up @@ -182,6 +185,7 @@ unsigned long AccelStepper::computeNewSpeed()
// You must call this at least once per step, preferably in your main loop
// If the motor is in the desired position, the cost is very small
// returns true if the motor is still running to the target position.
ISR_ATTR
boolean AccelStepper::run()
{
if (runSpeed())
Expand Down Expand Up @@ -325,6 +329,7 @@ float AccelStepper::speed()
}

// Subclasses can override
ISR_ATTR
void AccelStepper::step(long step)
{
switch (_interface)
Expand Down Expand Up @@ -381,6 +386,7 @@ long AccelStepper::stepBackward()
// bit 0 of the mask corresponds to _pin[0]
// bit 1 of the mask corresponds to _pin[1]
// ....
ISR_ATTR
void AccelStepper::setOutputPins(uint8_t mask)
{
uint8_t numpins = 2;
Expand All @@ -394,6 +400,7 @@ void AccelStepper::setOutputPins(uint8_t mask)
}

// 0 pin step function (ie for functional usage)
ISR_ATTR
void AccelStepper::step0(long step)
{
(void)(step); // Unused
Expand All @@ -406,6 +413,7 @@ void AccelStepper::step0(long step)
// 1 pin step function (ie for stepper drivers)
// This is passed the current step number (0 to 7)
// Subclasses can override
ISR_ATTR
void AccelStepper::step1(long step)
{
(void)(step); // Unused
Expand All @@ -423,6 +431,7 @@ void AccelStepper::step1(long step)
// 2 pin step function
// This is passed the current step number (0 to 7)
// Subclasses can override
ISR_ATTR
void AccelStepper::step2(long step)
{
switch (step & 0x3)
Expand All @@ -447,6 +456,7 @@ void AccelStepper::step2(long step)
// 3 pin step function
// This is passed the current step number (0 to 7)
// Subclasses can override
ISR_ATTR
void AccelStepper::step3(long step)
{
switch (step % 3)
Expand All @@ -469,6 +479,7 @@ void AccelStepper::step3(long step)
// 4 pin step function for half stepper
// This is passed the current step number (0 to 7)
// Subclasses can override
ISR_ATTR
void AccelStepper::step4(long step)
{
switch (step & 0x3)
Expand All @@ -494,6 +505,7 @@ void AccelStepper::step4(long step)
// 3 pin half step function
// This is passed the current step number (0 to 7)
// Subclasses can override
ISR_ATTR
void AccelStepper::step6(long step)
{
switch (step % 6)
Expand Down Expand Up @@ -528,6 +540,7 @@ void AccelStepper::step6(long step)
// 4 pin half step function
// This is passed the current step number (0 to 7)
// Subclasses can override
ISR_ATTR
void AccelStepper::step8(long step)
{
switch (step & 0x7)
Expand Down
7 changes: 7 additions & 0 deletions src/AccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@
#define YIELD
#endif

// on archs defining IRAM_ATTR (esp8266, esp32), use this attribute for functions callable from ISR
#ifdef IRAM_ATTR
#define ISR_ATTR IRAM_ATTR
#else
#define ISR_ATTR
#endif

/////////////////////////////////////////////////////////////////////
/// \class AccelStepper AccelStepper.h <AccelStepper.h>
/// \brief Support for stepper motors with acceleration etc.
Expand Down
1 change: 1 addition & 0 deletions src/MultiStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void MultiStepper::moveTo(long absolute[])
}

// Returns true if any motor is still running to the target position.
ISR_ATTR
boolean MultiStepper::run()
{
uint8_t i;
Expand Down