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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
//go:build stm32 && stm32l4
// +build stm32,stm32l4
package runtime
import (
"device/stm32"
"machine"
)
const (
PWR_CR1_VOS_0 = 1 << stm32.PWR_CR1_VOS_Pos
PWR_CR1_VOS_1 = 2 << stm32.PWR_CR1_VOS_Pos
PWR_REGULATOR_VOLTAGE_SCALE1 = PWR_CR1_VOS_0
PWR_REGULATOR_VOLTAGE_SCALE2 = PWR_CR1_VOS_1
FLASH_LATENCY_0 = 0
FLASH_LATENCY_1 = 1
FLASH_LATENCY_2 = 2
FLASH_LATENCY_3 = 3
FLASH_LATENCY_4 = 4
RCC_PLLP_DIV2 = 2
RCC_PLLP_DIV7 = 7
RCC_PLLQ_DIV2 = 2
RCC_PLLR_DIV2 = 2
RCC_CFGR_SWS_MSI = 0x0
RCC_CFGR_SWS_PLL = 0xC
RCC_PLLSOURCE_MSI = 1
RCC_PLL_SYSCLK = stm32.RCC_PLLCFGR_PLLREN
)
type arrtype = uint32
func init() {
initCLK()
machine.InitSerial()
initTickTimer(&machine.TIM15)
}
func putchar(c byte) {
machine.Serial.WriteByte(c)
}
func getchar() byte {
for machine.Serial.Buffered() == 0 {
Gosched()
}
v, _ := machine.Serial.ReadByte()
return v
}
func buffered() int {
return machine.Serial.Buffered()
}
func initCLK() {
// PWR_CLK_ENABLE
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_PWREN)
_ = stm32.RCC.APB1ENR1.Get()
// Disable Backup domain protection
if !stm32.PWR.CR1.HasBits(stm32.PWR_CR1_DBP) {
stm32.PWR.CR1.SetBits(stm32.PWR_CR1_DBP)
for !stm32.PWR.CR1.HasBits(stm32.PWR_CR1_DBP) {
}
}
// Set LSE Drive to LOW
stm32.RCC.BDCR.ReplaceBits(0, stm32.RCC_BDCR_LSEDRV_Msk, 0)
// Initialize the High-Speed External Oscillator
initOsc()
// PWR_VOLTAGESCALING_CONFIG
stm32.PWR.CR1.ReplaceBits(0, stm32.PWR_CR1_VOS_Msk, 0)
_ = stm32.PWR.CR1.Get()
// Set flash wait states (min 5 latency units) based on clock
if (stm32.FLASH.ACR.Get() & 0xF) < 5 {
stm32.FLASH.ACR.ReplaceBits(5, 0xF, 0)
}
// Ensure HCLK does not exceed max during transition
stm32.RCC.CFGR.ReplaceBits(8<<stm32.RCC_CFGR_HPRE_Pos, stm32.RCC_CFGR_HPRE_Msk, 0)
// Set SYSCLK source and wait
// (3 = RCC_SYSCLKSOURCE_PLLCLK, 2=RCC_CFGR_SWS_Pos)
stm32.RCC.CFGR.ReplaceBits(3, stm32.RCC_CFGR_SW_Msk, 0)
for stm32.RCC.CFGR.Get()&(3<<2) != (3 << 2) {
}
// Set HCLK
// (0 = RCC_SYSCLKSOURCE_PLLCLK)
stm32.RCC.CFGR.ReplaceBits(0, stm32.RCC_CFGR_HPRE_Msk, 0)
// Set flash wait states (max 5 latency units) based on clock
if (stm32.FLASH.ACR.Get() & 0xF) > 5 {
stm32.FLASH.ACR.ReplaceBits(5, 0xF, 0)
}
// Set APB1 and APB2 clocks (0 = DIV1)
stm32.RCC.CFGR.ReplaceBits(0, stm32.RCC_CFGR_PPRE1_Msk, 0)
stm32.RCC.CFGR.ReplaceBits(0, stm32.RCC_CFGR_PPRE2_Msk, 0)
}
func initOsc() {
sysclkSource := stm32.RCC.CFGR.Get() & stm32.RCC_CFGR_SWS_Msk
pllConfig := stm32.RCC.PLLCFGR.Get() & stm32.RCC_PLLCFGR_PLLSRC_Msk
// Enable MSI, adjusting flash latency
if sysclkSource == RCC_CFGR_SWS_MSI ||
(sysclkSource == RCC_CFGR_SWS_PLL && pllConfig == RCC_PLLSOURCE_MSI) {
if MSIRANGE > getMSIRange() {
setFlashLatencyFromMSIRange(MSIRANGE)
setMSIFreq(MSIRANGE, 0)
} else {
setMSIFreq(MSIRANGE, 0)
if sysclkSource == RCC_CFGR_SWS_MSI {
setFlashLatencyFromMSIRange(MSIRANGE)
}
}
} else {
stm32.RCC.CR.SetBits(stm32.RCC_CR_MSION)
for !stm32.RCC.CR.HasBits(stm32.RCC_CR_MSIRDY) {
}
setMSIFreq(MSIRANGE, 0)
}
// Enable LSE, wait until ready
stm32.RCC.BDCR.SetBits(stm32.RCC_BDCR_LSEON)
for !stm32.RCC.BDCR.HasBits(stm32.RCC_BDCR_LSEON) {
}
// Disable the PLL, wait until disabled
stm32.RCC.CR.ClearBits(stm32.RCC_CR_PLLON)
for stm32.RCC.CR.HasBits(stm32.RCC_CR_PLLRDY) {
}
// Configure the PLL
stm32.RCC.PLLCFGR.ReplaceBits(
(1)| // 1 = RCC_PLLSOURCE_MSI
(PLL_M-1)<<stm32.RCC_PLLCFGR_PLLM_Pos|
(PLL_N<<stm32.RCC_PLLCFGR_PLLN_Pos)|
(((PLL_Q>>1)-1)<<stm32.RCC_PLLCFGR_PLLQ_Pos)|
(((PLL_R>>1)-1)<<stm32.RCC_PLLCFGR_PLLR_Pos)|
(PLL_P<<stm32.RCC_PLLCFGR_PLLP_Pos),
stm32.RCC_PLLCFGR_PLLSRC_Msk|stm32.RCC_PLLCFGR_PLLM_Msk|
stm32.RCC_PLLCFGR_PLLN_Msk|stm32.RCC_PLLCFGR_PLLP_Msk|
stm32.RCC_PLLCFGR_PLLR_Msk|stm32.RCC_PLLCFGR_PLLP_Msk,
0)
// Enable the PLL and PLL System Clock Output, wait until ready
stm32.RCC.CR.SetBits(stm32.RCC_CR_PLLON)
stm32.RCC.PLLCFGR.SetBits(stm32.RCC_PLLCFGR_PLLREN) // = RCC_PLL_SYSCLK
for !stm32.RCC.CR.HasBits(stm32.RCC_CR_PLLRDY) {
}
// Enable system clock output
stm32.RCC.PLLCFGR.SetBits(RCC_PLL_SYSCLK)
}
func getMSIRange() uint32 {
if stm32.RCC.CR.HasBits(stm32.RCC_CR_MSIRGSEL) {
return (stm32.RCC.CR.Get() & stm32.RCC_CR_MSIRANGE_Msk) >> stm32.RCC_CR_MSIRANGE_Pos
}
return (stm32.RCC.CSR.Get() & stm32.RCC_CSR_MSISRANGE_Msk) >> stm32.RCC_CSR_MSISRANGE_Pos
}
func setMSIFreq(r uint32, calibration uint32) {
stm32.RCC.CR.SetBits(stm32.RCC_CR_MSIRGSEL)
stm32.RCC.CR.ReplaceBits(r<<stm32.RCC_CR_MSIRANGE_Pos, stm32.RCC_CR_MSIRANGE_Msk, 0)
stm32.RCC.ICSCR.ReplaceBits(calibration<<stm32.RCC_ICSCR_MSITRIM_Pos, stm32.RCC_ICSCR_MSITRIM_Msk, 0)
}
func setFlashLatencyFromMSIRange(r uint32) {
var vos uint32
if pwrIsClkEnabled() {
vos = pwrExGetVoltageRange()
} else {
pwrClkEnable()
vos = pwrExGetVoltageRange()
pwrClkDisable()
}
latency := uint32(FLASH_LATENCY_0)
if vos == PWR_REGULATOR_VOLTAGE_SCALE1 {
if r > stm32.RCC_CR_MSIRANGE_Range16M {
if r > stm32.RCC_CR_MSIRANGE_Range32M {
latency = FLASH_LATENCY_2
} else {
latency = FLASH_LATENCY_1
}
}
} else if r > stm32.RCC_CR_MSIRANGE_Range16M {
latency = FLASH_LATENCY_3
} else {
if r == stm32.RCC_CR_MSIRANGE_Range16M {
latency = FLASH_LATENCY_2
} else if r == stm32.RCC_CR_MSIRANGE_Range8M {
latency = FLASH_LATENCY_1
}
}
stm32.FLASH.ACR.ReplaceBits(latency, stm32.Flash_ACR_LATENCY_Msk, 0)
}
func pwrIsClkEnabled() bool {
return stm32.RCC.APB1ENR1.HasBits(stm32.RCC_APB1ENR1_PWREN)
}
func pwrClkEnable() {
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_PWREN)
}
func pwrClkDisable() {
stm32.RCC.APB1ENR1.ClearBits(stm32.RCC_APB1ENR1_PWREN)
}
func pwrExGetVoltageRange() uint32 {
return stm32.PWR.CR1.Get() & stm32.PWR_CR1_VOS_Msk
}
|