-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclocks.cpp
172 lines (153 loc) · 3.66 KB
/
clocks.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "clocks.hpp"
bool clk::initPll96 = false;
bool clk::initTCC0_TCC1 = false,
clk::initTCC2_TC3 = false,
clk::initTC4_TC5 = false;
std::uint32_t clk::clkTCC0_TCC1{}, clk::clkTCC2_TC3{}, clk::clkTC4_TC5{};
void clk::pll96(std::uint8_t gclkid, std::uint32_t srcFreq)
{
if (clk::isPll96())
{
return;
}
assert(gclkid <= 0x08);
assert(srcFreq <= 48000000U);
const auto divFactor = (96000000U << 4U) / srcFreq;
// Feed src to GCLK_DPLL
GCLK->CLKCTRL.reg =
GCLK_CLKCTRL_CLKEN |
GCLK_CLKCTRL_GEN(std::uint32_t(gclkid)) |
GCLK_CLKCTRL_ID(1);
while (GCLK->STATUS.bit.SYNCBUSY);
// Calculate division factor
// Set DPLL ratio to 8 MHz * (11 + 1) = 96 MHz
SYSCTRL->DPLLRATIO.reg =
SYSCTRL_DPLLRATIO_LDRFRAC(divFactor & 0x0F) | // Fractional ratio
SYSCTRL_DPLLRATIO_LDR((divFactor >> 4U) - 1U); // Integral ratio
// Configure DPLL to disregard phase lock and select GCLK as source
SYSCTRL->DPLLCTRLB.reg =
SYSCTRL_DPLLCTRLB_LBYPASS | // Bypass lock
SYSCTRL_DPLLCTRLB_WUF | // Wake up fast
SYSCTRL_DPLLCTRLB_REFCLK(SYSCTRL_DPLLCTRLB_REFCLK_GCLK_Val); // Select GCLK
// Enable DPLL
SYSCTRL->DPLLCTRLA.reg |= SYSCTRL_DPLLCTRLA_ENABLE;
clk::initPll96 = true;
}
bool clk::isPll96()
{
return clk::initPll96;
}
bool clk::isInit(tmr timer)
{
switch (timer)
{
case tmr::tTCC0:
case tmr::tTCC1:
return clk::initTCC0_TCC1;
case tmr::tTCC2:
case tmr::tTC3:
return clk::initTCC2_TC3;
case tmr::tTC4:
case tmr::tTC5:
return clk::initTC4_TC5;
default:
assert(!"Impossible error to reach!");
}
}
std::uint32_t clk::tmrSpeed(tmr timer)
{
switch (timer)
{
case tmr::tTCC0:
case tmr::tTCC1:
return clk::clkTCC0_TCC1;
case tmr::tTCC2:
case tmr::tTC3:
return clk::clkTCC2_TC3;
case tmr::tTC4:
case tmr::tTC5:
return clk::clkTC4_TC5;
default:
assert(!"Impossible error to reach!");
}
}
void clk::initGCLK(std::uint8_t gclkid, std::uint8_t src, std::uint32_t prescaler, bool expPrescaler)
{
assert(gclkid <= 0x08);
assert(src <= 0x08);
if ( ((prescaler > 1) && !expPrescaler) ||
((prescaler >= 1) && expPrescaler) )
{
GCLK->GENDIV.reg =
GCLK_GENDIV_DIV(prescaler - (expPrescaler != 0)) |
GCLK_GENDIV_ID(std::uint32_t(gclkid));
while (GCLK->STATUS.bit.SYNCBUSY);
}
else
{
expPrescaler = false;
}
GCLK->GENCTRL.reg =
GCLK_GENCTRL_IDC |
GCLK_GENCTRL_GENEN |
GCLK_GENCTRL_SRC(std::uint32_t(src)) |
(expPrescaler ? GCLK_GENCTRL_DIVSEL : 0) |
GCLK_GENCTRL_ID(std::uint32_t(gclkid));
while (GCLK->STATUS.bit.SYNCBUSY);
}
void clk::initTmr(tmr timer, std::uint8_t id, std::uint32_t srcFreq)
{
if (isInit(timer))
{
return;
}
assert(id <= 0x08);
std::uint32_t tmrId;
// Enable timer peripheral
switch (timer)
{
case tmr::tTCC0:
case tmr::tTCC1:
tmrId = GCLK_CLKCTRL_ID_TCC0_TCC1;
PM->APBCMASK.reg |= PM_APBCMASK_TCC0 | PM_APBCMASK_TCC1;
break;
case tmr::tTCC2:
case tmr::tTC3:
tmrId = GCLK_CLKCTRL_ID_TCC2_TC3;
PM->APBCMASK.reg |= PM_APBCMASK_TCC2 | PM_APBCMASK_TC3;
break;
case tmr::tTC4:
case tmr::tTC5:
tmrId = GCLK_CLKCTRL_ID_TC4_TC5;
PM->APBCMASK.reg |= PM_APBCMASK_TC4 | PM_APBCMASK_TC5;
break;
default:
assert(!"Impossible error to reach!");
}
// Enable timer clock
GCLK->CLKCTRL.reg =
GCLK_CLKCTRL_CLKEN |
GCLK_CLKCTRL_GEN(std::uint32_t(id)) |
tmrId;
while (GCLK->STATUS.bit.SYNCBUSY);
switch (timer)
{
case tmr::tTCC0:
case tmr::tTCC1:
clk::initTCC0_TCC1 = true;
clk::clkTCC0_TCC1 = srcFreq;
break;
case tmr::tTCC2:
case tmr::tTC3:
clk::initTCC2_TC3 = true;
clk::clkTCC2_TC3 = srcFreq;
break;
case tmr::tTC4:
case tmr::tTC5:
clk::initTC4_TC5 = true;
clk::clkTC4_TC5 = srcFreq;
break;
default:
assert(!"Impossible error to reach!");
}
}