system_at32f403a_407.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. **************************************************************************
  3. * @file system_at32f403a_407.c
  4. * @brief contains all the functions for cmsis cortex-m4 system source file
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. /** @addtogroup CMSIS
  25. * @{
  26. */
  27. /** @addtogroup AT32F403A_407_system
  28. * @{
  29. */
  30. #include "at32f403a_407.h"
  31. /** @addtogroup AT32F403A_407_system_private_defines
  32. * @{
  33. */
  34. #define VECT_TAB_OFFSET 0x21000 /*!< vector table base offset field. this value must be a multiple of 0x200. */
  35. /**
  36. * @}
  37. */
  38. /** @addtogroup AT32F403A_407_system_private_variables
  39. * @{
  40. */
  41. unsigned int system_core_clock = HICK_VALUE; /*!< system clock frequency (core clock) */
  42. /**
  43. * @}
  44. */
  45. /** @addtogroup AT32F403A_407_system_private_functions
  46. * @{
  47. */
  48. /**
  49. * @brief setup the microcontroller system
  50. * initialize the flash interface.
  51. * @note this function should be used only after reset.
  52. * @param none
  53. * @retval none
  54. */
  55. void SystemInit (void)
  56. {
  57. #if defined (__FPU_USED) && (__FPU_USED == 1U)
  58. SCB->CPACR |= ((3U << 10U * 2U) | /* set cp10 full access */
  59. (3U << 11U * 2U) ); /* set cp11 full access */
  60. #endif
  61. /* reset the crm clock configuration to the default reset state(for debug purpose) */
  62. /* set hicken bit */
  63. CRM->ctrl_bit.hicken = TRUE;
  64. /* wait hick stable */
  65. while(CRM->ctrl_bit.hickstbl != SET);
  66. /* hick used as system clock */
  67. CRM->cfg_bit.sclksel = CRM_SCLK_HICK;
  68. /* wait sclk switch status */
  69. while(CRM->cfg_bit.sclksts != CRM_SCLK_HICK);
  70. /* reset hexten, hextbyps, cfden and pllen bits */
  71. CRM->ctrl &= ~(0x010D0000U);
  72. /* reset cfg register, include sclk switch, ahbdiv, apb1div, apb2div, adcdiv,
  73. clkout pllrcs, pllhextdiv, pllmult, usbdiv and pllrange bits */
  74. CRM->cfg = 0;
  75. /* reset clkout[3], usbbufs, hickdiv, clkoutdiv */
  76. CRM->misc1 = 0;
  77. /* disable all interrupts enable and clear pending bits */
  78. CRM->clkint = 0x009F0000;
  79. #ifdef VECT_TAB_SRAM
  80. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* vector table relocation in internal sram. */
  81. #else
  82. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* vector table relocation in internal flash. */
  83. #endif
  84. }
  85. /**
  86. * @brief update system_core_clock variable according to clock register values.
  87. * the system_core_clock variable contains the core clock (hclk), it can
  88. * be used by the user application to setup the systick timer or configure
  89. * other parameters.
  90. * @param none
  91. * @retval none
  92. */
  93. void system_core_clock_update(void)
  94. {
  95. uint32_t hext_prediv = 0, pll_mult = 0, pll_mult_h = 0, pll_clock_source = 0, temp = 0, div_value = 0;
  96. crm_sclk_type sclk_source;
  97. static const uint8_t sys_ahb_div_table[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  98. /* get sclk source */
  99. sclk_source = crm_sysclk_switch_status_get();
  100. switch(sclk_source)
  101. {
  102. case CRM_SCLK_HICK:
  103. if(((CRM->misc3_bit.hick_to_sclk) != RESET) && ((CRM->misc1_bit.hickdiv) != RESET))
  104. system_core_clock = HICK_VALUE * 6;
  105. else
  106. system_core_clock = HICK_VALUE;
  107. break;
  108. case CRM_SCLK_HEXT:
  109. system_core_clock = HEXT_VALUE;
  110. break;
  111. case CRM_SCLK_PLL:
  112. pll_clock_source = CRM->cfg_bit.pllrcs;
  113. {
  114. /* get multiplication factor */
  115. pll_mult = CRM->cfg_bit.pllmult_l;
  116. pll_mult_h = CRM->cfg_bit.pllmult_h;
  117. /* process high bits */
  118. if((pll_mult_h != 0U) || (pll_mult == 15U)){
  119. pll_mult += ((16U * pll_mult_h) + 1U);
  120. }
  121. else
  122. {
  123. pll_mult += 2U;
  124. }
  125. if (pll_clock_source == 0x00)
  126. {
  127. /* hick divided by 2 selected as pll clock entry */
  128. system_core_clock = (HICK_VALUE >> 1) * pll_mult;
  129. }
  130. else
  131. {
  132. /* hext selected as pll clock entry */
  133. if (CRM->cfg_bit.pllhextdiv != RESET)
  134. {
  135. hext_prediv = CRM->misc3_bit.hextdiv;
  136. /* hext clock divided by 2 */
  137. system_core_clock = (HEXT_VALUE / (hext_prediv + 2)) * pll_mult;
  138. }
  139. else
  140. {
  141. system_core_clock = HEXT_VALUE * pll_mult;
  142. }
  143. }
  144. }
  145. break;
  146. default:
  147. system_core_clock = HICK_VALUE;
  148. break;
  149. }
  150. /* compute sclk, ahbclk frequency */
  151. /* get ahb division */
  152. temp = CRM->cfg_bit.ahbdiv;
  153. div_value = sys_ahb_div_table[temp];
  154. /* ahbclk frequency */
  155. system_core_clock = system_core_clock >> div_value;
  156. }
  157. /**
  158. * @}
  159. */
  160. /**
  161. * @}
  162. */
  163. /**
  164. * @}
  165. */