system_stm32f2xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 2016 STMicroelectronics</center></h2>
  25. *
  26. * Redistribution and use in source and binary forms, with or without modification,
  27. * are permitted provided that the following conditions are met:
  28. * 1. Redistributions of source code must retain the above copyright notice,
  29. * this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  34. * may be used to endorse or promote products derived from this software
  35. * without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  38. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. ******************************************************************************
  49. */
  50. /** @addtogroup CMSIS
  51. * @{
  52. */
  53. /** @addtogroup stm32f2xx_system
  54. * @{
  55. */
  56. /** @addtogroup STM32F2xx_System_Private_Includes
  57. * @{
  58. */
  59. #include "stm32f2xx.h"
  60. #if !defined (HSE_VALUE)
  61. #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
  62. #endif /* HSE_VALUE */
  63. #if !defined (HSI_VALUE)
  64. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  65. #endif /* HSI_VALUE */
  66. /**
  67. * @}
  68. */
  69. /** @addtogroup STM32F2xx_System_Private_TypesDefinitions
  70. * @{
  71. */
  72. /**
  73. * @}
  74. */
  75. /** @addtogroup STM32F2xx_System_Private_Defines
  76. * @{
  77. */
  78. /************************* Miscellaneous Configuration ************************/
  79. /*!< Uncomment the following line if you need to use external SRAM mounted
  80. on STM322xG_EVAL board as data memory */
  81. /* #define DATA_IN_ExtSRAM */
  82. /*!< Uncomment the following line if you need to relocate your vector Table in
  83. Internal SRAM. */
  84. /* #define VECT_TAB_SRAM */
  85. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  86. This value must be a multiple of 0x200. */
  87. /******************************************************************************/
  88. /**
  89. * @}
  90. */
  91. /** @addtogroup STM32F2xx_System_Private_Macros
  92. * @{
  93. */
  94. /**
  95. * @}
  96. */
  97. /** @addtogroup STM32F2xx_System_Private_Variables
  98. * @{
  99. */
  100. /* This variable can be updated in Three ways :
  101. 1) by calling CMSIS function SystemCoreClockUpdate()
  102. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  103. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  104. Note: If you use this function to configure the system clock; then there
  105. is no need to call the 2 first functions listed above, since SystemCoreClock
  106. variable is updated automatically.
  107. */
  108. uint32_t SystemCoreClock = 16000000;
  109. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  110. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  111. /**
  112. * @}
  113. */
  114. /** @addtogroup STM32F2xx_System_Private_FunctionPrototypes
  115. * @{
  116. */
  117. #ifdef DATA_IN_ExtSRAM
  118. static void SystemInit_ExtMemCtl(void);
  119. #endif /* DATA_IN_ExtSRAM */
  120. /**
  121. * @}
  122. */
  123. /** @addtogroup STM32F2xx_System_Private_Functions
  124. * @{
  125. */
  126. /**
  127. * @brief Setup the microcontroller system
  128. * Initialize the Embedded Flash Interface, the PLL and update the
  129. * SystemFrequency variable.
  130. * @param None
  131. * @retval None
  132. */
  133. void SystemInit(void)
  134. {
  135. /* Reset the RCC clock configuration to the default reset state ------------*/
  136. /* Set HSION bit */
  137. RCC->CR |= (uint32_t)0x00000001;
  138. /* Reset CFGR register */
  139. RCC->CFGR = 0x00000000;
  140. /* Reset HSEON, CSSON and PLLON bits */
  141. RCC->CR &= (uint32_t)0xFEF6FFFF;
  142. /* Reset PLLCFGR register */
  143. RCC->PLLCFGR = 0x24003010;
  144. /* Reset HSEBYP bit */
  145. RCC->CR &= (uint32_t)0xFFFBFFFF;
  146. /* Disable all interrupts */
  147. RCC->CIR = 0x00000000;
  148. #ifdef DATA_IN_ExtSRAM
  149. SystemInit_ExtMemCtl();
  150. #endif /* DATA_IN_ExtSRAM */
  151. /* Configure the Vector Table location add offset address ------------------*/
  152. #ifdef VECT_TAB_SRAM
  153. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  154. #else
  155. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  156. #endif
  157. }
  158. /**
  159. * @brief Update SystemCoreClock variable according to Clock Register Values.
  160. * The SystemCoreClock variable contains the core clock (HCLK), it can
  161. * be used by the user application to setup the SysTick timer or configure
  162. * other parameters.
  163. *
  164. * @note Each time the core clock (HCLK) changes, this function must be called
  165. * to update SystemCoreClock variable value. Otherwise, any configuration
  166. * based on this variable will be incorrect.
  167. *
  168. * @note - The system frequency computed by this function is not the real
  169. * frequency in the chip. It is calculated based on the predefined
  170. * constant and the selected clock source:
  171. *
  172. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  173. *
  174. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  175. *
  176. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  177. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  178. *
  179. * (*) HSI_VALUE is a constant defined in stm32f2xx_hal_conf.h file (default value
  180. * 16 MHz) but the real value may vary depending on the variations
  181. * in voltage and temperature.
  182. *
  183. * (**) HSE_VALUE is a constant defined in stm32f2xx_hal_conf.h file (its value
  184. * depends on the application requirements), user has to ensure that HSE_VALUE
  185. * is same as the real frequency of the crystal used. Otherwise, this function
  186. * may have wrong result.
  187. *
  188. * - The result of this function could be not correct when using fractional
  189. * value for HSE crystal.
  190. *
  191. * @param None
  192. * @retval None
  193. */
  194. void SystemCoreClockUpdate(void)
  195. {
  196. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  197. /* Get SYSCLK source -------------------------------------------------------*/
  198. tmp = RCC->CFGR & RCC_CFGR_SWS;
  199. switch (tmp)
  200. {
  201. case 0x00: /* HSI used as system clock source */
  202. SystemCoreClock = HSI_VALUE;
  203. break;
  204. case 0x04: /* HSE used as system clock source */
  205. SystemCoreClock = HSE_VALUE;
  206. break;
  207. case 0x08: /* PLL used as system clock source */
  208. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  209. SYSCLK = PLL_VCO / PLL_P
  210. */
  211. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  212. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  213. if (pllsource != 0)
  214. {
  215. /* HSE used as PLL clock source */
  216. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  217. }
  218. else
  219. {
  220. /* HSI used as PLL clock source */
  221. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  222. }
  223. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  224. SystemCoreClock = pllvco/pllp;
  225. break;
  226. default:
  227. SystemCoreClock = HSI_VALUE;
  228. break;
  229. }
  230. /* Compute HCLK frequency --------------------------------------------------*/
  231. /* Get HCLK prescaler */
  232. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  233. /* HCLK frequency */
  234. SystemCoreClock >>= tmp;
  235. }
  236. #ifdef DATA_IN_ExtSRAM
  237. /**
  238. * @brief Setup the external memory controller.
  239. * Called in startup_stm32f2xx.s before jump to main.
  240. * This function configures the external SRAM mounted on STM322xG_EVAL board
  241. * This SRAM will be used as program data memory (including heap and stack).
  242. * @param None
  243. * @retval None
  244. */
  245. void SystemInit_ExtMemCtl(void)
  246. {
  247. __IO uint32_t tmp = 0x00;
  248. /*-- GPIOs Configuration -----------------------------------------------------*/
  249. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  250. RCC->AHB1ENR |= 0x00000078;
  251. /* Delay after an RCC peripheral clock enabling */
  252. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
  253. (void)(tmp);
  254. /* Connect PDx pins to FSMC Alternate function */
  255. GPIOD->AFR[0] = 0x00CCC0CC;
  256. GPIOD->AFR[1] = 0xCCCCCCCC;
  257. /* Configure PDx pins in Alternate function mode */
  258. GPIOD->MODER = 0xAAAA0A8A;
  259. /* Configure PDx pins speed to 100 MHz */
  260. GPIOD->OSPEEDR = 0xFFFF0FCF;
  261. /* Configure PDx pins Output type to push-pull */
  262. GPIOD->OTYPER = 0x00000000;
  263. /* No pull-up, pull-down for PDx pins */
  264. GPIOD->PUPDR = 0x00000000;
  265. /* Connect PEx pins to FSMC Alternate function */
  266. GPIOE->AFR[0] = 0xC00CC0CC;
  267. GPIOE->AFR[1] = 0xCCCCCCCC;
  268. /* Configure PEx pins in Alternate function mode */
  269. GPIOE->MODER = 0xAAAA828A;
  270. /* Configure PEx pins speed to 100 MHz */
  271. GPIOE->OSPEEDR = 0xFFFFC3CF;
  272. /* Configure PEx pins Output type to push-pull */
  273. GPIOE->OTYPER = 0x00000000;
  274. /* No pull-up, pull-down for PEx pins */
  275. GPIOE->PUPDR = 0x00000000;
  276. /* Connect PFx pins to FSMC Alternate function */
  277. GPIOF->AFR[0] = 0x00CCCCCC;
  278. GPIOF->AFR[1] = 0xCCCC0000;
  279. /* Configure PFx pins in Alternate function mode */
  280. GPIOF->MODER = 0xAA000AAA;
  281. /* Configure PFx pins speed to 100 MHz */
  282. GPIOF->OSPEEDR = 0xFF000FFF;
  283. /* Configure PFx pins Output type to push-pull */
  284. GPIOF->OTYPER = 0x00000000;
  285. /* No pull-up, pull-down for PFx pins */
  286. GPIOF->PUPDR = 0x00000000;
  287. /* Connect PGx pins to FSMC Alternate function */
  288. GPIOG->AFR[0] = 0x00CCCCCC;
  289. GPIOG->AFR[1] = 0x000000C0;
  290. /* Configure PGx pins in Alternate function mode */
  291. GPIOG->MODER = 0x00085AAA;
  292. /* Configure PGx pins speed to 100 MHz */
  293. GPIOG->OSPEEDR = 0x000CAFFF;
  294. /* Configure PGx pins Output type to push-pull */
  295. GPIOG->OTYPER = 0x00000000;
  296. /* No pull-up, pull-down for PGx pins */
  297. GPIOG->PUPDR = 0x00000000;
  298. /*--FSMC Configuration -------------------------------------------------------*/
  299. /* Enable the FSMC interface clock */
  300. RCC->AHB3ENR |= 0x00000001;
  301. /* Configure and enable Bank1_SRAM2 */
  302. FSMC_Bank1->BTCR[2] = 0x00001011;
  303. FSMC_Bank1->BTCR[3] = 0x00000201;
  304. FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
  305. }
  306. #endif /* DATA_IN_ExtSRAM */
  307. /**
  308. * @}
  309. */
  310. /**
  311. * @}
  312. */
  313. /**
  314. * @}
  315. */
  316. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/