system_stm32f2xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f2xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  6. *
  7. * This file provides two functions and one global variable to be called from
  8. * user application:
  9. * - SystemInit(): This function is called at startup just after reset and
  10. * before branch to main program. This call is made inside
  11. * the "startup_stm32f2xx.s" file.
  12. *
  13. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  14. * by the user application to setup the SysTick
  15. * timer or configure other parameters.
  16. *
  17. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  18. * be called whenever the core clock is changed
  19. * during program execution.
  20. *
  21. ******************************************************************************
  22. * @attention
  23. *
  24. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  25. * All rights reserved.</center></h2>
  26. *
  27. * This software component is licensed by ST under BSD 3-Clause license,
  28. * the "License"; You may not use this file except in compliance with the
  29. * License. You may obtain a copy of the License at:
  30. * opensource.org/licenses/BSD-3-Clause
  31. *
  32. ******************************************************************************
  33. */
  34. /** @addtogroup CMSIS
  35. * @{
  36. */
  37. /** @addtogroup stm32f2xx_system
  38. * @{
  39. */
  40. /** @addtogroup STM32F2xx_System_Private_Includes
  41. * @{
  42. */
  43. #include "stm32f2xx.h"
  44. #if !defined (HSE_VALUE)
  45. #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
  46. #endif /* HSE_VALUE */
  47. #if !defined (HSI_VALUE)
  48. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  49. #endif /* HSI_VALUE */
  50. /**
  51. * @}
  52. */
  53. /** @addtogroup STM32F2xx_System_Private_TypesDefinitions
  54. * @{
  55. */
  56. /**
  57. * @}
  58. */
  59. /** @addtogroup STM32F2xx_System_Private_Defines
  60. * @{
  61. */
  62. /************************* Miscellaneous Configuration ************************/
  63. /*!< Uncomment the following line if you need to use external SRAM mounted
  64. on STM322xG_EVAL board as data memory */
  65. /* #define DATA_IN_ExtSRAM */
  66. /*!< Uncomment the following line if you need to relocate your vector Table in
  67. Internal SRAM. */
  68. /* #define VECT_TAB_SRAM */
  69. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  70. This value must be a multiple of 0x200. */
  71. /******************************************************************************/
  72. /**
  73. * @}
  74. */
  75. /** @addtogroup STM32F2xx_System_Private_Macros
  76. * @{
  77. */
  78. /**
  79. * @}
  80. */
  81. /** @addtogroup STM32F2xx_System_Private_Variables
  82. * @{
  83. */
  84. /* This varaible can be updated in Three ways :
  85. 1) by calling CMSIS function SystemCoreClockUpdate()
  86. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  87. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  88. Note: If you use this function to configure the system clock; then there
  89. is no need to call the 2 first functions listed above, since SystemCoreClock
  90. variable is updated automatically.
  91. */
  92. uint32_t SystemCoreClock = 12000000;
  93. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  94. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  95. /**
  96. * @}
  97. */
  98. /** @addtogroup STM32F2xx_System_Private_FunctionPrototypes
  99. * @{
  100. */
  101. #ifdef DATA_IN_ExtSRAM
  102. #include "bsp_fsmc_sram.h"
  103. // static void SystemInit_ExtMemCtl(void);
  104. #endif /* DATA_IN_ExtSRAM */
  105. /**
  106. * @}
  107. */
  108. /** @addtogroup STM32F2xx_System_Private_Functions
  109. * @{
  110. */
  111. /**
  112. * @brief Setup the microcontroller system
  113. * Initialize the Embedded Flash Interface, the PLL and update the
  114. * SystemFrequency variable.
  115. * @param None
  116. * @retval None
  117. */
  118. void SystemInit(void)
  119. {
  120. /* Reset the RCC clock configuration to the default reset state ------------*/
  121. /* Set HSION bit */
  122. RCC->CR |= (uint32_t)0x00000001;
  123. /* Reset CFGR register */
  124. RCC->CFGR = 0x00000000;
  125. /* Reset HSEON, CSSON and PLLON bits */
  126. RCC->CR &= (uint32_t)0xFEF6FFFF;
  127. /* Reset PLLCFGR register */
  128. RCC->PLLCFGR = 0x24003010;
  129. /* Reset HSEBYP bit */
  130. RCC->CR &= (uint32_t)0xFFFBFFFF;
  131. /* Disable all interrupts */
  132. RCC->CIR = 0x00000000;
  133. #ifdef DATA_IN_ExtSRAM
  134. MX_FSMC_SRAM_Init();
  135. #endif /* DATA_IN_ExtSRAM */
  136. /* Configure the Vector Table location add offset address ------------------*/
  137. #ifdef VECT_TAB_SRAM
  138. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  139. #else
  140. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  141. #endif
  142. }
  143. /**
  144. * @brief Update SystemCoreClock variable according to Clock Register Values.
  145. * The SystemCoreClock variable contains the core clock (HCLK), it can
  146. * be used by the user application to setup the SysTick timer or configure
  147. * other parameters.
  148. *
  149. * @note Each time the core clock (HCLK) changes, this function must be called
  150. * to update SystemCoreClock variable value. Otherwise, any configuration
  151. * based on this variable will be incorrect.
  152. *
  153. * @note - The system frequency computed by this function is not the real
  154. * frequency in the chip. It is calculated based on the predefined
  155. * constant and the selected clock source:
  156. *
  157. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  158. *
  159. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  160. *
  161. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  162. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  163. *
  164. * (*) HSI_VALUE is a constant defined in stm32f2xx_hal_conf.h file (default value
  165. * 16 MHz) but the real value may vary depending on the variations
  166. * in voltage and temperature.
  167. *
  168. * (**) HSE_VALUE is a constant defined in stm32f2xx_hal_conf.h file (default value
  169. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  170. * frequency of the crystal used. Otherwise, this function may
  171. * have wrong result.
  172. *
  173. * - The result of this function could be not correct when using fractional
  174. * value for HSE crystal.
  175. *
  176. * @param None
  177. * @retval None
  178. */
  179. void SystemCoreClockUpdate(void)
  180. {
  181. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  182. /* Get SYSCLK source -------------------------------------------------------*/
  183. tmp = RCC->CFGR & RCC_CFGR_SWS;
  184. switch (tmp)
  185. {
  186. case 0x00: /* HSI used as system clock source */
  187. SystemCoreClock = HSI_VALUE;
  188. break;
  189. case 0x04: /* HSE used as system clock source */
  190. SystemCoreClock = HSE_VALUE;
  191. break;
  192. case 0x08: /* PLL used as system clock source */
  193. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  194. SYSCLK = PLL_VCO / PLL_P
  195. */
  196. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  197. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  198. if (pllsource != 0)
  199. {
  200. /* HSE used as PLL clock source */
  201. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  202. }
  203. else
  204. {
  205. /* HSI used as PLL clock source */
  206. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  207. }
  208. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  209. SystemCoreClock = pllvco/pllp;
  210. break;
  211. default:
  212. SystemCoreClock = HSI_VALUE;
  213. break;
  214. }
  215. /* Compute HCLK frequency --------------------------------------------------*/
  216. /* Get HCLK prescaler */
  217. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  218. /* HCLK frequency */
  219. SystemCoreClock >>= tmp;
  220. }
  221. #ifdef DATA_IN_ExtSRAM
  222. ///**
  223. // * @brief Setup the external memory controller.
  224. // * Called in startup_stm32f2xx.s before jump to main.
  225. // * This function configures the external SRAM mounted on STM322xG_EVAL board
  226. // * This SRAM will be used as program data memory (including heap and stack).
  227. // * @param None
  228. // * @retval None
  229. // */
  230. //void SystemInit_ExtMemCtl(void)
  231. //{
  232. // __IO uint32_t tmp = 0x00;
  233. ///*-- GPIOs Configuration -----------------------------------------------------*/
  234. // /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  235. // RCC->AHB1ENR |= 0x00000078;
  236. // /* Delay after an RCC peripheral clock enabling */
  237. // tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
  238. // (void)(tmp);
  239. // /* Connect PDx pins to FSMC Alternate function */
  240. // GPIOD->AFR[0] = 0x00CCC0CC;
  241. // GPIOD->AFR[1] = 0xCCCCCCCC;
  242. // /* Configure PDx pins in Alternate function mode */
  243. // GPIOD->MODER = 0xAAAA0A8A;
  244. // /* Configure PDx pins speed to 100 MHz */
  245. // GPIOD->OSPEEDR = 0xFFFF0FCF;
  246. // /* Configure PDx pins Output type to push-pull */
  247. // GPIOD->OTYPER = 0x00000000;
  248. // /* No pull-up, pull-down for PDx pins */
  249. // GPIOD->PUPDR = 0x00000000;
  250. // /* Connect PEx pins to FSMC Alternate function */
  251. // GPIOE->AFR[0] = 0xC00CC0CC;
  252. // GPIOE->AFR[1] = 0xCCCCCCCC;
  253. // /* Configure PEx pins in Alternate function mode */
  254. // GPIOE->MODER = 0xAAAA828A;
  255. // /* Configure PEx pins speed to 100 MHz */
  256. // GPIOE->OSPEEDR = 0xFFFFC3CF;
  257. // /* Configure PEx pins Output type to push-pull */
  258. // GPIOE->OTYPER = 0x00000000;
  259. // /* No pull-up, pull-down for PEx pins */
  260. // GPIOE->PUPDR = 0x00000000;
  261. // /* Connect PFx pins to FSMC Alternate function */
  262. // GPIOF->AFR[0] = 0x00CCCCCC;
  263. // GPIOF->AFR[1] = 0xCCCC0000;
  264. // /* Configure PFx pins in Alternate function mode */
  265. // GPIOF->MODER = 0xAA000AAA;
  266. // /* Configure PFx pins speed to 100 MHz */
  267. // GPIOF->OSPEEDR = 0xFF000FFF;
  268. // /* Configure PFx pins Output type to push-pull */
  269. // GPIOF->OTYPER = 0x00000000;
  270. // /* No pull-up, pull-down for PFx pins */
  271. // GPIOF->PUPDR = 0x00000000;
  272. // /* Connect PGx pins to FSMC Alternate function */
  273. // GPIOG->AFR[0] = 0x00CCCCCC;
  274. // GPIOG->AFR[1] = 0x000000C0;
  275. // /* Configure PGx pins in Alternate function mode */
  276. // GPIOG->MODER = 0x00085AAA;
  277. // /* Configure PGx pins speed to 100 MHz */
  278. // GPIOG->OSPEEDR = 0x000CAFFF;
  279. // /* Configure PGx pins Output type to push-pull */
  280. // GPIOG->OTYPER = 0x00000000;
  281. // /* No pull-up, pull-down for PGx pins */
  282. // GPIOG->PUPDR = 0x00000000;
  283. //
  284. ///*--FSMC Configuration -------------------------------------------------------*/
  285. // /* Enable the FSMC interface clock */
  286. // RCC->AHB3ENR |= 0x00000001;
  287. // /* Configure and enable Bank1_SRAM2 */
  288. // FSMC_Bank1->BTCR[2] = 0x00001011;
  289. // FSMC_Bank1->BTCR[3] = 0x00000201;
  290. // FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
  291. //}
  292. #endif /* DATA_IN_ExtSRAM */
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/