system_stm32f4xx.c 51 KB

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