system_stm32f4xx.c 26 KB

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