hal_lld.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 STM32L1xx/hal_lld.c
  15. * @brief STM32L1xx HAL subsystem low level driver source.
  16. *
  17. * @addtogroup HAL
  18. * @{
  19. */
  20. /* TODO: LSEBYP like in F3.*/
  21. #include "hal.h"
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Driver exported variables. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief CMSIS system core clock variable.
  30. * @note It is declared in system_stm32l1xx.h.
  31. */
  32. uint32_t SystemCoreClock = STM32_HCLK;
  33. /*===========================================================================*/
  34. /* Driver local variables and types. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Driver local functions. */
  38. /*===========================================================================*/
  39. /**
  40. * @brief Initializes the backup domain.
  41. */
  42. static void hal_lld_backup_domain_init(void) {
  43. /* Backup domain access enabled and left open.*/
  44. PWR->CR |= PWR_CR_DBP;
  45. /* Reset BKP domain if different clock source selected.*/
  46. if ((RCC->CSR & STM32_RTCSEL_MASK) != STM32_RTCSEL){
  47. /* Backup domain reset.*/
  48. RCC->CSR |= RCC_CSR_RTCRST;
  49. RCC->CSR &= ~RCC_CSR_RTCRST;
  50. }
  51. /* If enabled then the LSE is started.*/
  52. #if STM32_LSE_ENABLED
  53. RCC->CSR |= RCC_CSR_LSEON;
  54. while ((RCC->CSR & RCC_CSR_LSERDY) == 0)
  55. ; /* Waits until LSE is stable. */
  56. #endif
  57. #if STM32_RTCSEL != STM32_RTCSEL_NOCLOCK
  58. /* If the backup domain hasn't been initialized yet then proceed with
  59. initialization.*/
  60. if ((RCC->CSR & RCC_CSR_RTCEN) == 0) {
  61. /* Selects clock source.*/
  62. RCC->CSR |= STM32_RTCSEL;
  63. /* RTC clock enabled.*/
  64. RCC->CSR |= RCC_CSR_RTCEN;
  65. }
  66. #endif /* STM32_RTCSEL != STM32_RTCSEL_NOCLOCK */
  67. }
  68. /*===========================================================================*/
  69. /* Driver interrupt handlers. */
  70. /*===========================================================================*/
  71. /*===========================================================================*/
  72. /* Driver exported functions. */
  73. /*===========================================================================*/
  74. /**
  75. * @brief Low level HAL driver initialization.
  76. *
  77. * @notapi
  78. */
  79. void hal_lld_init(void) {
  80. /* Reset of all peripherals.
  81. Note, GPIOs are not reset because initialized before this point in
  82. board files.*/
  83. rccResetAHB(~(RCC_AHBRSTR_FLITFRST | STM32_GPIO_EN_MASK));
  84. rccResetAPB1(~RCC_APB1RSTR_PWRRST);
  85. rccResetAPB2(~0);
  86. /* PWR clock enabled.*/
  87. rccEnablePWRInterface(true);
  88. /* Initializes the backup domain.*/
  89. hal_lld_backup_domain_init();
  90. /* DMA subsystems initialization.*/
  91. #if defined(STM32_DMA_REQUIRED)
  92. dmaInit();
  93. #endif
  94. /* IRQ subsystem initialization.*/
  95. irqInit();
  96. /* Programmable voltage detector enable.*/
  97. #if STM32_PVD_ENABLE
  98. PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
  99. #endif /* STM32_PVD_ENABLE */
  100. }
  101. /**
  102. * @brief STM32L1xx voltage, clocks and PLL initialization.
  103. * @note All the involved constants come from the file @p board.h.
  104. * @note This function should be invoked just after the system reset.
  105. *
  106. * @special
  107. */
  108. /**
  109. * @brief Clocks and internal voltage initialization.
  110. */
  111. void stm32_clock_init(void) {
  112. #if !STM32_NO_INIT
  113. /* PWR clock enable.*/
  114. RCC->APB1ENR = RCC_APB1ENR_PWREN;
  115. /* Core voltage setup.*/
  116. while ((PWR->CSR & PWR_CSR_VOSF) != 0)
  117. ; /* Waits until regulator is stable. */
  118. PWR->CR = STM32_VOS;
  119. while ((PWR->CSR & PWR_CSR_VOSF) != 0)
  120. ; /* Waits until regulator is stable. */
  121. /* Initial clocks setup and wait for MSI stabilization, the MSI clock is
  122. always enabled because it is the fallback clock when PLL the fails.
  123. Trim fields are not altered from reset values.*/
  124. RCC->CFGR = 0;
  125. RCC->ICSCR = (RCC->ICSCR & ~STM32_MSIRANGE_MASK) | STM32_MSIRANGE;
  126. RCC->CR = RCC_CR_MSION;
  127. while ((RCC->CR & RCC_CR_MSIRDY) == 0)
  128. ; /* Waits until MSI is stable. */
  129. #if STM32_HSI_ENABLED
  130. /* HSI activation.*/
  131. RCC->CR |= RCC_CR_HSION;
  132. while ((RCC->CR & RCC_CR_HSIRDY) == 0)
  133. ; /* Waits until HSI is stable. */
  134. #endif
  135. #if STM32_HSE_ENABLED
  136. #if defined(STM32_HSE_BYPASS)
  137. /* HSE Bypass.*/
  138. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  139. #endif
  140. /* HSE activation.*/
  141. RCC->CR |= RCC_CR_HSEON;
  142. while ((RCC->CR & RCC_CR_HSERDY) == 0)
  143. ; /* Waits until HSE is stable. */
  144. #endif
  145. #if STM32_LSI_ENABLED
  146. /* LSI activation.*/
  147. RCC->CSR |= RCC_CSR_LSION;
  148. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  149. ; /* Waits until LSI is stable. */
  150. #endif
  151. #if STM32_LSE_ENABLED
  152. /* LSE activation, have to unlock the register.*/
  153. if ((RCC->CSR & RCC_CSR_LSEON) == 0) {
  154. PWR->CR |= PWR_CR_DBP;
  155. RCC->CSR |= RCC_CSR_LSEON;
  156. PWR->CR &= ~PWR_CR_DBP;
  157. }
  158. while ((RCC->CSR & RCC_CSR_LSERDY) == 0)
  159. ; /* Waits until LSE is stable. */
  160. #endif
  161. #if STM32_ACTIVATE_PLL
  162. /* PLL activation.*/
  163. RCC->CFGR |= STM32_PLLDIV | STM32_PLLMUL | STM32_PLLSRC;
  164. RCC->CR |= RCC_CR_PLLON;
  165. while (!(RCC->CR & RCC_CR_PLLRDY))
  166. ; /* Waits until PLL is stable. */
  167. #endif
  168. /* Other clock-related settings (dividers, MCO etc).*/
  169. RCC->CR |= STM32_RTCPRE;
  170. RCC->CFGR |= STM32_MCOPRE | STM32_MCOSEL |
  171. STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
  172. RCC->CSR |= STM32_RTCSEL;
  173. /* Flash setup and final clock selection.*/
  174. #if defined(STM32_FLASHBITS1)
  175. FLASH->ACR = STM32_FLASHBITS1;
  176. #endif
  177. #if defined(STM32_FLASHBITS2)
  178. FLASH->ACR = STM32_FLASHBITS2;
  179. #endif
  180. /* Switching to the configured clock source if it is different from MSI.*/
  181. #if (STM32_SW != STM32_SW_MSI)
  182. RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
  183. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  184. ;
  185. #endif
  186. #endif /* STM32_NO_INIT */
  187. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  188. among multiple drivers.*/
  189. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  190. }
  191. /** @} */