Drive a stepper motor using A4988 with asynchronous. No wait() function.

Files at this revision

API Documentation at this revision

Comitter:
babylonica
Date:
Sat Jul 28 18:22:05 2018 +0000
Parent:
0:26fa30c35f74
Commit message:
minor changes

Changed in this revision

AsyncStepper.cpp Show annotated file Show diff for this revision Revisions of this file
AsyncStepper.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/AsyncStepper.cpp	Thu Jul 12 01:27:04 2018 +0000
+++ b/AsyncStepper.cpp	Sat Jul 28 18:22:05 2018 +0000
@@ -46,6 +46,8 @@
 
 void AsyncStepper::ISR_PULSEOUT( void )
 {
+  DISABLE_INTERRUPTS;
+  
   if ( !stopPulseOut ) {
     stepOut->write( !stepOut->read() );
 
@@ -58,16 +60,20 @@
     }
   }
   
+  RESTORE_INTERRUPTS;
   return;
 }
 
 void AsyncStepper::SetRPM( uint32_t rpm )
 {
+  DISABLE_INTERRUPTS;
+  
   ticker->detach();
   oneRotationSteps = oneRotationFullSteps * ( stepMode + 1 );
   pulseWidth_us = (uint64_t)( 30000000 / ( rpm * oneRotationSteps ) );
   ticker->attach_us( callback(this, &AsyncStepper::ISR_PULSEOUT), pulseWidth_us );
   
+  RESTORE_INTERRUPTS;
   return;
 }
 
@@ -101,11 +107,14 @@
 
 void AsyncStepper::Rotate( direction_e direction, uint32_t steps )
 {
+  DISABLE_INTERRUPTS;
+  
   dirOut->write( direction );
   
   AsyncStepper::Enable();
   currentMaxStepCount = steps;
   stopPulseOut = false;
   
+  RESTORE_INTERRUPTS;
   return;
 }
--- a/AsyncStepper.hpp	Thu Jul 12 01:27:04 2018 +0000
+++ b/AsyncStepper.hpp	Sat Jul 28 18:22:05 2018 +0000
@@ -17,6 +17,17 @@
 
 #include "mbed.h"
 
+/**
+	 Disable all interuupts and save the status of interrupts.
+	 This macro is usable at ONLY Cortex-M Series. 
+*/
+#define DISABLE_INTERRUPTS uint32_t primask = __get_PRIMASK();	__disable_irq()
+/**
+	 Enable all interuupts when the status of interrupts is ENABLED.
+	 This macro is usable at ONLY Cortex-M Series. 
+*/
+#define RESTORE_INTERRUPTS if( !(primask & 1) )	__enable_irq()
+
 using namespace std;
 
 enum stepMode_e {
@@ -49,12 +60,13 @@
   stopMode_e stopMode;
   uint32_t oneRotationSteps;
   uint64_t pulseWidth_us;
-  uint32_t halfPulseCount;
-  uint32_t currentMaxStepCount;
-  bool stopPulseOut;
+  volatile uint32_t halfPulseCount;
+  volatile uint32_t currentMaxStepCount;
+  volatile bool stopPulseOut;
   
   void ISR_PULSEOUT( void );
   
+  
 public:
   /*!
     @brief Create a new AsyncStepper port.