system_at32f423.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. **************************************************************************
  3. * @file system_at32f423.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 AT32F423_system
  28. * @{
  29. */
  30. #include "at32f423.h"
  31. /** @addtogroup AT32F423_system_private_defines
  32. * @{
  33. */
  34. #define VECT_TAB_OFFSET 0x0 /*!< vector table base offset field. this value must be a multiple of 0x200. */
  35. /**
  36. * @}
  37. */
  38. /** @addtogroup AT32F423_system_private_variables
  39. * @{
  40. */
  41. unsigned int system_core_clock = HICK_VALUE; /*!< system clock frequency (core clock) */
  42. /**
  43. * @}
  44. */
  45. /** @addtogroup AT32F423_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. /* enable auto step mode */
  63. crm_auto_step_mode_enable(TRUE);
  64. /* set hicken bit */
  65. CRM->ctrl_bit.hicken = TRUE;
  66. /* wait hick stable */
  67. while(CRM->ctrl_bit.hickstbl != SET);
  68. /* hick used as system clock */
  69. CRM->cfg_bit.sclksel = CRM_SCLK_HICK;
  70. /* wait sclk switch status */
  71. while(CRM->cfg_bit.sclksts != CRM_SCLK_HICK);
  72. /* reset hexten, hextbyps, cfden and pllen bits */
  73. CRM->ctrl &= ~(0x010D0000U);
  74. /* reset cfg register, include sclk switch, ahbdiv, apb1div, apb2div, adcdiv, clkout bits */
  75. CRM->cfg = 0x40000000U;
  76. /* reset pllms pllns pllfr pllrcs bits */
  77. CRM->pllcfg = 0x00033002U;
  78. /* reset clkout_sel, clkoutdiv, pllclk_to_adc, hick_to_sclk, hick_to_usb, hickdiv */
  79. CRM->misc1 = 0x000F0000U;
  80. /* disable all interrupts enable and clear pending bits */
  81. CRM->clkint = 0x009F0000U;
  82. /* disable auto step mode */
  83. crm_auto_step_mode_enable(FALSE);
  84. #ifdef VECT_TAB_SRAM
  85. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* vector table relocation in internal sram. */
  86. #else
  87. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* vector table relocation in internal flash. */
  88. #endif
  89. }
  90. /**
  91. * @brief update system_core_clock variable according to clock register values.
  92. * the system_core_clock variable contains the core clock (hclk), it can
  93. * be used by the user application to setup the systick timer or configure
  94. * other parameters.
  95. * @param none
  96. * @retval none
  97. */
  98. void system_core_clock_update(void)
  99. {
  100. uint32_t pll_ns = 0, pll_ms = 0, pll_fr = 0, pll_clock_source = 0, pllrcsfreq = 0;
  101. uint32_t temp = 0, div_value = 0;
  102. crm_sclk_type sclk_source;
  103. 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};
  104. static const uint8_t pll_fr_table[6] = {1, 2, 4, 8, 16, 32};
  105. /* get sclk source */
  106. sclk_source = crm_sysclk_switch_status_get();
  107. switch(sclk_source)
  108. {
  109. case CRM_SCLK_HICK:
  110. if(((CRM->misc1_bit.hick_to_sclk) != RESET) && ((CRM->misc1_bit.hickdiv) != RESET))
  111. system_core_clock = HICK_VALUE * 6;
  112. else
  113. system_core_clock = HICK_VALUE;
  114. break;
  115. case CRM_SCLK_HEXT:
  116. system_core_clock = HEXT_VALUE;
  117. break;
  118. case CRM_SCLK_PLL:
  119. /* get pll clock source */
  120. pll_clock_source = CRM->pllcfg_bit.pllrcs;
  121. /* get multiplication factor */
  122. pll_ns = CRM->pllcfg_bit.pllns;
  123. pll_ms = CRM->pllcfg_bit.pllms;
  124. pll_fr = pll_fr_table[CRM->pllcfg_bit.pllfr];
  125. if (pll_clock_source == CRM_PLL_SOURCE_HICK)
  126. {
  127. /* hick selected as pll clock entry */
  128. pllrcsfreq = HICK_VALUE;
  129. }
  130. else
  131. {
  132. /* hext selected as pll clock entry */
  133. pllrcsfreq = HEXT_VALUE;
  134. }
  135. system_core_clock = (uint32_t)(((uint64_t)pllrcsfreq * pll_ns) / (pll_ms * pll_fr) / 2);
  136. break;
  137. default:
  138. system_core_clock = HICK_VALUE;
  139. break;
  140. }
  141. /* compute sclk, ahbclk frequency */
  142. /* get ahb division */
  143. temp = CRM->cfg_bit.ahbdiv;
  144. div_value = sys_ahb_div_table[temp];
  145. /* ahbclk frequency */
  146. system_core_clock = system_core_clock >> div_value;
  147. }
  148. /**
  149. * @}
  150. */
  151. /**
  152. * @}
  153. */
  154. /**
  155. * @}
  156. */