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_stm32l1xx.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32l1xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  6. *
  7. * This file provides two functions and one global variable to be called from
  8. * user application:
  9. * - SystemInit(): This function is called at startup just after reset and
  10. * before branch to main program. This call is made inside
  11. * the "startup_stm32l1xx.s" file.
  12. *
  13. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  14. * by the user application to setup the SysTick
  15. * timer or configure other parameters.
  16. *
  17. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  18. * be called whenever the core clock is changed
  19. * during program execution.
  20. *
  21. ******************************************************************************
  22. * @attention
  23. *
  24. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  25. * All rights reserved.</center></h2>
  26. *
  27. * This software component is licensed by ST under BSD 3-Clause license,
  28. * the "License"; You may not use this file except in compliance with the
  29. * License. You may obtain a copy of the License at:
  30. * opensource.org/licenses/BSD-3-Clause
  31. *
  32. ******************************************************************************
  33. */
  34. /** @addtogroup CMSIS
  35. * @{
  36. */
  37. /** @addtogroup stm32l1xx_system
  38. * @{
  39. */
  40. /** @addtogroup STM32L1xx_System_Private_Includes
  41. * @{
  42. */
  43. #include "stm32l1xx.h"
  44. /**
  45. * @}
  46. */
  47. /** @addtogroup STM32L1xx_System_Private_TypesDefinitions
  48. * @{
  49. */
  50. /**
  51. * @}
  52. */
  53. /** @addtogroup STM32L1xx_System_Private_Defines
  54. * @{
  55. */
  56. #if !defined (HSE_VALUE)
  57. #define HSE_VALUE ((uint32_t)8000000U) /*!< Default value of the External oscillator in Hz.
  58. This value can be provided and adapted by the user application. */
  59. #endif /* HSE_VALUE */
  60. #if !defined (HSI_VALUE)
  61. #define HSI_VALUE ((uint32_t)8000000U) /*!< Default value of the Internal oscillator in Hz.
  62. This value can be provided and adapted by the user application. */
  63. #endif /* HSI_VALUE */
  64. /*!< Uncomment the following line if you need to use external SRAM mounted
  65. on STM32L152D_EVAL board as data memory */
  66. /* #define DATA_IN_ExtSRAM */
  67. /*!< Uncomment the following line if you need to relocate your vector Table in
  68. Internal SRAM. */
  69. /* #define VECT_TAB_SRAM */
  70. #define VECT_TAB_OFFSET 0x00U /*!< Vector Table base offset field.
  71. This value must be a multiple of 0x200. */
  72. /**
  73. * @}
  74. */
  75. /** @addtogroup STM32L1xx_System_Private_Macros
  76. * @{
  77. */
  78. /**
  79. * @}
  80. */
  81. /** @addtogroup STM32L1xx_System_Private_Variables
  82. * @{
  83. */
  84. /* This variable is updated in three ways:
  85. 1) by calling CMSIS function SystemCoreClockUpdate()
  86. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  87. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  88. Note: If you use this function to configure the system clock; then there
  89. is no need to call the 2 first functions listed above, since SystemCoreClock
  90. variable is updated automatically.
  91. */
  92. uint32_t SystemCoreClock = 2097000U;
  93. const uint8_t PLLMulTable[9] = {3U, 4U, 6U, 8U, 12U, 16U, 24U, 32U, 48U};
  94. const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  95. const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
  96. /**
  97. * @}
  98. */
  99. /** @addtogroup STM32L1xx_System_Private_FunctionPrototypes
  100. * @{
  101. */
  102. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  103. #ifdef DATA_IN_ExtSRAM
  104. static void SystemInit_ExtMemCtl(void);
  105. #endif /* DATA_IN_ExtSRAM */
  106. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  107. /**
  108. * @}
  109. */
  110. /** @addtogroup STM32L1xx_System_Private_Functions
  111. * @{
  112. */
  113. /**
  114. * @brief Setup the microcontroller system.
  115. * Initialize the Embedded Flash Interface, the PLL and update the
  116. * SystemCoreClock variable.
  117. * @param None
  118. * @retval None
  119. */
  120. void SystemInit (void)
  121. {
  122. #ifdef DATA_IN_ExtSRAM
  123. SystemInit_ExtMemCtl();
  124. #endif /* DATA_IN_ExtSRAM */
  125. #ifdef VECT_TAB_SRAM
  126. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  127. #else
  128. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  129. #endif
  130. }
  131. /**
  132. * @brief Update SystemCoreClock according to Clock Register Values
  133. * The SystemCoreClock variable contains the core clock (HCLK), it can
  134. * be used by the user application to setup the SysTick timer or configure
  135. * other parameters.
  136. *
  137. * @note Each time the core clock (HCLK) changes, this function must be called
  138. * to update SystemCoreClock variable value. Otherwise, any configuration
  139. * based on this variable will be incorrect.
  140. *
  141. * @note - The system frequency computed by this function is not the real
  142. * frequency in the chip. It is calculated based on the predefined
  143. * constant and the selected clock source:
  144. *
  145. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
  146. * value as defined by the MSI range.
  147. *
  148. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  149. *
  150. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  151. *
  152. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  153. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  154. *
  155. * (*) HSI_VALUE is a constant defined in stm32l1xx.h file (default value
  156. * 16 MHz) but the real value may vary depending on the variations
  157. * in voltage and temperature.
  158. *
  159. * (**) HSE_VALUE is a constant defined in stm32l1xx.h file (default value
  160. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  161. * frequency of the crystal used. Otherwise, this function may
  162. * have wrong result.
  163. *
  164. * - The result of this function could be not correct when using fractional
  165. * value for HSE crystal.
  166. * @param None
  167. * @retval None
  168. */
  169. void SystemCoreClockUpdate (void)
  170. {
  171. uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0;
  172. /* Get SYSCLK source -------------------------------------------------------*/
  173. tmp = RCC->CFGR & RCC_CFGR_SWS;
  174. switch (tmp)
  175. {
  176. case 0x00: /* MSI used as system clock */
  177. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
  178. SystemCoreClock = (32768 * (1 << (msirange + 1)));
  179. break;
  180. case 0x04: /* HSI used as system clock */
  181. SystemCoreClock = HSI_VALUE;
  182. break;
  183. case 0x08: /* HSE used as system clock */
  184. SystemCoreClock = HSE_VALUE;
  185. break;
  186. case 0x0C: /* PLL used as system clock */
  187. /* Get PLL clock source and multiplication factor ----------------------*/
  188. pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
  189. plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
  190. pllmul = PLLMulTable[(pllmul >> 18)];
  191. plldiv = (plldiv >> 22) + 1;
  192. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  193. if (pllsource == 0x00)
  194. {
  195. /* HSI oscillator clock selected as PLL clock entry */
  196. SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
  197. }
  198. else
  199. {
  200. /* HSE selected as PLL clock entry */
  201. SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
  202. }
  203. break;
  204. default: /* MSI used as system clock */
  205. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
  206. SystemCoreClock = (32768 * (1 << (msirange + 1)));
  207. break;
  208. }
  209. /* Compute HCLK clock frequency --------------------------------------------*/
  210. /* Get HCLK prescaler */
  211. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  212. /* HCLK clock frequency */
  213. SystemCoreClock >>= tmp;
  214. }
  215. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  216. #ifdef DATA_IN_ExtSRAM
  217. /**
  218. * @brief Setup the external memory controller.
  219. * Called in SystemInit() function before jump to main.
  220. * This function configures the external SRAM mounted on STM32L152D_EVAL board
  221. * This SRAM will be used as program data memory (including heap and stack).
  222. * @param None
  223. * @retval None
  224. */
  225. void SystemInit_ExtMemCtl(void)
  226. {
  227. __IO uint32_t tmpreg = 0;
  228. /* Flash 1 wait state */
  229. FLASH->ACR |= FLASH_ACR_LATENCY;
  230. /* Power enable */
  231. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  232. /* Delay after an RCC peripheral clock enabling */
  233. tmpreg = READ_BIT(RCC->APB1ENR, RCC_APB1ENR_PWREN);
  234. /* Select the Voltage Range 1 (1.8 V) */
  235. PWR->CR = PWR_CR_VOS_0;
  236. /* Wait Until the Voltage Regulator is ready */
  237. while((PWR->CSR & PWR_CSR_VOSF) != RESET)
  238. {
  239. }
  240. /*-- GPIOs Configuration -----------------------------------------------------*/
  241. /*
  242. +-------------------+--------------------+------------------+------------------+
  243. + SRAM pins assignment +
  244. +-------------------+--------------------+------------------+------------------+
  245. | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 |
  246. | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 |
  247. | PD4 <-> FSMC_NOE | PE7 <-> FSMC_D4 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 |
  248. | PD5 <-> FSMC_NWE | PE8 <-> FSMC_D5 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 |
  249. | PD8 <-> FSMC_D13 | PE9 <-> FSMC_D6 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 |
  250. | PD9 <-> FSMC_D14 | PE10 <-> FSMC_D7 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 |
  251. | PD10 <-> FSMC_D15 | PE11 <-> FSMC_D8 | PF12 <-> FSMC_A6 | PG10<-> FSMC_NE2 |
  252. | PD11 <-> FSMC_A16 | PE12 <-> FSMC_D9 | PF13 <-> FSMC_A7 |------------------+
  253. | PD12 <-> FSMC_A17 | PE13 <-> FSMC_D10 | PF14 <-> FSMC_A8 |
  254. | PD13 <-> FSMC_A18 | PE14 <-> FSMC_D11 | PF15 <-> FSMC_A9 |
  255. | PD14 <-> FSMC_D0 | PE15 <-> FSMC_D12 |------------------+
  256. | PD15 <-> FSMC_D1 |--------------------+
  257. +-------------------+
  258. */
  259. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  260. RCC->AHBENR = 0x000080D8;
  261. /* Delay after an RCC peripheral clock enabling */
  262. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIODEN);
  263. /* Connect PDx pins to FSMC Alternate function */
  264. GPIOD->AFR[0] = 0x00CC00CC;
  265. GPIOD->AFR[1] = 0xCCCCCCCC;
  266. /* Configure PDx pins in Alternate function mode */
  267. GPIOD->MODER = 0xAAAA0A0A;
  268. /* Configure PDx pins speed to 40 MHz */
  269. GPIOD->OSPEEDR = 0xFFFF0F0F;
  270. /* Configure PDx pins Output type to push-pull */
  271. GPIOD->OTYPER = 0x00000000;
  272. /* No pull-up, pull-down for PDx pins */
  273. GPIOD->PUPDR = 0x00000000;
  274. /* Connect PEx pins to FSMC Alternate function */
  275. GPIOE->AFR[0] = 0xC00000CC;
  276. GPIOE->AFR[1] = 0xCCCCCCCC;
  277. /* Configure PEx pins in Alternate function mode */
  278. GPIOE->MODER = 0xAAAA800A;
  279. /* Configure PEx pins speed to 40 MHz */
  280. GPIOE->OSPEEDR = 0xFFFFC00F;
  281. /* Configure PEx pins Output type to push-pull */
  282. GPIOE->OTYPER = 0x00000000;
  283. /* No pull-up, pull-down for PEx pins */
  284. GPIOE->PUPDR = 0x00000000;
  285. /* Connect PFx pins to FSMC Alternate function */
  286. GPIOF->AFR[0] = 0x00CCCCCC;
  287. GPIOF->AFR[1] = 0xCCCC0000;
  288. /* Configure PFx pins in Alternate function mode */
  289. GPIOF->MODER = 0xAA000AAA;
  290. /* Configure PFx pins speed to 40 MHz */
  291. GPIOF->OSPEEDR = 0xFF000FFF;
  292. /* Configure PFx pins Output type to push-pull */
  293. GPIOF->OTYPER = 0x00000000;
  294. /* No pull-up, pull-down for PFx pins */
  295. GPIOF->PUPDR = 0x00000000;
  296. /* Connect PGx pins to FSMC Alternate function */
  297. GPIOG->AFR[0] = 0x00CCCCCC;
  298. GPIOG->AFR[1] = 0x00000C00;
  299. /* Configure PGx pins in Alternate function mode */
  300. GPIOG->MODER = 0x00200AAA;
  301. /* Configure PGx pins speed to 40 MHz */
  302. GPIOG->OSPEEDR = 0x00300FFF;
  303. /* Configure PGx pins Output type to push-pull */
  304. GPIOG->OTYPER = 0x00000000;
  305. /* No pull-up, pull-down for PGx pins */
  306. GPIOG->PUPDR = 0x00000000;
  307. /*-- FSMC Configuration ------------------------------------------------------*/
  308. /* Enable the FSMC interface clock */
  309. RCC->AHBENR = 0x400080D8;
  310. /* Delay after an RCC peripheral clock enabling */
  311. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
  312. (void)(tmpreg);
  313. /* Configure and enable Bank1_SRAM3 */
  314. FSMC_Bank1->BTCR[4] = 0x00001011;
  315. FSMC_Bank1->BTCR[5] = 0x00000300;
  316. FSMC_Bank1E->BWTR[4] = 0x0FFFFFFF;
  317. /*
  318. Bank1_SRAM3 is configured as follow:
  319. p.FSMC_AddressSetupTime = 0;
  320. p.FSMC_AddressHoldTime = 0;
  321. p.FSMC_DataSetupTime = 3;
  322. p.FSMC_BusTurnAroundDuration = 0;
  323. p.FSMC_CLKDivision = 0;
  324. p.FSMC_DataLatency = 0;
  325. p.FSMC_AccessMode = FSMC_AccessMode_A;
  326. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
  327. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  328. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  329. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  330. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  331. FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  332. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  333. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  334. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  335. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  336. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  337. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  338. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  339. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  340. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  341. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  342. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
  343. */
  344. }
  345. #endif /* DATA_IN_ExtSRAM */
  346. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  347. /**
  348. * @}
  349. */
  350. /**
  351. * @}
  352. */
  353. /**
  354. * @}
  355. */
  356. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/