-
Notifications
You must be signed in to change notification settings - Fork 0
/
clk-freq_baud-rate.cpp
53 lines (43 loc) · 1.65 KB
/
clk-freq_baud-rate.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "avr/uart/detail/math.hpp"
#include <iostream>
#include <string>
#include <stdint.h>
using namespace avr::uart::detail;
constexpr auto bit_length_cycles(uint32_t clk, uint32_t baud_rate)
{ return clk*1.0/baud_rate; }
constexpr auto bit_length_cycles_error(uint32_t clk, uint32_t baud_rate) {
auto c = clk*1.0/baud_rate;
return (math::abs(c - math::round(c)) / c) * 100;
}
constexpr auto one_half_bit_length_cycles_error(uint32_t clk, uint32_t baud_rate) {
auto c = clk*1.5/baud_rate;
return (math::abs(c - math::round(c)) / c) * 100;
}
int main(int argc, char** argv) {
using namespace std;
if(argc != 3) {
cout
<< "usage: clk-freq_baud-rate <cpu_clock_hz> <bps>"
<< endl
<< "example: clk-freq_baud-rate 8000000 38400"
<< endl;
return 1;
}
auto clk = stoul(argv[1]);
auto baud_rate = stoul(argv[2]);
cout
<< "CPU frequency: " << clk << " Hz" << endl
<< "Baud rate: " << baud_rate << " bps" << endl << endl;
cout
<< "Bit length in CPU cycles (c): " << clk*1.0/baud_rate << " cycles" << endl
<< "Rounded bit length in CPU cycles (rc): " << unsigned(math::round(clk*1.0/baud_rate))
<< " cycles" << endl
<< " relative error (|c - rc|)/c: " << bit_length_cycles_error(clk, baud_rate) << " %" << endl
<< endl;
cout
<< "1.5 Bit length in CPU cycles (c): " << 1.5 * bit_length_cycles(clk, baud_rate) << " cycles" << endl
<< "Rounded 1.5 bit length in CPU cycles (rc): " << unsigned(math::round(1.5 * bit_length_cycles(clk, baud_rate)))
<< " cycles" << endl
<< " relative error (|c - rc|)/c: " << one_half_bit_length_cycles_error(clk, baud_rate) << " %" << endl
<< endl;
}