system_at32f413.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. **************************************************************************
  3. * @file system_at32f413.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 AT32F413_system
  28. * @{
  29. */
  30. #include "at32f413.h"
  31. /** @addtogroup AT32F413_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 AT32F413_system_private_variables
  39. * @{
  40. */
  41. unsigned int system_core_clock = HICK_VALUE; /*!< system clock frequency (core clock) */
  42. /**
  43. * @}
  44. */
  45. /** @addtogroup AT32F413_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. system_clock_config();
  80. #ifdef VECT_TAB_SRAM
  81. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* vector table relocation in internal sram. */
  82. #else
  83. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* vector table relocation in internal flash. */
  84. #endif
  85. }
  86. /**
  87. * use ext high speed clock 8M
  88. */
  89. void system_clock_config(void)
  90. {
  91. /* reset crm */
  92. crm_reset();
  93. crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE);
  94. /* wait till hext is ready */
  95. while(crm_hext_stable_wait() == ERROR)
  96. {
  97. }
  98. /* config pll clock resource */
  99. crm_pll_config(CRM_PLL_SOURCE_HEXT_DIV, CRM_PLL_MULT_50, CRM_PLL_OUTPUT_RANGE_GT72MHZ);
  100. /* enable pll */
  101. crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
  102. /* wait till pll is ready */
  103. while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
  104. {
  105. }
  106. /* config ahbclk */
  107. crm_ahb_div_set(CRM_AHB_DIV_1);
  108. /* config apb2clk */
  109. crm_apb2_div_set(CRM_APB2_DIV_2);
  110. /* config apb1clk */
  111. crm_apb1_div_set(CRM_APB1_DIV_2);
  112. /* enable auto step mode */
  113. crm_auto_step_mode_enable(TRUE);
  114. /* select pll as system clock source */
  115. crm_sysclk_switch(CRM_SCLK_PLL);
  116. /* wait till pll is used as system clock source */
  117. while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
  118. {
  119. }
  120. /* disable auto step mode */
  121. crm_auto_step_mode_enable(FALSE);
  122. /* update system_core_clock global variable */
  123. system_core_clock_update();
  124. }
  125. /**
  126. * @brief update system_core_clock variable according to clock register values.
  127. * the system_core_clock variable contains the core clock (hclk), it can
  128. * be used by the user application to setup the systick timer or configure
  129. * other parameters.
  130. * @param none
  131. * @retval none
  132. */
  133. void system_core_clock_update(void)
  134. {
  135. uint32_t pll_mult = 0, pll_mult_h = 0, pll_clock_source = 0, temp = 0, div_value = 0;
  136. crm_sclk_type sclk_source;
  137. 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};
  138. /* get sclk source */
  139. sclk_source = crm_sysclk_switch_status_get();
  140. switch(sclk_source)
  141. {
  142. case CRM_SCLK_HICK:
  143. if(((CRM->misc3_bit.hick_to_sclk) != RESET) && ((CRM->misc1_bit.hickdiv) != RESET))
  144. system_core_clock = HICK_VALUE * 6;
  145. else
  146. system_core_clock = HICK_VALUE;
  147. break;
  148. case CRM_SCLK_HEXT:
  149. system_core_clock = HEXT_VALUE;
  150. break;
  151. case CRM_SCLK_PLL:
  152. pll_clock_source = CRM->cfg_bit.pllrcs;
  153. {
  154. /* get multiplication factor */
  155. pll_mult = CRM->cfg_bit.pllmult_l;
  156. pll_mult_h = CRM->cfg_bit.pllmult_h;
  157. /* process high bits */
  158. if((pll_mult_h != 0U) || (pll_mult == 15U)){
  159. pll_mult += ((16U * pll_mult_h) + 1U);
  160. }
  161. else
  162. {
  163. pll_mult += 2U;
  164. }
  165. if (pll_clock_source == 0x00)
  166. {
  167. /* hick divided by 2 selected as pll clock entry */
  168. system_core_clock = (HICK_VALUE >> 1) * pll_mult;
  169. }
  170. else
  171. {
  172. /* hext selected as pll clock entry */
  173. if (CRM->cfg_bit.pllhextdiv != RESET)
  174. {
  175. /* hext clock divided by 2 */
  176. system_core_clock = (HEXT_VALUE / 2) * pll_mult;
  177. }
  178. else
  179. {
  180. system_core_clock = HEXT_VALUE * pll_mult;
  181. }
  182. }
  183. }
  184. break;
  185. default:
  186. system_core_clock = HICK_VALUE;
  187. break;
  188. }
  189. /* compute sclk, ahbclk frequency */
  190. /* get ahb division */
  191. temp = CRM->cfg_bit.ahbdiv;
  192. div_value = sys_ahb_div_table[temp];
  193. /* ahbclk frequency */
  194. system_core_clock = system_core_clock >> div_value;
  195. }
  196. /**
  197. * @}
  198. */
  199. /**
  200. * @}
  201. */
  202. /**
  203. * @}
  204. */