system_py32f0xx.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. uint32_t hsidiv;
  53. uint32_t hsifs;
  54. /* Get SYSCLK source -------------------------------------------------------*/
  55. switch (RCC->CFGR & RCC_CFGR_SWS)
  56. {
  57. case RCC_CFGR_SWS_0: /* HSE used as system clock */
  58. SystemCoreClock = HSE_VALUE;
  59. break;
  60. case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
  61. SystemCoreClock = LSI_VALUE;
  62. break;
  63. #if defined(RCC_LSE_SUPPORT)
  64. case RCC_CFGR_SWS_2: /* LSE used as system clock */
  65. SystemCoreClock = LSE_VALUE;
  66. break;
  67. #endif
  68. #if defined(RCC_PLL_SUPPORT)
  69. case RCC_CFGR_SWS_1: /* PLL used as system clock */
  70. if ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI) /* HSI used as PLL clock source */
  71. {
  72. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  73. SystemCoreClock = 2 * (HSIFreqTable[hsifs]);
  74. }
  75. else /* HSE used as PLL clock source */
  76. {
  77. SystemCoreClock = 2 * HSE_VALUE;
  78. }
  79. break;
  80. #endif
  81. case 0x00000000U: /* HSI used as system clock */
  82. default: /* HSI used as system clock */
  83. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  84. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  85. SystemCoreClock = (HSIFreqTable[hsifs] / hsidiv);
  86. break;
  87. }
  88. /* Compute HCLK clock frequency --------------------------------------------*/
  89. /* Get HCLK prescaler */
  90. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  91. /* HCLK clock frequency */
  92. SystemCoreClock >>= tmp;
  93. }
  94. /**
  95. * Initialize the system
  96. *
  97. * @param none
  98. * @return none
  99. *
  100. * @brief Setup the microcontroller system.
  101. * Initialize the System.
  102. */
  103. void SystemInit(void)
  104. {
  105. /* Set the HSI clock to 8MHz by default */
  106. RCC->ICSCR = (RCC->ICSCR & 0xFFFF0000) | (0x1 << 13) | *(uint32_t *)(0x1fff0f04);
  107. /* Configure the Vector Table location add offset address ------------------*/
  108. #ifdef VECT_TAB_SRAM
  109. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  110. #else
  111. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  112. #endif
  113. }
  114. #ifndef FORBID_VECT_TAB_MIGRATION
  115. #ifndef VECT_TAB_SRAM
  116. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  117. extern int32_t $Super$$main(void);
  118. uint32_t VECT_SRAM_TAB[48]__attribute__((section(".ARM.__at_0x20000000")));
  119. /* re-define main function */
  120. int $Sub$$main(void)
  121. {
  122. uint8_t i;
  123. uint32_t *pFmcVect = (uint32_t *)(FLASH_BASE | VECT_TAB_OFFSET);
  124. for (i = 0; i < 48; i++)
  125. {
  126. VECT_SRAM_TAB[i] = pFmcVect[i];
  127. }
  128. SCB->VTOR = SRAM_BASE;
  129. $Super$$main();
  130. return 0;
  131. }
  132. #elif defined(__ICCARM__)
  133. extern int32_t main(void);
  134. /* __low_level_init will auto called by IAR cstartup */
  135. extern void __iar_data_init3(void);
  136. uint32_t VECT_SRAM_TAB[48] @SRAM_BASE;
  137. int __low_level_init(void)
  138. {
  139. uint8_t i;
  140. uint32_t *pFmcVect = (uint32_t *)(FLASH_BASE | VECT_TAB_OFFSET);
  141. /* call IAR table copy function. */
  142. __iar_data_init3();
  143. for (i = 0; i < 48; i++)
  144. {
  145. VECT_SRAM_TAB[i] = pFmcVect[i];
  146. }
  147. SCB->VTOR = SRAM_BASE;
  148. main();
  149. return 0;
  150. }
  151. #endif
  152. #endif
  153. #endif