board.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * 2017-07-24 Tanek the first version
  9. * 2018-11-12 Ernest Chen modify copyright
  10. */
  11. #include "board.h"
  12. #include <stdint.h>
  13. #include "drv_usart.h"
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. /* Updates the variable SystemCoreClock and must be called
  17. * whenever the core clock is changed during program execution.
  18. */
  19. extern void SystemCoreClockUpdate(void);
  20. /* core clock */
  21. extern uint32_t SystemCoreClock;
  22. volatile rt_uint32_t g_ticks=0;
  23. static uint32_t _SysTick_Config(rt_uint32_t ticks)
  24. {
  25. g_ticks=ticks;
  26. /* disable hardware pushing stack automatically and disable interrupt nesting */
  27. PFIC->CFGR=0xFA050003;
  28. NVIC_SetPriority(SysTicK_IRQn,0xf0);
  29. NVIC_SetPriority(Software_IRQn,0xf0);
  30. SysTick->CTLR=0;
  31. SysTick->CNTL0=0;SysTick->CNTL1=0;SysTick->CNTL2=0;SysTick->CNTL3=0;
  32. SysTick->CNTH0=0;SysTick->CNTH1=0;SysTick->CNTH2=0;SysTick->CNTH3=0;
  33. SysTick->CMPLR0=(rt_uint8_t)(g_ticks-1);
  34. SysTick->CMPLR1=(rt_uint8_t)((g_ticks-1)>>8);
  35. SysTick->CMPLR2=(rt_uint8_t)((g_ticks-1)>>16);
  36. SysTick->CMPLR3=(rt_uint8_t)((g_ticks-1)>>24);
  37. SysTick->CMPHR0=0;SysTick->CMPHR1=0;SysTick->CMPHR2=0;SysTick->CMPHR3=0;
  38. SysTick->CTLR=0x1;
  39. NVIC_EnableIRQ(SysTicK_IRQn);
  40. NVIC_EnableIRQ(Software_IRQn);
  41. return 0;
  42. }
  43. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  44. #define RT_HEAP_SIZE (1024)
  45. /* heap default size: 4K(1024 * 4) */
  46. static uint32_t rt_heap[RT_HEAP_SIZE];
  47. rt_weak void *rt_heap_begin_get(void)
  48. {
  49. return rt_heap;
  50. }
  51. rt_weak void *rt_heap_end_get(void)
  52. {
  53. return rt_heap + RT_HEAP_SIZE;
  54. }
  55. #endif
  56. /**
  57. * This function will initial your board.
  58. */
  59. void rt_hw_board_init()
  60. {
  61. /* System Clock Update */
  62. SystemCoreClockUpdate();
  63. /* System Tick Configuration, systick clock is HCLK/8 */
  64. _SysTick_Config(SystemCoreClock / 8 / RT_TICK_PER_SECOND);
  65. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  66. #ifdef RT_USING_COMPONENTS_INIT
  67. rt_components_board_init();
  68. #endif
  69. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  70. rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
  71. #endif
  72. #ifdef RT_USING_CONSOLE
  73. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  74. #endif
  75. }
  76. void SysTick_Handler(void) __attribute__((interrupt()));
  77. void SysTick_Handler(void)
  78. {
  79. GET_INT_SP();
  80. /* enter interrupt */
  81. rt_interrupt_enter();
  82. SysTick->CTLR=0;
  83. SysTick->CNTL0=0;SysTick->CNTL1=0;SysTick->CNTL2=0;SysTick->CNTL3=0;
  84. SysTick->CNTH0=0;SysTick->CNTH1=0;SysTick->CNTH2=0;SysTick->CNTH3=0;
  85. SysTick->CTLR=0x1;
  86. rt_tick_increase();
  87. /* leave interrupt */
  88. rt_interrupt_leave();
  89. FREE_INT_SP();
  90. }