board.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2020-01-14 wangyq the first version
  21. * 2021-04-20 liuhy the second version
  22. */
  23. #include <rthw.h>
  24. #include <rtthread.h>
  25. #include "board.h"
  26. #include "drv_uart.h"
  27. #include "drv_gpio.h"
  28. #include <ald_gpio.h>
  29. /**
  30. * @addtogroup es32f3
  31. */
  32. /*@{*/
  33. /*******************************************************************************
  34. * Function Name : NVIC_Configuration
  35. * Description : Configures Vector Table base location.
  36. * Input : None
  37. * Output : None
  38. * Return : None
  39. *******************************************************************************/
  40. void NVIC_Configuration(void)
  41. {
  42. }
  43. /*******************************************************************************
  44. * Function Name : SystemClock_Configuration
  45. * Description : Configures the System Clock.
  46. * Input : None
  47. * Output : None
  48. * Return : None
  49. *******************************************************************************/
  50. void SystemClock_Config(void)
  51. {
  52. SYSCFG_UNLOCK();
  53. #if ES_CMU_LRC_EN
  54. SET_BIT(CMU->CLKENR, CMU_CLKENR_LRCEN_MSK);
  55. #else
  56. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_LRCEN_MSK);
  57. #endif /*ES_CMU_LRC_EN*/
  58. #if ES_CMU_LOSC_EN
  59. SET_BIT(CMU->CLKENR, CMU_CLKENR_LOSCEN_MSK);
  60. #else
  61. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_LOSCEN_MSK);
  62. #endif /*ES_CMU_LOSC_EN*/
  63. #if ES_CMU_HRC_EN
  64. SET_BIT(CMU->CLKENR, CMU_CLKENR_HRCEN_MSK);
  65. #else
  66. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_HRCEN_MSK);
  67. #endif /*ES_CMU_HRC_EN*/
  68. #if ES_CMU_HOSC_EN
  69. SET_BIT(CMU->CLKENR, CMU_CLKENR_HOSCEN_MSK);
  70. #else
  71. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_HOSCEN_MSK);
  72. #endif /*ES_CMU_HOSC_EN*/
  73. SYSCFG_LOCK();
  74. #if ES_CMU_PLL1_EN
  75. /*PLL的源必须是4M*/
  76. ald_cmu_pll1_config(ES_PLL1_REFER_CLK, ES_PLL1_OUT_CLK);
  77. #if ES_CMU_PLL1_SAFE_EN
  78. ald_cmu_pll_safe_config(ENABLE);
  79. #else
  80. ald_cmu_pll_safe_config(DISABLE);
  81. #endif
  82. #else
  83. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_PLL1EN_MSK);
  84. #endif /*ES_CMU_PLL1_EN*/
  85. ald_cmu_clock_config(ES_SYS_CLK_SOURSE, ES_SYS_CLK);
  86. ald_cmu_div_config(CMU_SYS,ES_CMU_SYS_DIV);
  87. ald_cmu_div_config(CMU_HCLK_1,ES_CMU_HCLK_1_DIV);
  88. ald_cmu_div_config(CMU_HCLK_2,ES_CMU_HCLK_2_DIV);
  89. ald_cmu_div_config(CMU_PCLK_1,ES_CMU_PCLK_1_DIV);
  90. ald_cmu_div_config(CMU_PCLK_2,ES_CMU_PCLK_2_DIV);
  91. ald_cmu_perh_clock_config(CMU_PERH_ALL, ENABLE);
  92. /*低功耗时钟使能*/
  93. #ifdef RT_USING_PM
  94. SYSCFG_UNLOCK();
  95. SET_BIT(CMU->LPENR, CMU_LPENR_LRCEN_MSK);
  96. SET_BIT(CMU->LPENR, CMU_LPENR_LOSCEN_MSK);
  97. SET_BIT(CMU->LPENR, CMU_LPENR_HRCEN_MSK);
  98. SET_BIT(CMU->LPENR, CMU_LPENR_HOSCEN_MSK);
  99. SYSCFG_LOCK();
  100. #endif
  101. }
  102. /*******************************************************************************
  103. * Function Name : SysTick_Configuration
  104. * Description : Configures the SysTick for OS tick.
  105. * Input : None
  106. * Output : None
  107. * Return : None
  108. *******************************************************************************/
  109. void SysTick_Configuration(void)
  110. {
  111. /* ticks = sysclk / RT_TICK_PER_SECOND */
  112. SysTick_Config(ald_cmu_get_sys_clock() / RT_TICK_PER_SECOND);
  113. __systick_interval = 1;
  114. }
  115. /**
  116. * This is the timer interrupt service routine.
  117. *
  118. */
  119. void SysTick_Handler(void)
  120. {
  121. /* enter interrupt */
  122. rt_interrupt_enter();
  123. ald_inc_tick();
  124. rt_tick_increase();
  125. /* leave interrupt */
  126. rt_interrupt_leave();
  127. }
  128. /**
  129. * This is the cmu interrupt service.
  130. *
  131. */
  132. void CMU_Handler(void)
  133. {
  134. ald_cmu_irq_handler();
  135. }
  136. /*@}*/
  137. /**
  138. * This function will initial ES32F3 board.
  139. */
  140. void rt_hw_board_init(void)
  141. {
  142. /* NVIC Configuration */
  143. NVIC_Configuration();
  144. /*System Clock Configuration */
  145. SystemClock_Config();
  146. /* Configure the SysTick */
  147. SysTick_Configuration();
  148. #ifdef RT_USING_HEAP
  149. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  150. #endif
  151. #ifdef RT_USING_COMPONENTS_INIT
  152. rt_components_board_init();
  153. #endif
  154. #ifdef RT_USING_CONSOLE
  155. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  156. #endif
  157. }
  158. /**
  159. * This function will delay for some us.
  160. *
  161. * @param us the delay time of us
  162. */
  163. void rt_hw_us_delay(rt_uint32_t us)
  164. {
  165. unsigned int start, now, delta, reload, us_tick;
  166. start = SysTick->VAL;
  167. reload = SysTick->LOAD;
  168. us_tick = ald_cmu_get_sys_clock() / 1000000UL;
  169. do
  170. {
  171. now = SysTick->VAL;
  172. delta = start > now ? start - now : reload + start - now;
  173. }
  174. while (delta < us_tick * us);
  175. }