system_stm32f4xx.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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, 9};
  122. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  123. /**
  124. * @}
  125. */
  126. typedef void(*pVector)(void);
  127. extern const pVector __vector_table;
  128. /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
  129. * @{
  130. */
  131. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  132. static void SystemInit_ExtMemCtl(void);
  133. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  134. void System_UpdateResetVector(void);
  135. void System_SetVector(uint32_t vector);
  136. /**
  137. * @}
  138. */
  139. /** @addtogroup STM32F4xx_System_Private_Functions
  140. * @{
  141. */
  142. /**
  143. * @brief Setup the microcontroller system
  144. * Initialize the FPU setting, vector table location and External memory
  145. * configuration.
  146. * @param None
  147. * @retval None
  148. */
  149. void SystemInit(void)
  150. {
  151. //__disable_irq();
  152. /* FPU settings ------------------------------------------------------------*/
  153. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  154. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  155. #endif
  156. /* Reset the RCC clock configuration to the default reset state ------------*/
  157. /* Set HSION bit */
  158. RCC->CR |= (uint32_t)0x00000001;
  159. /* Reset CFGR register */
  160. RCC->CFGR = 0x00000000;
  161. /* Reset HSEON, CSSON and PLLON bits */
  162. RCC->CR &= (uint32_t)0xFEF6FFFF;
  163. /* Reset PLLCFGR register */
  164. RCC->PLLCFGR = 0x24003010;
  165. /* Reset HSEBYP bit */
  166. RCC->CR &= (uint32_t)0xFFFBFFFF;
  167. /* Disable all interrupts */
  168. RCC->CIR = 0x00000000;
  169. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  170. SystemInit_ExtMemCtl();
  171. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  172. /* Configure the Vector Table location add offset address ------------------*/
  173. /*#ifdef VECT_TAB_SRAM
  174. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; // Vector Table Relocation in Internal SRAM
  175. #else
  176. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; // Vector Table Relocation in Internal FLASH
  177. #endif*/
  178. System_UpdateResetVector();
  179. //__enable_irq();
  180. }
  181. /*
  182. *
  183. */
  184. void System_UpdateResetVector(void)
  185. {
  186. uint32_t address = (uint32_t)&__vector_table;
  187. SCB->VTOR = address | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  188. }
  189. void System_SetVector(uint32_t vector)
  190. {
  191. SCB->VTOR = vector | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  192. }
  193. /**
  194. * @brief Update SystemCoreClock variable according to Clock Register Values.
  195. * The SystemCoreClock variable contains the core clock (HCLK), it can
  196. * be used by the user application to setup the SysTick timer or configure
  197. * other parameters.
  198. *
  199. * @note Each time the core clock (HCLK) changes, this function must be called
  200. * to update SystemCoreClock variable value. Otherwise, any configuration
  201. * based on this variable will be incorrect.
  202. *
  203. * @note - The system frequency computed by this function is not the real
  204. * frequency in the chip. It is calculated based on the predefined
  205. * constant and the selected clock source:
  206. *
  207. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  208. *
  209. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  210. *
  211. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  212. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  213. *
  214. * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  215. * 16 MHz) but the real value may vary depending on the variations
  216. * in voltage and temperature.
  217. *
  218. * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value
  219. * depends on the application requirements), user has to ensure that HSE_VALUE
  220. * is same as the real frequency of the crystal used. Otherwise, this function
  221. * may have wrong result.
  222. *
  223. * - The result of this function could be not correct when using fractional
  224. * value for HSE crystal.
  225. *
  226. * @param None
  227. * @retval None
  228. */
  229. void SystemCoreClockUpdate(void)
  230. {
  231. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  232. /* Get SYSCLK source -------------------------------------------------------*/
  233. tmp = RCC->CFGR & RCC_CFGR_SWS;
  234. switch (tmp)
  235. {
  236. case 0x00: /* HSI used as system clock source */
  237. SystemCoreClock = HSI_VALUE;
  238. break;
  239. case 0x04: /* HSE used as system clock source */
  240. SystemCoreClock = HSE_VALUE;
  241. break;
  242. case 0x08: /* PLL used as system clock source */
  243. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  244. SYSCLK = PLL_VCO / PLL_P
  245. */
  246. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  247. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  248. if (pllsource != 0)
  249. {
  250. /* HSE used as PLL clock source */
  251. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  252. }
  253. else
  254. {
  255. /* HSI used as PLL clock source */
  256. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  257. }
  258. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  259. SystemCoreClock = pllvco/pllp;
  260. break;
  261. default:
  262. SystemCoreClock = HSI_VALUE;
  263. break;
  264. }
  265. /* Compute HCLK frequency --------------------------------------------------*/
  266. /* Get HCLK prescaler */
  267. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  268. /* HCLK frequency */
  269. SystemCoreClock >>= tmp;
  270. }
  271. #if defined (DATA_IN_ExtSRAM) && defined (DATA_IN_ExtSDRAM)
  272. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  273. || defined(STM32F469xx) || defined(STM32F479xx)
  274. /**
  275. * @brief Setup the external memory controller.
  276. * Called in startup_stm32f4xx.s before jump to main.
  277. * This function configures the external memories (SRAM/SDRAM)
  278. * This SRAM/SDRAM will be used as program data memory (including heap and stack).
  279. * @param None
  280. * @retval None
  281. */
  282. void SystemInit_ExtMemCtl(void)
  283. {
  284. __IO uint32_t tmp = 0x00;
  285. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  286. register __IO uint32_t index;
  287. /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */
  288. RCC->AHB1ENR |= 0x000001F8;
  289. /* Delay after an RCC peripheral clock enabling */
  290. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
  291. /* Connect PDx pins to FMC Alternate function */
  292. GPIOD->AFR[0] = 0x00CCC0CC;
  293. GPIOD->AFR[1] = 0xCCCCCCCC;
  294. /* Configure PDx pins in Alternate function mode */
  295. GPIOD->MODER = 0xAAAA0A8A;
  296. /* Configure PDx pins speed to 100 MHz */
  297. GPIOD->OSPEEDR = 0xFFFF0FCF;
  298. /* Configure PDx pins Output type to push-pull */
  299. GPIOD->OTYPER = 0x00000000;
  300. /* No pull-up, pull-down for PDx pins */
  301. GPIOD->PUPDR = 0x00000000;
  302. /* Connect PEx pins to FMC Alternate function */
  303. GPIOE->AFR[0] = 0xC00CC0CC;
  304. GPIOE->AFR[1] = 0xCCCCCCCC;
  305. /* Configure PEx pins in Alternate function mode */
  306. GPIOE->MODER = 0xAAAA828A;
  307. /* Configure PEx pins speed to 100 MHz */
  308. GPIOE->OSPEEDR = 0xFFFFC3CF;
  309. /* Configure PEx pins Output type to push-pull */
  310. GPIOE->OTYPER = 0x00000000;
  311. /* No pull-up, pull-down for PEx pins */
  312. GPIOE->PUPDR = 0x00000000;
  313. /* Connect PFx pins to FMC Alternate function */
  314. GPIOF->AFR[0] = 0xCCCCCCCC;
  315. GPIOF->AFR[1] = 0xCCCCCCCC;
  316. /* Configure PFx pins in Alternate function mode */
  317. GPIOF->MODER = 0xAA800AAA;
  318. /* Configure PFx pins speed to 50 MHz */
  319. GPIOF->OSPEEDR = 0xAA800AAA;
  320. /* Configure PFx pins Output type to push-pull */
  321. GPIOF->OTYPER = 0x00000000;
  322. /* No pull-up, pull-down for PFx pins */
  323. GPIOF->PUPDR = 0x00000000;
  324. /* Connect PGx pins to FMC Alternate function */
  325. GPIOG->AFR[0] = 0xCCCCCCCC;
  326. GPIOG->AFR[1] = 0xCCCCCCCC;
  327. /* Configure PGx pins in Alternate function mode */
  328. GPIOG->MODER = 0xAAAAAAAA;
  329. /* Configure PGx pins speed to 50 MHz */
  330. GPIOG->OSPEEDR = 0xAAAAAAAA;
  331. /* Configure PGx pins Output type to push-pull */
  332. GPIOG->OTYPER = 0x00000000;
  333. /* No pull-up, pull-down for PGx pins */
  334. GPIOG->PUPDR = 0x00000000;
  335. /* Connect PHx pins to FMC Alternate function */
  336. GPIOH->AFR[0] = 0x00C0CC00;
  337. GPIOH->AFR[1] = 0xCCCCCCCC;
  338. /* Configure PHx pins in Alternate function mode */
  339. GPIOH->MODER = 0xAAAA08A0;
  340. /* Configure PHx pins speed to 50 MHz */
  341. GPIOH->OSPEEDR = 0xAAAA08A0;
  342. /* Configure PHx pins Output type to push-pull */
  343. GPIOH->OTYPER = 0x00000000;
  344. /* No pull-up, pull-down for PHx pins */
  345. GPIOH->PUPDR = 0x00000000;
  346. /* Connect PIx pins to FMC Alternate function */
  347. GPIOI->AFR[0] = 0xCCCCCCCC;
  348. GPIOI->AFR[1] = 0x00000CC0;
  349. /* Configure PIx pins in Alternate function mode */
  350. GPIOI->MODER = 0x0028AAAA;
  351. /* Configure PIx pins speed to 50 MHz */
  352. GPIOI->OSPEEDR = 0x0028AAAA;
  353. /* Configure PIx pins Output type to push-pull */
  354. GPIOI->OTYPER = 0x00000000;
  355. /* No pull-up, pull-down for PIx pins */
  356. GPIOI->PUPDR = 0x00000000;
  357. /*-- FMC Configuration -------------------------------------------------------*/
  358. /* Enable the FMC interface clock */
  359. RCC->AHB3ENR |= 0x00000001;
  360. /* Delay after an RCC peripheral clock enabling */
  361. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  362. FMC_Bank5_6->SDCR[0] = 0x000019E4;
  363. FMC_Bank5_6->SDTR[0] = 0x01115351;
  364. /* SDRAM initialization sequence */
  365. /* Clock enable command */
  366. FMC_Bank5_6->SDCMR = 0x00000011;
  367. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  368. while((tmpreg != 0) && (timeout-- > 0))
  369. {
  370. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  371. }
  372. /* Delay */
  373. for (index = 0; index<1000; index++);
  374. /* PALL command */
  375. FMC_Bank5_6->SDCMR = 0x00000012;
  376. timeout = 0xFFFF;
  377. while((tmpreg != 0) && (timeout-- > 0))
  378. {
  379. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  380. }
  381. /* Auto refresh command */
  382. FMC_Bank5_6->SDCMR = 0x00000073;
  383. timeout = 0xFFFF;
  384. while((tmpreg != 0) && (timeout-- > 0))
  385. {
  386. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  387. }
  388. /* MRD register program */
  389. FMC_Bank5_6->SDCMR = 0x00046014;
  390. timeout = 0xFFFF;
  391. while((tmpreg != 0) && (timeout-- > 0))
  392. {
  393. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  394. }
  395. /* Set refresh count */
  396. tmpreg = FMC_Bank5_6->SDRTR;
  397. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  398. /* Disable write protection */
  399. tmpreg = FMC_Bank5_6->SDCR[0];
  400. FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  401. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  402. /* Configure and enable Bank1_SRAM2 */
  403. FMC_Bank1->BTCR[2] = 0x00001011;
  404. FMC_Bank1->BTCR[3] = 0x00000201;
  405. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  406. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  407. #if defined(STM32F469xx) || defined(STM32F479xx)
  408. /* Configure and enable Bank1_SRAM2 */
  409. FMC_Bank1->BTCR[2] = 0x00001091;
  410. FMC_Bank1->BTCR[3] = 0x00110212;
  411. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  412. #endif /* STM32F469xx || STM32F479xx */
  413. (void)(tmp);
  414. }
  415. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  416. #elif defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  417. /**
  418. * @brief Setup the external memory controller.
  419. * Called in startup_stm32f4xx.s before jump to main.
  420. * This function configures the external memories (SRAM/SDRAM)
  421. * This SRAM/SDRAM will be used as program data memory (including heap and stack).
  422. * @param None
  423. * @retval None
  424. */
  425. void SystemInit_ExtMemCtl(void)
  426. {
  427. __IO uint32_t tmp = 0x00;
  428. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  429. || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  430. #if defined (DATA_IN_ExtSDRAM)
  431. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  432. register __IO uint32_t index;
  433. #if defined(STM32F446xx)
  434. /* Enable GPIOA, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG interface
  435. clock */
  436. RCC->AHB1ENR |= 0x0000007D;
  437. #else
  438. /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
  439. clock */
  440. RCC->AHB1ENR |= 0x000001F8;
  441. #endif /* STM32F446xx */
  442. /* Delay after an RCC peripheral clock enabling */
  443. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
  444. #if defined(STM32F446xx)
  445. /* Connect PAx pins to FMC Alternate function */
  446. GPIOA->AFR[0] |= 0xC0000000;
  447. GPIOA->AFR[1] |= 0x00000000;
  448. /* Configure PDx pins in Alternate function mode */
  449. GPIOA->MODER |= 0x00008000;
  450. /* Configure PDx pins speed to 50 MHz */
  451. GPIOA->OSPEEDR |= 0x00008000;
  452. /* Configure PDx pins Output type to push-pull */
  453. GPIOA->OTYPER |= 0x00000000;
  454. /* No pull-up, pull-down for PDx pins */
  455. GPIOA->PUPDR |= 0x00000000;
  456. /* Connect PCx pins to FMC Alternate function */
  457. GPIOC->AFR[0] |= 0x00CC0000;
  458. GPIOC->AFR[1] |= 0x00000000;
  459. /* Configure PDx pins in Alternate function mode */
  460. GPIOC->MODER |= 0x00000A00;
  461. /* Configure PDx pins speed to 50 MHz */
  462. GPIOC->OSPEEDR |= 0x00000A00;
  463. /* Configure PDx pins Output type to push-pull */
  464. GPIOC->OTYPER |= 0x00000000;
  465. /* No pull-up, pull-down for PDx pins */
  466. GPIOC->PUPDR |= 0x00000000;
  467. #endif /* STM32F446xx */
  468. /* Connect PDx pins to FMC Alternate function */
  469. GPIOD->AFR[0] = 0x000000CC;
  470. GPIOD->AFR[1] = 0xCC000CCC;
  471. /* Configure PDx pins in Alternate function mode */
  472. GPIOD->MODER = 0xA02A000A;
  473. /* Configure PDx pins speed to 50 MHz */
  474. GPIOD->OSPEEDR = 0xA02A000A;
  475. /* Configure PDx pins Output type to push-pull */
  476. GPIOD->OTYPER = 0x00000000;
  477. /* No pull-up, pull-down for PDx pins */
  478. GPIOD->PUPDR = 0x00000000;
  479. /* Connect PEx pins to FMC Alternate function */
  480. GPIOE->AFR[0] = 0xC00000CC;
  481. GPIOE->AFR[1] = 0xCCCCCCCC;
  482. /* Configure PEx pins in Alternate function mode */
  483. GPIOE->MODER = 0xAAAA800A;
  484. /* Configure PEx pins speed to 50 MHz */
  485. GPIOE->OSPEEDR = 0xAAAA800A;
  486. /* Configure PEx pins Output type to push-pull */
  487. GPIOE->OTYPER = 0x00000000;
  488. /* No pull-up, pull-down for PEx pins */
  489. GPIOE->PUPDR = 0x00000000;
  490. /* Connect PFx pins to FMC Alternate function */
  491. GPIOF->AFR[0] = 0xCCCCCCCC;
  492. GPIOF->AFR[1] = 0xCCCCCCCC;
  493. /* Configure PFx pins in Alternate function mode */
  494. GPIOF->MODER = 0xAA800AAA;
  495. /* Configure PFx pins speed to 50 MHz */
  496. GPIOF->OSPEEDR = 0xAA800AAA;
  497. /* Configure PFx pins Output type to push-pull */
  498. GPIOF->OTYPER = 0x00000000;
  499. /* No pull-up, pull-down for PFx pins */
  500. GPIOF->PUPDR = 0x00000000;
  501. /* Connect PGx pins to FMC Alternate function */
  502. GPIOG->AFR[0] = 0xCCCCCCCC;
  503. GPIOG->AFR[1] = 0xCCCCCCCC;
  504. /* Configure PGx pins in Alternate function mode */
  505. GPIOG->MODER = 0xAAAAAAAA;
  506. /* Configure PGx pins speed to 50 MHz */
  507. GPIOG->OSPEEDR = 0xAAAAAAAA;
  508. /* Configure PGx pins Output type to push-pull */
  509. GPIOG->OTYPER = 0x00000000;
  510. /* No pull-up, pull-down for PGx pins */
  511. GPIOG->PUPDR = 0x00000000;
  512. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  513. || defined(STM32F469xx) || defined(STM32F479xx)
  514. /* Connect PHx pins to FMC Alternate function */
  515. GPIOH->AFR[0] = 0x00C0CC00;
  516. GPIOH->AFR[1] = 0xCCCCCCCC;
  517. /* Configure PHx pins in Alternate function mode */
  518. GPIOH->MODER = 0xAAAA08A0;
  519. /* Configure PHx pins speed to 50 MHz */
  520. GPIOH->OSPEEDR = 0xAAAA08A0;
  521. /* Configure PHx pins Output type to push-pull */
  522. GPIOH->OTYPER = 0x00000000;
  523. /* No pull-up, pull-down for PHx pins */
  524. GPIOH->PUPDR = 0x00000000;
  525. /* Connect PIx pins to FMC Alternate function */
  526. GPIOI->AFR[0] = 0xCCCCCCCC;
  527. GPIOI->AFR[1] = 0x00000CC0;
  528. /* Configure PIx pins in Alternate function mode */
  529. GPIOI->MODER = 0x0028AAAA;
  530. /* Configure PIx pins speed to 50 MHz */
  531. GPIOI->OSPEEDR = 0x0028AAAA;
  532. /* Configure PIx pins Output type to push-pull */
  533. GPIOI->OTYPER = 0x00000000;
  534. /* No pull-up, pull-down for PIx pins */
  535. GPIOI->PUPDR = 0x00000000;
  536. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  537. /*-- FMC Configuration -------------------------------------------------------*/
  538. /* Enable the FMC interface clock */
  539. RCC->AHB3ENR |= 0x00000001;
  540. /* Delay after an RCC peripheral clock enabling */
  541. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  542. /* Configure and enable SDRAM bank1 */
  543. #if defined(STM32F446xx)
  544. FMC_Bank5_6->SDCR[0] = 0x00001954;
  545. #else
  546. FMC_Bank5_6->SDCR[0] = 0x000019E4;
  547. #endif /* STM32F446xx */
  548. FMC_Bank5_6->SDTR[0] = 0x01115351;
  549. /* SDRAM initialization sequence */
  550. /* Clock enable command */
  551. FMC_Bank5_6->SDCMR = 0x00000011;
  552. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  553. while((tmpreg != 0) && (timeout-- > 0))
  554. {
  555. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  556. }
  557. /* Delay */
  558. for (index = 0; index<1000; index++);
  559. /* PALL command */
  560. FMC_Bank5_6->SDCMR = 0x00000012;
  561. timeout = 0xFFFF;
  562. while((tmpreg != 0) && (timeout-- > 0))
  563. {
  564. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  565. }
  566. /* Auto refresh command */
  567. #if defined(STM32F446xx)
  568. FMC_Bank5_6->SDCMR = 0x000000F3;
  569. #else
  570. FMC_Bank5_6->SDCMR = 0x00000073;
  571. #endif /* STM32F446xx */
  572. timeout = 0xFFFF;
  573. while((tmpreg != 0) && (timeout-- > 0))
  574. {
  575. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  576. }
  577. /* MRD register program */
  578. #if defined(STM32F446xx)
  579. FMC_Bank5_6->SDCMR = 0x00044014;
  580. #else
  581. FMC_Bank5_6->SDCMR = 0x00046014;
  582. #endif /* STM32F446xx */
  583. timeout = 0xFFFF;
  584. while((tmpreg != 0) && (timeout-- > 0))
  585. {
  586. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  587. }
  588. /* Set refresh count */
  589. tmpreg = FMC_Bank5_6->SDRTR;
  590. #if defined(STM32F446xx)
  591. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C<<1));
  592. #else
  593. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  594. #endif /* STM32F446xx */
  595. /* Disable write protection */
  596. tmpreg = FMC_Bank5_6->SDCR[0];
  597. FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  598. #endif /* DATA_IN_ExtSDRAM */
  599. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
  600. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\
  601. || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\
  602. || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx)
  603. #if defined(DATA_IN_ExtSRAM)
  604. /*-- GPIOs Configuration -----------------------------------------------------*/
  605. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  606. RCC->AHB1ENR |= 0x00000078;
  607. /* Delay after an RCC peripheral clock enabling */
  608. tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN);
  609. /* Connect PDx pins to FMC Alternate function */
  610. GPIOD->AFR[0] = 0x00CCC0CC;
  611. GPIOD->AFR[1] = 0xCCCCCCCC;
  612. /* Configure PDx pins in Alternate function mode */
  613. GPIOD->MODER = 0xAAAA0A8A;
  614. /* Configure PDx pins speed to 100 MHz */
  615. GPIOD->OSPEEDR = 0xFFFF0FCF;
  616. /* Configure PDx pins Output type to push-pull */
  617. GPIOD->OTYPER = 0x00000000;
  618. /* No pull-up, pull-down for PDx pins */
  619. GPIOD->PUPDR = 0x00000000;
  620. /* Connect PEx pins to FMC Alternate function */
  621. GPIOE->AFR[0] = 0xC00CC0CC;
  622. GPIOE->AFR[1] = 0xCCCCCCCC;
  623. /* Configure PEx pins in Alternate function mode */
  624. GPIOE->MODER = 0xAAAA828A;
  625. /* Configure PEx pins speed to 100 MHz */
  626. GPIOE->OSPEEDR = 0xFFFFC3CF;
  627. /* Configure PEx pins Output type to push-pull */
  628. GPIOE->OTYPER = 0x00000000;
  629. /* No pull-up, pull-down for PEx pins */
  630. GPIOE->PUPDR = 0x00000000;
  631. /* Connect PFx pins to FMC Alternate function */
  632. GPIOF->AFR[0] = 0x00CCCCCC;
  633. GPIOF->AFR[1] = 0xCCCC0000;
  634. /* Configure PFx pins in Alternate function mode */
  635. GPIOF->MODER = 0xAA000AAA;
  636. /* Configure PFx pins speed to 100 MHz */
  637. GPIOF->OSPEEDR = 0xFF000FFF;
  638. /* Configure PFx pins Output type to push-pull */
  639. GPIOF->OTYPER = 0x00000000;
  640. /* No pull-up, pull-down for PFx pins */
  641. GPIOF->PUPDR = 0x00000000;
  642. /* Connect PGx pins to FMC Alternate function */
  643. GPIOG->AFR[0] = 0x00CCCCCC;
  644. GPIOG->AFR[1] = 0x000000C0;
  645. /* Configure PGx pins in Alternate function mode */
  646. GPIOG->MODER = 0x00085AAA;
  647. /* Configure PGx pins speed to 100 MHz */
  648. GPIOG->OSPEEDR = 0x000CAFFF;
  649. /* Configure PGx pins Output type to push-pull */
  650. GPIOG->OTYPER = 0x00000000;
  651. /* No pull-up, pull-down for PGx pins */
  652. GPIOG->PUPDR = 0x00000000;
  653. /*-- FMC/FSMC Configuration --------------------------------------------------*/
  654. /* Enable the FMC/FSMC interface clock */
  655. RCC->AHB3ENR |= 0x00000001;
  656. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
  657. /* Delay after an RCC peripheral clock enabling */
  658. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  659. /* Configure and enable Bank1_SRAM2 */
  660. FMC_Bank1->BTCR[2] = 0x00001011;
  661. FMC_Bank1->BTCR[3] = 0x00000201;
  662. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  663. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
  664. #if defined(STM32F469xx) || defined(STM32F479xx)
  665. /* Delay after an RCC peripheral clock enabling */
  666. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN);
  667. /* Configure and enable Bank1_SRAM2 */
  668. FMC_Bank1->BTCR[2] = 0x00001091;
  669. FMC_Bank1->BTCR[3] = 0x00110212;
  670. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  671. #endif /* STM32F469xx || STM32F479xx */
  672. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)\
  673. || defined(STM32F412Zx) || defined(STM32F412Vx)
  674. /* Delay after an RCC peripheral clock enabling */
  675. tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FSMCEN);
  676. /* Configure and enable Bank1_SRAM2 */
  677. FSMC_Bank1->BTCR[2] = 0x00001011;
  678. FSMC_Bank1->BTCR[3] = 0x00000201;
  679. FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF;
  680. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F412Zx || STM32F412Vx */
  681. #endif /* DATA_IN_ExtSRAM */
  682. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  683. STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */
  684. (void)(tmp);
  685. }
  686. #endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */
  687. /**
  688. * @}
  689. */
  690. /**
  691. * @}
  692. */
  693. /**
  694. * @}
  695. */
  696. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/