system_stm32l1xx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32l1xx.c
  4. * @author MCD Application Team
  5. * @version V2.0.0
  6. * @date 5-September-2014
  7. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  8. *
  9. * This file provides two functions and one global variable to be called from
  10. * user application:
  11. * - SystemInit(): This function is called at startup just after reset and
  12. * before branch to main program. This call is made inside
  13. * the "startup_stm32l1xx.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. ******************************************************************************
  24. * @attention
  25. *
  26. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  27. *
  28. * Redistribution and use in source and binary forms, with or without modification,
  29. * are permitted provided that the following conditions are met:
  30. * 1. Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above copyright notice,
  33. * this list of conditions and the following disclaimer in the documentation
  34. * and/or other materials provided with the distribution.
  35. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  36. * may be used to endorse or promote products derived from this software
  37. * without specific prior written permission.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  43. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. ******************************************************************************
  51. */
  52. /** @addtogroup CMSIS
  53. * @{
  54. */
  55. /** @addtogroup stm32l1xx_system
  56. * @{
  57. */
  58. /** @addtogroup STM32L1xx_System_Private_Includes
  59. * @{
  60. */
  61. #include "stm32l1xx.h"
  62. /**
  63. * @}
  64. */
  65. /** @addtogroup STM32L1xx_System_Private_TypesDefinitions
  66. * @{
  67. */
  68. /**
  69. * @}
  70. */
  71. /** @addtogroup STM32L1xx_System_Private_Defines
  72. * @{
  73. */
  74. #if !defined (HSE_VALUE)
  75. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
  76. This value can be provided and adapted by the user application. */
  77. #endif /* HSE_VALUE */
  78. #if !defined (HSI_VALUE)
  79. #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
  80. This value can be provided and adapted by the user application. */
  81. #endif /* HSI_VALUE */
  82. /*!< Uncomment the following line if you need to use external SRAM mounted
  83. on STM32L152D_EVAL board as data memory */
  84. /* #define DATA_IN_ExtSRAM */
  85. /*!< Uncomment the following line if you need to relocate your vector Table in
  86. Internal SRAM. */
  87. /* #define VECT_TAB_SRAM */
  88. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  89. This value must be a multiple of 0x200. */
  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 = 32000000;
  111. __IO const uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};
  112. __IO const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  113. /**
  114. * @}
  115. */
  116. /** @addtogroup STM32L1xx_System_Private_FunctionPrototypes
  117. * @{
  118. */
  119. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  120. #ifdef DATA_IN_ExtSRAM
  121. static void SystemInit_ExtMemCtl(void);
  122. #endif /* DATA_IN_ExtSRAM */
  123. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  124. /**
  125. * @}
  126. */
  127. /** @addtogroup STM32L1xx_System_Private_Functions
  128. * @{
  129. */
  130. /**
  131. * @brief Setup the microcontroller system.
  132. * Initialize the Embedded Flash Interface, the PLL and update the
  133. * SystemCoreClock variable.
  134. * @param None
  135. * @retval None
  136. */
  137. void SystemInit (void)
  138. {
  139. /*!< Set MSION bit */
  140. RCC->CR |= (uint32_t)0x00000100;
  141. /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */
  142. RCC->CFGR &= (uint32_t)0x88FFC00C;
  143. /*!< Reset HSION, HSEON, CSSON and PLLON bits */
  144. RCC->CR &= (uint32_t)0xEEFEFFFE;
  145. /*!< Reset HSEBYP bit */
  146. RCC->CR &= (uint32_t)0xFFFBFFFF;
  147. /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */
  148. RCC->CFGR &= (uint32_t)0xFF02FFFF;
  149. /*!< Disable all interrupts */
  150. RCC->CIR = 0x00000000;
  151. #ifdef DATA_IN_ExtSRAM
  152. SystemInit_ExtMemCtl();
  153. #endif /* DATA_IN_ExtSRAM */
  154. #ifdef VECT_TAB_SRAM
  155. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  156. #else
  157. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  158. #endif
  159. }
  160. /**
  161. * @brief Update SystemCoreClock according to Clock Register Values
  162. * The SystemCoreClock variable contains the core clock (HCLK), it can
  163. * be used by the user application to setup the SysTick timer or configure
  164. * other parameters.
  165. *
  166. * @note Each time the core clock (HCLK) changes, this function must be called
  167. * to update SystemCoreClock variable value. Otherwise, any configuration
  168. * based on this variable will be incorrect.
  169. *
  170. * @note - The system frequency computed by this function is not the real
  171. * frequency in the chip. It is calculated based on the predefined
  172. * constant and the selected clock source:
  173. *
  174. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
  175. * value as defined by the MSI range.
  176. *
  177. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  178. *
  179. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  180. *
  181. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  182. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  183. *
  184. * (*) HSI_VALUE is a constant defined in stm32l1xx.h file (default value
  185. * 16 MHz) but the real value may vary depending on the variations
  186. * in voltage and temperature.
  187. *
  188. * (**) HSE_VALUE is a constant defined in stm32l1xx.h file (default value
  189. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  190. * frequency of the crystal used. Otherwise, this function may
  191. * have wrong result.
  192. *
  193. * - The result of this function could be not correct when using fractional
  194. * value for HSE crystal.
  195. * @param None
  196. * @retval None
  197. */
  198. void SystemCoreClockUpdate (void)
  199. {
  200. uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0;
  201. /* Get SYSCLK source -------------------------------------------------------*/
  202. tmp = RCC->CFGR & RCC_CFGR_SWS;
  203. switch (tmp)
  204. {
  205. case 0x00: /* MSI used as system clock */
  206. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
  207. SystemCoreClock = (32768 * (1 << (msirange + 1)));
  208. break;
  209. case 0x04: /* HSI used as system clock */
  210. SystemCoreClock = HSI_VALUE;
  211. break;
  212. case 0x08: /* HSE used as system clock */
  213. SystemCoreClock = HSE_VALUE;
  214. break;
  215. case 0x0C: /* PLL used as system clock */
  216. /* Get PLL clock source and multiplication factor ----------------------*/
  217. pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
  218. plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
  219. pllmul = PLLMulTable[(pllmul >> 18)];
  220. plldiv = (plldiv >> 22) + 1;
  221. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  222. if (pllsource == 0x00)
  223. {
  224. /* HSI oscillator clock selected as PLL clock entry */
  225. SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
  226. }
  227. else
  228. {
  229. /* HSE selected as PLL clock entry */
  230. SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
  231. }
  232. break;
  233. default: /* MSI used as system clock */
  234. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
  235. SystemCoreClock = (32768 * (1 << (msirange + 1)));
  236. break;
  237. }
  238. /* Compute HCLK clock frequency --------------------------------------------*/
  239. /* Get HCLK prescaler */
  240. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  241. /* HCLK clock frequency */
  242. SystemCoreClock >>= tmp;
  243. }
  244. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  245. #ifdef DATA_IN_ExtSRAM
  246. /**
  247. * @brief Setup the external memory controller.
  248. * Called in SystemInit() function before jump to main.
  249. * This function configures the external SRAM mounted on STM32L152D_EVAL board
  250. * This SRAM will be used as program data memory (including heap and stack).
  251. * @param None
  252. * @retval None
  253. */
  254. void SystemInit_ExtMemCtl(void)
  255. {
  256. /*-- GPIOs Configuration -----------------------------------------------------*/
  257. /*
  258. +-------------------+--------------------+------------------+------------------+
  259. + SRAM pins assignment +
  260. +-------------------+--------------------+------------------+------------------+
  261. | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 |
  262. | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 |
  263. | PD4 <-> FSMC_NOE | PE7 <-> FSMC_D4 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 |
  264. | PD5 <-> FSMC_NWE | PE8 <-> FSMC_D5 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 |
  265. | PD8 <-> FSMC_D13 | PE9 <-> FSMC_D6 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 |
  266. | PD9 <-> FSMC_D14 | PE10 <-> FSMC_D7 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 |
  267. | PD10 <-> FSMC_D15 | PE11 <-> FSMC_D8 | PF12 <-> FSMC_A6 | PG10<-> FSMC_NE2 |
  268. | PD11 <-> FSMC_A16 | PE12 <-> FSMC_D9 | PF13 <-> FSMC_A7 |------------------+
  269. | PD12 <-> FSMC_A17 | PE13 <-> FSMC_D10 | PF14 <-> FSMC_A8 |
  270. | PD13 <-> FSMC_A18 | PE14 <-> FSMC_D11 | PF15 <-> FSMC_A9 |
  271. | PD14 <-> FSMC_D0 | PE15 <-> FSMC_D12 |------------------+
  272. | PD15 <-> FSMC_D1 |--------------------+
  273. +-------------------+
  274. */
  275. /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
  276. RCC->AHBENR = 0x000080D8;
  277. /* Connect PDx pins to FSMC Alternate function */
  278. GPIOD->AFR[0] = 0x00CC00CC;
  279. GPIOD->AFR[1] = 0xCCCCCCCC;
  280. /* Configure PDx pins in Alternate function mode */
  281. GPIOD->MODER = 0xAAAA0A0A;
  282. /* Configure PDx pins speed to 40 MHz */
  283. GPIOD->OSPEEDR = 0xFFFF0F0F;
  284. /* Configure PDx pins Output type to push-pull */
  285. GPIOD->OTYPER = 0x00000000;
  286. /* No pull-up, pull-down for PDx pins */
  287. GPIOD->PUPDR = 0x00000000;
  288. /* Connect PEx pins to FSMC Alternate function */
  289. GPIOE->AFR[0] = 0xC00000CC;
  290. GPIOE->AFR[1] = 0xCCCCCCCC;
  291. /* Configure PEx pins in Alternate function mode */
  292. GPIOE->MODER = 0xAAAA800A;
  293. /* Configure PEx pins speed to 40 MHz */
  294. GPIOE->OSPEEDR = 0xFFFFC00F;
  295. /* Configure PEx pins Output type to push-pull */
  296. GPIOE->OTYPER = 0x00000000;
  297. /* No pull-up, pull-down for PEx pins */
  298. GPIOE->PUPDR = 0x00000000;
  299. /* Connect PFx pins to FSMC Alternate function */
  300. GPIOF->AFR[0] = 0x00CCCCCC;
  301. GPIOF->AFR[1] = 0xCCCC0000;
  302. /* Configure PFx pins in Alternate function mode */
  303. GPIOF->MODER = 0xAA000AAA;
  304. /* Configure PFx pins speed to 40 MHz */
  305. GPIOF->OSPEEDR = 0xFF000FFF;
  306. /* Configure PFx pins Output type to push-pull */
  307. GPIOF->OTYPER = 0x00000000;
  308. /* No pull-up, pull-down for PFx pins */
  309. GPIOF->PUPDR = 0x00000000;
  310. /* Connect PGx pins to FSMC Alternate function */
  311. GPIOG->AFR[0] = 0x00CCCCCC;
  312. GPIOG->AFR[1] = 0x00000C00;
  313. /* Configure PGx pins in Alternate function mode */
  314. GPIOG->MODER = 0x00200AAA;
  315. /* Configure PGx pins speed to 40 MHz */
  316. GPIOG->OSPEEDR = 0x00300FFF;
  317. /* Configure PGx pins Output type to push-pull */
  318. GPIOG->OTYPER = 0x00000000;
  319. /* No pull-up, pull-down for PGx pins */
  320. GPIOG->PUPDR = 0x00000000;
  321. /*-- FSMC Configuration ------------------------------------------------------*/
  322. /* Enable the FSMC interface clock */
  323. RCC->AHBENR = 0x400080D8;
  324. /* Configure and enable Bank1_SRAM3 */
  325. FSMC_Bank1->BTCR[4] = 0x00001011;
  326. FSMC_Bank1->BTCR[5] = 0x00000300;
  327. FSMC_Bank1E->BWTR[4] = 0x0FFFFFFF;
  328. /*
  329. Bank1_SRAM3 is configured as follow:
  330. p.FSMC_AddressSetupTime = 0;
  331. p.FSMC_AddressHoldTime = 0;
  332. p.FSMC_DataSetupTime = 3;
  333. p.FSMC_BusTurnAroundDuration = 0;
  334. p.FSMC_CLKDivision = 0;
  335. p.FSMC_DataLatency = 0;
  336. p.FSMC_AccessMode = FSMC_AccessMode_A;
  337. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
  338. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  339. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  340. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  341. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  342. FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  343. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  344. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  345. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  346. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  347. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  348. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  349. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  350. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  351. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  352. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  353. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
  354. */
  355. }
  356. #endif /* DATA_IN_ExtSRAM */
  357. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  358. /**
  359. * @}
  360. */
  361. /**
  362. * @}
  363. */
  364. /**
  365. * @}
  366. */
  367. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/