You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

437 line
14 KiB

  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f4xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M4 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_stm32f4xx.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. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; COPYRIGHT 2017 STMicroelectronics </center></h2>
  26. *
  27. * Redistribution and use in source and binary forms, with or without modification,
  28. * are permitted provided that the following conditions are met:
  29. * 1. Redistributions of source code must retain the above copyright notice,
  30. * this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright notice,
  32. * this list of conditions and the following disclaimer in the documentation
  33. * and/or other materials provided with the distribution.
  34. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  44. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  47. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. *
  49. ******************************************************************************
  50. */
  51. /** @addtogroup CMSIS
  52. * @{
  53. */
  54. /** @addtogroup stm32f4xx_system
  55. * @{
  56. */
  57. /** @addtogroup STM32F4xx_System_Private_Includes
  58. * @{
  59. */
  60. #include "stm32f4xx.h"
  61. #if !defined (HSE_VALUE)
  62. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
  63. #endif /* HSE_VALUE */
  64. #if !defined (HSI_VALUE)
  65. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  66. #endif /* HSI_VALUE */
  67. /**
  68. * @}
  69. */
  70. /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
  71. * @{
  72. */
  73. /**
  74. * @}
  75. */
  76. /** @addtogroup STM32F4xx_System_Private_Defines
  77. * @{
  78. */
  79. /************************* Miscellaneous Configuration ************************/
  80. /*!< Uncomment the following line if you need to use external SDRAM mounted
  81. on DK as data memory */
  82. /* #define DATA_IN_ExtSDRAM */
  83. /*!< Uncomment the following line if you need to relocate your vector Table in
  84. Internal SRAM. */
  85. /* #define VECT_TAB_SRAM */
  86. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  87. This value must be a multiple of 0x200. */
  88. /******************************************************************************/
  89. /**
  90. * @}
  91. */
  92. /** @addtogroup STM32F4xx_System_Private_Macros
  93. * @{
  94. */
  95. /**
  96. * @}
  97. */
  98. /** @addtogroup STM32F4xx_System_Private_Variables
  99. * @{
  100. */
  101. /* This variable is updated in three ways:
  102. 1) by calling CMSIS function SystemCoreClockUpdate()
  103. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  104. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  105. Note: If you use this function to configure the system clock; then there
  106. is no need to call the 2 first functions listed above, since SystemCoreClock
  107. variable is updated automatically.
  108. */
  109. uint32_t SystemCoreClock = 16000000;
  110. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  111. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  112. /**
  113. * @}
  114. */
  115. /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
  116. * @{
  117. */
  118. #if defined (DATA_IN_ExtSDRAM)
  119. static void SystemInit_ExtMemCtl(void);
  120. #endif /* DATA_IN_ExtSDRAM */
  121. /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
  122. * @{
  123. */
  124. /**
  125. * @}
  126. */
  127. /** @addtogroup STM32F4xx_System_Private_Functions
  128. * @{
  129. */
  130. /**
  131. * @brief Setup the microcontroller system
  132. * Initialize the FPU setting, vector table location and External memory
  133. * configuration.
  134. * @param None
  135. * @retval None
  136. */
  137. void SystemInit(void)
  138. {
  139. /* FPU settings ------------------------------------------------------------*/
  140. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  141. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  142. #endif
  143. /* Reset the RCC clock configuration to the default reset state ------------*/
  144. /* Set HSION bit */
  145. RCC->CR |= (uint32_t)0x00000001;
  146. /* Reset CFGR register */
  147. RCC->CFGR = 0x00000000;
  148. /* Reset HSEON, CSSON and PLLON bits */
  149. RCC->CR &= (uint32_t)0xFEF6FFFF;
  150. /* Reset PLLCFGR register */
  151. RCC->PLLCFGR = 0x24003010;
  152. /* Reset HSEBYP bit */
  153. RCC->CR &= (uint32_t)0xFFFBFFFF;
  154. /* Disable all interrupts */
  155. RCC->CIR = 0x00000000;
  156. #if defined (DATA_IN_ExtSDRAM)
  157. SystemInit_ExtMemCtl();
  158. #endif /* DATA_IN_ExtSDRAM */
  159. /* Configure the Vector Table location add offset address ------------------*/
  160. #ifdef VECT_TAB_SRAM
  161. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  162. #else
  163. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  164. #endif
  165. }
  166. /**
  167. * @brief Update SystemCoreClock variable according to Clock Register Values.
  168. * The SystemCoreClock variable contains the core clock (HCLK), it can
  169. * be used by the user application to setup the SysTick timer or configure
  170. * other parameters.
  171. *
  172. * @note Each time the core clock (HCLK) changes, this function must be called
  173. * to update SystemCoreClock variable value. Otherwise, any configuration
  174. * based on this variable will be incorrect.
  175. *
  176. * @note - The system frequency computed by this function is not the real
  177. * frequency in the chip. It is calculated based on the predefined
  178. * constant and the selected clock source:
  179. *
  180. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  181. *
  182. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  183. *
  184. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  185. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  186. *
  187. * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  188. * 16 MHz) but the real value may vary depending on the variations
  189. * in voltage and temperature.
  190. *
  191. * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value
  192. * depends on the application requirements), user has to ensure that HSE_VALUE
  193. * is same as the real frequency of the crystal used. Otherwise, this function
  194. * may have wrong result.
  195. *
  196. * - The result of this function could be not correct when using fractional
  197. * value for HSE crystal.
  198. *
  199. * @param None
  200. * @retval None
  201. */
  202. void SystemCoreClockUpdate(void)
  203. {
  204. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  205. /* Get SYSCLK source -------------------------------------------------------*/
  206. tmp = RCC->CFGR & RCC_CFGR_SWS;
  207. switch (tmp)
  208. {
  209. case 0x00: /* HSI used as system clock source */
  210. SystemCoreClock = HSI_VALUE;
  211. break;
  212. case 0x04: /* HSE used as system clock source */
  213. SystemCoreClock = HSE_VALUE;
  214. break;
  215. case 0x08: /* PLL used as system clock source */
  216. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  217. SYSCLK = PLL_VCO / PLL_P
  218. */
  219. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  220. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  221. if (pllsource != 0)
  222. {
  223. /* HSE used as PLL clock source */
  224. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  225. }
  226. else
  227. {
  228. /* HSI used as PLL clock source */
  229. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  230. }
  231. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  232. SystemCoreClock = pllvco/pllp;
  233. break;
  234. default:
  235. SystemCoreClock = HSI_VALUE;
  236. break;
  237. }
  238. /* Compute HCLK frequency --------------------------------------------------*/
  239. /* Get HCLK prescaler */
  240. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  241. /* HCLK frequency */
  242. SystemCoreClock >>= tmp;
  243. }
  244. #if defined (DATA_IN_ExtSDRAM)
  245. /**
  246. * @brief Setup the external memory controller.
  247. * Called in startup_stm32f4xx.s before jump to main.
  248. * This function configures the external memories (SDRAM)
  249. * This SDRAM will be used as program data memory (including heap and stack).
  250. * @param None
  251. * @retval None
  252. */
  253. void SystemInit_ExtMemCtl(void)
  254. {
  255. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  256. register __IO uint32_t index;
  257. /* Enable GPIOB, GPIOC, GPIOD, GPIOE, GPIOF and GPIOG interface
  258. clock */
  259. RCC->AHB1ENR |= 0x0000007E;
  260. /* Connect PBx pins to FMC Alternate function */
  261. GPIOB->AFR[0] = 0x0CC00000;
  262. GPIOB->AFR[1] = 0x00000000;
  263. /* Configure PBx pins in Alternate function mode */
  264. GPIOB->MODER = 0x00002A80;
  265. /* Configure PBx pins speed to 100 MHz */
  266. GPIOB->OSPEEDR = 0x00003CC0;
  267. /* Configure PBx pins Output type to push-pull */
  268. GPIOB->OTYPER = 0x00000000;
  269. /* No pull-up, pull-down for PBx pins */
  270. GPIOB->PUPDR = 0x00000100;
  271. /* Connect PCx pins to FMC Alternate function */
  272. GPIOC->AFR[0] = 0x0000000C;
  273. GPIOC->AFR[1] = 0x00000000;
  274. /* Configure PCx pins in Alternate function mode */
  275. GPIOC->MODER = 0x00000002;
  276. /* Configure PCx pins speed to 100 MHz */
  277. GPIOC->OSPEEDR = 0x00000003;
  278. /* Configure PCx pins Output type to push-pull */
  279. GPIOC->OTYPER = 0x00000000;
  280. /* No pull-up, pull-down for PCx pins */
  281. GPIOC->PUPDR = 0x00000000;
  282. /* Connect PDx pins to FMC Alternate function */
  283. GPIOD->AFR[0] = 0x000000CC;
  284. GPIOD->AFR[1] = 0xCC000CCC;
  285. /* Configure PDx pins in Alternate function mode */
  286. GPIOD->MODER = 0xA02A000A;
  287. /* Configure PDx pins speed to 100 MHz */
  288. GPIOD->OSPEEDR = 0xF03F000F;
  289. /* Configure PDx pins Output type to push-pull */
  290. GPIOD->OTYPER = 0x00000000;
  291. /* No pull-up, pull-down for PDx pins */
  292. GPIOD->PUPDR = 0x00000000;
  293. /* Connect PEx pins to FMC Alternate function */
  294. GPIOE->AFR[0] = 0xC00000CC;
  295. GPIOE->AFR[1] = 0xCCCCCCCC;
  296. /* Configure PEx pins in Alternate function mode */
  297. GPIOE->MODER = 0xAAAA800A;
  298. /* Configure PEx pins speed to 100 MHz */
  299. GPIOE->OSPEEDR = 0xFFFFC00F;
  300. /* Configure PEx pins Output type to push-pull */
  301. GPIOE->OTYPER = 0x00000000;
  302. /* No pull-up, pull-down for PEx pins */
  303. GPIOE->PUPDR = 0x00000000;
  304. /* Connect PFx pins to FMC Alternate function */
  305. GPIOF->AFR[0] = 0x00CCCCCC;
  306. GPIOF->AFR[1] = 0xCCCCC000;
  307. /* Configure PFx pins in Alternate function mode */
  308. GPIOF->MODER = 0xAA800AAA;
  309. /* Configure PFx pins speed to 100 MHz */
  310. GPIOF->OSPEEDR = 0xFFC00FFF;
  311. /* Configure PFx pins Output type to push-pull */
  312. GPIOF->OTYPER = 0x00000000;
  313. /* No pull-up, pull-down for PFx pins */
  314. GPIOF->PUPDR = 0x00000000;
  315. /* Connect PGx pins to FMC Alternate function */
  316. GPIOG->AFR[0] = 0x00CC00CC;
  317. GPIOG->AFR[1] = 0xC000000C;
  318. /* Configure PGx pins in Alternate function mode */
  319. GPIOG->MODER = 0x80020A0A;
  320. /* Configure PGx pins speed to 100 MHz */
  321. GPIOG->OSPEEDR = 0xC0030F0F;
  322. /* Configure PGx pins Output type to push-pull */
  323. GPIOG->OTYPER = 0x00000000;
  324. /* No pull-up, pull-down for PGx pins */
  325. GPIOG->PUPDR = 0x00000000;
  326. /* FMC Configuration */
  327. /* Enable the FMC interface clock */
  328. RCC->AHB3ENR |= 0x00000001;
  329. /* Configure and enable SDRAM bank2 */
  330. FMC_Bank5_6->SDCR[0] = 0x00002ED0;
  331. FMC_Bank5_6->SDCR[1] = 0x000001D4;
  332. FMC_Bank5_6->SDTR[0] = 0x0F1F6FFF;
  333. FMC_Bank5_6->SDTR[1] = 0x01010361;
  334. /* SDRAM initialization sequence */
  335. /* Clock enable command */
  336. FMC_Bank5_6->SDCMR = 0x00000009;
  337. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  338. while((tmpreg != 0) && (timeout-- > 0))
  339. {
  340. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  341. }
  342. /* Delay */
  343. for (index = 0; index<1000; index++);
  344. /* PALL command */
  345. FMC_Bank5_6->SDCMR = 0x0000000A;
  346. timeout = 0xFFFF;
  347. while((tmpreg != 0) && (timeout-- > 0))
  348. {
  349. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  350. }
  351. /* Auto refresh command */
  352. FMC_Bank5_6->SDCMR = 0x0000006B;
  353. timeout = 0xFFFF;
  354. while((tmpreg != 0) && (timeout-- > 0))
  355. {
  356. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  357. }
  358. /* MRD register program */
  359. FMC_Bank5_6->SDCMR = 0x0004620C;
  360. timeout = 0xFFFF;
  361. while((tmpreg != 0) && (timeout-- > 0))
  362. {
  363. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  364. }
  365. /* Set refresh count */
  366. tmpreg = FMC_Bank5_6->SDRTR;
  367. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000056A<<1));
  368. /* Disable write protection */
  369. tmpreg = FMC_Bank5_6->SDCR[1];
  370. FMC_Bank5_6->SDCR[1] = (tmpreg & 0xFFFFFDFF);
  371. }
  372. #endif /* DATA_IN_ExtSDRAM */
  373. /**
  374. * @}
  375. */
  376. /**
  377. * @}
  378. */
  379. /**
  380. * @}
  381. */
  382. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/