Guard ISR-processed data in ATOMIC_BLOCK

pull/157/head
D.R.racer 2022-02-22 14:33:54 +01:00
parent 8e8a17c34e
commit 90be0f8665
2 changed files with 63 additions and 51 deletions

View File

@ -1,6 +1,12 @@
/// @file pulse_gen.cpp /// @file pulse_gen.cpp
#include "pulse_gen.h" #include "pulse_gen.h"
#ifdef AVR
#include <util/atomic.h>
#else
#define ATOMIC_BLOCK(x) /**/
#endif
namespace modules { namespace modules {
namespace pulse_gen { namespace pulse_gen {
@ -90,6 +96,7 @@ void PulseGen::CalculateTrapezoid(block_t *block, steps_t entry_speed, steps_t e
} }
bool PulseGen::PlanMoveTo(pos_t target, steps_t feed_rate, steps_t end_rate) { bool PulseGen::PlanMoveTo(pos_t target, steps_t feed_rate, steps_t end_rate) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// Prepare to set up new block // Prepare to set up new block
if (block_index.full()) if (block_index.full())
return false; return false;
@ -123,12 +130,14 @@ bool PulseGen::PlanMoveTo(pos_t target, steps_t feed_rate, steps_t end_rate) {
// Move forward and update the state // Move forward and update the state
block_index.push(); block_index.push();
}
position = target; position = target;
return true; return true;
} }
pos_t PulseGen::CurPosition() const { pos_t PulseGen::CurPosition() const {
pos_t cur_pos = position; pos_t cur_pos = position;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { // manipulating current_block needs sync
circular_index_t iter = block_index; circular_index_t iter = block_index;
// if we have a live block remove the partial offset // if we have a live block remove the partial offset
@ -142,7 +151,7 @@ pos_t PulseGen::CurPosition() const {
cur_pos -= BlockShift(&block_buffer[iter.front()]); cur_pos -= BlockShift(&block_buffer[iter.front()]);
iter.pop(); iter.pop();
} }
}
return cur_pos; return cur_pos;
} }
@ -150,6 +159,7 @@ void PulseGen::AbortPlannedMoves(bool halt) {
// always update to effective position // always update to effective position
position = CurPosition(); position = CurPosition();
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { // manipulating current_block needs sync
// destroy the current block // destroy the current block
if (current_block) { if (current_block) {
last_rate = acc_step_rate; last_rate = acc_step_rate;
@ -157,10 +167,11 @@ void PulseGen::AbortPlannedMoves(bool halt) {
while (!block_index.empty()) // drop all remaining blocks while (!block_index.empty()) // drop all remaining blocks
block_index.pop(); block_index.pop();
} }
}
// truncate the last rate if halting // truncate the last rate if halting
if (halt) if (halt) {
last_rate = 0; last_rate = 0;
}
} }
} // namespace motor } // namespace motor

View File

@ -74,6 +74,7 @@ public:
bool Full() const { return block_index.full(); } bool Full() const { return block_index.full(); }
/// Single-step the axis /// Single-step the axis
/// Called from ISR
/// @returns the interval for the next tick /// @returns the interval for the next tick
inline st_timer_t Step(const hal::tmc2130::MotorParams &motorParams) { inline st_timer_t Step(const hal::tmc2130::MotorParams &motorParams) {
if (!current_block) { if (!current_block) {