system_py32f002b.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. ******************************************************************************
  3. * @file system_py32f002b.c
  4. * @author MCU Application Team
  5. * @Version V1.0.0
  6. * @Date 2020-10-19
  7. * @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  12. * All rights reserved.</center></h2>
  13. *
  14. * This software component is licensed by Puya under BSD 3-Clause license,
  15. * the "License"; You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. ******************************************************************************
  20. * @attention
  21. *
  22. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  23. * All rights reserved.</center></h2>
  24. *
  25. * This software component is licensed by ST under BSD 3-Clause license,
  26. * the "License"; You may not use this file except in compliance with the
  27. * License. You may obtain a copy of the License at:
  28. * opensource.org/licenses/BSD-3-Clause
  29. *
  30. ******************************************************************************
  31. */
  32. #include "py32f0xx.h"
  33. #if !defined (HSE_VALUE)
  34. #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */
  35. #endif /* HSE_VALUE */
  36. #if !defined (HSI_VALUE)
  37. #define HSI_VALUE 24000000U /*!< Value of the Internal oscillator in Hz*/
  38. #endif /* HSI_VALUE */
  39. #if !defined (LSI_VALUE)
  40. #define LSI_VALUE 32768U /*!< Value of LSI in Hz*/
  41. #endif /* LSI_VALUE */
  42. #if !defined (LSE_VALUE)
  43. #define LSE_VALUE 32768U /*!< Value of LSE in Hz*/
  44. #endif /* LSE_VALUE */
  45. /************************* Miscellaneous Configuration ************************/
  46. /*!< Uncomment the following line if you need to relocate your vector Table in
  47. Internal SRAM. */
  48. /* #define FORBID_VECT_TAB_MIGRATION */
  49. /* #define VECT_TAB_SRAM */
  50. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  51. This value must be a multiple of 0x100. */
  52. /******************************************************************************/
  53. /*----------------------------------------------------------------------------
  54. Clock Variable definitions
  55. *----------------------------------------------------------------------------*/
  56. /* This variable is updated in three ways:
  57. 1) by calling CMSIS function SystemCoreClockUpdate()
  58. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  59. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  60. Note: If you use this function to configure the system clock; then there
  61. is no need to call the 2 first functions listed above, since SystemCoreClock
  62. variable is updated automatically.
  63. */
  64. uint32_t SystemCoreClock = HSI_VALUE;
  65. const uint32_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  66. const uint32_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  67. #if defined(RCC_HSI48M_SUPPORT)
  68. const uint32_t HSIFreqTable[8] = {0U, 0U, 0U, 0U, 24000000U, 48000000U, 0U, 0U};
  69. #else
  70. const uint32_t HSIFreqTable[8] = {0U, 0U, 0U, 0U, 24000000U, 0U, 0U, 0U};
  71. #endif
  72. /* Private function prototypes -----------------------------------------------*/
  73. #ifndef SWD_DELAY
  74. static void DelayTime(uint32_t mdelay);
  75. #endif /* SWD_DELAY */
  76. /**
  77. * @brief Clock functions.
  78. * @param none
  79. * @return none
  80. */
  81. void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */
  82. {
  83. uint32_t tmp;
  84. uint32_t hsidiv;
  85. uint32_t hsifs;
  86. /* Get SYSCLK source -------------------------------------------------------*/
  87. switch (RCC->CFGR & RCC_CFGR_SWS)
  88. {
  89. case RCC_CFGR_SWS_0: /* HSE used as system clock */
  90. SystemCoreClock = HSE_VALUE;
  91. break;
  92. case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
  93. SystemCoreClock = LSI_VALUE;
  94. break;
  95. #if defined(RCC_LSE_SUPPORT)
  96. case RCC_CFGR_SWS_2: /* LSE used as system clock */
  97. SystemCoreClock = LSE_VALUE;
  98. break;
  99. #endif /* RCC_LSE_SUPPORT */
  100. case 0x00000000U: /* HSI used as system clock */
  101. default: /* HSI used as system clock */
  102. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  103. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  104. SystemCoreClock = (HSIFreqTable[hsifs] / hsidiv);
  105. break;
  106. }
  107. /* Compute HCLK clock frequency --------------------------------------------*/
  108. /* Get HCLK prescaler */
  109. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  110. /* HCLK clock frequency */
  111. SystemCoreClock >>= tmp;
  112. }
  113. /**
  114. * @brief Setup the microcontroller system.
  115. * Initialize the System.
  116. * @param none
  117. * @return none
  118. */
  119. void SystemInit(void)
  120. {
  121. /*Set the HSI clock to 24MHz by default*/
  122. RCC->ICSCR = (RCC->ICSCR & 0xFFFF0000) | (*(uint32_t *)(0x1FFF0100));
  123. /*Set the LSI clock to 32.768KHz by default*/
  124. RCC->ICSCR = (RCC->ICSCR & 0xFE00FFFF) | ((*(uint32_t *)(0x1FFF0144)) << RCC_ICSCR_LSI_TRIM_Pos);
  125. /* Configure the Vector Table location add offset address ------------------*/
  126. #ifdef VECT_TAB_SRAM
  127. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  128. #else
  129. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  130. #endif /* VECT_TAB_SRAM */
  131. #ifndef SWD_DELAY
  132. /* When the SWD pin is reused for other functions, this function is used to solve the
  133. problem of not being able to update the code. */
  134. DelayTime(100);
  135. #endif /* SWD_DELAY */
  136. }
  137. #ifndef SWD_DELAY
  138. /**
  139. * @brief This function provides delay (in milliseconds) based on CPU cycles method.
  140. * @param mdelay: specifies the delay time length, in milliseconds.
  141. * @retval None
  142. */
  143. static void DelayTime(uint32_t mdelay)
  144. {
  145. __IO uint32_t Delay = mdelay * (24000000U / 8U / 1000U);
  146. do
  147. {
  148. __NOP();
  149. }
  150. while (Delay --);
  151. }
  152. #endif /* SWD_DELAY */
  153. /************************ (C) COPYRIGHT Puya *****END OF FILE******************/