board.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "ald_dma.h"
  30. /**
  31. * @addtogroup es32f3
  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_HCLK_2,ES_CMU_HCLK_2_DIV);
  90. ald_cmu_div_config(CMU_PCLK_1,ES_CMU_PCLK_1_DIV);
  91. ald_cmu_div_config(CMU_PCLK_2,ES_CMU_PCLK_2_DIV);
  92. ald_cmu_perh_clock_config(CMU_PERH_ALL, ENABLE);
  93. /*低功耗时钟使能*/
  94. #ifdef RT_USING_PM
  95. SYSCFG_UNLOCK();
  96. SET_BIT(CMU->LPENR, CMU_LPENR_LRCEN_MSK);
  97. SET_BIT(CMU->LPENR, CMU_LPENR_LOSCEN_MSK);
  98. SET_BIT(CMU->LPENR, CMU_LPENR_HRCEN_MSK);
  99. SET_BIT(CMU->LPENR, CMU_LPENR_HOSCEN_MSK);
  100. SYSCFG_LOCK();
  101. #endif
  102. }
  103. /*******************************************************************************
  104. * Function Name : SysTick_Configuration
  105. * Description : Configures the SysTick for OS tick.
  106. * Input : None
  107. * Output : None
  108. * Return : None
  109. *******************************************************************************/
  110. void SysTick_Configuration(void)
  111. {
  112. /* ticks = sysclk / RT_TICK_PER_SECOND */
  113. SysTick_Config(ald_cmu_get_sys_clock() / RT_TICK_PER_SECOND);
  114. __systick_interval = 1;
  115. }
  116. /**
  117. * This is the timer interrupt service routine.
  118. *
  119. */
  120. void SysTick_Handler(void)
  121. {
  122. /* enter interrupt */
  123. rt_interrupt_enter();
  124. ald_inc_tick();
  125. rt_tick_increase();
  126. /* leave interrupt */
  127. rt_interrupt_leave();
  128. }
  129. /**
  130. * This is the cmu interrupt service.
  131. *
  132. */
  133. void CMU_Handler(void)
  134. {
  135. ald_cmu_irq_handler();
  136. }
  137. /**
  138. * This is the DMA interrupt service.
  139. *
  140. */
  141. void DMA_Handler(void)
  142. {
  143. /* enter interrupt */
  144. rt_interrupt_enter();
  145. ald_dma_irq_handler();
  146. /* leave interrupt */
  147. rt_interrupt_leave();
  148. }
  149. /*@}*/
  150. /**
  151. * This function will initial ES32F3 board.
  152. */
  153. void rt_hw_board_init(void)
  154. {
  155. /* NVIC Configuration */
  156. NVIC_Configuration();
  157. /*System Clock Configuration */
  158. SystemClock_Config();
  159. /* Configure the SysTick */
  160. SysTick_Configuration();
  161. #ifdef RT_USING_HEAP
  162. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  163. #endif
  164. #ifdef RT_USING_COMPONENTS_INIT
  165. rt_components_board_init();
  166. #endif
  167. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  168. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  169. #endif
  170. #ifdef BSP_USING_DMA0
  171. ald_cmu_perh_clock_config(CMU_PERH_DMA, ENABLE);
  172. ald_dma_init(DMA0);
  173. #endif
  174. }
  175. /**
  176. * This function will delay for some us.
  177. *
  178. * @param us the delay time of us
  179. */
  180. void rt_hw_us_delay(rt_uint32_t us)
  181. {
  182. unsigned int start, now, delta, reload, us_tick;
  183. start = SysTick->VAL;
  184. reload = SysTick->LOAD;
  185. us_tick = ald_cmu_get_sys_clock() / 1000000UL;
  186. do
  187. {
  188. now = SysTick->VAL;
  189. delta = start > now ? start - now : reload + start - now;
  190. }
  191. while (delta < us_tick * us);
  192. }