hal_lld.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 STM32F37x/hal_lld.c
  15. * @brief STM32F37x 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. }
  111. /**
  112. * @brief STM32 clocks and PLL initialization.
  113. * @note All the involved constants come from the file @p board.h.
  114. * @note This function should be invoked just after the system reset.
  115. *
  116. * @special
  117. */
  118. void stm32_clock_init(void) {
  119. #if !STM32_NO_INIT
  120. /* HSI setup, it enforces the reset situation in order to handle possible
  121. problems with JTAG probes and re-initializations.*/
  122. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  123. while (!(RCC->CR & RCC_CR_HSIRDY))
  124. ; /* Wait until HSI is stable. */
  125. /* HSI is selected as new source without touching the other fields in
  126. CFGR. Clearing the register has to be postponed after HSI is the
  127. new source.*/
  128. RCC->CFGR &= ~RCC_CFGR_SW; /* Reset SW, selecting HSI. */
  129. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  130. ; /* Wait until HSI is selected. */
  131. /* Registers finally cleared to reset values.*/
  132. RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
  133. RCC->CFGR = 0; /* CFGR reset value. */
  134. #if STM32_HSE_ENABLED
  135. /* HSE activation.*/
  136. #if defined(STM32_HSE_BYPASS)
  137. /* HSE Bypass.*/
  138. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  139. #else
  140. /* No HSE Bypass.*/
  141. RCC->CR |= RCC_CR_HSEON;
  142. #endif
  143. while (!(RCC->CR & RCC_CR_HSERDY))
  144. ; /* Waits until HSE is stable. */
  145. #endif
  146. #if STM32_LSI_ENABLED
  147. /* LSI activation.*/
  148. RCC->CSR |= RCC_CSR_LSION;
  149. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  150. ; /* Waits until LSI is stable. */
  151. #endif
  152. /* Clock settings.*/
  153. RCC->CFGR = STM32_SDPRE | STM32_MCOSEL | STM32_USBPRE |
  154. STM32_PLLMUL | STM32_PLLSRC | STM32_ADCPRE |
  155. STM32_PPRE1 | STM32_PPRE2 | STM32_HPRE;
  156. RCC->CFGR2 = STM32_PREDIV;
  157. RCC->CFGR3 = STM32_USART3SW | STM32_USART2SW | STM32_I2C2SW |
  158. STM32_I2C1SW | STM32_USART1SW;
  159. #if STM32_ACTIVATE_PLL
  160. /* PLL activation.*/
  161. RCC->CR |= RCC_CR_PLLON;
  162. while (!(RCC->CR & RCC_CR_PLLRDY))
  163. ; /* Waits until PLL is stable. */
  164. #endif
  165. /* Flash setup and final clock selection. */
  166. FLASH->ACR = STM32_FLASHBITS;
  167. /* Switching to the configured clock source if it is different from HSI.*/
  168. #if (STM32_SW != STM32_SW_HSI)
  169. /* Switches clock source.*/
  170. RCC->CFGR |= STM32_SW;
  171. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  172. ; /* Waits selection complete. */
  173. #endif
  174. #endif /* !STM32_NO_INIT */
  175. }
  176. /** @} */