Projet voilier 4IRA1 Arnaud Vergnet Marino Benassai Bastien Picco Yohan Simard
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.

system_stm32f1xx.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f1xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  6. *
  7. * 1. This file provides two functions and one global variable to be called from
  8. * user application:
  9. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  10. * factors, AHB/APBx prescalers and Flash settings).
  11. * This function is called at startup just after reset and
  12. * before branch to main program. This call is made inside
  13. * the "startup_stm32f1xx_xx.s" file.
  14. *
  15. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  16. * by the user application to setup the SysTick
  17. * timer or configure other parameters.
  18. *
  19. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  20. * be called whenever the core clock is changed
  21. * during program execution.
  22. *
  23. * 2. After each device reset the HSI (8 MHz) is used as system clock source.
  24. * Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to
  25. * configure the system clock before to branch to main program.
  26. *
  27. * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
  28. * the product used), refer to "HSE_VALUE".
  29. * When HSE is used as system clock source, directly or through PLL, and you
  30. * are using different crystal you have to adapt the HSE value to your own
  31. * configuration.
  32. *
  33. ******************************************************************************
  34. * @attention
  35. *
  36. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  37. * All rights reserved.</center></h2>
  38. *
  39. * This software component is licensed by ST under BSD 3-Clause license,
  40. * the "License"; You may not use this file except in compliance with the
  41. * License. You may obtain a copy of the License at:
  42. * opensource.org/licenses/BSD-3-Clause
  43. *
  44. ******************************************************************************
  45. */
  46. /** @addtogroup CMSIS
  47. * @{
  48. */
  49. /** @addtogroup stm32f1xx_system
  50. * @{
  51. */
  52. /** @addtogroup STM32F1xx_System_Private_Includes
  53. * @{
  54. */
  55. #include "stm32f1xx.h"
  56. /**
  57. * @}
  58. */
  59. /** @addtogroup STM32F1xx_System_Private_TypesDefinitions
  60. * @{
  61. */
  62. /**
  63. * @}
  64. */
  65. /** @addtogroup STM32F1xx_System_Private_Defines
  66. * @{
  67. */
  68. #if !defined (HSE_VALUE)
  69. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
  70. This value can be provided and adapted by the user application. */
  71. #endif /* HSE_VALUE */
  72. #if !defined (HSI_VALUE)
  73. #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
  74. This value can be provided and adapted by the user application. */
  75. #endif /* HSI_VALUE */
  76. /*!< Uncomment the following line if you need to use external SRAM */
  77. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  78. /* #define DATA_IN_ExtSRAM */
  79. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  80. /*!< Uncomment the following line if you need to relocate your vector Table in
  81. Internal SRAM. */
  82. /* #define VECT_TAB_SRAM */
  83. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  84. This value must be a multiple of 0x200. */
  85. /**
  86. * @}
  87. */
  88. /** @addtogroup STM32F1xx_System_Private_Macros
  89. * @{
  90. */
  91. /**
  92. * @}
  93. */
  94. /** @addtogroup STM32F1xx_System_Private_Variables
  95. * @{
  96. */
  97. /* This variable is updated in three ways:
  98. 1) by calling CMSIS function SystemCoreClockUpdate()
  99. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  100. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  101. Note: If you use this function to configure the system clock; then there
  102. is no need to call the 2 first functions listed above, since SystemCoreClock
  103. variable is updated automatically.
  104. */
  105. uint32_t SystemCoreClock = 16000000;
  106. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  107. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  108. /**
  109. * @}
  110. */
  111. /** @addtogroup STM32F1xx_System_Private_FunctionPrototypes
  112. * @{
  113. */
  114. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  115. #ifdef DATA_IN_ExtSRAM
  116. static void SystemInit_ExtMemCtl(void);
  117. #endif /* DATA_IN_ExtSRAM */
  118. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  119. /**
  120. * @}
  121. */
  122. /** @addtogroup STM32F1xx_System_Private_Functions
  123. * @{
  124. */
  125. /**
  126. * @brief Setup the microcontroller system
  127. * Initialize the Embedded Flash Interface, the PLL and update the
  128. * SystemCoreClock variable.
  129. * @note This function should be used only after reset.
  130. * @param None
  131. * @retval None
  132. */
  133. void SystemInit (void)
  134. {
  135. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  136. /* Set HSION bit */
  137. RCC->CR |= (uint32_t)0x00000001;
  138. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  139. #if !defined(STM32F105xC) && !defined(STM32F107xC)
  140. RCC->CFGR &= (uint32_t)0xF8FF0000;
  141. #else
  142. RCC->CFGR &= (uint32_t)0xF0FF0000;
  143. #endif /* STM32F105xC */
  144. /* Reset HSEON, CSSON and PLLON bits */
  145. RCC->CR &= (uint32_t)0xFEF6FFFF;
  146. /* Reset HSEBYP bit */
  147. RCC->CR &= (uint32_t)0xFFFBFFFF;
  148. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  149. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  150. #if defined(STM32F105xC) || defined(STM32F107xC)
  151. /* Reset PLL2ON and PLL3ON bits */
  152. RCC->CR &= (uint32_t)0xEBFFFFFF;
  153. /* Disable all interrupts and clear pending bits */
  154. RCC->CIR = 0x00FF0000;
  155. /* Reset CFGR2 register */
  156. RCC->CFGR2 = 0x00000000;
  157. #elif defined(STM32F100xB) || defined(STM32F100xE)
  158. /* Disable all interrupts and clear pending bits */
  159. RCC->CIR = 0x009F0000;
  160. /* Reset CFGR2 register */
  161. RCC->CFGR2 = 0x00000000;
  162. #else
  163. /* Disable all interrupts and clear pending bits */
  164. RCC->CIR = 0x009F0000;
  165. #endif /* STM32F105xC */
  166. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  167. #ifdef DATA_IN_ExtSRAM
  168. SystemInit_ExtMemCtl();
  169. #endif /* DATA_IN_ExtSRAM */
  170. #endif
  171. #ifdef VECT_TAB_SRAM
  172. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  173. #else
  174. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  175. #endif
  176. }
  177. /**
  178. * @brief Update SystemCoreClock variable according to Clock Register Values.
  179. * The SystemCoreClock variable contains the core clock (HCLK), it can
  180. * be used by the user application to setup the SysTick timer or configure
  181. * other parameters.
  182. *
  183. * @note Each time the core clock (HCLK) changes, this function must be called
  184. * to update SystemCoreClock variable value. Otherwise, any configuration
  185. * based on this variable will be incorrect.
  186. *
  187. * @note - The system frequency computed by this function is not the real
  188. * frequency in the chip. It is calculated based on the predefined
  189. * constant and the selected clock source:
  190. *
  191. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  192. *
  193. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  194. *
  195. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  196. * or HSI_VALUE(*) multiplied by the PLL factors.
  197. *
  198. * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
  199. * 8 MHz) but the real value may vary depending on the variations
  200. * in voltage and temperature.
  201. *
  202. * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
  203. * 8 MHz or 25 MHz, depending on the product used), user has to ensure
  204. * that HSE_VALUE is same as the real frequency of the crystal used.
  205. * Otherwise, this function may have wrong result.
  206. *
  207. * - The result of this function could be not correct when using fractional
  208. * value for HSE crystal.
  209. * @param None
  210. * @retval None
  211. */
  212. void SystemCoreClockUpdate (void)
  213. {
  214. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  215. #if defined(STM32F105xC) || defined(STM32F107xC)
  216. uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
  217. #endif /* STM32F105xC */
  218. #if defined(STM32F100xB) || defined(STM32F100xE)
  219. uint32_t prediv1factor = 0;
  220. #endif /* STM32F100xB or STM32F100xE */
  221. /* Get SYSCLK source -------------------------------------------------------*/
  222. tmp = RCC->CFGR & RCC_CFGR_SWS;
  223. switch (tmp)
  224. {
  225. case 0x00: /* HSI used as system clock */
  226. SystemCoreClock = HSI_VALUE;
  227. break;
  228. case 0x04: /* HSE used as system clock */
  229. SystemCoreClock = HSE_VALUE;
  230. break;
  231. case 0x08: /* PLL used as system clock */
  232. /* Get PLL clock source and multiplication factor ----------------------*/
  233. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  234. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  235. #if !defined(STM32F105xC) && !defined(STM32F107xC)
  236. pllmull = ( pllmull >> 18) + 2;
  237. if (pllsource == 0x00)
  238. {
  239. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  240. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  241. }
  242. else
  243. {
  244. #if defined(STM32F100xB) || defined(STM32F100xE)
  245. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  246. /* HSE oscillator clock selected as PREDIV1 clock entry */
  247. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  248. #else
  249. /* HSE selected as PLL clock entry */
  250. if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
  251. {/* HSE oscillator clock divided by 2 */
  252. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  253. }
  254. else
  255. {
  256. SystemCoreClock = HSE_VALUE * pllmull;
  257. }
  258. #endif
  259. }
  260. #else
  261. pllmull = pllmull >> 18;
  262. if (pllmull != 0x0D)
  263. {
  264. pllmull += 2;
  265. }
  266. else
  267. { /* PLL multiplication factor = PLL input clock * 6.5 */
  268. pllmull = 13 / 2;
  269. }
  270. if (pllsource == 0x00)
  271. {
  272. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  273. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  274. }
  275. else
  276. {/* PREDIV1 selected as PLL clock entry */
  277. /* Get PREDIV1 clock source and division factor */
  278. prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
  279. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  280. if (prediv1source == 0)
  281. {
  282. /* HSE oscillator clock selected as PREDIV1 clock entry */
  283. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  284. }
  285. else
  286. {/* PLL2 clock selected as PREDIV1 clock entry */
  287. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  288. prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
  289. pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
  290. SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
  291. }
  292. }
  293. #endif /* STM32F105xC */
  294. break;
  295. default:
  296. SystemCoreClock = HSI_VALUE;
  297. break;
  298. }
  299. /* Compute HCLK clock frequency ----------------*/
  300. /* Get HCLK prescaler */
  301. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  302. /* HCLK clock frequency */
  303. SystemCoreClock >>= tmp;
  304. }
  305. #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
  306. /**
  307. * @brief Setup the external memory controller. Called in startup_stm32f1xx.s
  308. * before jump to __main
  309. * @param None
  310. * @retval None
  311. */
  312. #ifdef DATA_IN_ExtSRAM
  313. /**
  314. * @brief Setup the external memory controller.
  315. * Called in startup_stm32f1xx_xx.s/.c before jump to main.
  316. * This function configures the external SRAM mounted on STM3210E-EVAL
  317. * board (STM32 High density devices). This SRAM will be used as program
  318. * data memory (including heap and stack).
  319. * @param None
  320. * @retval None
  321. */
  322. void SystemInit_ExtMemCtl(void)
  323. {
  324. /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
  325. required, then adjust the Register Addresses */
  326. /* Enable FSMC clock */
  327. RCC->AHBENR = 0x00000114;
  328. /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  329. RCC->APB2ENR = 0x000001E0;
  330. /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/
  331. /*---------------- SRAM Address lines configuration -------------------------*/
  332. /*---------------- NOE and NWE configuration --------------------------------*/
  333. /*---------------- NE3 configuration ----------------------------------------*/
  334. /*---------------- NBL0, NBL1 configuration ---------------------------------*/
  335. GPIOD->CRL = 0x44BB44BB;
  336. GPIOD->CRH = 0xBBBBBBBB;
  337. GPIOE->CRL = 0xB44444BB;
  338. GPIOE->CRH = 0xBBBBBBBB;
  339. GPIOF->CRL = 0x44BBBBBB;
  340. GPIOF->CRH = 0xBBBB4444;
  341. GPIOG->CRL = 0x44BBBBBB;
  342. GPIOG->CRH = 0x44444B44;
  343. /*---------------- FSMC Configuration ---------------------------------------*/
  344. /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
  345. FSMC_Bank1->BTCR[4] = 0x00001011;
  346. FSMC_Bank1->BTCR[5] = 0x00000200;
  347. }
  348. #endif /* DATA_IN_ExtSRAM */
  349. #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
  350. /**
  351. * @}
  352. */
  353. /**
  354. * @}
  355. */
  356. /**
  357. * @}
  358. */
  359. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/