libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
rcc.c
Go to the documentation of this file.
1/** @defgroup rcc_file RCC peripheral API
2 *
3 * @ingroup peripheral_apis
4 *
5 * @brief <b>libopencm3 STM32G4xx Reset and Clock Control</b>
6 *
7 * @author @htmlonly &copy; @endhtmlonly 2009 Federico Ruiz-Ugalde <memeruiz at gmail dot com>
8 * @author @htmlonly &copy; @endhtmlonly 2009 Uwe Hermann <uwe@hermann-uwe.de>
9 * @author @htmlonly &copy; @endhtmlonly 2010 Thomas Otto <tommi@viadmin.org>
10 * @author @htmlonly &copy; @endhtmlonly 2013 Frantisek Burian <BuFran at seznam.cz>
11 * @author @htmlonly &copy; @endhtmlonly 2020 Sam Kirkham <sam.kirkham@codethink.co.uk>
12 * @author @htmlonly &copy; @endhtmlonly 2020 Ben Brewer <ben.brewer@codethink.co.uk>
13 *
14 * @date 30 July 2020
15 *
16 * This library supports the Reset and Clock Control System in the STM32 series
17 * of ARM Cortex Microcontrollers by ST Microelectronics.
18 *
19 * LGPL License Terms @ref lgpl_license
20 */
21
22/*
23 * This file is part of the libopencm3 project.
24 *
25 * Copyright (C) 2009 Federico Ruiz-Ugalde <memeruiz at gmail dot com>
26 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
27 * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
28 * Copyright (C) 2013 Frantisek Burian <BuFran at seznam.cz>
29 * Copyright (C) 2020 Sam Kirkham <sam.kirkham@codethink.co.uk>
30 * Copyright (C) 2020 Ben Brewer <ben.brewer@codethink.co.uk>
31 *
32 * This library is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU Lesser General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
36 *
37 * This library is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU Lesser General Public License for more details.
41 *
42 * You should have received a copy of the GNU Lesser General Public License
43 * along with this library. If not, see <http://www.gnu.org/licenses/>.
44 */
45
46
51
52/**@{*/
53
54/* Set the default clock frequencies after reset. */
55uint32_t rcc_ahb_frequency = 16000000;
56uint32_t rcc_apb1_frequency = 16000000;
57uint32_t rcc_apb2_frequency = 16000000;
58
60 { /* 24MHz */
61 .pllm = 2,
62 .plln = 12,
63 .pllp = 0,
64 .pllq = 2,
65 .pllr = 4,
66 .pll_source = RCC_PLLCFGR_PLLSRC_HSI16,
67 .hpre = RCC_CFGR_HPRE_NODIV,
68 .ppre1 = RCC_CFGR_PPREx_NODIV,
69 .ppre2 = RCC_CFGR_PPREx_NODIV,
70 .vos_scale = PWR_SCALE2,
71 .boost = false,
72 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
73 .flash_waitstates = 1,
74 .ahb_frequency = 24e6,
75 .apb1_frequency = 24e6,
76 .apb2_frequency = 24e6,
77 },
78 { /* 48MHz */
79 .pllm = 2,
80 .plln = 12,
81 .pllp = 0,
82 .pllq = 2,
83 .pllr = 2,
84 .pll_source = RCC_PLLCFGR_PLLSRC_HSI16,
85 .hpre = RCC_CFGR_HPRE_NODIV,
86 .ppre1 = RCC_CFGR_PPREx_NODIV,
87 .ppre2 = RCC_CFGR_PPREx_NODIV,
88 .vos_scale = PWR_SCALE1,
89 .boost = false,
90 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
91 .flash_waitstates = 1,
92 .ahb_frequency = 48e6,
93 .apb1_frequency = 48e6,
94 .apb2_frequency = 48e6,
95 },
96 { /* 96MHz */
97 .pllm = 2,
98 .plln = 24,
99 .pllp = 0,
100 .pllq = 4,
101 .pllr = 2,
102 .pll_source = RCC_PLLCFGR_PLLSRC_HSI16,
103 .hpre = RCC_CFGR_HPRE_NODIV,
104 .ppre1 = RCC_CFGR_PPREx_NODIV,
105 .ppre2 = RCC_CFGR_PPREx_NODIV,
106 .vos_scale = PWR_SCALE1,
107 .boost = false,
108 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
109 .flash_waitstates = 3,
110 .ahb_frequency = 96e6,
111 .apb1_frequency = 96e6,
112 .apb2_frequency = 96e6,
113 },
114 { /* 170MHz */
115 .pllm = 4,
116 .plln = 85,
117 .pllp = 0,
118 .pllq = 0, /* USB requires CRS at this speed. */
119 .pllr = 2,
120 .pll_source = RCC_PLLCFGR_PLLSRC_HSI16,
121 .hpre = RCC_CFGR_HPRE_NODIV,
122 .ppre1 = RCC_CFGR_PPREx_NODIV,
123 .ppre2 = RCC_CFGR_PPREx_NODIV,
124 .vos_scale = PWR_SCALE1,
125 .boost = true,
126 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
127 .flash_waitstates = 4,
128 .ahb_frequency = 170e6,
129 .apb1_frequency = 170e6,
130 .apb2_frequency = 170e6,
131 },
132};
133
135 { /* 24MHz */
136 .pllm = 1,
137 .plln = 12,
138 .pllp = 0,
139 .pllq = 2,
140 .pllr = 4,
141 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
142 .hpre = RCC_CFGR_HPRE_NODIV,
143 .ppre1 = RCC_CFGR_PPREx_NODIV,
144 .ppre2 = RCC_CFGR_PPREx_NODIV,
145 .vos_scale = PWR_SCALE2,
146 .boost = false,
147 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
148 .flash_waitstates = 1,
149 .ahb_frequency = 24e6,
150 .apb1_frequency = 24e6,
151 .apb2_frequency = 24e6,
152 },
153 { /* 48MHz */
154 .pllm = 1,
155 .plln = 12,
156 .pllp = 0,
157 .pllq = 2,
158 .pllr = 2,
159 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
160 .hpre = RCC_CFGR_HPRE_NODIV,
161 .ppre1 = RCC_CFGR_PPREx_NODIV,
162 .ppre2 = RCC_CFGR_PPREx_NODIV,
163 .vos_scale = PWR_SCALE1,
164 .boost = false,
165 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
166 .flash_waitstates = 1,
167 .ahb_frequency = 48e6,
168 .apb1_frequency = 48e6,
169 .apb2_frequency = 48e6,
170 },
171 { /* 96MHz */
172 .pllm = 1,
173 .plln = 24,
174 .pllp = 0,
175 .pllq = 4,
176 .pllr = 2,
177 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
178 .hpre = RCC_CFGR_HPRE_NODIV,
179 .ppre1 = RCC_CFGR_PPREx_NODIV,
180 .ppre2 = RCC_CFGR_PPREx_NODIV,
181 .vos_scale = PWR_SCALE1,
182 .boost = false,
183 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
184 .flash_waitstates = 3,
185 .ahb_frequency = 96e6,
186 .apb1_frequency = 96e6,
187 .apb2_frequency = 96e6,
188 },
189 { /* 170MHz */
190 .pllm = 2,
191 .plln = 85,
192 .pllp = 0,
193 .pllq = 0, /* USB requires CRS at this speed. */
194 .pllr = 2,
195 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
196 .hpre = RCC_CFGR_HPRE_NODIV,
197 .ppre1 = RCC_CFGR_PPREx_NODIV,
198 .ppre2 = RCC_CFGR_PPREx_NODIV,
199 .vos_scale = PWR_SCALE1,
200 .boost = true,
201 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
202 .flash_waitstates = 4,
203 .ahb_frequency = 170e6,
204 .apb1_frequency = 170e6,
205 .apb2_frequency = 170e6,
206 },
207};
208
209
211 { /* 24MHz */
212 .pllm = 2,
213 .plln = 16,
214 .pllp = 0,
215 .pllq = 2,
216 .pllr = 4,
217 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
218 .hpre = RCC_CFGR_HPRE_NODIV,
219 .ppre1 = RCC_CFGR_PPREx_NODIV,
220 .ppre2 = RCC_CFGR_PPREx_NODIV,
221 .vos_scale = PWR_SCALE2,
222 .boost = false,
223 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
224 .flash_waitstates = 1,
225 .ahb_frequency = 24e6,
226 .apb1_frequency = 24e6,
227 .apb2_frequency = 24e6,
228 },
229 { /* 48MHz */
230 .pllm = 2,
231 .plln = 16,
232 .pllp = 0,
233 .pllq = 2,
234 .pllr = 2,
235 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
236 .hpre = RCC_CFGR_HPRE_NODIV,
237 .ppre1 = RCC_CFGR_PPREx_NODIV,
238 .ppre2 = RCC_CFGR_PPREx_NODIV,
239 .vos_scale = PWR_SCALE1,
240 .boost = false,
241 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
242 .flash_waitstates = 1,
243 .ahb_frequency = 48e6,
244 .apb1_frequency = 48e6,
245 .apb2_frequency = 48e6,
246 },
247 { /* 96MHz */
248 .pllm = 2,
249 .plln = 32,
250 .pllp = 0,
251 .pllq = 4,
252 .pllr = 2,
253 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
254 .hpre = RCC_CFGR_HPRE_NODIV,
255 .ppre1 = RCC_CFGR_PPREx_NODIV,
256 .ppre2 = RCC_CFGR_PPREx_NODIV,
257 .vos_scale = PWR_SCALE1,
258 .boost = false,
259 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
260 .flash_waitstates = 3,
261 .ahb_frequency = 96e6,
262 .apb1_frequency = 96e6,
263 .apb2_frequency = 96e6,
264 },
265 { /* 170MHz */
266 .pllm = 3,
267 .plln = 85,
268 .pllp = 0,
269 .pllq = 0, /* USB requires CRS at this speed. */
270 .pllr = 2,
271 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
272 .hpre = RCC_CFGR_HPRE_NODIV,
273 .ppre1 = RCC_CFGR_PPREx_NODIV,
274 .ppre2 = RCC_CFGR_PPREx_NODIV,
275 .vos_scale = PWR_SCALE1,
276 .boost = true,
277 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
278 .flash_waitstates = 4,
279 .ahb_frequency = 170e6,
280 .apb1_frequency = 170e6,
281 .apb2_frequency = 170e6,
282 },
283};
284
286 { /* 24MHz */
287 .pllm = 2,
288 .plln = 12,
289 .pllp = 0,
290 .pllq = 2,
291 .pllr = 4,
292 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
293 .hpre = RCC_CFGR_HPRE_NODIV,
294 .ppre1 = RCC_CFGR_PPREx_NODIV,
295 .ppre2 = RCC_CFGR_PPREx_NODIV,
296 .vos_scale = PWR_SCALE2,
297 .boost = false,
298 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
299 .flash_waitstates = 1,
300 .ahb_frequency = 24e6,
301 .apb1_frequency = 24e6,
302 .apb2_frequency = 24e6,
303 },
304 { /* 48MHz */
305 .pllm = 2,
306 .plln = 12,
307 .pllp = 0,
308 .pllq = 2,
309 .pllr = 2,
310 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
311 .hpre = RCC_CFGR_HPRE_NODIV,
312 .ppre1 = RCC_CFGR_PPREx_NODIV,
313 .ppre2 = RCC_CFGR_PPREx_NODIV,
314 .vos_scale = PWR_SCALE1,
315 .boost = false,
316 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
317 .flash_waitstates = 1,
318 .ahb_frequency = 48e6,
319 .apb1_frequency = 48e6,
320 .apb2_frequency = 48e6,
321 },
322 { /* 96MHz */
323 .pllm = 2,
324 .plln = 24,
325 .pllp = 0,
326 .pllq = 4,
327 .pllr = 2,
328 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
329 .hpre = RCC_CFGR_HPRE_NODIV,
330 .ppre1 = RCC_CFGR_PPREx_NODIV,
331 .ppre2 = RCC_CFGR_PPREx_NODIV,
332 .vos_scale = PWR_SCALE1,
333 .boost = false,
334 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
335 .flash_waitstates = 3,
336 .ahb_frequency = 96e6,
337 .apb1_frequency = 96e6,
338 .apb2_frequency = 96e6,
339 },
340 { /* 170MHz */
341 .pllm = 4,
342 .plln = 85,
343 .pllp = 0,
344 .pllq = 0, /* USB requires CRS at this speed. */
345 .pllr = 2,
346 .pll_source = RCC_PLLCFGR_PLLSRC_HSE,
347 .hpre = RCC_CFGR_HPRE_NODIV,
348 .ppre1 = RCC_CFGR_PPREx_NODIV,
349 .ppre2 = RCC_CFGR_PPREx_NODIV,
350 .vos_scale = PWR_SCALE1,
351 .boost = true,
352 .flash_config = FLASH_ACR_DCEN | FLASH_ACR_ICEN,
353 .flash_waitstates = 4,
354 .ahb_frequency = 170e6,
355 .apb1_frequency = 170e6,
356 .apb2_frequency = 170e6,
357 },
358};
359
360
361
363{
364 switch (osc) {
365 case RCC_HSI48:
367 break;
368 case RCC_PLL:
370 break;
371 case RCC_HSE:
373 break;
374 case RCC_HSI16:
376 break;
377 case RCC_LSE:
379 break;
380 case RCC_LSI:
382 break;
383 }
384}
385
387{
388 switch (osc) {
389 case RCC_HSI48:
391 break;
392 case RCC_PLL:
394 break;
395 case RCC_HSE:
397 break;
398 case RCC_HSI16:
400 break;
401 case RCC_LSE:
403 break;
404 case RCC_LSI:
406 break;
407 }
408}
409
411{
412 switch (osc) {
413 case RCC_HSI48:
414 RCC_CIER &= ~RCC_CIER_HSI48RDYIE;
415 break;
416 case RCC_PLL:
417 RCC_CIER &= ~RCC_CIER_PLLRDYIE;
418 break;
419 case RCC_HSE:
420 RCC_CIER &= ~RCC_CIER_HSERDYIE;
421 break;
422 case RCC_HSI16:
423 RCC_CIER &= ~RCC_CIER_HSIRDYIE;
424 break;
425 case RCC_LSE:
426 RCC_CIER &= ~RCC_CIER_LSERDYIE;
427 break;
428 case RCC_LSI:
429 RCC_CIER &= ~RCC_CIER_LSIRDYIE;
430 break;
431 }
432}
433
435{
436 switch (osc) {
437 case RCC_HSI48:
438 return ((RCC_CIFR & RCC_CIFR_HSI48RDYF) != 0);
439 case RCC_PLL:
440 return ((RCC_CIFR & RCC_CIFR_PLLRDYF) != 0);
441 case RCC_HSE:
442 return ((RCC_CIFR & RCC_CIFR_HSERDYF) != 0);
443 case RCC_HSI16:
444 return ((RCC_CIFR & RCC_CIFR_HSIRDYF) != 0);
445 case RCC_LSE:
446 return ((RCC_CIFR & RCC_CIFR_LSERDYF) != 0);
447 case RCC_LSI:
448 return ((RCC_CIFR & RCC_CIFR_LSIRDYF) != 0);
449 }
450 return 0;
451}
452
454{
456}
457
459{
460 return ((RCC_CIFR & RCC_CIFR_CSSF) != 0);
461}
462
464{
465 switch (osc) {
466 case RCC_HSI48:
468 case RCC_PLL:
469 return RCC_CR & RCC_CR_PLLRDY;
470 case RCC_HSE:
471 return RCC_CR & RCC_CR_HSERDY;
472 case RCC_HSI16:
473 return RCC_CR & RCC_CR_HSIRDY;
474 case RCC_LSE:
475 return RCC_BDCR & RCC_BDCR_LSERDY;
476 case RCC_LSI:
477 return RCC_CSR & RCC_CSR_LSIRDY;
478 }
479 return false;
480}
481
483{
484 while (!rcc_is_osc_ready(osc));
485}
486
488{
489 switch (osc) {
490 case RCC_PLL:
493 break;
494 case RCC_HSE:
497 break;
498 case RCC_HSI16:
501 break;
502 default:
503 /* Shouldn't be reached. */
504 break;
505 }
506}
507
508void rcc_osc_on(enum rcc_osc osc)
509{
510 switch (osc) {
511 case RCC_HSI48:
513 break;
514 case RCC_PLL:
516 break;
517 case RCC_HSE:
519 break;
520 case RCC_HSI16:
522 break;
523 case RCC_LSE:
525 break;
526 case RCC_LSI:
528 break;
529 }
530}
531
532void rcc_osc_off(enum rcc_osc osc)
533{
534 switch (osc) {
535 case RCC_HSI48:
536 RCC_CRRCR &= ~RCC_CRRCR_HSI48ON;
537 break;
538 case RCC_PLL:
539 RCC_CR &= ~RCC_CR_PLLON;
540 break;
541 case RCC_HSE:
542 RCC_CR &= ~RCC_CR_HSEON;
543 break;
544 case RCC_HSI16:
545 RCC_CR &= ~RCC_CR_HSION;
546 break;
547 case RCC_LSE:
548 RCC_BDCR &= ~RCC_BDCR_LSEON;
549 break;
550 case RCC_LSI:
551 RCC_CSR &= ~RCC_CSR_LSION;
552 break;
553 }
554}
555
557{
559}
560
562{
563 RCC_CR &= ~RCC_CR_CSSON;
564}
565
566void rcc_set_sysclk_source(uint32_t clk)
567{
568 uint32_t reg32;
569
570 reg32 = RCC_CFGR;
571 reg32 &= ~(RCC_CFGR_SW_MASK << RCC_CFGR_SW_SHIFT);
572 RCC_CFGR = (reg32 | (clk << RCC_CFGR_SW_SHIFT));
573}
574
575void rcc_set_pll_source(uint32_t pllsrc)
576{
577 uint32_t reg32;
578
579 reg32 = RCC_PLLCFGR;
581 RCC_PLLCFGR = (reg32 | (pllsrc << RCC_PLLCFGR_PLLSRC_SHIFT));
582}
583
584void rcc_set_ppre2(uint32_t ppre2)
585{
586 uint32_t reg32;
587
588 reg32 = RCC_CFGR;
590 RCC_CFGR = (reg32 | (ppre2 << RCC_CFGR_PPRE2_SHIFT));
591}
592
593void rcc_set_ppre1(uint32_t ppre1)
594{
595 uint32_t reg32;
596
597 reg32 = RCC_CFGR;
599 RCC_CFGR = (reg32 | (ppre1 << RCC_CFGR_PPRE1_SHIFT));
600}
601
602void rcc_set_hpre(uint32_t hpre)
603{
604 uint32_t reg32;
605
606 reg32 = RCC_CFGR;
608 RCC_CFGR = (reg32 | (hpre << RCC_CFGR_HPRE_SHIFT));
609}
610
611/**
612 * Reconfigures the main PLL for a HSE source.
613 * Any reserved bits are kept at their reset values.
614 * @param pllsrc Source for the main PLL input clock
615 * @param pllm Divider for the main PLL input clock
616 * @param plln Main PLL multiplication factor for VCO
617 * @param pllp Main PLL divider for ADC
618 * @param pllq Main PLL divider for QUADSPI, FDCAN, USB, SAI & I2S
619 * @param pllr Main PLL divider for main system clock
620 */
621void rcc_set_main_pll(uint32_t pllsrc, uint32_t pllm, uint32_t plln,
622 uint32_t pllp, uint32_t pllq, uint32_t pllr)
623{
624 bool pllpen = (pllp != 0);
625 bool pllqen = (pllq != 0);
626 bool pllren = (pllr != 0);
627
628 pllm -= 1;
629
630 uint32_t pllpdiv = pllp;
631 pllp = (pllpdiv == 17);
632 if ((pllpdiv == 7) || (pllpdiv == 17)) {
633 pllpdiv = 0;
634 }
635
636 pllr = (pllr >> 1) - 1;
637 pllq = (pllq >> 1) - 1;
638
642 (pllpen ? RCC_PLLCFGR_PLLPEN : 0 ) |
644 (pllqen ? RCC_PLLCFGR_PLLQEN : 0 ) |
646 (pllren ? RCC_PLLCFGR_PLLREN : 0 ) |
649}
650
652{
653 /* Return the clock source which is used as system clock. */
655}
656
657/**
658 * Setup clocks to run from PLL.
659 *
660 * The arguments provide the pll source, multipliers, dividers, all that's
661 * needed to establish a system clock.
662 *
663 * @param clock clock information structure.
664 */
665void rcc_clock_setup_pll(const struct rcc_clock_scale *clock)
666{
667 /* Enable internal high-speed oscillator (HSI16). */
670
671 /* Select HSI16 as SYSCLK source. */
673
674 /* Enable external high-speed oscillator (HSE). */
675 if (clock->pll_source == RCC_PLLCFGR_PLLSRC_HSE) {
678 }
679
680 /* Set the VOS scale mode */
683
684 if (clock->boost) {
686 } else {
688 }
689
690 /*
691 * Set prescalers for AHB, ADC, APB1, APB2.
692 * Do this before touching the PLL (TODO: why?).
693 */
694 rcc_set_hpre(clock->hpre);
695 rcc_set_ppre1(clock->ppre1);
696 rcc_set_ppre2(clock->ppre2);
697
698 /* Disable PLL oscillator before changing its configuration. */
700
701 /* Configure the PLL oscillator. */
703 clock->pllm, clock->plln,
704 clock->pllp, clock->pllq, clock->pllr);
705
706 /* Enable PLL oscillator and wait for it to stabilize. */
709
710 /* Configure flash settings. */
711 if (clock->flash_config & FLASH_ACR_DCEN) {
713 } else {
715 }
716 if (clock->flash_config & FLASH_ACR_ICEN) {
718 } else {
720 }
722
723 /* Select PLL as SYSCLK source. */
725
726 /* Wait for PLL clock to be selected. */
728
729 /* Set the peripheral clock frequencies used. */
733
734 /* Disable internal high-speed oscillator. */
735 if (clock->pll_source == RCC_PLLCFGR_PLLSRC_HSE) {
737 }
738}
739
740/**
741 * Setup clocks with the HSE.
742 *
743 * @deprecated replaced by rcc_clock_setup_pll as a drop in replacement.
744 * @see rcc_clock_setup_pll which supports HSI16 as well as HSE, using the same
745 * clock structures.
746 */
748{
749 rcc_clock_setup_pll(clock);
750}
751
752/** Set clock source for 48MHz clock
753 *
754 * The 48 MHz clock is derived from one of the four following sources:
755 * - PLLQ VCO (RCC_CCIPR_CLK48_PLLQ)
756 * - HSI48 internal oscillator (RCC_CCIPR_CLK48_HSI48)
757 *
758 * @param clksel One of the definitions above
759 */
760void rcc_set_clock48_source(uint32_t clksel)
761{
763 RCC_CCIPR |= (clksel << RCC_CCIPR_CLK48SEL_SHIFT);
764}
765
766static uint32_t rcc_get_clksel_freq(uint8_t shift) {
767 uint8_t clksel = (RCC_CCIPR >> shift) & RCC_CCIPR_SEL_MASK;
769 switch (clksel) {
771 return rcc_apb1_frequency;
775 return 32768;
777 return 16000000U;
778 }
780}
781
782
783uint32_t rcc_get_usart_clk_freq(uint32_t usart)
784{
785 if (usart == USART1_BASE) {
787 } else if (usart == USART2_BASE) {
789 } else if (usart == USART3_BASE) {
791 } else if (usart == UART4_BASE) {
793 } else if (usart == UART5_BASE) {
795 } else if (usart == LPUART1_BASE) {
797 }
799}
800
801/**@}*/
#define cm3_assert_not_reached()
Check if unreachable code is reached.
Definition: assert.h:102
#define FLASH_ACR_ICEN
#define FLASH_ACR_DCEN
void flash_icache_disable(void)
Disable the Instruction Cache.
void flash_icache_enable(void)
Enable the Instruction Cache.
void flash_dcache_disable(void)
Disable the data cache.
void flash_dcache_enable(void)
Enable the data cache.
void flash_set_ws(uint32_t ws)
Set the Number of Wait States.
void pwr_enable_boost(void)
Enable Boost Mode.
Definition: pwr.c:108
void pwr_set_vos_scale(enum pwr_vos_scale scale)
Definition: pwr.c:43
void pwr_disable_boost(void)
Disable Boost Mode.
Definition: pwr.c:116
@ PWR_SCALE1
Definition: g4/pwr.h:174
@ PWR_SCALE2
Definition: g4/pwr.h:175
#define RCC_BDCR_LSEON
Definition: g4/rcc.h:685
#define RCC_BDCR_LSERDY
Definition: g4/rcc.h:684
#define RCC_CCIPR_USARTxSEL_LSE
Definition: g4/rcc.h:622
#define RCC_CCIPR_USARTxSEL_PCLK
Definition: g4/rcc.h:619
#define RCC_CCIPR_USART3SEL_SHIFT
Definition: g4/rcc.h:636
#define RCC_CCIPR_UART4SEL_SHIFT
Definition: g4/rcc.h:635
#define RCC_CCIPR_USART1SEL_SHIFT
Definition: g4/rcc.h:638
#define RCC_CCIPR_SEL_MASK
Definition: g4/rcc.h:573
#define RCC_CCIPR_LPUART1SEL_SHIFT
Definition: g4/rcc.h:628
#define RCC_CCIPR_USARTxSEL_SYSCLK
Definition: g4/rcc.h:620
#define RCC_CCIPR_USART2SEL_SHIFT
Definition: g4/rcc.h:637
#define RCC_CCIPR_USARTxSEL_HSI16
Definition: g4/rcc.h:621
#define RCC_CCIPR_UART5SEL_SHIFT
Definition: g4/rcc.h:634
#define RCC_CCIPR_CLK48SEL_SHIFT
Definition: g4/rcc.h:587
#define RCC_CFGR_HPRE_NODIV
Definition: g4/rcc.h:171
#define RCC_CFGR_PPREx_NODIV
Definition: g4/rcc.h:156
#define RCC_CFGR_SWx_HSI16
Definition: g4/rcc.h:187
#define RCC_CFGR_SWx_HSE
Definition: g4/rcc.h:188
#define RCC_CFGR_SWx_PLL
Definition: g4/rcc.h:189
#define RCC_CFGR_PPRE1_MASK
Definition: g4/rcc.h:164
#define RCC_CFGR_PPRE2_SHIFT
Definition: g4/rcc.h:163
#define RCC_CFGR_PPRE2_MASK
Definition: g4/rcc.h:162
#define RCC_CFGR_HPRE_MASK
Definition: g4/rcc.h:181
#define RCC_CFGR_SWS_MASK
Definition: g4/rcc.h:191
#define RCC_CFGR_SW_MASK
Definition: g4/rcc.h:193
#define RCC_CFGR_SWS_SHIFT
Definition: g4/rcc.h:192
#define RCC_CFGR_HPRE_SHIFT
Definition: g4/rcc.h:182
#define RCC_CFGR_SW_SHIFT
Definition: g4/rcc.h:194
#define RCC_CFGR_PPRE1_SHIFT
Definition: g4/rcc.h:165
#define RCC_CICR_HSIRDYC
Definition: g4/rcc.h:289
#define RCC_CICR_PLLRDYC
Definition: g4/rcc.h:287
#define RCC_CICR_LSIRDYC
Definition: g4/rcc.h:291
#define RCC_CICR_LSERDYC
Definition: g4/rcc.h:290
#define RCC_CICR_CSSC
Definition: g4/rcc.h:286
#define RCC_CICR_HSI48RDYC
Definition: g4/rcc.h:284
#define RCC_CICR_HSERDYC
Definition: g4/rcc.h:288
#define RCC_CIER_HSI48RDYIE
Definition: g4/rcc.h:258
#define RCC_CIER_LSERDYIE
Definition: g4/rcc.h:264
#define RCC_CIER_HSIRDYIE
Definition: g4/rcc.h:263
#define RCC_CIER_LSIRDYIE
Definition: g4/rcc.h:265
#define RCC_CIER_PLLRDYIE
Definition: g4/rcc.h:261
#define RCC_CIER_HSERDYIE
Definition: g4/rcc.h:262
#define RCC_CIFR_HSIRDYF
Definition: g4/rcc.h:276
#define RCC_CIFR_LSERDYF
Definition: g4/rcc.h:277
#define RCC_CIFR_HSI48RDYF
Definition: g4/rcc.h:271
#define RCC_CIFR_PLLRDYF
Definition: g4/rcc.h:274
#define RCC_CIFR_HSERDYF
Definition: g4/rcc.h:275
#define RCC_CIFR_CSSF
Definition: g4/rcc.h:273
#define RCC_CIFR_LSIRDYF
Definition: g4/rcc.h:278
#define RCC_CR_HSERDY
Definition: g4/rcc.h:107
#define RCC_CR_HSIRDY
Definition: g4/rcc.h:109
#define RCC_CR_CSSON
Definition: g4/rcc.h:105
#define RCC_CR_PLLON
Definition: g4/rcc.h:104
#define RCC_CR_HSEON
Definition: g4/rcc.h:108
#define RCC_CR_HSION
Definition: g4/rcc.h:111
#define RCC_CR_PLLRDY
Definition: g4/rcc.h:103
#define RCC_CRRCR_HSI48ON
Definition: g4/rcc.h:714
#define RCC_CRRCR_HSI48RDY
Definition: g4/rcc.h:713
#define RCC_CSR_LSION
Definition: g4/rcc.h:704
#define RCC_CSR_LSIRDY
Definition: g4/rcc.h:703
rcc_osc
Definition: g4/rcc.h:770
void rcc_periph_clock_enable(enum rcc_periph_clken clken)
Enable Peripheral Clock in running mode.
uint16_t rcc_get_div_from_hpre(uint8_t div_val)
This will return the divisor 1/2/4/8/16/64/128/256/512 which is set as a 4-bit value,...
@ RCC_PWR
Definition: g4/rcc.h:817
@ RCC_HSI48
Definition: g4/rcc.h:771
@ RCC_LSI
Definition: g4/rcc.h:776
@ RCC_PLL
Definition: g4/rcc.h:772
@ RCC_LSE
Definition: g4/rcc.h:775
@ RCC_HSE
Definition: g4/rcc.h:773
@ RCC_HSI16
Definition: g4/rcc.h:774
@ RCC_CLOCK_3V3_END
Definition: g4/rcc.h:743
int rcc_osc_ready_int_flag(enum rcc_osc osc)
Definition: rcc.c:434
void rcc_clock_setup_hse_3v3(const struct rcc_clock_scale *clock)
Setup clocks with the HSE.
Definition: rcc.c:747
int rcc_css_int_flag(void)
Definition: rcc.c:458
void rcc_set_clock48_source(uint32_t clksel)
Set clock source for 48MHz clock.
Definition: rcc.c:760
void rcc_osc_ready_int_clear(enum rcc_osc osc)
Definition: rcc.c:362
void rcc_wait_for_osc_ready(enum rcc_osc osc)
Wait for Oscillator Ready.
Definition: rcc.c:482
void rcc_css_disable(void)
Definition: rcc.c:561
bool rcc_is_osc_ready(enum rcc_osc osc)
Is the given oscillator ready?
Definition: rcc.c:463
void rcc_set_sysclk_source(uint32_t clk)
Definition: rcc.c:566
uint32_t rcc_apb2_frequency
Definition: rcc.c:57
void rcc_set_pll_source(uint32_t pllsrc)
Definition: rcc.c:575
uint32_t rcc_system_clock_source(void)
Definition: rcc.c:651
const struct rcc_clock_scale rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_END]
Definition: rcc.c:134
uint32_t rcc_get_usart_clk_freq(uint32_t usart)
Get the peripheral clock speed for the specified (LP)UxART.
Definition: rcc.c:783
const struct rcc_clock_scale rcc_hsi_configs[RCC_CLOCK_3V3_END]
Definition: rcc.c:59
void rcc_clock_setup_pll(const struct rcc_clock_scale *clock)
Setup clocks to run from PLL.
Definition: rcc.c:665
const struct rcc_clock_scale rcc_hse_12mhz_3v3[RCC_CLOCK_3V3_END]
Definition: rcc.c:210
void rcc_osc_ready_int_enable(enum rcc_osc osc)
Definition: rcc.c:386
void rcc_osc_ready_int_disable(enum rcc_osc osc)
Definition: rcc.c:410
void rcc_osc_on(enum rcc_osc osc)
Definition: rcc.c:508
uint32_t rcc_ahb_frequency
Definition: rcc.c:55
void rcc_osc_off(enum rcc_osc osc)
Definition: rcc.c:532
uint32_t rcc_apb1_frequency
Definition: rcc.c:56
void rcc_wait_for_sysclk_status(enum rcc_osc osc)
Definition: rcc.c:487
void rcc_set_ppre1(uint32_t ppre1)
Definition: rcc.c:593
void rcc_css_int_clear(void)
Definition: rcc.c:453
void rcc_set_main_pll(uint32_t pllsrc, uint32_t pllm, uint32_t plln, uint32_t pllp, uint32_t pllq, uint32_t pllr)
Reconfigures the main PLL for a HSE source.
Definition: rcc.c:621
void rcc_set_ppre2(uint32_t ppre2)
Definition: rcc.c:584
static uint32_t rcc_get_clksel_freq(uint8_t shift)
Definition: rcc.c:766
void rcc_css_enable(void)
Definition: rcc.c:556
void rcc_set_hpre(uint32_t hpre)
Definition: rcc.c:602
const struct rcc_clock_scale rcc_hse_16mhz_3v3[RCC_CLOCK_3V3_END]
Definition: rcc.c:285
#define RCC_PLLCFGR_PLLM_SHIFT
Definition: g4/rcc.h:241
#define RCC_PLLCFGR_PLLM_MASK
Definition: g4/rcc.h:242
#define RCC_PLLCFGR_PLLN_SHIFT
Definition: g4/rcc.h:234
#define RCC_PLLCFGR_PLLN_MASK
Definition: g4/rcc.h:235
#define RCC_PLLCFGR_PLLPEN
Definition: g4/rcc.h:229
#define RCC_PLLCFGR_PLLSRC_SHIFT
Definition: g4/rcc.h:249
#define RCC_PLLCFGR_PLLR_MASK
Definition: g4/rcc.h:212
#define RCC_PLLCFGR_PLLP_DIV7
Definition: g4/rcc.h:227
#define RCC_PLLCFGR_PLLSRC_MASK
Definition: g4/rcc.h:250
#define RCC_PLLCFGR_PLLP_DIV17
Definition: g4/rcc.h:228
#define RCC_PLLCFGR_PLLSRC_HSI16
Definition: g4/rcc.h:247
#define RCC_PLLCFGR_PLLQEN
Definition: g4/rcc.h:223
#define RCC_PLLCFGR_PLLPDIV_MASK
Definition: g4/rcc.h:200
#define RCC_PLLCFGR_PLLR_SHIFT
Definition: g4/rcc.h:211
#define RCC_PLLCFGR_PLLQ_SHIFT
Definition: g4/rcc.h:220
#define RCC_PLLCFGR_PLLPDIV_SHIFT
Definition: g4/rcc.h:201
#define RCC_PLLCFGR_PLLQ_MASK
Definition: g4/rcc.h:221
#define RCC_PLLCFGR_PLLREN
Definition: g4/rcc.h:214
#define RCC_PLLCFGR_PLLSRC_HSE
Definition: g4/rcc.h:248
#define RCC_CICR
Definition: g4/rcc.h:55
#define RCC_CR
Definition: g4/rcc.h:49
#define RCC_CIFR
Definition: g4/rcc.h:54
#define RCC_CCIPR
Definition: g4/rcc.h:92
#define RCC_CSR
Definition: g4/rcc.h:94
#define RCC_PLLCFGR
Definition: g4/rcc.h:52
#define RCC_CIER
Definition: g4/rcc.h:53
#define RCC_CFGR
Definition: g4/rcc.h:51
#define RCC_BDCR
Definition: g4/rcc.h:93
#define RCC_CRRCR
Definition: g4/rcc.h:95
#define LPUART1_BASE
#define USART1_BASE
#define UART4_BASE
#define UART5_BASE
#define USART3_BASE
#define USART2_BASE
uint8_t ppre1
Definition: g4/rcc.h:754
uint8_t pllq
Definition: g4/rcc.h:750
uint8_t flash_waitstates
Definition: g4/rcc.h:759
uint8_t ppre2
Definition: g4/rcc.h:755
uint8_t pllp
Definition: g4/rcc.h:749
uint32_t apb1_frequency
Definition: g4/rcc.h:761
uint8_t pllm
Definition: g4/rcc.h:747
uint32_t ahb_frequency
Definition: g4/rcc.h:760
enum pwr_vos_scale vos_scale
Definition: g4/rcc.h:756
uint16_t plln
Definition: g4/rcc.h:748
uint8_t pllr
Definition: g4/rcc.h:751
uint32_t flash_config
Definition: g4/rcc.h:758
uint8_t hpre
Definition: g4/rcc.h:753
uint32_t apb2_frequency
Definition: g4/rcc.h:762
uint8_t pll_source
Definition: g4/rcc.h:752