MHAA Timer Core v1.0.0
Wrapping timer counts and optional control
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1#ifndef MHAA_TIMER_HPP
2#define MHAA_TIMER_HPP
3
4#include <stdint.h>
5#include <timer/counter.hpp>
6#include <timer/clock_control.hpp>
7#include <timer/compare_control.hpp>
8#include <timer/input_capture.hpp>
9#include <timer/types.hpp>
10
11namespace mhaa
12{
13 namespace timer
14 {
16 class Timer
17 {
18 public:
19 explicit Timer(Counter &counter, CompareControl *compare = nullptr,
20 InputCapture *capture = nullptr)
21 : counter_(counter), clock_(nullptr), compare_(compare), capture_(capture) {}
22
23 explicit Timer(ClockControl &counter, CompareControl *compare = nullptr,
24 InputCapture *capture = nullptr)
25 : counter_(counter), clock_(&counter), compare_(compare), capture_(capture) {}
26
27 void start() { counter_.start(); }
28 void stop() { counter_.stop(); }
29 Count ticks() const { return counter_.value(); }
30
31 uint8_t width_bits() const { return counter_.width_bits(); }
32 bool set_ticks(Count count) { return counter_.set_value(count); }
33
34 bool set_tick_frequency_hz(uint32_t frequency_hz)
35 {
36 return clock_ && frequency_hz != 0 && clock_->set_tick_frequency_hz(frequency_hz);
37 }
38
39 bool set_compare(ChannelId channel, Count threshold)
40 {
41 return compare_ && compare_->set_compare(channel, threshold);
42 }
43
45 bool try_capture(Count &count) { return capture_ && capture_->try_capture(count); }
46
48 Count elapsed_since(Count previous) const
49 {
50 const uint8_t width = width_bits();
51 const Count elapsed = ticks() - previous;
52 return width == 32 ? elapsed : elapsed & ((Count{1} << width) - 1);
53 }
54
55 private:
56 Counter &counter_;
57 ClockControl *clock_;
58 CompareControl *compare_;
59 InputCapture *capture_;
60 };
61 }
62}
63
64#endif
uint8_t width_bits() const
Definition timer.hpp:31
bool set_compare(ChannelId channel, Count threshold)
Definition timer.hpp:39
bool set_tick_frequency_hz(uint32_t frequency_hz)
Definition timer.hpp:34
bool try_capture(Count &count)
Preserves count if capture is unavailable or no event is ready.
Definition timer.hpp:45
Timer(ClockControl &counter, CompareControl *compare=nullptr, InputCapture *capture=nullptr)
Definition timer.hpp:23
Timer(Counter &counter, CompareControl *compare=nullptr, InputCapture *capture=nullptr)
Definition timer.hpp:19
Count ticks() const
Definition timer.hpp:29
bool set_ticks(Count count)
Definition timer.hpp:32
Count elapsed_since(Count previous) const
Computes elapsed ticks modulo the counter width.
Definition timer.hpp:48
Definition timer.hpp:12