1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: #include <sys/machine.h>
15: #ifdef CPU_STM32L4
16:
17: 18: 19: 20:
21:
22: #include <tk/tkernel.h>
23:
24: #include "sysdepend.h"
25:
26: 27: 28:
29: EXPORT void startup_clock(ATR clkatr)
30: {
31: UW clk_sw, pll_src;
32: UW f_ratency;
33:
34:
35:
36: if( clkatr & CLKATR_HSI ) {
37: *(_UW*)RCC_CR |= RCC_CR_HSION;
38: while((*(_UW*)RCC_CR & RCC_CR_HSIRDY) == 0 );
39: clk_sw = RCC_CFGR_SW_HSI16;
40:
41:
42: } else if( clkatr & CLKATR_HSE ) {
43: *(_UW*)RCC_CR |= RCC_CR_HSEON;
44: while( (*(_UW*)RCC_CR & RCC_CR_HSERDY) == 0 );
45: clk_sw = RCC_CFGR_SW_HSE;
46:
47:
48: } else {
49: *(_UW*)RCC_CR |= RCC_CR_MSION;
50: while((*(_UW*)RCC_CR & RCC_CR_MSIRDY) == 0 );
51: clk_sw = RCC_CFGR_SW_MSI;
52: }
53:
54:
55: if(clkatr & CLKATR_USE_PLL) {
56: pll_src = clk_sw + 1;
57: clk_sw = RCC_CFGR_SW_PLL;
58:
59:
60: *(_UW*)RCC_CR &= ~RCC_CR_PLLON;
61: while((*(_UW*)RCC_CR & RCC_CR_PLLRDY) != 0 );
62:
63: out_w(RCC_PLLCFGR, (RCC_PLLCFGR_INIT & ~RCC_PLLCFGR_PLLSRC) | pll_src);
64:
65: *(_UW*)RCC_CR |= RCC_CR_PLLON;
66: *(_UW*)RCC_PLLCFGR |= RCC_PLLCFGR_PLLREN;
67: while((*(_UW*)RCC_CR & RCC_CR_PLLRDY) == 0);
68:
69:
70: *(_UW*)RCC_CR &= ~RCC_CR_PLLSAI1ON;
71: while((*(_UW*)RCC_CR & RCC_CR_PLLSAI1RDY) != 0 );
72:
73: out_w(RCC_PLLSAI1CFGR, RCC_PLLSAI1CFGR_INIT);
74:
75: *(_UW*)RCC_CR |= RCC_CR_PLLSAI1ON;
76: while((*(_UW*)RCC_CR & RCC_CR_PLLSAI1RDY) == 0);
77:
78:
79: *(_UW*)RCC_CR &= ~RCC_CR_PLLSAI2ON;
80: while((*(_UW*)RCC_CR & RCC_CR_PLLSAI2RDY) != 0 );
81:
82: out_w(RCC_PLLSAI1CFGR, RCC_PLLSAI2CFGR_INIT);
83:
84: *(_UW*)RCC_CR |= RCC_CR_PLLSAI2ON;
85: while((*(_UW*)RCC_CR & RCC_CR_PLLSAI2RDY) == 0);
86:
87: }
88:
89:
90: f_ratency = (clkatr & CLKATR_LATENCY_MASK)>>8;
91: *(_UW*)FLASH_ACR = (*(_UW*)FLASH_ACR & ~FLASH_ACR_LATENCY_MASK)| FLASH_ACR_LATENCY(f_ratency);
92: while( (*(_UW*)FLASH_ACR & FLASH_ACR_LATENCY_MASK) != FLASH_ACR_LATENCY(f_ratency) );
93:
94:
95: out_w(RCC_CFGR, (RCC_CFGR_INIT & ~RCC_CFGR_SW) | clk_sw);
96: while((*(_UW*)RCC_CFGR & RCC_CFGR_SW) != clk_sw);
97:
98:
99: out_w(RCC_CIER, 0);
100: }
101:
102: EXPORT void shutdown_clock(void)
103: {
104:
105: }
106:
107: #endif