1 /*
2  * Copyright (c) 2017-2019, MindMotion AE Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-10-02     spaceman           first version
9  */
10 #include <rtthread.h>
11 #include <rthw.h>
12 #include <board.h>
13 #include <drv_uart.h>
14 // extern uint32_t SystemCoreClock;
15 // extern void SystemInit(void);
16 
17 #ifdef RT_USING_FINSH
18 #include <finsh.h>
reboot(uint8_t argc,char ** argv)19 static void reboot(uint8_t argc, char **argv)
20 {
21     rt_hw_cpu_reset();
22 }
23 MSH_CMD_EXPORT(reboot, Reboot System);
24 #endif /* RT_USING_FINSH */
25 
bsp_clock_config(void)26 static void bsp_clock_config(void)
27 {
28     RemapVtorTable();
29     SystemClk_HSEInit(RCC_PLLMul_20);//启动PLL时钟,12MHz*20=240MHz
30     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//2:2,全局性函数,仅需设置一次
31 
32     uint32_t sysclk = 0;
33     getSystemClock(&sysclk);
34 
35     SysTick_Config(sysclk / RT_TICK_PER_SECOND);
36     SysTick->CTRL |= 0x00000004UL;
37 }
38 
SysTick_Handler(void)39 void SysTick_Handler(void)
40 {
41     /* enter interrupt */
42     rt_interrupt_enter();
43 
44     rt_tick_increase();
45 
46     /* leave interrupt */
47     rt_interrupt_leave();
48 }
49 
rt_hw_us_delay(rt_uint32_t us)50 void rt_hw_us_delay(rt_uint32_t us)
51 {
52     rt_uint32_t ticks;
53     rt_uint32_t told, tnow, tcnt = 0;
54     rt_uint32_t reload = SysTick->LOAD;
55 
56     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
57     told = SysTick->VAL;
58     while (1)
59     {
60         tnow = SysTick->VAL;
61         if (tnow != told)
62         {
63             if (tnow < told)
64             {
65                 tcnt += told - tnow;
66             }
67             else
68             {
69                 tcnt += reload - tnow + told;
70             }
71             told = tnow;
72             if (tcnt >= ticks)
73             {
74                 break;
75             }
76         }
77     }
78 }
79 
rt_hw_board_init()80 void rt_hw_board_init()
81 {
82     bsp_clock_config();
83 #if defined(RT_USING_HEAP)
84     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
85 #endif
86     // volatile uint16_t i=0;
87     // rt_hw_uart_init();
88     // i = UINT16_MAX;
89     // while(i--);     //wait for a while after uart initiated.
90 #ifdef RT_USING_COMPONENTS_INIT
91     rt_components_board_init();
92 #endif
93 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
94     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
95 #endif
96 }
97