system_stm32l1xx.c 16 KB

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