system_stm32f4xx.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f4xx.c
  4. * @author MCD Application Team
  5. * @version V2.6.1
  6. * @date 14-February-2017
  7. * @brief CMSIS Cortex-M4 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_stm32f4xx.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. ******************************************************************************
  25. * @attention
  26. *
  27. * <h2><center>&copy; COPYRIGHT 2017 STMicroelectronics</center></h2>
  28. *
  29. * Redistribution and use in source and binary forms, with or without modification,
  30. * are permitted provided that the following conditions are met:
  31. * 1. Redistributions of source code must retain the above copyright notice,
  32. * this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright notice,
  34. * this list of conditions and the following disclaimer in the documentation
  35. * and/or other materials provided with the distribution.
  36. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  37. * may be used to endorse or promote products derived from this software
  38. * without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  41. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  46. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  47. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  48. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  49. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. *
  51. ******************************************************************************
  52. */
  53. /** @addtogroup CMSIS
  54. * @{
  55. */
  56. /** @addtogroup stm32f4xx_system
  57. * @{
  58. */
  59. /** @addtogroup STM32F4xx_System_Private_Includes
  60. * @{
  61. */
  62. #include "stm32f4xx.h"
  63. #if !defined (HSE_VALUE)
  64. #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
  65. #endif /* HSE_VALUE */
  66. #if !defined (HSI_VALUE)
  67. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  68. #endif /* HSI_VALUE */
  69. /**
  70. * @}
  71. */
  72. /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
  73. * @{
  74. */
  75. /**
  76. * @}
  77. */
  78. /** @addtogroup STM32F4xx_System_Private_Defines
  79. * @{
  80. */
  81. /************************* Miscellaneous Configuration ************************/
  82. /*!< Uncomment the following line if you need to use external SRAM or SDRAM as data memory */
  83. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
  84. || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  85. || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
  86. /* #define DATA_IN_ExtSRAM */
  87. #endif /* STM32F40xxx || STM32F41xxx || STM32F42xxx || STM32F43xxx || STM32F469xx || STM32F479xx ||\
  88. STM32F412Zx || STM32F412Vx */
  89. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  90. || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  91. /* #define DATA_IN_ExtSDRAM */
  92. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
  93. STM32F479xx */
  94. /*!< Uncomment the following line if you need to relocate your vector Table in
  95. Internal SRAM. */
  96. /* #define VECT_TAB_SRAM */
  97. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  98. This value must be a multiple of 0x200. */
  99. /******************************************************************************/
  100. /**
  101. * @}
  102. */
  103. /** @addtogroup STM32F4xx_System_Private_Macros
  104. * @{
  105. */
  106. /**
  107. * @}
  108. */
  109. /** @addtogroup STM32F4xx_System_Private_Variables
  110. * @{
  111. */
  112. /* This variable is updated in three ways:
  113. 1) by calling CMSIS function SystemCoreClockUpdate()
  114. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  115. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  116. Note: If you use this function to configure the system clock; then there
  117. is no need to call the 2 first functions listed above, since SystemCoreClock
  118. variable is updated automatically.
  119. */
  120. uint32_t SystemCoreClock = 16000000;
  121. const uint8_t AHBPrescTable[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8,
  122. 9 };
  123. const uint8_t APBPrescTable[8] = { 0, 0, 0, 0, 1, 2, 3, 4 };
  124. /**
  125. * @}
  126. */
  127. /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
  128. * @{
  129. */
  130. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  131. static void SystemInit_ExtMemCtl(void);
  132. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  133. /**
  134. * @}
  135. */
  136. /** @addtogroup STM32F4xx_System_Private_Functions
  137. * @{
  138. */
  139. /**
  140. * @brief Setup the microcontroller system
  141. * Initialize the FPU setting, vector table location and External memory
  142. * configuration.
  143. * @param None
  144. * @retval None
  145. */
  146. void SystemInit(void) {
  147. /* FPU settings ------------------------------------------------------------*/
  148. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  149. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  150. #endif
  151. /* Reset the RCC clock configuration to the default reset state ------------*/
  152. /* Set HSION bit */
  153. RCC->CR |= (uint32_t) 0x00000001;
  154. /* Reset CFGR register */
  155. RCC->CFGR = 0x00000000;
  156. /* Reset HSEON, CSSON and PLLON bits */
  157. RCC->CR &= (uint32_t) 0xFEF6FFFF;
  158. /* Reset PLLCFGR register */
  159. RCC->PLLCFGR = 0x24003010;
  160. /* Reset HSEBYP bit */
  161. RCC->CR &= (uint32_t) 0xFFFBFFFF;
  162. /* Disable all interrupts */
  163. RCC->CIR = 0x00000000;
  164. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  165. SystemInit_ExtMemCtl();
  166. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  167. /* Configure the Vector Table location add offset address ------------------*/
  168. #ifdef VECT_TAB_SRAM
  169. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  170. #else
  171. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  172. #endif
  173. }
  174. /**
  175. * @brief Update SystemCoreClock variable according to Clock Register Values.
  176. * The SystemCoreClock variable contains the core clock (HCLK), it can
  177. * be used by the user application to setup the SysTick timer or configure
  178. * other parameters.
  179. *
  180. * @note Each time the core clock (HCLK) changes, this function must be called
  181. * to update SystemCoreClock variable value. Otherwise, any configuration
  182. * based on this variable will be incorrect.
  183. *
  184. * @note - The system frequency computed by this function is not the real
  185. * frequency in the chip. It is calculated based on the predefined
  186. * constant and the selected clock source:
  187. *
  188. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  189. *
  190. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  191. *
  192. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  193. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  194. *
  195. * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  196. * 16 MHz) but the real value may vary depending on the variations
  197. * in voltage and temperature.
  198. *
  199. * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value
  200. * depends on the application requirements), user has to ensure that HSE_VALUE
  201. * is same as the real frequency of the crystal used. Otherwise, this function
  202. * may have wrong result.
  203. *
  204. * - The result of this function could be not correct when using fractional
  205. * value for HSE crystal.
  206. *
  207. * @param None
  208. * @retval None
  209. */
  210. void SystemCoreClockUpdate(void) {
  211. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  212. /* Get SYSCLK source -------------------------------------------------------*/
  213. tmp = RCC->CFGR & RCC_CFGR_SWS;
  214. switch (tmp) {
  215. case 0x00: /* HSI used as system clock source */
  216. SystemCoreClock = HSI_VALUE;
  217. break;
  218. case 0x04: /* HSE used as system clock source */
  219. SystemCoreClock = HSE_VALUE;
  220. break;
  221. case 0x08: /* PLL used as system clock source */
  222. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  223. SYSCLK = PLL_VCO / PLL_P
  224. */
  225. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  226. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  227. if (pllsource != 0) {
  228. /* HSE used as PLL clock source */
  229. pllvco = (HSE_VALUE / pllm)
  230. * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  231. } else {
  232. /* HSI used as PLL clock source */
  233. pllvco = (HSI_VALUE / pllm)
  234. * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  235. }
  236. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> 16) + 1) * 2;
  237. SystemCoreClock = pllvco / pllp;
  238. break;
  239. default:
  240. SystemCoreClock = HSI_VALUE;
  241. break;
  242. }
  243. /* Compute HCLK frequency --------------------------------------------------*/
  244. /* Get HCLK prescaler */
  245. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  246. /* HCLK frequency */
  247. SystemCoreClock >>= tmp;
  248. }
  249. #if defined (DATA_IN_ExtSRAM) && defined (DATA_IN_ExtSDRAM)
  250. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  251. || defined(STM32F469xx) || defined(STM32F479xx)
  252. /**
  253. * @brief Setup the external memory controller.
  254. * Called in startup_stm32f4xx.s before jump to main.
  255. * This function configures the external memories (SRAM/SDRAM)
  256. * This SRAM/SDRAM will be used as program data memory (including heap and stack).
  257. * @param None
  258. * @retval None
  259. */
  260. void SystemInit_ExtMemCtl(void)
  261. {
  262. __IO uint32_t tmp = 0x00;
  263. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  264. register __IO uint32_t index;
  265. /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */
  266. RCC->AHB1ENR |= 0x000001F8;
  267. /* Delay after an RCC peripheral clock enabling */
  268. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
  269. /* Connect PDx pins to FMC Alternate function */
  270. GPIOD->AFR[0] = 0x00CCC0CC;
  271. GPIOD->AFR[1] = 0xCCCCCCCC;
  272. /* Configure PDx pins in Alternate function mode */
  273. GPIOD->MODER = 0xAAAA0A8A;
  274. /* Configure PDx pins speed to 100 MHz */
  275. GPIOD->OSPEEDR = 0xFFFF0FCF;
  276. /* Configure PDx pins Output type to push-pull */
  277. GPIOD->OTYPER = 0x00000000;
  278. /* No pull-up, pull-down for PDx pins */
  279. GPIOD->PUPDR = 0x00000000;
  280. /* Connect PEx pins to FMC Alternate function */
  281. GPIOE->AFR[0] = 0xC00CC0CC;
  282. GPIOE->AFR[1] = 0xCCCCCCCC;
  283. /* Configure PEx pins in Alternate function mode */
  284. GPIOE->MODER = 0xAAAA828A;
  285. /* Configure PEx pins speed to 100 MHz */
  286. GPIOE->OSPEEDR = 0xFFFFC3CF;
  287. /* Configure PEx pins Output type to push-pull */
  288. GPIOE->OTYPER = 0x00000000;
  289. /* No pull-up, pull-down for PEx pins */
  290. GPIOE->PUPDR = 0x00000000;
  291. /* Connect PFx pins to FMC Alternate function */
  292. GPIOF->AFR[0] = 0xCCCCCCCC;
  293. GPIOF->AFR[1] = 0xCCCCCCCC;
  294. /* Configure PFx pins in Alternate function mode */
  295. GPIOF->MODER = 0xAA800AAA;
  296. /* Configure PFx pins speed to 50 MHz */
  297. GPIOF->OSPEEDR = 0xAA800AAA;
  298. /* Configure PFx pins Output type to push-pull */
  299. GPIOF->OTYPER = 0x00000000;
  300. /* No pull-up, pull-down for PFx pins */
  301. GPIOF->PUPDR = 0x00000000;
  302. /* Connect PGx pins to FMC Alternate function */
  303. GPIOG->AFR[0] = 0xCCCCCCCC;
  304. GPIOG->AFR[1] = 0xCCCCCCCC;
  305. /* Configure PGx pins in Alternate function mode */
  306. GPIOG->MODER = 0xAAAAAAAA;
  307. /* Configure PGx pins speed to 50 MHz */
  308. GPIOG->OSPEEDR = 0xAAAAAAAA;
  309. /* Configure PGx pins Output type to push-pull */
  310. GPIOG->OTYPER = 0x00000000;
  311. /* No pull-up, pull-down for PGx pins */
  312. GPIOG->PUPDR = 0x00000000;
  313. /* Connect PHx pins to FMC Alternate function */
  314. GPIOH->AFR[0] = 0x00C0CC00;
  315. GPIOH->AFR[1] = 0xCCCCCCCC;
  316. /* Configure PHx pins in Alternate function mode */
  317. GPIOH->MODER = 0xAAAA08A0;
  318. /* Configure PHx pins speed to 50 MHz */
  319. GPIOH->OSPEEDR = 0xAAAA08A0;
  320. /* Configure PHx pins Output type to push-pull */
  321. GPIOH->OTYPER = 0x00000000;
  322. /* No pull-up, pull-down for PHx pins */
  323. GPIOH->PUPDR = 0x00000000;
  324. /* Connect PIx pins to FMC Alternate function */
  325. GPIOI->AFR[0] = 0xCCCCCCCC;
  326. GPIOI->AFR[1] = 0x00000CC0;
  327. /* Configure PIx pins in Alternate function mode */
  328. GPIOI->MODER = 0x0028AAAA;
  329. /* Configure PIx pins speed to 50 MHz */
  330. GPIOI->OSPEEDR = 0x0028AAAA;
  331. /* Configure PIx pins Output type to push-pull */
  332. GPIOI->OTYPER = 0x00000000;
  333. /* No pull-up, pull-down for PIx pins */
  334. GPIOI->PUPDR = 0x00000000;
  335. /*-- FMC Configuration -------------------------------------------------------*/
  336. /* Enable the FMC interface clock */
  337. RCC->AHB3ENR |= 0x00000001;
  338. /* Delay after an RCC peripheral clock enabling */
  339. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  340. FMC_Bank5_6->SDCR[0] = 0x000019E4;
  341. FMC_Bank5_6->SDTR[0] = 0x01115351;
  342. /* SDRAM initialization sequence */
  343. /* Clock enable command */
  344. FMC_Bank5_6->SDCMR = 0x00000011;
  345. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  346. while((tmpreg != 0) && (timeout-- > 0))
  347. {
  348. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  349. }
  350. /* Delay */
  351. for (index = 0; index<1000; index++);
  352. /* PALL command */
  353. FMC_Bank5_6->SDCMR = 0x00000012;
  354. timeout = 0xFFFF;
  355. while((tmpreg != 0) && (timeout-- > 0))
  356. {
  357. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  358. }
  359. /* Auto refresh command */
  360. FMC_Bank5_6->SDCMR = 0x00000073;
  361. timeout = 0xFFFF;
  362. while((tmpreg != 0) && (timeout-- > 0))
  363. {
  364. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  365. }
  366. /* MRD register program */
  367. FMC_Bank5_6->SDCMR = 0x00046014;
  368. timeout = 0xFFFF;
  369. while((tmpreg != 0) && (timeout-- > 0))
  370. {
  371. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  372. }
  373. /* Set refresh count */
  374. tmpreg = FMC_Bank5_6->SDRTR;
  375. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  376. /* Disable write protection */
  377. tmpreg = FMC_Bank5_6->SDCR[0];
  378. FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  379. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  380. /* Configure and enable Bank1_SRAM2 */
  381. FMC_Bank1->BTCR[2] = 0x00001011;
  382. FMC_Bank1->BTCR[3] = 0x00000201;
  383. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  384. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  385. #if defined(STM32F469xx) || defined(STM32F479xx)
  386. /* Configure and enable Bank1_SRAM2 */
  387. FMC_Bank1->BTCR[2] = 0x00001091;
  388. FMC_Bank1->BTCR[3] = 0x00110212;
  389. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  390. #endif /* STM32F469xx || STM32F479xx */
  391. (void)(tmp);
  392. }
  393. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  394. #elif defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  395. /**
  396. * @brief Setup the external memory controller.
  397. * Called in startup_stm32f4xx.s before jump to main.
  398. * This function configures the external memories (SRAM/SDRAM)
  399. * This SRAM/SDRAM will be used as program data memory (including heap and stack).
  400. * @param None
  401. * @retval None
  402. */
  403. void SystemInit_ExtMemCtl(void)
  404. {
  405. __IO uint32_t tmp = 0x00;
  406. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  407. || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  408. #if defined (DATA_IN_ExtSDRAM)
  409. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  410. register __IO uint32_t index;
  411. #if defined(STM32F446xx)
  412. /* Enable GPIOA, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG interface
  413. clock */
  414. RCC->AHB1ENR |= 0x0000007D;
  415. #else
  416. /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
  417. clock */
  418. RCC->AHB1ENR |= 0x000001F8;
  419. #endif /* STM32F446xx */
  420. /* Delay after an RCC peripheral clock enabling */
  421. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
  422. #if defined(STM32F446xx)
  423. /* Connect PAx pins to FMC Alternate function */
  424. GPIOA->AFR[0] |= 0xC0000000;
  425. GPIOA->AFR[1] |= 0x00000000;
  426. /* Configure PDx pins in Alternate function mode */
  427. GPIOA->MODER |= 0x00008000;
  428. /* Configure PDx pins speed to 50 MHz */
  429. GPIOA->OSPEEDR |= 0x00008000;
  430. /* Configure PDx pins Output type to push-pull */
  431. GPIOA->OTYPER |= 0x00000000;
  432. /* No pull-up, pull-down for PDx pins */
  433. GPIOA->PUPDR |= 0x00000000;
  434. /* Connect PCx pins to FMC Alternate function */
  435. GPIOC->AFR[0] |= 0x00CC0000;
  436. GPIOC->AFR[1] |= 0x00000000;
  437. /* Configure PDx pins in Alternate function mode */
  438. GPIOC->MODER |= 0x00000A00;
  439. /* Configure PDx pins speed to 50 MHz */
  440. GPIOC->OSPEEDR |= 0x00000A00;
  441. /* Configure PDx pins Output type to push-pull */
  442. GPIOC->OTYPER |= 0x00000000;
  443. /* No pull-up, pull-down for PDx pins */
  444. GPIOC->PUPDR |= 0x00000000;
  445. #endif /* STM32F446xx */
  446. /* Connect PDx pins to FMC Alternate function */
  447. GPIOD->AFR[0] = 0x000000CC;
  448. GPIOD->AFR[1] = 0xCC000CCC;
  449. /* Configure PDx pins in Alternate function mode */
  450. GPIOD->MODER = 0xA02A000A;
  451. /* Configure PDx pins speed to 50 MHz */
  452. GPIOD->OSPEEDR = 0xA02A000A;
  453. /* Configure PDx pins Output type to push-pull */
  454. GPIOD->OTYPER = 0x00000000;
  455. /* No pull-up, pull-down for PDx pins */
  456. GPIOD->PUPDR = 0x00000000;
  457. /* Connect PEx pins to FMC Alternate function */
  458. GPIOE->AFR[0] = 0xC00000CC;
  459. GPIOE->AFR[1] = 0xCCCCCCCC;
  460. /* Configure PEx pins in Alternate function mode */
  461. GPIOE->MODER = 0xAAAA800A;
  462. /* Configure PEx pins speed to 50 MHz */
  463. GPIOE->OSPEEDR = 0xAAAA800A;
  464. /* Configure PEx pins Output type to push-pull */
  465. GPIOE->OTYPER = 0x00000000;
  466. /* No pull-up, pull-down for PEx pins */
  467. GPIOE->PUPDR = 0x00000000;
  468. /* Connect PFx pins to FMC Alternate function */
  469. GPIOF->AFR[0] = 0xCCCCCCCC;
  470. GPIOF->AFR[1] = 0xCCCCCCCC;
  471. /* Configure PFx pins in Alternate function mode */
  472. GPIOF->MODER = 0xAA800AAA;
  473. /* Configure PFx pins speed to 50 MHz */
  474. GPIOF->OSPEEDR = 0xAA800AAA;
  475. /* Configure PFx pins Output type to push-pull */
  476. GPIOF->OTYPER = 0x00000000;
  477. /* No pull-up, pull-down for PFx pins */
  478. GPIOF->PUPDR = 0x00000000;
  479. /* Connect PGx pins to FMC Alternate function */
  480. GPIOG->AFR[0] = 0xCCCCCCCC;
  481. GPIOG->AFR[1] = 0xCCCCCCCC;
  482. /* Configure PGx pins in Alternate function mode */
  483. GPIOG->MODER = 0xAAAAAAAA;
  484. /* Configure PGx pins speed to 50 MHz */
  485. GPIOG->OSPEEDR = 0xAAAAAAAA;
  486. /* Configure PGx pins Output type to push-pull */
  487. GPIOG->OTYPER = 0x00000000;
  488. /* No pull-up, pull-down for PGx pins */
  489. GPIOG->PUPDR = 0x00000000;
  490. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  491. || defined(STM32F469xx) || defined(STM32F479xx)
  492. /* Connect PHx pins to FMC Alternate function */
  493. GPIOH->AFR[0] = 0x00C0CC00;
  494. GPIOH->AFR[1] = 0xCCCCCCCC;
  495. /* Configure PHx pins in Alternate function mode */
  496. GPIOH->MODER = 0xAAAA08A0;
  497. /* Configure PHx pins speed to 50 MHz */
  498. GPIOH->OSPEEDR = 0xAAAA08A0;
  499. /* Configure PHx pins Output type to push-pull */
  500. GPIOH->OTYPER = 0x00000000;
  501. /* No pull-up, pull-down for PHx pins */
  502. GPIOH->PUPDR = 0x00000000;
  503. /* Connect PIx pins to FMC Alternate function */
  504. GPIOI->AFR[0] = 0xCCCCCCCC;
  505. GPIOI->AFR[1] = 0x00000CC0;
  506. /* Configure PIx pins in Alternate function mode */
  507. GPIOI->MODER = 0x0028AAAA;
  508. /* Configure PIx pins speed to 50 MHz */
  509. GPIOI->OSPEEDR = 0x0028AAAA;
  510. /* Configure PIx pins Output type to push-pull */
  511. GPIOI->OTYPER = 0x00000000;
  512. /* No pull-up, pull-down for PIx pins */
  513. GPIOI->PUPDR = 0x00000000;
  514. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  515. /*-- FMC Configuration -------------------------------------------------------*/
  516. /* Enable the FMC interface clock */
  517. RCC->AHB3ENR |= 0x00000001;
  518. /* Delay after an RCC peripheral clock enabling */
  519. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  520. /* Configure and enable SDRAM bank1 */
  521. #if defined(STM32F446xx)
  522. FMC_Bank5_6->SDCR[0] = 0x00001954;
  523. #else
  524. FMC_Bank5_6->SDCR[0] = 0x000019E4;
  525. #endif /* STM32F446xx */
  526. FMC_Bank5_6->SDTR[0] = 0x01115351;
  527. /* SDRAM initialization sequence */
  528. /* Clock enable command */
  529. FMC_Bank5_6->SDCMR = 0x00000011;
  530. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  531. while((tmpreg != 0) && (timeout-- > 0))
  532. {
  533. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  534. }
  535. /* Delay */
  536. for (index = 0; index<1000; index++);
  537. /* PALL command */
  538. FMC_Bank5_6->SDCMR = 0x00000012;
  539. timeout = 0xFFFF;
  540. while((tmpreg != 0) && (timeout-- > 0))
  541. {
  542. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  543. }
  544. /* Auto refresh command */
  545. #if defined(STM32F446xx)
  546. FMC_Bank5_6->SDCMR = 0x000000F3;
  547. #else
  548. FMC_Bank5_6->SDCMR = 0x00000073;
  549. #endif /* STM32F446xx */
  550. timeout = 0xFFFF;
  551. while((tmpreg != 0) && (timeout-- > 0))
  552. {
  553. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  554. }
  555. /* MRD register program */
  556. #if defined(STM32F446xx)
  557. FMC_Bank5_6->SDCMR = 0x00044014;
  558. #else
  559. FMC_Bank5_6->SDCMR = 0x00046014;
  560. #endif /* STM32F446xx */
  561. timeout = 0xFFFF;
  562. while((tmpreg != 0) && (timeout-- > 0))
  563. {
  564. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  565. }
  566. /* Set refresh count */
  567. tmpreg = FMC_Bank5_6->SDRTR;
  568. #if defined(STM32F446xx)
  569. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C<<1));
  570. #else
  571. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  572. #endif /* STM32F446xx */
  573. /* Disable write protection */
  574. tmpreg = FMC_Bank5_6->SDCR[0];
  575. FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  576. #endif /* DATA_IN_ExtSDRAM */
  577. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
  578. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
  579. || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  580. || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
  581. #if defined(DATA_IN_ExtSRAM)
  582. /*-- GPIOs Configuration -----------------------------------------------------*/
  583. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  584. RCC->AHB1ENR |= 0x00000078;
  585. /* Delay after an RCC peripheral clock enabling */
  586. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
  587. /* Connect PDx pins to FMC Alternate function */
  588. GPIOD->AFR[0] = 0x00CCC0CC;
  589. GPIOD->AFR[1] = 0xCCCCCCCC;
  590. /* Configure PDx pins in Alternate function mode */
  591. GPIOD->MODER = 0xAAAA0A8A;
  592. /* Configure PDx pins speed to 100 MHz */
  593. GPIOD->OSPEEDR = 0xFFFF0FCF;
  594. /* Configure PDx pins Output type to push-pull */
  595. GPIOD->OTYPER = 0x00000000;
  596. /* No pull-up, pull-down for PDx pins */
  597. GPIOD->PUPDR = 0x00000000;
  598. /* Connect PEx pins to FMC Alternate function */
  599. GPIOE->AFR[0] = 0xC00CC0CC;
  600. GPIOE->AFR[1] = 0xCCCCCCCC;
  601. /* Configure PEx pins in Alternate function mode */
  602. GPIOE->MODER = 0xAAAA828A;
  603. /* Configure PEx pins speed to 100 MHz */
  604. GPIOE->OSPEEDR = 0xFFFFC3CF;
  605. /* Configure PEx pins Output type to push-pull */
  606. GPIOE->OTYPER = 0x00000000;
  607. /* No pull-up, pull-down for PEx pins */
  608. GPIOE->PUPDR = 0x00000000;
  609. /* Connect PFx pins to FMC Alternate function */
  610. GPIOF->AFR[0] = 0x00CCCCCC;
  611. GPIOF->AFR[1] = 0xCCCC0000;
  612. /* Configure PFx pins in Alternate function mode */
  613. GPIOF->MODER = 0xAA000AAA;
  614. /* Configure PFx pins speed to 100 MHz */
  615. GPIOF->OSPEEDR = 0xFF000FFF;
  616. /* Configure PFx pins Output type to push-pull */
  617. GPIOF->OTYPER = 0x00000000;
  618. /* No pull-up, pull-down for PFx pins */
  619. GPIOF->PUPDR = 0x00000000;
  620. /* Connect PGx pins to FMC Alternate function */
  621. GPIOG->AFR[0] = 0x00CCCCCC;
  622. GPIOG->AFR[1] = 0x000000C0;
  623. /* Configure PGx pins in Alternate function mode */
  624. GPIOG->MODER = 0x00085AAA;
  625. /* Configure PGx pins speed to 100 MHz */
  626. GPIOG->OSPEEDR = 0x000CAFFF;
  627. /* Configure PGx pins Output type to push-pull */
  628. GPIOG->OTYPER = 0x00000000;
  629. /* No pull-up, pull-down for PGx pins */
  630. GPIOG->PUPDR = 0x00000000;
  631. /*-- FMC/FSMC Configuration --------------------------------------------------*/
  632. /* Enable the FMC/FSMC interface clock */
  633. RCC->AHB3ENR |= 0x00000001;
  634. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  635. /* Delay after an RCC peripheral clock enabling */
  636. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  637. /* Configure and enable Bank1_SRAM2 */
  638. FMC_Bank1->BTCR[2] = 0x00001011;
  639. FMC_Bank1->BTCR[3] = 0x00000201;
  640. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  641. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  642. #if defined(STM32F469xx) || defined(STM32F479xx)
  643. /* Delay after an RCC peripheral clock enabling */
  644. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  645. /* Configure and enable Bank1_SRAM2 */
  646. FMC_Bank1->BTCR[2] = 0x00001091;
  647. FMC_Bank1->BTCR[3] = 0x00110212;
  648. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  649. #endif /* STM32F469xx || STM32F479xx */
  650. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)\
  651. || defined(STM32F412Zx) || defined(STM32F412Vx)
  652. /* Delay after an RCC peripheral clock enabling */
  653. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FSMCEN);
  654. /* Configure and enable Bank1_SRAM2 */
  655. FSMC_Bank1->BTCR[2] = 0x00001011;
  656. FSMC_Bank1->BTCR[3] = 0x00000201;
  657. FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
  658. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F412Zx || STM32F412Vx */
  659. #endif /* DATA_IN_ExtSRAM */
  660. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  661. STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */
  662. (void)(tmp);
  663. }
  664. #endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */
  665. /**
  666. * @}
  667. */
  668. /**
  669. * @}
  670. */
  671. /**
  672. * @}
  673. */
  674. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/