-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_equations.hpp
39 lines (34 loc) · 977 Bytes
/
timer_equations.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <defs.hpp>
#include <types.hpp>
#include <units.hpp>
namespace timer {
/*
Given timer input frequency, desired frequency, timer counts
return prescaler value.
*/
CONSTFN constexpr u32
calc_prescaler(const units::Frequency & fin, const units::Frequency & fout, const u32 counts)
{
return fin / (fout * counts);
}
/*
Given timer input frequency, desired frequency, timer prescaler,
return number of timer counts required.
*/
CONSTFN constexpr u32
calc_counts(const units::Frequency & fin, const units::Frequency & fout, const u32 presc)
{
return fin / (fout * presc);
}
/*
Given timer frequency and desired frequency,
return value of overflow register.
Note -1 which account for overflow.
*/
CONSTFN constexpr u32
calc_top(const units::Frequency & fin, const units::Frequency & fout)
{
return calc_counts(fin, fout, 1) - 1;
}
}