hal_lld.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 STM32F4xx/hal_lld.c
  15. * @brief STM32F4xx/STM32F2xx 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_stm32f4xx.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 STM32_LSE_ENABLED
  53. #if defined(STM32_LSE_BYPASS)
  54. /* LSE Bypass.*/
  55. RCC->BDCR |= RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  56. #else
  57. /* No LSE Bypass.*/
  58. RCC->BDCR |= RCC_BDCR_LSEON;
  59. #endif
  60. while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
  61. ; /* Waits until LSE is stable. */
  62. #endif
  63. #if HAL_USE_RTC
  64. /* If the backup domain hasn't been initialized yet then proceed with
  65. initialization.*/
  66. if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
  67. /* Selects clock source.*/
  68. RCC->BDCR |= STM32_RTCSEL;
  69. /* RTC clock enabled.*/
  70. RCC->BDCR |= RCC_BDCR_RTCEN;
  71. }
  72. #endif /* HAL_USE_RTC */
  73. #if STM32_BKPRAM_ENABLE
  74. rccEnableBKPSRAM(true);
  75. PWR->CSR |= PWR_CSR_BRE;
  76. while ((PWR->CSR & PWR_CSR_BRR) == 0)
  77. ; /* Waits until the regulator is stable */
  78. #else
  79. PWR->CSR &= ~PWR_CSR_BRE;
  80. #endif /* STM32_BKPRAM_ENABLE */
  81. }
  82. /*===========================================================================*/
  83. /* Driver interrupt handlers. */
  84. /*===========================================================================*/
  85. /*===========================================================================*/
  86. /* Driver exported functions. */
  87. /*===========================================================================*/
  88. /**
  89. * @brief Low level HAL driver initialization.
  90. *
  91. * @notapi
  92. */
  93. void hal_lld_init(void) {
  94. /* Reset of all peripherals. AHB3 is not reseted because it could have
  95. been initialized in the board initialization file (board.c).
  96. Note, GPIOs are not reset because initialized before this point in
  97. board files.*/
  98. rccResetAHB1(~STM32_GPIO_EN_MASK);
  99. #if !defined(STM32F410xx)
  100. rccResetAHB2(~0);
  101. #endif
  102. rccResetAPB1(~RCC_APB1RSTR_PWRRST);
  103. rccResetAPB2(~0);
  104. /* PWR clock enabled.*/
  105. rccEnablePWRInterface(true);
  106. /* Initializes the backup domain.*/
  107. hal_lld_backup_domain_init();
  108. /* DMA subsystems initialization.*/
  109. #if defined(STM32_DMA_REQUIRED)
  110. dmaInit();
  111. #endif
  112. /* IRQ subsystem initialization.*/
  113. irqInit();
  114. /* Programmable voltage detector enable.*/
  115. #if STM32_PVD_ENABLE
  116. PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
  117. #endif /* STM32_PVD_ENABLE */
  118. }
  119. /**
  120. * @brief STM32F2xx clocks and PLL initialization.
  121. * @note All the involved constants come from the file @p board.h.
  122. * @note This function should be invoked just after the system reset.
  123. *
  124. * @special
  125. */
  126. void stm32_clock_init(void) {
  127. #if !STM32_NO_INIT
  128. /* PWR clock enable.*/
  129. #if defined(HAL_USE_RTC) && defined(RCC_APB1ENR_RTCAPBEN)
  130. RCC->APB1ENR = RCC_APB1ENR_PWREN | RCC_APB1ENR_RTCAPBEN;
  131. #else
  132. RCC->APB1ENR = RCC_APB1ENR_PWREN;
  133. #endif
  134. /* PWR initialization.*/
  135. #if defined(STM32F4XX) || defined(__DOXYGEN__)
  136. PWR->CR = STM32_VOS;
  137. #else
  138. PWR->CR = 0;
  139. #endif
  140. /* HSI setup, it enforces the reset situation in order to handle possible
  141. problems with JTAG probes and re-initializations.*/
  142. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  143. while (!(RCC->CR & RCC_CR_HSIRDY))
  144. ; /* Wait until HSI is stable. */
  145. /* HSI is selected as new source without touching the other fields in
  146. CFGR. Clearing the register has to be postponed after HSI is the
  147. new source.*/
  148. RCC->CFGR &= ~RCC_CFGR_SW; /* Reset SW, selecting HSI. */
  149. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  150. ; /* Wait until HSI is selected. */
  151. /* Registers finally cleared to reset values.*/
  152. RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
  153. RCC->CFGR = 0; /* CFGR reset value. */
  154. #if STM32_HSE_ENABLED
  155. /* HSE activation.*/
  156. #if defined(STM32_HSE_BYPASS)
  157. /* HSE Bypass.*/
  158. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  159. #else
  160. /* No HSE Bypass.*/
  161. RCC->CR |= RCC_CR_HSEON;
  162. #endif
  163. while ((RCC->CR & RCC_CR_HSERDY) == 0)
  164. ; /* Waits until HSE is stable. */
  165. #endif
  166. #if STM32_LSI_ENABLED
  167. /* LSI activation.*/
  168. RCC->CSR |= RCC_CSR_LSION;
  169. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  170. ; /* Waits until LSI is stable. */
  171. #endif
  172. #if STM32_ACTIVATE_PLL
  173. /* PLL activation.*/
  174. RCC->PLLCFGR = STM32_PLLQ | STM32_PLLSRC | STM32_PLLP | STM32_PLLN |
  175. STM32_PLLM;
  176. RCC->CR |= RCC_CR_PLLON;
  177. /* Synchronization with voltage regulator stabilization.*/
  178. #if defined(STM32F4XX)
  179. while ((PWR->CSR & PWR_CSR_VOSRDY) == 0)
  180. ; /* Waits until power regulator is stable. */
  181. #if STM32_OVERDRIVE_REQUIRED
  182. /* Overdrive activation performed after activating the PLL in order to save
  183. time as recommended in RM in "Entering Over-drive mode" paragraph.*/
  184. PWR->CR |= PWR_CR_ODEN;
  185. while (!(PWR->CSR & PWR_CSR_ODRDY))
  186. ;
  187. PWR->CR |= PWR_CR_ODSWEN;
  188. while (!(PWR->CSR & PWR_CSR_ODSWRDY))
  189. ;
  190. #endif /* STM32_OVERDRIVE_REQUIRED */
  191. #endif /* defined(STM32F4XX) */
  192. /* Waiting for PLL lock.*/
  193. while (!(RCC->CR & RCC_CR_PLLRDY))
  194. ;
  195. #endif /* STM32_ACTIVATE_PLL */
  196. #if STM32_ACTIVATE_PLLI2S
  197. /* PLLI2S activation.*/
  198. RCC->PLLI2SCFGR = STM32_PLLI2SR | STM32_PLLI2SN | STM32_PLLI2SP |
  199. STM32_PLLI2SSRC | STM32_PLLI2SQ | STM32_PLLI2SM;
  200. RCC->CR |= RCC_CR_PLLI2SON;
  201. /* Waiting for PLL lock.*/
  202. while (!(RCC->CR & RCC_CR_PLLI2SRDY))
  203. ;
  204. #endif /* STM32_ACTIVATE_PLLI2S */
  205. #if STM32_ACTIVATE_PLLSAI
  206. /* PLLSAI activation.*/
  207. RCC->PLLSAICFGR = STM32_PLLSAIR | STM32_PLLSAIN | STM32_PLLSAIP |
  208. STM32_PLLSAIQ | STM32_PLLSAIM;
  209. RCC->CR |= RCC_CR_PLLSAION;
  210. /* Waiting for PLL lock.*/
  211. while (!(RCC->CR & RCC_CR_PLLSAIRDY))
  212. ;
  213. #endif /* STM32_ACTIVATE_PLLSAI */
  214. /* Other clock-related settings (dividers, MCO etc).*/
  215. #if !defined(STM32F413xx)
  216. RCC->CFGR = STM32_MCO2PRE | STM32_MCO2SEL | STM32_MCO1PRE | STM32_MCO1SEL |
  217. STM32_I2SSRC | STM32_RTCPRE | STM32_PPRE2 | STM32_PPRE1 |
  218. STM32_HPRE;
  219. #else
  220. RCC->CFGR = STM32_MCO2PRE | STM32_MCO2SEL | STM32_MCO1PRE | STM32_MCO1SEL |
  221. STM32_RTCPRE | STM32_PPRE2 | STM32_PPRE1 |
  222. STM32_HPRE;
  223. #endif
  224. #if STM32_HAS_RCC_DCKCFGR
  225. /* DCKCFGR register initialization, note, must take care of the _OFF
  226. pseudo settings.*/
  227. {
  228. uint32_t dckcfgr = 0;
  229. #if STM32_SAI2SEL != STM32_SAI2SEL_OFF
  230. dckcfgr |= STM32_SAI2SEL;
  231. #endif
  232. #if STM32_SAI1SEL != STM32_SAI1SEL_OFF
  233. dckcfgr |= STM32_SAI1SEL;
  234. #endif
  235. #if (STM32_ACTIVATE_PLLSAI == TRUE) && \
  236. (STM32_PLLSAIDIVR != STM32_PLLSAIDIVR_OFF)
  237. dckcfgr |= STM32_PLLSAIDIVR;
  238. #endif
  239. #if defined(STM32F469xx) || defined(STM32F479xx)
  240. /* Special case, in those devices STM32_CK48MSEL is located in the
  241. DCKCFGR register.*/
  242. dckcfgr |= STM32_CK48MSEL;
  243. #endif
  244. #if !defined(STM32F413xx)
  245. RCC->DCKCFGR = dckcfgr |
  246. STM32_TIMPRE | STM32_PLLSAIDIVQ | STM32_PLLI2SDIVQ;
  247. #else
  248. RCC->DCKCFGR = dckcfgr |
  249. STM32_TIMPRE | STM32_PLLDIVR | STM32_PLLI2SDIVR;
  250. #endif
  251. }
  252. #endif
  253. #if STM32_HAS_RCC_DCKCFGR2
  254. /* DCKCFGR2 register initialization.*/
  255. RCC->DCKCFGR2 = STM32_CK48MSEL;
  256. #endif
  257. /* Flash setup.*/
  258. #if !defined(STM32_REMOVE_REVISION_A_FIX)
  259. /* Some old revisions of F4x MCUs randomly crashes with compiler
  260. optimizations enabled AND flash caches enabled. */
  261. if ((DBGMCU->IDCODE == 0x20006411) && (SCB->CPUID == 0x410FC241))
  262. FLASH->ACR = FLASH_ACR_PRFTEN | STM32_FLASHBITS;
  263. else
  264. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |
  265. FLASH_ACR_DCEN | STM32_FLASHBITS;
  266. #else
  267. FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |
  268. FLASH_ACR_DCEN | STM32_FLASHBITS;
  269. #endif
  270. /* Switching to the configured clock source if it is different from HSI.*/
  271. #if (STM32_SW != STM32_SW_HSI)
  272. RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
  273. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  274. ;
  275. #endif
  276. #endif /* STM32_NO_INIT */
  277. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  278. among multiple drivers.*/
  279. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  280. }
  281. /** @} */