board.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2019, 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 <stdint.h>
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include "stm32f1xx_hal.h"
  15. #define _SCB_BASE (0xE000E010UL)
  16. #define _SYSTICK_CTRL (*(rt_uint32_t *)(_SCB_BASE + 0x0))
  17. #define _SYSTICK_LOAD (*(rt_uint32_t *)(_SCB_BASE + 0x4))
  18. #define _SYSTICK_VAL (*(rt_uint32_t *)(_SCB_BASE + 0x8))
  19. #define _SYSTICK_CALIB (*(rt_uint32_t *)(_SCB_BASE + 0xC))
  20. #define _SYSTICK_PRI (*(rt_uint8_t *)(0xE000ED23UL))
  21. // Updates the variable SystemCoreClock and must be called
  22. // whenever the core clock is changed during program execution.
  23. extern void SystemCoreClockUpdate(void);
  24. // Holds the system core clock, which is the system clock
  25. // frequency supplied to the SysTick timer and the processor
  26. // core clock.
  27. extern uint32_t SystemCoreClock;
  28. extern UART_HandleTypeDef huart5;
  29. void rt_hw_console_output(const char *str)
  30. {
  31. rt_size_t i = 0, size = 0;
  32. char a = '\r';
  33. __HAL_UNLOCK(&huart5);
  34. size = rt_strlen(str);
  35. for (i = 0; i < size; i++)
  36. {
  37. if (*(str + i) == '\n')
  38. {
  39. HAL_UART_Transmit(&huart5, (uint8_t *)&a, 1, 1);
  40. }
  41. HAL_UART_Transmit(&huart5, (uint8_t *)(str + i), 1, 1);
  42. }
  43. }
  44. char rt_hw_console_getchar(void)
  45. {
  46. int ch = -1;
  47. if (__HAL_UART_GET_FLAG(&huart5, UART_FLAG_RXNE) != RESET)
  48. {
  49. ch = huart5.Instance->DR & 0xff;
  50. }
  51. else
  52. {
  53. if(__HAL_UART_GET_FLAG(&huart5, UART_FLAG_ORE) != RESET)
  54. {
  55. __HAL_UART_CLEAR_OREFLAG(&huart5);
  56. }
  57. rt_thread_mdelay(10);
  58. }
  59. return ch;
  60. }
  61. static uint32_t _SysTick_Config(rt_uint32_t ticks)
  62. {
  63. if ((ticks - 1) > 0xFFFFFF)
  64. {
  65. return 1;
  66. }
  67. _SYSTICK_LOAD = ticks - 1;
  68. _SYSTICK_PRI = 0xFF;
  69. _SYSTICK_VAL = 0;
  70. _SYSTICK_CTRL = 0x07;
  71. return 0;
  72. }
  73. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  74. #define RT_HEAP_SIZE 1024*8
  75. static uint32_t rt_heap[RT_HEAP_SIZE]; // heap default size: 4K(1024 * 4)
  76. RT_WEAK void *rt_heap_begin_get(void)
  77. {
  78. return rt_heap;
  79. }
  80. RT_WEAK void *rt_heap_end_get(void)
  81. {
  82. return rt_heap + RT_HEAP_SIZE;
  83. }
  84. #endif
  85. /**
  86. * This function will initial your board.
  87. */
  88. void rt_hw_board_init()
  89. {
  90. /* System Clock Update */
  91. SystemCoreClockUpdate();
  92. /* System Tick Configuration */
  93. _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  94. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  95. #ifdef RT_USING_COMPONENTS_INIT
  96. rt_components_board_init();
  97. #endif
  98. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  99. rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
  100. #endif
  101. }
  102. void SysTick_Handler(void)
  103. {
  104. /* enter interrupt */
  105. rt_interrupt_enter();
  106. rt_tick_increase();
  107. /* leave interrupt */
  108. rt_interrupt_leave();
  109. }