system_stm32f2xx.c 12 KB

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