forked from UrsusPilot/QuadCopterGroundStation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_rs232.c
338 lines (296 loc) · 10.6 KB
/
module_rs232.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*=====================================================================================================*/
/*=====================================================================================================*/
#include "stm32f4_system.h"
#include "stm32f4_usart.h"
#include "module_rs232.h"
#include "algorithm_string.h"
#include <unistd.h>
#include <stdarg.h>
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_Config
**功能 :
**輸入 :
**輸出 :
**使用 :
**=====================================================================================================*/
/*=====================================================================================================*/
void RS232_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
/* USART1 Tx PB10 */ /* USART1 Rx PB11 */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStruct);
USART_Cmd(USART3, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_SendStr
**功能 : 發送字串
**輸入 : USARTx, *pWord
**輸出 : None
**使用 : RS232_SendStr(USART1, (u8*)"Hellow World!");
**=====================================================================================================*/
/*=====================================================================================================*/
void RS232_SendStr(USART_TypeDef *USARTx, uc8 *pWord)
{
while (*pWord != '\0') {
USART_SendByte(USARTx, (u8 *)pWord);
pWord++;
}
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_SendNum
**功能 : 將數值轉字串發送
**輸入 : USARTx, Type, NumLen, SendData
**輸出 : None
**使用 : RS232_SendNum(USART1, Type_O, 6, 1024);
**=====================================================================================================*/
/*=====================================================================================================*/
void RS232_SendNum(USART_TypeDef *USARTx, u8 Type, u8 NumLen, s32 SendData)
{
u8 TrData[32] = {0};
u8 *pWord = TrData;
Str_NumToChar(Type, NumLen, TrData, SendData);
while (*pWord != '\0') {
USART_SendByte(USARTx, (u8 *)pWord);
pWord++;
}
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_SendData
**功能 : 發送資料
**輸入 : USARTx, *SendData, DataLen
**輸出 : None
**使用 : RS232_SendData(USART1, SendData, DataLen);
**=====================================================================================================*/
/*=====================================================================================================*/
int RS232_SendData(USART_TypeDef *USARTx, uc8 *SendData, u16 DataLen)
{
do {
USART_SendByte(USARTx, SendData);
SendData++;
DataLen--;
} while (DataLen);
return 1;
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_RecvStr
**功能 : 接收字串
**輸入 : USARTx, *pWord
**輸出 : None
**使用 : RS232_RecvStr(USART1, Stirng);
**=====================================================================================================*/
/*=====================================================================================================*/
void RS232_RecvStr(USART_TypeDef *USARTx, u8 *pWord)
{
do {
*pWord = USART_RecvByte(USARTx);
pWord++;
} while (*(pWord - 1) != '\0');
*pWord = '\0';
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_RecvData
**功能 : 接收資料
**輸入 : USARTx, *RecvData, DataLen
**輸出 : None
**使用 : RS232_RecvData(USART1, RecvData, DataLen);
**=====================================================================================================*/
/*=====================================================================================================*/
void RS232_RecvData(USART_TypeDef *USARTx, u8 *RecvData, u16 DataLen)
{
do {
*RecvData = USART_RecvByte(USARTx);
RecvData++;
DataLen--;
} while (DataLen);
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_VisualScope_CRC16
**功能 :
**輸入 :
**輸出 :
**使用 :
**=====================================================================================================*/
/*=====================================================================================================*/
static u16 RS232_VisualScope_CRC16(u8 *Array, u16 Len)
{
u16 USART_IX, USART_IY, USART_CRC;
USART_CRC = 0xffff;
for (USART_IX = 0; USART_IX < Len; USART_IX++) {
USART_CRC = USART_CRC ^ (u16)(Array[USART_IX]);
for (USART_IY = 0; USART_IY <= 7; USART_IY++) {
if ((USART_CRC & 1) != 0)
USART_CRC = (USART_CRC >> 1) ^ 0xA001;
else
USART_CRC = USART_CRC >> 1;
}
}
return (USART_CRC);
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : RS232_VisualScope
**功能 :
**輸入 :
**輸出 :
**使用 :
**=====================================================================================================*/
/*=====================================================================================================*/
void RS232_VisualScope(USART_TypeDef *USARTx, u8 *pWord, u16 Len)
{
u8 i = 0;
u16 Temp = 0;
Temp = RS232_VisualScope_CRC16(pWord, Len);
pWord[8] = Temp & 0x00ff;
pWord[9] = (Temp & 0xff00) >> 8;
for (i = 0; i < 10; i++) {
USART_SendData(USARTx, *pWord);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
pWord++;
}
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : itoa
**功能 : Support the sprintf & printf function
**輸入 :
**輸出 :
**使用 :
**=====================================================================================================*/
/*=====================================================================================================*/
char *itoa(int value, char *str)
{
int base = 10;
int divideNum = base;
int i = 0;
while (value / divideNum > 0) {
divideNum *= base;
}
if (value < 0) {
str[0] = '-';
i++;
}
while (divideNum / base > 0) {
divideNum /= base;
str[i++] = value / divideNum + 48;
value %= divideNum;
}
str[i] = '\0';
return str;
}
/*=====================================================================================================*/
/*=====================================================================================================*
**函數 : sprintf
**功能 :
**輸入 : ( char * str, const char * format, ... )
**輸出 : strlen(str)
**使用 :
**=====================================================================================================*/
/*=====================================================================================================*/
int sprintf(char *str, const char *format, ...)
{
va_list para;
va_start(para, format);
int curr_pos = 0;
char ch[] = {'0', '\0'};
char integer[11];
str[0] = '\0';
while (format[curr_pos] != '\0') {
if (format[curr_pos] != '%') {
ch[0] = format[curr_pos];
strcat(str, ch);
} else {
switch (format[++curr_pos]) {
case 's':
strcat(str, va_arg(para, char *));
break;
case 'c':
ch[0] = (char)va_arg(para, int);
strcat(str, ch);
break;
case 'i':
case 'd':
strcat(str, itoa(va_arg(para, int), integer));
break;
case 'u':
strcat(str, itoa(va_arg(para, unsigned), integer));
break;
default:
break;
}
}
curr_pos++;
}
va_end(para);
return strlen(str);
}
/*=====================================================================================================*
**函數 : printf
**功能 :
**輸入 :
**輸出 : RS232_SendData(USART3, str, strlen(str));
**使用 :
**=====================================================================================================*/
/*=====================================================================================================*/
int printf(const char *format, ...)
{
char str[128];
va_list para;
va_start(para, format);
int curr_pos = 0;
char ch[] = {'0', '\0'};
char integer[11];
str[0] = '\0';
while (format[curr_pos] != '\0') {
if (format[curr_pos] != '%') {
ch[0] = format[curr_pos];
strcat(str, ch);
} else {
switch (format[++curr_pos]) {
case 's':
strcat(str, va_arg(para, char *));
break;
case 'c':
ch[0] = (char)va_arg(para, int);
strcat(str, ch);
break;
case 'i':
case 'd':
strcat(str, itoa(va_arg(para, int), integer));
break;
case 'u':
strcat(str, itoa(va_arg(para, unsigned), integer));
break;
default:
break;
}
}
curr_pos++;
}
va_end(para);
return RS232_SendData(USART3, str, strlen(str));
}