hal_lld.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 STM32H7xx/hal_lld.c
  15. * @brief STM32H7xx 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_CORE_CK;
  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 inline void init_bkp_domain(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. }
  74. /**
  75. * @brief Initializes the PWR unit.
  76. */
  77. static inline void init_pwr(void) {
  78. #if 0
  79. PWR_TypeDef *pwr = PWR; /* For inspection.*/
  80. (void)pwr;
  81. #endif
  82. PWR->CR1 = STM32_PWR_CR1 | 0xF0000000;
  83. PWR->CR2 = STM32_PWR_CR2;
  84. PWR->CR3 = STM32_PWR_CR3;
  85. PWR->CPUCR = STM32_PWR_CPUCR;
  86. PWR->D3CR = STM32_VOS;
  87. while ((PWR->D3CR & PWR_D3CR_VOSRDY) == 0)
  88. ;
  89. #if STM32_PWR_CR2 & PWR_CR2_BREN
  90. // while ((PWR->CR2 & PWR_CR2_BRRDY) == 0)
  91. // ;
  92. // rccEnableBKPRAM(true);
  93. #endif
  94. }
  95. /*===========================================================================*/
  96. /* Driver interrupt handlers. */
  97. /*===========================================================================*/
  98. /*===========================================================================*/
  99. /* Driver exported functions. */
  100. /*===========================================================================*/
  101. /**
  102. * @brief Low level HAL driver initialization.
  103. *
  104. * @notapi
  105. */
  106. void hal_lld_init(void) {
  107. /* Reset of all peripherals. AHB3 is not reset entirely because FMC could
  108. have been initialized in the board initialization file (board.c).
  109. Note, GPIOs are not reset because initialized before this point in
  110. board files.*/
  111. rccResetAHB1(~0);
  112. rccResetAHB2(~0);
  113. rccResetAHB3(~(RCC_AHB3RSTR_CPURST | RCC_AHB3RSTR_FMCRST));
  114. rccResetAHB4(~(STM32_GPIO_EN_MASK));
  115. rccResetAPB1L(~0);
  116. rccResetAPB1H(~0);
  117. rccResetAPB2(~0);
  118. rccResetAPB3(~0);
  119. rccResetAPB4(~0);
  120. /* DMA subsystems initialization.*/
  121. #if defined(STM32_BDMA_REQUIRED)
  122. bdmaInit();
  123. #endif
  124. #if defined(STM32_DMA_REQUIRED)
  125. dmaInit();
  126. #endif
  127. /* IRQ subsystem initialization.*/
  128. irqInit();
  129. /* MPU initialization.*/
  130. #if (STM32_NOCACHE_SRAM1_SRAM2 == TRUE) || (STM32_NOCACHE_SRAM3 == TRUE)
  131. {
  132. uint32_t base, size;
  133. #if (STM32_NOCACHE_SRAM1_SRAM2 == TRUE) && (STM32_NOCACHE_SRAM3 == TRUE)
  134. base = 0x30000000U;
  135. size = MPU_RASR_SIZE_512K;
  136. #elif (STM32_NOCACHE_SRAM1_SRAM2 == TRUE) && (STM32_NOCACHE_SRAM3 == FALSE)
  137. base = 0x30000000U;
  138. size = MPU_RASR_SIZE_256K;
  139. #elif (STM32_NOCACHE_SRAM1_SRAM2 == FALSE) && (STM32_NOCACHE_SRAM3 == TRUE)
  140. base = 0x30040000U;
  141. size = MPU_RASR_SIZE_16K;
  142. #else
  143. #error "invalid constants used in mcuconf.h"
  144. #endif
  145. /* The SRAM2 bank can optionally made a non cache-able area for use by
  146. DMA engines.*/
  147. mpuConfigureRegion(MPU_REGION_7,
  148. base,
  149. MPU_RASR_ATTR_AP_RW_RW |
  150. MPU_RASR_ATTR_NON_CACHEABLE |
  151. size |
  152. MPU_RASR_ENABLE);
  153. mpuEnable(MPU_CTRL_PRIVDEFENA);
  154. /* Invalidating data cache to make sure that the MPU settings are taken
  155. immediately.*/
  156. SCB_CleanInvalidateDCache();
  157. }
  158. #endif
  159. }
  160. /**
  161. * @brief STM32H7xx clocks and PLL initialization.
  162. * @note All the involved constants come from the file @p board.h.
  163. * @note This function should be invoked just after the system reset.
  164. *
  165. * @special
  166. */
  167. void stm32_clock_init(void) {
  168. uint32_t cfgr;
  169. #if 0
  170. RCC_TypeDef *rcc = RCC; /* For inspection.*/
  171. (void)rcc;
  172. #endif
  173. #if STM32_NO_INIT == FALSE
  174. #if !defined(STM32_DISABLE_ERRATA_2_2_15)
  175. /* Fix for errata 2.2.15: Reading from AXI SRAM might lead to data
  176. read corruption.
  177. AXI->TARG7_FN_MOD.*/
  178. *((volatile uint32_t *)(0x51000000 + 0x1108 + 0x7000)) = 0x00000001U;
  179. #endif
  180. /* PWR initialization.*/
  181. init_pwr();
  182. /* Backup domain initialization.*/
  183. init_bkp_domain();
  184. /* HSI setup, it enforces the reset situation in order to handle possible
  185. problems with JTAG probes and re-initializations.*/
  186. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  187. while (!(RCC->CR & RCC_CR_HSIRDY))
  188. ; /* Wait until HSI is stable. */
  189. /* HSI is selected as new source without touching the other fields in
  190. CFGR. This is only required when using a debugger than can cause
  191. restarts.*/
  192. RCC->CFGR = 0x00000000U; /* Reset SW to HSI. */
  193. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  194. ; /* Wait until HSI is selected. */
  195. /* Registers cleared to reset values.*/
  196. RCC->CR = RCC_CR_HSION; /* CR Reset value. */
  197. RCC->ICSCR = 0x40000000U; /* ICSCR Reset value. */
  198. RCC->CSR = 0x00000000U; /* CSR reset value. */
  199. RCC->PLLCFGR = 0x01FF0000U; /* PLLCFGR reset value. */
  200. /* Other clock-related settings, done before other things because
  201. recommended in the RM.*/
  202. cfgr = STM32_MCO2SEL | RCC_CFGR_MCO2PRE_VALUE(STM32_MCO2PRE_VALUE) |
  203. STM32_MCO1SEL | RCC_CFGR_MCO1PRE_VALUE(STM32_MCO1PRE_VALUE) |
  204. RCC_CFGR_RTCPRE_VALUE(STM32_RTCPRE_VALUE) |
  205. STM32_HRTIMSEL | STM32_STOPKERWUCK | STM32_STOPWUCK;
  206. #if STM32_TIMPRE_ENABLE == TRUE
  207. cfgr |= RCC_CFGR_TIMPRE;
  208. #endif
  209. RCC->CFGR = cfgr;
  210. /* HSE activation with optional bypass.*/
  211. #if STM32_HSE_ENABLED == TRUE
  212. #if defined(STM32_HSE_BYPASS)
  213. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  214. #else
  215. RCC->CR |= RCC_CR_HSEON;
  216. #endif
  217. while ((RCC->CR & RCC_CR_HSERDY) == 0)
  218. ; /* Waits until HSE is stable. */
  219. #endif
  220. /* CSI activation.*/
  221. #if STM32_CSI_ENABLED == TRUE
  222. RCC->CR |= RCC_CR_CSION;
  223. while ((RCC->CR & RCC_CR_CSIRDY) == 0)
  224. ; /* Waits until CSI is stable. */
  225. #endif /* STM32_HSE_ENABLED == TRUE */
  226. /* LSI activation.*/
  227. #if STM32_LSI_ENABLED == TRUE
  228. RCC->CSR |= RCC_CSR_LSION;
  229. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  230. ; /* Waits until LSI is stable. */
  231. #endif /* STM32_LSI_ENABLED == TRUE */
  232. /* PLLs activation, it happens in parallel in order to
  233. reduce boot time.*/
  234. #if (STM32_PLL1_ENABLED == TRUE) || \
  235. (STM32_PLL2_ENABLED == TRUE) || \
  236. (STM32_PLL3_ENABLED == TRUE)
  237. {
  238. uint32_t onmask = 0;
  239. uint32_t rdymask = 0;
  240. uint32_t cfgmask = 0;
  241. RCC->PLLCKSELR = RCC_PLLCKSELR_DIVM3_VALUE(STM32_PLL3_DIVM_VALUE) |
  242. RCC_PLLCKSELR_DIVM2_VALUE(STM32_PLL2_DIVM_VALUE) |
  243. RCC_PLLCKSELR_DIVM1_VALUE(STM32_PLL1_DIVM_VALUE) |
  244. RCC_PLLCKSELR_PLLSRC_VALUE(STM32_PLLSRC);
  245. cfgmask = STM32_PLLCFGR_PLL3RGE | STM32_PLLCFGR_PLL3VCOSEL | RCC_PLLCFGR_PLL3FRACEN |
  246. STM32_PLLCFGR_PLL2RGE | STM32_PLLCFGR_PLL2VCOSEL | RCC_PLLCFGR_PLL2FRACEN |
  247. STM32_PLLCFGR_PLL1RGE | STM32_PLLCFGR_PLL1VCOSEL | RCC_PLLCFGR_PLL1FRACEN;
  248. #if STM32_PLL1_ENABLED == TRUE
  249. RCC->PLL1FRACR = STM32_PLL1_FRACN;
  250. RCC->PLL1DIVR = STM32_PLL1_DIVR | STM32_PLL1_DIVQ |
  251. STM32_PLL1_DIVP | STM32_PLL1_DIVN;
  252. onmask |= RCC_CR_PLL1ON;
  253. rdymask |= RCC_CR_PLL1RDY;
  254. #if STM32_PLL1_P_ENABLED == TRUE
  255. cfgmask |= RCC_PLLCFGR_DIVP1EN;
  256. #endif
  257. #if STM32_PLL1_Q_ENABLED == TRUE
  258. cfgmask |= RCC_PLLCFGR_DIVQ1EN;
  259. #endif
  260. #if STM32_PLL1_R_ENABLED == TRUE
  261. cfgmask |= RCC_PLLCFGR_DIVR1EN;
  262. #endif
  263. #endif /* STM32_PLL1_ENABLED == TRUE */
  264. #if STM32_PLL2_ENABLED == TRUE
  265. RCC->PLL2FRACR = STM32_PLL2_FRACN;
  266. RCC->PLL2DIVR = STM32_PLL2_DIVR | STM32_PLL2_DIVQ |
  267. STM32_PLL2_DIVP | STM32_PLL2_DIVN;
  268. onmask |= RCC_CR_PLL2ON;
  269. rdymask |= RCC_CR_PLL2RDY;
  270. #if STM32_PLL2_P_ENABLED == TRUE
  271. cfgmask |= RCC_PLLCFGR_DIVP2EN;
  272. #endif
  273. #if STM32_PLL2_Q_ENABLED == TRUE
  274. cfgmask |= RCC_PLLCFGR_DIVQ2EN;
  275. #endif
  276. #if STM32_PLL2_R_ENABLED == TRUE
  277. cfgmask |= RCC_PLLCFGR_DIVR2EN;
  278. #endif
  279. #endif /* STM32_PLL2_ENABLED == TRUE */
  280. #if STM32_PLL3_ENABLED == TRUE
  281. RCC->PLL3FRACR = STM32_PLL3_FRACN;
  282. RCC->PLL3DIVR = STM32_PLL3_DIVR | STM32_PLL3_DIVQ |
  283. STM32_PLL3_DIVP | STM32_PLL3_DIVN;
  284. onmask |= RCC_CR_PLL3ON;
  285. rdymask |= RCC_CR_PLL3RDY;
  286. #if STM32_PLL3_P_ENABLED == TRUE
  287. cfgmask |= RCC_PLLCFGR_DIVP3EN;
  288. #endif
  289. #if STM32_PLL3_Q_ENABLED == TRUE
  290. cfgmask |= RCC_PLLCFGR_DIVQ3EN;
  291. #endif
  292. #if STM32_PLL3_R_ENABLED == TRUE
  293. cfgmask |= RCC_PLLCFGR_DIVR3EN;
  294. #endif
  295. #endif /* STM32_PLL3_ENABLED == TRUE */
  296. /* Activating enabled PLLs and waiting for all of them to become ready.*/
  297. RCC->PLLCFGR = cfgmask & STM32_PLLCFGR_MASK;
  298. RCC->CR |= onmask;
  299. while ((RCC->CR & rdymask) != rdymask)
  300. ;
  301. }
  302. #endif /* STM32_PLL1_ENABLED || STM32_PLL2_ENABLED || STM32_PLL3_ENABLED */
  303. /* AHB and APB dividers.*/
  304. RCC->D1CFGR = STM32_D1CPRE | STM32_D1PPRE3 | STM32_D1HPRE;
  305. RCC->D2CFGR = STM32_D2PPRE2 | STM32_D2PPRE1;
  306. RCC->D3CFGR = STM32_D3PPRE4;
  307. /* Peripherals clocks.*/
  308. RCC->D1CCIPR = STM32_CKPERSEL | STM32_SDMMCSEL | STM32_QSPISEL |
  309. STM32_FMCSEL;
  310. RCC->D2CCIP1R = STM32_SWPSEL | STM32_FDCANSEL | STM32_DFSDM1SEL |
  311. STM32_SPDIFSEL | STM32_SPDIFSEL | STM32_SPI45SEL |
  312. STM32_SPI123SEL | STM32_SAI23SEL | STM32_SAI1SEL;
  313. RCC->D2CCIP2R = STM32_LPTIM1SEL | STM32_CECSEL | STM32_USBSEL |
  314. STM32_I2C123SEL | STM32_RNGSEL | STM32_USART16SEL |
  315. STM32_USART234578SEL;
  316. RCC->D3CCIPR = STM32_SPI6SEL | STM32_SAI4BSEL | STM32_SAI4ASEL |
  317. STM32_ADCSEL | STM32_LPTIM345SEL | STM32_LPTIM2SEL |
  318. STM32_I2C4SEL | STM32_LPUART1SEL;
  319. /* Flash setup.*/
  320. FLASH->ACR = FLASH_ACR_WRHIGHFREQ_2 | STM32_FLASHBITS;
  321. /* Switching to the configured clock source if it is different
  322. from HSI.*/
  323. #if STM32_SW != STM32_SW_HSI_CK
  324. RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */
  325. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 3U))
  326. ;
  327. #endif
  328. #if 0
  329. /* Peripheral clock sources.*/
  330. RCC->DCKCFGR2 = STM32_SDMMCSEL | STM32_CK48MSEL | STM32_CECSEL |
  331. STM32_LPTIM1SEL | STM32_I2C4SEL | STM32_I2C3SEL |
  332. STM32_I2C2SEL | STM32_I2C1SEL | STM32_UART8SEL |
  333. STM32_UART7SEL | STM32_USART6SEL | STM32_UART5SEL |
  334. STM32_UART4SEL | STM32_USART3SEL | STM32_USART2SEL |
  335. STM32_USART1SEL;
  336. #endif
  337. #endif /* STM32_NO_INIT */
  338. /* RAM1 2 and 3 clocks enabled.*/
  339. rccEnableSRAM1(true);
  340. rccEnableSRAM2(true);
  341. rccEnableSRAM3(true);
  342. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  343. among multiple drivers.*/
  344. rccEnableAPB4(RCC_APB4ENR_SYSCFGEN, true);
  345. }
  346. /** @} */