system_at32f413.c 6.8 KB

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