hal_lld.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file STM32L4xx/hal_lld.c
  15. * @brief STM32L4xx HAL subsystem low level driver source.
  16. *
  17. * @addtogroup STM32L4xx_ISR
  18. * @{
  19. */
  20. #include "hal.h"
  21. /*===========================================================================*/
  22. /* Driver local definitions. */
  23. /*===========================================================================*/
  24. /*===========================================================================*/
  25. /* Driver exported variables. */
  26. /*===========================================================================*/
  27. /**
  28. * @brief CMSIS system core clock variable.
  29. * @note It is declared in system_stm32f7xx.h.
  30. */
  31. uint32_t SystemCoreClock = STM32_HCLK;
  32. /*===========================================================================*/
  33. /* Driver local variables and types. */
  34. /*===========================================================================*/
  35. /*===========================================================================*/
  36. /* Driver local functions. */
  37. /*===========================================================================*/
  38. /**
  39. * @brief Initializes the backup domain.
  40. * @note WARNING! Changing RTC clock source impossible without resetting
  41. * of the whole BKP domain.
  42. */
  43. static void hal_lld_backup_domain_init(void) {
  44. /* Reset BKP domain if different clock source selected.*/
  45. if ((RCC->BDCR & STM32_RTCSEL_MASK) != STM32_RTCSEL) {
  46. /* Backup domain reset.*/
  47. RCC->BDCR = RCC_BDCR_BDRST;
  48. RCC->BDCR = 0;
  49. }
  50. #if STM32_LSE_ENABLED
  51. /* LSE activation.*/
  52. #if defined(STM32_LSE_BYPASS)
  53. /* LSE Bypass.*/
  54. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  55. #else
  56. /* No LSE Bypass.*/
  57. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON;
  58. #endif
  59. while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
  60. ; /* Wait until LSE is stable. */
  61. #endif
  62. #if STM32_MSIPLL_ENABLED
  63. /* MSI PLL activation depends on LSE. Reactivating and checking for
  64. MSI stability.*/
  65. RCC->CR |= RCC_CR_MSIPLLEN;
  66. while ((RCC->CR & RCC_CR_MSIRDY) == 0)
  67. ; /* Wait until MSI is stable. */
  68. #endif
  69. #if HAL_USE_RTC
  70. /* If the backup domain hasn't been initialized yet then proceed with
  71. initialization.*/
  72. if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
  73. /* Selects clock source.*/
  74. RCC->BDCR |= STM32_RTCSEL;
  75. /* RTC clock enabled.*/
  76. RCC->BDCR |= RCC_BDCR_RTCEN;
  77. }
  78. #endif /* HAL_USE_RTC */
  79. /* Low speed output mode.*/
  80. RCC->BDCR |= STM32_LSCOSEL;
  81. }
  82. /*===========================================================================*/
  83. /* Driver interrupt handlers. */
  84. /*===========================================================================*/
  85. /*===========================================================================*/
  86. /* Driver exported functions. */
  87. /*===========================================================================*/
  88. /**
  89. * @brief Low level HAL driver initialization.
  90. *
  91. * @notapi
  92. */
  93. void hal_lld_init(void) {
  94. /* Reset of all peripherals.
  95. Note, GPIOs are not reset because initialized before this point in
  96. board files.*/
  97. rccResetAHB1(~0);
  98. rccResetAHB2(~STM32_GPIO_EN_MASK);
  99. rccResetAHB3(~0);
  100. rccResetAPB1R1(~RCC_APB1RSTR1_PWRRST);
  101. rccResetAPB1R2(~0);
  102. rccResetAPB2(~0);
  103. /* PWR clock enabled.*/
  104. rccEnablePWRInterface(true);
  105. /* Initializes the backup domain.*/
  106. hal_lld_backup_domain_init();
  107. /* DMA subsystems initialization.*/
  108. #if defined(STM32_DMA_REQUIRED)
  109. dmaInit();
  110. #endif
  111. /* IRQ subsystem initialization.*/
  112. irqInit();
  113. /* Programmable voltage detector enable.*/
  114. #if STM32_PVD_ENABLE
  115. PWR->CR2 = PWR_CR2_PVDE | (STM32_PLS & STM32_PLS_MASK);
  116. #else
  117. PWR->CR2 = 0;
  118. #endif /* STM32_PVD_ENABLE */
  119. /* Enabling independent VDDUSB.*/
  120. #if HAL_USE_USB
  121. PWR->CR2 |= PWR_CR2_USV;
  122. #endif /* HAL_USE_USB */
  123. /* Enabling independent VDDIO2 required by GPIOG.*/
  124. #if STM32_HAS_GPIOG
  125. PWR->CR2 |= PWR_CR2_IOSV;
  126. #endif /* STM32_HAS_GPIOG */
  127. }
  128. /**
  129. * @brief STM32L4xx clocks and PLL initialization.
  130. * @note All the involved constants come from the file @p board.h.
  131. * @note This function should be invoked just after the system reset.
  132. *
  133. * @special
  134. */
  135. void stm32_clock_init(void) {
  136. #if !STM32_NO_INIT
  137. /* PWR clock enable.*/
  138. #if defined(HAL_USE_RTC) && defined(RCC_APB1ENR1_RTCAPBEN)
  139. RCC->APB1ENR1 = RCC_APB1ENR1_PWREN | RCC_APB1ENR1_RTCAPBEN;
  140. #else
  141. RCC->APB1ENR1 = RCC_APB1ENR1_PWREN;
  142. #endif
  143. /* Initial clocks setup and wait for MSI stabilization, the MSI clock is
  144. always enabled because it is the fall back clock when PLL the fails.
  145. Trim fields are not altered from reset values.*/
  146. /* MSIRANGE can be set only when MSI is OFF or READY.*/
  147. RCC->CR = RCC_CR_MSION;
  148. while ((RCC->CR & RCC_CR_MSIRDY) == 0)
  149. ; /* Wait until MSI is stable. */
  150. /* Clocking from MSI, in case MSI was not the default source.*/
  151. RCC->CFGR = 0;
  152. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI)
  153. ; /* Wait until MSI is selected. */
  154. /* Core voltage setup.*/
  155. PWR->CR1 = STM32_VOS;
  156. while ((PWR->SR2 & PWR_SR2_VOSF) != 0) /* Wait until regulator is */
  157. ; /* stable. */
  158. #if STM32_HSI16_ENABLED
  159. /* HSI activation.*/
  160. RCC->CR |= RCC_CR_HSION;
  161. while ((RCC->CR & RCC_CR_HSIRDY) == 0)
  162. ; /* Wait until HSI16 is stable. */
  163. #endif
  164. #if STM32_CLOCK_HAS_HSI48
  165. #if STM32_HSI48_ENABLED
  166. /* HSI activation.*/
  167. RCC->CRRCR |= RCC_CRRCR_HSI48ON;
  168. while ((RCC->CRRCR & RCC_CRRCR_HSI48RDY) == 0)
  169. ; /* Wait until HSI48 is stable. */
  170. #endif
  171. #endif
  172. #if STM32_HSE_ENABLED
  173. #if defined(STM32_HSE_BYPASS)
  174. /* HSE Bypass.*/
  175. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  176. #endif
  177. /* HSE activation.*/
  178. RCC->CR |= RCC_CR_HSEON;
  179. while ((RCC->CR & RCC_CR_HSERDY) == 0)
  180. ; /* Wait until HSE is stable. */
  181. #endif
  182. #if STM32_LSI_ENABLED
  183. /* LSI activation.*/
  184. RCC->CSR |= RCC_CSR_LSION;
  185. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  186. ; /* Wait until LSI is stable. */
  187. #endif
  188. /* Backup domain access enabled and left open.*/
  189. PWR->CR1 |= PWR_CR1_DBP;
  190. #if STM32_LSE_ENABLED
  191. /* LSE activation.*/
  192. #if defined(STM32_LSE_BYPASS)
  193. /* LSE Bypass.*/
  194. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  195. #else
  196. /* No LSE Bypass.*/
  197. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON;
  198. #endif
  199. while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
  200. ; /* Wait until LSE is stable. */
  201. #endif
  202. /* Flash setup for selected MSI speed setting.*/
  203. FLASH->ACR = FLASH_ACR_DCEN | FLASH_ACR_ICEN | FLASH_ACR_PRFTEN |
  204. STM32_MSI_FLASHBITS;
  205. /* Changing MSIRANGE to configured value.*/
  206. RCC->CR |= STM32_MSIRANGE;
  207. /* Switching from MSISRANGE to MSIRANGE.*/
  208. RCC->CR |= RCC_CR_MSIRGSEL;
  209. while ((RCC->CR & RCC_CR_MSIRDY) == 0)
  210. ;
  211. /* MSI is configured SYSCLK source so wait for it to be stable as well.*/
  212. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI)
  213. ;
  214. #if STM32_MSIPLL_ENABLED
  215. /* MSI PLL (to LSE) activation */
  216. RCC->CR |= RCC_CR_MSIPLLEN;
  217. #endif
  218. /* Updating MSISRANGE value. MSISRANGE can be set only when MSIRGSEL is high.
  219. This range is used exiting the Standby mode until MSIRGSEL is set.*/
  220. RCC->CSR |= STM32_MSISRANGE;
  221. #if STM32_ACTIVATE_PLL || STM32_ACTIVATE_PLLSAI1 || STM32_ACTIVATE_PLLSAI2
  222. /* PLLM and PLLSRC are common to all PLLs.*/
  223. #if defined(STM32L496xx) || defined(STM32L4A6xx)
  224. RCC->PLLCFGR = STM32_PLLPDIV | STM32_PLLR |
  225. STM32_PLLREN | STM32_PLLQ |
  226. STM32_PLLQEN | STM32_PLLP |
  227. STM32_PLLPEN | STM32_PLLN |
  228. STM32_PLLM | STM32_PLLSRC;
  229. #else
  230. RCC->PLLCFGR = STM32_PLLR | STM32_PLLREN |
  231. STM32_PLLQ | STM32_PLLQEN |
  232. STM32_PLLP | STM32_PLLPEN |
  233. STM32_PLLN | STM32_PLLM |
  234. STM32_PLLSRC;
  235. #endif
  236. #endif
  237. #if STM32_ACTIVATE_PLL
  238. /* PLL activation.*/
  239. RCC->CR |= RCC_CR_PLLON;
  240. /* Waiting for PLL lock.*/
  241. while ((RCC->CR & RCC_CR_PLLRDY) == 0)
  242. ;
  243. #endif
  244. #if STM32_ACTIVATE_PLLSAI1
  245. /* PLLSAI1 activation.*/
  246. #if defined(STM32L496xx) || defined(STM32L4A6xx)
  247. RCC->PLLSAI1CFGR = STM32_PLLSAI1PDIV | STM32_PLLSAI1R |
  248. STM32_PLLSAI1REN | STM32_PLLSAI1Q |
  249. STM32_PLLSAI1QEN | STM32_PLLSAI1P |
  250. STM32_PLLSAI1PEN | STM32_PLLSAI1N;
  251. #else
  252. RCC->PLLSAI1CFGR = STM32_PLLSAI1R | STM32_PLLSAI1REN |
  253. STM32_PLLSAI1Q | STM32_PLLSAI1QEN |
  254. STM32_PLLSAI1P | STM32_PLLSAI1PEN |
  255. STM32_PLLSAI1N;
  256. #endif
  257. RCC->CR |= RCC_CR_PLLSAI1ON;
  258. /* Waiting for PLL lock.*/
  259. while ((RCC->CR & RCC_CR_PLLSAI1RDY) == 0)
  260. ;
  261. #endif
  262. #if STM32_ACTIVATE_PLLSAI2
  263. /* PLLSAI2 activation.*/
  264. #if defined(STM32L496xx) || defined(STM32L4A6xx)
  265. RCC->PLLSAI2CFGR = STM32_PLLSAI2PDIV | STM32_PLLSAI2R |
  266. STM32_PLLSAI2REN | STM32_PLLSAI2P |
  267. STM32_PLLSAI2PEN | STM32_PLLSAI2N;
  268. #else
  269. RCC->PLLSAI2CFGR = STM32_PLLSAI2R | STM32_PLLSAI2REN |
  270. STM32_PLLSAI2P | STM32_PLLSAI2PEN |
  271. STM32_PLLSAI2N;
  272. #endif
  273. RCC->CR |= RCC_CR_PLLSAI2ON;
  274. /* Waiting for PLL lock.*/
  275. while ((RCC->CR & RCC_CR_PLLSAI2RDY) == 0)
  276. ;
  277. #endif
  278. /* Other clock-related settings (dividers, MCO etc).*/
  279. RCC->CFGR = STM32_MCOPRE | STM32_MCOSEL | STM32_STOPWUCK |
  280. STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
  281. /* CCIPR register initialization, note, must take care of the _OFF
  282. pseudo settings.*/
  283. {
  284. uint32_t ccipr = STM32_DFSDMSEL | STM32_SWPMI1SEL | STM32_ADCSEL |
  285. STM32_CLK48SEL | STM32_LPTIM2SEL | STM32_LPTIM1SEL |
  286. STM32_I2C3SEL | STM32_I2C2SEL | STM32_I2C1SEL |
  287. STM32_UART5SEL | STM32_UART4SEL | STM32_USART3SEL |
  288. STM32_USART2SEL | STM32_USART1SEL | STM32_LPUART1SEL;
  289. #if STM32_SAI2SEL != STM32_SAI2SEL_OFF
  290. ccipr |= STM32_SAI2SEL;
  291. #endif
  292. #if STM32_SAI1SEL != STM32_SAI1SEL_OFF
  293. ccipr |= STM32_SAI1SEL;
  294. #endif
  295. RCC->CCIPR = ccipr;
  296. }
  297. /* Set flash WS's for SYSCLK source */
  298. if (STM32_FLASHBITS > STM32_MSI_FLASHBITS)
  299. FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY_Msk) | STM32_FLASHBITS;
  300. /* Switching to the configured SYSCLK source if it is different from MSI.*/
  301. #if (STM32_SW != STM32_SW_MSI)
  302. RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
  303. /* Wait until SYSCLK is stable.*/
  304. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  305. ;
  306. #endif
  307. /* Reduce the flash WS's for SYSCLK source if they are less than MSI WSs */
  308. if (STM32_FLASHBITS < STM32_MSI_FLASHBITS)
  309. FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY_Msk) | STM32_FLASHBITS;
  310. #endif /* STM32_NO_INIT */
  311. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  312. among multiple drivers.*/
  313. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  314. }
  315. /** @} */