drv_common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * 2022-07-1 Rbb666 first version
  9. */
  10. #include "drv_common.h"
  11. #ifdef RT_USING_SERIAL
  12. #include "drv_uart.h"
  13. #endif
  14. #define DBG_TAG "drv_common"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #ifdef RT_USING_FINSH
  18. #include <finsh.h>
  19. static void reboot(uint8_t argc, char **argv)
  20. {
  21. rt_hw_cpu_reset();
  22. }
  23. MSH_CMD_EXPORT(reboot, Reboot System);
  24. #endif /* RT_USING_FINSH */
  25. /**
  26. * this is the timer interrupt service routine.
  27. */
  28. void SysTick_Handler_CB(void)
  29. {
  30. /* enter interrupt */
  31. rt_interrupt_enter();
  32. rt_tick_increase();
  33. /* leave interrupt */
  34. rt_interrupt_leave();
  35. }
  36. /* systick configuration */
  37. void rt_hw_systick_init(void)
  38. {
  39. Cy_SysTick_Init(CY_SYSTICK_CLOCK_SOURCE_CLK_CPU, SystemCoreClock / RT_TICK_PER_SECOND);
  40. Cy_SysTick_SetCallback(0, SysTick_Handler_CB);
  41. Cy_SysTick_EnableInterrupt();
  42. }
  43. /**
  44. * @brief this function is executed in case of error occurrence.
  45. * @param none
  46. * @retval none
  47. */
  48. void _Error_Handler(char *s, int num)
  49. {
  50. /* User can add his own implementation to report the HAL error return state */
  51. LOG_E("Error_Handler at file:%s num:%d", s, num);
  52. while (1)
  53. {
  54. }
  55. }
  56. /**
  57. * this function will delay for some us.
  58. *
  59. * @param us the delay time of us
  60. */
  61. void rt_hw_us_delay(rt_uint32_t us)
  62. {
  63. rt_uint32_t start, now, delta, reload, us_tick;
  64. start = SysTick->VAL;
  65. reload = SysTick->LOAD;
  66. us_tick = SystemCoreClock / 1000000UL;
  67. do
  68. {
  69. now = SysTick->VAL;
  70. delta = start > now ? start - now : reload + start - now;
  71. }
  72. while(delta < us_tick * us);
  73. }
  74. /**
  75. * this function will initial ifx board.
  76. */
  77. rt_weak void rt_hw_board_init()
  78. {
  79. cy_bsp_all_init();
  80. /* systick init */
  81. rt_hw_systick_init();
  82. /* heap initialization */
  83. #if defined(RT_USING_HEAP)
  84. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  85. #endif
  86. /* pin driver initialization is open by default */
  87. #ifdef RT_USING_PIN
  88. rt_hw_pin_init();
  89. #endif
  90. /* usart driver initialization is open by default */
  91. #ifdef RT_USING_SERIAL
  92. rt_hw_uart_init();
  93. #endif
  94. /* set the shell console output device */
  95. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  96. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  97. #endif
  98. /* board underlying hardware initialization */
  99. #ifdef RT_USING_COMPONENTS_INIT
  100. rt_components_board_init();
  101. #endif
  102. }