A bundled STM32F10x Std Periph and CMSIS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

467 lines
16 KiB

  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f10x.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  8. *
  9. * 1. This file provides two functions and one global variable to be called from
  10. * user application:
  11. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  12. * factors, AHB/APBx prescalers and Flash settings).
  13. * This function is called at startup just after reset and
  14. * before branch to main program. This call is made inside
  15. * the "startup_stm32f10x_xx.s" file.
  16. *
  17. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  18. * by the user application to setup the SysTick
  19. * timer or configure other parameters.
  20. *
  21. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  22. * be called whenever the core clock is changed
  23. * during program execution.
  24. *
  25. * 2. After each device reset the HSI (8 MHz) is used as system clock source.
  26. * Then SystemInit() function is called, in "startup_stm32f10x_xx.s" file, to
  27. * configure the system clock before to branch to main program.
  28. *
  29. * 3. If the system clock source selected by user fails to startup, the SystemInit()
  30. * function will do nothing and HSI still used as system clock source. User can
  31. * add some code to deal with this issue inside the SetSysClock() function.
  32. *
  33. * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depedning on
  34. * the product used), refer to "HSE_VALUE" define in "stm32f10x.h" file.
  35. * When HSE is used as system clock source, directly or through PLL, and you
  36. * are using different crystal you have to adapt the HSE value to your own
  37. * configuration.
  38. *
  39. ******************************************************************************
  40. * @attention
  41. *
  42. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  43. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  44. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  45. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  46. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  47. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  48. *
  49. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  50. ******************************************************************************
  51. */
  52. /** @addtogroup CMSIS
  53. * @{
  54. */
  55. /** @addtogroup stm32f10x_system
  56. * @{
  57. */
  58. /** @addtogroup STM32F10x_System_Private_Includes
  59. * @{
  60. */
  61. #include "stm32f10x.h"
  62. /**
  63. * @}
  64. */
  65. /** @addtogroup STM32F10x_System_Private_TypesDefinitions
  66. * @{
  67. */
  68. /**
  69. * @}
  70. */
  71. /** @addtogroup STM32F10x_System_Private_Defines
  72. * @{
  73. */
  74. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  75. frequency (after reset the HSI is used as SYSCLK source)
  76. IMPORTANT NOTE:
  77. ==============
  78. 1. After each device reset the HSI is used as System clock source.
  79. 2. Please make sure that the selected System clock doesn't exceed your device's
  80. maximum frequency.
  81. 3. If none of the define below is enabled, the HSI is used as System clock
  82. source.
  83. 4. The System clock configuration functions provided within this file assume that:
  84. - For Low, Medium and High density Value line devices an external 8MHz
  85. crystal is used to drive the System clock.
  86. - For Low, Medium and High density devices an external 8MHz crystal is
  87. used to drive the System clock.
  88. - For Connectivity line devices an external 25MHz crystal is used to drive
  89. the System clock.
  90. If you are using different crystal you have to adapt those functions accordingly.
  91. */
  92. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  93. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  94. #define SYSCLK_FREQ_8MHz 8000000
  95. #else
  96. /* #define SYSCLK_FREQ_HSE HSE_VALUE */
  97. /* #define SYSCLK_FREQ_24MHz 24000000 */
  98. /* #define SYSCLK_FREQ_36MHz 36000000 */
  99. /* #define SYSCLK_FREQ_48MHz 48000000 */
  100. /* #define SYSCLK_FREQ_56MHz 56000000 */
  101. #define SYSCLK_FREQ_72MHz 72000000
  102. #endif
  103. /*!< Uncomment the following line if you need to use external SRAM mounted
  104. on STM3210E-EVAL board (STM32 High density and XL-density devices) or on
  105. STM32100E-EVAL board (STM32 High-density value line devices) as data memory */
  106. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  107. /* #define DATA_IN_ExtSRAM */
  108. #endif
  109. /*!< Uncomment the following line if you need to relocate your vector Table in
  110. Internal SRAM. */
  111. /* #define VECT_TAB_SRAM */
  112. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  113. This value must be a multiple of 0x200. */
  114. /**
  115. * @}
  116. */
  117. /** @addtogroup STM32F10x_System_Private_Macros
  118. * @{
  119. */
  120. /**
  121. * @}
  122. */
  123. /** @addtogroup STM32F10x_System_Private_Variables
  124. * @{
  125. */
  126. /*******************************************************************************
  127. * Clock Definitions
  128. *******************************************************************************/
  129. #ifdef SYSCLK_FREQ_HSE
  130. uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
  131. #elif defined SYSCLK_FREQ_8MHz
  132. uint32_t SystemCoreClock = SYSCLK_FREQ_8MHz; /*!< System Clock Frequency (Core Clock) */
  133. #elif defined SYSCLK_FREQ_36MHz
  134. uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
  135. #elif defined SYSCLK_FREQ_48MHz
  136. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
  137. #elif defined SYSCLK_FREQ_56MHz
  138. uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
  139. #elif defined SYSCLK_FREQ_72MHz
  140. uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
  141. #else /*!< HSI Selected as System Clock source */
  142. uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
  143. #endif
  144. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  145. /**
  146. * @}
  147. */
  148. /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
  149. * @{
  150. */
  151. #ifdef DATA_IN_ExtSRAM
  152. static void SystemInit_ExtMemCtl(void);
  153. #endif /* DATA_IN_ExtSRAM */
  154. /**
  155. * @}
  156. */
  157. /** @addtogroup STM32F10x_System_Private_Functions
  158. * @{
  159. */
  160. /**
  161. * @brief Setup the microcontroller system
  162. * Initialize the Embedded Flash Interface, the PLL and update the
  163. * SystemCoreClock variable.
  164. * @note This function should be used only after reset.
  165. * @param None
  166. * @retval None
  167. */
  168. void SystemInit (void)
  169. {
  170. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  171. /* Set HSION bit */
  172. RCC->CR |= (uint32_t)0x00000001;
  173. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  174. #ifndef STM32F10X_CL
  175. RCC->CFGR &= (uint32_t)0xF8FF0000;
  176. #else
  177. RCC->CFGR &= (uint32_t)0xF0FF0000;
  178. #endif /* STM32F10X_CL */
  179. /* Reset HSEON, CSSON and PLLON bits */
  180. RCC->CR &= (uint32_t)0xFEF6FFFF;
  181. /* Reset HSEBYP bit */
  182. RCC->CR &= (uint32_t)0xFFFBFFFF;
  183. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  184. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  185. #ifdef STM32F10X_CL
  186. /* Reset PLL2ON and PLL3ON bits */
  187. RCC->CR &= (uint32_t)0xEBFFFFFF;
  188. /* Disable all interrupts and clear pending bits */
  189. RCC->CIR = 0x00FF0000;
  190. /* Reset CFGR2 register */
  191. RCC->CFGR2 = 0x00000000;
  192. #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  193. /* Disable all interrupts and clear pending bits */
  194. RCC->CIR = 0x009F0000;
  195. /* Reset CFGR2 register */
  196. RCC->CFGR2 = 0x00000000;
  197. #else
  198. /* Disable all interrupts and clear pending bits */
  199. RCC->CIR = 0x009F0000;
  200. #endif /* STM32F10X_CL */
  201. #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
  202. #ifdef DATA_IN_ExtSRAM
  203. SystemInit_ExtMemCtl();
  204. #endif /* DATA_IN_ExtSRAM */
  205. #endif
  206. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  207. /* Configure the Flash Latency cycles and enable prefetch buffer */
  208. //SetSysClock();
  209. #ifdef VECT_TAB_SRAM
  210. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  211. #else
  212. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  213. #endif
  214. }
  215. /**
  216. * @brief Update SystemCoreClock variable according to Clock Register Values.
  217. * The SystemCoreClock variable contains the core clock (HCLK), it can
  218. * be used by the user application to setup the SysTick timer or configure
  219. * other parameters.
  220. *
  221. * @note Each time the core clock (HCLK) changes, this function must be called
  222. * to update SystemCoreClock variable value. Otherwise, any configuration
  223. * based on this variable will be incorrect.
  224. *
  225. * @note - The system frequency computed by this function is not the real
  226. * frequency in the chip. It is calculated based on the predefined
  227. * constant and the selected clock source:
  228. *
  229. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  230. *
  231. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  232. *
  233. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  234. * or HSI_VALUE(*) multiplied by the PLL factors.
  235. *
  236. * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
  237. * 8 MHz) but the real value may vary depending on the variations
  238. * in voltage and temperature.
  239. *
  240. * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
  241. * 8 MHz or 25 MHz, depedning on the product used), user has to ensure
  242. * that HSE_VALUE is same as the real frequency of the crystal used.
  243. * Otherwise, this function may have wrong result.
  244. *
  245. * - The result of this function could be not correct when using fractional
  246. * value for HSE crystal.
  247. * @param None
  248. * @retval None
  249. */
  250. void SystemCoreClockUpdate (void)
  251. {
  252. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  253. #ifdef STM32F10X_CL
  254. uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  255. #endif /* STM32F10X_CL */
  256. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  257. uint32_t prediv1factor = 0;
  258. #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */
  259. /* Get SYSCLK source -------------------------------------------------------*/
  260. tmp = RCC->CFGR & RCC_CFGR_SWS;
  261. switch (tmp)
  262. {
  263. case 0x00: /* HSI used as system clock */
  264. SystemCoreClock = HSI_VALUE;
  265. break;
  266. case 0x04: /* HSE used as system clock */
  267. SystemCoreClock = HSE_VALUE;
  268. break;
  269. case 0x08: /* PLL used as system clock */
  270. /* Get PLL clock source and multiplication factor ----------------------*/
  271. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  272. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  273. #ifndef STM32F10X_CL
  274. pllmull = ( pllmull >> 18) + 2;
  275. if (pllsource == 0x00)
  276. {
  277. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  278. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  279. }
  280. else
  281. {
  282. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  283. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  284. /* HSE oscillator clock selected as PREDIV1 clock entry */
  285. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  286. #else
  287. /* HSE selected as PLL clock entry */
  288. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  289. {/* HSE oscillator clock divided by 2 */
  290. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  291. }
  292. else
  293. {
  294. SystemCoreClock = HSE_VALUE * pllmull;
  295. }
  296. #endif
  297. }
  298. #else
  299. pllmull = pllmull >> 18;
  300. if (pllmull != 0x0D)
  301. {
  302. pllmull += 2;
  303. }
  304. else
  305. { /* PLL multiplication factor = PLL input clock * 6.5 */
  306. pllmull = 13 / 2;
  307. }
  308. if (pllsource == 0x00)
  309. {
  310. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  311. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  312. }
  313. else
  314. {/* PREDIV1 selected as PLL clock entry */
  315. /* Get PREDIV1 clock source and division factor */
  316. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  317. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  318. if (prediv1source == 0)
  319. {
  320. /* HSE oscillator clock selected as PREDIV1 clock entry */
  321. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  322. }
  323. else
  324. {/* PLL2 clock selected as PREDIV1 clock entry */
  325. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  326. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  327. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  328. SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  329. }
  330. }
  331. #endif /* STM32F10X_CL */
  332. break;
  333. default:
  334. SystemCoreClock = HSI_VALUE;
  335. break;
  336. }
  337. /* Compute HCLK clock frequency ----------------*/
  338. /* Get HCLK prescaler */
  339. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  340. /* HCLK clock frequency */
  341. SystemCoreClock >>= tmp;
  342. }
  343. /**
  344. * @brief Setup the external memory controller. Called in startup_stm32f10x.s
  345. * before jump to __main
  346. * @param None
  347. * @retval None
  348. */
  349. #ifdef DATA_IN_ExtSRAM
  350. /**
  351. * @brief Setup the external memory controller.
  352. * Called in startup_stm32f10x_xx.s/.c before jump to main.
  353. * This function configures the external SRAM mounted on STM3210E-EVAL
  354. * board (STM32 High density devices). This SRAM will be used as program
  355. * data memory (including heap and stack).
  356. * @param None
  357. * @retval None
  358. */
  359. void SystemInit_ExtMemCtl(void)
  360. {
  361. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  362. required, then adjust the Register Addresses */
  363. /* Enable FSMC clock */
  364. RCC->AHBENR = 0x00000114;
  365. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  366. RCC->APB2ENR = 0x000001E0;
  367. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  368. /*---------------- SRAM Address lines configuration -------------------------*/
  369. /*---------------- NOE and NWE configuration --------------------------------*/
  370. /*---------------- NE3 configuration ----------------------------------------*/
  371. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  372. GPIOD->CRL = 0x44BB44BB;
  373. GPIOD->CRH = 0xBBBBBBBB;
  374. GPIOE->CRL = 0xB44444BB;
  375. GPIOE->CRH = 0xBBBBBBBB;
  376. GPIOF->CRL = 0x44BBBBBB;
  377. GPIOF->CRH = 0xBBBB4444;
  378. GPIOG->CRL = 0x44BBBBBB;
  379. GPIOG->CRH = 0x44444B44;
  380. /*---------------- FSMC Configuration ---------------------------------------*/
  381. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  382. FSMC_Bank1->BTCR[4] = 0x00001011;
  383. FSMC_Bank1->BTCR[5] = 0x00000200;
  384. }
  385. #endif /* DATA_IN_ExtSRAM */
  386. /**
  387. * @}
  388. */
  389. /**
  390. * @}
  391. */
  392. /**
  393. * @}
  394. */
  395. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/