add uart
Showing
17 changed files
with
474 additions
and
27 deletions
This diff is collapsed.
Click to expand it.
... | @@ -6,7 +6,7 @@ | ... | @@ -6,7 +6,7 @@ |
6 | <provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/> | 6 | <provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/> |
7 | <provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/> | 7 | <provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/> |
8 | <provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/> | 8 | <provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/> |
9 | <provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="175442219548387224" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> | 9 | <provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-1285976871464041894" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> |
10 | <language-scope id="org.eclipse.cdt.core.gcc"/> | 10 | <language-scope id="org.eclipse.cdt.core.gcc"/> |
11 | <language-scope id="org.eclipse.cdt.core.g++"/> | 11 | <language-scope id="org.eclipse.cdt.core.g++"/> |
12 | </provider> | 12 | </provider> |
... | @@ -18,7 +18,7 @@ | ... | @@ -18,7 +18,7 @@ |
18 | <provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/> | 18 | <provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/> |
19 | <provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/> | 19 | <provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/> |
20 | <provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/> | 20 | <provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/> |
21 | <provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="175442219548387224" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> | 21 | <provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-1285976871464041894" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> |
22 | <language-scope id="org.eclipse.cdt.core.gcc"/> | 22 | <language-scope id="org.eclipse.cdt.core.gcc"/> |
23 | <language-scope id="org.eclipse.cdt.core.g++"/> | 23 | <language-scope id="org.eclipse.cdt.core.g++"/> |
24 | </provider> | 24 | </provider> | ... | ... |
... | @@ -59,8 +59,8 @@ | ... | @@ -59,8 +59,8 @@ |
59 | #define configCPU_CLOCK_HZ ( SystemCoreClock ) | 59 | #define configCPU_CLOCK_HZ ( SystemCoreClock ) |
60 | #define configTICK_RATE_HZ ((TickType_t)1000) | 60 | #define configTICK_RATE_HZ ((TickType_t)1000) |
61 | #define configMAX_PRIORITIES ( 56 ) | 61 | #define configMAX_PRIORITIES ( 56 ) |
62 | #define configMINIMAL_STACK_SIZE ((uint16_t)128) | 62 | #define configMINIMAL_STACK_SIZE ((uint16_t)64) |
63 | #define configTOTAL_HEAP_SIZE ((size_t)3072) | 63 | #define configTOTAL_HEAP_SIZE ((size_t)2560) |
64 | #define configMAX_TASK_NAME_LEN ( 16 ) | 64 | #define configMAX_TASK_NAME_LEN ( 16 ) |
65 | #define configUSE_TRACE_FACILITY 1 | 65 | #define configUSE_TRACE_FACILITY 1 |
66 | #define configUSE_16_BIT_TICKS 0 | 66 | #define configUSE_16_BIT_TICKS 0 | ... | ... |
... | @@ -32,9 +32,11 @@ extern "C" { | ... | @@ -32,9 +32,11 @@ extern "C" { |
32 | 32 | ||
33 | /* Private includes ----------------------------------------------------------*/ | 33 | /* Private includes ----------------------------------------------------------*/ |
34 | /* USER CODE BEGIN Includes */ | 34 | /* USER CODE BEGIN Includes */ |
35 | #include <stdio.h> | ||
35 | #include "led.h" | 36 | #include "led.h" |
36 | #include "beep.h" | 37 | #include "beep.h" |
37 | #include "key.h" | 38 | #include "key.h" |
39 | #include "usart.h" | ||
38 | /* USER CODE END Includes */ | 40 | /* USER CODE END Includes */ |
39 | 41 | ||
40 | /* Exported types ------------------------------------------------------------*/ | 42 | /* Exported types ------------------------------------------------------------*/ |
... | @@ -60,6 +62,10 @@ void Error_Handler(void); | ... | @@ -60,6 +62,10 @@ void Error_Handler(void); |
60 | /* USER CODE END EFP */ | 62 | /* USER CODE END EFP */ |
61 | 63 | ||
62 | /* Private defines -----------------------------------------------------------*/ | 64 | /* Private defines -----------------------------------------------------------*/ |
65 | #define DEBUG_TX_Pin GPIO_PIN_9 | ||
66 | #define DEBUG_TX_GPIO_Port GPIOA | ||
67 | #define DEBUG_RX_Pin GPIO_PIN_10 | ||
68 | #define DEBUG_RX_GPIO_Port GPIOA | ||
63 | #define BEEP_Pin GPIO_PIN_12 | 69 | #define BEEP_Pin GPIO_PIN_12 |
64 | #define BEEP_GPIO_Port GPIOA | 70 | #define BEEP_GPIO_Port GPIOA |
65 | #define LED2_Pin GPIO_PIN_6 | 71 | #define LED2_Pin GPIO_PIN_6 | ... | ... |
... | @@ -49,7 +49,7 @@ | ... | @@ -49,7 +49,7 @@ |
49 | /*#define HAL_RTC_MODULE_ENABLED */ | 49 | /*#define HAL_RTC_MODULE_ENABLED */ |
50 | /*#define HAL_SPI_MODULE_ENABLED */ | 50 | /*#define HAL_SPI_MODULE_ENABLED */ |
51 | #define HAL_TIM_MODULE_ENABLED | 51 | #define HAL_TIM_MODULE_ENABLED |
52 | /*#define HAL_UART_MODULE_ENABLED */ | 52 | #define HAL_UART_MODULE_ENABLED |
53 | /*#define HAL_USART_MODULE_ENABLED */ | 53 | /*#define HAL_USART_MODULE_ENABLED */ |
54 | /*#define HAL_IRDA_MODULE_ENABLED */ | 54 | /*#define HAL_IRDA_MODULE_ENABLED */ |
55 | /*#define HAL_SMARTCARD_MODULE_ENABLED */ | 55 | /*#define HAL_SMARTCARD_MODULE_ENABLED */ | ... | ... |
... | @@ -50,6 +50,7 @@ | ... | @@ -50,6 +50,7 @@ |
50 | void NMI_Handler(void); | 50 | void NMI_Handler(void); |
51 | void HardFault_Handler(void); | 51 | void HardFault_Handler(void); |
52 | void TIM1_BRK_UP_TRG_COM_IRQHandler(void); | 52 | void TIM1_BRK_UP_TRG_COM_IRQHandler(void); |
53 | void USART2_IRQHandler(void); | ||
53 | /* USER CODE BEGIN EFP */ | 54 | /* USER CODE BEGIN EFP */ |
54 | 55 | ||
55 | /* USER CODE END EFP */ | 56 | /* USER CODE END EFP */ | ... | ... |
Core/Inc/usart.h
0 → 100644
1 | /** | ||
2 | ****************************************************************************** | ||
3 | * File Name : USART.h | ||
4 | * Description : This file provides code for the configuration | ||
5 | * of the USART instances. | ||
6 | ****************************************************************************** | ||
7 | * @attention | ||
8 | * | ||
9 | * <h2><center>© Copyright (c) 2019 STMicroelectronics. | ||
10 | * All rights reserved.</center></h2> | ||
11 | * | ||
12 | * This software component is licensed by ST under Ultimate Liberty license | ||
13 | * SLA0044, the "License"; You may not use this file except in compliance with | ||
14 | * the License. You may obtain a copy of the License at: | ||
15 | * www.st.com/SLA0044 | ||
16 | * | ||
17 | ****************************************************************************** | ||
18 | */ | ||
19 | /* Define to prevent recursive inclusion -------------------------------------*/ | ||
20 | #ifndef __usart_H | ||
21 | #define __usart_H | ||
22 | #ifdef __cplusplus | ||
23 | extern "C" { | ||
24 | #endif | ||
25 | |||
26 | /* Includes ------------------------------------------------------------------*/ | ||
27 | #include "main.h" | ||
28 | |||
29 | /* USER CODE BEGIN Includes */ | ||
30 | #define USART_REC_LEN 200 // 最大接收字节数 | ||
31 | |||
32 | extern uint8_t USART_RX_BUF[USART_REC_LEN]; // 接收缓冲,最大USART_REC_LEN个字节,末字节为换行 | ||
33 | extern uint16_t USART_RX_STA; // 接收状态 | ||
34 | |||
35 | #define RXBUFFERSIZE 1 // 缓存大小 | ||
36 | extern uint8_t aRxBuffer[RXBUFFERSIZE]; // HAL库USART接收Buffer | ||
37 | |||
38 | /* USER CODE END Includes */ | ||
39 | |||
40 | extern UART_HandleTypeDef huart1; | ||
41 | extern UART_HandleTypeDef huart2; | ||
42 | |||
43 | /* USER CODE BEGIN Private defines */ | ||
44 | |||
45 | /* USER CODE END Private defines */ | ||
46 | |||
47 | void MX_USART1_UART_Init(void); | ||
48 | void MX_USART2_UART_Init(void); | ||
49 | |||
50 | /* USER CODE BEGIN Prototypes */ | ||
51 | |||
52 | /* USER CODE END Prototypes */ | ||
53 | |||
54 | #ifdef __cplusplus | ||
55 | } | ||
56 | #endif | ||
57 | #endif /*__ usart_H */ | ||
58 | |||
59 | /** | ||
60 | * @} | ||
61 | */ | ||
62 | |||
63 | /** | ||
64 | * @} | ||
65 | */ | ||
66 | |||
67 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
... | @@ -49,6 +49,8 @@ | ... | @@ -49,6 +49,8 @@ |
49 | /* USER CODE END Variables */ | 49 | /* USER CODE END Variables */ |
50 | osThreadId_t LEDTaskHandle; | 50 | osThreadId_t LEDTaskHandle; |
51 | osThreadId_t KeyScanTaskHandle; | 51 | osThreadId_t KeyScanTaskHandle; |
52 | osThreadId_t UartTaskHandle; | ||
53 | osThreadId_t CoreTaskHandle; | ||
52 | 54 | ||
53 | /* Private function prototypes -----------------------------------------------*/ | 55 | /* Private function prototypes -----------------------------------------------*/ |
54 | /* USER CODE BEGIN FunctionPrototypes */ | 56 | /* USER CODE BEGIN FunctionPrototypes */ |
... | @@ -57,6 +59,8 @@ osThreadId_t KeyScanTaskHandle; | ... | @@ -57,6 +59,8 @@ osThreadId_t KeyScanTaskHandle; |
57 | 59 | ||
58 | void StartLEDTask(void *argument); | 60 | void StartLEDTask(void *argument); |
59 | void StartKeyScanTask(void *argument); | 61 | void StartKeyScanTask(void *argument); |
62 | void StartUartTask(void *argument); | ||
63 | void StartCoreTask(void *argument); | ||
60 | 64 | ||
61 | void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ | 65 | void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ |
62 | 66 | ||
... | @@ -92,18 +96,34 @@ osKernelInitialize(); | ... | @@ -92,18 +96,34 @@ osKernelInitialize(); |
92 | const osThreadAttr_t LEDTask_attributes = { | 96 | const osThreadAttr_t LEDTask_attributes = { |
93 | .name = "LEDTask", | 97 | .name = "LEDTask", |
94 | .priority = (osPriority_t) osPriorityNormal, | 98 | .priority = (osPriority_t) osPriorityNormal, |
95 | .stack_size = 128 | 99 | .stack_size = 64 |
96 | }; | 100 | }; |
97 | LEDTaskHandle = osThreadNew(StartLEDTask, NULL, &LEDTask_attributes); | 101 | LEDTaskHandle = osThreadNew(StartLEDTask, NULL, &LEDTask_attributes); |
98 | 102 | ||
99 | /* definition and creation of KeyScanTask */ | 103 | /* definition and creation of KeyScanTask */ |
100 | const osThreadAttr_t KeyScanTask_attributes = { | 104 | const osThreadAttr_t KeyScanTask_attributes = { |
101 | .name = "KeyScanTask", | 105 | .name = "KeyScanTask", |
102 | .priority = (osPriority_t) osPriorityBelowNormal1, | 106 | .priority = (osPriority_t) osPriorityNormal, |
103 | .stack_size = 128 | 107 | .stack_size = 64 |
104 | }; | 108 | }; |
105 | KeyScanTaskHandle = osThreadNew(StartKeyScanTask, NULL, &KeyScanTask_attributes); | 109 | KeyScanTaskHandle = osThreadNew(StartKeyScanTask, NULL, &KeyScanTask_attributes); |
106 | 110 | ||
111 | /* definition and creation of UartTask */ | ||
112 | const osThreadAttr_t UartTask_attributes = { | ||
113 | .name = "UartTask", | ||
114 | .priority = (osPriority_t) osPriorityNormal, | ||
115 | .stack_size = 128 | ||
116 | }; | ||
117 | UartTaskHandle = osThreadNew(StartUartTask, NULL, &UartTask_attributes); | ||
118 | |||
119 | /* definition and creation of CoreTask */ | ||
120 | const osThreadAttr_t CoreTask_attributes = { | ||
121 | .name = "CoreTask", | ||
122 | .priority = (osPriority_t) osPriorityLow, | ||
123 | .stack_size = 128 | ||
124 | }; | ||
125 | CoreTaskHandle = osThreadNew(StartCoreTask, NULL, &CoreTask_attributes); | ||
126 | |||
107 | /* USER CODE BEGIN RTOS_THREADS */ | 127 | /* USER CODE BEGIN RTOS_THREADS */ |
108 | /* add threads, ... */ | 128 | /* add threads, ... */ |
109 | /* USER CODE END RTOS_THREADS */ | 129 | /* USER CODE END RTOS_THREADS */ |
... | @@ -153,6 +173,64 @@ void StartKeyScanTask(void *argument) | ... | @@ -153,6 +173,64 @@ void StartKeyScanTask(void *argument) |
153 | /* USER CODE END StartKeyScanTask */ | 173 | /* USER CODE END StartKeyScanTask */ |
154 | } | 174 | } |
155 | 175 | ||
176 | /* USER CODE BEGIN Header_StartUartTask */ | ||
177 | /** | ||
178 | * @brief Function implementing the UartTask thread. | ||
179 | * @param argument: Not used | ||
180 | * @retval None | ||
181 | */ | ||
182 | /* USER CODE END Header_StartUartTask */ | ||
183 | void StartUartTask(void *argument) | ||
184 | { | ||
185 | /* USER CODE BEGIN StartUartTask */ | ||
186 | uint8_t len; | ||
187 | uint16_t times; | ||
188 | /* Infinite loop */ | ||
189 | for(;;) | ||
190 | { | ||
191 | if (USART_RX_STA & 0x8000) | ||
192 | { | ||
193 | len = USART_RX_STA & 0x3fff; // 获取接收到的数据长度 | ||
194 | printf("\r\n接收到的数据:\r\n"); | ||
195 | HAL_UART_Transmit(&huart1, (uint8_t*)USART_RX_BUF, len, 1000); | ||
196 | while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET); | ||
197 | printf("\r\n\r\n"); // 插入换行 | ||
198 | USART_RX_STA=0; | ||
199 | } | ||
200 | else | ||
201 | { | ||
202 | times++; | ||
203 | if (times % 5000 == 0) | ||
204 | { | ||
205 | printf("\r\n串口实验\r\n"); | ||
206 | } | ||
207 | if (times % 200 == 0) | ||
208 | printf("请输入数据,以回车键结束\r\n"); | ||
209 | } | ||
210 | |||
211 | osDelay(1); | ||
212 | } | ||
213 | /* USER CODE END StartUartTask */ | ||
214 | } | ||
215 | |||
216 | /* USER CODE BEGIN Header_StartCoreTask */ | ||
217 | /** | ||
218 | * @brief Function implementing the CoreTask thread. | ||
219 | * @param argument: Not used | ||
220 | * @retval None | ||
221 | */ | ||
222 | /* USER CODE END Header_StartCoreTask */ | ||
223 | void StartCoreTask(void *argument) | ||
224 | { | ||
225 | /* USER CODE BEGIN StartCoreTask */ | ||
226 | /* Infinite loop */ | ||
227 | for(;;) | ||
228 | { | ||
229 | osDelay(1); | ||
230 | } | ||
231 | /* USER CODE END StartCoreTask */ | ||
232 | } | ||
233 | |||
156 | /* Private application code --------------------------------------------------*/ | 234 | /* Private application code --------------------------------------------------*/ |
157 | /* USER CODE BEGIN Application */ | 235 | /* USER CODE BEGIN Application */ |
158 | 236 | ... | ... |
... | @@ -57,8 +57,8 @@ void MX_GPIO_Init(void) | ... | @@ -57,8 +57,8 @@ void MX_GPIO_Init(void) |
57 | /*Configure GPIO pin : PtPin */ | 57 | /*Configure GPIO pin : PtPin */ |
58 | GPIO_InitStruct.Pin = BEEP_Pin; | 58 | GPIO_InitStruct.Pin = BEEP_Pin; |
59 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; | 59 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
60 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; | 60 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
61 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; | 61 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
62 | HAL_GPIO_Init(BEEP_GPIO_Port, &GPIO_InitStruct); | 62 | HAL_GPIO_Init(BEEP_GPIO_Port, &GPIO_InitStruct); |
63 | 63 | ||
64 | /*Configure GPIO pins : PBPin PBPin */ | 64 | /*Configure GPIO pins : PBPin PBPin */ | ... | ... |
... | @@ -21,6 +21,7 @@ | ... | @@ -21,6 +21,7 @@ |
21 | /* Includes ------------------------------------------------------------------*/ | 21 | /* Includes ------------------------------------------------------------------*/ |
22 | #include "main.h" | 22 | #include "main.h" |
23 | #include "cmsis_os.h" | 23 | #include "cmsis_os.h" |
24 | #include "usart.h" | ||
24 | #include "gpio.h" | 25 | #include "gpio.h" |
25 | 26 | ||
26 | /* Private includes ----------------------------------------------------------*/ | 27 | /* Private includes ----------------------------------------------------------*/ |
... | @@ -89,8 +90,11 @@ int main(void) | ... | @@ -89,8 +90,11 @@ int main(void) |
89 | 90 | ||
90 | /* Initialize all configured peripherals */ | 91 | /* Initialize all configured peripherals */ |
91 | MX_GPIO_Init(); | 92 | MX_GPIO_Init(); |
93 | MX_USART1_UART_Init(); | ||
94 | MX_USART2_UART_Init(); | ||
92 | /* USER CODE BEGIN 2 */ | 95 | /* USER CODE BEGIN 2 */ |
93 | 96 | // 使能UART1中断 | |
97 | HAL_UART_Receive_IT(&huart2, (uint8_t*)aRxBuffer, RXBUFFERSIZE); | ||
94 | /* USER CODE END 2 */ | 98 | /* USER CODE END 2 */ |
95 | 99 | ||
96 | /* Call init function for freertos objects (in freertos.c) */ | 100 | /* Call init function for freertos objects (in freertos.c) */ |
... | @@ -120,6 +124,7 @@ void SystemClock_Config(void) | ... | @@ -120,6 +124,7 @@ void SystemClock_Config(void) |
120 | { | 124 | { |
121 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; | 125 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
122 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; | 126 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
127 | RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; | ||
123 | 128 | ||
124 | /** Initializes the CPU, AHB and APB busses clocks | 129 | /** Initializes the CPU, AHB and APB busses clocks |
125 | */ | 130 | */ |
... | @@ -146,6 +151,12 @@ void SystemClock_Config(void) | ... | @@ -146,6 +151,12 @@ void SystemClock_Config(void) |
146 | { | 151 | { |
147 | Error_Handler(); | 152 | Error_Handler(); |
148 | } | 153 | } |
154 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1; | ||
155 | PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1; | ||
156 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) | ||
157 | { | ||
158 | Error_Handler(); | ||
159 | } | ||
149 | } | 160 | } |
150 | 161 | ||
151 | /* USER CODE BEGIN 4 */ | 162 | /* USER CODE BEGIN 4 */ | ... | ... |
... | @@ -58,6 +58,7 @@ | ... | @@ -58,6 +58,7 @@ |
58 | /* USER CODE END 0 */ | 58 | /* USER CODE END 0 */ |
59 | 59 | ||
60 | /* External variables --------------------------------------------------------*/ | 60 | /* External variables --------------------------------------------------------*/ |
61 | extern UART_HandleTypeDef huart2; | ||
61 | extern TIM_HandleTypeDef htim1; | 62 | extern TIM_HandleTypeDef htim1; |
62 | 63 | ||
63 | /* USER CODE BEGIN EV */ | 64 | /* USER CODE BEGIN EV */ |
... | @@ -116,6 +117,20 @@ void TIM1_BRK_UP_TRG_COM_IRQHandler(void) | ... | @@ -116,6 +117,20 @@ void TIM1_BRK_UP_TRG_COM_IRQHandler(void) |
116 | /* USER CODE END TIM1_BRK_UP_TRG_COM_IRQn 1 */ | 117 | /* USER CODE END TIM1_BRK_UP_TRG_COM_IRQn 1 */ |
117 | } | 118 | } |
118 | 119 | ||
120 | /** | ||
121 | * @brief This function handles USART2 global interrupt. | ||
122 | */ | ||
123 | void USART2_IRQHandler(void) | ||
124 | { | ||
125 | /* USER CODE BEGIN USART2_IRQn 0 */ | ||
126 | |||
127 | /* USER CODE END USART2_IRQn 0 */ | ||
128 | HAL_UART_IRQHandler(&huart2); | ||
129 | /* USER CODE BEGIN USART2_IRQn 1 */ | ||
130 | |||
131 | /* USER CODE END USART2_IRQn 1 */ | ||
132 | } | ||
133 | |||
119 | /* USER CODE BEGIN 1 */ | 134 | /* USER CODE BEGIN 1 */ |
120 | 135 | ||
121 | /* USER CODE END 1 */ | 136 | /* USER CODE END 1 */ | ... | ... |
Core/Src/usart.c
0 → 100644
1 | /** | ||
2 | ****************************************************************************** | ||
3 | * File Name : USART.c | ||
4 | * Description : This file provides code for the configuration | ||
5 | * of the USART instances. | ||
6 | ****************************************************************************** | ||
7 | * @attention | ||
8 | * | ||
9 | * <h2><center>© Copyright (c) 2019 STMicroelectronics. | ||
10 | * All rights reserved.</center></h2> | ||
11 | * | ||
12 | * This software component is licensed by ST under Ultimate Liberty license | ||
13 | * SLA0044, the "License"; You may not use this file except in compliance with | ||
14 | * the License. You may obtain a copy of the License at: | ||
15 | * www.st.com/SLA0044 | ||
16 | * | ||
17 | ****************************************************************************** | ||
18 | */ | ||
19 | |||
20 | /* Includes ------------------------------------------------------------------*/ | ||
21 | #include "usart.h" | ||
22 | |||
23 | /* USER CODE BEGIN 0 */ | ||
24 | // 标准库需要的支持函数 | ||
25 | struct __FILE | ||
26 | { | ||
27 | int handle; | ||
28 | }; | ||
29 | |||
30 | FILE __stdout; | ||
31 | |||
32 | uint8_t USART_RX_BUF[USART_REC_LEN]; | ||
33 | |||
34 | uint16_t USART_RX_STA = 0; | ||
35 | |||
36 | uint8_t aRxBuffer[RXBUFFERSIZE]; | ||
37 | |||
38 | /* USER CODE END 0 */ | ||
39 | |||
40 | UART_HandleTypeDef huart1; | ||
41 | UART_HandleTypeDef huart2; | ||
42 | |||
43 | /* USART1 init function */ | ||
44 | |||
45 | void MX_USART1_UART_Init(void) | ||
46 | { | ||
47 | |||
48 | huart1.Instance = USART1; | ||
49 | huart1.Init.BaudRate = 115200; | ||
50 | huart1.Init.WordLength = UART_WORDLENGTH_8B; | ||
51 | huart1.Init.StopBits = UART_STOPBITS_1; | ||
52 | huart1.Init.Parity = UART_PARITY_NONE; | ||
53 | huart1.Init.Mode = UART_MODE_TX_RX; | ||
54 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; | ||
55 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; | ||
56 | huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; | ||
57 | huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; | ||
58 | if (HAL_UART_Init(&huart1) != HAL_OK) | ||
59 | { | ||
60 | Error_Handler(); | ||
61 | } | ||
62 | |||
63 | } | ||
64 | /* USART2 init function */ | ||
65 | |||
66 | void MX_USART2_UART_Init(void) | ||
67 | { | ||
68 | |||
69 | huart2.Instance = USART2; | ||
70 | huart2.Init.BaudRate = 115200; | ||
71 | huart2.Init.WordLength = UART_WORDLENGTH_8B; | ||
72 | huart2.Init.StopBits = UART_STOPBITS_1; | ||
73 | huart2.Init.Parity = UART_PARITY_NONE; | ||
74 | huart2.Init.Mode = UART_MODE_TX_RX; | ||
75 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; | ||
76 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; | ||
77 | huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; | ||
78 | huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; | ||
79 | if (HAL_UART_Init(&huart2) != HAL_OK) | ||
80 | { | ||
81 | Error_Handler(); | ||
82 | } | ||
83 | |||
84 | } | ||
85 | |||
86 | void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) | ||
87 | { | ||
88 | |||
89 | GPIO_InitTypeDef GPIO_InitStruct = {0}; | ||
90 | if(uartHandle->Instance==USART1) | ||
91 | { | ||
92 | /* USER CODE BEGIN USART1_MspInit 0 */ | ||
93 | |||
94 | /* USER CODE END USART1_MspInit 0 */ | ||
95 | /* USART1 clock enable */ | ||
96 | __HAL_RCC_USART1_CLK_ENABLE(); | ||
97 | |||
98 | __HAL_RCC_GPIOA_CLK_ENABLE(); | ||
99 | /**USART1 GPIO Configuration | ||
100 | PA9 ------> USART1_TX | ||
101 | PA10 ------> USART1_RX | ||
102 | */ | ||
103 | GPIO_InitStruct.Pin = DEBUG_TX_Pin|DEBUG_RX_Pin; | ||
104 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; | ||
105 | GPIO_InitStruct.Pull = GPIO_PULLUP; | ||
106 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; | ||
107 | GPIO_InitStruct.Alternate = GPIO_AF1_USART1; | ||
108 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | ||
109 | |||
110 | /* USER CODE BEGIN USART1_MspInit 1 */ | ||
111 | |||
112 | /* USER CODE END USART1_MspInit 1 */ | ||
113 | } | ||
114 | else if(uartHandle->Instance==USART2) | ||
115 | { | ||
116 | /* USER CODE BEGIN USART2_MspInit 0 */ | ||
117 | |||
118 | /* USER CODE END USART2_MspInit 0 */ | ||
119 | /* USART2 clock enable */ | ||
120 | __HAL_RCC_USART2_CLK_ENABLE(); | ||
121 | |||
122 | __HAL_RCC_GPIOA_CLK_ENABLE(); | ||
123 | /**USART2 GPIO Configuration | ||
124 | PA2 ------> USART2_TX | ||
125 | PA3 ------> USART2_RX | ||
126 | */ | ||
127 | GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; | ||
128 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; | ||
129 | GPIO_InitStruct.Pull = GPIO_NOPULL; | ||
130 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; | ||
131 | GPIO_InitStruct.Alternate = GPIO_AF1_USART2; | ||
132 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | ||
133 | |||
134 | /* USART2 interrupt Init */ | ||
135 | HAL_NVIC_SetPriority(USART2_IRQn, 3, 0); | ||
136 | HAL_NVIC_EnableIRQ(USART2_IRQn); | ||
137 | /* USER CODE BEGIN USART2_MspInit 1 */ | ||
138 | |||
139 | /* USER CODE END USART2_MspInit 1 */ | ||
140 | } | ||
141 | } | ||
142 | |||
143 | void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) | ||
144 | { | ||
145 | |||
146 | if(uartHandle->Instance==USART1) | ||
147 | { | ||
148 | /* USER CODE BEGIN USART1_MspDeInit 0 */ | ||
149 | |||
150 | /* USER CODE END USART1_MspDeInit 0 */ | ||
151 | /* Peripheral clock disable */ | ||
152 | __HAL_RCC_USART1_CLK_DISABLE(); | ||
153 | |||
154 | /**USART1 GPIO Configuration | ||
155 | PA9 ------> USART1_TX | ||
156 | PA10 ------> USART1_RX | ||
157 | */ | ||
158 | HAL_GPIO_DeInit(GPIOA, DEBUG_TX_Pin|DEBUG_RX_Pin); | ||
159 | |||
160 | /* USER CODE BEGIN USART1_MspDeInit 1 */ | ||
161 | |||
162 | /* USER CODE END USART1_MspDeInit 1 */ | ||
163 | } | ||
164 | else if(uartHandle->Instance==USART2) | ||
165 | { | ||
166 | /* USER CODE BEGIN USART2_MspDeInit 0 */ | ||
167 | |||
168 | /* USER CODE END USART2_MspDeInit 0 */ | ||
169 | /* Peripheral clock disable */ | ||
170 | __HAL_RCC_USART2_CLK_DISABLE(); | ||
171 | |||
172 | /**USART2 GPIO Configuration | ||
173 | PA2 ------> USART2_TX | ||
174 | PA3 ------> USART2_RX | ||
175 | */ | ||
176 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); | ||
177 | |||
178 | /* USART2 interrupt Deinit */ | ||
179 | HAL_NVIC_DisableIRQ(USART2_IRQn); | ||
180 | /* USER CODE BEGIN USART2_MspDeInit 1 */ | ||
181 | |||
182 | /* USER CODE END USART2_MspDeInit 1 */ | ||
183 | } | ||
184 | } | ||
185 | |||
186 | /* USER CODE BEGIN 1 */ | ||
187 | /** | ||
188 | * 定义_sys_exit以避免使用半主机模式 | ||
189 | */ | ||
190 | void _sys_exit(int x) | ||
191 | { | ||
192 | x = x; | ||
193 | } | ||
194 | |||
195 | /** | ||
196 | * 重定义fputc函数 | ||
197 | */ | ||
198 | int fputc(int ch, FILE *f) | ||
199 | { | ||
200 | while ((USART1->ISR & 0X40) == 0); // 循环发送 | ||
201 | USART1->RDR = (uint8_t) ch; | ||
202 | return ch; | ||
203 | } | ||
204 | |||
205 | /** | ||
206 | * 串口接收回调函数 | ||
207 | */ | ||
208 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) | ||
209 | { | ||
210 | if (huart->Instance == USART2) // 串口1 | ||
211 | { | ||
212 | if ((USART_RX_STA & 0x8000) == 0) // 接收未完�? | ||
213 | { | ||
214 | if (USART_RX_STA & 0x4000) // 接收到了0x0D | ||
215 | { | ||
216 | if (aRxBuffer[0] != 0x0a) | ||
217 | USART_RX_STA = 0; // 接收错误,重新接�? | ||
218 | else | ||
219 | USART_RX_STA |= 0x8000; // 接收完成 | ||
220 | } | ||
221 | else // 还没接收�?0x0D | ||
222 | { | ||
223 | if (aRxBuffer[0] == 0x0d) | ||
224 | { | ||
225 | USART_RX_STA |= 0x4000; | ||
226 | } | ||
227 | else | ||
228 | { | ||
229 | USART_RX_BUF[USART_RX_STA & 0X3FFF] = aRxBuffer[0] ; | ||
230 | USART_RX_STA++; | ||
231 | if (USART_RX_STA > (USART_REC_LEN-1)) | ||
232 | { | ||
233 | USART_RX_STA = 0; // 接收错误,重新开始接�? | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | /* USER CODE END 1 */ | ||
241 | |||
242 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
This diff is collapsed.
Click to expand it.
This diff is collapsed.
Click to expand it.
This diff could not be displayed because it is too large.
This diff is collapsed.
Click to expand it.
1 | #MicroXplorer Configuration settings - do not modify | 1 | #MicroXplorer Configuration settings - do not modify |
2 | FREERTOS.FootprintOK=true | 2 | FREERTOS.FootprintOK=true |
3 | FREERTOS.IPParameters=Tasks01,FootprintOK,configTOTAL_HEAP_SIZE | 3 | FREERTOS.IPParameters=Tasks01,FootprintOK,configTOTAL_HEAP_SIZE |
4 | FREERTOS.Tasks01=LEDTask,24,128,StartLEDTask,Default,NULL,Dynamic,NULL,NULL;KeyScanTask,17,128,StartKeyScanTask,Default,NULL,Dynamic,NULL,NULL | 4 | FREERTOS.Tasks01=LEDTask,24,128,StartLEDTask,Default,NULL,Dynamic,NULL,NULL;KeyScanTask,24,128,StartKeyScanTask,Default,NULL,Dynamic,NULL,NULL;UartTask,24,128,StartUartTask,Default,NULL,Dynamic,NULL,NULL;CoreTask,8,128,StartCoreTask,Default,NULL,Dynamic,NULL,NULL |
5 | FREERTOS.configTOTAL_HEAP_SIZE=3072 | 5 | FREERTOS.configTOTAL_HEAP_SIZE=2560 |
6 | File.Version=6 | 6 | File.Version=6 |
7 | KeepUserPlacement=false | 7 | KeepUserPlacement=false |
8 | Mcu.Family=STM32F0 | 8 | Mcu.Family=STM32F0 |
... | @@ -10,22 +10,28 @@ Mcu.IP0=FREERTOS | ... | @@ -10,22 +10,28 @@ Mcu.IP0=FREERTOS |
10 | Mcu.IP1=NVIC | 10 | Mcu.IP1=NVIC |
11 | Mcu.IP2=RCC | 11 | Mcu.IP2=RCC |
12 | Mcu.IP3=SYS | 12 | Mcu.IP3=SYS |
13 | Mcu.IPNb=4 | 13 | Mcu.IP4=USART1 |
14 | Mcu.IP5=USART2 | ||
15 | Mcu.IPNb=6 | ||
14 | Mcu.Name=STM32F030C8Tx | 16 | Mcu.Name=STM32F030C8Tx |
15 | Mcu.Package=LQFP48 | 17 | Mcu.Package=LQFP48 |
16 | Mcu.Pin0=PC14-OSC32_IN | 18 | Mcu.Pin0=PC14-OSC32_IN |
17 | Mcu.Pin1=PC15-OSC32_OUT | 19 | Mcu.Pin1=PC15-OSC32_OUT |
18 | Mcu.Pin10=VP_FREERTOS_VS_CMSIS_V2 | 20 | Mcu.Pin10=PA14 |
19 | Mcu.Pin11=VP_SYS_VS_tim1 | 21 | Mcu.Pin11=PB6 |
22 | Mcu.Pin12=PB7 | ||
23 | Mcu.Pin13=PB9 | ||
24 | Mcu.Pin14=VP_FREERTOS_VS_CMSIS_V2 | ||
25 | Mcu.Pin15=VP_SYS_VS_tim1 | ||
20 | Mcu.Pin2=PF0-OSC_IN | 26 | Mcu.Pin2=PF0-OSC_IN |
21 | Mcu.Pin3=PF1-OSC_OUT | 27 | Mcu.Pin3=PF1-OSC_OUT |
22 | Mcu.Pin4=PA12 | 28 | Mcu.Pin4=PA2 |
23 | Mcu.Pin5=PA13 | 29 | Mcu.Pin5=PA3 |
24 | Mcu.Pin6=PA14 | 30 | Mcu.Pin6=PA9 |
25 | Mcu.Pin7=PB6 | 31 | Mcu.Pin7=PA10 |
26 | Mcu.Pin8=PB7 | 32 | Mcu.Pin8=PA12 |
27 | Mcu.Pin9=PB9 | 33 | Mcu.Pin9=PA13 |
28 | Mcu.PinsNb=12 | 34 | Mcu.PinsNb=16 |
29 | Mcu.ThirdPartyNb=0 | 35 | Mcu.ThirdPartyNb=0 |
30 | Mcu.UserConstants= | 36 | Mcu.UserConstants= |
31 | Mcu.UserName=STM32F030C8Tx | 37 | Mcu.UserName=STM32F030C8Tx |
... | @@ -40,16 +46,31 @@ NVIC.SysTick_IRQn=true\:3\:0\:false\:false\:false\:true\:false\:false | ... | @@ -40,16 +46,31 @@ NVIC.SysTick_IRQn=true\:3\:0\:false\:false\:false\:true\:false\:false |
40 | NVIC.TIM1_BRK_UP_TRG_COM_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true | 46 | NVIC.TIM1_BRK_UP_TRG_COM_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true |
41 | NVIC.TimeBase=TIM1_BRK_UP_TRG_COM_IRQn | 47 | NVIC.TimeBase=TIM1_BRK_UP_TRG_COM_IRQn |
42 | NVIC.TimeBaseIP=TIM1 | 48 | NVIC.TimeBaseIP=TIM1 |
43 | PA12.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label | 49 | NVIC.USART2_IRQn=true\:3\:0\:false\:false\:true\:true\:true\:true |
50 | PA10.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label | ||
51 | PA10.GPIO_Label=DEBUG_RX | ||
52 | PA10.GPIO_PuPd=GPIO_PULLUP | ||
53 | PA10.GPIO_Speed=GPIO_SPEED_FREQ_MEDIUM | ||
54 | PA10.Mode=Asynchronous | ||
55 | PA10.Signal=USART1_RX | ||
56 | PA12.GPIOParameters=GPIO_Label | ||
44 | PA12.GPIO_Label=BEEP | 57 | PA12.GPIO_Label=BEEP |
45 | PA12.GPIO_PuPd=GPIO_PULLDOWN | ||
46 | PA12.GPIO_Speed=GPIO_SPEED_FREQ_HIGH | ||
47 | PA12.Locked=true | 58 | PA12.Locked=true |
48 | PA12.Signal=GPIO_Output | 59 | PA12.Signal=GPIO_Output |
49 | PA13.Mode=Serial_Wire | 60 | PA13.Mode=Serial_Wire |
50 | PA13.Signal=SYS_SWDIO | 61 | PA13.Signal=SYS_SWDIO |
51 | PA14.Mode=Serial_Wire | 62 | PA14.Mode=Serial_Wire |
52 | PA14.Signal=SYS_SWCLK | 63 | PA14.Signal=SYS_SWCLK |
64 | PA2.Mode=Asynchronous | ||
65 | PA2.Signal=USART2_TX | ||
66 | PA3.Mode=Asynchronous | ||
67 | PA3.Signal=USART2_RX | ||
68 | PA9.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label | ||
69 | PA9.GPIO_Label=DEBUG_TX | ||
70 | PA9.GPIO_PuPd=GPIO_PULLUP | ||
71 | PA9.GPIO_Speed=GPIO_SPEED_FREQ_MEDIUM | ||
72 | PA9.Mode=Asynchronous | ||
73 | PA9.Signal=USART1_TX | ||
53 | PB6.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label | 74 | PB6.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label |
54 | PB6.GPIO_Label=LED2 | 75 | PB6.GPIO_Label=LED2 |
55 | PB6.GPIO_PuPd=GPIO_PULLDOWN | 76 | PB6.GPIO_PuPd=GPIO_PULLDOWN |
... | @@ -111,7 +132,7 @@ ProjectManager.StackSize=0x400 | ... | @@ -111,7 +132,7 @@ ProjectManager.StackSize=0x400 |
111 | ProjectManager.TargetToolchain=STM32CubeIDE | 132 | ProjectManager.TargetToolchain=STM32CubeIDE |
112 | ProjectManager.ToolChainLocation= | 133 | ProjectManager.ToolChainLocation= |
113 | ProjectManager.UnderRoot=true | 134 | ProjectManager.UnderRoot=true |
114 | ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,1-MX_GPIO_Init-GPIO-false-HAL-true | 135 | ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,1-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART1_UART_Init-USART1-false-HAL-true |
115 | RCC.AHBFreq_Value=48000000 | 136 | RCC.AHBFreq_Value=48000000 |
116 | RCC.APB1Freq_Value=48000000 | 137 | RCC.APB1Freq_Value=48000000 |
117 | RCC.APB1TimFreq_Value=48000000 | 138 | RCC.APB1TimFreq_Value=48000000 |
... | @@ -127,6 +148,12 @@ RCC.SYSCLKFreq_VALUE=48000000 | ... | @@ -127,6 +148,12 @@ RCC.SYSCLKFreq_VALUE=48000000 |
127 | RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK | 148 | RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK |
128 | RCC.TimSysFreq_Value=48000000 | 149 | RCC.TimSysFreq_Value=48000000 |
129 | RCC.USART1Freq_Value=48000000 | 150 | RCC.USART1Freq_Value=48000000 |
151 | USART1.BaudRate=115200 | ||
152 | USART1.IPParameters=VirtualMode-Asynchronous,BaudRate | ||
153 | USART1.VirtualMode-Asynchronous=VM_ASYNC | ||
154 | USART2.BaudRate=115200 | ||
155 | USART2.IPParameters=VirtualMode-Asynchronous,BaudRate | ||
156 | USART2.VirtualMode-Asynchronous=VM_ASYNC | ||
130 | VP_FREERTOS_VS_CMSIS_V2.Mode=CMSIS_V2 | 157 | VP_FREERTOS_VS_CMSIS_V2.Mode=CMSIS_V2 |
131 | VP_FREERTOS_VS_CMSIS_V2.Signal=FREERTOS_VS_CMSIS_V2 | 158 | VP_FREERTOS_VS_CMSIS_V2.Signal=FREERTOS_VS_CMSIS_V2 |
132 | VP_SYS_VS_tim1.Mode=TIM1 | 159 | VP_SYS_VS_tim1.Mode=TIM1 | ... | ... |
-
Please register or sign in to post a comment