system_py32f0xx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. ******************************************************************************
  3. * @file system_py32f0xx.c
  4. * @author MCU Application Team
  5. * @brief CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. #include "py32f0xx.h"
  31. #if !defined (HSE_VALUE)
  32. #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */
  33. #endif /* HSE_VALUE */
  34. #if !defined (HSI_VALUE)
  35. #define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz*/
  36. #endif /* HSI_VALUE */
  37. #if !defined (LSI_VALUE)
  38. #define LSI_VALUE 32768U /*!< Value of LSI in Hz*/
  39. #endif /* LSI_VALUE */
  40. #if !defined (LSE_VALUE)
  41. #define LSE_VALUE 32768U /*!< Value of LSE in Hz*/
  42. #endif /* LSE_VALUE */
  43. /************************* Miscellaneous Configuration ************************/
  44. /*!< Uncomment the following line if you need to relocate your vector Table in
  45. Internal SRAM. */
  46. /* #define FORBID_VECT_TAB_MIGRATION */
  47. /* #define VECT_TAB_SRAM */
  48. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  49. This value must be a multiple of 0x100. */
  50. /******************************************************************************/
  51. /*----------------------------------------------------------------------------
  52. Clock Variable definitions
  53. *----------------------------------------------------------------------------*/
  54. /* This variable is updated in three ways:
  55. 1) by calling CMSIS function SystemCoreClockUpdate()
  56. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  57. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  58. Note: If you use this function to configure the system clock; then there
  59. is no need to call the 2 first functions listed above, since SystemCoreClock
  60. variable is updated automatically.
  61. */
  62. uint32_t SystemCoreClock = HSI_VALUE;
  63. const uint32_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  64. const uint32_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  65. const uint32_t HSIFreqTable[8] = {4000000U, 8000000U, 16000000U, 22120000U, 24000000U, 4000000U, 4000000U, 4000000U};
  66. /**
  67. * @brief Clock functions.
  68. * @param none
  69. * @return none
  70. */
  71. void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */
  72. {
  73. uint32_t tmp;
  74. #if defined(RCC_HSIDIV_SUPPORT)
  75. uint32_t hsidiv;
  76. #endif
  77. uint32_t hsifs;
  78. /* Get SYSCLK source -------------------------------------------------------*/
  79. switch (RCC->CFGR & RCC_CFGR_SWS)
  80. {
  81. case RCC_CFGR_SWS_0: /* HSE used as system clock */
  82. SystemCoreClock = HSE_VALUE;
  83. break;
  84. case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
  85. SystemCoreClock = LSI_VALUE;
  86. break;
  87. #if defined(RCC_LSE_SUPPORT)
  88. case RCC_CFGR_SWS_2: /* LSE used as system clock */
  89. SystemCoreClock = LSE_VALUE;
  90. break;
  91. #endif /* RCC_LSE_SUPPORT */
  92. #if defined(RCC_PLL_SUPPORT)
  93. case RCC_CFGR_SWS_1: /* PLL used as system clock */
  94. if ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI) /* HSI used as PLL clock source */
  95. {
  96. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  97. SystemCoreClock = 2 * (HSIFreqTable[hsifs]);
  98. }
  99. else /* HSE used as PLL clock source */
  100. {
  101. SystemCoreClock = 2 * HSE_VALUE;
  102. }
  103. break;
  104. #endif /* RCC_PLL_SUPPORT */
  105. case 0x00000000U: /* HSI used as system clock */
  106. default: /* HSI used as system clock */
  107. hsifs = ((READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS)) >> RCC_ICSCR_HSI_FS_Pos);
  108. #if defined(RCC_HSIDIV_SUPPORT)
  109. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV)) >> RCC_CR_HSIDIV_Pos));
  110. SystemCoreClock = (HSIFreqTable[hsifs] / hsidiv);
  111. #else
  112. SystemCoreClock = HSIFreqTable[hsifs];
  113. #endif
  114. break;
  115. }
  116. /* Compute HCLK clock frequency --------------------------------------------*/
  117. /* Get HCLK prescaler */
  118. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  119. /* HCLK clock frequency */
  120. SystemCoreClock >>= tmp;
  121. }
  122. /**
  123. * @brief Setup the microcontroller system.
  124. * Initialize the System.
  125. * @param none
  126. * @return none
  127. */
  128. void SystemInit(void)
  129. {
  130. /* Set the HSI clock to 8MHz by default */
  131. RCC->ICSCR = (RCC->ICSCR & 0xFFFF0000) | (0x1 << 13) | ((*(uint32_t *)(0x1FFF0F04)) & 0x1FFF);
  132. /* Set the LSI clock to 32.768KHz by default */
  133. RCC->ICSCR = (RCC->ICSCR & 0xFE00FFFF) | (((*(uint32_t *)(0x1FFF0FA4)) & 0x1FF) << RCC_ICSCR_LSI_TRIM_Pos);
  134. /* Configure the Vector Table location add offset address ------------------*/
  135. #ifdef VECT_TAB_SRAM
  136. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  137. #else
  138. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  139. #endif /* VECT_TAB_SRAM */
  140. }
  141. #ifndef FORBID_VECT_TAB_MIGRATION
  142. #ifndef VECT_TAB_SRAM
  143. #if (defined (__CC_ARM)) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  144. extern int32_t $Super$$main(void);
  145. uint32_t VECT_SRAM_TAB[48]__attribute__((section(".ARM.__at_0x20000000")));
  146. /**
  147. * @brief re-define main function.
  148. * @param none
  149. * @return int
  150. */
  151. int $Sub$$main(void)
  152. {
  153. uint8_t i;
  154. uint32_t *pFmcVect = (uint32_t *)(FLASH_BASE | VECT_TAB_OFFSET);
  155. for (i = 0; i < 48; i++)
  156. {
  157. VECT_SRAM_TAB[i] = pFmcVect[i];
  158. }
  159. SCB->VTOR = SRAM_BASE;
  160. $Super$$main();
  161. return 0;
  162. }
  163. #elif defined(__ICCARM__)
  164. extern int32_t main(void);
  165. /* __low_level_init will auto called by IAR cstartup */
  166. extern void __iar_data_init3(void);
  167. uint32_t VECT_SRAM_TAB[48] @SRAM_BASE;
  168. int __low_level_init(void)
  169. {
  170. uint8_t i;
  171. uint32_t *pFmcVect = (uint32_t *)(FLASH_BASE | VECT_TAB_OFFSET);
  172. /* call IAR table copy function. */
  173. __iar_data_init3();
  174. for (i = 0; i < 48; i++)
  175. {
  176. VECT_SRAM_TAB[i] = pFmcVect[i];
  177. }
  178. SCB->VTOR = SRAM_BASE;
  179. main();
  180. return 0;
  181. }
  182. #elif defined(__GNUC__)
  183. extern int32_t main(void);
  184. extern int entry(void);
  185. uint32_t VECT_SRAM_TAB[48]__attribute__((section(".vectable_sram")));
  186. int entry(void)
  187. {
  188. uint8_t i;
  189. uint32_t *pFmcVect = (uint32_t *)(FLASH_BASE | VECT_TAB_OFFSET);
  190. for (i = 0; i < 48; i++)
  191. {
  192. VECT_SRAM_TAB[i] = pFmcVect[i];
  193. }
  194. SCB->VTOR = SRAM_BASE;
  195. main();
  196. return 0;
  197. }
  198. #endif /* __ICCARM__ */
  199. #endif /* VECT_TAB_SRAM */
  200. #endif /* FORBID_VECT_TAB_MIGRATION */
  201. /************************ (C) COPYRIGHT Puya *****END OF FILE******************/