1 /*
2  * Copyright (c) 2006-2020, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-02-11     supperthomas first version
9  *
10  */
11 
12 #include <rtthread.h>
13 #include <rthw.h>
14 #include <stdio.h>
15 #include "board.h"
16 #include "mxc_sys.h"
17 #ifdef RT_USING_SERIAL
18 #include "drv_usart.h"
19 #endif
20 
21 /**
22  * This is the timer interrupt service routine.
23  *
24  */
SysTick_Handler(void)25 void SysTick_Handler(void)
26 {
27     /* enter interrupt */
28     rt_interrupt_enter();
29 
30     rt_tick_increase();
31 
32     /* leave interrupt */
33     rt_interrupt_leave();
34 }
35 
rt_hw_systick_init(void)36 void rt_hw_systick_init(void)
37 {
38     uint32_t error;
39     error = SYS_SysTick_Config(SYS_SysTick_GetFreq() / RT_TICK_PER_SECOND, 1, MXC_TMR0);
40 
41     if (error != E_NO_ERROR)
42     {
43         rt_kprintf("ERROR: Ticks is not valid");
44     }
45 }
46 
rt_hw_board_init(void)47 void rt_hw_board_init(void)
48 {
49 
50     rt_hw_systick_init();
51 
52 #if defined(RT_USING_HEAP)
53     rt_system_heap_init((void *)(HEAP_BEGIN), (void *)(HEAP_END));
54 #endif
55 
56 #ifdef RT_USING_SERIAL
57     rt_hw_usart_init();
58 #endif
59 
60 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
61     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
62 #endif
63 
64 #ifdef RT_USING_COMPONENTS_INIT
65     rt_components_board_init();
66 #endif
67 }
68 
rt_hw_us_delay(rt_uint32_t us)69 void rt_hw_us_delay(rt_uint32_t us)
70 {
71     rt_uint32_t start, now, delta, reload, us_tick;
72     start = SysTick->VAL;
73     reload = SysTick->LOAD;
74     us_tick = SystemCoreClock / 1000000UL;
75     do
76     {
77         now = SysTick->VAL;
78         delta = start >= now ? start - now : reload + start - now;
79     }
80     while (delta < us_tick * us);
81 }
82