system_stm32f1xx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f1xx.c
  4. * @author MCD Application Team
  5. * @version V4.2.0
  6. * @date 31-March-2017
  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_stm32f1xx_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_stm32f1xx_xx.s" file, to
  27. * configure the system clock before to branch to main program.
  28. *
  29. * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
  30. * the product used), refer to "HSE_VALUE".
  31. * When HSE is used as system clock source, directly or through PLL, and you
  32. * are using different crystal you have to adapt the HSE value to your own
  33. * configuration.
  34. *
  35. ******************************************************************************
  36. * @attention
  37. *
  38. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  39. *
  40. * Redistribution and use in source and binary forms, with or without modification,
  41. * are permitted provided that the following conditions are met:
  42. * 1. Redistributions of source code must retain the above copyright notice,
  43. * this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright notice,
  45. * this list of conditions and the following disclaimer in the documentation
  46. * and/or other materials provided with the distribution.
  47. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  48. * may be used to endorse or promote products derived from this software
  49. * without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  52. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  54. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  55. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  58. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  59. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. ******************************************************************************
  63. */
  64. /** @addtogroup CMSIS
  65. * @{
  66. */
  67. /** @addtogroup stm32f1xx_system
  68. * @{
  69. */
  70. /** @addtogroup STM32F1xx_System_Private_Includes
  71. * @{
  72. */
  73. #include "stm32f1xx.h"
  74. /**
  75. * @}
  76. */
  77. /** @addtogroup STM32F1xx_System_Private_TypesDefinitions
  78. * @{
  79. */
  80. /**
  81. * @}
  82. */
  83. /** @addtogroup STM32F1xx_System_Private_Defines
  84. * @{
  85. */
  86. #if !defined (HSE_VALUE)
  87. #define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz.
  88. This value can be provided and adapted by the user application. */
  89. #endif /* HSE_VALUE */
  90. #if !defined (HSI_VALUE)
  91. #define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz.
  92. This value can be provided and adapted by the user application. */
  93. #endif /* HSI_VALUE */
  94. /*!< Uncomment the following line if you need to use external SRAM */
  95. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  96. /* #define DATA_IN_ExtSRAM */
  97. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  98. /*!< Uncomment the following line if you need to relocate your vector Table in
  99. Internal SRAM. */
  100. /* #define VECT_TAB_SRAM */
  101. #define VECT_TAB_OFFSET 0x00000000U
  102. /*#define VECT_TAB_OFFSET 0x4000*//* 0x00000000U *//*!< Vector Table base offset field.
  103. This value must be a multiple of 0x200. */
  104. /**
  105. * @}
  106. */
  107. /** @addtogroup STM32F1xx_System_Private_Macros
  108. * @{
  109. */
  110. /**
  111. * @}
  112. */
  113. /** @addtogroup STM32F1xx_System_Private_Variables
  114. * @{
  115. */
  116. /*******************************************************************************
  117. * Clock Definitions
  118. *******************************************************************************/
  119. #if defined(STM32F100xB) ||defined(STM32F100xE)
  120. uint32_t SystemCoreClock = 24000000U; /*!< System Clock Frequency (Core Clock) */
  121. #else /*!< HSI Selected as System Clock source */
  122. uint32_t SystemCoreClock = 72000000U; /*!< System Clock Frequency (Core Clock) */
  123. #endif
  124. const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  125. const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4};
  126. /**
  127. * @}
  128. */
  129. /** @addtogroup STM32F1xx_System_Private_FunctionPrototypes
  130. * @{
  131. */
  132. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  133. #ifdef DATA_IN_ExtSRAM
  134. static void SystemInit_ExtMemCtl(void);
  135. #endif /* DATA_IN_ExtSRAM */
  136. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  137. /**
  138. * @}
  139. */
  140. /** @addtogroup STM32F1xx_System_Private_Functions
  141. * @{
  142. */
  143. /**
  144. * @brief Setup the microcontroller system
  145. * Initialize the Embedded Flash Interface, the PLL and update the
  146. * SystemCoreClock variable.
  147. * @note This function should be used only after reset.
  148. * @param None
  149. * @retval None
  150. */
  151. void SystemInit (void)
  152. {
  153. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  154. /* Set HSION bit */
  155. RCC->CR |= 0x00000001U;
  156. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  157. #if !defined(STM32F105xC) && !defined(STM32F107xC)
  158. RCC->CFGR &= 0xF8FF0000U;
  159. #else
  160. RCC->CFGR &= 0xF0FF0000U;
  161. #endif /* STM32F105xC */
  162. /* Reset HSEON, CSSON and PLLON bits */
  163. RCC->CR &= 0xFEF6FFFFU;
  164. /* Reset HSEBYP bit */
  165. RCC->CR &= 0xFFFBFFFFU;
  166. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  167. RCC->CFGR &= 0xFF80FFFFU;
  168. #if defined(STM32F105xC) || defined(STM32F107xC)
  169. /* Reset PLL2ON and PLL3ON bits */
  170. RCC->CR &= 0xEBFFFFFFU;
  171. /* Disable all interrupts and clear pending bits */
  172. RCC->CIR = 0x00FF0000U;
  173. /* Reset CFGR2 register */
  174. RCC->CFGR2 = 0x00000000U;
  175. #elif defined(STM32F100xB) || defined(STM32F100xE)
  176. /* Disable all interrupts and clear pending bits */
  177. RCC->CIR = 0x009F0000U;
  178. /* Reset CFGR2 register */
  179. RCC->CFGR2 = 0x00000000U;
  180. #else
  181. /* Disable all interrupts and clear pending bits */
  182. RCC->CIR = 0x009F0000U;
  183. #endif /* STM32F105xC */
  184. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  185. #ifdef DATA_IN_ExtSRAM
  186. SystemInit_ExtMemCtl();
  187. #endif /* DATA_IN_ExtSRAM */
  188. #endif
  189. #ifdef VECT_TAB_SRAM
  190. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  191. #else
  192. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  193. #endif
  194. }
  195. /**
  196. * @brief Update SystemCoreClock variable according to Clock Register Values.
  197. * The SystemCoreClock variable contains the core clock (HCLK), it can
  198. * be used by the user application to setup the SysTick timer or configure
  199. * other parameters.
  200. *
  201. * @note Each time the core clock (HCLK) changes, this function must be called
  202. * to update SystemCoreClock variable value. Otherwise, any configuration
  203. * based on this variable will be incorrect.
  204. *
  205. * @note - The system frequency computed by this function is not the real
  206. * frequency in the chip. It is calculated based on the predefined
  207. * constant and the selected clock source:
  208. *
  209. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  210. *
  211. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  212. *
  213. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  214. * or HSI_VALUE(*) multiplied by the PLL factors.
  215. *
  216. * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
  217. * 8 MHz) but the real value may vary depending on the variations
  218. * in voltage and temperature.
  219. *
  220. * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
  221. * 8 MHz or 25 MHz, depending on the product used), user has to ensure
  222. * that HSE_VALUE is same as the real frequency of the crystal used.
  223. * Otherwise, this function may have wrong result.
  224. *
  225. * - The result of this function could be not correct when using fractional
  226. * value for HSE crystal.
  227. * @param None
  228. * @retval None
  229. */
  230. void SystemCoreClockUpdate (void)
  231. {
  232. uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U;
  233. #if defined(STM32F105xC) || defined(STM32F107xC)
  234. uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U;
  235. #endif /* STM32F105xC */
  236. #if defined(STM32F100xB) || defined(STM32F100xE)
  237. uint32_t prediv1factor = 0U;
  238. #endif /* STM32F100xB or STM32F100xE */
  239. /* Get SYSCLK source -------------------------------------------------------*/
  240. tmp = RCC->CFGR & RCC_CFGR_SWS;
  241. switch (tmp)
  242. {
  243. case 0x00U: /* HSI used as system clock */
  244. SystemCoreClock = HSI_VALUE;
  245. break;
  246. case 0x04U: /* HSE used as system clock */
  247. SystemCoreClock = HSE_VALUE;
  248. break;
  249. case 0x08U: /* PLL used as system clock */
  250. /* Get PLL clock source and multiplication factor ----------------------*/
  251. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  252. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  253. #if !defined(STM32F105xC) && !defined(STM32F107xC)
  254. pllmull = ( pllmull >> 18U) + 2U;
  255. if (pllsource == 0x00U)
  256. {
  257. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  258. SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
  259. }
  260. else
  261. {
  262. #if defined(STM32F100xB) || defined(STM32F100xE)
  263. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
  264. /* HSE oscillator clock selected as PREDIV1 clock entry */
  265. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  266. #else
  267. /* HSE selected as PLL clock entry */
  268. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  269. {/* HSE oscillator clock divided by 2 */
  270. SystemCoreClock = (HSE_VALUE >> 1U) * pllmull;
  271. }
  272. else
  273. {
  274. SystemCoreClock = HSE_VALUE * pllmull;
  275. }
  276. #endif
  277. }
  278. #else
  279. pllmull = pllmull >> 18U;
  280. if (pllmull != 0x0DU)
  281. {
  282. pllmull += 2U;
  283. }
  284. else
  285. { /* PLL multiplication factor = PLL input clock * 6.5 */
  286. pllmull = 13U / 2U;
  287. }
  288. if (pllsource == 0x00U)
  289. {
  290. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  291. SystemCoreClock = (HSI_VALUE >> 1U) * pllmull;
  292. }
  293. else
  294. {/* PREDIV1 selected as PLL clock entry */
  295. /* Get PREDIV1 clock source and division factor */
  296. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  297. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U;
  298. if (prediv1source == 0U)
  299. {
  300. /* HSE oscillator clock selected as PREDIV1 clock entry */
  301. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  302. }
  303. else
  304. {/* PLL2 clock selected as PREDIV1 clock entry */
  305. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  306. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U;
  307. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U;
  308. SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  309. }
  310. }
  311. #endif /* STM32F105xC */
  312. break;
  313. default:
  314. SystemCoreClock = HSI_VALUE;
  315. break;
  316. }
  317. /* Compute HCLK clock frequency ----------------*/
  318. /* Get HCLK prescaler */
  319. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
  320. /* HCLK clock frequency */
  321. SystemCoreClock >>= tmp;
  322. }
  323. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  324. /**
  325. * @brief Setup the external memory controller. Called in startup_stm32f1xx.s
  326. * before jump to __main
  327. * @param None
  328. * @retval None
  329. */
  330. #ifdef DATA_IN_ExtSRAM
  331. /**
  332. * @brief Setup the external memory controller.
  333. * Called in startup_stm32f1xx_xx.s/.c before jump to main.
  334. * This function configures the external SRAM mounted on STM3210E-EVAL
  335. * board (STM32 High density devices). This SRAM will be used as program
  336. * data memory (including heap and stack).
  337. * @param None
  338. * @retval None
  339. */
  340. void SystemInit_ExtMemCtl(void)
  341. {
  342. __IO uint32_t tmpreg;
  343. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  344. required, then adjust the Register Addresses */
  345. /* Enable FSMC clock */
  346. RCC->AHBENR = 0x00000114U;
  347. /* Delay after an RCC peripheral clock enabling */
  348. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
  349. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  350. RCC->APB2ENR = 0x000001E0U;
  351. /* Delay after an RCC peripheral clock enabling */
  352. tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN);
  353. (void)(tmpreg);
  354. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  355. /*---------------- SRAM Address lines configuration -------------------------*/
  356. /*---------------- NOE and NWE configuration --------------------------------*/
  357. /*---------------- NE3 configuration ----------------------------------------*/
  358. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  359. GPIOD->CRL = 0x44BB44BBU;
  360. GPIOD->CRH = 0xBBBBBBBBU;
  361. GPIOE->CRL = 0xB44444BBU;
  362. GPIOE->CRH = 0xBBBBBBBBU;
  363. GPIOF->CRL = 0x44BBBBBBU;
  364. GPIOF->CRH = 0xBBBB4444U;
  365. GPIOG->CRL = 0x44BBBBBBU;
  366. GPIOG->CRH = 0x444B4B44U;
  367. /*---------------- FSMC Configuration ---------------------------------------*/
  368. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  369. FSMC_Bank1->BTCR[4U] = 0x00001091U;
  370. FSMC_Bank1->BTCR[5U] = 0x00110212U;
  371. }
  372. #endif /* DATA_IN_ExtSRAM */
  373. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  374. /**
  375. * @}
  376. */
  377. /**
  378. * @}
  379. */
  380. /**
  381. * @}
  382. */
  383. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/