hal_lld.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 STM32L0xx/hal_lld.c
  15. * @brief STM32L0xx HAL subsystem low level driver source.
  16. *
  17. * @addtogroup HAL
  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_stm32l0xx.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. */
  41. static void hal_lld_backup_domain_init(void) {
  42. /* Backup domain access enabled and left open.*/
  43. PWR->CR |= PWR_CR_DBP;
  44. /* Reset BKP domain if different clock source selected.*/
  45. if ((RCC->CSR & STM32_RTCSEL_MASK) != STM32_RTCSEL){
  46. /* Backup domain reset.*/
  47. RCC->CSR |= RCC_CSR_RTCRST;
  48. RCC->CSR &= ~RCC_CSR_RTCRST;
  49. }
  50. /* If enabled then the LSE is started.*/
  51. #if STM32_LSE_ENABLED
  52. RCC->CSR |= RCC_CSR_LSEON;
  53. while ((RCC->CSR & RCC_CSR_LSERDY) == 0)
  54. ; /* Waits until LSE is stable. */
  55. #endif
  56. #if STM32_RTCSEL != STM32_RTCSEL_NOCLOCK
  57. /* If the backup domain hasn't been initialized yet then proceed with
  58. initialization.*/
  59. if ((RCC->CSR & RCC_CSR_RTCEN) == 0) {
  60. /* Selects clock source.*/
  61. RCC->CSR |= STM32_RTCSEL;
  62. /* RTC clock enabled.*/
  63. RCC->CSR |= RCC_CSR_RTCEN;
  64. }
  65. #endif /* STM32_RTCSEL != STM32_RTCSEL_NOCLOCK */
  66. }
  67. /*===========================================================================*/
  68. /* Driver interrupt handlers. */
  69. /*===========================================================================*/
  70. #if defined(STM32_DMA_REQUIRED) || defined(__DOXYGEN__)
  71. #if defined(STM32_DMA1_CH23_HANDLER) || defined(__DOXYGEN__)
  72. /**
  73. * @brief DMA1 streams 2 and 3 shared ISR.
  74. * @note It is declared here because this device has a non-standard
  75. * DMA shared IRQ handler.
  76. *
  77. * @isr
  78. */
  79. OSAL_IRQ_HANDLER(STM32_DMA1_CH23_HANDLER) {
  80. OSAL_IRQ_PROLOGUE();
  81. /* Check on channel 2.*/
  82. dmaServeInterrupt(STM32_DMA1_STREAM2);
  83. /* Check on channel 3.*/
  84. dmaServeInterrupt(STM32_DMA1_STREAM3);
  85. OSAL_IRQ_EPILOGUE();
  86. }
  87. #endif /* defined(STM32_DMA1_CH23_HANDLER) */
  88. #if defined(STM32_DMA1_CH4567_HANDLER) || defined(__DOXYGEN__)
  89. /**
  90. * @brief DMA1 streams 4, 5, 6 and 7 shared ISR.
  91. *
  92. * @isr
  93. */
  94. OSAL_IRQ_HANDLER(STM32_DMA1_CH4567_HANDLER) {
  95. OSAL_IRQ_PROLOGUE();
  96. /* Check on channel 4.*/
  97. dmaServeInterrupt(STM32_DMA1_STREAM4);
  98. /* Check on channel 5.*/
  99. dmaServeInterrupt(STM32_DMA1_STREAM5);
  100. #if STM32_DMA1_NUM_CHANNELS > 5
  101. /* Check on channel 6.*/
  102. dmaServeInterrupt(STM32_DMA1_STREAM6);
  103. #endif
  104. #if STM32_DMA1_NUM_CHANNELS > 6
  105. /* Check on channel 7.*/
  106. dmaServeInterrupt(STM32_DMA1_STREAM7);
  107. #endif
  108. OSAL_IRQ_EPILOGUE();
  109. }
  110. #endif /* defined(STM32_DMA1_CH4567_HANDLER) */
  111. #endif /* defined(STM32_DMA_REQUIRED) */
  112. /*===========================================================================*/
  113. /* Driver exported functions. */
  114. /*===========================================================================*/
  115. /**
  116. * @brief Low level HAL driver initialization.
  117. *
  118. * @notapi
  119. */
  120. void hal_lld_init(void) {
  121. /* Reset of all peripherals except those on IOP.*/
  122. rccResetAHB(~RCC_AHBRSTR_MIFRST);
  123. rccResetAPB1(~RCC_APB1RSTR_PWRRST);
  124. rccResetAPB2(~0);
  125. /* PWR clock enabled.*/
  126. rccEnablePWRInterface(true);
  127. /* Initializes the backup domain.*/
  128. hal_lld_backup_domain_init();
  129. /* DMA subsystems initialization.*/
  130. #if defined(STM32_DMA_REQUIRED)
  131. dmaInit();
  132. #endif
  133. /* IRQ subsystem initialization.*/
  134. irqInit();
  135. /* Programmable voltage detector enable.*/
  136. #if STM32_PVD_ENABLE
  137. PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
  138. #endif /* STM32_PVD_ENABLE */
  139. }
  140. /**
  141. * @brief STM32L0xx voltage, clocks and PLL initialization.
  142. * @note All the involved constants come from the file @p board.h.
  143. * @note This function should be invoked just after the system reset.
  144. *
  145. * @special
  146. */
  147. /**
  148. * @brief Clocks and internal voltage initialization.
  149. */
  150. void stm32_clock_init(void) {
  151. #if !STM32_NO_INIT
  152. /* PWR clock enable.*/
  153. RCC->APB1ENR = RCC_APB1ENR_PWREN;
  154. /* Core voltage setup.*/
  155. while ((PWR->CSR & PWR_CSR_VOSF) != 0)
  156. ; /* Waits until regulator is stable. */
  157. PWR->CR = STM32_VOS;
  158. while ((PWR->CSR & PWR_CSR_VOSF) != 0)
  159. ; /* Waits until regulator is stable. */
  160. /* Initial clocks setup and wait for MSI stabilization, the MSI clock is
  161. always enabled because it is the fallback clock when PLL the fails.
  162. Trim fields are not altered from reset values.*/
  163. RCC->CFGR = 0;
  164. RCC->ICSCR = (RCC->ICSCR & ~STM32_MSIRANGE_MASK) | STM32_MSIRANGE;
  165. RCC->CR = RCC_CR_MSION;
  166. while ((RCC->CR & RCC_CR_MSIRDY) == 0)
  167. ; /* Waits until MSI is stable. */
  168. #if STM32_HSI16_ENABLED
  169. /* HSI activation.*/
  170. RCC->CR |= RCC_CR_HSION;
  171. while ((RCC->CR & RCC_CR_HSIRDY) == 0)
  172. ; /* Waits until HSI16 is stable. */
  173. #if STM32_HSI16_DIVIDER_ENABLED
  174. RCC->CR |= RCC_CR_HSIDIVEN;
  175. while ((RCC->CR & RCC_CR_HSIDIVF) == 0)
  176. ;
  177. #endif
  178. #endif
  179. #if STM32_HSE_ENABLED
  180. #if defined(STM32_HSE_BYPASS)
  181. /* HSE Bypass.*/
  182. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  183. #endif
  184. /* HSE activation.*/
  185. RCC->CR |= RCC_CR_HSEON;
  186. while ((RCC->CR & RCC_CR_HSERDY) == 0)
  187. ; /* Waits until HSE is stable. */
  188. #endif
  189. #if STM32_LSI_ENABLED
  190. /* LSI activation.*/
  191. RCC->CSR |= RCC_CSR_LSION;
  192. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  193. ; /* Waits until LSI is stable. */
  194. #endif
  195. #if STM32_LSE_ENABLED
  196. /* LSE activation, have to unlock the register.*/
  197. if ((RCC->CSR & RCC_CSR_LSEON) == 0) {
  198. PWR->CR |= PWR_CR_DBP;
  199. #if defined(STM32_LSE_BYPASS)
  200. /* LSE Bypass.*/
  201. RCC->CSR |= STM32_LSEDRV | RCC_CSR_LSEBYP;
  202. #else
  203. /* No LSE Bypass.*/
  204. RCC->CSR |= STM32_LSEDRV;
  205. #endif
  206. RCC->CSR |= RCC_CSR_LSEON;
  207. PWR->CR &= ~PWR_CR_DBP;
  208. }
  209. while ((RCC->CSR & RCC_CSR_LSERDY) == 0)
  210. ; /* Waits until LSE is stable. */
  211. #endif
  212. #if STM32_ACTIVATE_PLL
  213. /* PLL activation.*/
  214. RCC->CFGR |= STM32_PLLDIV | STM32_PLLMUL | STM32_PLLSRC;
  215. RCC->CR |= RCC_CR_PLLON;
  216. while (!(RCC->CR & RCC_CR_PLLRDY))
  217. ; /* Waits until PLL is stable. */
  218. #endif
  219. #if STM32_ACTIVATE_HSI48
  220. /* Enabling SYSCFG clock. */
  221. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  222. /* Configuring SYSCFG to enable VREFINT and HSI48 VREFINT buffer. */
  223. SYSCFG->CFGR3 = STM32_VREFINT_EN | SYSCFG_CFGR3_ENREF_HSI48;
  224. while (!(SYSCFG->CFGR3 & SYSCFG_CFGR3_VREFINT_RDYF))
  225. ; /* Waits until VREFINT is stable. */
  226. /* Disabling SYSCFG clock. */
  227. rccDisableAPB2(RCC_APB2ENR_SYSCFGEN);
  228. /* Enabling HSI48. */
  229. RCC->CRRCR |= RCC_CRRCR_HSI48ON;
  230. while (!(RCC->CRRCR & RCC_CRRCR_HSI48RDY))
  231. ; /* Waits until HSI48 is stable. */
  232. #endif
  233. /* Other clock-related settings (dividers, MCO etc).*/
  234. RCC->CR |= STM32_RTCPRE;
  235. RCC->CFGR |= STM32_MCOPRE | STM32_MCOSEL |
  236. STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
  237. RCC->CSR |= STM32_RTCSEL;
  238. /* Flash setup and final clock selection.*/
  239. #if defined(STM32_FLASHBITS)
  240. FLASH->ACR = STM32_FLASHBITS;
  241. #endif
  242. /* Switching to the configured clock source if it is different from MSI. */
  243. #if (STM32_SW != STM32_SW_MSI)
  244. RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
  245. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  246. ;
  247. #endif
  248. /* Peripherals clock sources setup.*/
  249. RCC->CCIPR = STM32_HSI48SEL | STM32_LPTIM1SEL | STM32_I2C1SEL |
  250. STM32_LPUART1SEL | STM32_USART2SEL | STM32_USART1SEL;
  251. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  252. among multiple drivers.*/
  253. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  254. #endif /* STM32_NO_INIT */
  255. }
  256. /** @} */