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.

system_stm32f4xx.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 (c) 2017 STMicroelectronics.
  26. * All rights reserved.</center></h2>
  27. *
  28. * This software component is licensed by ST under BSD 3-Clause license,
  29. * the "License"; You may not use this file except in compliance with the
  30. * License. You may obtain a copy of the License at:
  31. * opensource.org/licenses/BSD-3-Clause
  32. *
  33. ******************************************************************************
  34. */
  35. /** @addtogroup CMSIS
  36. * @{
  37. */
  38. /** @addtogroup stm32f4xx_system
  39. * @{
  40. */
  41. /** @addtogroup STM32F4xx_System_Private_Includes
  42. * @{
  43. */
  44. #include "stm32f4xx.h"
  45. #if !defined (HSE_VALUE)
  46. #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
  47. #endif /* HSE_VALUE */
  48. #if !defined (HSI_VALUE)
  49. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  50. #endif /* HSI_VALUE */
  51. /**
  52. * @}
  53. */
  54. /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
  55. * @{
  56. */
  57. /**
  58. * @}
  59. */
  60. /** @addtogroup STM32F4xx_System_Private_Defines
  61. * @{
  62. */
  63. /************************* Miscellaneous Configuration ************************/
  64. /*!< Uncomment the following line if you need to use external SRAM or SDRAM as data memory */
  65. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
  66. || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  67. || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
  68. /* #define DATA_IN_ExtSRAM */
  69. #endif /* STM32F40xxx || STM32F41xxx || STM32F42xxx || STM32F43xxx || STM32F469xx || STM32F479xx ||\
  70. STM32F412Zx || STM32F412Vx */
  71. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  72. || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  73. /* #define DATA_IN_ExtSDRAM */
  74. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
  75. STM32F479xx */
  76. /*!< Uncomment the following line if you need to relocate your vector Table in
  77. Internal SRAM. */
  78. /* #define VECT_TAB_SRAM */
  79. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  80. This value must be a multiple of 0x200. */
  81. /******************************************************************************/
  82. /**
  83. * @}
  84. */
  85. /** @addtogroup STM32F4xx_System_Private_Macros
  86. * @{
  87. */
  88. /**
  89. * @}
  90. */
  91. /** @addtogroup STM32F4xx_System_Private_Variables
  92. * @{
  93. */
  94. /* This variable is updated in three ways:
  95. 1) by calling CMSIS function SystemCoreClockUpdate()
  96. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  97. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  98. Note: If you use this function to configure the system clock; then there
  99. is no need to call the 2 first functions listed above, since SystemCoreClock
  100. variable is updated automatically.
  101. */
  102. uint32_t SystemCoreClock = 16000000;
  103. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  104. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  105. /**
  106. * @}
  107. */
  108. /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
  109. * @{
  110. */
  111. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  112. static void SystemInit_ExtMemCtl(void);
  113. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  114. /**
  115. * @}
  116. */
  117. /** @addtogroup STM32F4xx_System_Private_Functions
  118. * @{
  119. */
  120. /**
  121. * @brief Setup the microcontroller system
  122. * Initialize the FPU setting, vector table location and External memory
  123. * configuration.
  124. * @param None
  125. * @retval None
  126. */
  127. void SystemInit(void)
  128. {
  129. /* FPU settings ------------------------------------------------------------*/
  130. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  131. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  132. #endif
  133. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  134. SystemInit_ExtMemCtl();
  135. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  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 stm32f4xx_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 stm32f4xx_hal_conf.h file (its value
  169. * depends on the application requirements), user has to ensure that HSE_VALUE
  170. * is same as the real frequency of the crystal used. Otherwise, this function
  171. * may 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. #if defined (DATA_IN_ExtSRAM) && defined (DATA_IN_ExtSDRAM)
  222. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  223. || defined(STM32F469xx) || defined(STM32F479xx)
  224. /**
  225. * @brief Setup the external memory controller.
  226. * Called in startup_stm32f4xx.s before jump to main.
  227. * This function configures the external memories (SRAM/SDRAM)
  228. * This SRAM/SDRAM will be used as program data memory (including heap and stack).
  229. * @param None
  230. * @retval None
  231. */
  232. void SystemInit_ExtMemCtl(void)
  233. {
  234. __IO uint32_t tmp = 0x00;
  235. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  236. register __IO uint32_t index;
  237. /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */
  238. RCC->AHB1ENR |= 0x000001F8;
  239. /* Delay after an RCC peripheral clock enabling */
  240. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
  241. /* Connect PDx pins to FMC Alternate function */
  242. GPIOD->AFR[0] = 0x00CCC0CC;
  243. GPIOD->AFR[1] = 0xCCCCCCCC;
  244. /* Configure PDx pins in Alternate function mode */
  245. GPIOD->MODER = 0xAAAA0A8A;
  246. /* Configure PDx pins speed to 100 MHz */
  247. GPIOD->OSPEEDR = 0xFFFF0FCF;
  248. /* Configure PDx pins Output type to push-pull */
  249. GPIOD->OTYPER = 0x00000000;
  250. /* No pull-up, pull-down for PDx pins */
  251. GPIOD->PUPDR = 0x00000000;
  252. /* Connect PEx pins to FMC Alternate function */
  253. GPIOE->AFR[0] = 0xC00CC0CC;
  254. GPIOE->AFR[1] = 0xCCCCCCCC;
  255. /* Configure PEx pins in Alternate function mode */
  256. GPIOE->MODER = 0xAAAA828A;
  257. /* Configure PEx pins speed to 100 MHz */
  258. GPIOE->OSPEEDR = 0xFFFFC3CF;
  259. /* Configure PEx pins Output type to push-pull */
  260. GPIOE->OTYPER = 0x00000000;
  261. /* No pull-up, pull-down for PEx pins */
  262. GPIOE->PUPDR = 0x00000000;
  263. /* Connect PFx pins to FMC Alternate function */
  264. GPIOF->AFR[0] = 0xCCCCCCCC;
  265. GPIOF->AFR[1] = 0xCCCCCCCC;
  266. /* Configure PFx pins in Alternate function mode */
  267. GPIOF->MODER = 0xAA800AAA;
  268. /* Configure PFx pins speed to 50 MHz */
  269. GPIOF->OSPEEDR = 0xAA800AAA;
  270. /* Configure PFx pins Output type to push-pull */
  271. GPIOF->OTYPER = 0x00000000;
  272. /* No pull-up, pull-down for PFx pins */
  273. GPIOF->PUPDR = 0x00000000;
  274. /* Connect PGx pins to FMC Alternate function */
  275. GPIOG->AFR[0] = 0xCCCCCCCC;
  276. GPIOG->AFR[1] = 0xCCCCCCCC;
  277. /* Configure PGx pins in Alternate function mode */
  278. GPIOG->MODER = 0xAAAAAAAA;
  279. /* Configure PGx pins speed to 50 MHz */
  280. GPIOG->OSPEEDR = 0xAAAAAAAA;
  281. /* Configure PGx pins Output type to push-pull */
  282. GPIOG->OTYPER = 0x00000000;
  283. /* No pull-up, pull-down for PGx pins */
  284. GPIOG->PUPDR = 0x00000000;
  285. /* Connect PHx pins to FMC Alternate function */
  286. GPIOH->AFR[0] = 0x00C0CC00;
  287. GPIOH->AFR[1] = 0xCCCCCCCC;
  288. /* Configure PHx pins in Alternate function mode */
  289. GPIOH->MODER = 0xAAAA08A0;
  290. /* Configure PHx pins speed to 50 MHz */
  291. GPIOH->OSPEEDR = 0xAAAA08A0;
  292. /* Configure PHx pins Output type to push-pull */
  293. GPIOH->OTYPER = 0x00000000;
  294. /* No pull-up, pull-down for PHx pins */
  295. GPIOH->PUPDR = 0x00000000;
  296. /* Connect PIx pins to FMC Alternate function */
  297. GPIOI->AFR[0] = 0xCCCCCCCC;
  298. GPIOI->AFR[1] = 0x00000CC0;
  299. /* Configure PIx pins in Alternate function mode */
  300. GPIOI->MODER = 0x0028AAAA;
  301. /* Configure PIx pins speed to 50 MHz */
  302. GPIOI->OSPEEDR = 0x0028AAAA;
  303. /* Configure PIx pins Output type to push-pull */
  304. GPIOI->OTYPER = 0x00000000;
  305. /* No pull-up, pull-down for PIx pins */
  306. GPIOI->PUPDR = 0x00000000;
  307. /*-- FMC Configuration -------------------------------------------------------*/
  308. /* Enable the FMC interface clock */
  309. RCC->AHB3ENR |= 0x00000001;
  310. /* Delay after an RCC peripheral clock enabling */
  311. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  312. FMC_Bank5_6->SDCR[0] = 0x000019E4;
  313. FMC_Bank5_6->SDTR[0] = 0x01115351;
  314. /* SDRAM initialization sequence */
  315. /* Clock enable command */
  316. FMC_Bank5_6->SDCMR = 0x00000011;
  317. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  318. while((tmpreg != 0) && (timeout-- > 0))
  319. {
  320. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  321. }
  322. /* Delay */
  323. for (index = 0; index<1000; index++);
  324. /* PALL command */
  325. FMC_Bank5_6->SDCMR = 0x00000012;
  326. timeout = 0xFFFF;
  327. while((tmpreg != 0) && (timeout-- > 0))
  328. {
  329. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  330. }
  331. /* Auto refresh command */
  332. FMC_Bank5_6->SDCMR = 0x00000073;
  333. timeout = 0xFFFF;
  334. while((tmpreg != 0) && (timeout-- > 0))
  335. {
  336. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  337. }
  338. /* MRD register program */
  339. FMC_Bank5_6->SDCMR = 0x00046014;
  340. timeout = 0xFFFF;
  341. while((tmpreg != 0) && (timeout-- > 0))
  342. {
  343. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  344. }
  345. /* Set refresh count */
  346. tmpreg = FMC_Bank5_6->SDRTR;
  347. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  348. /* Disable write protection */
  349. tmpreg = FMC_Bank5_6->SDCR[0];
  350. FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  351. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  352. /* Configure and enable Bank1_SRAM2 */
  353. FMC_Bank1->BTCR[2] = 0x00001011;
  354. FMC_Bank1->BTCR[3] = 0x00000201;
  355. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  356. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  357. #if defined(STM32F469xx) || defined(STM32F479xx)
  358. /* Configure and enable Bank1_SRAM2 */
  359. FMC_Bank1->BTCR[2] = 0x00001091;
  360. FMC_Bank1->BTCR[3] = 0x00110212;
  361. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  362. #endif /* STM32F469xx || STM32F479xx */
  363. (void)(tmp);
  364. }
  365. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  366. #elif defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  367. /**
  368. * @brief Setup the external memory controller.
  369. * Called in startup_stm32f4xx.s before jump to main.
  370. * This function configures the external memories (SRAM/SDRAM)
  371. * This SRAM/SDRAM will be used as program data memory (including heap and stack).
  372. * @param None
  373. * @retval None
  374. */
  375. void SystemInit_ExtMemCtl(void)
  376. {
  377. __IO uint32_t tmp = 0x00;
  378. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  379. || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  380. #if defined (DATA_IN_ExtSDRAM)
  381. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  382. register __IO uint32_t index;
  383. #if defined(STM32F446xx)
  384. /* Enable GPIOA, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG interface
  385. clock */
  386. RCC->AHB1ENR |= 0x0000007D;
  387. #else
  388. /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
  389. clock */
  390. RCC->AHB1ENR |= 0x000001F8;
  391. #endif /* STM32F446xx */
  392. /* Delay after an RCC peripheral clock enabling */
  393. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
  394. #if defined(STM32F446xx)
  395. /* Connect PAx pins to FMC Alternate function */
  396. GPIOA->AFR[0] |= 0xC0000000;
  397. GPIOA->AFR[1] |= 0x00000000;
  398. /* Configure PDx pins in Alternate function mode */
  399. GPIOA->MODER |= 0x00008000;
  400. /* Configure PDx pins speed to 50 MHz */
  401. GPIOA->OSPEEDR |= 0x00008000;
  402. /* Configure PDx pins Output type to push-pull */
  403. GPIOA->OTYPER |= 0x00000000;
  404. /* No pull-up, pull-down for PDx pins */
  405. GPIOA->PUPDR |= 0x00000000;
  406. /* Connect PCx pins to FMC Alternate function */
  407. GPIOC->AFR[0] |= 0x00CC0000;
  408. GPIOC->AFR[1] |= 0x00000000;
  409. /* Configure PDx pins in Alternate function mode */
  410. GPIOC->MODER |= 0x00000A00;
  411. /* Configure PDx pins speed to 50 MHz */
  412. GPIOC->OSPEEDR |= 0x00000A00;
  413. /* Configure PDx pins Output type to push-pull */
  414. GPIOC->OTYPER |= 0x00000000;
  415. /* No pull-up, pull-down for PDx pins */
  416. GPIOC->PUPDR |= 0x00000000;
  417. #endif /* STM32F446xx */
  418. /* Connect PDx pins to FMC Alternate function */
  419. GPIOD->AFR[0] = 0x000000CC;
  420. GPIOD->AFR[1] = 0xCC000CCC;
  421. /* Configure PDx pins in Alternate function mode */
  422. GPIOD->MODER = 0xA02A000A;
  423. /* Configure PDx pins speed to 50 MHz */
  424. GPIOD->OSPEEDR = 0xA02A000A;
  425. /* Configure PDx pins Output type to push-pull */
  426. GPIOD->OTYPER = 0x00000000;
  427. /* No pull-up, pull-down for PDx pins */
  428. GPIOD->PUPDR = 0x00000000;
  429. /* Connect PEx pins to FMC Alternate function */
  430. GPIOE->AFR[0] = 0xC00000CC;
  431. GPIOE->AFR[1] = 0xCCCCCCCC;
  432. /* Configure PEx pins in Alternate function mode */
  433. GPIOE->MODER = 0xAAAA800A;
  434. /* Configure PEx pins speed to 50 MHz */
  435. GPIOE->OSPEEDR = 0xAAAA800A;
  436. /* Configure PEx pins Output type to push-pull */
  437. GPIOE->OTYPER = 0x00000000;
  438. /* No pull-up, pull-down for PEx pins */
  439. GPIOE->PUPDR = 0x00000000;
  440. /* Connect PFx pins to FMC Alternate function */
  441. GPIOF->AFR[0] = 0xCCCCCCCC;
  442. GPIOF->AFR[1] = 0xCCCCCCCC;
  443. /* Configure PFx pins in Alternate function mode */
  444. GPIOF->MODER = 0xAA800AAA;
  445. /* Configure PFx pins speed to 50 MHz */
  446. GPIOF->OSPEEDR = 0xAA800AAA;
  447. /* Configure PFx pins Output type to push-pull */
  448. GPIOF->OTYPER = 0x00000000;
  449. /* No pull-up, pull-down for PFx pins */
  450. GPIOF->PUPDR = 0x00000000;
  451. /* Connect PGx pins to FMC Alternate function */
  452. GPIOG->AFR[0] = 0xCCCCCCCC;
  453. GPIOG->AFR[1] = 0xCCCCCCCC;
  454. /* Configure PGx pins in Alternate function mode */
  455. GPIOG->MODER = 0xAAAAAAAA;
  456. /* Configure PGx pins speed to 50 MHz */
  457. GPIOG->OSPEEDR = 0xAAAAAAAA;
  458. /* Configure PGx pins Output type to push-pull */
  459. GPIOG->OTYPER = 0x00000000;
  460. /* No pull-up, pull-down for PGx pins */
  461. GPIOG->PUPDR = 0x00000000;
  462. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  463. || defined(STM32F469xx) || defined(STM32F479xx)
  464. /* Connect PHx pins to FMC Alternate function */
  465. GPIOH->AFR[0] = 0x00C0CC00;
  466. GPIOH->AFR[1] = 0xCCCCCCCC;
  467. /* Configure PHx pins in Alternate function mode */
  468. GPIOH->MODER = 0xAAAA08A0;
  469. /* Configure PHx pins speed to 50 MHz */
  470. GPIOH->OSPEEDR = 0xAAAA08A0;
  471. /* Configure PHx pins Output type to push-pull */
  472. GPIOH->OTYPER = 0x00000000;
  473. /* No pull-up, pull-down for PHx pins */
  474. GPIOH->PUPDR = 0x00000000;
  475. /* Connect PIx pins to FMC Alternate function */
  476. GPIOI->AFR[0] = 0xCCCCCCCC;
  477. GPIOI->AFR[1] = 0x00000CC0;
  478. /* Configure PIx pins in Alternate function mode */
  479. GPIOI->MODER = 0x0028AAAA;
  480. /* Configure PIx pins speed to 50 MHz */
  481. GPIOI->OSPEEDR = 0x0028AAAA;
  482. /* Configure PIx pins Output type to push-pull */
  483. GPIOI->OTYPER = 0x00000000;
  484. /* No pull-up, pull-down for PIx pins */
  485. GPIOI->PUPDR = 0x00000000;
  486. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  487. /*-- FMC Configuration -------------------------------------------------------*/
  488. /* Enable the FMC interface clock */
  489. RCC->AHB3ENR |= 0x00000001;
  490. /* Delay after an RCC peripheral clock enabling */
  491. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  492. /* Configure and enable SDRAM bank1 */
  493. #if defined(STM32F446xx)
  494. FMC_Bank5_6->SDCR[0] = 0x00001954;
  495. #else
  496. FMC_Bank5_6->SDCR[0] = 0x000019E4;
  497. #endif /* STM32F446xx */
  498. FMC_Bank5_6->SDTR[0] = 0x01115351;
  499. /* SDRAM initialization sequence */
  500. /* Clock enable command */
  501. FMC_Bank5_6->SDCMR = 0x00000011;
  502. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  503. while((tmpreg != 0) && (timeout-- > 0))
  504. {
  505. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  506. }
  507. /* Delay */
  508. for (index = 0; index<1000; index++);
  509. /* PALL command */
  510. FMC_Bank5_6->SDCMR = 0x00000012;
  511. timeout = 0xFFFF;
  512. while((tmpreg != 0) && (timeout-- > 0))
  513. {
  514. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  515. }
  516. /* Auto refresh command */
  517. #if defined(STM32F446xx)
  518. FMC_Bank5_6->SDCMR = 0x000000F3;
  519. #else
  520. FMC_Bank5_6->SDCMR = 0x00000073;
  521. #endif /* STM32F446xx */
  522. timeout = 0xFFFF;
  523. while((tmpreg != 0) && (timeout-- > 0))
  524. {
  525. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  526. }
  527. /* MRD register program */
  528. #if defined(STM32F446xx)
  529. FMC_Bank5_6->SDCMR = 0x00044014;
  530. #else
  531. FMC_Bank5_6->SDCMR = 0x00046014;
  532. #endif /* STM32F446xx */
  533. timeout = 0xFFFF;
  534. while((tmpreg != 0) && (timeout-- > 0))
  535. {
  536. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  537. }
  538. /* Set refresh count */
  539. tmpreg = FMC_Bank5_6->SDRTR;
  540. #if defined(STM32F446xx)
  541. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C<<1));
  542. #else
  543. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  544. #endif /* STM32F446xx */
  545. /* Disable write protection */
  546. tmpreg = FMC_Bank5_6->SDCR[0];
  547. FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  548. #endif /* DATA_IN_ExtSDRAM */
  549. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
  550. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
  551. || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  552. || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
  553. #if defined(DATA_IN_ExtSRAM)
  554. /*-- GPIOs Configuration -----------------------------------------------------*/
  555. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  556. RCC->AHB1ENR |= 0x00000078;
  557. /* Delay after an RCC peripheral clock enabling */
  558. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
  559. /* Connect PDx pins to FMC Alternate function */
  560. GPIOD->AFR[0] = 0x00CCC0CC;
  561. GPIOD->AFR[1] = 0xCCCCCCCC;
  562. /* Configure PDx pins in Alternate function mode */
  563. GPIOD->MODER = 0xAAAA0A8A;
  564. /* Configure PDx pins speed to 100 MHz */
  565. GPIOD->OSPEEDR = 0xFFFF0FCF;
  566. /* Configure PDx pins Output type to push-pull */
  567. GPIOD->OTYPER = 0x00000000;
  568. /* No pull-up, pull-down for PDx pins */
  569. GPIOD->PUPDR = 0x00000000;
  570. /* Connect PEx pins to FMC Alternate function */
  571. GPIOE->AFR[0] = 0xC00CC0CC;
  572. GPIOE->AFR[1] = 0xCCCCCCCC;
  573. /* Configure PEx pins in Alternate function mode */
  574. GPIOE->MODER = 0xAAAA828A;
  575. /* Configure PEx pins speed to 100 MHz */
  576. GPIOE->OSPEEDR = 0xFFFFC3CF;
  577. /* Configure PEx pins Output type to push-pull */
  578. GPIOE->OTYPER = 0x00000000;
  579. /* No pull-up, pull-down for PEx pins */
  580. GPIOE->PUPDR = 0x00000000;
  581. /* Connect PFx pins to FMC Alternate function */
  582. GPIOF->AFR[0] = 0x00CCCCCC;
  583. GPIOF->AFR[1] = 0xCCCC0000;
  584. /* Configure PFx pins in Alternate function mode */
  585. GPIOF->MODER = 0xAA000AAA;
  586. /* Configure PFx pins speed to 100 MHz */
  587. GPIOF->OSPEEDR = 0xFF000FFF;
  588. /* Configure PFx pins Output type to push-pull */
  589. GPIOF->OTYPER = 0x00000000;
  590. /* No pull-up, pull-down for PFx pins */
  591. GPIOF->PUPDR = 0x00000000;
  592. /* Connect PGx pins to FMC Alternate function */
  593. GPIOG->AFR[0] = 0x00CCCCCC;
  594. GPIOG->AFR[1] = 0x000000C0;
  595. /* Configure PGx pins in Alternate function mode */
  596. GPIOG->MODER = 0x00085AAA;
  597. /* Configure PGx pins speed to 100 MHz */
  598. GPIOG->OSPEEDR = 0x000CAFFF;
  599. /* Configure PGx pins Output type to push-pull */
  600. GPIOG->OTYPER = 0x00000000;
  601. /* No pull-up, pull-down for PGx pins */
  602. GPIOG->PUPDR = 0x00000000;
  603. /*-- FMC/FSMC Configuration --------------------------------------------------*/
  604. /* Enable the FMC/FSMC interface clock */
  605. RCC->AHB3ENR |= 0x00000001;
  606. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  607. /* Delay after an RCC peripheral clock enabling */
  608. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  609. /* Configure and enable Bank1_SRAM2 */
  610. FMC_Bank1->BTCR[2] = 0x00001011;
  611. FMC_Bank1->BTCR[3] = 0x00000201;
  612. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  613. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  614. #if defined(STM32F469xx) || defined(STM32F479xx)
  615. /* Delay after an RCC peripheral clock enabling */
  616. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  617. /* Configure and enable Bank1_SRAM2 */
  618. FMC_Bank1->BTCR[2] = 0x00001091;
  619. FMC_Bank1->BTCR[3] = 0x00110212;
  620. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  621. #endif /* STM32F469xx || STM32F479xx */
  622. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)\
  623. || defined(STM32F412Zx) || defined(STM32F412Vx)
  624. /* Delay after an RCC peripheral clock enabling */
  625. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FSMCEN);
  626. /* Configure and enable Bank1_SRAM2 */
  627. FSMC_Bank1->BTCR[2] = 0x00001011;
  628. FSMC_Bank1->BTCR[3] = 0x00000201;
  629. FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
  630. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F412Zx || STM32F412Vx */
  631. #endif /* DATA_IN_ExtSRAM */
  632. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  633. STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */
  634. (void)(tmp);
  635. }
  636. #endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */
  637. /**
  638. * @}
  639. */
  640. /**
  641. * @}
  642. */
  643. /**
  644. * @}
  645. */
  646. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/