hal_lld.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 STM32F0xx/hal_lld.c
  15. * @brief STM32F0xx HAL subsystem low level driver source.
  16. *
  17. * @addtogroup HAL
  18. * @{
  19. */
  20. #include "hal.h"
  21. /*===========================================================================*/
  22. /* Driver local definitions. */
  23. /*===========================================================================*/
  24. #define STM32_PLLXTPRE_OFFSET 17 /**< PLLXTPRE offset */
  25. #define STM32_PLLXTPRE_MASK 0x01 /**< PLLXTPRE mask */
  26. /*===========================================================================*/
  27. /* Driver exported variables. */
  28. /*===========================================================================*/
  29. /**
  30. * @brief CMSIS system core clock variable.
  31. * @note It is declared in system_stm32f0xx.h.
  32. */
  33. uint32_t SystemCoreClock = STM32_HCLK;
  34. /*===========================================================================*/
  35. /* Driver local variables and types. */
  36. /*===========================================================================*/
  37. /*===========================================================================*/
  38. /* Driver local functions. */
  39. /*===========================================================================*/
  40. /**
  41. * @brief Initializes the backup domain.
  42. * @note WARNING! Changing clock source impossible without resetting
  43. * of the whole BKP domain.
  44. */
  45. static void hal_lld_backup_domain_init(void) {
  46. /* Backup domain access enabled and left open.*/
  47. PWR->CR |= PWR_CR_DBP;
  48. /* Reset BKP domain if different clock source selected.*/
  49. if ((RCC->BDCR & STM32_RTCSEL_MASK) != STM32_RTCSEL){
  50. /* Backup domain reset.*/
  51. RCC->BDCR = RCC_BDCR_BDRST;
  52. RCC->BDCR = 0;
  53. }
  54. /* If enabled then the LSE is started.*/
  55. #if STM32_LSE_ENABLED
  56. #if defined(STM32_LSE_BYPASS)
  57. /* LSE Bypass.*/
  58. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
  59. #else
  60. /* No LSE Bypass.*/
  61. RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON;
  62. #endif
  63. while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
  64. ; /* Waits until LSE is stable. */
  65. #endif
  66. #if STM32_RTCSEL != STM32_RTCSEL_NOCLOCK
  67. /* If the backup domain hasn't been initialized yet then proceed with
  68. initialization.*/
  69. if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
  70. /* Selects clock source.*/
  71. RCC->BDCR |= STM32_RTCSEL;
  72. /* RTC clock enabled.*/
  73. RCC->BDCR |= RCC_BDCR_RTCEN;
  74. }
  75. #endif /* STM32_RTCSEL != STM32_RTCSEL_NOCLOCK */
  76. }
  77. /*===========================================================================*/
  78. /* Driver interrupt handlers. */
  79. /*===========================================================================*/
  80. #if defined(STM32_DMA_REQUIRED) || defined(__DOXYGEN__)
  81. #if defined(STM32_DMA1_CH23_HANDLER) || defined(__DOXYGEN__)
  82. /**
  83. * @brief DMA1 streams 2 and 3 shared ISR.
  84. * @note It is declared here because this device has a non-standard
  85. * DMA shared IRQ handler.
  86. *
  87. * @isr
  88. */
  89. OSAL_IRQ_HANDLER(STM32_DMA1_CH23_HANDLER) {
  90. OSAL_IRQ_PROLOGUE();
  91. /* Check on channel 2.*/
  92. dmaServeInterrupt(STM32_DMA1_STREAM2);
  93. /* Check on channel 3.*/
  94. dmaServeInterrupt(STM32_DMA1_STREAM3);
  95. OSAL_IRQ_EPILOGUE();
  96. }
  97. #endif /* defined(STM32_DMA1_CH23_HANDLER) */
  98. #if defined(STM32_DMA1_CH4567_HANDLER) || defined(__DOXYGEN__)
  99. /**
  100. * @brief DMA1 streams 4, 5, 6 and 7 shared ISR.
  101. *
  102. * @isr
  103. */
  104. OSAL_IRQ_HANDLER(STM32_DMA1_CH4567_HANDLER) {
  105. OSAL_IRQ_PROLOGUE();
  106. /* Check on channel 4.*/
  107. dmaServeInterrupt(STM32_DMA1_STREAM4);
  108. /* Check on channel 5.*/
  109. dmaServeInterrupt(STM32_DMA1_STREAM5);
  110. #if STM32_DMA1_NUM_CHANNELS > 5
  111. /* Check on channel 6.*/
  112. dmaServeInterrupt(STM32_DMA1_STREAM6);
  113. #endif
  114. #if STM32_DMA1_NUM_CHANNELS > 6
  115. /* Check on channel 7.*/
  116. dmaServeInterrupt(STM32_DMA1_STREAM7);
  117. #endif
  118. OSAL_IRQ_EPILOGUE();
  119. }
  120. #endif /* defined(STM32_DMA1_CH4567_HANDLER) */
  121. #if defined(STM32_DMA12_CH23_CH12_HANDLER) || defined(__DOXYGEN__)
  122. /**
  123. * @brief DMA1 streams 2 and 3, DMA2 streams 1 and 1 shared ISR.
  124. * @note It is declared here because this device has a non-standard
  125. * DMA shared IRQ handler.
  126. *
  127. * @isr
  128. */
  129. OSAL_IRQ_HANDLER(STM32_DMA12_CH23_CH12_HANDLER) {
  130. OSAL_IRQ_PROLOGUE();
  131. /* Check on channel 2 of DMA1.*/
  132. dmaServeInterrupt(STM32_DMA1_STREAM2);
  133. /* Check on channel 3 of DMA1.*/
  134. dmaServeInterrupt(STM32_DMA1_STREAM3);
  135. /* Check on channel 1 of DMA2.*/
  136. dmaServeInterrupt(STM32_DMA2_STREAM1);
  137. /* Check on channel 2 of DMA2.*/
  138. dmaServeInterrupt(STM32_DMA2_STREAM2);
  139. OSAL_IRQ_EPILOGUE();
  140. }
  141. #endif /* defined(STM32_DMA12_CH23_CH12_HANDLER) */
  142. #if defined(STM32_DMA12_CH4567_CH345_HANDLER) || defined(__DOXYGEN__)
  143. /**
  144. * @brief DMA1 streams 4, 5, 6 and 7, DMA2 streams 3, 4 and 5 shared ISR.
  145. * @note It is declared here because this device has a non-standard
  146. * DMA shared IRQ handler.
  147. *
  148. * @isr
  149. */
  150. OSAL_IRQ_HANDLER(STM32_DMA12_CH4567_CH345_HANDLER) {
  151. OSAL_IRQ_PROLOGUE();
  152. /* Check on channel 4 of DMA1.*/
  153. dmaServeInterrupt(STM32_DMA1_STREAM4);
  154. /* Check on channel 5 of DMA1.*/
  155. dmaServeInterrupt(STM32_DMA1_STREAM5);
  156. /* Check on channel 6 of DMA1.*/
  157. dmaServeInterrupt(STM32_DMA1_STREAM6);
  158. /* Check on channel 7 of DMA1.*/
  159. dmaServeInterrupt(STM32_DMA1_STREAM7);
  160. /* Check on channel 3 of DMA2.*/
  161. dmaServeInterrupt(STM32_DMA2_STREAM3);
  162. /* Check on channel 4 of DMA2.*/
  163. dmaServeInterrupt(STM32_DMA2_STREAM4);
  164. /* Check on channel 5 of DMA2.*/
  165. dmaServeInterrupt(STM32_DMA2_STREAM5);
  166. OSAL_IRQ_EPILOGUE();
  167. }
  168. #endif /* defined(STM32_DMA12_CH4567_CH345_HANDLER) */
  169. #endif /* defined(STM32_DMA_REQUIRED) */
  170. /*===========================================================================*/
  171. /* Driver exported functions. */
  172. /*===========================================================================*/
  173. /**
  174. * @brief Low level HAL driver initialization.
  175. *
  176. * @notapi
  177. */
  178. void hal_lld_init(void) {
  179. /* Reset of all peripherals.
  180. Note, GPIOs are not reset because initialized before this point in
  181. board files.*/
  182. rccResetAHB(~STM32_GPIO_EN_MASK);
  183. rccResetAPB1(0xFFFFFFFF);
  184. rccResetAPB2(~RCC_APB2RSTR_DBGMCURST);
  185. /* PWR clock enabled.*/
  186. rccEnablePWRInterface(true);
  187. /* Initializes the backup domain.*/
  188. hal_lld_backup_domain_init();
  189. /* DMA subsystems initialization.*/
  190. #if defined(STM32_DMA_REQUIRED)
  191. dmaInit();
  192. #endif
  193. /* IRQ subsystem initialization.*/
  194. irqInit();
  195. /* Programmable voltage detector enable.*/
  196. #if STM32_PVD_ENABLE
  197. PWR->CR |= PWR_CR_PVDE | (STM32_PLS & STM32_PLS_MASK);
  198. #endif /* STM32_PVD_ENABLE */
  199. }
  200. /**
  201. * @brief STM32 clocks and PLL initialization.
  202. * @note All the involved constants come from the file @p board.h.
  203. * @note This function should be invoked just after the system reset.
  204. *
  205. * @special
  206. */
  207. void stm32_clock_init(void) {
  208. #if !STM32_NO_INIT
  209. /* HSI setup, it enforces the reset situation in order to handle possible
  210. problems with JTAG probes and re-initializations.*/
  211. RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
  212. while (!(RCC->CR & RCC_CR_HSIRDY))
  213. ; /* Wait until HSI is stable. */
  214. /* HSI is selected as new source without touching the other fields in
  215. CFGR. Clearing the register has to be postponed after HSI is the
  216. new source.*/
  217. RCC->CFGR &= ~RCC_CFGR_SW; /* Reset SW, selecting HSI. */
  218. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
  219. ; /* Wait until HSI is selected. */
  220. /* Registers finally cleared to reset values.*/
  221. RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
  222. RCC->CFGR = 0; /* CFGR reset value. */
  223. #if STM32_HSE_ENABLED
  224. /* HSE activation.*/
  225. #if defined(STM32_HSE_BYPASS)
  226. /* HSE Bypass.*/
  227. RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
  228. #else
  229. /* No HSE Bypass.*/
  230. RCC->CR |= RCC_CR_HSEON;
  231. #endif
  232. while (!(RCC->CR & RCC_CR_HSERDY))
  233. ; /* Waits until HSE is stable. */
  234. #endif
  235. #if STM32_HSI14_ENABLED
  236. /* HSI14 activation.*/
  237. RCC->CR2 |= RCC_CR2_HSI14ON;
  238. while (!(RCC->CR2 & RCC_CR2_HSI14RDY))
  239. ; /* Waits until HSI14 is stable. */
  240. #endif
  241. #if STM32_HSI48_ENABLED
  242. /* HSI48 activation.*/
  243. RCC->CR2 |= RCC_CR2_HSI48ON;
  244. while (!(RCC->CR2 & RCC_CR2_HSI48RDY))
  245. ; /* Waits until HSI48 is stable. */
  246. #endif
  247. #if STM32_LSI_ENABLED
  248. /* LSI activation.*/
  249. RCC->CSR |= RCC_CSR_LSION;
  250. while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
  251. ; /* Waits until LSI is stable. */
  252. #endif
  253. /* Clock settings.*/
  254. /* CFGR2 must be configured first since CFGR value could change CFGR2 */
  255. RCC->CFGR2 = STM32_PREDIV;
  256. RCC->CFGR = STM32_PLLNODIV | STM32_MCOPRE | STM32_MCOSEL | STM32_PLLMUL |
  257. STM32_PLLSRC | STM32_PPRE | STM32_HPRE |
  258. ((STM32_PREDIV & STM32_PLLXTPRE_MASK) << STM32_PLLXTPRE_OFFSET);
  259. #if STM32_CECSW == STM32_CECSW_OFF
  260. RCC->CFGR3 = STM32_USBSW | STM32_I2C1SW | STM32_USART1SW;
  261. #else
  262. RCC->CFGR3 = STM32_USBSW | STM32_CECSW | STM32_I2C1SW | STM32_USART1SW;
  263. #endif
  264. #if STM32_ACTIVATE_PLL
  265. /* PLL activation.*/
  266. RCC->CR |= RCC_CR_PLLON;
  267. while (!(RCC->CR & RCC_CR_PLLRDY))
  268. ; /* Waits until PLL is stable. */
  269. #endif
  270. /* Flash setup and final clock selection. */
  271. FLASH->ACR = STM32_FLASHBITS;
  272. /* Switching to the configured clock source if it is different from HSI.*/
  273. #if (STM32_SW != STM32_SW_HSI)
  274. /* Switches clock source.*/
  275. RCC->CFGR |= STM32_SW;
  276. while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2))
  277. ; /* Waits selection complete. */
  278. #endif
  279. /* SYSCFG clock enabled here because it is a multi-functional unit shared
  280. among multiple drivers.*/
  281. rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true);
  282. #endif /* !STM32_NO_INIT */
  283. }
  284. /** @} */