drv_common.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * 2018-11-7 SummerGift first version
  9. * 2021-08-27 Jiao change to fm33
  10. */
  11. #include "drv_common.h"
  12. #include "system_fm33lc0xx.h"
  13. #ifdef RT_USING_SERIAL
  14. #include "drv_usart.h"
  15. #endif
  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. /* SysTick configuration */
  25. void rt_hw_systick_init(void)
  26. {
  27. SystemCoreClockUpdate();
  28. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  29. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  30. }
  31. /**
  32. * This is the timer interrupt service routine.
  33. *
  34. */
  35. void SysTick_Handler(void)
  36. {
  37. /* enter interrupt */
  38. rt_interrupt_enter();
  39. rt_tick_increase();
  40. /* leave interrupt */
  41. rt_interrupt_leave();
  42. }
  43. uint32_t HAL_GetTick(void)
  44. {
  45. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  46. }
  47. /**
  48. * @brief This function is executed in case of error occurrence.
  49. * @param None
  50. * @retval None
  51. */
  52. void _Error_Handler(char *s, int num)
  53. {
  54. /* USER CODE BEGIN Error_Handler */
  55. /* User can add his own implementation to report the HAL error return state */
  56. while (1)
  57. {
  58. }
  59. /* USER CODE END Error_Handler */
  60. }
  61. /**
  62. * This function will delay for some us.
  63. *
  64. * @param us the delay time of us
  65. */
  66. void rt_hw_us_delay(rt_uint32_t us)
  67. {
  68. rt_uint32_t start, now, delta, reload, us_tick;
  69. start = SysTick->VAL;
  70. reload = SysTick->LOAD;
  71. us_tick = SystemCoreClock / 1000000UL;
  72. do
  73. {
  74. now = SysTick->VAL;
  75. delta = start > now ? start - now : reload + start - now;
  76. }
  77. while (delta < us_tick * us);
  78. }
  79. /**
  80. * This function will initial STM32 board.
  81. */
  82. rt_weak void rt_hw_board_init()
  83. {
  84. void SelRCHFToPLL(uint32_t rchf, uint32_t clock);
  85. SelRCHFToPLL(RCHF8M_TRIM, 63);
  86. rt_hw_systick_init();
  87. extern int Image$$RW_IRAM1$$ZI$$Limit;
  88. #define HEAP_BEGIN ((void *)&Image$$RW_IRAM1$$ZI$$Limit)
  89. /* Heap initialization */
  90. #if defined(RT_USING_HEAP)
  91. rt_system_heap_init((void *)HEAP_BEGIN, (void *)(0x20000000 + 0x6000));
  92. #endif
  93. /* USART driver initialization is open by default */
  94. #ifdef RT_USING_SERIAL
  95. rt_hw_usart_init();
  96. #endif
  97. /* Set the shell console output device */
  98. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  99. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  100. #endif
  101. /* Board underlying hardware initialization */
  102. #ifdef RT_USING_COMPONENTS_INIT
  103. rt_components_board_init();
  104. #endif
  105. }