system_stm32f4xx.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f4xx.c
  4. * @author MCD Application Team
  5. * @version V1.4.0
  6. * @date 04-August-2014
  7. * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
  8. * This file contains the system clock configuration for STM32F4xx devices.
  9. *
  10. * 1. This file provides two functions and one global variable to be called from
  11. * user application:
  12. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  13. * and Divider factors, AHB/APBx prescalers and Flash settings),
  14. * depending on the configuration made in the clock xls tool.
  15. * This function is called at startup just after reset and
  16. * before branch to main program. This call is made inside
  17. * the "startup_stm32f4xx.s" file.
  18. *
  19. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  20. * by the user application to setup the SysTick
  21. * timer or configure other parameters.
  22. *
  23. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  24. * be called whenever the core clock is changed
  25. * during program execution.
  26. *
  27. * 2. After each device reset the HSI (16 MHz) is used as system clock source.
  28. * Then SystemInit() function is called, in "startup_stm32f4xx.s" file, to
  29. * configure the system clock before to branch to main program.
  30. *
  31. * 3. If the system clock source selected by user fails to startup, the SystemInit()
  32. * function will do nothing and HSI still used as system clock source. User can
  33. * add some code to deal with this issue inside the SetSysClock() function.
  34. *
  35. * 4. The default value of HSE crystal is set to 25MHz, refer to "HSE_VALUE" define
  36. * in "stm32f4xx.h" file. When HSE is used as system clock source, directly or
  37. * through PLL, and you are using different crystal you have to adapt the HSE
  38. * value to your own configuration.
  39. *
  40. * 5. This file configures the system clock as follows:
  41. *=============================================================================
  42. *=============================================================================
  43. * Supported STM32F40xxx/41xxx devices
  44. *-----------------------------------------------------------------------------
  45. * System Clock source | PLL (HSE)
  46. *-----------------------------------------------------------------------------
  47. * SYSCLK(Hz) | 168000000
  48. *-----------------------------------------------------------------------------
  49. * HCLK(Hz) | 168000000
  50. *-----------------------------------------------------------------------------
  51. * AHB Prescaler | 1
  52. *-----------------------------------------------------------------------------
  53. * APB1 Prescaler | 4
  54. *-----------------------------------------------------------------------------
  55. * APB2 Prescaler | 2
  56. *-----------------------------------------------------------------------------
  57. * HSE Frequency(Hz) | 25000000
  58. *-----------------------------------------------------------------------------
  59. * PLL_M | 25
  60. *-----------------------------------------------------------------------------
  61. * PLL_N | 336
  62. *-----------------------------------------------------------------------------
  63. * PLL_P | 2
  64. *-----------------------------------------------------------------------------
  65. * PLL_Q | 7
  66. *-----------------------------------------------------------------------------
  67. * PLLI2S_N | NA
  68. *-----------------------------------------------------------------------------
  69. * PLLI2S_R | NA
  70. *-----------------------------------------------------------------------------
  71. * I2S input clock | NA
  72. *-----------------------------------------------------------------------------
  73. * VDD(V) | 3.3
  74. *-----------------------------------------------------------------------------
  75. * Main regulator output voltage | Scale1 mode
  76. *-----------------------------------------------------------------------------
  77. * Flash Latency(WS) | 5
  78. *-----------------------------------------------------------------------------
  79. * Prefetch Buffer | ON
  80. *-----------------------------------------------------------------------------
  81. * Instruction cache | ON
  82. *-----------------------------------------------------------------------------
  83. * Data cache | ON
  84. *-----------------------------------------------------------------------------
  85. * Require 48MHz for USB OTG FS, | Disabled
  86. * SDIO and RNG clock |
  87. *-----------------------------------------------------------------------------
  88. *=============================================================================
  89. *=============================================================================
  90. * Supported STM32F42xxx/43xxx devices
  91. *-----------------------------------------------------------------------------
  92. * System Clock source | PLL (HSE)
  93. *-----------------------------------------------------------------------------
  94. * SYSCLK(Hz) | 180000000
  95. *-----------------------------------------------------------------------------
  96. * HCLK(Hz) | 180000000
  97. *-----------------------------------------------------------------------------
  98. * AHB Prescaler | 1
  99. *-----------------------------------------------------------------------------
  100. * APB1 Prescaler | 4
  101. *-----------------------------------------------------------------------------
  102. * APB2 Prescaler | 2
  103. *-----------------------------------------------------------------------------
  104. * HSE Frequency(Hz) | 25000000
  105. *-----------------------------------------------------------------------------
  106. * PLL_M | 25
  107. *-----------------------------------------------------------------------------
  108. * PLL_N | 360
  109. *-----------------------------------------------------------------------------
  110. * PLL_P | 2
  111. *-----------------------------------------------------------------------------
  112. * PLL_Q | 7
  113. *-----------------------------------------------------------------------------
  114. * PLLI2S_N | NA
  115. *-----------------------------------------------------------------------------
  116. * PLLI2S_R | NA
  117. *-----------------------------------------------------------------------------
  118. * I2S input clock | NA
  119. *-----------------------------------------------------------------------------
  120. * VDD(V) | 3.3
  121. *-----------------------------------------------------------------------------
  122. * Main regulator output voltage | Scale1 mode
  123. *-----------------------------------------------------------------------------
  124. * Flash Latency(WS) | 5
  125. *-----------------------------------------------------------------------------
  126. * Prefetch Buffer | ON
  127. *-----------------------------------------------------------------------------
  128. * Instruction cache | ON
  129. *-----------------------------------------------------------------------------
  130. * Data cache | ON
  131. *-----------------------------------------------------------------------------
  132. * Require 48MHz for USB OTG FS, | Disabled
  133. * SDIO and RNG clock |
  134. *-----------------------------------------------------------------------------
  135. *=============================================================================
  136. *=============================================================================
  137. * Supported STM32F401xx devices
  138. *-----------------------------------------------------------------------------
  139. * System Clock source | PLL (HSE)
  140. *-----------------------------------------------------------------------------
  141. * SYSCLK(Hz) | 84000000
  142. *-----------------------------------------------------------------------------
  143. * HCLK(Hz) | 84000000
  144. *-----------------------------------------------------------------------------
  145. * AHB Prescaler | 1
  146. *-----------------------------------------------------------------------------
  147. * APB1 Prescaler | 2
  148. *-----------------------------------------------------------------------------
  149. * APB2 Prescaler | 1
  150. *-----------------------------------------------------------------------------
  151. * HSE Frequency(Hz) | 25000000
  152. *-----------------------------------------------------------------------------
  153. * PLL_M | 25
  154. *-----------------------------------------------------------------------------
  155. * PLL_N | 336
  156. *-----------------------------------------------------------------------------
  157. * PLL_P | 4
  158. *-----------------------------------------------------------------------------
  159. * PLL_Q | 7
  160. *-----------------------------------------------------------------------------
  161. * PLLI2S_N | NA
  162. *-----------------------------------------------------------------------------
  163. * PLLI2S_R | NA
  164. *-----------------------------------------------------------------------------
  165. * I2S input clock | NA
  166. *-----------------------------------------------------------------------------
  167. * VDD(V) | 3.3
  168. *-----------------------------------------------------------------------------
  169. * Main regulator output voltage | Scale1 mode
  170. *-----------------------------------------------------------------------------
  171. * Flash Latency(WS) | 2
  172. *-----------------------------------------------------------------------------
  173. * Prefetch Buffer | ON
  174. *-----------------------------------------------------------------------------
  175. * Instruction cache | ON
  176. *-----------------------------------------------------------------------------
  177. * Data cache | ON
  178. *-----------------------------------------------------------------------------
  179. * Require 48MHz for USB OTG FS, | Disabled
  180. * SDIO and RNG clock |
  181. *-----------------------------------------------------------------------------
  182. *=============================================================================
  183. *=============================================================================
  184. * Supported STM32F411xx devices
  185. *-----------------------------------------------------------------------------
  186. * System Clock source | PLL (HSI)
  187. *-----------------------------------------------------------------------------
  188. * SYSCLK(Hz) | 100000000
  189. *-----------------------------------------------------------------------------
  190. * HCLK(Hz) | 100000000
  191. *-----------------------------------------------------------------------------
  192. * AHB Prescaler | 1
  193. *-----------------------------------------------------------------------------
  194. * APB1 Prescaler | 2
  195. *-----------------------------------------------------------------------------
  196. * APB2 Prescaler | 1
  197. *-----------------------------------------------------------------------------
  198. * HSI Frequency(Hz) | 16000000
  199. *-----------------------------------------------------------------------------
  200. * PLL_M | 16
  201. *-----------------------------------------------------------------------------
  202. * PLL_N | 400
  203. *-----------------------------------------------------------------------------
  204. * PLL_P | 4
  205. *-----------------------------------------------------------------------------
  206. * PLL_Q | 7
  207. *-----------------------------------------------------------------------------
  208. * PLLI2S_N | NA
  209. *-----------------------------------------------------------------------------
  210. * PLLI2S_R | NA
  211. *-----------------------------------------------------------------------------
  212. * I2S input clock | NA
  213. *-----------------------------------------------------------------------------
  214. * VDD(V) | 3.3
  215. *-----------------------------------------------------------------------------
  216. * Main regulator output voltage | Scale1 mode
  217. *-----------------------------------------------------------------------------
  218. * Flash Latency(WS) | 3
  219. *-----------------------------------------------------------------------------
  220. * Prefetch Buffer | ON
  221. *-----------------------------------------------------------------------------
  222. * Instruction cache | ON
  223. *-----------------------------------------------------------------------------
  224. * Data cache | ON
  225. *-----------------------------------------------------------------------------
  226. * Require 48MHz for USB OTG FS, | Disabled
  227. * SDIO and RNG clock |
  228. *-----------------------------------------------------------------------------
  229. *=============================================================================
  230. ******************************************************************************
  231. * @attention
  232. *
  233. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  234. *
  235. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  236. * You may not use this file except in compliance with the License.
  237. * You may obtain a copy of the License at:
  238. *
  239. * http://www.st.com/software_license_agreement_liberty_v2
  240. *
  241. * Unless required by applicable law or agreed to in writing, software
  242. * distributed under the License is distributed on an "AS IS" BASIS,
  243. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  244. * See the License for the specific language governing permissions and
  245. * limitations under the License.
  246. *
  247. ******************************************************************************
  248. */
  249. /** @addtogroup CMSIS
  250. * @{
  251. */
  252. /** @addtogroup stm32f4xx_system
  253. * @{
  254. */
  255. /** @addtogroup STM32F4xx_System_Private_Includes
  256. * @{
  257. */
  258. #include "stm32f4xx.h"
  259. /**
  260. * @}
  261. */
  262. /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
  263. * @{
  264. */
  265. /**
  266. * @}
  267. */
  268. /** @addtogroup STM32F4xx_System_Private_Defines
  269. * @{
  270. */
  271. /************************* Miscellaneous Configuration ************************/
  272. /*!< Uncomment the following line if you need to use external SRAM or SDRAM mounted
  273. on STM324xG_EVAL/STM324x7I_EVAL/STM324x9I_EVAL boards as data memory */
  274. #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx)
  275. /* #define DATA_IN_ExtSRAM */
  276. #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */
  277. #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
  278. /* #define DATA_IN_ExtSDRAM */
  279. #endif /* STM32F427_437x || STM32F429_439xx */
  280. #if defined (STM32F411xE)
  281. /*!< Uncomment the following line if you need to clock the STM32F411xE by HSE Bypass
  282. through STLINK MCO pin of STM32F103 microcontroller. The frequency cannot be changed
  283. and is fixed at 8 MHz.
  284. Hardware configuration needed for Nucleo Board:
  285. ?SB54, SB55 OFF
  286. ?R35 removed
  287. ?SB16, SB50 ON */
  288. /* #define USE_HSE_BYPASS */
  289. #if defined (USE_HSE_BYPASS)
  290. #define HSE_BYPASS_INPUT_FREQUENCY 8000000
  291. #endif /* USE_HSE_BYPASS */
  292. #endif /* STM32F411xE */
  293. /*!< Uncomment the following line if you need to relocate your vector Table in
  294. Internal SRAM. */
  295. /* #define VECT_TAB_SRAM */
  296. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  297. This value must be a multiple of 0x200. */
  298. /******************************************************************************/
  299. /************************* PLL Parameters *************************************/
  300. #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || defined (STM32F401xx)
  301. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
  302. #define PLL_M 8
  303. #else /* STM32F411xE */
  304. #if defined (USE_HSE_BYPASS)
  305. #define PLL_M 8
  306. #else /* STM32F411xE */
  307. #define PLL_M 16
  308. #endif /* USE_HSE_BYPASS */
  309. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */
  310. /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
  311. #define PLL_Q 7
  312. #if defined (STM32F40_41xxx)
  313. #define PLL_N 336
  314. /* SYSCLK = PLL_VCO / PLL_P */
  315. #define PLL_P 2
  316. #endif /* STM32F40_41xxx */
  317. #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
  318. #define PLL_N 360
  319. /* SYSCLK = PLL_VCO / PLL_P */
  320. #define PLL_P 2
  321. #endif /* STM32F427_437x || STM32F429_439xx */
  322. #if defined (STM32F401xx)
  323. #define PLL_N 336
  324. /* SYSCLK = PLL_VCO / PLL_P */
  325. #define PLL_P 4
  326. #endif /* STM32F401xx */
  327. #if defined (STM32F411xE)
  328. #define PLL_N 400
  329. /* SYSCLK = PLL_VCO / PLL_P */
  330. #define PLL_P 4
  331. #endif /* STM32F411xx */
  332. /******************************************************************************/
  333. /**
  334. * @}
  335. */
  336. /** @addtogroup STM32F4xx_System_Private_Macros
  337. * @{
  338. */
  339. /**
  340. * @}
  341. */
  342. /** @addtogroup STM32F4xx_System_Private_Variables
  343. * @{
  344. */
  345. #if defined (STM32F40_41xxx)
  346. uint32_t SystemCoreClock = 168000000;
  347. #endif /* STM32F40_41xxx */
  348. #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
  349. uint32_t SystemCoreClock = 180000000;
  350. #endif /* STM32F427_437x || STM32F429_439xx */
  351. #if defined (STM32F401xx)
  352. uint32_t SystemCoreClock = 84000000;
  353. #endif /* STM32F401xx */
  354. #if defined (STM32F411xE)
  355. uint32_t SystemCoreClock = 100000000;
  356. #endif /* STM32F401xx */
  357. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  358. /**
  359. * @}
  360. */
  361. /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
  362. * @{
  363. */
  364. static void SetSysClock(void);
  365. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  366. static void SystemInit_ExtMemCtl(void);
  367. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  368. /**
  369. * @}
  370. */
  371. /** @addtogroup STM32F4xx_System_Private_Functions
  372. * @{
  373. */
  374. /**
  375. * @brief Setup the microcontroller system
  376. * Initialize the Embedded Flash Interface, the PLL and update the
  377. * SystemFrequency variable.
  378. * @param None
  379. * @retval None
  380. */
  381. void SystemInit(void)
  382. {
  383. /* FPU settings ------------------------------------------------------------*/
  384. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  385. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  386. #endif
  387. /* Reset the RCC clock configuration to the default reset state ------------*/
  388. /* Set HSION bit */
  389. RCC->CR |= (uint32_t)0x00000001;
  390. /* Reset CFGR register */
  391. RCC->CFGR = 0x00000000;
  392. /* Reset HSEON, CSSON and PLLON bits */
  393. RCC->CR &= (uint32_t)0xFEF6FFFF;
  394. /* Reset PLLCFGR register */
  395. RCC->PLLCFGR = 0x24003010;
  396. /* Reset HSEBYP bit */
  397. RCC->CR &= (uint32_t)0xFFFBFFFF;
  398. /* Disable all interrupts */
  399. RCC->CIR = 0x00000000;
  400. #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  401. SystemInit_ExtMemCtl();
  402. #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
  403. /* Configure the System clock source, PLL Multiplier and Divider factors,
  404. AHB/APBx prescalers and Flash settings ----------------------------------*/
  405. SetSysClock();
  406. /* Configure the Vector Table location add offset address ------------------*/
  407. #ifdef VECT_TAB_SRAM
  408. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  409. #else
  410. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  411. #endif
  412. }
  413. /**
  414. * @brief Update SystemCoreClock variable according to Clock Register Values.
  415. * The SystemCoreClock variable contains the core clock (HCLK), it can
  416. * be used by the user application to setup the SysTick timer or configure
  417. * other parameters.
  418. *
  419. * @note Each time the core clock (HCLK) changes, this function must be called
  420. * to update SystemCoreClock variable value. Otherwise, any configuration
  421. * based on this variable will be incorrect.
  422. *
  423. * @note - The system frequency computed by this function is not the real
  424. * frequency in the chip. It is calculated based on the predefined
  425. * constant and the selected clock source:
  426. *
  427. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  428. *
  429. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  430. *
  431. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  432. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  433. *
  434. * (*) HSI_VALUE is a constant defined in stm32f4xx.h file (default value
  435. * 16 MHz) but the real value may vary depending on the variations
  436. * in voltage and temperature.
  437. *
  438. * (**) HSE_VALUE is a constant defined in stm32f4xx.h file (default value
  439. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  440. * frequency of the crystal used. Otherwise, this function may
  441. * have wrong result.
  442. *
  443. * - The result of this function could be not correct when using fractional
  444. * value for HSE crystal.
  445. *
  446. * @param None
  447. * @retval None
  448. */
  449. void SystemCoreClockUpdate(void)
  450. {
  451. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  452. /* Get SYSCLK source -------------------------------------------------------*/
  453. tmp = RCC->CFGR & RCC_CFGR_SWS;
  454. switch (tmp)
  455. {
  456. case 0x00: /* HSI used as system clock source */
  457. SystemCoreClock = HSI_VALUE;
  458. break;
  459. case 0x04: /* HSE used as system clock source */
  460. SystemCoreClock = HSE_VALUE;
  461. break;
  462. case 0x08: /* PLL used as system clock source */
  463. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  464. SYSCLK = PLL_VCO / PLL_P
  465. */
  466. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  467. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  468. #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || defined (STM32F401xx)
  469. if (pllsource != 0)
  470. {
  471. /* HSE used as PLL clock source */
  472. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  473. }
  474. else
  475. {
  476. /* HSI used as PLL clock source */
  477. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  478. }
  479. #elif defined (STM32F411xE)
  480. #if defined (USE_HSE_BYPASS)
  481. if (pllsource != 0)
  482. {
  483. /* HSE used as PLL clock source */
  484. pllvco = (HSE_BYPASS_INPUT_FREQUENCY / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  485. }
  486. #else
  487. if (pllsource == 0)
  488. {
  489. /* HSI used as PLL clock source */
  490. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  491. }
  492. #endif /* USE_HSE_BYPASS */
  493. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */
  494. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  495. SystemCoreClock = pllvco/pllp;
  496. break;
  497. default:
  498. SystemCoreClock = HSI_VALUE;
  499. break;
  500. }
  501. /* Compute HCLK frequency --------------------------------------------------*/
  502. /* Get HCLK prescaler */
  503. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  504. /* HCLK frequency */
  505. SystemCoreClock >>= tmp;
  506. }
  507. /**
  508. * @brief Configures the System clock source, PLL Multiplier and Divider factors,
  509. * AHB/APBx prescalers and Flash settings
  510. * @Note This function should be called only once the RCC clock configuration
  511. * is reset to the default reset state (done in SystemInit() function).
  512. * @param None
  513. * @retval None
  514. */
  515. static void SetSysClock(void)
  516. {
  517. #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || defined (STM32F401xx)
  518. /******************************************************************************/
  519. /* PLL (clocked by HSE) used as System clock source */
  520. /******************************************************************************/
  521. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  522. /* Enable HSE */
  523. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  524. /* Wait till HSE is ready and if Time out is reached exit */
  525. do
  526. {
  527. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  528. StartUpCounter++;
  529. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  530. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  531. {
  532. HSEStatus = (uint32_t)0x01;
  533. }
  534. else
  535. {
  536. HSEStatus = (uint32_t)0x00;
  537. }
  538. if (HSEStatus == (uint32_t)0x01)
  539. {
  540. /* Select regulator voltage output Scale 1 mode */
  541. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  542. PWR->CR |= PWR_CR_VOS;
  543. /* HCLK = SYSCLK / 1*/
  544. RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  545. #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx)
  546. /* PCLK2 = HCLK / 2*/
  547. RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
  548. /* PCLK1 = HCLK / 4*/
  549. RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
  550. #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx */
  551. #if defined (STM32F401xx)
  552. /* PCLK2 = HCLK / 2*/
  553. RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
  554. /* PCLK1 = HCLK / 4*/
  555. RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
  556. #endif /* STM32F401xx */
  557. /* Configure the main PLL */
  558. RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
  559. (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
  560. /* Enable the main PLL */
  561. RCC->CR |= RCC_CR_PLLON;
  562. /* Wait till the main PLL is ready */
  563. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  564. {
  565. }
  566. #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
  567. /* Enable the Over-drive to extend the clock frequency to 180 Mhz */
  568. PWR->CR |= PWR_CR_ODEN;
  569. while((PWR->CSR & PWR_CSR_ODRDY) == 0)
  570. {
  571. }
  572. PWR->CR |= PWR_CR_ODSWEN;
  573. while((PWR->CSR & PWR_CSR_ODSWRDY) == 0)
  574. {
  575. }
  576. /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  577. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
  578. #endif /* STM32F427_437x || STM32F429_439xx */
  579. #if defined (STM32F40_41xxx)
  580. /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  581. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
  582. #endif /* STM32F40_41xxx */
  583. #if defined (STM32F401xx)
  584. /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  585. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
  586. #endif /* STM32F401xx */
  587. /* Select the main PLL as system clock source */
  588. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  589. RCC->CFGR |= RCC_CFGR_SW_PLL;
  590. /* Wait till the main PLL is used as system clock source */
  591. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL)
  592. {
  593. }
  594. }
  595. else
  596. { /* If HSE fails to start-up, the application will have wrong clock
  597. configuration. User can add here some code to deal with this error */
  598. }
  599. #elif defined (STM32F411xE)
  600. #if defined (USE_HSE_BYPASS)
  601. /******************************************************************************/
  602. /* PLL (clocked by HSE) used as System clock source */
  603. /******************************************************************************/
  604. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  605. /* Enable HSE and HSE BYPASS */
  606. RCC->CR |= ((uint32_t)RCC_CR_HSEON | RCC_CR_HSEBYP);
  607. /* Wait till HSE is ready and if Time out is reached exit */
  608. do
  609. {
  610. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  611. StartUpCounter++;
  612. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  613. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  614. {
  615. HSEStatus = (uint32_t)0x01;
  616. }
  617. else
  618. {
  619. HSEStatus = (uint32_t)0x00;
  620. }
  621. if (HSEStatus == (uint32_t)0x01)
  622. {
  623. /* Select regulator voltage output Scale 1 mode */
  624. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  625. PWR->CR |= PWR_CR_VOS;
  626. /* HCLK = SYSCLK / 1*/
  627. RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  628. /* PCLK2 = HCLK / 2*/
  629. RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
  630. /* PCLK1 = HCLK / 4*/
  631. RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
  632. /* Configure the main PLL */
  633. RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) |
  634. (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);
  635. /* Enable the main PLL */
  636. RCC->CR |= RCC_CR_PLLON;
  637. /* Wait till the main PLL is ready */
  638. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  639. {
  640. }
  641. /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  642. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
  643. /* Select the main PLL as system clock source */
  644. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  645. RCC->CFGR |= RCC_CFGR_SW_PLL;
  646. /* Wait till the main PLL is used as system clock source */
  647. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
  648. {
  649. }
  650. }
  651. else
  652. { /* If HSE fails to start-up, the application will have wrong clock
  653. configuration. User can add here some code to deal with this error */
  654. }
  655. #else /* HSI will be used as PLL clock source */
  656. /* Select regulator voltage output Scale 1 mode */
  657. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  658. PWR->CR |= PWR_CR_VOS;
  659. /* HCLK = SYSCLK / 1*/
  660. RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  661. /* PCLK2 = HCLK / 2*/
  662. RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;
  663. /* PCLK1 = HCLK / 4*/
  664. RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
  665. /* Configure the main PLL */
  666. RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | (PLL_Q << 24);
  667. /* Enable the main PLL */
  668. RCC->CR |= RCC_CR_PLLON;
  669. /* Wait till the main PLL is ready */
  670. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  671. {
  672. }
  673. /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  674. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_2WS;
  675. /* Select the main PLL as system clock source */
  676. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  677. RCC->CFGR |= RCC_CFGR_SW_PLL;
  678. /* Wait till the main PLL is used as system clock source */
  679. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL);
  680. {
  681. }
  682. #endif /* USE_HSE_BYPASS */
  683. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */
  684. }
  685. /**
  686. * @brief Setup the external memory controller. Called in startup_stm32f4xx.s
  687. * before jump to __main
  688. * @param None
  689. * @retval None
  690. */
  691. #ifdef DATA_IN_ExtSRAM
  692. /**
  693. * @brief Setup the external memory controller.
  694. * Called in startup_stm32f4xx.s before jump to main.
  695. * This function configures the external SRAM mounted on STM324xG_EVAL/STM324x7I boards
  696. * This SRAM will be used as program data memory (including heap and stack).
  697. * @param None
  698. * @retval None
  699. */
  700. void SystemInit_ExtMemCtl(void)
  701. {
  702. /*-- GPIOs Configuration -----------------------------------------------------*/
  703. /*
  704. +-------------------+--------------------+------------------+--------------+
  705. + SRAM pins assignment +
  706. +-------------------+--------------------+------------------+--------------+
  707. | PD0 <-> FMC_D2 | PE0 <-> FMC_NBL0 | PF0 <-> FMC_A0 | PG0 <-> FMC_A10 |
  708. | PD1 <-> FMC_D3 | PE1 <-> FMC_NBL1 | PF1 <-> FMC_A1 | PG1 <-> FMC_A11 |
  709. | PD4 <-> FMC_NOE | PE3 <-> FMC_A19 | PF2 <-> FMC_A2 | PG2 <-> FMC_A12 |
  710. | PD5 <-> FMC_NWE | PE4 <-> FMC_A20 | PF3 <-> FMC_A3 | PG3 <-> FMC_A13 |
  711. | PD8 <-> FMC_D13 | PE7 <-> FMC_D4 | PF4 <-> FMC_A4 | PG4 <-> FMC_A14 |
  712. | PD9 <-> FMC_D14 | PE8 <-> FMC_D5 | PF5 <-> FMC_A5 | PG5 <-> FMC_A15 |
  713. | PD10 <-> FMC_D15 | PE9 <-> FMC_D6 | PF12 <-> FMC_A6 | PG9 <-> FMC_NE2 |
  714. | PD11 <-> FMC_A16 | PE10 <-> FMC_D7 | PF13 <-> FMC_A7 |-----------------+
  715. | PD12 <-> FMC_A17 | PE11 <-> FMC_D8 | PF14 <-> FMC_A8 |
  716. | PD13 <-> FMC_A18 | PE12 <-> FMC_D9 | PF15 <-> FMC_A9 |
  717. | PD14 <-> FMC_D0 | PE13 <-> FMC_D10 |-----------------+
  718. | PD15 <-> FMC_D1 | PE14 <-> FMC_D11 |
  719. | | PE15 <-> FMC_D12 |
  720. +------------------+------------------+
  721. */
  722. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  723. RCC->AHB1ENR |= 0x00000078;
  724. /* Connect PDx pins to FMC Alternate function */
  725. GPIOD->AFR[0] = 0x00cc00cc;
  726. GPIOD->AFR[1] = 0xcccccccc;
  727. /* Configure PDx pins in Alternate function mode */
  728. GPIOD->MODER = 0xaaaa0a0a;
  729. /* Configure PDx pins speed to 100 MHz */
  730. GPIOD->OSPEEDR = 0xffff0f0f;
  731. /* Configure PDx pins Output type to push-pull */
  732. GPIOD->OTYPER = 0x00000000;
  733. /* No pull-up, pull-down for PDx pins */
  734. GPIOD->PUPDR = 0x00000000;
  735. /* Connect PEx pins to FMC Alternate function */
  736. GPIOE->AFR[0] = 0xcccccccc;
  737. GPIOE->AFR[1] = 0xcccccccc;
  738. /* Configure PEx pins in Alternate function mode */
  739. GPIOE->MODER = 0xaaaaaaaa;
  740. /* Configure PEx pins speed to 100 MHz */
  741. GPIOE->OSPEEDR = 0xffffffff;
  742. /* Configure PEx pins Output type to push-pull */
  743. GPIOE->OTYPER = 0x00000000;
  744. /* No pull-up, pull-down for PEx pins */
  745. GPIOE->PUPDR = 0x00000000;
  746. /* Connect PFx pins to FMC Alternate function */
  747. GPIOF->AFR[0] = 0x00cccccc;
  748. GPIOF->AFR[1] = 0xcccc0000;
  749. /* Configure PFx pins in Alternate function mode */
  750. GPIOF->MODER = 0xaa000aaa;
  751. /* Configure PFx pins speed to 100 MHz */
  752. GPIOF->OSPEEDR = 0xff000fff;
  753. /* Configure PFx pins Output type to push-pull */
  754. GPIOF->OTYPER = 0x00000000;
  755. /* No pull-up, pull-down for PFx pins */
  756. GPIOF->PUPDR = 0x00000000;
  757. /* Connect PGx pins to FMC Alternate function */
  758. GPIOG->AFR[0] = 0x00cccccc;
  759. GPIOG->AFR[1] = 0x000000c0;
  760. /* Configure PGx pins in Alternate function mode */
  761. GPIOG->MODER = 0x00080aaa;
  762. /* Configure PGx pins speed to 100 MHz */
  763. GPIOG->OSPEEDR = 0x000c0fff;
  764. /* Configure PGx pins Output type to push-pull */
  765. GPIOG->OTYPER = 0x00000000;
  766. /* No pull-up, pull-down for PGx pins */
  767. GPIOG->PUPDR = 0x00000000;
  768. /*-- FMC Configuration ------------------------------------------------------*/
  769. /* Enable the FMC/FSMC interface clock */
  770. RCC->AHB3ENR |= 0x00000001;
  771. #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
  772. /* Configure and enable Bank1_SRAM2 */
  773. FMC_Bank1->BTCR[2] = 0x00001011;
  774. FMC_Bank1->BTCR[3] = 0x00000201;
  775. FMC_Bank1E->BWTR[2] = 0x0fffffff;
  776. #endif /* STM32F427_437xx || STM32F429_439xx */
  777. #if defined (STM32F40_41xxx)
  778. /* Configure and enable Bank1_SRAM2 */
  779. FSMC_Bank1->BTCR[2] = 0x00001011;
  780. FSMC_Bank1->BTCR[3] = 0x00000201;
  781. FSMC_Bank1E->BWTR[2] = 0x0fffffff;
  782. #endif /* STM32F40_41xxx */
  783. /*
  784. Bank1_SRAM2 is configured as follow:
  785. In case of FSMC configuration
  786. NORSRAMTimingStructure.FSMC_AddressSetupTime = 1;
  787. NORSRAMTimingStructure.FSMC_AddressHoldTime = 0;
  788. NORSRAMTimingStructure.FSMC_DataSetupTime = 2;
  789. NORSRAMTimingStructure.FSMC_BusTurnAroundDuration = 0;
  790. NORSRAMTimingStructure.FSMC_CLKDivision = 0;
  791. NORSRAMTimingStructure.FSMC_DataLatency = 0;
  792. NORSRAMTimingStructure.FSMC_AccessMode = FMC_AccessMode_A;
  793. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
  794. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  795. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  796. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  797. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  798. FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  799. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  800. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  801. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  802. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  803. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  804. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  805. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  806. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
  807. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &NORSRAMTimingStructure;
  808. In case of FMC configuration
  809. NORSRAMTimingStructure.FMC_AddressSetupTime = 1;
  810. NORSRAMTimingStructure.FMC_AddressHoldTime = 0;
  811. NORSRAMTimingStructure.FMC_DataSetupTime = 2;
  812. NORSRAMTimingStructure.FMC_BusTurnAroundDuration = 0;
  813. NORSRAMTimingStructure.FMC_CLKDivision = 0;
  814. NORSRAMTimingStructure.FMC_DataLatency = 0;
  815. NORSRAMTimingStructure.FMC_AccessMode = FMC_AccessMode_A;
  816. FMC_NORSRAMInitStructure.FMC_Bank = FMC_Bank1_NORSRAM2;
  817. FMC_NORSRAMInitStructure.FMC_DataAddressMux = FMC_DataAddressMux_Disable;
  818. FMC_NORSRAMInitStructure.FMC_MemoryType = FMC_MemoryType_SRAM;
  819. FMC_NORSRAMInitStructure.FMC_MemoryDataWidth = FMC_MemoryDataWidth_16b;
  820. FMC_NORSRAMInitStructure.FMC_BurstAccessMode = FMC_BurstAccessMode_Disable;
  821. FMC_NORSRAMInitStructure.FMC_AsynchronousWait = FMC_AsynchronousWait_Disable;
  822. FMC_NORSRAMInitStructure.FMC_WaitSignalPolarity = FMC_WaitSignalPolarity_Low;
  823. FMC_NORSRAMInitStructure.FMC_WrapMode = FMC_WrapMode_Disable;
  824. FMC_NORSRAMInitStructure.FMC_WaitSignalActive = FMC_WaitSignalActive_BeforeWaitState;
  825. FMC_NORSRAMInitStructure.FMC_WriteOperation = FMC_WriteOperation_Enable;
  826. FMC_NORSRAMInitStructure.FMC_WaitSignal = FMC_WaitSignal_Disable;
  827. FMC_NORSRAMInitStructure.FMC_ExtendedMode = FMC_ExtendedMode_Disable;
  828. FMC_NORSRAMInitStructure.FMC_WriteBurst = FMC_WriteBurst_Disable;
  829. FMC_NORSRAMInitStructure.FMC_ContinousClock = FMC_CClock_SyncOnly;
  830. FMC_NORSRAMInitStructure.FMC_ReadWriteTimingStruct = &NORSRAMTimingStructure;
  831. FMC_NORSRAMInitStructure.FMC_WriteTimingStruct = &NORSRAMTimingStructure;
  832. */
  833. }
  834. #endif /* DATA_IN_ExtSRAM */
  835. #ifdef DATA_IN_ExtSDRAM
  836. /**
  837. * @brief Setup the external memory controller.
  838. * Called in startup_stm32f4xx.s before jump to main.
  839. * This function configures the external SDRAM mounted on STM324x9I_EVAL board
  840. * This SDRAM will be used as program data memory (including heap and stack).
  841. * @param None
  842. * @retval None
  843. */
  844. void SystemInit_ExtMemCtl(void)
  845. {
  846. register uint32_t tmpreg = 0, timeout = 0xFFFF;
  847. register uint32_t index;
  848. /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface
  849. clock */
  850. RCC->AHB1ENR |= 0x000001FC;
  851. /* Connect PCx pins to FMC Alternate function */
  852. GPIOC->AFR[0] = 0x0000000c;
  853. GPIOC->AFR[1] = 0x00007700;
  854. /* Configure PCx pins in Alternate function mode */
  855. GPIOC->MODER = 0x00a00002;
  856. /* Configure PCx pins speed to 50 MHz */
  857. GPIOC->OSPEEDR = 0x00a00002;
  858. /* Configure PCx pins Output type to push-pull */
  859. GPIOC->OTYPER = 0x00000000;
  860. /* No pull-up, pull-down for PCx pins */
  861. GPIOC->PUPDR = 0x00500000;
  862. /* Connect PDx pins to FMC Alternate function */
  863. GPIOD->AFR[0] = 0x000000CC;
  864. GPIOD->AFR[1] = 0xCC000CCC;
  865. /* Configure PDx pins in Alternate function mode */
  866. GPIOD->MODER = 0xA02A000A;
  867. /* Configure PDx pins speed to 50 MHz */
  868. GPIOD->OSPEEDR = 0xA02A000A;
  869. /* Configure PDx pins Output type to push-pull */
  870. GPIOD->OTYPER = 0x00000000;
  871. /* No pull-up, pull-down for PDx pins */
  872. GPIOD->PUPDR = 0x00000000;
  873. /* Connect PEx pins to FMC Alternate function */
  874. GPIOE->AFR[0] = 0xC00000CC;
  875. GPIOE->AFR[1] = 0xCCCCCCCC;
  876. /* Configure PEx pins in Alternate function mode */
  877. GPIOE->MODER = 0xAAAA800A;
  878. /* Configure PEx pins speed to 50 MHz */
  879. GPIOE->OSPEEDR = 0xAAAA800A;
  880. /* Configure PEx pins Output type to push-pull */
  881. GPIOE->OTYPER = 0x00000000;
  882. /* No pull-up, pull-down for PEx pins */
  883. GPIOE->PUPDR = 0x00000000;
  884. /* Connect PFx pins to FMC Alternate function */
  885. GPIOF->AFR[0] = 0xcccccccc;
  886. GPIOF->AFR[1] = 0xcccccccc;
  887. /* Configure PFx pins in Alternate function mode */
  888. GPIOF->MODER = 0xAA800AAA;
  889. /* Configure PFx pins speed to 50 MHz */
  890. GPIOF->OSPEEDR = 0xAA800AAA;
  891. /* Configure PFx pins Output type to push-pull */
  892. GPIOF->OTYPER = 0x00000000;
  893. /* No pull-up, pull-down for PFx pins */
  894. GPIOF->PUPDR = 0x00000000;
  895. /* Connect PGx pins to FMC Alternate function */
  896. GPIOG->AFR[0] = 0xcccccccc;
  897. GPIOG->AFR[1] = 0xcccccccc;
  898. /* Configure PGx pins in Alternate function mode */
  899. GPIOG->MODER = 0xaaaaaaaa;
  900. /* Configure PGx pins speed to 50 MHz */
  901. GPIOG->OSPEEDR = 0xaaaaaaaa;
  902. /* Configure PGx pins Output type to push-pull */
  903. GPIOG->OTYPER = 0x00000000;
  904. /* No pull-up, pull-down for PGx pins */
  905. GPIOG->PUPDR = 0x00000000;
  906. /* Connect PHx pins to FMC Alternate function */
  907. GPIOH->AFR[0] = 0x00C0CC00;
  908. GPIOH->AFR[1] = 0xCCCCCCCC;
  909. /* Configure PHx pins in Alternate function mode */
  910. GPIOH->MODER = 0xAAAA08A0;
  911. /* Configure PHx pins speed to 50 MHz */
  912. GPIOH->OSPEEDR = 0xAAAA08A0;
  913. /* Configure PHx pins Output type to push-pull */
  914. GPIOH->OTYPER = 0x00000000;
  915. /* No pull-up, pull-down for PHx pins */
  916. GPIOH->PUPDR = 0x00000000;
  917. /* Connect PIx pins to FMC Alternate function */
  918. GPIOI->AFR[0] = 0xCCCCCCCC;
  919. GPIOI->AFR[1] = 0x00000CC0;
  920. /* Configure PIx pins in Alternate function mode */
  921. GPIOI->MODER = 0x0028AAAA;
  922. /* Configure PIx pins speed to 50 MHz */
  923. GPIOI->OSPEEDR = 0x0028AAAA;
  924. /* Configure PIx pins Output type to push-pull */
  925. GPIOI->OTYPER = 0x00000000;
  926. /* No pull-up, pull-down for PIx pins */
  927. GPIOI->PUPDR = 0x00000000;
  928. /*-- FMC Configuration ------------------------------------------------------*/
  929. /* Enable the FMC interface clock */
  930. RCC->AHB3ENR |= 0x00000001;
  931. /* Configure and enable SDRAM bank1 */
  932. FMC_Bank5_6->SDCR[0] = 0x000039D0;
  933. FMC_Bank5_6->SDTR[0] = 0x01115351;
  934. /* SDRAM initialization sequence */
  935. /* Clock enable command */
  936. FMC_Bank5_6->SDCMR = 0x00000011;
  937. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  938. while((tmpreg != 0) & (timeout-- > 0))
  939. {
  940. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  941. }
  942. /* Delay */
  943. for (index = 0; index<1000; index++);
  944. /* PALL command */
  945. FMC_Bank5_6->SDCMR = 0x00000012;
  946. timeout = 0xFFFF;
  947. while((tmpreg != 0) & (timeout-- > 0))
  948. {
  949. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  950. }
  951. /* Auto refresh command */
  952. FMC_Bank5_6->SDCMR = 0x00000073;
  953. timeout = 0xFFFF;
  954. while((tmpreg != 0) & (timeout-- > 0))
  955. {
  956. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  957. }
  958. /* MRD register program */
  959. FMC_Bank5_6->SDCMR = 0x00046014;
  960. timeout = 0xFFFF;
  961. while((tmpreg != 0) & (timeout-- > 0))
  962. {
  963. tmpreg = FMC_Bank5_6->SDSR & 0x00000020;
  964. }
  965. /* Set refresh count */
  966. tmpreg = FMC_Bank5_6->SDRTR;
  967. FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1));
  968. /* Disable write protection */
  969. tmpreg = FMC_Bank5_6->SDCR[0];
  970. FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF);
  971. /*
  972. Bank1_SDRAM is configured as follow:
  973. FMC_SDRAMTimingInitStructure.FMC_LoadToActiveDelay = 2;
  974. FMC_SDRAMTimingInitStructure.FMC_ExitSelfRefreshDelay = 6;
  975. FMC_SDRAMTimingInitStructure.FMC_SelfRefreshTime = 4;
  976. FMC_SDRAMTimingInitStructure.FMC_RowCycleDelay = 6;
  977. FMC_SDRAMTimingInitStructure.FMC_WriteRecoveryTime = 2;
  978. FMC_SDRAMTimingInitStructure.FMC_RPDelay = 2;
  979. FMC_SDRAMTimingInitStructure.FMC_RCDDelay = 2;
  980. FMC_SDRAMInitStructure.FMC_Bank = SDRAM_BANK;
  981. FMC_SDRAMInitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
  982. FMC_SDRAMInitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_11b;
  983. FMC_SDRAMInitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
  984. FMC_SDRAMInitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
  985. FMC_SDRAMInitStructure.FMC_CASLatency = FMC_CAS_Latency_3;
  986. FMC_SDRAMInitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
  987. FMC_SDRAMInitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
  988. FMC_SDRAMInitStructure.FMC_ReadBurst = FMC_Read_Burst_disable;
  989. FMC_SDRAMInitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
  990. FMC_SDRAMInitStructure.FMC_SDRAMTimingStruct = &FMC_SDRAMTimingInitStructure;
  991. */
  992. }
  993. #endif /* DATA_IN_ExtSDRAM */
  994. /**
  995. * @}
  996. */
  997. /**
  998. * @}
  999. */
  1000. /**
  1001. * @}
  1002. */
  1003. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/