Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / libs / StepTicker.h
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8
9
10 #ifndef STEPTICKER_H
11 #define STEPTICKER_H
12
13 #include <stdint.h>
14 #include <vector>
15 #include <bitset>
16 #include <functional>
17 #include <atomic>
18
19 class StepperMotor;
20
21 class StepTicker{
22 public:
23 static StepTicker* global_step_ticker;
24
25 StepTicker();
26 ~StepTicker();
27 void set_frequency( float frequency );
28 void signal_a_move_finished();
29 void set_reset_delay( float seconds );
30 int register_motor(StepperMotor* motor);
31 void add_motor_to_active_list(StepperMotor* motor);
32 void remove_motor_from_active_list(StepperMotor* motor);
33 void set_acceleration_ticks_per_second(uint32_t acceleration_ticks_per_second);
34 float get_frequency() const { return frequency; }
35 void unstep_tick();
36 uint32_t get_tick_cnt() const { return tick_cnt; }
37 uint32_t ticks_since(uint32_t last) const { return (tick_cnt>=last) ? tick_cnt-last : (UINT32_MAX-last) + tick_cnt + 1; }
38
39 void TIMER0_IRQHandler (void);
40 void PendSV_IRQHandler (void);
41 void register_acceleration_tick_handler(std::function<void(void)> cb){
42 acceleration_tick_handlers.push_back(cb);
43 }
44 void acceleration_tick();
45 void synchronize_acceleration(bool fire_now);
46
47 void start();
48
49 friend class StepperMotor;
50
51 private:
52 float frequency;
53 uint32_t period;
54 volatile uint32_t tick_cnt;
55 std::vector<std::function<void(void)>> acceleration_tick_handlers;
56 std::vector<StepperMotor*> motor;
57 std::bitset<32> active_motor; // limit to 32 motors
58 std::bitset<32> unstep; // limit to 32 motors
59 std::atomic_uchar do_move_finished;
60 uint8_t num_motors;
61 volatile bool a_move_finished;
62 };
63
64
65
66 #endif