-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
51 lines (42 loc) · 1.06 KB
/
utils.cpp
File metadata and controls
51 lines (42 loc) · 1.06 KB
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
#include "utils.h"
void set32MHzClock(const CLK_PSADIV_t divider) {
//Start 32 Mhz clock and wait it ti stabilize
OSC.CTRL = OSC_RC32MEN_bm;
while (!(OSC.STATUS & OSC_RC32MRDY_bm));
// Set divider
CCP = CCP_IOREG_gc;
CLK.PSCTRL = divider;
// Switch to 32 Mhz clock
CCP = CCP_IOREG_gc;
CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
}
uint32_t muldiv(uint32_t value, const uint32_t multiplier, const uint32_t divider, uint32_t& remainder )
{
uint32_t q = 0; // the quotient
uint32_t r = 0; // the remainder
uint32_t qn = multiplier / divider;
uint32_t rn = multiplier % divider;
while(value)
{
if (value & 1)
{
q += qn;
r += rn;
if (r >= divider)
{
q++;
r -= divider;
}
}
value >>= 1;
qn <<= 1;
rn <<= 1;
if (rn >= divider)
{
qn++;
rn -= divider;
}
}
remainder = r;
return q;
}