fc8c2e27 by 张亚玄

add key time state & tim & new beep

1 parent 499ce9a5
...@@ -17,5 +17,6 @@ ...@@ -17,5 +17,6 @@
17 #define BEEP_Set() HAL_GPIO_WritePin(BEEP_GPIO_Port, BEEP_Pin, GPIO_PIN_SET) 17 #define BEEP_Set() HAL_GPIO_WritePin(BEEP_GPIO_Port, BEEP_Pin, GPIO_PIN_SET)
18 18
19 extern void BEEP_call(); 19 extern void BEEP_call();
20 extern void BEEP_close();
20 21
21 #endif /* INC_HARDWARE_BEEP_H_ */ 22 #endif /* INC_HARDWARE_BEEP_H_ */
......
...@@ -22,6 +22,17 @@ ...@@ -22,6 +22,17 @@
22 #define KEY2_UP 0 22 #define KEY2_UP 0
23 #define KEY2_DOWN !KEY2_UP 23 #define KEY2_DOWN !KEY2_UP
24 24
25 #define KEY1 1
26 #define KEY2 2
27
28 #define KEY_CONTINUE_TRIGGLE_MODE 1
29 #define KEY_UNCONTINUE_TRIGGLE_MODE 0
30
31 #define KEY_PRESS_0_5S 1
32 #define KEY_PRESS_5_10S 2
33 #define KEY_PRESS_10_15S 3
34 #define KEY_PRESS_UP_15S 4
35
25 36
26 /* Exported functions --------------------------------------------------------*/ 37 /* Exported functions --------------------------------------------------------*/
27 extern uint8_t KEY_Scan(uint8_t mode); 38 extern uint8_t KEY_Scan(uint8_t mode);
......
1 #ifndef AT_PRINTF_H
2 #define AT_PRINTF_H
3
4 #ifdef __cplusplus
5 extern "C"
6 {
7 #endif
8
9 int mprintf(const char *format, ...);
10 int msprintf(char *out, const char *format, ...);
11
12 #ifdef __cplusplus
13 }
14 #endif
15
16 #endif /* AT_PRINTF_H */
...@@ -51,6 +51,8 @@ void NMI_Handler(void); ...@@ -51,6 +51,8 @@ void NMI_Handler(void);
51 void HardFault_Handler(void); 51 void HardFault_Handler(void);
52 void EXTI4_15_IRQHandler(void); 52 void EXTI4_15_IRQHandler(void);
53 void TIM1_BRK_UP_TRG_COM_IRQHandler(void); 53 void TIM1_BRK_UP_TRG_COM_IRQHandler(void);
54 void TIM3_IRQHandler(void);
55 void TIM6_IRQHandler(void);
54 void USART2_IRQHandler(void); 56 void USART2_IRQHandler(void);
55 /* USER CODE BEGIN EFP */ 57 /* USER CODE BEGIN EFP */
56 58
......
1 /**
2 ******************************************************************************
3 * File Name : TIM.h
4 * Description : This file provides code for the configuration
5 * of the TIM instances.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; 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 __tim_H
21 #define __tim_H
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 /* Includes ------------------------------------------------------------------*/
27 #include "main.h"
28
29 /* USER CODE BEGIN Includes */
30
31 /* USER CODE END Includes */
32
33 extern TIM_HandleTypeDef htim3;
34 extern TIM_HandleTypeDef htim6;
35
36 /* USER CODE BEGIN Private defines */
37
38 /* USER CODE END Private defines */
39
40 void MX_TIM3_Init(void);
41 void MX_TIM6_Init(void);
42
43 /* USER CODE BEGIN Prototypes */
44
45 /* USER CODE END Prototypes */
46
47 #ifdef __cplusplus
48 }
49 #endif
50 #endif /*__ tim_H */
51
52 /**
53 * @}
54 */
55
56 /**
57 * @}
58 */
59
60 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
27 #include "main.h" 27 #include "main.h"
28 28
29 /* USER CODE BEGIN Includes */ 29 /* USER CODE BEGIN Includes */
30 #include <stdio.h> 30 #include <mprintf.h>
31 /* USER CODE END Includes */ 31 /* USER CODE END Includes */
32 32
33 extern UART_HandleTypeDef huart1; 33 extern UART_HandleTypeDef huart1;
...@@ -41,6 +41,8 @@ extern uint16_t USART_RX_STA; ...@@ -41,6 +41,8 @@ extern uint16_t USART_RX_STA;
41 41
42 #define RXBUFFERSIZE 1 // 缓存大小 42 #define RXBUFFERSIZE 1 // 缓存大小
43 extern uint8_t aRxBuffer[RXBUFFERSIZE]; // HAL库USART接收Buffer 43 extern uint8_t aRxBuffer[RXBUFFERSIZE]; // HAL库USART接收Buffer
44
45 #define DEBUG_PRINT(str, len) HAL_UART_Transmit(&huart1, (uint8_t*)str, len, 1000);
44 /* USER CODE END Private defines */ 46 /* USER CODE END Private defines */
45 47
46 void MX_USART1_UART_Init(void); 48 void MX_USART1_UART_Init(void);
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
7 7
8 /* Includes ------------------------------------------------------------------*/ 8 /* Includes ------------------------------------------------------------------*/
9 #include "beep.h" 9 #include "beep.h"
10 #include "cmsis_os.h" 10 #include "tim.h"
11 11
12 /** 12 /**
13 * crazy beep 13 * crazy beep
...@@ -15,6 +15,13 @@ ...@@ -15,6 +15,13 @@
15 void BEEP_call() 15 void BEEP_call()
16 { 16 {
17 BEEP_Set(); 17 BEEP_Set();
18 osDelay(300); 18 HAL_TIM_Base_Start_IT(&htim6);
19 }
20
21 /**
22 * close crazy beep
23 */
24 void BEEP_close()
25 {
19 BEEP_Reset(); 26 BEEP_Reset();
20 } 27 }
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
12 12
13 /** 13 /**
14 * 按键处理函数 14 * 按键处理函数
15 * @param mode 0 - 连续触发 1 - 不连续触发 15 * @param mode 0 - 不连续触发 1 - 连续触发
16 */ 16 */
17 uint8_t KEY_Scan(uint8_t mode) 17 uint8_t KEY_Scan(uint8_t mode)
18 { 18 {
...@@ -21,16 +21,20 @@ uint8_t KEY_Scan(uint8_t mode) ...@@ -21,16 +21,20 @@ uint8_t KEY_Scan(uint8_t mode)
21 { 21 {
22 key_up = 1; 22 key_up = 1;
23 } 23 }
24 if (key_up && KEY2_read() == KEY2_DOWN) 24 if (key_up && (KEY1_read() == KEY1_DOWN || KEY2_read() == KEY2_DOWN))
25 { 25 {
26 osDelay(10); 26 osDelay(10);
27 key_up = 0; 27 key_up = 0;
28 if (KEY1_read() == KEY1_DOWN)
29 {
30 return KEY1;
31 }
28 if (KEY2_read() == KEY2_DOWN) 32 if (KEY2_read() == KEY2_DOWN)
29 { 33 {
30 return 1; 34 return KEY2;
31 } 35 }
32 } 36 }
33 else if (KEY2_read() == KEY2_UP) 37 else if (KEY1_read() == KEY1_UP && KEY2_read() == KEY2_UP)
34 { 38 {
35 key_up = 1; 39 key_up = 1;
36 } 40 }
...@@ -38,17 +42,17 @@ uint8_t KEY_Scan(uint8_t mode) ...@@ -38,17 +42,17 @@ uint8_t KEY_Scan(uint8_t mode)
38 return 0; 42 return 0;
39 } 43 }
40 44
41 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) 45 //void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
42 { 46 //{
43 switch (GPIO_Pin) { 47 // switch (GPIO_Pin) {
44 case GPIO_PIN_8: 48 // case GPIO_PIN_8:
45 osDelay(30); 49 // osDelay(30);
46 if (KEY1_read() == KEY1_DOWN) 50 // if (KEY1_read() == KEY1_DOWN)
47 { 51 // {
48 ledState++; 52 // //ledState++;
49 } 53 // }
50 break; 54 // break;
51 default: 55 // default:
52 break; 56 // break;
53 } 57 // }
54 } 58 //}
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
26 26
27 /* Private includes ----------------------------------------------------------*/ 27 /* Private includes ----------------------------------------------------------*/
28 /* USER CODE BEGIN Includes */ 28 /* USER CODE BEGIN Includes */
29 #include <stdio.h>
30 #include "led.h" 29 #include "led.h"
31 #include "beep.h" 30 #include "beep.h"
32 #include "key.h" 31 #include "key.h"
...@@ -119,7 +118,7 @@ osKernelInitialize(); ...@@ -119,7 +118,7 @@ osKernelInitialize();
119 const osThreadAttr_t UartTask_attributes = { 118 const osThreadAttr_t UartTask_attributes = {
120 .name = "UartTask", 119 .name = "UartTask",
121 .priority = (osPriority_t) osPriorityNormal, 120 .priority = (osPriority_t) osPriorityNormal,
122 .stack_size = 128 121 .stack_size = 512
123 }; 122 };
124 UartTaskHandle = osThreadNew(StartUartTask, NULL, &UartTask_attributes); 123 UartTaskHandle = osThreadNew(StartUartTask, NULL, &UartTask_attributes);
125 124
...@@ -134,7 +133,7 @@ osKernelInitialize(); ...@@ -134,7 +133,7 @@ osKernelInitialize();
134 /* definition and creation of SensorTask */ 133 /* definition and creation of SensorTask */
135 const osThreadAttr_t SensorTask_attributes = { 134 const osThreadAttr_t SensorTask_attributes = {
136 .name = "SensorTask", 135 .name = "SensorTask",
137 .priority = (osPriority_t) osPriorityLow, 136 .priority = (osPriority_t) osPriorityNormal,
138 .stack_size = 128 137 .stack_size = 128
139 }; 138 };
140 SensorTaskHandle = osThreadNew(StartSensorTask, NULL, &SensorTask_attributes); 139 SensorTaskHandle = osThreadNew(StartSensorTask, NULL, &SensorTask_attributes);
...@@ -159,8 +158,8 @@ void StartLEDTask(void *argument) ...@@ -159,8 +158,8 @@ void StartLEDTask(void *argument)
159 /* Infinite loop */ 158 /* Infinite loop */
160 for(;;) 159 for(;;)
161 { 160 {
162 ledState = ledState % LED_TOTAL_STATE_NUM; 161 // ledState = ledState % LED_TOTAL_STATE_NUM;
163 LED_StateControl(ledState); 162 // LED_StateControl(ledState);
164 osDelay(1); 163 osDelay(1);
165 } 164 }
166 /* USER CODE END StartLEDTask */ 165 /* USER CODE END StartLEDTask */
...@@ -176,12 +175,60 @@ void StartLEDTask(void *argument) ...@@ -176,12 +175,60 @@ void StartLEDTask(void *argument)
176 void StartKeyScanTask(void *argument) 175 void StartKeyScanTask(void *argument)
177 { 176 {
178 /* USER CODE BEGIN StartKeyScanTask */ 177 /* USER CODE BEGIN StartKeyScanTask */
178 uint16_t count = 0;
179 uint8_t flag = 0;
179 /* Infinite loop */ 180 /* Infinite loop */
180 for(;;) 181 for(;;)
181 { 182 {
182 if(KEY_Scan(0)) { 183 /* Infinite loop */
183 BEEP_call(); 184 if(KEY1_read() == KEY1_DOWN)
185 {
186 osDelay(10);
187 if (KEY1_read() == KEY1_UP)
188 {
189 LED2_Reset();
190 count = 0;
191 flag = 0;
192 goto DONE;
193 }
194 if(count < 500)// 0 ~ 5 000 ms
195 {
196 flag = KEY_PRESS_0_5S;
197 if(count % 10 == 0)
198 {
199 HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
200 }
201 }
202 else if((count >= 500) && (count < 1000))// 5 000 ~ 10 000 ms
203 {
204 flag = KEY_PRESS_5_10S;
205 if(count % 30 == 0)
206 {
207 HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
208 }
209 }
210 else if((count >= 1000) && (count < 1500))// 10 000 ~ 15 000 ms
211 {
212 flag = KEY_PRESS_10_15S;
213 if(count % 50 == 0)
214 {
215 HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
216 }
217 }
218 else
219 {
220 flag = KEY_PRESS_UP_15S;
184 } 221 }
222 count++;
223 }
224 else
225 {
226 LED2_Reset();
227 count = 0;
228 flag = 0;
229 }
230
231 DONE:
185 osDelay(1); 232 osDelay(1);
186 } 233 }
187 /* USER CODE END StartKeyScanTask */ 234 /* USER CODE END StartKeyScanTask */
...@@ -205,10 +252,10 @@ void StartUartTask(void *argument) ...@@ -205,10 +252,10 @@ void StartUartTask(void *argument)
205 if (USART_RX_STA & 0x8000) 252 if (USART_RX_STA & 0x8000)
206 { 253 {
207 len = USART_RX_STA & 0x3fff; // 获取接收到的数据长度 254 len = USART_RX_STA & 0x3fff; // 获取接收到的数据长度
208 HAL_UART_Transmit(&huart1, (uint8_t*)"\r\nReceived:\r\n", sizeof("\r\nReceived:\r\n"), 1000); 255 mprintf("\r\nReceived:\r\n");
209 HAL_UART_Transmit(&huart1, (uint8_t*)USART_RX_BUF, len, 1000); 256 HAL_UART_Transmit(&huart1, (uint8_t*)USART_RX_BUF, len, 1000);
210 while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET); 257 while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET);
211 HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n\r\n", sizeof("\r\n\r\n"), 1000); 258 mprintf("\r\n\r\n");
212 USART_RX_STA=0; 259 USART_RX_STA=0;
213 } 260 }
214 else 261 else
...@@ -216,10 +263,12 @@ void StartUartTask(void *argument) ...@@ -216,10 +263,12 @@ void StartUartTask(void *argument)
216 times++; 263 times++;
217 if (times % 5000 == 0) 264 if (times % 5000 == 0)
218 { 265 {
219 HAL_UART_Transmit(&huart1, (uint8_t*)"\r\nUART Test\r\n", sizeof("\r\nUART Test\r\n"), 1000); 266 mprintf("\r\nUART Test\r\n");
220 } 267 }
221 if (times % 1000 == 0) 268 // if (times % 1000 == 0)
222 HAL_UART_Transmit(&huart1, (uint8_t*)"print char\r\n", sizeof("print char\r\n"), 1000); 269 // {
270 // mprintf("print char\r\n");
271 // }
223 } 272 }
224 273
225 osDelay(1); 274 osDelay(1);
...@@ -265,7 +314,6 @@ void StartSensorTask(void *argument) ...@@ -265,7 +314,6 @@ void StartSensorTask(void *argument)
265 314
266 /* Private application code --------------------------------------------------*/ 315 /* Private application code --------------------------------------------------*/
267 /* USER CODE BEGIN Application */ 316 /* USER CODE BEGIN Application */
268
269 /* USER CODE END Application */ 317 /* USER CODE END Application */
270 318
271 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 319 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
......
...@@ -21,12 +21,13 @@ ...@@ -21,12 +21,13 @@
21 /* Includes ------------------------------------------------------------------*/ 21 /* Includes ------------------------------------------------------------------*/
22 #include "main.h" 22 #include "main.h"
23 #include "cmsis_os.h" 23 #include "cmsis_os.h"
24 #include "tim.h"
24 #include "usart.h" 25 #include "usart.h"
25 #include "gpio.h" 26 #include "gpio.h"
26 27
27 /* Private includes ----------------------------------------------------------*/ 28 /* Private includes ----------------------------------------------------------*/
28 /* USER CODE BEGIN Includes */ 29 /* USER CODE BEGIN Includes */
29 30 #include "beep.h"
30 /* USER CODE END Includes */ 31 /* USER CODE END Includes */
31 32
32 /* Private typedef -----------------------------------------------------------*/ 33 /* Private typedef -----------------------------------------------------------*/
...@@ -92,7 +93,11 @@ int main(void) ...@@ -92,7 +93,11 @@ int main(void)
92 MX_GPIO_Init(); 93 MX_GPIO_Init();
93 MX_USART1_UART_Init(); 94 MX_USART1_UART_Init();
94 MX_USART2_UART_Init(); 95 MX_USART2_UART_Init();
96 MX_TIM3_Init();
97 MX_TIM6_Init();
95 /* USER CODE BEGIN 2 */ 98 /* USER CODE BEGIN 2 */
99 // 使能TIM3中断
100 HAL_TIM_Base_Start_IT(&htim3);
96 // 使能UART2中断 101 // 使能UART2中断
97 HAL_UART_Receive_IT(&huart2, (uint8_t*)aRxBuffer, RXBUFFERSIZE); 102 HAL_UART_Receive_IT(&huart2, (uint8_t*)aRxBuffer, RXBUFFERSIZE);
98 /* USER CODE END 2 */ 103 /* USER CODE END 2 */
...@@ -180,7 +185,17 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) ...@@ -180,7 +185,17 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
180 HAL_IncTick(); 185 HAL_IncTick();
181 } 186 }
182 /* USER CODE BEGIN Callback 1 */ 187 /* USER CODE BEGIN Callback 1 */
183 188 if(htim->Instance == TIM3)
189 {
190 mprintf("TIM3 elapsed\r\n");
191 BEEP_call();
192 }
193 if(htim->Instance == TIM6)
194 {
195 mprintf("TIM6 elapsed\r\n");
196 HAL_TIM_Base_Stop_IT(&htim6);
197 BEEP_close();
198 }
184 /* USER CODE END Callback 1 */ 199 /* USER CODE END Callback 1 */
185 } 200 }
186 201
......
1 /*
2 Copyright 2001, 2002 Georges Menie (www.menie.org)
3 stdarg version contributed by Christian Ettinger
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /*
21 putchar is the only external dependency for this file,
22 if you have a working putchar, leave it commented out.
23 If not, uncomment the define below and
24 replace outbyte(c) by your own function call.
25
26 #define putchar(c) outbyte(c)
27 */
28
29 #include <mprintf.h>
30 #include "stdarg.h"
31 #include "usart.h"
32
33 void mputc(char c);
34 void mputc(char c){
35 DEBUG_PRINT(&c,1);
36 }
37
38
39 static void printchar(char **str, int c)
40 {
41 extern void mputc(char c);
42
43 if (str) {
44 **str = c;
45 ++(*str);
46 }
47 else (void)mputc((char)c);
48 }
49
50 #define PAD_RIGHT 1
51 #define PAD_ZERO 2
52
53 static int prints(char **out, const char *string, int width, int pad)
54 {
55 register int pc = 0, padchar = ' ';
56
57 if (width > 0) {
58 register int len = 0;
59 register const char *ptr;
60 for (ptr = string; *ptr; ++ptr) ++len;
61 if (len >= width) width = 0;
62 else width -= len;
63 if (pad & PAD_ZERO) padchar = '0';
64 }
65 if (!(pad & PAD_RIGHT)) {
66 for ( ; width > 0; --width) {
67 printchar (out, padchar);
68 ++pc;
69 }
70 }
71 for ( ; *string ; ++string) {
72 printchar (out, *string);
73 ++pc;
74 }
75 for ( ; width > 0; --width) {
76 printchar (out, padchar);
77 ++pc;
78 }
79
80 return pc;
81 }
82
83 /* the following should be enough for 16 bit int */
84 #define PRINT_BUF_LEN 6
85
86 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
87 {
88 char print_buf[PRINT_BUF_LEN];
89 register char *s;
90 register int t, neg = 0, pc = 0;
91 register unsigned int u = i;
92
93 if (i == 0) {
94 print_buf[0] = '0';
95 print_buf[1] = '\0';
96 return prints (out, print_buf, width, pad);
97 }
98
99 if (sg && b == 10 && i < 0) {
100 neg = 1;
101 u = -i;
102 }
103
104 s = print_buf + PRINT_BUF_LEN-1;
105 *s = '\0';
106
107 while (u) {
108 t = u % b;
109 if( t >= 10 )
110 t += letbase - '0' - 10;
111 *--s = t + '0';
112 u /= b;
113 }
114
115 if (neg) {
116 if( width && (pad & PAD_ZERO) ) {
117 printchar (out, '-');
118 ++pc;
119 --width;
120 }
121 else {
122 *--s = '-';
123 }
124 }
125
126 return pc + prints (out, s, width, pad);
127 }
128
129 static int print(char **out, const char *format, va_list args )
130 {
131 register int width, pad;
132 register int pc = 0;
133 char scr[2];
134
135 for (; *format != 0; ++format) {
136 if (*format == '%') {
137 ++format;
138 width = pad = 0;
139 if (*format == '\0') break;
140 if (*format == '%') goto out;
141 if (*format == '-') {
142 ++format;
143 pad = PAD_RIGHT;
144 }
145 while (*format == '0') {
146 ++format;
147 pad |= PAD_ZERO;
148 }
149 for ( ; *format >= '0' && *format <= '9'; ++format) {
150 width *= 10;
151 width += *format - '0';
152 }
153 if( *format == 's' ) {
154 register char *s = (char *)va_arg( args, int );
155 pc += prints (out, s?s:"(null)", width, pad);
156 continue;
157 }
158 if( *format == 'd' ) {
159 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
160 continue;
161 }
162 if( *format == 'x' ) {
163 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
164 continue;
165 }
166 if( *format == 'X' ) {
167 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
168 continue;
169 }
170 if( *format == 'u' ) {
171 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
172 continue;
173 }
174 if( *format == 'c' ) {
175 /* char are converted to int then pushed on the stack */
176 scr[0] = (char)va_arg( args, int );
177 scr[1] = '\0';
178 pc += prints (out, scr, width, pad);
179 continue;
180 }
181 }
182 else {
183 out:
184 printchar (out, *format);
185 ++pc;
186 }
187 }
188 if (out) **out = '\0';
189 va_end( args );
190 return pc;
191 }
192
193 int mprintf(const char *format, ...)
194 {
195 va_list args;
196
197 va_start( args, format );
198 return print( 0, format, args );
199 }
200
201 int msprintf(char *out, const char *format, ...)
202 {
203 va_list args;
204
205 va_start( args, format );
206 return print( &out, format, args );
207 }
...@@ -59,6 +59,8 @@ ...@@ -59,6 +59,8 @@
59 /* USER CODE END 0 */ 59 /* USER CODE END 0 */
60 60
61 /* External variables --------------------------------------------------------*/ 61 /* External variables --------------------------------------------------------*/
62 extern TIM_HandleTypeDef htim3;
63 extern TIM_HandleTypeDef htim6;
62 extern UART_HandleTypeDef huart2; 64 extern UART_HandleTypeDef huart2;
63 extern TIM_HandleTypeDef htim1; 65 extern TIM_HandleTypeDef htim1;
64 66
...@@ -133,6 +135,34 @@ void TIM1_BRK_UP_TRG_COM_IRQHandler(void) ...@@ -133,6 +135,34 @@ void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
133 } 135 }
134 136
135 /** 137 /**
138 * @brief This function handles TIM3 global interrupt.
139 */
140 void TIM3_IRQHandler(void)
141 {
142 /* USER CODE BEGIN TIM3_IRQn 0 */
143
144 /* USER CODE END TIM3_IRQn 0 */
145 HAL_TIM_IRQHandler(&htim3);
146 /* USER CODE BEGIN TIM3_IRQn 1 */
147
148 /* USER CODE END TIM3_IRQn 1 */
149 }
150
151 /**
152 * @brief This function handles TIM6 global interrupt.
153 */
154 void TIM6_IRQHandler(void)
155 {
156 /* USER CODE BEGIN TIM6_IRQn 0 */
157
158 /* USER CODE END TIM6_IRQn 0 */
159 HAL_TIM_IRQHandler(&htim6);
160 /* USER CODE BEGIN TIM6_IRQn 1 */
161
162 /* USER CODE END TIM6_IRQn 1 */
163 }
164
165 /**
136 * @brief This function handles USART2 global interrupt. 166 * @brief This function handles USART2 global interrupt.
137 */ 167 */
138 void USART2_IRQHandler(void) 168 void USART2_IRQHandler(void)
......
1 /**
2 ******************************************************************************
3 * File Name : TIM.c
4 * Description : This file provides code for the configuration
5 * of the TIM instances.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; 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 "tim.h"
22
23 /* USER CODE BEGIN 0 */
24 #include "beep.h"
25 /* USER CODE END 0 */
26
27 TIM_HandleTypeDef htim3;
28 TIM_HandleTypeDef htim6;
29
30 /* TIM3 init function */
31 void MX_TIM3_Init(void)
32 {
33 TIM_MasterConfigTypeDef sMasterConfig = {0};
34 TIM_OC_InitTypeDef sConfigOC = {0};
35
36 htim3.Instance = TIM3;
37 htim3.Init.Prescaler = 23999;
38 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
39 htim3.Init.Period = 1999;
40 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
41 htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
42 if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
43 {
44 Error_Handler();
45 }
46 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
47 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
48 if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
49 {
50 Error_Handler();
51 }
52 sConfigOC.OCMode = TIM_OCMODE_TIMING;
53 sConfigOC.Pulse = 0;
54 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
55 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
56 if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
57 {
58 Error_Handler();
59 }
60
61 }
62 /* TIM6 init function */
63 void MX_TIM6_Init(void)
64 {
65
66 htim6.Instance = TIM6;
67 htim6.Init.Prescaler = 47999;
68 htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
69 htim6.Init.Period = 299;
70 htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
71 if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
72 {
73 Error_Handler();
74 }
75
76 }
77
78 void HAL_TIM_OC_MspInit(TIM_HandleTypeDef* tim_ocHandle)
79 {
80
81 if(tim_ocHandle->Instance==TIM3)
82 {
83 /* USER CODE BEGIN TIM3_MspInit 0 */
84
85 /* USER CODE END TIM3_MspInit 0 */
86 /* TIM3 clock enable */
87 __HAL_RCC_TIM3_CLK_ENABLE();
88
89 /* TIM3 interrupt Init */
90 HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
91 HAL_NVIC_EnableIRQ(TIM3_IRQn);
92 /* USER CODE BEGIN TIM3_MspInit 1 */
93
94 /* USER CODE END TIM3_MspInit 1 */
95 }
96 }
97
98 void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
99 {
100
101 if(tim_baseHandle->Instance==TIM6)
102 {
103 /* USER CODE BEGIN TIM6_MspInit 0 */
104
105 /* USER CODE END TIM6_MspInit 0 */
106 /* TIM6 clock enable */
107 __HAL_RCC_TIM6_CLK_ENABLE();
108
109 /* TIM6 interrupt Init */
110 HAL_NVIC_SetPriority(TIM6_IRQn, 3, 0);
111 HAL_NVIC_EnableIRQ(TIM6_IRQn);
112 /* USER CODE BEGIN TIM6_MspInit 1 */
113
114 /* USER CODE END TIM6_MspInit 1 */
115 }
116 }
117
118 void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef* tim_ocHandle)
119 {
120
121 if(tim_ocHandle->Instance==TIM3)
122 {
123 /* USER CODE BEGIN TIM3_MspDeInit 0 */
124
125 /* USER CODE END TIM3_MspDeInit 0 */
126 /* Peripheral clock disable */
127 __HAL_RCC_TIM3_CLK_DISABLE();
128
129 /* TIM3 interrupt Deinit */
130 HAL_NVIC_DisableIRQ(TIM3_IRQn);
131 /* USER CODE BEGIN TIM3_MspDeInit 1 */
132
133 /* USER CODE END TIM3_MspDeInit 1 */
134 }
135 }
136
137 void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
138 {
139
140 if(tim_baseHandle->Instance==TIM6)
141 {
142 /* USER CODE BEGIN TIM6_MspDeInit 0 */
143
144 /* USER CODE END TIM6_MspDeInit 0 */
145 /* Peripheral clock disable */
146 __HAL_RCC_TIM6_CLK_DISABLE();
147
148 /* TIM6 interrupt Deinit */
149 HAL_NVIC_DisableIRQ(TIM6_IRQn);
150 /* USER CODE BEGIN TIM6_MspDeInit 1 */
151
152 /* USER CODE END TIM6_MspDeInit 1 */
153 }
154 }
155
156 /* USER CODE BEGIN 1 */
157
158 /* USER CODE END 1 */
159
160 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
...@@ -21,14 +21,6 @@ ...@@ -21,14 +21,6 @@
21 #include "usart.h" 21 #include "usart.h"
22 22
23 /* USER CODE BEGIN 0 */ 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]; 24 uint8_t USART_RX_BUF[USART_REC_LEN];
33 25
34 uint16_t USART_RX_STA = 0; 26 uint16_t USART_RX_STA = 0;
...@@ -185,40 +177,22 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) ...@@ -185,40 +177,22 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
185 177
186 /* USER CODE BEGIN 1 */ 178 /* USER CODE BEGIN 1 */
187 /** 179 /**
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 * 串口接收回调函数 180 * 串口接收回调函数
207 */ 181 */
208 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 182 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
209 { 183 {
210 if (huart->Instance == USART2) // 串口1 184 if (huart->Instance == USART2) // 串口1
211 { 185 {
212 if ((USART_RX_STA & 0x8000) == 0) // 接收未完???? 186 if ((USART_RX_STA & 0x8000) == 0) // 接收未完?????
213 { 187 {
214 if (USART_RX_STA & 0x4000) // 接收到了0x0D 188 if (USART_RX_STA & 0x4000) // 接收到了0x0D
215 { 189 {
216 if (aRxBuffer[0] != 0x0a) 190 if (aRxBuffer[0] != 0x0a)
217 USART_RX_STA = 0; // 接收错误,重新接???? 191 USART_RX_STA = 0; // 接收错误,重新接?????
218 else 192 else
219 USART_RX_STA |= 0x8000; // 接收完成 193 USART_RX_STA |= 0x8000; // 接收完成
220 } 194 }
221 else // 还没接收????0x0D 195 else // 还没接收?????0x0D
222 { 196 {
223 if (aRxBuffer[0] == 0x0d) 197 if (aRxBuffer[0] == 0x0d)
224 { 198 {
...@@ -230,7 +204,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) ...@@ -230,7 +204,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
230 USART_RX_STA++; 204 USART_RX_STA++;
231 if (USART_RX_STA > (USART_REC_LEN-1)) 205 if (USART_RX_STA > (USART_REC_LEN-1))
232 { 206 {
233 USART_RX_STA = 0; // 接收错误,重新开始接???? 207 USART_RX_STA = 0; // 接收错误,重新开始接?????
234 } 208 }
235 } 209 }
236 } 210 }
......
1 #MicroXplorer Configuration settings - do not modify 1 #MicroXplorer Configuration settings - do not modify
2 FREERTOS.FootprintOK=false 2 FREERTOS.FootprintOK=false
3 FREERTOS.IPParameters=Tasks01,FootprintOK,configTOTAL_HEAP_SIZE,configTIMER_TASK_STACK_DEPTH 3 FREERTOS.IPParameters=Tasks01,configTOTAL_HEAP_SIZE,configTIMER_TASK_STACK_DEPTH,FootprintOK
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,24,128,StartCoreTask,Default,NULL,Dynamic,NULL,NULL;SensorTask,8,128,StartSensorTask,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,512,StartUartTask,Default,NULL,Dynamic,NULL,NULL;CoreTask,24,128,StartCoreTask,Default,NULL,Dynamic,NULL,NULL;SensorTask,24,128,StartSensorTask,Default,NULL,Dynamic,NULL,NULL
5 FREERTOS.configTIMER_TASK_STACK_DEPTH=128 5 FREERTOS.configTIMER_TASK_STACK_DEPTH=128
6 FREERTOS.configTOTAL_HEAP_SIZE=2560 6 FREERTOS.configTOTAL_HEAP_SIZE=2560
7 File.Version=6 7 File.Version=6
...@@ -11,9 +11,11 @@ Mcu.IP0=FREERTOS ...@@ -11,9 +11,11 @@ Mcu.IP0=FREERTOS
11 Mcu.IP1=NVIC 11 Mcu.IP1=NVIC
12 Mcu.IP2=RCC 12 Mcu.IP2=RCC
13 Mcu.IP3=SYS 13 Mcu.IP3=SYS
14 Mcu.IP4=USART1 14 Mcu.IP4=TIM3
15 Mcu.IP5=USART2 15 Mcu.IP5=TIM6
16 Mcu.IPNb=6 16 Mcu.IP6=USART1
17 Mcu.IP7=USART2
18 Mcu.IPNb=8
17 Mcu.Name=STM32F030C8Tx 19 Mcu.Name=STM32F030C8Tx
18 Mcu.Package=LQFP48 20 Mcu.Package=LQFP48
19 Mcu.Pin0=PC14-OSC32_IN 21 Mcu.Pin0=PC14-OSC32_IN
...@@ -25,6 +27,8 @@ Mcu.Pin13=PB8 ...@@ -25,6 +27,8 @@ Mcu.Pin13=PB8
25 Mcu.Pin14=PB9 27 Mcu.Pin14=PB9
26 Mcu.Pin15=VP_FREERTOS_VS_CMSIS_V2 28 Mcu.Pin15=VP_FREERTOS_VS_CMSIS_V2
27 Mcu.Pin16=VP_SYS_VS_tim1 29 Mcu.Pin16=VP_SYS_VS_tim1
30 Mcu.Pin17=VP_TIM3_VS_no_output1
31 Mcu.Pin18=VP_TIM6_VS_ClockSourceINT
28 Mcu.Pin2=PF0-OSC_IN 32 Mcu.Pin2=PF0-OSC_IN
29 Mcu.Pin3=PF1-OSC_OUT 33 Mcu.Pin3=PF1-OSC_OUT
30 Mcu.Pin4=PA2 34 Mcu.Pin4=PA2
...@@ -33,7 +37,7 @@ Mcu.Pin6=PA9 ...@@ -33,7 +37,7 @@ Mcu.Pin6=PA9
33 Mcu.Pin7=PA10 37 Mcu.Pin7=PA10
34 Mcu.Pin8=PA12 38 Mcu.Pin8=PA12
35 Mcu.Pin9=PA13 39 Mcu.Pin9=PA13
36 Mcu.PinsNb=17 40 Mcu.PinsNb=19
37 Mcu.ThirdPartyNb=0 41 Mcu.ThirdPartyNb=0
38 Mcu.UserConstants= 42 Mcu.UserConstants=
39 Mcu.UserName=STM32F030C8Tx 43 Mcu.UserName=STM32F030C8Tx
...@@ -47,6 +51,8 @@ NVIC.PendSV_IRQn=true\:3\:0\:false\:false\:false\:true\:false\:false ...@@ -47,6 +51,8 @@ NVIC.PendSV_IRQn=true\:3\:0\:false\:false\:false\:true\:false\:false
47 NVIC.SVC_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:false 51 NVIC.SVC_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:false
48 NVIC.SysTick_IRQn=true\:3\:0\:false\:false\:false\:true\:false\:false 52 NVIC.SysTick_IRQn=true\:3\:0\:false\:false\:false\:true\:false\:false
49 NVIC.TIM1_BRK_UP_TRG_COM_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true 53 NVIC.TIM1_BRK_UP_TRG_COM_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true
54 NVIC.TIM3_IRQn=true\:3\:0\:true\:false\:true\:true\:true\:true
55 NVIC.TIM6_IRQn=true\:3\:0\:false\:false\:true\:true\:true\:true
50 NVIC.TimeBase=TIM1_BRK_UP_TRG_COM_IRQn 56 NVIC.TimeBase=TIM1_BRK_UP_TRG_COM_IRQn
51 NVIC.TimeBaseIP=TIM1 57 NVIC.TimeBaseIP=TIM1
52 NVIC.USART2_IRQn=true\:3\:0\:false\:false\:true\:true\:true\:true 58 NVIC.USART2_IRQn=true\:3\:0\:false\:false\:true\:true\:true\:true
...@@ -159,6 +165,13 @@ RCC.TimSysFreq_Value=48000000 ...@@ -159,6 +165,13 @@ RCC.TimSysFreq_Value=48000000
159 RCC.USART1Freq_Value=48000000 165 RCC.USART1Freq_Value=48000000
160 SH.GPXTI8.0=GPIO_EXTI8 166 SH.GPXTI8.0=GPIO_EXTI8
161 SH.GPXTI8.ConfNb=1 167 SH.GPXTI8.ConfNb=1
168 TIM3.Channel-Output\ Compare1\ No\ Output=TIM_CHANNEL_1
169 TIM3.IPParameters=Channel-Output Compare1 No Output,Prescaler,Period
170 TIM3.Period=1999
171 TIM3.Prescaler=23999
172 TIM6.IPParameters=Prescaler,Period
173 TIM6.Period=299
174 TIM6.Prescaler=47999
162 USART1.BaudRate=115200 175 USART1.BaudRate=115200
163 USART1.IPParameters=VirtualMode-Asynchronous,BaudRate 176 USART1.IPParameters=VirtualMode-Asynchronous,BaudRate
164 USART1.VirtualMode-Asynchronous=VM_ASYNC 177 USART1.VirtualMode-Asynchronous=VM_ASYNC
...@@ -169,5 +182,9 @@ VP_FREERTOS_VS_CMSIS_V2.Mode=CMSIS_V2 ...@@ -169,5 +182,9 @@ VP_FREERTOS_VS_CMSIS_V2.Mode=CMSIS_V2
169 VP_FREERTOS_VS_CMSIS_V2.Signal=FREERTOS_VS_CMSIS_V2 182 VP_FREERTOS_VS_CMSIS_V2.Signal=FREERTOS_VS_CMSIS_V2
170 VP_SYS_VS_tim1.Mode=TIM1 183 VP_SYS_VS_tim1.Mode=TIM1
171 VP_SYS_VS_tim1.Signal=SYS_VS_tim1 184 VP_SYS_VS_tim1.Signal=SYS_VS_tim1
185 VP_TIM3_VS_no_output1.Mode=Output Compare1 No Output
186 VP_TIM3_VS_no_output1.Signal=TIM3_VS_no_output1
187 VP_TIM6_VS_ClockSourceINT.Mode=Enable_Timer
188 VP_TIM6_VS_ClockSourceINT.Signal=TIM6_VS_ClockSourceINT
172 board=custom 189 board=custom
173 isbadioc=false 190 isbadioc=false
......