34 #include <libopencm3/stm32/gpio.h>
35 #include <libopencm3/stm32/rcc.h>
36 #include <libopencm3/stm32/timer.h>
37 #include <libopencm3/stm32/flash.h>
38 #include <libopencm3/cm3/scb.h>
44 const struct rcc_clock_scale rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_END] = {
50 .hpre = RCC_CFGR_HPRE_DIV_NONE,
51 .ppre1 = RCC_CFGR_PPRE_DIV_4,
52 .ppre2 = RCC_CFGR_PPRE_DIV_2,
54 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS,
55 .ahb_frequency = 48000000,
56 .apb1_frequency = 12000000,
57 .apb2_frequency = 24000000,
64 .hpre = RCC_CFGR_HPRE_DIV_NONE,
65 .ppre1 = RCC_CFGR_PPRE_DIV_2,
66 .ppre2 = RCC_CFGR_PPRE_DIV_NONE,
68 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_2WS,
69 .ahb_frequency = 84000000,
70 .apb1_frequency = 42000000,
71 .apb2_frequency = 84000000,
78 .hpre = RCC_CFGR_HPRE_DIV_NONE,
79 .ppre1 = RCC_CFGR_PPRE_DIV_4,
80 .ppre2 = RCC_CFGR_PPRE_DIV_2,
82 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS,
83 .ahb_frequency = 120000000,
84 .apb1_frequency = 30000000,
85 .apb2_frequency = 60000000,
92 .hpre = RCC_CFGR_HPRE_DIV_NONE,
93 .ppre1 = RCC_CFGR_PPRE_DIV_4,
94 .ppre2 = RCC_CFGR_PPRE_DIV_2,
95 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS,
96 .ahb_frequency = 168000000,
97 .apb1_frequency = 42000000,
98 .apb2_frequency = 84000000,
108 void rcc_clock_setup_in_hse_24mhz_out_24mhz_pprz(
void);
109 void rcc_clock_setup_in_hse_24mhz_out_24mhz_pprz(
void)
113 rcc_wait_for_osc_ready(RCC_HSI);
116 rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSICLK);
120 rcc_wait_for_osc_ready(RCC_HSE);
121 rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSECLK);
127 rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_NODIV);
128 rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV2);
129 rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_NODIV);
130 rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV);
138 flash_set_ws(FLASH_ACR_LATENCY_0WS);
144 rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL2);
147 rcc_set_pll_source(RCC_CFGR_PLLSRC_HSE_CLK);
153 rcc_set_pllxtpre(RCC_CFGR_PLLXTPRE_HSE_CLK_DIV2);
156 rcc_wait_for_osc_ready(RCC_PLL);
159 rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK);
162 rcc_ahb_frequency = 24000000;
163 rcc_apb1_frequency = 24000000;
164 rcc_apb2_frequency = 24000000;
171 PRINT_CONFIG_MSG(
"We are running luftboot, the interrupt vector is being relocated.")
173 SCB_VTOR = 0x00004000;
175 SCB_VTOR = 0x00002000;
178 #if EXT_CLK == 8000000
181 rcc_clock_setup_in_hse_8mhz_out_72mhz();
182 #elif defined(STM32F4)
183 #if AHB_CLK == 84000000
185 rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
188 rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
191 #elif EXT_CLK == 12000000
194 rcc_clock_setup_in_hse_12mhz_out_72mhz();
195 #elif defined(STM32F4)
197 rcc_clock_setup_hse_3v3(&rcc_hse_12mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
199 #elif EXT_CLK == 16000000
202 rcc_clock_setup_hse_3v3(&rcc_hse_16mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
204 #elif EXT_CLK == 24000000
207 rcc_clock_setup_hse_3v3(&rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
208 #elif defined(STM32F1)
209 rcc_clock_setup_in_hse_24mhz_out_24mhz_pprz();
211 #elif EXT_CLK == 25000000
214 rcc_clock_setup_hse_3v3(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
217 #error EXT_CLK is either set to an unsupported frequency or not defined at all. Please check!
224 scb_set_priority_grouping(SCB_AIRCR_PRIGROUP_NOGROUP_SUB16);
229 #define RCC_CFGR_PPRE2_SHIFT 11
230 #define RCC_CFGR_PPRE2 (7 << RCC_CFGR_PPRE2_SHIFT)
232 #define RCC_CFGR_PPRE1_SHIFT 8
233 #define RCC_CFGR_PPRE1 (7 << RCC_CFGR_PPRE1_SHIFT)
235 static inline uint32_t rcc_get_ppre1(
void)
237 return RCC_CFGR & RCC_CFGR_PPRE1;
240 static inline uint32_t rcc_get_ppre2(
void)
242 return RCC_CFGR & RCC_CFGR_PPRE2;
244 #elif defined(STM32F4)
245 static inline uint32_t rcc_get_ppre1(
void)
247 return RCC_CFGR & ((1 << 10) | (1 << 11) | (1 << 12));
250 static inline uint32_t rcc_get_ppre2(
void)
252 return RCC_CFGR & ((1 << 13) | (1 << 14) | (1 << 15));
265 switch (timer_peripheral) {
278 if (!rcc_get_ppre2()) {
280 return rcc_apb2_frequency;
283 return rcc_apb2_frequency * 2;
302 if (!rcc_get_ppre1()) {
304 return rcc_apb1_frequency;
307 return rcc_apb1_frequency * 2;
uint32_t timer_get_frequency(uint32_t timer_peripheral)
Get Timer clock frequency (before prescaling) Only valid if using the internal clock for the timer...
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
Arch independent mcu ( Micro Controller Unit ) utilities.