board.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2017-2019, MindMotion AE Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-02 spaceman first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <board.h>
  13. #include <drv_uart.h>
  14. // extern uint32_t SystemCoreClock;
  15. // extern void SystemInit(void);
  16. #ifdef RT_USING_FINSH
  17. #include <finsh.h>
  18. static void reboot(uint8_t argc, char **argv)
  19. {
  20. rt_hw_cpu_reset();
  21. }
  22. MSH_CMD_EXPORT(reboot, Reboot System);
  23. #endif /* RT_USING_FINSH */
  24. static void bsp_clock_config(void)
  25. {
  26. RemapVtorTable();
  27. SystemClk_HSEInit(RCC_PLLMul_20);//启动PLL时钟,12MHz*20=240MHz
  28. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//2:2,全局性函数,仅需设置一次
  29. uint32_t sysclk = 0;
  30. getSystemClock(&sysclk);
  31. SysTick_Config(sysclk / RT_TICK_PER_SECOND);
  32. SysTick->CTRL |= 0x00000004UL;
  33. }
  34. void SysTick_Handler(void)
  35. {
  36. /* enter interrupt */
  37. rt_interrupt_enter();
  38. rt_tick_increase();
  39. /* leave interrupt */
  40. rt_interrupt_leave();
  41. }
  42. void rt_hw_us_delay(rt_uint32_t us)
  43. {
  44. rt_uint32_t ticks;
  45. rt_uint32_t told, tnow, tcnt = 0;
  46. rt_uint32_t reload = SysTick->LOAD;
  47. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  48. told = SysTick->VAL;
  49. while (1)
  50. {
  51. tnow = SysTick->VAL;
  52. if (tnow != told)
  53. {
  54. if (tnow < told)
  55. {
  56. tcnt += told - tnow;
  57. }
  58. else
  59. {
  60. tcnt += reload - tnow + told;
  61. }
  62. told = tnow;
  63. if (tcnt >= ticks)
  64. {
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. void rt_hw_board_init()
  71. {
  72. bsp_clock_config();
  73. #if defined(RT_USING_HEAP)
  74. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  75. #endif
  76. // volatile uint16_t i=0;
  77. // rt_hw_uart_init();
  78. // i = UINT16_MAX;
  79. // while(i--); //wait for a while after uart initiated.
  80. #ifdef RT_USING_COMPONENTS_INIT
  81. rt_components_board_init();
  82. #endif
  83. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  84. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  85. #endif
  86. }