add new usart
Showing
7 changed files
with
259 additions
and
85 deletions
Core/Inc/ringbuffer.h
0 → 100644
1 | #ifndef _RINGBUF_H_ | ||
2 | #define _RINGBUF_H_ | ||
3 | |||
4 | #include "cmsis_os.h" | ||
5 | |||
6 | #if !defined(__WINDOWS__) && !defined (FREERTOS) && !defined (LINUX) | ||
7 | #define FREERTOS | ||
8 | #endif | ||
9 | |||
10 | /** | ||
11 | * Put data in a specific length. | ||
12 | * @param *buffer the position of buffer specified by user. | ||
13 | * @param len the length of data which user wants to put. | ||
14 | * @return WRITE_SUCCEED if write succeed,FAIL_LACK_OF_SPACE if no enough space to write in. | ||
15 | */ | ||
16 | int8_t RINGBUF_SEND(const char * buffer, uint16_t len); | ||
17 | |||
18 | /** | ||
19 | * Take data out in a specific length. | ||
20 | * @param *buffer the position of buffer specified by user. | ||
21 | * @param len the length of data which user wants to take. | ||
22 | * @return RINGBUF_READ if read succeed,FAIL_READ_TOO_FAST if readpoint would pass writepoint. | ||
23 | */ | ||
24 | int8_t RINGBUF_READ(uint8_t * buffer, uint16_t len); | ||
25 | |||
26 | /** | ||
27 | * Get data from a specific length. | ||
28 | * @param *buffer the position of buffer specified by user. | ||
29 | * @param len the length of data which user wants to get,not take out!! | ||
30 | * @return RINGBUF_READ if read succeed,FAIL_READ_TOO_FAST if readpoint would pass writepoint. | ||
31 | */ | ||
32 | int8_t RINGBUF_PEEK(uint8_t * buffer, uint16_t len); | ||
33 | |||
34 | /** | ||
35 | * use buffer specified by user as ringbuffer. | ||
36 | * @param *buffer the position of buffer specified by user. | ||
37 | * @param len the length of buffer which user wants. | ||
38 | * @return NULL. | ||
39 | */ | ||
40 | void RINGBUF_INIT(uint8_t * ringbuffer, uint16_t len); | ||
41 | |||
42 | /** | ||
43 | * check if read too fast. | ||
44 | * @param NULL | ||
45 | * @return 1 if read gradually,0 if readpoint catch writepoint. | ||
46 | */ | ||
47 | uint8_t RINGBUF_CHECKIO(void); | ||
48 | |||
49 | /** | ||
50 | * request for space avaliable to write. | ||
51 | * @param NULL | ||
52 | * @return len the space avaliable to use of this ringbuffer. | ||
53 | */ | ||
54 | uint16_t RINGBUF_AVALIABLE_SPACE(void); | ||
55 | |||
56 | #endif |
... | @@ -28,21 +28,19 @@ | ... | @@ -28,21 +28,19 @@ |
28 | 28 | ||
29 | /* USER CODE BEGIN Includes */ | 29 | /* USER CODE BEGIN Includes */ |
30 | #include <mprintf.h> | 30 | #include <mprintf.h> |
31 | #include "ringbuffer.h" | ||
31 | /* USER CODE END Includes */ | 32 | /* USER CODE END Includes */ |
32 | 33 | ||
33 | extern UART_HandleTypeDef huart1; | 34 | extern UART_HandleTypeDef huart1; |
34 | extern UART_HandleTypeDef huart2; | 35 | extern UART_HandleTypeDef huart2; |
35 | 36 | ||
36 | /* USER CODE BEGIN Private defines */ | 37 | /* USER CODE BEGIN Private defines */ |
37 | #define USART_REC_LEN 200 | 38 | #define USART1_BUF_LEN 200 |
38 | 39 | ||
39 | extern uint8_t USART_RX_BUF[USART_REC_LEN]; // 接收缓冲,最大USART_REC_LEN个字节,末字节为换行 | 40 | extern uint8_t USART1_RingBuff[USART1_BUF_LEN]; // 接收缓冲 |
40 | extern uint16_t USART_RX_STA; | ||
41 | 41 | ||
42 | #define RXBUFFERSIZE 1 // 缓存大小 | 42 | #define AT_RESP(str, len) HAL_UART_Transmit(&huart2, (uint8_t*)str, len, 1000) |
43 | extern uint8_t aRxBuffer[RXBUFFERSIZE]; // HAL库USART接收Buffer | 43 | #define DEBUG_PRINT(str, len) HAL_UART_Transmit(&huart1, (uint8_t*)str, len, 1000) |
44 | |||
45 | #define DEBUG_PRINT(str, len) HAL_UART_Transmit(&huart1, (uint8_t*)str, len, 1000); | ||
46 | /* USER CODE END Private defines */ | 44 | /* USER CODE END Private defines */ |
47 | 45 | ||
48 | void MX_USART1_UART_Init(void); | 46 | void MX_USART1_UART_Init(void); | ... | ... |
... | @@ -260,34 +260,24 @@ DONE: | ... | @@ -260,34 +260,24 @@ DONE: |
260 | void StartUartTask(void *argument) | 260 | void StartUartTask(void *argument) |
261 | { | 261 | { |
262 | /* USER CODE BEGIN StartUartTask */ | 262 | /* USER CODE BEGIN StartUartTask */ |
263 | uint8_t len; | 263 | uint8_t temp; |
264 | uint16_t times; | 264 | uint16_t times; |
265 | /* Infinite loop */ | 265 | /* Infinite loop */ |
266 | for(;;) | 266 | for(;;) |
267 | { | 267 | { |
268 | if (USART_RX_STA & 0x8000) | 268 | while(RINGBUF_CHECKIO()) |
269 | { | ||
270 | RINGBUF_READ(&temp, 1); | ||
271 | mprintf("%c", temp); | ||
272 | } | ||
273 | |||
274 | times++; | ||
275 | if (times % 5000 == 0) | ||
269 | { | 276 | { |
270 | len = USART_RX_STA & 0x3fff; // 获取接收到的数据长度 | 277 | mprintf("\r\nUART Test\r\n"); |
271 | mprintf("\r\nReceived:\r\n"); | ||
272 | HAL_UART_Transmit(&huart1, (uint8_t*)USART_RX_BUF, len, 1000); | ||
273 | while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET); | ||
274 | mprintf("\r\n\r\n"); | ||
275 | USART_RX_STA=0; | ||
276 | } | ||
277 | else | ||
278 | { | ||
279 | times++; | ||
280 | if (times % 5000 == 0) | ||
281 | { | ||
282 | mprintf("\r\nUART Test\r\n"); | ||
283 | } | ||
284 | // if (times % 1000 == 0) | ||
285 | // { | ||
286 | // mprintf("print char\r\n"); | ||
287 | // } | ||
288 | } | 278 | } |
289 | 279 | ||
290 | osDelay(1); | 280 | osDelay(5); |
291 | } | 281 | } |
292 | /* USER CODE END StartUartTask */ | 282 | /* USER CODE END StartUartTask */ |
293 | } | 283 | } | ... | ... |
... | @@ -98,8 +98,9 @@ int main(void) | ... | @@ -98,8 +98,9 @@ int main(void) |
98 | /* USER CODE BEGIN 2 */ | 98 | /* USER CODE BEGIN 2 */ |
99 | // 使能TIM3中断 | 99 | // 使能TIM3中断 |
100 | HAL_TIM_Base_Start_IT(&htim3); | 100 | HAL_TIM_Base_Start_IT(&htim3); |
101 | // 使能UART2中断 | 101 | // 使能RingBuf和UART2中断 |
102 | HAL_UART_Receive_IT(&huart2, (uint8_t*)aRxBuffer, RXBUFFERSIZE); | 102 | RINGBUF_INIT(USART1_RingBuff, USART1_BUF_LEN); |
103 | __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE); | ||
103 | /* USER CODE END 2 */ | 104 | /* USER CODE END 2 */ |
104 | 105 | ||
105 | /* Call init function for freertos objects (in freertos.c) */ | 106 | /* Call init function for freertos objects (in freertos.c) */ | ... | ... |
Core/Src/ringbuffer.c
0 → 100644
1 | #include "ringbuffer.h" | ||
2 | #include "string.h" | ||
3 | |||
4 | #ifdef FREERTOS | ||
5 | #include "FreeRTOS.h" | ||
6 | #include "task.h" | ||
7 | #include "queue.h" | ||
8 | #include "timers.h" | ||
9 | #include "semphr.h" | ||
10 | #include "portmacro.h" | ||
11 | |||
12 | xSemaphoreHandle xSemaphore_RINGBUF = NULL; | ||
13 | #endif | ||
14 | |||
15 | #define WRITE_SUCCEED 0 | ||
16 | #define READ_SUCCESS 0 | ||
17 | |||
18 | #define FAIL_LACK_OF_SPACE -1 | ||
19 | #define FAIL_READ_TOO_FAST -2 | ||
20 | |||
21 | static uint16_t WRITE_ID; | ||
22 | static uint16_t READ_ID; | ||
23 | static uint16_t BUF_LEN; | ||
24 | static uint8_t *RING_BUFFER; | ||
25 | |||
26 | uint16_t RingBuffer_everMAX = 0; | ||
27 | |||
28 | /***************************RINGBUF_SEND***************************/ | ||
29 | /* | ||
30 | *warning:this API is not safe for interrupt operation,please ensure only one IRQ functions call this function. | ||
31 | */ | ||
32 | int8_t RINGBUF_SEND(const char *buffer,uint16_t len) | ||
33 | { | ||
34 | if(len > RINGBUF_AVALIABLE_SPACE()) | ||
35 | { | ||
36 | return FAIL_LACK_OF_SPACE; | ||
37 | } | ||
38 | |||
39 | uint16_t distance = BUF_LEN - WRITE_ID; | ||
40 | if(distance>len) | ||
41 | { | ||
42 | memcpy((void *)&RING_BUFFER[WRITE_ID],buffer,len); | ||
43 | WRITE_ID += len; | ||
44 | } | ||
45 | else | ||
46 | { | ||
47 | memcpy((void *)&RING_BUFFER[WRITE_ID],buffer,distance); | ||
48 | WRITE_ID = 0; | ||
49 | if(len-distance) | ||
50 | { | ||
51 | memcpy((void *)&RING_BUFFER[WRITE_ID],buffer+distance,len-distance); | ||
52 | WRITE_ID = len-distance; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | (WRITE_ID+BUF_LEN-READ_ID)%BUF_LEN > RingBuffer_everMAX ? RingBuffer_everMAX = (WRITE_ID+BUF_LEN-READ_ID)%BUF_LEN : 1; | ||
57 | |||
58 | return WRITE_SUCCEED; | ||
59 | } | ||
60 | |||
61 | /***************************RINGBUF_READ***************************/ | ||
62 | int8_t RINGBUF_READ(uint8_t *buffer,uint16_t len) | ||
63 | { | ||
64 | #ifdef FREERTOS | ||
65 | xSemaphoreTake( xSemaphore_RINGBUF, portMAX_DELAY );//take Mutex for read | ||
66 | #endif | ||
67 | |||
68 | if(len > (BUF_LEN - RINGBUF_AVALIABLE_SPACE())) | ||
69 | { | ||
70 | #ifdef FREERTOS | ||
71 | xSemaphoreGive( xSemaphore_RINGBUF ); | ||
72 | #endif | ||
73 | |||
74 | return FAIL_READ_TOO_FAST; | ||
75 | } | ||
76 | uint16_t distance = BUF_LEN - READ_ID; | ||
77 | if(distance>len) | ||
78 | { | ||
79 | memcpy((void *)buffer,&RING_BUFFER[READ_ID],len); | ||
80 | READ_ID += len; | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | memcpy((void *)buffer,&RING_BUFFER[READ_ID],distance); | ||
85 | READ_ID = 0; | ||
86 | if(len-distance) | ||
87 | { | ||
88 | memcpy((void *)(buffer+distance),&RING_BUFFER[READ_ID],len-distance); | ||
89 | READ_ID = len-distance; | ||
90 | } | ||
91 | } | ||
92 | *(buffer+len) = NULL; | ||
93 | |||
94 | #ifdef FREERTOS | ||
95 | xSemaphoreGive( xSemaphore_RINGBUF );//give Mutex for read | ||
96 | #endif | ||
97 | |||
98 | return READ_SUCCESS; | ||
99 | } | ||
100 | |||
101 | /***************************RINGBUF_PEEK***************************/ | ||
102 | int8_t RINGBUF_PEEK(uint8_t *buffer,uint16_t len) | ||
103 | { | ||
104 | #ifdef FREERTOS | ||
105 | xSemaphoreTake( xSemaphore_RINGBUF, portMAX_DELAY );//take Mutex for read | ||
106 | #endif | ||
107 | |||
108 | if(len > (BUF_LEN - RINGBUF_AVALIABLE_SPACE())) | ||
109 | { | ||
110 | #ifdef FREERTOS | ||
111 | xSemaphoreGive( xSemaphore_RINGBUF ); | ||
112 | #endif | ||
113 | |||
114 | return FAIL_READ_TOO_FAST; | ||
115 | } | ||
116 | uint16_t readpoint = READ_ID; | ||
117 | uint16_t distance = BUF_LEN - readpoint; | ||
118 | if(distance>len) | ||
119 | { | ||
120 | memcpy((void *)buffer,&RING_BUFFER[readpoint],len); | ||
121 | readpoint += len; | ||
122 | } | ||
123 | else | ||
124 | { | ||
125 | memcpy((void *)buffer,&RING_BUFFER[readpoint],distance); | ||
126 | readpoint = 0; | ||
127 | if(len-distance) | ||
128 | { | ||
129 | memcpy((void *)(buffer+distance),&RING_BUFFER[readpoint],len-distance); | ||
130 | readpoint = len-distance; | ||
131 | } | ||
132 | } | ||
133 | *(buffer+len) = NULL; | ||
134 | |||
135 | #ifdef FREERTOS | ||
136 | xSemaphoreGive( xSemaphore_RINGBUF ); | ||
137 | #endif | ||
138 | |||
139 | return READ_SUCCESS; | ||
140 | } | ||
141 | |||
142 | /***************************RINGBUF_INIT***************************/ | ||
143 | void RINGBUF_INIT(uint8_t * ringbuffer,uint16_t len) | ||
144 | { | ||
145 | #ifdef FREERTOS | ||
146 | xSemaphore_RINGBUF = xSemaphoreCreateMutex(); | ||
147 | #endif | ||
148 | |||
149 | WRITE_ID = 0; | ||
150 | READ_ID = 0; | ||
151 | RING_BUFFER = ringbuffer; | ||
152 | BUF_LEN = len; | ||
153 | memset(RING_BUFFER,0x00,BUF_LEN); | ||
154 | } | ||
155 | |||
156 | /***************************RINGBUF_CHECKIO************************/ | ||
157 | uint8_t RINGBUF_CHECKIO(void) | ||
158 | { | ||
159 | if(READ_ID == WRITE_ID) | ||
160 | return 0; | ||
161 | else | ||
162 | return 1; | ||
163 | } | ||
164 | |||
165 | /****************************RINGBUF_AVALIABLE_SPACE***************/ | ||
166 | uint16_t RINGBUF_AVALIABLE_SPACE() | ||
167 | { | ||
168 | uint16_t len = 0; | ||
169 | if(WRITE_ID < READ_ID) | ||
170 | { | ||
171 | len = READ_ID - WRITE_ID; | ||
172 | } | ||
173 | else | ||
174 | { | ||
175 | len = BUF_LEN - (WRITE_ID - READ_ID); | ||
176 | } | ||
177 | return len; | ||
178 | } |
... | @@ -168,24 +168,14 @@ void TIM6_IRQHandler(void) | ... | @@ -168,24 +168,14 @@ void TIM6_IRQHandler(void) |
168 | void USART2_IRQHandler(void) | 168 | void USART2_IRQHandler(void) |
169 | { | 169 | { |
170 | /* USER CODE BEGIN USART2_IRQn 0 */ | 170 | /* USER CODE BEGIN USART2_IRQn 0 */ |
171 | uint16_t timeout = 0; | 171 | uint8_t res; |
172 | /* USER CODE END USART2_IRQn 0 */ | 172 | /* USER CODE END USART2_IRQn 0 */ |
173 | HAL_UART_IRQHandler(&huart2); | 173 | // HAL_UART_IRQHandler(&huart2); |
174 | /* USER CODE BEGIN USART2_IRQn 1 */ | 174 | /* USER CODE BEGIN USART2_IRQn 1 */ |
175 | timeout = 0; | 175 | if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE) == pdTRUE) |
176 | while (HAL_UART_GetState(&huart2) != HAL_UART_STATE_READY) | ||
177 | { | 176 | { |
178 | timeout++; | 177 | res = huart2.Instance->RDR; |
179 | if (timeout > HAL_MAX_DELAY) | 178 | RINGBUF_SEND((const char *)&res, 1); |
180 | break; | ||
181 | } | ||
182 | |||
183 | timeout=0; | ||
184 | while (HAL_UART_Receive_IT(&huart2, (uint8_t*)aRxBuffer, RXBUFFERSIZE) != HAL_OK) | ||
185 | { | ||
186 | timeout++; | ||
187 | if (timeout > HAL_MAX_DELAY) | ||
188 | break; | ||
189 | } | 179 | } |
190 | /* USER CODE END USART2_IRQn 1 */ | 180 | /* USER CODE END USART2_IRQn 1 */ |
191 | } | 181 | } | ... | ... |
... | @@ -21,11 +21,7 @@ | ... | @@ -21,11 +21,7 @@ |
21 | #include "usart.h" | 21 | #include "usart.h" |
22 | 22 | ||
23 | /* USER CODE BEGIN 0 */ | 23 | /* USER CODE BEGIN 0 */ |
24 | uint8_t USART_RX_BUF[USART_REC_LEN]; | 24 | uint8_t USART1_RingBuff[USART1_BUF_LEN]; |
25 | |||
26 | uint16_t USART_RX_STA = 0; | ||
27 | |||
28 | uint8_t aRxBuffer[RXBUFFERSIZE]; | ||
29 | 25 | ||
30 | /* USER CODE END 0 */ | 26 | /* USER CODE END 0 */ |
31 | 27 | ||
... | @@ -176,41 +172,6 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) | ... | @@ -176,41 +172,6 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) |
176 | } | 172 | } |
177 | 173 | ||
178 | /* USER CODE BEGIN 1 */ | 174 | /* USER CODE BEGIN 1 */ |
179 | /** | ||
180 | * 串口接收回调函数 | ||
181 | */ | ||
182 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) | ||
183 | { | ||
184 | if (huart->Instance == USART2) // 串口1 | ||
185 | { | ||
186 | if ((USART_RX_STA & 0x8000) == 0) // 接收未完????? | ||
187 | { | ||
188 | if (USART_RX_STA & 0x4000) // 接收到了0x0D | ||
189 | { | ||
190 | if (aRxBuffer[0] != 0x0a) | ||
191 | USART_RX_STA = 0; // 接收错误,重新接????? | ||
192 | else | ||
193 | USART_RX_STA |= 0x8000; // 接收完成 | ||
194 | } | ||
195 | else // 还没接收?????0x0D | ||
196 | { | ||
197 | if (aRxBuffer[0] == 0x0d) | ||
198 | { | ||
199 | USART_RX_STA |= 0x4000; | ||
200 | } | ||
201 | else | ||
202 | { | ||
203 | USART_RX_BUF[USART_RX_STA & 0X3FFF] = aRxBuffer[0] ; | ||
204 | USART_RX_STA++; | ||
205 | if (USART_RX_STA > (USART_REC_LEN-1)) | ||
206 | { | ||
207 | USART_RX_STA = 0; // 接收错误,重新开始接????? | ||
208 | } | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | } | ||
214 | /* USER CODE END 1 */ | 175 | /* USER CODE END 1 */ |
215 | 176 | ||
216 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ | 177 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ | ... | ... |
-
Please register or sign in to post a comment