system_stm32f10x.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f10x.c
  4. * @author MCD Application Team
  5. * @version V3.3.0
  6. * @date 04/16/2010
  7. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  8. ******************************************************************************
  9. *
  10. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  11. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  12. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  13. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  14. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  15. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  16. *
  17. * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  18. ******************************************************************************
  19. */
  20. /** @addtogroup CMSIS
  21. * @{
  22. */
  23. /** @addtogroup stm32f10x_system
  24. * @{
  25. */
  26. /** @addtogroup STM32F10x_System_Private_Includes
  27. * @{
  28. */
  29. #include "stm32f10x.h"
  30. /**
  31. * @}
  32. */
  33. /** @addtogroup STM32F10x_System_Private_TypesDefinitions
  34. * @{
  35. */
  36. /**
  37. * @}
  38. */
  39. /** @addtogroup STM32F10x_System_Private_Defines
  40. * @{
  41. */
  42. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  43. frequency (after reset the HSI is used as SYSCLK source)
  44. IMPORTANT NOTE:
  45. ==============
  46. 1. After each device reset the HSI is used as System clock source.
  47. 2. Please make sure that the selected System clock doesn't exceed your device's
  48. maximum frequency.
  49. 3. If none of the define below is enabled, the HSI is used as System clock
  50. source.
  51. 4. The System clock configuration functions provided within this file assume that:
  52. - For Low and Medium density Value line devices an external 8MHz crystal
  53. is used to drive the System clock.
  54. - For Low, Medium and High density devices an external 8MHz crystal is
  55. used to drive the System clock.
  56. - For Connectivity line devices an external 25MHz crystal is used to drive
  57. the System clock.
  58. If you are using different crystal you have to adapt those functions accordingly.
  59. */
  60. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL)
  61. /* #define SYSCLK_FREQ_HSE HSE_Value */
  62. #define SYSCLK_FREQ_24MHz 24000000
  63. #else
  64. /* #define SYSCLK_FREQ_HSE HSE_Value */
  65. /* #define SYSCLK_FREQ_24MHz 24000000 */
  66. /* #define SYSCLK_FREQ_36MHz 36000000 */
  67. /* #define SYSCLK_FREQ_48MHz 48000000 */
  68. /* #define SYSCLK_FREQ_56MHz 56000000 */
  69. #define SYSCLK_FREQ_72MHz 72000000
  70. #endif
  71. /*!< Uncomment the following line if you need to use external SRAM mounted
  72. on STM3210E-EVAL board (STM32 High density and XL-density devices) as data memory */
  73. #if defined (STM32F10X_HD) || (defined STM32F10X_XL)
  74. /* #define DATA_IN_ExtSRAM */
  75. #endif
  76. /**
  77. * @}
  78. */
  79. /** @addtogroup STM32F10x_System_Private_Macros
  80. * @{
  81. */
  82. /**
  83. * @}
  84. */
  85. /** @addtogroup STM32F10x_System_Private_Variables
  86. * @{
  87. */
  88. /*******************************************************************************
  89. * Clock Definitions
  90. *******************************************************************************/
  91. #ifdef SYSCLK_FREQ_HSE
  92. uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
  93. #elif defined SYSCLK_FREQ_24MHz
  94. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
  95. #elif defined SYSCLK_FREQ_36MHz
  96. uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
  97. #elif defined SYSCLK_FREQ_48MHz
  98. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
  99. #elif defined SYSCLK_FREQ_56MHz
  100. uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
  101. #elif defined SYSCLK_FREQ_72MHz
  102. uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
  103. #else /*!< HSI Selected as System Clock source */
  104. uint32_t SystemCoreClock = HSI_Value; /*!< System Clock Frequency (Core Clock) */
  105. #endif
  106. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  107. /**
  108. * @}
  109. */
  110. /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
  111. * @{
  112. */
  113. static void SetSysClock(void);
  114. #ifdef SYSCLK_FREQ_HSE
  115. static void SetSysClockToHSE(void);
  116. #elif defined SYSCLK_FREQ_24MHz
  117. static void SetSysClockTo24(void);
  118. #elif defined SYSCLK_FREQ_36MHz
  119. static void SetSysClockTo36(void);
  120. #elif defined SYSCLK_FREQ_48MHz
  121. static void SetSysClockTo48(void);
  122. #elif defined SYSCLK_FREQ_56MHz
  123. static void SetSysClockTo56(void);
  124. #elif defined SYSCLK_FREQ_72MHz
  125. static void SetSysClockTo72(void);
  126. #endif
  127. #ifdef DATA_IN_ExtSRAM
  128. static void SystemInit_ExtMemCtl(void);
  129. #endif /* DATA_IN_ExtSRAM */
  130. /**
  131. * @}
  132. */
  133. /** @addtogroup STM32F10x_System_Private_Functions
  134. * @{
  135. */
  136. /**
  137. * @brief Setup the microcontroller system
  138. * Initialize the Embedded Flash Interface, the PLL and update the
  139. * SystemCoreClock variable.
  140. * @note This function should be used only after reset.
  141. * @param None
  142. * @retval None
  143. */
  144. void SystemInit (void)
  145. {
  146. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  147. /* Set HSION bit */
  148. RCC->CR |= (uint32_t)0x00000001;
  149. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  150. #ifndef STM32F10X_CL
  151. RCC->CFGR &= (uint32_t)0xF8FF0000;
  152. #else
  153. RCC->CFGR &= (uint32_t)0xF0FF0000;
  154. #endif /* STM32F10X_CL */
  155. /* Reset HSEON, CSSON and PLLON bits */
  156. RCC->CR &= (uint32_t)0xFEF6FFFF;
  157. /* Reset HSEBYP bit */
  158. RCC->CR &= (uint32_t)0xFFFBFFFF;
  159. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  160. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  161. #ifdef STM32F10X_CL
  162. /* Reset PLL2ON and PLL3ON bits */
  163. RCC->CR &= (uint32_t)0xEBFFFFFF;
  164. /* Disable all interrupts and clear pending bits */
  165. RCC->CIR = 0x00FF0000;
  166. /* Reset CFGR2 register */
  167. RCC->CFGR2 = 0x00000000;
  168. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL)
  169. /* Disable all interrupts and clear pending bits */
  170. RCC->CIR = 0x009F0000;
  171. /* Reset CFGR2 register */
  172. RCC->CFGR2 = 0x00000000;
  173. #else
  174. /* Disable all interrupts and clear pending bits */
  175. RCC->CIR = 0x009F0000;
  176. #endif /* STM32F10X_CL */
  177. #if defined (STM32F10X_HD) || (defined STM32F10X_XL)
  178. #ifdef DATA_IN_ExtSRAM
  179. SystemInit_ExtMemCtl();
  180. #endif /* DATA_IN_ExtSRAM */
  181. #endif
  182. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  183. /* Configure the Flash Latency cycles and enable prefetch buffer */
  184. SetSysClock();
  185. }
  186. /**
  187. * @brief Update SystemCoreClock according to Clock Register Values
  188. * @note None
  189. * @param None
  190. * @retval None
  191. */
  192. void SystemCoreClockUpdate (void)
  193. {
  194. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  195. #ifdef STM32F10X_CL
  196. uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  197. #endif /* STM32F10X_CL */
  198. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL)
  199. uint32_t prediv1factor = 0;
  200. #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL */
  201. /* Get SYSCLK source -------------------------------------------------------*/
  202. tmp = RCC->CFGR & RCC_CFGR_SWS;
  203. switch (tmp)
  204. {
  205. case 0x00: /* HSI used as system clock */
  206. SystemCoreClock = HSI_Value;
  207. break;
  208. case 0x04: /* HSE used as system clock */
  209. SystemCoreClock = HSE_Value;
  210. break;
  211. case 0x08: /* PLL used as system clock */
  212. /* Get PLL clock source and multiplication factor ----------------------*/
  213. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  214. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  215. #ifndef STM32F10X_CL
  216. pllmull = ( pllmull >> 18) + 2;
  217. if (pllsource == 0x00)
  218. {
  219. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  220. SystemCoreClock = (HSI_Value >> 1) * pllmull;
  221. }
  222. else
  223. {
  224. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL)
  225. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  226. /* HSE oscillator clock selected as PREDIV1 clock entry */
  227. SystemCoreClock = (HSE_Value / prediv1factor) * pllmull;
  228. #else
  229. /* HSE selected as PLL clock entry */
  230. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  231. {/* HSE oscillator clock divided by 2 */
  232. SystemCoreClock = (HSE_Value >> 1) * pllmull;
  233. }
  234. else
  235. {
  236. SystemCoreClock = HSE_Value * pllmull;
  237. }
  238. #endif
  239. }
  240. #else
  241. pllmull = pllmull >> 18;
  242. if (pllmull != 0x0D)
  243. {
  244. pllmull += 2;
  245. }
  246. else
  247. { /* PLL multiplication factor = PLL input clock * 6.5 */
  248. pllmull = 13 / 2;
  249. }
  250. if (pllsource == 0x00)
  251. {
  252. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  253. SystemCoreClock = (HSI_Value >> 1) * pllmull;
  254. }
  255. else
  256. {/* PREDIV1 selected as PLL clock entry */
  257. /* Get PREDIV1 clock source and division factor */
  258. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  259. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  260. if (prediv1source == 0)
  261. {
  262. /* HSE oscillator clock selected as PREDIV1 clock entry */
  263. SystemCoreClock = (HSE_Value / prediv1factor) * pllmull;
  264. }
  265. else
  266. {/* PLL2 clock selected as PREDIV1 clock entry */
  267. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  268. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  269. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  270. SystemCoreClock = (((HSE_Value / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  271. }
  272. }
  273. #endif /* STM32F10X_CL */
  274. break;
  275. default:
  276. SystemCoreClock = HSI_Value;
  277. break;
  278. }
  279. /* Compute HCLK clock frequency ----------------*/
  280. /* Get HCLK prescaler */
  281. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  282. /* HCLK clock frequency */
  283. SystemCoreClock >>= tmp;
  284. }
  285. /**
  286. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  287. * @param None
  288. * @retval None
  289. */
  290. static void SetSysClock(void)
  291. {
  292. #ifdef SYSCLK_FREQ_HSE
  293. SetSysClockToHSE();
  294. #elif defined SYSCLK_FREQ_24MHz
  295. SetSysClockTo24();
  296. #elif defined SYSCLK_FREQ_36MHz
  297. SetSysClockTo36();
  298. #elif defined SYSCLK_FREQ_48MHz
  299. SetSysClockTo48();
  300. #elif defined SYSCLK_FREQ_56MHz
  301. SetSysClockTo56();
  302. #elif defined SYSCLK_FREQ_72MHz
  303. SetSysClockTo72();
  304. #endif
  305. /* If none of the define above is enabled, the HSI is used as System clock
  306. source (default after reset) */
  307. }
  308. /**
  309. * @brief Setup the external memory controller. Called in startup_stm32f10x.s
  310. * before jump to __main
  311. * @param None
  312. * @retval None
  313. */
  314. #ifdef DATA_IN_ExtSRAM
  315. /**
  316. * @brief Setup the external memory controller.
  317. * Called in startup_stm32f10x_xx.s/.c before jump to main.
  318. * This function configures the external SRAM mounted on STM3210E-EVAL
  319. * board (STM32 High density devices). This SRAM will be used as program
  320. * data memory (including heap and stack).
  321. * @param None
  322. * @retval None
  323. */
  324. void SystemInit_ExtMemCtl(void)
  325. {
  326. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  327. required, then adjust the Register Addresses */
  328. /* Enable FSMC clock */
  329. RCC->AHBENR = 0x00000114;
  330. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  331. RCC->APB2ENR = 0x000001E0;
  332. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  333. /*---------------- SRAM Address lines configuration -------------------------*/
  334. /*---------------- NOE and NWE configuration --------------------------------*/
  335. /*---------------- NE3 configuration ----------------------------------------*/
  336. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  337. GPIOD->CRL = 0x44BB44BB;
  338. GPIOD->CRH = 0xBBBBBBBB;
  339. GPIOE->CRL = 0xB44444BB;
  340. GPIOE->CRH = 0xBBBBBBBB;
  341. GPIOF->CRL = 0x44BBBBBB;
  342. GPIOF->CRH = 0xBBBB4444;
  343. GPIOG->CRL = 0x44BBBBBB;
  344. GPIOG->CRH = 0x44444B44;
  345. /*---------------- FSMC Configuration ---------------------------------------*/
  346. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  347. FSMC_Bank1->BTCR[4] = 0x00001011;
  348. FSMC_Bank1->BTCR[5] = 0x00000200;
  349. }
  350. #endif /* DATA_IN_ExtSRAM */
  351. #ifdef SYSCLK_FREQ_HSE
  352. /**
  353. * @brief Selects HSE as System clock source and configure HCLK, PCLK2
  354. * and PCLK1 prescalers.
  355. * @note This function should be used only after reset.
  356. * @param None
  357. * @retval None
  358. */
  359. static void SetSysClockToHSE(void)
  360. {
  361. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  362. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  363. /* Enable HSE */
  364. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  365. /* Wait till HSE is ready and if Time out is reached exit */
  366. do
  367. {
  368. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  369. StartUpCounter++;
  370. } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
  371. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  372. {
  373. HSEStatus = (uint32_t)0x01;
  374. }
  375. else
  376. {
  377. HSEStatus = (uint32_t)0x00;
  378. }
  379. if (HSEStatus == (uint32_t)0x01)
  380. {
  381. #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL
  382. /* Enable Prefetch Buffer */
  383. FLASH->ACR |= FLASH_ACR_PRFTBE;
  384. /* Flash 0 wait state */
  385. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  386. #ifndef STM32F10X_CL
  387. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  388. #else
  389. if (HSE_Value <= 24000000)
  390. {
  391. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  392. }
  393. else
  394. {
  395. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  396. }
  397. #endif /* STM32F10X_CL */
  398. #endif
  399. /* HCLK = SYSCLK */
  400. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  401. /* PCLK2 = HCLK */
  402. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  403. /* PCLK1 = HCLK */
  404. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  405. /* Select HSE as system clock source */
  406. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  407. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
  408. /* Wait till HSE is used as system clock source */
  409. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
  410. {
  411. }
  412. }
  413. else
  414. { /* If HSE fails to start-up, the application will have wrong clock
  415. configuration. User can add here some code to deal with this error */
  416. }
  417. }
  418. #elif defined SYSCLK_FREQ_24MHz
  419. /**
  420. * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
  421. * and PCLK1 prescalers.
  422. * @note This function should be used only after reset.
  423. * @param None
  424. * @retval None
  425. */
  426. static void SetSysClockTo24(void)
  427. {
  428. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  429. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  430. /* Enable HSE */
  431. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  432. /* Wait till HSE is ready and if Time out is reached exit */
  433. do
  434. {
  435. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  436. StartUpCounter++;
  437. } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
  438. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  439. {
  440. HSEStatus = (uint32_t)0x01;
  441. }
  442. else
  443. {
  444. HSEStatus = (uint32_t)0x00;
  445. }
  446. if (HSEStatus == (uint32_t)0x01)
  447. {
  448. #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL
  449. /* Enable Prefetch Buffer */
  450. FLASH->ACR |= FLASH_ACR_PRFTBE;
  451. /* Flash 0 wait state */
  452. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  453. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
  454. #endif
  455. /* HCLK = SYSCLK */
  456. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  457. /* PCLK2 = HCLK */
  458. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  459. /* PCLK1 = HCLK */
  460. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  461. #ifdef STM32F10X_CL
  462. /* Configure PLLs ------------------------------------------------------*/
  463. /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */
  464. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  465. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  466. RCC_CFGR_PLLMULL6);
  467. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  468. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
  469. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  470. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  471. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  472. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
  473. /* Enable PLL2 */
  474. RCC->CR |= RCC_CR_PLL2ON;
  475. /* Wait till PLL2 is ready */
  476. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  477. {
  478. }
  479. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL)
  480. /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
  481. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  482. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);
  483. #else
  484. /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */
  485. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  486. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  487. #endif /* STM32F10X_CL */
  488. /* Enable PLL */
  489. RCC->CR |= RCC_CR_PLLON;
  490. /* Wait till PLL is ready */
  491. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  492. {
  493. }
  494. /* Select PLL as system clock source */
  495. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  496. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  497. /* Wait till PLL is used as system clock source */
  498. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  499. {
  500. }
  501. }
  502. else
  503. { /* If HSE fails to start-up, the application will have wrong clock
  504. configuration. User can add here some code to deal with this error */
  505. }
  506. }
  507. #elif defined SYSCLK_FREQ_36MHz
  508. /**
  509. * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  510. * and PCLK1 prescalers.
  511. * @note This function should be used only after reset.
  512. * @param None
  513. * @retval None
  514. */
  515. static void SetSysClockTo36(void)
  516. {
  517. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  518. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  519. /* Enable HSE */
  520. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  521. /* Wait till HSE is ready and if Time out is reached exit */
  522. do
  523. {
  524. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  525. StartUpCounter++;
  526. } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
  527. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  528. {
  529. HSEStatus = (uint32_t)0x01;
  530. }
  531. else
  532. {
  533. HSEStatus = (uint32_t)0x00;
  534. }
  535. if (HSEStatus == (uint32_t)0x01)
  536. {
  537. /* Enable Prefetch Buffer */
  538. FLASH->ACR |= FLASH_ACR_PRFTBE;
  539. /* Flash 1 wait state */
  540. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  541. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  542. /* HCLK = SYSCLK */
  543. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  544. /* PCLK2 = HCLK */
  545. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  546. /* PCLK1 = HCLK */
  547. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  548. #ifdef STM32F10X_CL
  549. /* Configure PLLs ------------------------------------------------------*/
  550. /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */
  551. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  552. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  553. RCC_CFGR_PLLMULL9);
  554. /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  555. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
  556. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  557. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  558. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  559. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);
  560. /* Enable PLL2 */
  561. RCC->CR |= RCC_CR_PLL2ON;
  562. /* Wait till PLL2 is ready */
  563. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  564. {
  565. }
  566. #else
  567. /* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */
  568. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  569. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);
  570. #endif /* STM32F10X_CL */
  571. /* Enable PLL */
  572. RCC->CR |= RCC_CR_PLLON;
  573. /* Wait till PLL is ready */
  574. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  575. {
  576. }
  577. /* Select PLL as system clock source */
  578. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  579. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  580. /* Wait till PLL is used as system clock source */
  581. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  582. {
  583. }
  584. }
  585. else
  586. { /* If HSE fails to start-up, the application will have wrong clock
  587. configuration. User can add here some code to deal with this error */
  588. }
  589. }
  590. #elif defined SYSCLK_FREQ_48MHz
  591. /**
  592. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  593. * and PCLK1 prescalers.
  594. * @note This function should be used only after reset.
  595. * @param None
  596. * @retval None
  597. */
  598. static void SetSysClockTo48(void)
  599. {
  600. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  601. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  602. /* Enable HSE */
  603. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  604. /* Wait till HSE is ready and if Time out is reached exit */
  605. do
  606. {
  607. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  608. StartUpCounter++;
  609. } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
  610. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  611. {
  612. HSEStatus = (uint32_t)0x01;
  613. }
  614. else
  615. {
  616. HSEStatus = (uint32_t)0x00;
  617. }
  618. if (HSEStatus == (uint32_t)0x01)
  619. {
  620. /* Enable Prefetch Buffer */
  621. FLASH->ACR |= FLASH_ACR_PRFTBE;
  622. /* Flash 1 wait state */
  623. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  624. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
  625. /* HCLK = SYSCLK */
  626. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  627. /* PCLK2 = HCLK */
  628. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  629. /* PCLK1 = HCLK */
  630. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  631. #ifdef STM32F10X_CL
  632. /* Configure PLLs ------------------------------------------------------*/
  633. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  634. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  635. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  636. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  637. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  638. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  639. /* Enable PLL2 */
  640. RCC->CR |= RCC_CR_PLL2ON;
  641. /* Wait till PLL2 is ready */
  642. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  643. {
  644. }
  645. /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
  646. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  647. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  648. RCC_CFGR_PLLMULL6);
  649. #else
  650. /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
  651. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  652. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
  653. #endif /* STM32F10X_CL */
  654. /* Enable PLL */
  655. RCC->CR |= RCC_CR_PLLON;
  656. /* Wait till PLL is ready */
  657. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  658. {
  659. }
  660. /* Select PLL as system clock source */
  661. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  662. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  663. /* Wait till PLL is used as system clock source */
  664. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  665. {
  666. }
  667. }
  668. else
  669. { /* If HSE fails to start-up, the application will have wrong clock
  670. configuration. User can add here some code to deal with this error */
  671. }
  672. }
  673. #elif defined SYSCLK_FREQ_56MHz
  674. /**
  675. * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
  676. * and PCLK1 prescalers.
  677. * @note This function should be used only after reset.
  678. * @param None
  679. * @retval None
  680. */
  681. static void SetSysClockTo56(void)
  682. {
  683. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  684. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  685. /* Enable HSE */
  686. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  687. /* Wait till HSE is ready and if Time out is reached exit */
  688. do
  689. {
  690. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  691. StartUpCounter++;
  692. } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
  693. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  694. {
  695. HSEStatus = (uint32_t)0x01;
  696. }
  697. else
  698. {
  699. HSEStatus = (uint32_t)0x00;
  700. }
  701. if (HSEStatus == (uint32_t)0x01)
  702. {
  703. /* Enable Prefetch Buffer */
  704. FLASH->ACR |= FLASH_ACR_PRFTBE;
  705. /* Flash 2 wait state */
  706. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  707. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  708. /* HCLK = SYSCLK */
  709. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  710. /* PCLK2 = HCLK */
  711. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  712. /* PCLK1 = HCLK */
  713. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  714. #ifdef STM32F10X_CL
  715. /* Configure PLLs ------------------------------------------------------*/
  716. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  717. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  718. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  719. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  720. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  721. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  722. /* Enable PLL2 */
  723. RCC->CR |= RCC_CR_PLL2ON;
  724. /* Wait till PLL2 is ready */
  725. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  726. {
  727. }
  728. /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
  729. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  730. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  731. RCC_CFGR_PLLMULL7);
  732. #else
  733. /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
  734. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  735. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
  736. #endif /* STM32F10X_CL */
  737. /* Enable PLL */
  738. RCC->CR |= RCC_CR_PLLON;
  739. /* Wait till PLL is ready */
  740. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  741. {
  742. }
  743. /* Select PLL as system clock source */
  744. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  745. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  746. /* Wait till PLL is used as system clock source */
  747. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  748. {
  749. }
  750. }
  751. else
  752. { /* If HSE fails to start-up, the application will have wrong clock
  753. configuration. User can add here some code to deal with this error */
  754. }
  755. }
  756. #elif defined SYSCLK_FREQ_72MHz
  757. /**
  758. * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  759. * and PCLK1 prescalers.
  760. * @note This function should be used only after reset.
  761. * @param None
  762. * @retval None
  763. */
  764. static void SetSysClockTo72(void)
  765. {
  766. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  767. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  768. /* Enable HSE */
  769. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  770. /* Wait till HSE is ready and if Time out is reached exit */
  771. do
  772. {
  773. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  774. StartUpCounter++;
  775. } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
  776. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  777. {
  778. HSEStatus = (uint32_t)0x01;
  779. }
  780. else
  781. {
  782. HSEStatus = (uint32_t)0x00;
  783. }
  784. if (HSEStatus == (uint32_t)0x01)
  785. {
  786. /* Enable Prefetch Buffer */
  787. FLASH->ACR |= FLASH_ACR_PRFTBE;
  788. /* Flash 2 wait state */
  789. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  790. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
  791. /* HCLK = SYSCLK */
  792. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  793. /* PCLK2 = HCLK */
  794. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  795. /* PCLK1 = HCLK */
  796. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  797. #ifdef STM32F10X_CL
  798. /* Configure PLLs ------------------------------------------------------*/
  799. /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  800. /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
  801. RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
  802. RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
  803. RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
  804. RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
  805. /* Enable PLL2 */
  806. RCC->CR |= RCC_CR_PLL2ON;
  807. /* Wait till PLL2 is ready */
  808. while((RCC->CR & RCC_CR_PLL2RDY) == 0)
  809. {
  810. }
  811. /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */
  812. RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
  813. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
  814. RCC_CFGR_PLLMULL9);
  815. #else
  816. /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
  817. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
  818. RCC_CFGR_PLLMULL));
  819. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
  820. #endif /* STM32F10X_CL */
  821. /* Enable PLL */
  822. RCC->CR |= RCC_CR_PLLON;
  823. /* Wait till PLL is ready */
  824. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  825. {
  826. }
  827. /* Select PLL as system clock source */
  828. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  829. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  830. /* Wait till PLL is used as system clock source */
  831. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  832. {
  833. }
  834. }
  835. else
  836. { /* If HSE fails to start-up, the application will have wrong clock
  837. configuration. User can add here some code to deal with this error */
  838. }
  839. }
  840. #endif
  841. /**
  842. * @}
  843. */
  844. /**
  845. * @}
  846. */
  847. /**
  848. * @}
  849. */
  850. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
  851. void _exit(int i)
  852. {
  853. while (1);
  854. }