hal_lld.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 STM32F7xx/hal_lld.c
  15. * @brief STM32F7xx 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_stm32f7xx.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->CR1 |= PWR_CR1_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 |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  56. #else
  57. /* No LSE Bypass.*/
  58. RCC->BDCR |= STM32_LSEDRV | 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->CSR1 |= PWR_CSR1_BRE;
  76. while ((PWR->CSR1 & PWR_CSR1_BRR) == 0)
  77. ; /* Waits until the regulator is stable */
  78. #else
  79. PWR->CSR1 &= ~PWR_CSR1_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. rccResetAHB2(~0);
  100. rccResetAPB1(~RCC_APB1RSTR_PWRRST);
  101. rccResetAPB2(~0);
  102. /* Initializes the backup domain.*/
  103. hal_lld_backup_domain_init();
  104. /* DMA subsystems initialization.*/
  105. #if defined(STM32_DMA_REQUIRED)
  106. dmaInit();
  107. #endif
  108. /* IRQ subsystem initialization.*/
  109. irqInit();
  110. #if STM32_SRAM2_NOCACHE
  111. /* The SRAM2 bank can optionally made a non cache-able area for use by
  112. DMA engines.*/
  113. mpuConfigureRegion(MPU_REGION_7,
  114. SRAM2_BASE,
  115. MPU_RASR_ATTR_AP_RW_RW |
  116. MPU_RASR_ATTR_NON_CACHEABLE |
  117. MPU_RASR_SIZE_16K |
  118. MPU_RASR_ENABLE);
  119. mpuEnable(MPU_CTRL_PRIVDEFENA);
  120. /* Invalidating data cache to make sure that the MPU settings are taken
  121. immediately.*/
  122. SCB_CleanInvalidateDCache();
  123. #endif
  124. /* Programmable voltage detector enable.*/
  125. #if STM32_PVD_ENABLE
  126. PWR->CR1 |= PWR_CR1_PVDE | (STM32_PLS & STM32_PLS_MASK);
  127. #endif /* STM32_PVD_ENABLE */
  128. }
  129. /**
  130. * @brief STM32F2xx clocks and PLL initialization.
  131. * @note All the involved constants come from the file @p board.h.
  132. * @note This function should be invoked just after the system reset.
  133. *
  134. * @special
  135. */
  136. void stm32_clock_init(void) {
  137. #if !STM32_NO_INIT
  138. /* PWR clock enabled.*/
  139. #if defined(HAL_USE_RTC) && defined(RCC_APB1ENR_RTCEN)
  140. RCC->APB1ENR = RCC_APB1ENR_PWREN | RCC_APB1ENR_RTCEN;
  141. #else
  142. RCC->APB1ENR = RCC_APB1ENR_PWREN;
  143. #endif
  144. /* PWR initialization.*/
  145. PWR->CR1 = STM32_VOS;
  146. /* HSI setup, it enforces the reset situation in order to handle possible
  147. problems with JTAG probes and re-initializations.*/
  148. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  149. while (!(RCC->CR & RCC_CR_HSIRDY))
  150. ; /* Wait until HSI is stable. */
  151. /* HSI is selected as new source without touching the other fields in
  152. CFGR. Clearing the register has to be postponed after HSI is the
  153. new source.*/
  154. RCC->CFGR &= ~RCC_CFGR_SW; /* Reset SW, selecting HSI. */
  155. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  156. ; /* Wait until HSI is selected. */
  157. /* Registers finally cleared to reset values.*/
  158. RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
  159. RCC->CFGR = 0; /* CFGR reset value. */
  160. #if STM32_HSE_ENABLED
  161. /* HSE activation.*/
  162. #if defined(STM32_HSE_BYPASS)
  163. /* HSE Bypass.*/
  164. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  165. #else
  166. /* No HSE Bypass.*/
  167. RCC->CR |= RCC_CR_HSEON;
  168. #endif
  169. while ((RCC->CR & RCC_CR_HSERDY) == 0)
  170. ; /* Waits until HSE is stable. */
  171. #endif
  172. #if STM32_LSI_ENABLED
  173. /* LSI activation.*/
  174. RCC->CSR |= RCC_CSR_LSION;
  175. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  176. ; /* Waits until LSI is stable. */
  177. #endif
  178. #if STM32_ACTIVATE_PLL
  179. /* PLL activation.*/
  180. RCC->PLLCFGR = STM32_PLLQ | STM32_PLLSRC | STM32_PLLP | STM32_PLLN |
  181. STM32_PLLM;
  182. RCC->CR |= RCC_CR_PLLON;
  183. /* Synchronization with voltage regulator stabilization.*/
  184. while ((PWR->CSR1 & PWR_CSR1_VOSRDY) == 0)
  185. ; /* Waits until power regulator is stable. */
  186. #if STM32_OVERDRIVE_REQUIRED
  187. /* Overdrive activation performed after activating the PLL in order to save
  188. time as recommended in RM in "Entering Over-drive mode" paragraph.*/
  189. PWR->CR1 |= PWR_CR1_ODEN;
  190. while (!(PWR->CSR1 & PWR_CSR1_ODRDY))
  191. ;
  192. PWR->CR1 |= PWR_CR1_ODSWEN;
  193. while (!(PWR->CSR1 & PWR_CSR1_ODSWRDY))
  194. ;
  195. #endif /* STM32_OVERDRIVE_REQUIRED */
  196. /* Waiting for PLL lock.*/
  197. while (!(RCC->CR & RCC_CR_PLLRDY))
  198. ;
  199. #endif /* STM32_OVERDRIVE_REQUIRED */
  200. #if STM32_ACTIVATE_PLLI2S
  201. /* PLLI2S activation.*/
  202. RCC->PLLI2SCFGR = STM32_PLLI2SR | STM32_PLLI2SQ | STM32_PLLI2SP |
  203. STM32_PLLI2SN;
  204. RCC->CR |= RCC_CR_PLLI2SON;
  205. /* Waiting for PLL lock.*/
  206. while (!(RCC->CR & RCC_CR_PLLI2SRDY))
  207. ;
  208. #endif
  209. #if STM32_ACTIVATE_PLLSAI
  210. /* PLLSAI activation.*/
  211. RCC->PLLSAICFGR = STM32_PLLSAIR | STM32_PLLSAIQ | STM32_PLLSAIP |
  212. STM32_PLLSAIN;
  213. RCC->CR |= RCC_CR_PLLSAION;
  214. /* Waiting for PLL lock.*/
  215. while (!(RCC->CR & RCC_CR_PLLSAIRDY))
  216. ;
  217. #endif
  218. /* Other clock-related settings (dividers, MCO etc).*/
  219. RCC->CFGR = STM32_MCO2SEL | STM32_MCO2PRE | STM32_MCO1PRE | STM32_I2SSRC |
  220. STM32_MCO1SEL | STM32_RTCPRE | STM32_PPRE2 | STM32_PPRE1 |
  221. STM32_HPRE;
  222. /* DCKCFGR1 register initialization, note, must take care of the _OFF
  223. pseudo settings.*/
  224. {
  225. uint32_t dckcfgr1 = STM32_PLLI2SDIVQ | STM32_PLLSAIDIVQ | STM32_PLLSAIDIVR;
  226. #if STM32_SAI2SEL != STM32_SAI2SEL_OFF
  227. dckcfgr1 |= STM32_SAI2SEL;
  228. #endif
  229. #if STM32_SAI1SEL != STM32_SAI1SEL_OFF
  230. dckcfgr1 |= STM32_SAI1SEL;
  231. #endif
  232. RCC->DCKCFGR1 = dckcfgr1;
  233. }
  234. /* Peripheral clock sources.*/
  235. RCC->DCKCFGR2 = STM32_SDMMC2SEL | STM32_SDMMC1SEL | STM32_CK48MSEL |
  236. STM32_CECSEL | STM32_LPTIM1SEL | STM32_I2C4SEL |
  237. STM32_I2C3SEL | STM32_I2C2SEL | STM32_I2C1SEL |
  238. STM32_UART8SEL | STM32_UART7SEL | STM32_USART6SEL |
  239. STM32_UART5SEL | STM32_UART4SEL | STM32_USART3SEL |
  240. STM32_USART2SEL | STM32_USART1SEL;
  241. /* Flash setup.*/
  242. FLASH->ACR = FLASH_ACR_ARTEN | FLASH_ACR_PRFTEN | STM32_FLASHBITS;
  243. /* Switching to the configured clock source if it is different from HSI.*/
  244. #if (STM32_SW != STM32_SW_HSI)
  245. RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
  246. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  247. ;
  248. #endif
  249. #endif /* STM32_NO_INIT */
  250. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  251. among multiple drivers.*/
  252. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  253. }
  254. /** @} */