drv_common.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 ticks;
  64. rt_uint32_t told, tnow, tcnt = 0;
  65. rt_uint32_t reload = SysTick->LOAD;
  66. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  67. told = SysTick->VAL;
  68. while (1)
  69. {
  70. tnow = SysTick->VAL;
  71. if (tnow != told)
  72. {
  73. if (tnow < told)
  74. {
  75. tcnt += told - tnow;
  76. }
  77. else
  78. {
  79. tcnt += reload - tnow + told;
  80. }
  81. told = tnow;
  82. if (tcnt >= ticks)
  83. {
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * this function will initial ifx board.
  91. */
  92. RT_WEAK void rt_hw_board_init()
  93. {
  94. cy_bsp_all_init();
  95. /* systick init */
  96. rt_hw_systick_init();
  97. /* heap initialization */
  98. #if defined(RT_USING_HEAP)
  99. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  100. #endif
  101. /* pin driver initialization is open by default */
  102. #ifdef RT_USING_PIN
  103. rt_hw_pin_init();
  104. #endif
  105. /* usart driver initialization is open by default */
  106. #ifdef RT_USING_SERIAL
  107. rt_hw_uart_init();
  108. #endif
  109. /* set the shell console output device */
  110. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  111. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  112. #endif
  113. /* board underlying hardware initialization */
  114. #ifdef RT_USING_COMPONENTS_INIT
  115. rt_components_board_init();
  116. #endif
  117. }