hal_lld.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 STM32F3xx/hal_lld.c
  15. * @brief STM32F3xx 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_stm32f3xx.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. /* Reset BKP domain if different clock source selected.*/
  47. if ((RCC->BDCR & STM32_RTCSEL_MASK) != STM32_RTCSEL){
  48. /* Backup domain reset.*/
  49. RCC->BDCR = RCC_BDCR_BDRST;
  50. RCC->BDCR = 0;
  51. }
  52. /* If enabled then the LSE is started.*/
  53. #if STM32_LSE_ENABLED
  54. #if defined(STM32_LSE_BYPASS)
  55. /* LSE Bypass.*/
  56. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  57. #else
  58. /* No LSE Bypass.*/
  59. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON;
  60. #endif
  61. while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
  62. ; /* Waits until LSE is stable. */
  63. #endif
  64. #if STM32_RTCSEL != STM32_RTCSEL_NOCLOCK
  65. /* If the backup domain hasn't been initialized yet then proceed with
  66. initialization.*/
  67. if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
  68. /* Selects clock source.*/
  69. RCC->BDCR |= STM32_RTCSEL;
  70. /* RTC clock enabled.*/
  71. RCC->BDCR |= RCC_BDCR_RTCEN;
  72. }
  73. #endif /* STM32_RTCSEL != STM32_RTCSEL_NOCLOCK */
  74. }
  75. /*===========================================================================*/
  76. /* Driver interrupt handlers. */
  77. /*===========================================================================*/
  78. /*===========================================================================*/
  79. /* Driver exported functions. */
  80. /*===========================================================================*/
  81. /**
  82. * @brief Low level HAL driver initialization.
  83. *
  84. * @notapi
  85. */
  86. void hal_lld_init(void) {
  87. /* Reset of all peripherals.
  88. Note, GPIOs are not reset because initialized before this point in
  89. board files.*/
  90. rccResetAHB(~STM32_GPIO_EN_MASK);
  91. rccResetAPB1(0xFFFFFFFF);
  92. rccResetAPB2(0xFFFFFFFF);
  93. /* PWR clock enabled.*/
  94. rccEnablePWRInterface(true);
  95. /* Initializes the backup domain.*/
  96. hal_lld_backup_domain_init();
  97. /* DMA subsystems initialization.*/
  98. #if defined(STM32_DMA_REQUIRED)
  99. dmaInit();
  100. #endif
  101. /* IRQ subsystem initialization.*/
  102. irqInit();
  103. /* Programmable voltage detector enable.*/
  104. #if STM32_PVD_ENABLE
  105. PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
  106. #endif /* STM32_PVD_ENABLE */
  107. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  108. among multiple drivers.*/
  109. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  110. #if STM32_HAS_USB
  111. /* USB IRQ relocated to not conflict with CAN.*/
  112. SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP;
  113. #endif
  114. }
  115. /**
  116. * @brief STM32 clocks and PLL initialization.
  117. * @note All the involved constants come from the file @p board.h.
  118. * @note This function should be invoked just after the system reset.
  119. *
  120. * @special
  121. */
  122. void stm32_clock_init(void) {
  123. #if !STM32_NO_INIT
  124. /* HSI setup, it enforces the reset situation in order to handle possible
  125. problems with JTAG probes and re-initializations.*/
  126. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  127. while (!(RCC->CR & RCC_CR_HSIRDY))
  128. ; /* Wait until HSI is stable. */
  129. /* HSI is selected as new source without touching the other fields in
  130. CFGR. Clearing the register has to be postponed after HSI is the
  131. new source.*/
  132. RCC->CFGR &= ~RCC_CFGR_SW; /* Reset SW, selecting HSI. */
  133. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  134. ; /* Wait until HSI is selected. */
  135. /* Registers finally cleared to reset values.*/
  136. RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
  137. RCC->CFGR = 0; /* CFGR reset value. */
  138. #if STM32_HSE_ENABLED
  139. /* HSE activation.*/
  140. #if defined(STM32_HSE_BYPASS)
  141. /* HSE Bypass.*/
  142. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  143. #else
  144. /* No HSE Bypass.*/
  145. RCC->CR |= RCC_CR_HSEON;
  146. #endif
  147. while (!(RCC->CR & RCC_CR_HSERDY))
  148. ; /* Waits until HSE is stable. */
  149. #endif
  150. #if STM32_LSI_ENABLED
  151. /* LSI activation.*/
  152. RCC->CSR |= RCC_CSR_LSION;
  153. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  154. ; /* Waits until LSI is stable. */
  155. #endif
  156. /* Clock settings.*/
  157. RCC->CFGR = STM32_MCOSEL | STM32_USBPRE | STM32_PLLMUL |
  158. STM32_PLLSRC | STM32_PPRE1 | STM32_PPRE2 |
  159. STM32_HPRE;
  160. RCC->CFGR2 = STM32_ADC34PRES | STM32_ADC12PRES | STM32_PREDIV;
  161. RCC->CFGR3 = STM32_UART5SW | STM32_UART4SW | STM32_USART3SW |
  162. STM32_USART2SW | STM32_I2C2SW | STM32_I2C1SW |
  163. STM32_USART1SW;
  164. #if STM32_ACTIVATE_PLL
  165. /* PLL activation.*/
  166. RCC->CR |= RCC_CR_PLLON;
  167. while (!(RCC->CR & RCC_CR_PLLRDY))
  168. ; /* Waits until PLL is stable. */
  169. #endif
  170. /* Flash setup and final clock selection. */
  171. FLASH->ACR = STM32_FLASHBITS;
  172. /* Switching to the configured clock source if it is different from HSI.*/
  173. #if (STM32_SW != STM32_SW_HSI)
  174. /* Switches clock source.*/
  175. RCC->CFGR |= STM32_SW;
  176. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  177. ; /* Waits selection complete. */
  178. #endif
  179. /* After PLL activation because the special requirements for TIM1 and
  180. TIM8 bits.*/
  181. RCC->CFGR3 |= STM32_HRTIM1SW | STM32_TIM8SW | STM32_TIM1SW;
  182. #endif /* !STM32_NO_INIT */
  183. }
  184. /** @} */