hal_lld.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 STM32F1xx/hal_lld.c
  15. * @brief STM32F1xx 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_stm32f10x.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 clock source impossible without resetting
  41. * of the whole BKP domain.
  42. */
  43. static void hal_lld_backup_domain_init(void) {
  44. /* Backup domain access enabled and left open.*/
  45. PWR->CR |= PWR_CR_DBP;
  46. #if HAL_USE_RTC
  47. /* Reset BKP domain if different clock source selected.*/
  48. if ((RCC->BDCR & STM32_RTCSEL_MASK) != STM32_RTCSEL){
  49. /* Backup domain reset.*/
  50. RCC->BDCR = RCC_BDCR_BDRST;
  51. RCC->BDCR = 0;
  52. }
  53. /* If enabled then the LSE is started.*/
  54. #if STM32_LSE_ENABLED
  55. #if defined(STM32_LSE_BYPASS)
  56. /* LSE Bypass.*/
  57. RCC->BDCR |= RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  58. #else
  59. /* No LSE Bypass.*/
  60. RCC->BDCR |= RCC_BDCR_LSEON;
  61. #endif
  62. while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
  63. ; /* Waits until LSE is stable. */
  64. #endif /* STM32_LSE_ENABLED */
  65. #if STM32_RTCSEL != STM32_RTCSEL_NOCLOCK
  66. /* If the backup domain hasn't been initialized yet then proceed with
  67. initialization.*/
  68. if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
  69. /* Selects clock source.*/
  70. RCC->BDCR |= STM32_RTCSEL;
  71. /* Prescaler value loaded in registers.*/
  72. rtc_lld_set_prescaler();
  73. /* RTC clock enabled.*/
  74. RCC->BDCR |= RCC_BDCR_RTCEN;
  75. }
  76. #endif /* STM32_RTCSEL != STM32_RTCSEL_NOCLOCK */
  77. #endif /* HAL_USE_RTC */
  78. }
  79. /*===========================================================================*/
  80. /* Driver interrupt handlers. */
  81. /*===========================================================================*/
  82. #if defined(STM32_DMA_REQUIRED) || defined(__DOXYGEN__)
  83. #if defined(STM32_DMA2_CH45_HANDLER) || defined(__DOXYGEN__)
  84. /**
  85. * @brief DMA2 streams 4 and 5 shared ISR.
  86. *
  87. * @isr
  88. */
  89. OSAL_IRQ_HANDLER(STM32_DMA2_CH45_HANDLER) {
  90. OSAL_IRQ_PROLOGUE();
  91. /* Check on channel 4 of DMA2.*/
  92. dmaServeInterrupt(STM32_DMA2_STREAM4);
  93. /* Check on channel 5 of DMA2.*/
  94. dmaServeInterrupt(STM32_DMA2_STREAM5);
  95. OSAL_IRQ_EPILOGUE();
  96. }
  97. #endif /* defined(STM32_DMA2_CH45_HANDLER) */
  98. #endif /* defined(STM32_DMA_REQUIRED) */
  99. /*===========================================================================*/
  100. /* Driver exported functions. */
  101. /*===========================================================================*/
  102. /**
  103. * @brief Low level HAL driver initialization.
  104. *
  105. * @notapi
  106. */
  107. void hal_lld_init(void) {
  108. /* Reset of all peripherals.*/
  109. rccResetAPB1(0xFFFFFFFF);
  110. rccResetAPB2(0xFFFFFFFF);
  111. /* PWR and BD clocks enabled.*/
  112. rccEnablePWRInterface(true);
  113. rccEnableBKPInterface(true);
  114. /* Initializes the backup domain.*/
  115. hal_lld_backup_domain_init();
  116. /* DMA subsystems initialization.*/
  117. #if defined(STM32_DMA_REQUIRED)
  118. dmaInit();
  119. #endif
  120. /* IRQ subsystem initialization.*/
  121. irqInit();
  122. /* Programmable voltage detector enable.*/
  123. #if STM32_PVD_ENABLE
  124. PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
  125. #endif /* STM32_PVD_ENABLE */
  126. }
  127. /**
  128. * @brief STM32 clocks and PLL initialization.
  129. * @note All the involved constants come from the file @p board.h.
  130. * @note This function should be invoked just after the system reset.
  131. *
  132. * @special
  133. */
  134. #if defined(STM32F10X_LD) || defined(STM32F10X_LD_VL) || \
  135. defined(STM32F10X_MD) || defined(STM32F10X_MD_VL) || \
  136. defined(STM32F10X_HD) || defined(STM32F10X_XL) || \
  137. defined(__DOXYGEN__)
  138. /*
  139. * Clocks initialization for all sub-families except CL.
  140. */
  141. void stm32_clock_init(void) {
  142. #if !STM32_NO_INIT
  143. /* HSI setup, it enforces the reset situation in order to handle possible
  144. problems with JTAG probes and re-initializations.*/
  145. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  146. while (!(RCC->CR & RCC_CR_HSIRDY))
  147. ; /* Wait until HSI is stable. */
  148. RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
  149. RCC->CFGR = 0; /* CFGR reset value. */
  150. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  151. ; /* Waits until HSI is selected. */
  152. #if STM32_HSE_ENABLED
  153. #if defined(STM32_HSE_BYPASS)
  154. /* HSE Bypass.*/
  155. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  156. #endif
  157. /* HSE activation.*/
  158. RCC->CR |= RCC_CR_HSEON;
  159. while (!(RCC->CR & RCC_CR_HSERDY))
  160. ; /* Waits until HSE is stable. */
  161. #endif
  162. #if STM32_LSI_ENABLED
  163. /* LSI activation.*/
  164. RCC->CSR |= RCC_CSR_LSION;
  165. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  166. ; /* Waits until LSI is stable. */
  167. #endif
  168. #if STM32_ACTIVATE_PLL
  169. /* PLL activation.*/
  170. RCC->CFGR |= STM32_PLLMUL | STM32_PLLXTPRE | STM32_PLLSRC;
  171. RCC->CR |= RCC_CR_PLLON;
  172. while (!(RCC->CR & RCC_CR_PLLRDY))
  173. ; /* Waits until PLL is stable. */
  174. #endif
  175. /* Clock settings.*/
  176. #if STM32_HAS_USB
  177. RCC->CFGR = STM32_MCOSEL | STM32_USBPRE | STM32_PLLMUL | STM32_PLLXTPRE |
  178. STM32_PLLSRC | STM32_ADCPRE | STM32_PPRE2 | STM32_PPRE1 |
  179. STM32_HPRE;
  180. #else
  181. RCC->CFGR = STM32_MCOSEL | STM32_PLLMUL | STM32_PLLXTPRE |
  182. STM32_PLLSRC | STM32_ADCPRE | STM32_PPRE2 | STM32_PPRE1 |
  183. STM32_HPRE;
  184. #endif
  185. /* Flash setup and final clock selection. */
  186. FLASH->ACR = STM32_FLASHBITS;
  187. /* Switching to the configured clock source if it is different from HSI.*/
  188. #if (STM32_SW != STM32_SW_HSI)
  189. /* Switches clock source.*/
  190. RCC->CFGR |= STM32_SW;
  191. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  192. ; /* Waits selection complete. */
  193. #endif
  194. #if !STM32_HSI_ENABLED
  195. RCC->CR &= ~RCC_CR_HSION;
  196. #endif
  197. #endif /* !STM32_NO_INIT */
  198. }
  199. #elif defined(STM32F10X_CL)
  200. /*
  201. * Clocks initialization for the CL sub-family.
  202. */
  203. void stm32_clock_init(void) {
  204. #if !STM32_NO_INIT
  205. /* HSI setup, it enforces the reset situation in order to handle possible
  206. problems with JTAG probes and re-initializations.*/
  207. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  208. while (!(RCC->CR & RCC_CR_HSIRDY))
  209. ; /* Wait until HSI is stable. */
  210. /* HSI is selected as new source without touching the other fields in
  211. CFGR. Clearing the register has to be postponed after HSI is the
  212. new source.*/
  213. RCC->CFGR &= ~RCC_CFGR_SW; /* Reset SW, selecting HSI. */
  214. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  215. ; /* Wait until HSI is selected. */
  216. /* Registers finally cleared to reset values.*/
  217. RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
  218. RCC->CFGR = 0; /* CFGR reset value. */
  219. #if STM32_HSE_ENABLED
  220. #if defined(STM32_HSE_BYPASS)
  221. /* HSE Bypass.*/
  222. RCC->CR |= RCC_CR_HSEBYP;
  223. #endif
  224. /* HSE activation.*/
  225. RCC->CR |= RCC_CR_HSEON;
  226. while (!(RCC->CR & RCC_CR_HSERDY))
  227. ; /* Waits until HSE is stable. */
  228. #endif
  229. #if STM32_LSI_ENABLED
  230. /* LSI activation.*/
  231. RCC->CSR |= RCC_CSR_LSION;
  232. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  233. ; /* Waits until LSI is stable. */
  234. #endif
  235. /* Settings of various dividers and multipliers in CFGR2.*/
  236. RCC->CFGR2 = STM32_PLL3MUL | STM32_PLL2MUL | STM32_PREDIV2 |
  237. STM32_PREDIV1 | STM32_PREDIV1SRC;
  238. /* PLL2 setup, if activated.*/
  239. #if STM32_ACTIVATE_PLL2
  240. RCC->CR |= RCC_CR_PLL2ON;
  241. while (!(RCC->CR & RCC_CR_PLL2RDY))
  242. ; /* Waits until PLL2 is stable. */
  243. #endif
  244. /* PLL3 setup, if activated.*/
  245. #if STM32_ACTIVATE_PLL3
  246. RCC->CR |= RCC_CR_PLL3ON;
  247. while (!(RCC->CR & RCC_CR_PLL3RDY))
  248. ; /* Waits until PLL3 is stable. */
  249. #endif
  250. /* PLL1 setup, if activated.*/
  251. #if STM32_ACTIVATE_PLL1
  252. RCC->CFGR |= STM32_PLLMUL | STM32_PLLSRC;
  253. RCC->CR |= RCC_CR_PLLON;
  254. while (!(RCC->CR & RCC_CR_PLLRDY))
  255. ; /* Waits until PLL1 is stable. */
  256. #endif
  257. /* Clock settings.*/
  258. #if STM32_HAS_OTG1
  259. RCC->CFGR = STM32_MCOSEL | STM32_OTGFSPRE | STM32_PLLMUL | STM32_PLLSRC |
  260. STM32_ADCPRE | STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
  261. #else
  262. RCC->CFGR = STM32_MCO | STM32_PLLMUL | STM32_PLLSRC |
  263. STM32_ADCPRE | STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
  264. #endif
  265. /* Flash setup and final clock selection. */
  266. FLASH->ACR = STM32_FLASHBITS; /* Flash wait states depending on clock. */
  267. /* Switching to the configured clock source if it is different from HSI.*/
  268. #if (STM32_SW != STM32_SW_HSI)
  269. RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
  270. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  271. ;
  272. #endif
  273. #if !STM32_HSI_ENABLED
  274. RCC->CR &= ~RCC_CR_HSION;
  275. #endif
  276. #endif /* !STM32_NO_INIT */
  277. }
  278. #else
  279. void stm32_clock_init(void) {}
  280. #endif
  281. /** @} */