1 /*
2  * Copyright (c) 2006-2024 RT-Thread Development Team
3  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Change Logs:
8  * Date           Author       Notes
9  * 2024-02-06     yandld       first implementation
10  */
11 
12 #include <rthw.h>
13 #include <rtthread.h>
14 
15 #include "board.h"
16 #include "clock_config.h"
17 #include "drv_uart.h"
18 
19 /**
20  * This is the timer interrupt service routine.
21  *
22  */
SysTick_Handler(void)23 void SysTick_Handler(void)
24 {
25     /* enter interrupt */
26     rt_interrupt_enter();
27 
28     rt_tick_increase();
29 
30     /* leave interrupt */
31     rt_interrupt_leave();
32 }
33 
34 /**
35  * This function will initial board.
36  */
rt_hw_board_init()37 void rt_hw_board_init()
38 {
39     BOARD_InitBootPins();
40 
41     edma_config_t userConfig = {0};
42     EDMA_GetDefaultConfig(&userConfig);
43     EDMA_Init(DMA0, &userConfig);
44 
45     /* This init has finished in secure side of TF-M  */
46     BOARD_InitBootClocks();
47 
48     SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
49     /* set pend exception priority */
50     NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
51 
52     /*init uart device*/
53     rt_hw_uart_init();
54 
55 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
56     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
57 #endif
58 
59 #ifdef RT_USING_COMPONENTS_INIT
60     /* initialization board with RT-Thread Components */
61     rt_components_board_init();
62 #endif
63 
64 #ifdef RT_USING_HEAP
65     rt_kprintf("sram heap, begin: 0x%p, end: 0x%p\n", HEAP_BEGIN, HEAP_END);
66     rt_system_heap_init((void *)HEAP_BEGIN, (void *)(HEAP_END));
67 #endif
68 }
69 
70 /**
71  * This function will called when memory fault.
72  */
MemManage_Handler(void)73 void MemManage_Handler(void)
74 {
75     extern void HardFault_Handler(void);
76 
77     rt_kprintf("Memory Fault!\n");
78     HardFault_Handler();
79 }
80 
rt_hw_us_delay(rt_uint32_t us)81 void rt_hw_us_delay(rt_uint32_t us)
82 {
83     rt_uint32_t ticks;
84     rt_uint32_t told, tnow, tcnt = 0;
85     rt_uint32_t reload = SysTick->LOAD;
86 
87     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
88     told = SysTick->VAL;
89     while (1)
90     {
91         tnow = SysTick->VAL;
92         if (tnow != told)
93         {
94             if (tnow < told)
95             {
96                 tcnt += told - tnow;
97             }
98             else
99             {
100                 tcnt += reload - tnow + told;
101             }
102             told = tnow;
103             if (tcnt >= ticks)
104             {
105                 break;
106             }
107         }
108     }
109 }
110