board.c 2.8 KB

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