system_stm32f2xx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f2xx.c
  4. * @author MCD Application Team
  5. * @version V1.2.0
  6. * @date 13-April-2012
  7. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  8. * This file contains the system clock configuration for STM32F2xx devices,
  9. * and is generated by the clock configuration tool
  10. * "STM32f2xx_Clock_Configuration_V1.0.0.xls"
  11. *
  12. * 1. This file provides two functions and one global variable to be called from
  13. * user application:
  14. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  15. * and Divider factors, AHB/APBx prescalers and Flash settings),
  16. * depending on the configuration made in the clock xls tool.
  17. * This function is called at startup just after reset and
  18. * before branch to main program. This call is made inside
  19. * the "startup_stm32f2xx.s" file.
  20. *
  21. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  22. * by the user application to setup the SysTick
  23. * timer or configure other parameters.
  24. *
  25. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  26. * be called whenever the core clock is changed
  27. * during program execution.
  28. *
  29. * 2. After each device reset the HSI (16 MHz) is used as system clock source.
  30. * Then SystemInit() function is called, in "startup_stm32f2xx.s" file, to
  31. * configure the system clock before to branch to main program.
  32. *
  33. * 3. If the system clock source selected by user fails to startup, the SystemInit()
  34. * function will do nothing and HSI still used as system clock source. User can
  35. * add some code to deal with this issue inside the SetSysClock() function.
  36. *
  37. * 4. The default value of HSE crystal is set to 25MHz, refer to "HSE_VALUE" define
  38. * in "stm32f2xx.h" file. When HSE is used as system clock source, directly or
  39. * through PLL, and you are using different crystal you have to adapt the HSE
  40. * value to your own configuration.
  41. *
  42. * 5. This file configures the system clock as follows:
  43. *=============================================================================
  44. *=============================================================================
  45. * Supported STM32F2xx device revision | Rev B and Y
  46. *-----------------------------------------------------------------------------
  47. * System Clock source | PLL (HSE)
  48. *-----------------------------------------------------------------------------
  49. * SYSCLK(Hz) | 120000000
  50. *-----------------------------------------------------------------------------
  51. * HCLK(Hz) | 120000000
  52. *-----------------------------------------------------------------------------
  53. * AHB Prescaler | 1
  54. *-----------------------------------------------------------------------------
  55. * APB1 Prescaler | 4
  56. *-----------------------------------------------------------------------------
  57. * APB2 Prescaler | 2
  58. *-----------------------------------------------------------------------------
  59. * HSE Frequency(Hz) | 25000000
  60. *-----------------------------------------------------------------------------
  61. * PLL_M | 25
  62. *-----------------------------------------------------------------------------
  63. * PLL_N | 240
  64. *-----------------------------------------------------------------------------
  65. * PLL_P | 2
  66. *-----------------------------------------------------------------------------
  67. * PLL_Q | 5
  68. *-----------------------------------------------------------------------------
  69. * PLLI2S_N | NA
  70. *-----------------------------------------------------------------------------
  71. * PLLI2S_R | NA
  72. *-----------------------------------------------------------------------------
  73. * I2S input clock | NA
  74. *-----------------------------------------------------------------------------
  75. * VDD(V) | 3.3
  76. *-----------------------------------------------------------------------------
  77. * Flash Latency(WS) | 3
  78. *-----------------------------------------------------------------------------
  79. * Prefetch Buffer | ON
  80. *-----------------------------------------------------------------------------
  81. * Instruction cache | ON
  82. *-----------------------------------------------------------------------------
  83. * Data cache | ON
  84. *-----------------------------------------------------------------------------
  85. * Require 48MHz for USB OTG FS, | Enabled
  86. * SDIO and RNG clock |
  87. *-----------------------------------------------------------------------------
  88. *=============================================================================
  89. ******************************************************************************
  90. * @attention
  91. *
  92. * Copyright (c) 2012 STMicroelectronics.
  93. * All rights reserved.
  94. *
  95. * This software is licensed under terms that can be found in the LICENSE file
  96. * in the root directory of this software component.
  97. * If no LICENSE file comes with this software, it is provided AS-IS.
  98. *
  99. ******************************************************************************
  100. */
  101. /** @addtogroup CMSIS
  102. * @{
  103. */
  104. /** @addtogroup stm32f2xx_system
  105. * @{
  106. */
  107. /** @addtogroup STM32F2xx_System_Private_Includes
  108. * @{
  109. */
  110. #include "stm32f2xx.h"
  111. #include "sram.h"
  112. /**
  113. * @}
  114. */
  115. /** @addtogroup STM32F2xx_System_Private_TypesDefinitions
  116. * @{
  117. */
  118. /**
  119. * @}
  120. */
  121. /** @addtogroup STM32F2xx_System_Private_Defines
  122. * @{
  123. */
  124. /*!< Uncomment the following line if you need to use external SRAM mounted
  125. on STM322xG_EVAL board as data memory */
  126. /* #define DATA_IN_ExtSRAM */
  127. /*!< Uncomment the following line if you need to relocate your vector Table in
  128. Internal SRAM. */
  129. /* #define VECT_TAB_SRAM */
  130. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  131. This value must be a multiple of 0x200. */
  132. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
  133. #define PLL_M 25
  134. #define PLL_N 240
  135. /* SYSCLK = PLL_VCO / PLL_P */
  136. #define PLL_P 2
  137. /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
  138. #define PLL_Q 5
  139. /**
  140. * @}
  141. */
  142. /** @addtogroup STM32F2xx_System_Private_Macros
  143. * @{
  144. */
  145. /**
  146. * @}
  147. */
  148. /** @addtogroup STM32F2xx_System_Private_Variables
  149. * @{
  150. */
  151. uint32_t SystemCoreClock = 120000000;
  152. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  153. /**
  154. * @}
  155. */
  156. /** @addtogroup STM32F2xx_System_Private_FunctionPrototypes
  157. * @{
  158. */
  159. static void SetSysClock(void);
  160. #ifdef DATA_IN_ExtSRAM
  161. static void SystemInit_ExtMemCtl(void);
  162. #endif /* DATA_IN_ExtSRAM */
  163. /**
  164. * @}
  165. */
  166. /** @addtogroup STM32F2xx_System_Private_Functions
  167. * @{
  168. */
  169. /**
  170. * @brief Setup the microcontroller system
  171. * Initialize the Embedded Flash Interface, the PLL and update the
  172. * SystemFrequency variable.
  173. * @param None
  174. * @retval None
  175. */
  176. void SystemInit(void)
  177. {
  178. /* Reset the RCC clock configuration to the default reset state ------------*/
  179. /* Set HSION bit */
  180. RCC->CR |= (uint32_t)0x00000001;
  181. /* Reset CFGR register */
  182. RCC->CFGR = 0x00000000;
  183. /* Reset HSEON, CSSON and PLLON bits */
  184. RCC->CR &= (uint32_t)0xFEF6FFFF;
  185. /* Reset PLLCFGR register */
  186. RCC->PLLCFGR = 0x24003010;
  187. /* Reset HSEBYP bit */
  188. RCC->CR &= (uint32_t)0xFFFBFFFF;
  189. /* Disable all interrupts */
  190. RCC->CIR = 0x00000000;
  191. #ifdef DATA_IN_ExtSRAM
  192. SystemInit_ExtMemCtl();
  193. #endif /* DATA_IN_ExtSRAM */
  194. /* Configure the System clock source, PLL Multiplier and Divider factors,
  195. AHB/APBx prescalers and Flash settings ----------------------------------*/
  196. SetSysClock();
  197. /* Configure the Vector Table location add offset address ------------------*/
  198. #ifdef VECT_TAB_SRAM
  199. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  200. #else
  201. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  202. #endif
  203. }
  204. /**
  205. * @brief Update SystemCoreClock variable according to Clock Register Values.
  206. * The SystemCoreClock variable contains the core clock (HCLK), it can
  207. * be used by the user application to setup the SysTick timer or configure
  208. * other parameters.
  209. *
  210. * @note Each time the core clock (HCLK) changes, this function must be called
  211. * to update SystemCoreClock variable value. Otherwise, any configuration
  212. * based on this variable will be incorrect.
  213. *
  214. * @note - The system frequency computed by this function is not the real
  215. * frequency in the chip. It is calculated based on the predefined
  216. * constant and the selected clock source:
  217. *
  218. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  219. *
  220. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  221. *
  222. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  223. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  224. *
  225. * (*) HSI_VALUE is a constant defined in stm32f2xx.h file (default value
  226. * 16 MHz) but the real value may vary depending on the variations
  227. * in voltage and temperature.
  228. *
  229. * (**) HSE_VALUE is a constant defined in stm32f2xx.h file (default value
  230. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  231. * frequency of the crystal used. Otherwise, this function may
  232. * have wrong result.
  233. *
  234. * - The result of this function could be not correct when using fractional
  235. * value for HSE crystal.
  236. *
  237. * @param None
  238. * @retval None
  239. */
  240. void SystemCoreClockUpdate(void)
  241. {
  242. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  243. /* Get SYSCLK source -------------------------------------------------------*/
  244. tmp = RCC->CFGR & RCC_CFGR_SWS;
  245. switch (tmp)
  246. {
  247. case 0x00: /* HSI used as system clock source */
  248. SystemCoreClock = HSI_VALUE;
  249. break;
  250. case 0x04: /* HSE used as system clock source */
  251. SystemCoreClock = HSE_VALUE;
  252. break;
  253. case 0x08: /* PLL used as system clock source */
  254. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  255. SYSCLK = PLL_VCO / PLL_P
  256. */
  257. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  258. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  259. if (pllsource != 0)
  260. {
  261. /* HSE used as PLL clock source */
  262. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  263. }
  264. else
  265. {
  266. /* HSI used as PLL clock source */
  267. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  268. }
  269. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  270. SystemCoreClock = pllvco/pllp;
  271. break;
  272. default:
  273. SystemCoreClock = HSI_VALUE;
  274. break;
  275. }
  276. /* Compute HCLK frequency --------------------------------------------------*/
  277. /* Get HCLK prescaler */
  278. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  279. /* HCLK frequency */
  280. SystemCoreClock >>= tmp;
  281. }
  282. /**
  283. * @brief Configures the System clock source, PLL Multiplier and Divider factors,
  284. * AHB/APBx prescalers and Flash settings
  285. * @Note This function should be called only once the RCC clock configuration
  286. * is reset to the default reset state (done in SystemInit() function).
  287. * @param None
  288. * @retval None
  289. */
  290. static void SetSysClock(void)
  291. {
  292. /******************************************************************************/
  293. /* PLL (clocked by HSE) used as System clock source */
  294. /******************************************************************************/
  295. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  296. /* Enable HSE */
  297. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  298. /* Wait till HSE is ready and if Time out is reached exit */
  299. do
  300. {
  301. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  302. StartUpCounter++;
  303. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  304. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  305. {
  306. HSEStatus = (uint32_t)0x01;
  307. }
  308. else
  309. {
  310. HSEStatus = (uint32_t)0x00;
  311. }
  312. if (HSEStatus == (uint32_t)0x01)
  313. {
  314. /* HCLK = SYSCLK / 1*/
  315. RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  316. /* PCLK2 = HCLK / 2*/
  317. RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
  318. /* PCLK1 = HCLK / 4*/
  319. RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
  320. /* Configure the main PLL */
  321. RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
  322. (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
  323. /* Enable the main PLL */
  324. RCC->CR |= RCC_CR_PLLON;
  325. /* Wait till the main PLL is ready */
  326. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  327. {
  328. }
  329. /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  330. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS;
  331. /* Select the main PLL as system clock source */
  332. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  333. RCC->CFGR |= RCC_CFGR_SW_PLL;
  334. /* Wait till the main PLL is used as system clock source */
  335. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL){;}
  336. {
  337. }
  338. }
  339. else
  340. { /* If HSE fails to start-up, the application will have wrong clock
  341. configuration. User can add here some code to deal with this error */
  342. }
  343. }
  344. /**
  345. * @brief Setup the external memory controller. Called in startup_stm32f2xx.s
  346. * before jump to __main
  347. * @param None
  348. * @retval None
  349. */
  350. #ifdef DATA_IN_ExtSRAM
  351. /**
  352. * @brief Setup the external memory controller.
  353. * Called in startup_stm32f2xx.s before jump to main.
  354. * This function configures the external SRAM mounted on STM322xG_EVAL board
  355. * This SRAM will be used as program data memory (including heap and stack).
  356. * @param None
  357. * @retval None
  358. */
  359. void SystemInit_ExtMemCtl(void)
  360. {
  361. FSMC_SRAM_Init();
  362. }
  363. #endif /* DATA_IN_ExtSRAM */
  364. /**
  365. * @}
  366. */
  367. /**
  368. * @}
  369. */
  370. /**
  371. * @}
  372. */