system_n32l40x.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*****************************************************************************
  2. * Copyright (c) 2022, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file system_n32l40x.c
  29. * @author Nations
  30. * @version v1.2.0
  31. *
  32. * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32l40x.h"
  35. /* Uncomment the line corresponding to the desired System clock (SYSCLK)
  36. frequency (after reset the HSI is used as SYSCLK source)
  37. IMPORTANT NOTE:
  38. ==============
  39. 1. After each device reset the HSI is used as System clock source.
  40. 2. Please make sure that the selected System clock doesn't exceed your
  41. device's maximum frequency.
  42. 3. If none of the define below is enabled, the HSI is used as System clock
  43. source.
  44. 4. The System clock configuration functions provided within this file assume
  45. that:
  46. - For Low, Medium and High density Value line devices an external 8MHz
  47. crystal is used to drive the System clock.
  48. - For Low, Medium and High density devices an external 8MHz crystal is
  49. used to drive the System clock.
  50. - For Connectivity line devices an external 25MHz crystal is used to
  51. drive the System clock. If you are using different crystal you have to adapt
  52. those functions accordingly.
  53. */
  54. #define SYSCLK_USE_MSI 0
  55. #define SYSCLK_USE_HSI 1
  56. #define SYSCLK_USE_HSE 2
  57. #define SYSCLK_USE_HSI_PLL 3
  58. #define SYSCLK_USE_HSE_PLL 4
  59. #ifndef SYSCLK_FREQ
  60. #define SYSCLK_FREQ 64000000
  61. #endif
  62. /*
  63. * SYSCLK_SRC *
  64. ** SYSCLK_USE_MSI **
  65. ** SYSCLK_USE_HSI **
  66. ** SYSCLK_USE_HSE **
  67. ** SYSCLK_USE_HSI_PLL **
  68. ** SYSCLK_USE_HSE_PLL **
  69. */
  70. #ifndef SYSCLK_SRC
  71. #define SYSCLK_SRC SYSCLK_USE_HSE_PLL
  72. #endif
  73. #define PLL_DIV2_DISABLE 0x00000000
  74. #define PLL_DIV2_ENABLE 0x00000002
  75. #define SRAM_VOL (__IO unsigned*)(0x40001800 + 0x20)
  76. #define ConfigSRAMVoltage(vale) do{(*SRAM_VOL ) &= (~(uint32_t)(1 <<25));(*SRAM_VOL ) |= (uint32_t)(vale <<25);}while (0) //vale only equal to 0,1
  77. #if SYSCLK_SRC == SYSCLK_USE_MSI
  78. #if (SYSCLK_FREQ == MSI_VALUE_L0)
  79. #define MSI_CLK 0
  80. #elif (SYSCLK_FREQ == MSI_VALUE_L1)
  81. #define MSI_CLK 1
  82. #elif (SYSCLK_FREQ == MSI_VALUE_L2)
  83. #define MSI_CLK 2
  84. #elif (SYSCLK_FREQ == MSI_VALUE_L3)
  85. #define MSI_CLK 3
  86. #elif (SYSCLK_FREQ == MSI_VALUE_L4)
  87. #define MSI_CLK 4
  88. #elif (SYSCLK_FREQ == MSI_VALUE_L5)
  89. #define MSI_CLK 5
  90. #elif (SYSCLK_FREQ == MSI_VALUE_L6)
  91. #define MSI_CLK 6
  92. #else
  93. #error SYSCL_FREQ must be set to MSI_VALUE_Lx(x=0~6)
  94. #endif
  95. #elif SYSCLK_SRC == SYSCLK_USE_HSI
  96. #if SYSCLK_FREQ != HSI_VALUE
  97. #error SYSCL_FREQ must be set to HSI_VALUE
  98. #endif
  99. #elif SYSCLK_SRC == SYSCLK_USE_HSE
  100. #ifndef HSE_VALUE
  101. #error HSE_VALUE must be defined!
  102. #endif
  103. #if SYSCLK_FREQ != HSE_VALUE
  104. #error SYSCL_FREQ must be set to HSE_VALUE
  105. #endif
  106. #elif SYSCLK_SRC == SYSCLK_USE_HSI_PLL
  107. #ifndef HSI_VALUE
  108. #error HSI_VALUE must be defined!
  109. #endif
  110. #if ((SYSCLK_FREQ % (HSI_VALUE / 2)) == 0) && (SYSCLK_FREQ / (HSI_VALUE / 2) >= 2) \
  111. && (SYSCLK_FREQ / (HSI_VALUE / 2) <= 32)
  112. #define PLLSRC_DIV 2
  113. #define PLL_DIV PLL_DIV2_DISABLE
  114. #define PLL_MUL (SYSCLK_FREQ / (HSI_VALUE / 2))
  115. #elif (SYSCLK_FREQ % HSI_VALUE == 0) && (SYSCLK_FREQ / HSI_VALUE >= 2) && (SYSCLK_FREQ / HSI_VALUE <= 32)
  116. #define PLLSRC_DIV 1
  117. #define PLL_DIV PLL_DIV2_DISABLE
  118. #define PLL_MUL (SYSCLK_FREQ / HSI_VALUE)
  119. #elif ((SYSCLK_FREQ % (HSI_VALUE / 4)) == 0) && (SYSCLK_FREQ / (HSI_VALUE / 4) >= 2) \
  120. && (SYSCLK_FREQ / (HSI_VALUE / 4) <= 32)
  121. #define PLLSRC_DIV 2
  122. #define PLL_DIV PLL_DIV2_ENABLE
  123. #define PLL_MUL (SYSCLK_FREQ / (HSI_VALUE / 4))
  124. #else
  125. #error Cannot make a PLL multiply factor to SYSCLK_FREQ.
  126. #endif
  127. #elif SYSCLK_SRC == SYSCLK_USE_HSE_PLL
  128. #ifndef HSE_VALUE
  129. #error HSE_VALUE must be defined!
  130. #endif
  131. #if ((SYSCLK_FREQ % (HSE_VALUE / 2)) == 0) && (SYSCLK_FREQ / (HSE_VALUE / 2) >= 2) \
  132. && (SYSCLK_FREQ / (HSE_VALUE / 2) <= 32)
  133. #define PLLSRC_DIV 2
  134. #define PLL_DIV PLL_DIV2_DISABLE
  135. #define PLL_MUL (SYSCLK_FREQ / (HSE_VALUE / 2))
  136. #elif (SYSCLK_FREQ % HSE_VALUE == 0) && (SYSCLK_FREQ / HSE_VALUE >= 2) && (SYSCLK_FREQ / HSE_VALUE <= 32)
  137. #define PLLSRC_DIV 1
  138. #define PLL_DIV PLL_DIV2_DISABLE
  139. #define PLL_MUL (SYSCLK_FREQ / HSE_VALUE)
  140. #elif ((SYSCLK_FREQ % (HSE_VALUE / 4)) == 0) && (SYSCLK_FREQ / (HSE_VALUE / 4) >= 2) \
  141. && (SYSCLK_FREQ / (HSE_VALUE / 4) <= 32)
  142. #define PLLSRC_DIV 2
  143. #define PLL_DIV PLL_DIV2_ENABLE
  144. #define PLL_MUL (SYSCLK_FREQ / (HSE_VALUE / 4))
  145. #else
  146. #error Cannot make a PLL multiply factor to SYSCLK_FREQ.
  147. #endif
  148. #else
  149. #error wrong value for SYSCLK_SRC
  150. #endif
  151. /* #define VECT_TAB_SRAM */
  152. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. This value must be a multiple of 0x200. */
  153. /*******************************************************************************
  154. * Clock Definitions
  155. *******************************************************************************/
  156. uint32_t SystemCoreClock = SYSCLK_FREQ; /*!< System Clock Frequency (Core Clock) */
  157. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  158. const uint32_t MSIClockTable[7] = {MSI_VALUE_L0, MSI_VALUE_L1, MSI_VALUE_L2, MSI_VALUE_L3,
  159. MSI_VALUE_L4, MSI_VALUE_L5, MSI_VALUE_L6};
  160. static void SetSysClock(void);
  161. #ifdef DATA_IN_ExtSRAM
  162. static void SystemInit_ExtMemCtl(void);
  163. #endif /* DATA_IN_ExtSRAM */
  164. /**
  165. * @brief Setup the microcontroller system
  166. * Initialize the Embedded Flash Interface, the PLL and update the
  167. * SystemCoreClock variable.
  168. * @note This function should be used only after reset.
  169. */
  170. void SystemInit(void)
  171. {
  172. /* FPU settings
  173. * ------------------------------------------------------------*/
  174. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  175. SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* set CP10 and CP11 Full Access */
  176. #endif
  177. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  178. /* Set MSIEN bit */
  179. RCC->CTRLSTS |= (uint32_t)0x00000004;
  180. /* Reset SW, HPRE, PPRE1, PPRE2 and MCO bits */
  181. RCC->CFG &= (uint32_t)0xF8FFC000;
  182. /* Reset HSEON, CLKSSEN and PLLEN bits */
  183. RCC->CTRL &= (uint32_t)0xFEF6FFFF;
  184. /* Reset HSEBYP bit */
  185. RCC->CTRL &= (uint32_t)0xFFFBFFFF;
  186. /* Reset PLLSRC, PLLXTPRE, PLLMUL, MCOPRES and USBPRES bits */
  187. RCC->CFG &= (uint32_t)0x0700FFFF;
  188. /* Reset CFG2 register */
  189. RCC->CFG2 = 0x00007000;
  190. /* Reset CFG3 register */
  191. RCC->CFG3 = 0x00003800;
  192. /* Reset RDCTRL register */
  193. RCC->RDCTRL = 0x00000000;
  194. /* Reset PLLHSIPRE register */
  195. RCC->PLLHSIPRE = 0x00000000;
  196. /* Disable all interrupts and clear pending bits */
  197. RCC->CLKINT = 0x04BF8000;
  198. /* Enable ex mode */
  199. RCC->APB1PCLKEN |= RCC_APB1PCLKEN_PWREN;
  200. if ((PWR->CTRL1 & PWR_CTRL1_MRSEL2) == PWR_CTRL1_MRSEL2)
  201. {
  202. ConfigSRAMVoltage(1);
  203. }
  204. /* Enable ICACHE and Prefetch Buffer */
  205. FLASH->AC |= (uint32_t)(FLASH_AC_ICAHEN | FLASH_AC_PRFTBFEN);
  206. /* Checks whether the Low Voltage Mode status is SET or RESET */
  207. if ((FLASH->AC & FLASH_AC_LVMF) != RESET)
  208. {
  209. /* FLASH Low Voltage Mode Disable */
  210. FLASH->AC &= (uint32_t)(~FLASH_AC_LVMEN);
  211. }
  212. #ifdef DATA_IN_ExtSRAM
  213. SystemInit_ExtMemCtl();
  214. #endif /* DATA_IN_ExtSRAM */
  215. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  216. /* Configure the Flash Latency cycles and enable prefetch buffer */
  217. SetSysClock();
  218. #ifdef VECT_TAB_SRAM
  219. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  220. #else
  221. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  222. #endif
  223. }
  224. /**
  225. * @brief Update SystemCoreClock variable according to Clock Register Values.
  226. * The SystemCoreClock variable contains the core clock (HCLK), it can
  227. * be used by the user application to setup the SysTick timer or
  228. * configure other parameters.
  229. *
  230. * @note Each time the core clock (HCLK) changes, this function must be called
  231. * to update SystemCoreClock variable value. Otherwise, any
  232. * configuration based on this variable will be incorrect.
  233. *
  234. * @note - The system frequency computed by this function is not the real
  235. * frequency in the chip. It is calculated based on the predefined
  236. * constant and the selected clock source:
  237. *
  238. * - If SYSCLK source is MSI, SystemCoreClock will contain the
  239. * MSI_VALUE(*)
  240. *
  241. * - If SYSCLK source is HSI, SystemCoreClock will contain the
  242. * HSI_VALUE(**)
  243. *
  244. * - If SYSCLK source is HSE, SystemCoreClock will contain the
  245. * HSE_VALUE(***)
  246. *
  247. * - If SYSCLK source is PLL, SystemCoreClock will contain the
  248. * HSE_VALUE(***) or HSI_VALUE(**) multiplied by the PLL factors.
  249. *
  250. * (*) MSI_VALUE is a constant defined in n32l40x.h file (default value
  251. * 4 MHz, 100KHz/200KHz/400KHz/800KHz/1MHz/2MHz/4MHz ) but the real
  252. * value may vary depending on the variations in voltage and temperature.
  253. *
  254. * (**) HSI_VALUE is a constant defined in n32l40x.h file (default value
  255. * 8 MHz) but the real value may vary depending on the variations
  256. * in voltage and temperature.
  257. *
  258. * (***) HSE_VALUE is a constant defined in n32l40x.h file (default value
  259. * 8 MHz or 25 MHz, depedning on the product used), user has to
  260. * ensure that HSE_VALUE is same as the real frequency of the crystal used.
  261. * Otherwise, this function may have wrong result.
  262. *
  263. * - The result of this function could be not correct when using
  264. * fractional value for HSE crystal.
  265. */
  266. void SystemCoreClockUpdate(void)
  267. {
  268. uint32_t tmp = 0, pllmull = 0, pllsource = 0, plldiv2 = 0;
  269. uint8_t msi_clk = 0;
  270. /* Get SYSCLK source
  271. * -------------------------------------------------------*/
  272. tmp = RCC->CFG & RCC_CFG_SCLKSTS;
  273. /* Get MSI clock
  274. * -------------------------------------------------------*/
  275. msi_clk = (uint8_t) ((RCC->CTRLSTS & RCC_CTRLSTS_MSIRANGE)>>4);
  276. switch (tmp)
  277. {
  278. case 0x00: /* MSI used as system clock */
  279. SystemCoreClock = MSIClockTable[msi_clk];
  280. break;
  281. case 0x04: /* HSI used as system clock */
  282. SystemCoreClock = HSI_VALUE;
  283. break;
  284. case 0x08: /* HSE used as system clock */
  285. SystemCoreClock = HSE_VALUE;
  286. break;
  287. case 0x0C: /* PLL used as system clock */
  288. /* Get PLL clock source and multiplication factor
  289. * ----------------------*/
  290. pllmull = RCC->CFG & RCC_CFG_PLLMULFCT;
  291. pllsource = RCC->CFG & RCC_CFG_PLLSRC;
  292. plldiv2 = RCC->PLLHSIPRE & RCC_PLLHSIPRE_PLLSRCDIV;
  293. if ((pllmull & RCC_CFG_PLLMULFCT_4) == 0)
  294. {
  295. pllmull = (pllmull >> 18) + 2; // PLLMUL[4]=0
  296. }
  297. else
  298. {
  299. pllmull = ((pllmull >> 18) - 496) + 1; // PLLMUL[4]=1
  300. }
  301. if (pllsource == 0x00)
  302. {
  303. /* HSI selected as PLL clock entry */
  304. if ((RCC->PLLHSIPRE & RCC_PLLHSIPRE_PLLSRCDIV) != (uint32_t)RESET)
  305. { /* HSI oscillator clock divided by 2 */
  306. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  307. }
  308. else
  309. {
  310. SystemCoreClock = HSI_VALUE * pllmull;
  311. }
  312. }
  313. else
  314. {
  315. /* HSE selected as PLL clock entry */
  316. if ((RCC->CFG & RCC_CFG_PLLHSEPRES) != (uint32_t)RESET)
  317. { /* HSE oscillator clock divided by 2 */
  318. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  319. }
  320. else
  321. {
  322. SystemCoreClock = HSE_VALUE * pllmull;
  323. }
  324. }
  325. if (plldiv2 == 0x02)
  326. {
  327. /* PLL source clock divided by 2 selected as PLL clock entry */
  328. SystemCoreClock >>= 1;
  329. }
  330. break;
  331. default:
  332. SystemCoreClock = MSIClockTable[msi_clk];
  333. break;
  334. }
  335. /* Compute HCLK clock frequency ----------------*/
  336. /* Get HCLK prescaler */
  337. tmp = AHBPrescTable[((RCC->CFG & RCC_CFG_AHBPRES) >> 4)];
  338. /* HCLK clock frequency */
  339. SystemCoreClock >>= tmp;
  340. }
  341. /**
  342. * @brief Configures the System PWR level to 1.0V
  343. * .
  344. */
  345. void ConfigMRVoltage1V(void)
  346. {
  347. uint32_t i=0;
  348. ConfigSRAMVoltage(1); //SRAM read margin setting switch in 0.9/lprun mode: use low voltage mode settings and 1.0v use normal mode
  349. PWR->CTRL1 &= (uint32_t)(~PWR_CTRL1_MRSEL);
  350. PWR->CTRL1 |= PWR_CTRL1_MRSEL2; //MR=1.0V
  351. while ((PWR->STS2 &PWR_STS2_MRF) != 0); // wait VOSF to be 0 first
  352. for(i=0;i<0x2A;i++);
  353. while ((PWR->STS2 & PWR_STS2_MRF) != PWR_STS2_MRF); // wait VOSF to be 1 then
  354. }
  355. /**
  356. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1
  357. * prescalers.
  358. */
  359. static void SetSysClock(void)
  360. {
  361. uint32_t rcc_cfg = 0;
  362. uint32_t rcc_pllhsipre = 0;
  363. uint32_t StartUpCounter = 0;
  364. #if (SYSCLK_SRC == SYSCLK_USE_MSI)
  365. uint8_t i=0;
  366. bool MSIStatus = 0;
  367. /* Config MSI */
  368. RCC->CTRLSTS &= 0xFFFFFF8F;
  369. /*Delay for while*/
  370. for(i=0;i<0x30;i++);
  371. RCC->CTRLSTS |= (((uint32_t)MSI_CLK) << 4);
  372. /*Delay for while*/
  373. for(i=0;i<0x30;i++);
  374. /* Enable MSI */
  375. RCC->CTRLSTS |= ((uint32_t)RCC_CTRLSTS_MSIEN);
  376. /* Wait till MSI is ready and if Time out is reached exit */
  377. do
  378. {
  379. MSIStatus = RCC->CTRLSTS & RCC_CTRLSTS_MSIRD;
  380. StartUpCounter++;
  381. } while ((MSIStatus == 0) && (StartUpCounter != MSI_STARTUP_TIMEOUT));
  382. MSIStatus = ((RCC->CTRLSTS & RCC_CTRLSTS_MSIRD) != RESET);
  383. if (!MSIStatus)
  384. {
  385. /* If MSI fails to start-up, the application will have wrong clock
  386. * configuration. User can add here some code to deal with this error */
  387. SystemCoreClock = MSI_VALUE_L6;
  388. return;
  389. }
  390. #elif ((SYSCLK_SRC == SYSCLK_USE_HSI) || (SYSCLK_SRC == SYSCLK_USE_HSI_PLL))
  391. bool HSIStatus = 0;
  392. /* Enable HSI */
  393. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSIEN);
  394. /* Wait till HSI is ready and if Time out is reached exit */
  395. do
  396. {
  397. HSIStatus = RCC->CTRL & RCC_CTRL_HSIRDF;
  398. StartUpCounter++;
  399. } while ((HSIStatus == 0) && (StartUpCounter != HSI_STARTUP_TIMEOUT));
  400. HSIStatus = ((RCC->CTRL & RCC_CTRL_HSIRDF) != RESET);
  401. if (!HSIStatus)
  402. {
  403. /* If HSI fails to start-up, the application will have wrong clock
  404. * configuration. User can add here some code to deal with this error */
  405. SystemCoreClock = MSI_VALUE_L6;
  406. return;
  407. }
  408. #elif ((SYSCLK_SRC == SYSCLK_USE_HSE) || (SYSCLK_SRC == SYSCLK_USE_HSE_PLL))
  409. bool HSEStatus = 0;
  410. /* Enable HSE */
  411. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  412. /* Wait till HSE is ready and if Time out is reached exit */
  413. do
  414. {
  415. HSEStatus = RCC->CTRL & RCC_CTRL_HSERDF;
  416. StartUpCounter++;
  417. } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  418. HSEStatus = ((RCC->CTRL & RCC_CTRL_HSERDF) != RESET);
  419. if (!HSEStatus)
  420. {
  421. /* If HSE fails to start-up, the application will have wrong clock
  422. * configuration. User can add here some code to deal with this error */
  423. SystemCoreClock = MSI_VALUE_L6;
  424. return;
  425. }
  426. #endif
  427. ConfigMRVoltage1V();
  428. /* Flash wait state
  429. 0: HCLK <= 32M
  430. 1: HCLK <= 64M
  431. */
  432. FLASH->AC &= (uint32_t)((uint32_t)~FLASH_AC_LATENCY);
  433. FLASH->AC |= (uint32_t)((SYSCLK_FREQ - 1) / 32000000);
  434. /* HCLK = SYSCLK */
  435. RCC->CFG |= (uint32_t)RCC_CFG_AHBPRES_DIV1;
  436. /* PCLK2 max 32M */
  437. if (SYSCLK_FREQ > 54000000)
  438. {
  439. RCC->CFG |= (uint32_t)RCC_CFG_APB2PRES_DIV2;
  440. }
  441. else
  442. {
  443. RCC->CFG |= (uint32_t)RCC_CFG_APB2PRES_DIV1;
  444. }
  445. /* PCLK1 max 16M */
  446. if (SYSCLK_FREQ > 54000000)
  447. {
  448. RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV4;
  449. }
  450. else if (SYSCLK_FREQ > 27000000)
  451. {
  452. RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV2;
  453. }
  454. else
  455. {
  456. RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV1;
  457. }
  458. #if SYSCLK_SRC == SYSCLK_USE_MSI
  459. /* Select MSI as system clock source */
  460. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_SCLKSW));
  461. RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_MSI;
  462. /* Wait till MSI is used as system clock source */
  463. while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x00)
  464. {
  465. }
  466. #elif SYSCLK_SRC == SYSCLK_USE_HSI
  467. /* Select HSI as system clock source */
  468. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_SCLKSW));
  469. RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_HSI;
  470. /* Wait till HSI is used as system clock source */
  471. while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x04)
  472. {
  473. }
  474. #elif SYSCLK_SRC == SYSCLK_USE_HSE
  475. /* Select HSE as system clock source */
  476. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_SCLKSW));
  477. RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_HSE;
  478. /* Wait till HSE is used as system clock source */
  479. while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x08)
  480. {
  481. }
  482. #elif SYSCLK_SRC == SYSCLK_USE_HSI_PLL || SYSCLK_SRC == SYSCLK_USE_HSE_PLL
  483. /* clear bits */
  484. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_PLLSRC | RCC_CFG_PLLHSEPRES | RCC_CFG_PLLMULFCT));
  485. RCC->PLLHSIPRE &= (uint32_t)((uint32_t) ~(RCC_PLLHSIPRE_PLLHSIPRE | RCC_PLLHSIPRE_PLLSRCDIV));
  486. /* set PLL source */
  487. rcc_cfg = RCC->CFG;
  488. rcc_cfg |= (SYSCLK_SRC == SYSCLK_USE_HSI_PLL ? RCC_CFG_PLLSRC_HSI : RCC_CFG_PLLSRC_HSE);
  489. /* PLL DIV */
  490. rcc_pllhsipre = RCC->PLLHSIPRE;
  491. #if SYSCLK_SRC == SYSCLK_USE_HSI_PLL
  492. rcc_pllhsipre |= (PLLSRC_DIV == 1 ? RCC_PLLHSIPRE_PLLHSIPRE_HSI : RCC_PLLHSIPRE_PLLHSIPRE_HSI_DIV2);
  493. #elif SYSCLK_SRC == SYSCLK_USE_HSE_PLL
  494. rcc_cfg |= (PLLSRC_DIV == 1 ? RCC_CFG_PLLHSEPRES_HSE : RCC_CFG_PLLHSEPRES_HSE_DIV2);
  495. #endif
  496. /* set PLL DIV */
  497. rcc_pllhsipre |= (PLL_DIV == PLL_DIV2_DISABLE ? RCC_PLLHSIPRE_PLLSRCDIV_DISABLE : RCC_PLLHSIPRE_PLLSRCDIV_ENABLE);
  498. /* set PLL multiply factor */
  499. #if PLL_MUL <= 16
  500. rcc_cfg |= (PLL_MUL - 2) << 18;
  501. #else
  502. rcc_cfg |= ((PLL_MUL - 17) << 18) | (1 << 27);
  503. #endif
  504. RCC->CFG = rcc_cfg;
  505. RCC->PLLHSIPRE = rcc_pllhsipre;
  506. /* Enable PLL */
  507. RCC->CTRL |= RCC_CTRL_PLLEN;
  508. /* Wait till PLL is ready */
  509. while ((RCC->CTRL & RCC_CTRL_PLLRDF) == 0)
  510. {
  511. }
  512. /* Select PLL as system clock source */
  513. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_SCLKSW));
  514. RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_PLL;
  515. /* Wait till PLL is used as system clock source */
  516. while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x0C)
  517. {
  518. }
  519. #endif
  520. }