1 /*
2 * Copyright (c) 2006-2022, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-01-30 lizhirui first version
9 */
10
11 #include <rthw.h>
12 #include <rtthread.h>
13 #include <rtdevice.h>
14
15 #include "board.h"
16 #include "mm_aspace.h"
17 #include "tick.h"
18
19 #include "drv_uart.h"
20 #include "encoding.h"
21 #include "stack.h"
22 #include "sbi.h"
23 #include "riscv.h"
24 #include "stack.h"
25
26 #include "hal_gpio.h"
27 #include "hal_clk.h"
28 #include "hal_uart.h"
29 #include "hal_dma.h"
30
31 #ifdef RT_USING_SMART
32 #include "riscv_mmu.h"
33 #include "mmu.h"
34 #include "page.h"
35 #include "lwp_arch.h"
36
37 // 这个结构体描述了buddy system的页分配范围
38 rt_region_t init_page_region =
39 {
40 (rt_size_t)RT_HW_PAGE_START,
41 (rt_size_t)RT_HW_PAGE_END};
42
43 // 内核页表
44 extern volatile rt_size_t MMUTable[__SIZE(VPN2_BIT)] __attribute__((aligned(4 * 1024)));
45
46 struct mem_desc platform_mem_desc[] = {
47 {KERNEL_VADDR_START, KERNEL_VADDR_START + 0x4000000 - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
48 {0x1000, 0x3ffff000 - 1, (rt_size_t)ARCH_MAP_FAILED, DEVICE_MEM},
49 };
50
51 #define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))
52
53 #endif /* RT_USING_SMART */
54
55 // 初始化BSS节区
init_bss(void)56 void init_bss(void)
57 {
58 unsigned int *dst;
59
60 dst = &__bss_start;
61 while (dst < &__bss_end)
62 {
63 *dst++ = 0;
64 }
65 }
66
__rt_assert_handler(const char * ex_string,const char * func,rt_size_t line)67 static void __rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
68 {
69 rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
70 asm volatile("ebreak" ::
71 : "memory");
72 }
73
74 // BSP的C入口
primary_cpu_entry(void)75 void primary_cpu_entry(void)
76 {
77 // 关中断
78 rt_hw_interrupt_disable();
79 rt_assert_set_hook(__rt_assert_handler);
80 // 启动RT-Thread Smart内核
81 entry();
82 }
83
84 #define IOREMAP_SIZE (1ul << 30)
85
86 // 这个初始化程序由内核主动调用,此时调度器还未启动,因此在此不能使用依赖线程上下文的函数
rt_hw_board_init(void)87 void rt_hw_board_init(void)
88 {
89 #ifdef RT_USING_SMART
90 rt_hw_mmu_map_init(&rt_kernel_space, (void *)(USER_VADDR_START - IOREMAP_SIZE), IOREMAP_SIZE, (rt_size_t *)MMUTable, 0);
91 rt_page_init(init_page_region);
92 rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, NUM_MEM_DESC);
93 #endif
94 #ifdef RT_USING_HEAP
95 /* initialize memory system */
96 rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
97 #endif
98 /* initalize interrupt */
99 rt_hw_interrupt_init();
100
101 /* init hal hardware */
102 hal_clock_init();
103 hal_gpio_init();
104 hal_dma_init();
105
106 /* init rtthread hardware */
107 rt_hw_uart_init();
108 rt_hw_tick_init();
109
110 #ifdef RT_USING_CONSOLE
111 /* set console device */
112 rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
113 #endif /* RT_USING_CONSOLE */
114
115 #ifdef RT_USING_COMPONENTS_INIT
116 rt_components_board_init();
117 #endif
118 }
119