system_stm32f0.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Taken from ST Cube library and modified. See below for original header.
  5. */
  6. /**
  7. ******************************************************************************
  8. * @file system_stm32f0xx.c
  9. * @author MCD Application Team
  10. * @brief CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  16. *
  17. * Redistribution and use in source and binary forms, with or without modification,
  18. * are permitted provided that the following conditions are met:
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************
  40. */
  41. #include STM32_HAL_H
  42. #ifndef HSE_VALUE
  43. #define HSE_VALUE (8000000)
  44. #endif
  45. #ifndef HSI_VALUE
  46. #define HSI_VALUE (8000000)
  47. #endif
  48. #ifndef HSI48_VALUE
  49. #define HSI48_VALUE (48000000)
  50. #endif
  51. /* This variable is updated in three ways:
  52. 1) by calling CMSIS function SystemCoreClockUpdate()
  53. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  54. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  55. Note: If you use this function to configure the system clock there is no need to
  56. call the 2 first functions listed above, since SystemCoreClock variable is
  57. updated automatically.
  58. */
  59. uint32_t SystemCoreClock = 8000000;
  60. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  61. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  62. void SystemInit(void) {
  63. // Set HSION bit
  64. RCC->CR |= (uint32_t)0x00000001U;
  65. #if defined(STM32F051x8) || defined(STM32F058x8)
  66. // Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits
  67. RCC->CFGR &= (uint32_t)0xF8FFB80CU;
  68. #else
  69. // Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits
  70. RCC->CFGR &= (uint32_t)0x08FFB80CU;
  71. #endif
  72. // Reset HSEON, CSSON and PLLON bits
  73. RCC->CR &= (uint32_t)0xFEF6FFFFU;
  74. // Reset HSEBYP bit
  75. RCC->CR &= (uint32_t)0xFFFBFFFFU;
  76. // Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits
  77. RCC->CFGR &= (uint32_t)0xFFC0FFFFU;
  78. // Reset PREDIV[3:0] bits
  79. RCC->CFGR2 &= (uint32_t)0xFFFFFFF0U;
  80. #if defined(STM32F072xB) || defined(STM32F078xx)
  81. // Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits
  82. RCC->CFGR3 &= (uint32_t)0xFFFCFE2CU;
  83. #elif defined(STM32F071xB)
  84. // Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits
  85. RCC->CFGR3 &= (uint32_t)0xFFFFCEACU;
  86. #elif defined(STM32F091xC) || defined(STM32F098xx)
  87. // Reset USART3SW[1:0], USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits
  88. RCC->CFGR3 &= (uint32_t)0xFFF0FEACU;
  89. #elif defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || defined(STM32F030xC)
  90. // Reset USART1SW[1:0], I2C1SW and ADCSW bits
  91. RCC->CFGR3 &= (uint32_t)0xFFFFFEECU;
  92. #elif defined(STM32F051x8) || defined(STM32F058xx)
  93. // Reset USART1SW[1:0], I2C1SW, CECSW and ADCSW bits
  94. RCC->CFGR3 &= (uint32_t)0xFFFFFEACU;
  95. #elif defined(STM32F042x6) || defined(STM32F048xx)
  96. // Reset USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits
  97. RCC->CFGR3 &= (uint32_t)0xFFFFFE2CU;
  98. #elif defined(STM32F070x6) || defined(STM32F070xB)
  99. // Reset USART1SW[1:0], I2C1SW, USBSW and ADCSW bits
  100. RCC->CFGR3 &= (uint32_t)0xFFFFFE6CU;
  101. // Set default USB clock to PLLCLK, since there is no HSI48
  102. RCC->CFGR3 |= (uint32_t)0x00000080U;
  103. #else
  104. #warning "No target selected"
  105. #endif
  106. // Reset HSI14 bit
  107. RCC->CR2 &= (uint32_t)0xFFFFFFFEU;
  108. // Disable all interrupts
  109. RCC->CIR = 0x00000000U;
  110. // dpgeorge: enable 8-byte stack alignment for IRQ handlers, in accord with EABI
  111. SCB->CCR |= SCB_CCR_STKALIGN_Msk;
  112. }
  113. void SystemClock_Config(void) {
  114. // Set flash latency to 1 because SYSCLK > 24MHz
  115. FLASH->ACR = (FLASH->ACR & ~0x7) | 0x1;
  116. // Use the 48MHz internal oscillator
  117. RCC->CR2 |= RCC_CR2_HSI48ON;
  118. while ((RCC->CR2 & RCC_CR2_HSI48RDY) == 0) {
  119. }
  120. RCC->CFGR |= 3 << RCC_CFGR_SW_Pos;
  121. while (((RCC->CFGR >> RCC_CFGR_SWS_Pos) & 0x3) != 0x03) {
  122. // Wait for SYSCLK source to change
  123. }
  124. SystemCoreClockUpdate();
  125. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
  126. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  127. }
  128. void SystemCoreClockUpdate(void) {
  129. // Get SYSCLK source
  130. uint32_t tmp = RCC->CFGR & RCC_CFGR_SWS;
  131. switch (tmp) {
  132. case RCC_CFGR_SWS_HSI:
  133. SystemCoreClock = HSI_VALUE;
  134. break;
  135. case RCC_CFGR_SWS_HSE:
  136. SystemCoreClock = HSE_VALUE;
  137. break;
  138. case RCC_CFGR_SWS_PLL: {
  139. /* Get PLL clock source and multiplication factor */
  140. uint32_t pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
  141. uint32_t pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  142. pllmull = (pllmull >> 18) + 2;
  143. uint32_t predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  144. if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV) {
  145. /* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */
  146. SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull;
  147. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) \
  148. || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx)
  149. } else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV) {
  150. /* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */
  151. SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull;
  152. #endif
  153. } else {
  154. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6) \
  155. || defined(STM32F078xx) || defined(STM32F071xB) || defined(STM32F072xB) \
  156. || defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
  157. /* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */
  158. SystemCoreClock = (HSI_VALUE / predivfactor) * pllmull;
  159. #else
  160. /* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */
  161. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  162. #endif
  163. }
  164. break;
  165. }
  166. case RCC_CFGR_SWS_HSI48:
  167. SystemCoreClock = HSI48_VALUE;
  168. break;
  169. default:
  170. SystemCoreClock = HSI_VALUE;
  171. break;
  172. }
  173. // Compute HCLK clock frequency
  174. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  175. SystemCoreClock >>= tmp;
  176. }
  177. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/