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>
39 #include <libopencm3/stm32/rtc.h>
40 #include <libopencm3/stm32/pwr.h>
46 const struct rcc_clock_scale rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_END] = {
52 .hpre = RCC_CFGR_HPRE_DIV_NONE,
53 .ppre1 = RCC_CFGR_PPRE_DIV_4,
54 .ppre2 = RCC_CFGR_PPRE_DIV_2,
55 .voltage_scale = PWR_SCALE1,
56 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS,
57 .ahb_frequency = 48000000,
58 .apb1_frequency = 12000000,
59 .apb2_frequency = 24000000,
66 .hpre = RCC_CFGR_HPRE_DIV_NONE,
67 .ppre1 = RCC_CFGR_PPRE_DIV_2,
68 .ppre2 = RCC_CFGR_PPRE_DIV_NONE,
69 .voltage_scale = PWR_SCALE1,
70 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_2WS,
71 .ahb_frequency = 84000000,
72 .apb1_frequency = 42000000,
73 .apb2_frequency = 84000000,
80 .hpre = RCC_CFGR_HPRE_DIV_NONE,
81 .ppre1 = RCC_CFGR_PPRE_DIV_4,
82 .ppre2 = RCC_CFGR_PPRE_DIV_2,
83 .voltage_scale = PWR_SCALE1,
84 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS,
85 .ahb_frequency = 120000000,
86 .apb1_frequency = 30000000,
87 .apb2_frequency = 60000000,
94 .hpre = RCC_CFGR_HPRE_DIV_NONE,
95 .ppre1 = RCC_CFGR_PPRE_DIV_4,
96 .ppre2 = RCC_CFGR_PPRE_DIV_2,
97 .flash_config = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS,
98 .ahb_frequency = 168000000,
99 .apb1_frequency = 42000000,
100 .apb2_frequency = 84000000,
110 void rcc_clock_setup_in_hse_24mhz_out_24mhz_pprz(
void);
111 void rcc_clock_setup_in_hse_24mhz_out_24mhz_pprz(
void)
115 rcc_wait_for_osc_ready(RCC_HSI);
118 rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSICLK);
122 rcc_wait_for_osc_ready(RCC_HSE);
123 rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSECLK);
129 rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_NODIV);
130 rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV2);
131 rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_NODIV);
132 rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV);
140 flash_set_ws(FLASH_ACR_LATENCY_0WS);
146 rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL2);
149 rcc_set_pll_source(RCC_CFGR_PLLSRC_HSE_CLK);
155 rcc_set_pllxtpre(RCC_CFGR_PLLXTPRE_HSE_CLK_DIV2);
158 rcc_wait_for_osc_ready(RCC_PLL);
161 rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK);
164 rcc_ahb_frequency = 24000000;
165 rcc_apb1_frequency = 24000000;
166 rcc_apb2_frequency = 24000000;
171 #ifdef SYSTEM_MEMORY_BASE
172 void reset_to_dfu(
void) {
174 pwr_disable_backup_domain_write_protect();
176 pwr_enable_backup_domain_write_protect();
181 typedef void resetHandler_t(
void);
183 typedef struct isrVector_s {
185 resetHandler_t *resetHandler;
188 static isrVector_t *system_isr_vector_table_base = (isrVector_t *)
SYSTEM_MEMORY_BASE;
190 static void init_dfu(
void) {
192 rcc_periph_clock_enable(RCC_RTC);
193 rcc_periph_clock_enable(RCC_PWR);
194 if (RTC_BKPXR(0) == 0xFF) {
197 pwr_disable_backup_domain_write_protect();
199 pwr_enable_backup_domain_write_protect();
203 register uint32_t __regMainStackPointer __asm(
"msp") __attribute__((unused));
204 __regMainStackPointer = system_isr_vector_table_base->stackEnd;
206 system_isr_vector_table_base->resetHandler();
215 #ifdef SYSTEM_MEMORY_BASE
220 PRINT_CONFIG_MSG(
"We are running luftboot, the interrupt vector is being relocated.")
222 SCB_VTOR = 0x00004000;
224 SCB_VTOR = 0x00002000;
227 #if EXT_CLK == 8000000
230 rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
231 #elif defined(STM32F4)
232 #if AHB_CLK == 84000000
234 rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
237 rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
240 #elif EXT_CLK == 12000000
243 rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE12_72MHZ]);
244 #elif defined(STM32F4)
246 rcc_clock_setup_pll(&rcc_hse_12mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
248 #elif EXT_CLK == 16000000
251 rcc_clock_setup_pll(&rcc_hse_16mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
253 #elif EXT_CLK == 24000000
256 rcc_clock_setup_pll(&rcc_hse_24mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
257 #elif defined(STM32F1)
258 rcc_clock_setup_in_hse_24mhz_out_24mhz_pprz();
260 #elif EXT_CLK == 25000000
263 rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
266 #error EXT_CLK is either set to an unsupported frequency or not defined at all. Please check!
273 scb_set_priority_grouping(SCB_AIRCR_PRIGROUP_NOGROUP_SUB16);
277 #define RCC_CFGR_PPRE2_SHIFT 11
278 #define RCC_CFGR_PPRE2 (7 << RCC_CFGR_PPRE2_SHIFT)
280 #define RCC_CFGR_PPRE1_SHIFT 8
281 #define RCC_CFGR_PPRE1 (7 << RCC_CFGR_PPRE1_SHIFT)
283 static inline uint32_t rcc_get_ppre1(
void)
285 return RCC_CFGR & RCC_CFGR_PPRE1;
288 static inline uint32_t rcc_get_ppre2(
void)
290 return RCC_CFGR & RCC_CFGR_PPRE2;
292 #elif defined(STM32F4)
293 static inline uint32_t rcc_get_ppre1(
void)
295 return RCC_CFGR & ((1 << 10) | (1 << 11) | (1 << 12));
298 static inline uint32_t rcc_get_ppre2(
void)
300 return RCC_CFGR & ((1 << 13) | (1 << 14) | (1 << 15));
313 switch (timer_peripheral) {
326 if (!rcc_get_ppre2()) {
328 return rcc_apb2_frequency;
331 return rcc_apb2_frequency * 2;
350 if (!rcc_get_ppre1()) {
352 return rcc_apb1_frequency;
355 return rcc_apb1_frequency * 2;
void mcu_arch_init(void)
Initialize the specific archittecture functions.
#define SYSTEM_MEMORY_BASE
System memory base - for DFU bootloader.
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
Arch independent mcu ( Micro Controller Unit ) utilities.
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.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.