system_py32f0xx.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. ******************************************************************************
  3. * @file system_py32f0xx.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. */
  10. #include "py32f0xx.h"
  11. #if !defined (HSE_VALUE)
  12. #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */
  13. #endif /* HSE_VALUE */
  14. #if !defined (HSI_VALUE)
  15. #define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz*/
  16. #endif /* HSI_VALUE */
  17. #if !defined (LSI_VALUE)
  18. #define LSI_VALUE 32768U /*!< Value of LSI in Hz*/
  19. #endif /* LSI_VALUE */
  20. #if !defined (LSE_VALUE)
  21. #define LSE_VALUE 32768U /*!< Value of LSE in Hz*/
  22. #endif /* LSE_VALUE */
  23. /************************* Miscellaneous Configuration ************************/
  24. /*!< Uncomment the following line if you need to relocate your vector Table in
  25. Internal SRAM. */
  26. /* #define FORBID_VECT_TAB_MIGRATION */
  27. /* #define VECT_TAB_SRAM */
  28. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  29. This value must be a multiple of 0x100. */
  30. /******************************************************************************/
  31. /*----------------------------------------------------------------------------
  32. Clock Variable definitions
  33. *----------------------------------------------------------------------------*/
  34. /* This variable is updated in three ways:
  35. 1) by calling CMSIS function SystemCoreClockUpdate()
  36. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  37. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  38. Note: If you use this function to configure the system clock; then there
  39. is no need to call the 2 first functions listed above, since SystemCoreClock
  40. variable is updated automatically.
  41. */
  42. uint32_t SystemCoreClock = HSI_VALUE;
  43. const uint32_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  44. const uint32_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  45. const uint32_t HSIFreqTable[8] = {4000000U, 8000000U, 16000000U, 22120000U, 24000000U, 4000000U, 4000000U, 4000000U};
  46. /*----------------------------------------------------------------------------
  47. Clock functions
  48. *----------------------------------------------------------------------------*/
  49. void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */
  50. {
  51. uint32_t tmp;
  52. #if defined(RCC_HSIDIV_SUPPORT)
  53. uint32_t hsidiv;
  54. #endif
  55. uint32_t hsifs;
  56. /* Get SYSCLK source -------------------------------------------------------*/
  57. switch (RCC->CFGR & RCC_CFGR_SWS)
  58. {
  59. case RCC_CFGR_SWS_0: /* HSE used as system clock */
  60. SystemCoreClock = HSE_VALUE;
  61. break;
  62. case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
  63. SystemCoreClock = LSI_VALUE;
  64. break;
  65. #if defined(RCC_LSE_SUPPORT)
  66. case RCC_CFGR_SWS_2: /* LSE used as system clock */
  67. SystemCoreClock = LSE_VALUE;
  68. break;
  69. #endif
  70. #if defined(RCC_PLL_SUPPORT)
  71. case RCC_CFGR_SWS_1: /* PLL used as system clock */
  72. if ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI) /* HSI used as PLL clock source */
  73. {
  74. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  75. SystemCoreClock = 2 * (HSIFreqTable[hsifs]);
  76. }
  77. else /* HSE used as PLL clock source */
  78. {
  79. SystemCoreClock = 2 * HSE_VALUE;
  80. }
  81. break;
  82. #endif
  83. case 0x00000000U: /* HSI used as system clock */
  84. default: /* HSI used as system clock */
  85. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  86. #if defined(RCC_HSIDIV_SUPPORT)
  87. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  88. SystemCoreClock = (HSIFreqTable[hsifs] / hsidiv);
  89. #else
  90. SystemCoreClock = HSIFreqTable[hsifs];
  91. #endif
  92. break;
  93. }
  94. /* Compute HCLK clock frequency --------------------------------------------*/
  95. /* Get HCLK prescaler */
  96. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  97. /* HCLK clock frequency */
  98. SystemCoreClock >>= tmp;
  99. }
  100. /**
  101. * Initialize the system
  102. *
  103. * @param none
  104. * @return none
  105. *
  106. * @brief Setup the microcontroller system.
  107. * Initialize the System.
  108. */
  109. void SystemInit(void)
  110. {
  111. /* Set the HSI clock to 8MHz by default */
  112. RCC->ICSCR = (RCC->ICSCR & 0xFFFF0000) | (0x1 << 13) | ((*(uint32_t *)(0x1FFF0F04)) & 0x1FFF);
  113. /* Set the LSI clock to 32.768KHz by default */
  114. RCC->ICSCR = (RCC->ICSCR & 0xFE00FFFF) | (((*(uint32_t *)(0x1FFF0FA4)) & 0x1FF) << RCC_ICSCR_LSI_TRIM_Pos);
  115. /* Configure the Vector Table location add offset address ------------------*/
  116. #ifdef VECT_TAB_SRAM
  117. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  118. #else
  119. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  120. #endif
  121. }
  122. #ifndef FORBID_VECT_TAB_MIGRATION
  123. #ifndef VECT_TAB_SRAM
  124. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  125. extern int32_t $Super$$main(void);
  126. uint32_t VECT_SRAM_TAB[48]__attribute__((section(".ARM.__at_0x20000000")));
  127. /* re-define main function */
  128. int $Sub$$main(void)
  129. {
  130. uint8_t i;
  131. uint32_t *pFmcVect = (uint32_t *)(FLASH_BASE | VECT_TAB_OFFSET);
  132. for (i = 0; i < 48; i++)
  133. {
  134. VECT_SRAM_TAB[i] = pFmcVect[i];
  135. }
  136. SCB->VTOR = SRAM_BASE;
  137. $Super$$main();
  138. return 0;
  139. }
  140. #elif defined(__ICCARM__)
  141. extern int32_t main(void);
  142. /* __low_level_init will auto called by IAR cstartup */
  143. extern void __iar_data_init3(void);
  144. uint32_t VECT_SRAM_TAB[48] @SRAM_BASE;
  145. int __low_level_init(void)
  146. {
  147. uint8_t i;
  148. uint32_t *pFmcVect = (uint32_t *)(FLASH_BASE | VECT_TAB_OFFSET);
  149. /* call IAR table copy function. */
  150. __iar_data_init3();
  151. for (i = 0; i < 48; i++)
  152. {
  153. VECT_SRAM_TAB[i] = pFmcVect[i];
  154. }
  155. SCB->VTOR = SRAM_BASE;
  156. main();
  157. return 0;
  158. }
  159. #endif
  160. #endif
  161. #endif