bsp_usart.c
25.8 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
*********************************************************************************************************
*
* 模块名称 : 串口驱动模块
* 文件名称 : bsp_usart.c
* 版 本 : V2.0
* 说 明 : 实现printf和scanf函数重定向到串口1,即支持printf信息到USART1
* 实现重定向,只需要添加2个函数:
* int fputc(int ch, FILE *f);
* int fgetc(FILE *f);
* 对于KEIL MDK编译器,编译选项中需要在MicorLib前面打钩,否则不会有数据打印到USART1。
*
*********************************************************************************************************
*/
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "cpu.h"
#include "stm32f4xx_flash.h"
#include "includes.h"
#include "bsp.h"
#include <common.h>
UartStruct Uart1Stu;
UartStruct Uart2Stu;
UartStruct Uart3Stu;
UartStruct Uart4Stu;
UartStruct Uart6Stu;
Imu_msg imu_msg;
/*
*********************************************************************************************************
* 函 数 名: bsp_InitUart
* 功能说明: 初始化CPU的USART1串口硬件设备。未启用中断。
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
//初始化 COM1 COM2 485-1 485-2 COM6--TTL串口(WIFI/Zigbee) 2022-4-12修改
// COM1 TX--PA9 RX--PA10 UART1 232-1
// COM2 TX--PD5 RX--PD6 UART2 232-2
// COM3 TX--PD8 RX--PD9 UART3 485-1
// COM4 TX--PC10 RX--PC11 UART4 485-2
// COM6 TX--PG14 RX--PG9 TTL串口(Zigbee/WiFi)
//bound:波特率
void uart_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOG,ENABLE); //使能GPIOA GPIOC GPIOD时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟 232-1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//使能USART6时钟 TTL ZIGBEE/WIFI
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // 使能 USART2 clock 232-2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // 485-1 COM3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); // 485-2 COM4
//串口1对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1 --- COM1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1
// 串口2
GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2); //GPIOD5复用为USART2 --- COM2
GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2); //GPIOD6复用为USART2
// 485-1
GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_USART3); //GPIOD8复用为USART3 --- 485-1
GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_USART3); //GPIOD9复用为USART3
//485-2
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4); //GPIOC10复用为UART4 --- 485-2
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4); //GPIOC11复用为UART4
// TTL COM6 ZIGBEE/WIFI
GPIO_PinAFConfig(GPIOG,GPIO_PinSource14,GPIO_AF_USART6); //GPIOG14复用为USART6 --- COM6 ZIGBEE
GPIO_PinAFConfig(GPIOG,GPIO_PinSource9,GPIO_AF_USART6); //GPIOG9复用为USART6
//USART1端口配置 232-1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10
// USART2 232-2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD6,PD5
// USART3 485-1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD8,PD9
//UART4端口配置 485-2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PC10,PC11
//UART6端口配置 TTL串口 Zigbee WiFi
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_14;
GPIO_Init(GPIOG,&GPIO_InitStructure); //初始化Pg9,Pg14
//USART1 初始化设置 232-1
USART_InitStructure.USART_BaudRate = bound;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
//USART2 初始化设置 232-2
USART_InitStructure.USART_BaudRate = 115200;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口2
// USART3
USART_InitStructure.USART_BaudRate = 115200;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART3, &USART_InitStructure); //初始化串口3 485-1
USART_Init(UART4, &USART_InitStructure); //初始化串口4 485-2
USART_Init(USART6, &USART_InitStructure); //初始化串口6 TTL ZIGBEE WIFI
USART_Cmd(USART1, ENABLE); //使能 COM1
USART_Cmd(USART2, ENABLE); //使能 COM2
USART_Cmd(USART3, ENABLE); //使能 485-1
USART_Cmd(UART4, ENABLE); //使能 485-2
USART_Cmd(USART6, ENABLE); //使能 COM6 ZIGBEE
// USART_ClearFlag(USART1, USART_FLAG_TC);
// COM1 232-1 接收使能
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
// USART2 232-2 接收使能
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//串口3中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
// USART3 485-1
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//串口3中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
// UART4 485-2
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//开启相关中断
//Uart4 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//串口3中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
// COM6 TTL ZIGBEE WIFI
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart6 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//串口3中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
}
/****************************************************************************
* 函数名: USART1_IRQHandler
* 功 能: USART1中断服务程序.
* 输 入: 无
* 输 出: 无
* 返 回: 无
*/
#if 1
void USART1_IRQHandler(void)
{
CPU_SR_ALLOC();
unsigned int i;
unsigned char ch;
unsigned int flag = USART_GetITStatus(USART1, USART_IT_RXNE);
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
/*接收中断*/
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
ch = USART_ReceiveData(USART1);
if(((Uart1Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart1Stu.RxBuf_Out)
{
}
else
{
Uart1Stu.RxBuf[(Uart1Stu.RxBuf_In)] = ch;
Uart1Stu.RxBuf_In = (Uart1Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
/* 发送缓冲区空中断 */
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
if(Uart1Stu.TxBuf_In != Uart1Stu.TxBuf_Out)
{
i = Uart1Stu.TxBuf_Out;
Uart1Stu.TxBuf_Out = (Uart1Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart1Stu.TxBuf[i];
USART_SendData(USART1,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
}
}
/* 1个字节发送完毕的中断 */
else if (USART_GetITStatus(USART1, USART_IT_TC) != RESET)
{
if(Uart1Stu.TxBuf_In != Uart1Stu.TxBuf_Out)
{
i = Uart1Stu.TxBuf_Out;
Uart1Stu.TxBuf_Out = (Uart1Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart1Stu.TxBuf[i];
USART_SendData(USART1,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(USART1, USART_IT_TC, DISABLE);
}
}
else//接收
{
/* Read one byte from the receive data register */
ch = USART_ReceiveData(USART1);
if(((Uart1Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart1Stu.RxBuf_Out)
{
}
else
{
Uart1Stu.RxBuf[(Uart1Stu.RxBuf_In)] = ch;
Uart1Stu.RxBuf_In = (Uart1Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
OSIntExit();
}
#endif
/****************************************************************************
* 函数名: USART6_IRQHandler
* 功 能: USART6中断服务程序.
* 输 入: 无
* 输 出: 无
* 返 回: 无
*/
unsigned char recvBuff[120];
void USART3_IRQHandler(void)
{
unsigned int i;
unsigned char ch;
u8 flag = 0;
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
/*接收中断*/
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
flag = 1;
/* Read one byte from the receive data register */
ch = USART_ReceiveData(USART3);
if(((Uart3Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart3Stu.RxBuf_Out)
{
}
else
{
Uart3Stu.RxBuf[(Uart3Stu.RxBuf_In)] = ch;
Uart3Stu.RxBuf_In = (Uart3Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
/* 发送缓冲区空中断 */
if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
{
if(Uart3Stu.TxBuf_In != Uart3Stu.TxBuf_Out)
{
i = Uart3Stu.TxBuf_Out;
Uart3Stu.TxBuf_Out = (Uart3Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart3Stu.TxBuf[i];
USART_SendData(USART3,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_TC, ENABLE);
}
}
/* 1个字节发送完毕的中断 */
else if (USART_GetITStatus(USART3, USART_IT_TC) != RESET)
{
if(Uart3Stu.TxBuf_In != Uart3Stu.TxBuf_Out)
{
i = Uart3Stu.TxBuf_Out;
Uart3Stu.TxBuf_Out = (Uart3Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart3Stu.TxBuf[i];
USART_SendData(USART3,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(USART3, USART_IT_TC, DISABLE);
}
}
else if(flag == 0)
{
/* Read one byte from the receive data register */
ch = USART_ReceiveData(USART3);
if(((Uart3Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart3Stu.RxBuf_Out)
{
}
else
{
Uart3Stu.RxBuf[(Uart3Stu.RxBuf_In)] = ch;
Uart3Stu.RxBuf_In = (Uart3Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
// Uart_Printf(COM1,"%c",ch);
static int StepFlag = 0,iii = 0,lastTime = 0;
if(ch == 0x3c)
{
iii = 0;
StepFlag = 1;
}
else if(ch == 0x3e&&StepFlag == 1)
{
StepFlag = 2;
// Uart_Printf(COM1,"一帧数据完成\r\n");
}
if(iii>110)
{
StepFlag = 0;
iii = 0;
}
if(StepFlag == 1)
{
recvBuff[iii++] = ch;
}
else if(StepFlag == 2)
{
recvBuff[iii++] = ch;
StepFlag = 0;
Camera.UpdataFlag = 1;
Camera.OffCount = 100;
// Uart_Printf(COM1,"%s\r\n",recvBuff);
}
OSIntExit();
}
unsigned char recvBuffTwo[120];
void UART4_IRQHandler(void)
{
unsigned int i;
unsigned char ch;
u8 flag = 0;
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
/*接收中断*/
if (USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
{
flag = 1;
/* Read one byte from the receive data register */
ch = USART_ReceiveData(UART4);
if(((Uart4Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart4Stu.RxBuf_Out)
{
}
else
{
Uart4Stu.RxBuf[(Uart4Stu.RxBuf_In)] = ch;
Uart4Stu.RxBuf_In = (Uart4Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
/* 发送缓冲区空中断 */
if (USART_GetITStatus(UART4, USART_IT_TXE) != RESET)
{
if(Uart4Stu.TxBuf_In != Uart4Stu.TxBuf_Out)
{
i = Uart4Stu.TxBuf_Out;
Uart4Stu.TxBuf_Out = (Uart4Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart4Stu.TxBuf[i];
USART_SendData(UART4,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
USART_ITConfig(UART4, USART_IT_TC, ENABLE);
}
}
/* 1个字节发送完毕的中断 */
else if (USART_GetITStatus(UART4, USART_IT_TC) != RESET)
{
if(Uart4Stu.TxBuf_In != Uart4Stu.TxBuf_Out)
{
i = Uart4Stu.TxBuf_Out;
Uart4Stu.TxBuf_Out = (Uart4Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart4Stu.TxBuf[i];
USART_SendData(UART4,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(UART4, USART_IT_TC, DISABLE);
}
}
else if(flag == 0)
{
/* Read one byte from the receive data register */
ch = USART_ReceiveData(UART4);
if(((Uart4Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart4Stu.RxBuf_Out)
{
}
else
{
Uart4Stu.RxBuf[(Uart4Stu.RxBuf_In)] = ch;
Uart4Stu.RxBuf_In = (Uart4Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
// Uart_Printf(COM1,"%c",ch);
static int StepFlag = 0,iii = 0,lastTime = 0;
if(ch == 0x3c)
{
iii = 0;
StepFlag = 1;
}
else if(ch == 0x3e&&StepFlag == 1)
{
StepFlag = 2;
// Uart_Printf(COM1,"一帧数据完成\r\n");
}
if(iii>110)
{
StepFlag = 0;
iii = 0;
}
if(StepFlag == 1)
{
recvBuffTwo[iii++] = ch;
}
else if(StepFlag == 2)
{
recvBuffTwo[iii++] = ch;
StepFlag = 0;
CameraTwo.UpdataFlag = 1;
CameraTwo.OffCount = 100;
// Uart_Printf(COM1,"%s\r\n",recvBuff);
}
OSIntExit();
}
void SetRS485ReadCOM()
{
GPIO_ResetBits(GPIOC,GPIO_Pin_8);
}
void SetRS485WriteCOM()
{
GPIO_SetBits(GPIOC,GPIO_Pin_8);
}
void USART6_IRQHandler(void)
{
unsigned int i;
unsigned char ch;
u8 flag = 0;
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
/*接收中断*/
if (USART_GetITStatus(USART6, USART_IT_RXNE) != RESET)
{
flag = 1;
/* Read one byte from the receive data register */
ch = USART_ReceiveData(USART6);
if(((Uart6Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart6Stu.RxBuf_Out)
{
}
else
{
Uart6Stu.RxBuf[(Uart6Stu.RxBuf_In)] = ch;
Uart6Stu.RxBuf_In = (Uart6Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
/* 发送缓冲区空中断 */
if (USART_GetITStatus(USART6, USART_IT_TXE) != RESET)
{
if(Uart6Stu.TxBuf_In != Uart6Stu.TxBuf_Out)
{
i = Uart6Stu.TxBuf_Out;
Uart6Stu.TxBuf_Out = (Uart6Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart6Stu.TxBuf[i];
SetRS485WriteCOM();
USART_SendData(USART6,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(USART6, USART_IT_TXE, DISABLE);
USART_ITConfig(USART6, USART_IT_TC, ENABLE);
}
}
/* 1个字节发送完毕的中断 */
else if (USART_GetITStatus(USART6, USART_IT_TC) != RESET)
{
if(Uart6Stu.TxBuf_In != Uart6Stu.TxBuf_Out)
{
i = Uart6Stu.TxBuf_Out;
Uart6Stu.TxBuf_Out = (Uart6Stu.TxBuf_Out + 1) & UART_BUF_LEN;
ch = Uart6Stu.TxBuf[i];
SetRS485WriteCOM();
USART_SendData(USART6,ch);
}
else
{
/* 禁止发送缓冲区空中断,使能发送完毕中断 */
USART_ITConfig(USART6, USART_IT_TC, DISABLE);
SetRS485ReadCOM();
}
}
else if(flag == 0)
{
/* Read one byte from the receive data register */
ch = USART_ReceiveData(USART6);
if(((Uart6Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart6Stu.RxBuf_Out)
{
}
else
{
Uart6Stu.RxBuf[(Uart6Stu.RxBuf_In)] = ch;
Uart6Stu.RxBuf_In = (Uart6Stu.RxBuf_In + 1) & UART_BUF_LEN;
}
}
OSIntExit();
}
/****************************************************************************
* 函数名: ReadUart
* 功 能: 从指定的串口接收缓存区中取固定长度数据.
* 输 入: UartID-串口号,buff-数据存放位置, length-预取数据长度
* 输 出: 无
* 返 回: 实际获取的数据长度
*/
unsigned int ReadUart(unsigned char UartID,unsigned char *buff,unsigned int length)
{
unsigned int i = 0;
unsigned int count = 0;
UartStruct *pUartStu;
if(UartID == COM1)
pUartStu = &Uart1Stu;
else if(UartID == COM2)
pUartStu = &Uart2Stu;
else if(UartID == COM3)
pUartStu = &Uart3Stu;
else if(UartID == COM4)
pUartStu = &Uart4Stu;
else if(UartID == COM6)
pUartStu = &Uart6Stu;
else
return 0;
for(i = 0; i < length; i++)
{
if(pUartStu->RxBuf_In == pUartStu->RxBuf_Out)
break;
count += 1;
buff[i] = pUartStu->RxBuf[(pUartStu->RxBuf_Out)];
pUartStu->RxBuf_Out = (pUartStu->RxBuf_Out + 1) & UART_BUF_LEN;
}
return count;
}
/****************************************************************************
* 函数名: WriteUart
* 功 能: 发送数据到指定的串口缓存区.
* 输 入: UartID-串口号,buff-需要发送的数据存放位置, length-需要发送的数据长度
* 输 出: 无
* 返 回: 无
*/
void WriteUart(unsigned char UartID,unsigned char *buff,unsigned int length)
{
unsigned int i;
UartStruct *pUartStu;
USART_TypeDef* USARTx;
unsigned char write;
if(UartID == COM1)
{
pUartStu = &Uart1Stu;
USARTx = USART1;
}
else if(UartID == COM2)
{
pUartStu = &Uart2Stu;
USARTx = USART2;
}
else if(UartID == COM3)
{
pUartStu = &Uart3Stu;
USARTx = USART3;
}
else if(UartID == COM4)
{
pUartStu = &Uart4Stu;
USARTx = UART4;
}
else if(UartID == COM6)
{
pUartStu = &Uart6Stu;
USARTx = USART6;
}
else
return;
if(pUartStu->TxBuf_In == pUartStu->TxBuf_Out)
{
write = 1;
}
else
{
write = 0;
}
for(i = 0; i < length; i++)
{
if(((pUartStu->TxBuf_In + 1) & UART_BUF_LEN) != pUartStu->TxBuf_Out)
{
pUartStu->TxBuf[(pUartStu->TxBuf_In)] = buff[i];
pUartStu->TxBuf_In = (pUartStu->TxBuf_In + 1) & UART_BUF_LEN;
}
else
break;
}
if(write)
{
USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);
}
}
void UartSend(unsigned char UartID,unsigned char *buff,unsigned int length)
{
unsigned int i;
unsigned int time;
USART_TypeDef* USARTx;
if(UartID == COM1)
USARTx = USART1;
else if(UartID == COM2)
USARTx = USART2;
else if(UartID == COM3)
USARTx = USART3;
else if(UartID == COM4)
USARTx = UART4;
else if(UartID == COM5)
USARTx = UART5;
else if(UartID == COM6)
USARTx = USART6;
else
return;
for(i = 0; i < length; i++)
{
/* 写一个字节到USART1 */
USART_SendData(USARTx, buff[i]);
/* 等待发送结束 */
time = 0;
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)
{
time++;
if(time > 100000)
{
break;
}
};
}
}
void ProcessDataFormUartCard()
{
unsigned char buff[64];
unsigned int len;
unsigned i;
len = ReadUart(COM6,buff,12);
if( len > 0)
{
for(i = 0; i < len; i++)
{
}
}
}
float getData(unsigned char *Res)
{
int x = 1;
int baiwei = Res[0] & 0xf;
int shiwei = Res[1] >> 4;
int gewei = Res[1] & 0xf;
int dian1 = Res[2] >> 4;
int dian2 = Res[2] & 0xf;
if ((Res[0] >> 4) == 1)
{
x = -1;
}
return x * ((float)baiwei * 100.0f + (float)shiwei * 10.0f + (float)gewei + (float)dian1 * 0.1f + (float)dian2 * 0.01f);
}
float _getData(unsigned char *Res)
{
int x = 1;
int gewei = Res[1] >> 4;
int dian1 = Res[1] & 0xf;
int dian2 = Res[2] >> 4;
int dian3 = Res[2] & 0xf;
if ((Res[0] >> 4) == 1)
{
x = -1;
}
return x * ((float)gewei + (float)dian1 * 0.1f + (float)dian2 * 0.01f + (float)dian3 * 0.001f);
}
void getdata(unsigned char Res)
{
static int offset = 0;
static unsigned char buffer[70];
static int step = 0;
buffer[offset++] = Res;
// Uart_Printf(COM1,"%02X",Res);
if (offset > 69)
offset = 0;
if (Res == 0x68 && step == 0)
{
// Uart_Printf(COM1,"step0\r\n");
step = 1;
}
else if (Res == 0x21 && step == 1)
{
// Uart_Printf(COM1,"step1\r\n");
step = 2;
}
else if (Res == 0x00 && step == 2)
{
// Uart_Printf(COM1,"step2\r\n");
step = 3;
}
else if (Res == 0x84 && step == 3)
{
// Uart_Printf(COM1,"step3\r\n");
step = 0;
offset = 0;
// Uart_Printf(COM1,"get one frame\r\n");
}
else
{
step = 0;
}
if (offset == 30)
{
unsigned char sum = 0xa5;
for (int i = 0; i < 29;)
{
sum += buffer[i];
i++;
}
if (sum == buffer[29])
{
imu_msg.angular_velocity_x = (getData(buffer + 18)) * 0.017453f; //Gx;//*0.017453f; //raw_msg->raw_angular_velocity.x - gyroscope_bias_["x"];
imu_msg.angular_velocity_y = (getData(buffer + 21)) * 0.017453f; //Gy;//*0.017453f; //raw_msg->raw_angular_velocity.y - gyroscope_bias_["y"];
imu_msg.angular_velocity_z = (getData(buffer + 24)) * 0.017453f; //*0.017453f; //raw_msg->raw_angular_velocity.z - gyroscope_bias_["z"];
imu_msg.linear_acceleration_x = (_getData(buffer + 9)); //raw_msg->raw_linear_acceleration.x - acceleration_bias_["x"];
imu_msg.linear_acceleration_y = (_getData(buffer + 12)); //raw_msg->raw_linear_acceleration.y - acceleration_bias_["y"];
imu_msg.linear_acceleration_z = (_getData(buffer + 15)); //raw_msg->raw_linear_acceleration.z - acceleration_bias_["z"];
imu_msg.angular_roll = getData(buffer) * 0.017453f;
imu_msg.angular_pitch = getData(buffer + 3) * 0.017453f;
imu_msg.angular_yaw = getData(buffer + 6) * 0.017453f;
// static u8 testCount = 0;
// if(testCount ++ > 15)
// {
// Uart_Printf(COM1,"imu_msg.angular_yaw = %f\r\n",imu_msg.angular_yaw*180.0f/PI);
// testCount = 0;
// }
}
}
}
void ProcessDataFormUartGoya()
{
unsigned char buff[180];
unsigned int len;
unsigned i;
len = ReadUart(COM3,buff,180);
//WriteUart(COM1,buff,len);
if( len > 0)
{
for(i = 0; i < len; i++)
{
getdata(buff[i]);
}
}
}
////////////////////////////////////////////////////////////////////////////////
/*********************************************************************************************************
** 函数名称: Uart_Printf
** 函数功能: 串口接收数据
** 入口参数: num 句柄 *buff 数据缓存
** 出口参数: 无
** 函数说明:
*********************************************************************************************************/
void Uart_Printf(unsigned char UartID, const char *fmt,...)
{
// va_list ap;
// char string[1024];
// va_start(ap,fmt);
// vsprintf(string,fmt,ap);
// WriteUart(UartID,(unsigned char *)string,strlen(string));
// va_end(ap);
}
//////////////////////////////////////////////////////////////////
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
//#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
// while((USART3->SR&0X40)==0);//循环发送,直到发送完毕
// USART3->DR = (u8) ch;
return ch;
}