system_stm32l1xx.c 16 KB

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