bsp_can.c
19.1 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
/*********************************************************************************************************
* 模块名称 : CAN网络演示程序。
* 文件名称 : can_network.c
* 版 本 : V1.0
* 说 明 : 演示如何实现多个CAN节点(节点的程序相同)之间的通信。
*********************************************************************************************************/
#include "includes.h"
//CAN1?óê?RX0?D??ê1?ü
#define CAN1_RX0_INT_ENABLE 1 //0,2?ê1?ü;1,ê1?ü.
#define CAN2_RX0_INT_ENABLE 1
/* 定义全局变量 */
CANStruct can1Struct,can2Struct;
/* 仅允许本文件内调用的函数声明 */
void SendCanMsg(void);
/*
*********************************************************************************************************
* 函 数 名: can_Init
* 功能说明: 配置CAN硬件
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
u8 CAN1_Mode_Init(CanBaudRate CanBaudValue,u8 mode) // CAN1RX--PA11 CAN1TX--PA12
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN1_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能PORTA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化PA11,PA12
//引脚复用映射配置
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11复用为CAN1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12复用为CAN1
//CAN单元设置
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式
CAN_InitStructure.CAN_ABOM=ENABLE; //软件自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE;//睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
CAN_InitStructure.CAN_NART=DISABLE; //禁止报文自动传送
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的
CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定
CAN_InitStructure.CAN_Mode= mode; //模式设置
switch(CanBaudValue) //CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq
{
case BandRate_100kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 30;
break;
case BandRate_125kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 24;
break;
case BandRate_200kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 15;
break;
case BandRate_250kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 12;
break;
case BandRate_500kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 6;
break;
// case BandRate_1Mbps:
// CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
// CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
// CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
// CAN_InitStructure.CAN_Prescaler = 2;
// break;
default:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 6;
break;
}
// CAN_InitStructure.CAN_SJW=tsjw; //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
// CAN_InitStructure.CAN_BS1=tbs1; //Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
// CAN_InitStructure.CAN_BS2=tbs2;//Tbs2范围CAN_BS2_1tq ~ CAN_BS2_8tq
// CAN_InitStructure.CAN_Prescaler=brp; //分频系数(Fdiv)为brp+1
CAN_Init(CAN1, &CAN_InitStructure); // 初始化CAN1
//配置过滤器
CAN_FilterInitStructure.CAN_FilterNumber=0; //过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32位
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;////32位ID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //激活过滤器0
CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
#if CAN1_RX0_INT_ENABLE
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0消息挂号中断允许.
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 次优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
u8 CAN2_Mode_Init(CanBaudRate CanBaudValue,u8 mode) // CAN2RX---PB5 CAN2TX---PB6
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN2_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能PORTA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1|RCC_APB1Periph_CAN2, ENABLE);//使能CAN2时钟时,CAN1时钟必须开启!!
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5| GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化PA11,PA12
//引脚复用映射配置
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_CAN2); //GPIOA11复用为CAN1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_CAN2); //GPIOA12复用为CAN1
//CAN单元设置
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式
CAN_InitStructure.CAN_ABOM=ENABLE; //软件自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE;//睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
CAN_InitStructure.CAN_NART=DISABLE; //禁止报文自动传送
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的
CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定
CAN_InitStructure.CAN_Mode= mode; //模式设置
switch(CanBaudValue) //CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq
{
case BandRate_100kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 30;
break;
case BandRate_125kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 24;
break;
case BandRate_200kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 15;
break;
case BandRate_250kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 12;
break;
case BandRate_500kbps:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 6;
break;
// case BandRate_1Mbps:
// CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
// CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
// CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
// CAN_InitStructure.CAN_Prescaler = 2;
// break;
default:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 6;
break;
}
// CAN_InitStructure.CAN_SJW=tsjw; //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
// CAN_InitStructure.CAN_BS1=tbs1; //Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
// CAN_InitStructure.CAN_BS2=tbs2;//Tbs2范围CAN_BS2_1tq ~ CAN_BS2_8tq
// CAN_InitStructure.CAN_Prescaler=brp; //分频系数(Fdiv)为brp+1
CAN_Init(CAN2, &CAN_InitStructure); // 初始化CAN1
//配置过滤器
CAN_FilterInitStructure.CAN_FilterNumber=14; //CAN1滤波器号从0~13 CAN2滤波器号从14~27!!!
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32位
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;////32位ID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //激活过滤器0
CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
#if CAN2_RX0_INT_ENABLE
CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);//FIFO0消息挂号中断允许.
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 次优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
/*
*********************************************************************************************************
* 函 数 名: can_ISR
* 功能说明: CAN中断服务程序. 这个函数在 stm32f4xx_it.c中被调用
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
void CAN2_TX_IRQHandler(void)
{
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
CAN_ClearITPendingBit(CAN2,CAN_IT_TME);
if(CAN_GetITStatus(CAN2,CAN_IT_RQCP0) == SET)
{
CAN_ClearITPendingBit(CAN2,CAN_IT_RQCP0);
}
else if(CAN_GetITStatus(CAN2,CAN_IT_RQCP1) == SET)
{
CAN_ClearITPendingBit(CAN2,CAN_IT_RQCP1);
}
else if(CAN_GetITStatus(CAN2,CAN_IT_RQCP2) == SET)
{
CAN_ClearITPendingBit(CAN2,CAN_IT_RQCP2);
}
CAN_ITConfig(CAN2, CAN_IT_TME, DISABLE);
OSIntExit();
}
//CAN数据发送
u8 CAN2_Send_Msg(unsigned short int StdID,unsigned char *msg, unsigned char len,u16 time)
{
OS_ERR err;
u8 j = 20;
unsigned char mbox;
unsigned int i = 0;
CanTxMsg TxMessage;
TxMessage.StdId = StdID; // 标准标识符为0
TxMessage.ExtId = 0x12; // 设置扩展标示符(29位)
TxMessage.IDE = 0; // 使用扩展标识符
TxMessage.RTR = 0; // 消息类型为数据帧,一帧8位
TxMessage.DLC = len;
for(i = 0; i < len; i++)
{
TxMessage.Data[i] = msg[i];
}
while(1)
{
{
mbox = CAN_Transmit(CAN2, &TxMessage); //向CAN总线传输数据
}
if(mbox != CAN_TxStatus_NoMailBox)
{
while(CAN_TransmitStatus(CAN2, mbox) == CAN_TxStatus_Failed)
{
if(j-- < 1)
{
break;
}
}
break;
}
if(j-- < 1)
{
break;
}
}
OSTimeDly(time, OS_OPT_TIME_PERIODIC, &err);
return 0;
}
/*
*********************************************************************************************************
* 函 数 名: can_ISR
* 功能说明: CAN中断服务程序. 这个函数在 stm32f4xx_it.c中被调用
* 形 参:无
* 返 回 值: 无`z
*********************************************************************************************************
*/
//extern ElecHDFeeddback LElecHDFeeddback,RElecHDFeeddback;
int OneLeftState,OneRightState,TwoLeftState,TwoRightState,ThreeLeftState,ThreeRightState,FourLeftState,FourRightState;
int OneLeftData,OneRightData,TwoLeftData,TwoRightData,ThreeLeftData,ThreeRightData,FourLeftData,FourRightData;
float OneLeftMagBias,OneRightMagBias,TwoLeftMagBias,TwoRightMagBias,ThreeLeftMagBias,ThreeRightMagBias,FourLeftMagBias,FourRightMagBias;
void CAN1_RX0_IRQHandler(void)
{
int i = 0;
/*接收中断*/
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
CanRxMsg g_tCanRxMsg; /* 用于接收 */
if(CAN_GetITStatus(CAN1,CAN_IT_FMP0) != RESET)
{
CAN_Receive(CAN1, CAN_FIFO0, &g_tCanRxMsg);
if(g_tCanRxMsg.StdId == 0x181)//左行走
{
WheelDataProcess1(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x183)//右行走
{
WheelDataProcess3(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x182)//转向1
{
WheelDataProcess2(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x184)//转向2
{
WheelDataProcess4(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x185)//举升
{
WheelDataProcess5(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x381)//1
{
WheelSpeedProcess1(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x383)//3
{
WheelSpeedProcess3(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x482)//转向2
{
WheelAngleProcess2(&g_tCanRxMsg);
}
if(g_tCanRxMsg.StdId == 0x484)//转向4
{
WheelAngleProcess4(&g_tCanRxMsg);
}
}
g_tCanRxMsg.StdId = 0;
g_tCanRxMsg.ExtId = 0;
OSIntExit();
}
void CAN2_RX0_IRQHandler(void)
{
/*接收中断*/
int i = 0;
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
CanRxMsg g_tCanRxMsg; /* 用于接收 */
if(CAN_GetITStatus(CAN2,CAN_IT_FMP0) != RESET)
{
CAN_Receive(CAN2, CAN_FIFO0, &g_tCanRxMsg);
if(g_tCanRxMsg.StdId == 0x200)
{
agv.Public.Voltage = (g_tCanRxMsg.Data[0]<<8|g_tCanRxMsg.Data[1]);
}
if(g_tCanRxMsg.StdId == 0x201)
{
agv.Public.SOC = g_tCanRxMsg.Data[6]<<8|g_tCanRxMsg.Data[7];
}
if(g_tCanRxMsg.StdId == 0x202)
{
}
}
g_tCanRxMsg.StdId = 0;
g_tCanRxMsg.ExtId = 0;
OSIntExit();
}
void CAN1_TX_IRQHandler(void)
{
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
OSIntEnter();
CPU_CRITICAL_EXIT();
CAN_ClearITPendingBit(CAN1,CAN_IT_TME);
if(CAN_GetITStatus(CAN1,CAN_IT_RQCP0) == SET)
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP0);
}
else if(CAN_GetITStatus(CAN1,CAN_IT_RQCP1) == SET)
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP1);
}
else if(CAN_GetITStatus(CAN1,CAN_IT_RQCP2) == SET)
{
CAN_ClearITPendingBit(CAN1,CAN_IT_RQCP2);
}
CAN_ITConfig(CAN1, CAN_IT_TME, DISABLE);
OSIntExit();
}
//CAN数据发送
u8 CAN1_Send_Msg(unsigned short int StdID,unsigned char *msg, unsigned char len,u16 time)
{
OS_ERR err;
u8 j = 20;
unsigned char mbox;
unsigned int i = 0;
CanTxMsg TxMessage;
TxMessage.StdId = StdID; // 标准标识符为0
TxMessage.ExtId = 0x12; // 设置扩展标示符(29位)
TxMessage.IDE = 0; // 使用扩展标识符
TxMessage.RTR = 0; // 消息类型为数据帧,一帧8位
TxMessage.DLC = len;
for(i = 0; i < len; i++)
{
TxMessage.Data[i] = msg[i];
}
while(1)
{
{
mbox = CAN_Transmit(CAN1, &TxMessage); //向CAN总线传输数据
}
// printf("发送完毕\r\n");
if(mbox != CAN_TxStatus_NoMailBox)
{
while(CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed)
{
if(j-- < 1)
{
break;
}
}
break;
}
if(j-- < 1)
{
break;
}
}
OSTimeDly(time, OS_OPT_TIME_PERIODIC, &err);
return 0;
}
//CAN数据发送 标准帧
u8 CAN1_Send_Msg_Extend(unsigned int StdID,unsigned char *msg, unsigned char len,u16 time)
{
OS_ERR err;
u8 j = 20;
unsigned char mbox;
unsigned int i = 0;
CanTxMsg TxMessage;
// TxMessage.StdId = CYLINDER_FEEDBACK; // 标准标识符为0
TxMessage.ExtId = StdID; // 设置扩展标示符(29位)
TxMessage.IDE = CAN_ID_EXT; // 使用扩展标识符
TxMessage.RTR = CAN_RTR_DATA; // 消息类型为数据帧,一帧8位
TxMessage.DLC = len;
for(i = 0; i < len; i++)
{
TxMessage.Data[i] = msg[i];
}
while(1)
{
{
mbox = CAN_Transmit(CAN1, &TxMessage); //向CAN总线传输数据
}
if(mbox != CAN_TxStatus_NoMailBox)
{
while(CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed)
{
if(j-- < 1)
{
break;
}
}
break;
}
if(j-- < 1)
{
break;
}
}
OSTimeDly(time, OS_OPT_TIME_PERIODIC, &err);
return 0;
}
u8 CAN2_Send_Msg_Extend(unsigned int StdID,unsigned char *msg, unsigned char len,u16 time)
{
OS_ERR err;
u8 j = 20;
unsigned char mbox;
unsigned int i = 0;
CanTxMsg TxMessage;
// TxMessage.StdId = CYLINDER_FEEDBACK; // 标准标识符为0
TxMessage.ExtId = StdID; // 设置扩展标示符(29位)
TxMessage.IDE = CAN_ID_EXT; // 使用扩展标识符
TxMessage.RTR = CAN_RTR_DATA; // 消息类型为数据帧,一帧8位
TxMessage.DLC = len;
for(i = 0; i < len; i++)
{
TxMessage.Data[i] = msg[i];
}
while(1)
{
{
mbox = CAN_Transmit(CAN2, &TxMessage); //向CAN总线传输数据
}
if(mbox != CAN_TxStatus_NoMailBox)
{
while(CAN_TransmitStatus(CAN2, mbox) == CAN_TxStatus_Failed)
{
if(j-- < 1)
{
break;
}
}
break;
}
if(j-- < 1)
{
break;
}
}
OSTimeDly(time, OS_OPT_TIME_PERIODIC, &err);
return 0;
}
//CAN数据发送
u8 CAN_Send_Msg(unsigned short int StdID,unsigned char *msg, unsigned char len,u16 time,u8 CanSelect)
{
OS_ERR err;
u8 j = 20;
unsigned char mbox;
CanTxMsg TxMessage;
TxMessage.StdId = StdID; // 标准标识符为0
TxMessage.ExtId = 0x12; // 设置扩展标示符(29位)
TxMessage.IDE = 0; // 使用扩展标识符
TxMessage.RTR = 0; // 消息类型为数据帧,一帧8位
TxMessage.DLC = len;
for(unsigned int i = 0; i < len; i++)
{
TxMessage.Data[i] = msg[i];
}
while(1)
{
if(CanSelect == 1)
{
mbox = CAN_Transmit(CAN1, &TxMessage); //向CAN总线传输数据
if(mbox != CAN_TxStatus_NoMailBox)
{
while(CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed)
{
if(j-- < 1)
{
break;
}
}
break;
}
if(j-- < 1)
{
break;
}
}
else
{
mbox = CAN_Transmit(CAN2, &TxMessage); //向CAN总线传输数据
if(mbox != CAN_TxStatus_NoMailBox)
{
while(CAN_TransmitStatus(CAN2, mbox) == CAN_TxStatus_Failed)
{
if(j-- < 1)
{
break;
}
}
break;
}
if(j-- < 1)
{
break;
}
}
}
OSTimeDly(time, OS_OPT_TIME_PERIODIC, &err);
return 0;
}