board.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2024-01-29 yandld first implementation
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "board.h"
  14. #include "clock_config.h"
  15. #include "drv_uart.h"
  16. #include "fsl_cache_lpcac.h"
  17. /**
  18. * This is the timer interrupt service routine.
  19. *
  20. */
  21. void SysTick_Handler(void)
  22. {
  23. /* enter interrupt */
  24. rt_interrupt_enter();
  25. rt_tick_increase();
  26. /* leave interrupt */
  27. rt_interrupt_leave();
  28. }
  29. /**
  30. * This function will initial board.
  31. */
  32. void rt_hw_board_init()
  33. {
  34. /* Hardware Initialization */
  35. BOARD_InitBootPins();
  36. L1CACHE_EnableCodeCache();
  37. CLOCK_EnableClock(kCLOCK_Freqme);
  38. CLOCK_EnableClock(kCLOCK_InputMux);
  39. CLOCK_EnableClock(kCLOCK_Port0);
  40. CLOCK_EnableClock(kCLOCK_Port1);
  41. CLOCK_EnableClock(kCLOCK_Port2);
  42. CLOCK_EnableClock(kCLOCK_Port3);
  43. CLOCK_EnableClock(kCLOCK_Port4);
  44. CLOCK_EnableClock(kCLOCK_Gpio0);
  45. CLOCK_EnableClock(kCLOCK_Gpio1);
  46. CLOCK_EnableClock(kCLOCK_Gpio2);
  47. CLOCK_EnableClock(kCLOCK_Gpio3);
  48. CLOCK_EnableClock(kCLOCK_Gpio4);
  49. CLOCK_EnableClock(kCLOCK_Pint);
  50. CLOCK_EnableClock(kCLOCK_Flexcan0);
  51. CLOCK_EnableClock(kCLOCK_Flexcan1);
  52. CLOCK_AttachClk(kFRO_HF_to_ADC0);
  53. CLOCK_SetClkDiv(kCLOCK_DivAdc0Clk, 1u);
  54. /* enable VREF */
  55. SPC0->ACTIVE_CFG1 |= 0xFFFFFFFF;
  56. CLOCK_EnableClock(kCLOCK_Dma0);
  57. CLOCK_EnableClock(kCLOCK_Dma1);
  58. edma_config_t userConfig = {0};
  59. EDMA_GetDefaultConfig(&userConfig);
  60. EDMA_Init(DMA0, &userConfig);
  61. EDMA_Init(DMA1, &userConfig);
  62. /* This init has finished in secure side of TF-M */
  63. BOARD_InitBootClocks();
  64. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  65. /* set pend exception priority */
  66. NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
  67. /*init uart device*/
  68. rt_hw_uart_init();
  69. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  70. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  71. #endif
  72. #ifdef RT_USING_COMPONENTS_INIT
  73. /* initialization board with RT-Thread Components */
  74. rt_components_board_init();
  75. #endif
  76. #ifdef RT_USING_HEAP
  77. rt_kprintf("sram heap, begin: 0x%p, end: 0x%p\n", HEAP_BEGIN, HEAP_END);
  78. rt_system_heap_init((void *)HEAP_BEGIN, (void *)(HEAP_END));
  79. #endif
  80. }
  81. /**
  82. * This function will called when memory fault.
  83. */
  84. void MemManage_Handler(void)
  85. {
  86. extern void HardFault_Handler(void);
  87. rt_kprintf("Memory Fault!\n");
  88. HardFault_Handler();
  89. }
  90. void rt_hw_us_delay(rt_uint32_t us)
  91. {
  92. rt_uint32_t ticks;
  93. rt_uint32_t told, tnow, tcnt = 0;
  94. rt_uint32_t reload = SysTick->LOAD;
  95. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  96. told = SysTick->VAL;
  97. while (1)
  98. {
  99. tnow = SysTick->VAL;
  100. if (tnow != told)
  101. {
  102. if (tnow < told)
  103. {
  104. tcnt += told - tnow;
  105. }
  106. else
  107. {
  108. tcnt += reload - tnow + told;
  109. }
  110. told = tnow;
  111. if (tcnt >= ticks)
  112. {
  113. break;
  114. }
  115. }
  116. }
  117. }