board.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2021, 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 "tick.h"
  15. #include "drv_uart.h"
  16. #include "encoding.h"
  17. #include "stack.h"
  18. #include "sbi.h"
  19. #include "riscv.h"
  20. #include "stack.h"
  21. #include "hal_gpio.h"
  22. #include "hal_clk.h"
  23. #include "hal_uart.h"
  24. #include "hal_dma.h"
  25. #ifdef RT_USING_SMART
  26. #include "riscv_mmu.h"
  27. #include "mmu.h"
  28. #include "page.h"
  29. #include "lwp_arch.h"
  30. //这个结构体描述了buddy system的页分配范围
  31. rt_region_t init_page_region =
  32. {
  33. (rt_size_t)RT_HW_PAGE_START,
  34. (rt_size_t)RT_HW_PAGE_END
  35. };
  36. //内核页表
  37. volatile rt_size_t MMUTable[__SIZE(VPN2_BIT)] __attribute__((aligned(4 * 1024)));
  38. rt_mmu_info mmu_info;
  39. #endif
  40. //初始化BSS节区
  41. void init_bss(void)
  42. {
  43. unsigned int *dst;
  44. dst = &__bss_start;
  45. while (dst < &__bss_end)
  46. {
  47. *dst++ = 0;
  48. }
  49. }
  50. static void __rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  51. {
  52. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  53. asm volatile("ebreak":::"memory");
  54. }
  55. //BSP的C入口
  56. void primary_cpu_entry(void)
  57. {
  58. //初始化BSS
  59. init_bss();
  60. //关中断
  61. rt_hw_interrupt_disable();
  62. rt_assert_set_hook(__rt_assert_handler);
  63. //启动RT-Thread Smart内核
  64. entry();
  65. }
  66. //这个初始化程序由内核主动调用,此时调度器还未启动,因此在此不能使用依赖线程上下文的函数
  67. void rt_hw_board_init(void)
  68. {
  69. #ifdef RT_USING_SMART
  70. rt_page_init(init_page_region);
  71. rt_hw_mmu_map_init(&mmu_info,(void *)USER_VADDR_START, USER_VADDR_TOP - USER_VADDR_START, (rt_size_t *)MMUTable, 0);
  72. //将低1GB MMIO区域设置为无Cache与Strong Order访存模式
  73. MMUTable[0] &= ~PTE_CACHE;
  74. MMUTable[0] &= ~PTE_SHARE;
  75. MMUTable[0] |= PTE_SO;
  76. rt_hw_mmu_switch((void *)MMUTable);
  77. #endif
  78. /* initalize interrupt */
  79. rt_hw_interrupt_init();
  80. #ifdef RT_USING_HEAP
  81. rt_kprintf("heap: [0x%08x - 0x%08x]\n", (rt_ubase_t) RT_HW_HEAP_BEGIN, (rt_ubase_t) RT_HW_HEAP_END);
  82. /* initialize memory system */
  83. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  84. #endif
  85. /* init hal hardware */
  86. hal_clock_init();
  87. hal_gpio_init();
  88. hal_uart_init(0);
  89. hal_dma_init();
  90. /* init rtthread hardware */
  91. rt_hw_uart_init();
  92. rt_hw_tick_init();
  93. #ifdef RT_USING_CONSOLE
  94. /* set console device */
  95. rt_console_set_device("uart");
  96. #endif /* RT_USING_CONSOLE */
  97. #ifdef RT_USING_COMPONENTS_INIT
  98. rt_components_board_init();
  99. #endif
  100. }
  101. void rt_hw_cpu_reset(void)
  102. {
  103. sbi_shutdown();
  104. while(1);
  105. }
  106. MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reboot, reset machine);