|
| 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 | + * 2021-05-24 the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rthw.h> |
| 12 | +#include <rtthread.h> |
| 13 | + |
| 14 | +#include "main.h" |
| 15 | + |
| 16 | +#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP) |
| 17 | +/* |
| 18 | + * Please modify RT_HEAP_SIZE if you enable RT_USING_HEAP |
| 19 | + * the RT_HEAP_SIZE max value = (sram size - ZI size), 1024 means 1024 bytes |
| 20 | + */ |
| 21 | +#define RT_HEAP_SIZE (15*1024) |
| 22 | +static rt_uint8_t rt_heap[RT_HEAP_SIZE]; |
| 23 | + |
| 24 | +RT_WEAK void *rt_heap_begin_get(void) |
| 25 | +{ |
| 26 | + return rt_heap; |
| 27 | +} |
| 28 | + |
| 29 | +RT_WEAK void *rt_heap_end_get(void) |
| 30 | +{ |
| 31 | + return rt_heap + RT_HEAP_SIZE; |
| 32 | +} |
| 33 | +#endif |
| 34 | + |
| 35 | +void SysTick_Handler(void) |
| 36 | +{ |
| 37 | + rt_interrupt_enter(); |
| 38 | + |
| 39 | + rt_tick_increase(); |
| 40 | + |
| 41 | + rt_interrupt_leave(); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * This function will initial your board. |
| 46 | + */ |
| 47 | +void rt_hw_board_init(void) |
| 48 | +{ |
| 49 | + extern void SystemClock_Config(void); |
| 50 | + |
| 51 | + HAL_Init(); |
| 52 | + SystemClock_Config(); |
| 53 | + SystemCoreClockUpdate(); |
| 54 | + /* |
| 55 | + * 1: OS Tick Configuration |
| 56 | + * Enable the hardware timer and call the rt_os_tick_callback function |
| 57 | + * periodically with the frequency RT_TICK_PER_SECOND. |
| 58 | + */ |
| 59 | + HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND); |
| 60 | + |
| 61 | + /* Call components board initial (use INIT_BOARD_EXPORT()) */ |
| 62 | +#ifdef RT_USING_COMPONENTS_INIT |
| 63 | + rt_components_board_init(); |
| 64 | +#endif |
| 65 | + |
| 66 | +#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP) |
| 67 | + rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get()); |
| 68 | +#endif |
| 69 | +} |
| 70 | + |
| 71 | +#ifdef RT_USING_CONSOLE |
| 72 | + |
| 73 | +static UART_HandleTypeDef UartHandle; |
| 74 | +static int uart_init(void) |
| 75 | +{ |
| 76 | + /* TODO: Please modify the UART port number according to your needs */ |
| 77 | + UartHandle.Instance = USART2; |
| 78 | + UartHandle.Init.BaudRate = 115200; |
| 79 | + UartHandle.Init.WordLength = UART_WORDLENGTH_8B; |
| 80 | + UartHandle.Init.StopBits = UART_STOPBITS_1; |
| 81 | + UartHandle.Init.Parity = UART_PARITY_NONE; |
| 82 | + UartHandle.Init.Mode = UART_MODE_TX_RX; |
| 83 | + UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
| 84 | + UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; |
| 85 | + |
| 86 | + if (HAL_UART_Init(&UartHandle) != HAL_OK) |
| 87 | + { |
| 88 | + while (1); |
| 89 | + } |
| 90 | + return 0; |
| 91 | +} |
| 92 | +INIT_BOARD_EXPORT(uart_init); |
| 93 | + |
| 94 | +void rt_hw_console_output(const char *str) |
| 95 | +{ |
| 96 | + rt_size_t i = 0, size = 0; |
| 97 | + char a = '\r'; |
| 98 | + |
| 99 | + __HAL_UNLOCK(&UartHandle); |
| 100 | + |
| 101 | + size = rt_strlen(str); |
| 102 | + |
| 103 | + for (i = 0; i < size; i++) |
| 104 | + { |
| 105 | + if (*(str + i) == '\n') |
| 106 | + { |
| 107 | + HAL_UART_Transmit(&UartHandle, (uint8_t *)&a, 1, 1); |
| 108 | + } |
| 109 | + HAL_UART_Transmit(&UartHandle, (uint8_t *)(str + i), 1, 1); |
| 110 | + } |
| 111 | +} |
| 112 | +#endif |
| 113 | + |
| 114 | +#ifdef RT_USING_FINSH |
| 115 | +char rt_hw_console_getchar(void) |
| 116 | +{ |
| 117 | + /* Note: the initial value of ch must < 0 */ |
| 118 | + int ch = -1; |
| 119 | + |
| 120 | + if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) |
| 121 | + { |
| 122 | + ch = UartHandle.Instance->DR & 0xff; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + rt_thread_mdelay(10); |
| 127 | + } |
| 128 | + return ch; |
| 129 | +} |
| 130 | +#endif |
0 commit comments