drv_common_subm.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2021-07-14 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtthread.h>
  13. #if defined(USE_MA35D1_SUBM)
  14. #include <rthw.h>
  15. #include <stdio.h>
  16. #include "drv_common.h"
  17. #include "board.h"
  18. #include "drv_uart.h"
  19. #define LOG_TAG "drv.common"
  20. #undef DBG_ENABLE
  21. #define DBG_SECTION_NAME LOG_TAG
  22. #define DBG_LEVEL LOG_LVL_DBG
  23. #define DBG_COLOR
  24. #include <rtdbg.h>
  25. extern void nu_clock_init(void);
  26. void SysTick_Handler(void)
  27. {
  28. /* enter interrupt */
  29. rt_interrupt_enter();
  30. rt_tick_increase();
  31. /* leave interrupt */
  32. rt_interrupt_leave();
  33. }
  34. int rt_hw_systick_init(void)
  35. {
  36. /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  37. SystemCoreClockUpdate();
  38. /* Configure SysTick */
  39. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  40. return 0;
  41. }
  42. /**
  43. * The time delay function.
  44. *
  45. * @param microseconds.
  46. */
  47. void rt_hw_us_delay(rt_uint32_t us)
  48. {
  49. rt_uint32_t ticks;
  50. rt_uint32_t told, tnow, tcnt = 0;
  51. rt_uint32_t reload = SysTick->LOAD;
  52. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  53. told = SysTick->VAL;
  54. while (1)
  55. {
  56. tnow = SysTick->VAL;
  57. if (tnow != told)
  58. {
  59. if (tnow < told)
  60. {
  61. tcnt += told - tnow;
  62. }
  63. else
  64. {
  65. tcnt += reload - tnow + told;
  66. }
  67. told = tnow;
  68. if (tcnt >= ticks)
  69. {
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. void idle_wfi(void)
  76. {
  77. __WFI();
  78. }
  79. rt_weak void nutool_pincfg_init(void)
  80. {
  81. }
  82. /**
  83. * This function will initial board.
  84. */
  85. rt_weak void rt_hw_board_init(void)
  86. {
  87. /* Unlock protected registers */
  88. SYS_UnlockReg();
  89. /* initialize base clock */
  90. nu_clock_init();
  91. /* initialize peripheral pin function */
  92. nutool_pincfg_init();
  93. /* initialize hardware interrupt */
  94. rt_hw_interrupt_init();
  95. #if defined(RT_USING_HEAP)
  96. rt_system_heap_init((void *)BOARD_HEAP_START, (void *)BOARD_HEAP_END);
  97. #endif
  98. /* initialize uart */
  99. rt_hw_uart_init();
  100. #if defined(RT_USING_CONSOLE)
  101. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  102. #endif
  103. #if defined(RT_USING_HEAP)
  104. /* Dump heap information */
  105. rt_kprintf("Heap: Begin@%08x, END@%08x, SIZE: %d KiB\n", BOARD_HEAP_START, BOARD_HEAP_END, ((rt_uint32_t)BOARD_HEAP_END - (rt_uint32_t)BOARD_HEAP_START) / 1024);
  106. #endif
  107. /* initialize systick */
  108. rt_hw_systick_init();
  109. rt_thread_idle_sethook(idle_wfi);
  110. #if defined(RT_USING_COMPONENTS_INIT)
  111. rt_components_board_init();
  112. #endif
  113. }
  114. #endif