2024-02-29 12:22:16 +01:00
|
|
|
#include "CH32V00xxx.h"
|
|
|
|
extern "C" void SystemInit ();
|
|
|
|
enum CLKSRC : uint32_t {
|
|
|
|
CLK_HSI = 0u,
|
|
|
|
CLK_HSE,
|
|
|
|
CLK_PLL,
|
|
|
|
};
|
2024-03-04 22:23:14 +01:00
|
|
|
// HSE i HSI mají frekvenci 24 MHz
|
2024-02-29 12:22:16 +01:00
|
|
|
void SystemInit(void) {
|
2024-03-04 22:23:14 +01:00
|
|
|
RCC.CFGR0.R = 0u; // prescaler OFF
|
|
|
|
#if USE_HSE
|
|
|
|
RCC.CTLR.modify([](RCC_Type::CTLR_DEF & r) -> auto {
|
|
|
|
r.B.HSITRIM = 0x10u;
|
|
|
|
r.B.HSION = SET;
|
|
|
|
r.B.HSEON = SET;
|
|
|
|
r.B.HSEBYP = RESET; // krystal
|
|
|
|
r.B.CSSON = SET;
|
|
|
|
r.B.PLLON = RESET;
|
|
|
|
return r.R;
|
|
|
|
});
|
|
|
|
while (RCC.CTLR.B.HSERDY == RESET);
|
|
|
|
RCC.CFGR0.modify([](RCC_Type::CFGR0_DEF & r) -> auto {
|
|
|
|
r.B.SW = CLK_HSE;
|
|
|
|
r.B.PLLSRC = SET; // write only when PLL is off
|
|
|
|
return r.R;
|
|
|
|
});
|
|
|
|
RCC.CTLR.modify([](RCC_Type::CTLR_DEF & r) -> auto {
|
|
|
|
//r.B.HSION = RESET; // je možné vypnout, ale není to dobrý nápad, pak je nutný unbrick
|
|
|
|
r.B.PLLON = SET;
|
|
|
|
return r.R;
|
|
|
|
});
|
|
|
|
#else // HSI
|
2024-02-29 12:22:16 +01:00
|
|
|
RCC.CTLR.modify([](RCC_Type::CTLR_DEF & r) -> auto {
|
|
|
|
r.B.HSITRIM = 0x10u;
|
|
|
|
r.B.HSION = SET;
|
|
|
|
r.B.HSEBYP = SET;
|
|
|
|
r.B.CSSON = SET;
|
|
|
|
r.B.PLLON = SET;
|
|
|
|
return r.R;
|
|
|
|
});
|
2024-03-04 22:23:14 +01:00
|
|
|
#endif // USE_HSE
|
2024-02-29 12:22:16 +01:00
|
|
|
FLASH.ACTLR.B.LATENCY = SET;
|
|
|
|
RCC.INTR.R = 0x009F0000u; // clear interrupts
|
|
|
|
while (RCC.CTLR.B.PLLRDY == RESET);
|
2024-03-04 22:23:14 +01:00
|
|
|
// USE PLL - zdvojení frekvence, tj. 48 MHz
|
|
|
|
RCC.CFGR0.B.SW = CLK_PLL ;
|
2024-02-29 12:22:16 +01:00
|
|
|
while (RCC.CFGR0.B.SWS != CLK_PLL);
|
|
|
|
}
|