system_stm32f2xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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) 2016 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) /*!< Default 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 variable 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 = 16000000;
  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. static void SystemInit_ExtMemCtl(void);
  103. #endif /* DATA_IN_ExtSRAM */
  104. /**
  105. * @}
  106. */
  107. /** @addtogroup STM32F2xx_System_Private_Functions
  108. * @{
  109. */
  110. /**
  111. * @brief Setup the microcontroller system
  112. * Initialize the Embedded Flash Interface, the PLL and update the
  113. * SystemFrequency variable.
  114. * @param None
  115. * @retval None
  116. */
  117. void SystemInit(void)
  118. {
  119. /* Reset the RCC clock configuration to the default reset state ------------*/
  120. /* Set HSION bit */
  121. RCC->CR |= (uint32_t)0x00000001;
  122. /* Reset CFGR register */
  123. RCC->CFGR = 0x00000000;
  124. /* Reset HSEON, CSSON and PLLON bits */
  125. RCC->CR &= (uint32_t)0xFEF6FFFF;
  126. /* Reset PLLCFGR register */
  127. RCC->PLLCFGR = 0x24003010;
  128. /* Reset HSEBYP bit */
  129. RCC->CR &= (uint32_t)0xFFFBFFFF;
  130. /* Disable all interrupts */
  131. RCC->CIR = 0x00000000;
  132. #ifdef DATA_IN_ExtSRAM
  133. SystemInit_ExtMemCtl();
  134. #endif /* DATA_IN_ExtSRAM */
  135. /* Configure the Vector Table location add offset address ------------------*/
  136. #ifdef VECT_TAB_SRAM
  137. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  138. #else
  139. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  140. #endif
  141. }
  142. /**
  143. * @brief Update SystemCoreClock variable according to Clock Register Values.
  144. * The SystemCoreClock variable contains the core clock (HCLK), it can
  145. * be used by the user application to setup the SysTick timer or configure
  146. * other parameters.
  147. *
  148. * @note Each time the core clock (HCLK) changes, this function must be called
  149. * to update SystemCoreClock variable value. Otherwise, any configuration
  150. * based on this variable will be incorrect.
  151. *
  152. * @note - The system frequency computed by this function is not the real
  153. * frequency in the chip. It is calculated based on the predefined
  154. * constant and the selected clock source:
  155. *
  156. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  157. *
  158. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  159. *
  160. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  161. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  162. *
  163. * (*) HSI_VALUE is a constant defined in stm32f2xx_hal_conf.h file (default value
  164. * 16 MHz) but the real value may vary depending on the variations
  165. * in voltage and temperature.
  166. *
  167. * (**) HSE_VALUE is a constant defined in stm32f2xx_hal_conf.h file (its value
  168. * depends on the application requirements), user has to ensure that HSE_VALUE
  169. * is same as the real frequency of the crystal used. Otherwise, this function
  170. * may have wrong result.
  171. *
  172. * - The result of this function could be not correct when using fractional
  173. * value for HSE crystal.
  174. *
  175. * @param None
  176. * @retval None
  177. */
  178. void SystemCoreClockUpdate(void)
  179. {
  180. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  181. /* Get SYSCLK source -------------------------------------------------------*/
  182. tmp = RCC->CFGR & RCC_CFGR_SWS;
  183. switch (tmp)
  184. {
  185. case 0x00: /* HSI used as system clock source */
  186. SystemCoreClock = HSI_VALUE;
  187. break;
  188. case 0x04: /* HSE used as system clock source */
  189. SystemCoreClock = HSE_VALUE;
  190. break;
  191. case 0x08: /* PLL used as system clock source */
  192. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  193. SYSCLK = PLL_VCO / PLL_P
  194. */
  195. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  196. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  197. if (pllsource != 0)
  198. {
  199. /* HSE used as PLL clock source */
  200. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  201. }
  202. else
  203. {
  204. /* HSI used as PLL clock source */
  205. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  206. }
  207. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  208. SystemCoreClock = pllvco/pllp;
  209. break;
  210. default:
  211. SystemCoreClock = HSI_VALUE;
  212. break;
  213. }
  214. /* Compute HCLK frequency --------------------------------------------------*/
  215. /* Get HCLK prescaler */
  216. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  217. /* HCLK frequency */
  218. SystemCoreClock >>= tmp;
  219. }
  220. #ifdef DATA_IN_ExtSRAM
  221. /**
  222. * @brief Setup the external memory controller.
  223. * Called in startup_stm32f2xx.s before jump to main.
  224. * This function configures the external SRAM mounted on STM322xG_EVAL board
  225. * This SRAM will be used as program data memory (including heap and stack).
  226. * @param None
  227. * @retval None
  228. */
  229. void SystemInit_ExtMemCtl(void)
  230. {
  231. __IO uint32_t tmp = 0x00;
  232. /*-- GPIOs Configuration -----------------------------------------------------*/
  233. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  234. RCC->AHB1ENR |= 0x00000078;
  235. /* Delay after an RCC peripheral clock enabling */
  236. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
  237. (void)(tmp);
  238. /* Connect PDx pins to FSMC Alternate function */
  239. GPIOD->AFR[0] = 0x00CCC0CC;
  240. GPIOD->AFR[1] = 0xCCCCCCCC;
  241. /* Configure PDx pins in Alternate function mode */
  242. GPIOD->MODER = 0xAAAA0A8A;
  243. /* Configure PDx pins speed to 100 MHz */
  244. GPIOD->OSPEEDR = 0xFFFF0FCF;
  245. /* Configure PDx pins Output type to push-pull */
  246. GPIOD->OTYPER = 0x00000000;
  247. /* No pull-up, pull-down for PDx pins */
  248. GPIOD->PUPDR = 0x00000000;
  249. /* Connect PEx pins to FSMC Alternate function */
  250. GPIOE->AFR[0] = 0xC00CC0CC;
  251. GPIOE->AFR[1] = 0xCCCCCCCC;
  252. /* Configure PEx pins in Alternate function mode */
  253. GPIOE->MODER = 0xAAAA828A;
  254. /* Configure PEx pins speed to 100 MHz */
  255. GPIOE->OSPEEDR = 0xFFFFC3CF;
  256. /* Configure PEx pins Output type to push-pull */
  257. GPIOE->OTYPER = 0x00000000;
  258. /* No pull-up, pull-down for PEx pins */
  259. GPIOE->PUPDR = 0x00000000;
  260. /* Connect PFx pins to FSMC Alternate function */
  261. GPIOF->AFR[0] = 0x00CCCCCC;
  262. GPIOF->AFR[1] = 0xCCCC0000;
  263. /* Configure PFx pins in Alternate function mode */
  264. GPIOF->MODER = 0xAA000AAA;
  265. /* Configure PFx pins speed to 100 MHz */
  266. GPIOF->OSPEEDR = 0xFF000FFF;
  267. /* Configure PFx pins Output type to push-pull */
  268. GPIOF->OTYPER = 0x00000000;
  269. /* No pull-up, pull-down for PFx pins */
  270. GPIOF->PUPDR = 0x00000000;
  271. /* Connect PGx pins to FSMC Alternate function */
  272. GPIOG->AFR[0] = 0x00CCCCCC;
  273. GPIOG->AFR[1] = 0x000000C0;
  274. /* Configure PGx pins in Alternate function mode */
  275. GPIOG->MODER = 0x00085AAA;
  276. /* Configure PGx pins speed to 100 MHz */
  277. GPIOG->OSPEEDR = 0x000CAFFF;
  278. /* Configure PGx pins Output type to push-pull */
  279. GPIOG->OTYPER = 0x00000000;
  280. /* No pull-up, pull-down for PGx pins */
  281. GPIOG->PUPDR = 0x00000000;
  282. /*--FSMC Configuration -------------------------------------------------------*/
  283. /* Enable the FSMC interface clock */
  284. RCC->AHB3ENR |= 0x00000001;
  285. /* Configure and enable Bank1_SRAM2 */
  286. FSMC_Bank1->BTCR[2] = 0x00001011;
  287. FSMC_Bank1->BTCR[3] = 0x00000201;
  288. FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
  289. }
  290. #endif /* DATA_IN_ExtSRAM */
  291. /**
  292. * @}
  293. */
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @}
  299. */
  300. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/