board.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * 2019-01-23 wangyq the first version
  21. * 2019-11-01 wangyq update libraries
  22. * 2021-04-20 liuhy the second version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include "board.h"
  27. #include "drv_uart.h"
  28. #include "drv_gpio.h"
  29. #include <ald_gpio.h>
  30. /**
  31. * @addtogroup es32f0
  32. */
  33. /*@{*/
  34. /*******************************************************************************
  35. * Function Name : NVIC_Configuration
  36. * Description : Configures Vector Table base location.
  37. * Input : None
  38. * Output : None
  39. * Return : None
  40. *******************************************************************************/
  41. void NVIC_Configuration(void)
  42. {
  43. }
  44. /*******************************************************************************
  45. * Function Name : SystemClock_Configuration
  46. * Description : Configures the System Clock.
  47. * Input : None
  48. * Output : None
  49. * Return : None
  50. *******************************************************************************/
  51. void SystemClock_Config(void)
  52. {
  53. SYSCFG_UNLOCK();
  54. #if ES_CMU_LRC_EN
  55. SET_BIT(CMU->CLKENR, CMU_CLKENR_LRCEN_MSK);
  56. #else
  57. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_LRCEN_MSK);
  58. #endif /*ES_CMU_LRC_EN*/
  59. #if ES_CMU_LOSC_EN
  60. SET_BIT(CMU->CLKENR, CMU_CLKENR_LOSCEN_MSK);
  61. #else
  62. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_LOSCEN_MSK);
  63. #endif /*ES_CMU_LOSC_EN*/
  64. #if ES_CMU_HRC_EN
  65. SET_BIT(CMU->CLKENR, CMU_CLKENR_HRCEN_MSK);
  66. #else
  67. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_HRCEN_MSK);
  68. #endif /*ES_CMU_HRC_EN*/
  69. #if ES_CMU_HOSC_EN
  70. SET_BIT(CMU->CLKENR, CMU_CLKENR_HOSCEN_MSK);
  71. #else
  72. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_HOSCEN_MSK);
  73. #endif /*ES_CMU_HOSC_EN*/
  74. SYSCFG_LOCK();
  75. #if ES_CMU_PLL1_EN
  76. /*PLL的源必须是4M*/
  77. ald_cmu_pll1_config(ES_PLL1_REFER_CLK, ES_PLL1_OUT_CLK);
  78. #if ES_CMU_PLL1_SAFE_EN
  79. ald_cmu_pll_safe_config(ENABLE);
  80. #else
  81. ald_cmu_pll_safe_config(DISABLE);
  82. #endif
  83. #else
  84. CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_PLL1EN_MSK);
  85. #endif /*ES_CMU_PLL1_EN*/
  86. ald_cmu_clock_config(ES_SYS_CLK_SOURSE, ES_SYS_CLK);
  87. ald_cmu_div_config(CMU_SYS,ES_CMU_SYS_DIV);
  88. ald_cmu_div_config(CMU_HCLK_1,ES_CMU_HCLK_1_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 ES32F0 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. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  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. }