00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pic24_all.h"
00031 #include <stdio.h>
00032
00033
00034 #if (NUM_ECAN_MODS >= 1)
00035
00036
00037
00038
00039
00040
00047 #ifndef ECAN_1TIME_CODE_DEFS
00048
00057 void formatStandardDataFrameECAN (ECANMSG* p_ecanmsg, uint16 u16_id, uint8 u8_len){
00058 p_ecanmsg->w0.IDE = 0;
00059 p_ecanmsg->w0.SRR = 0;
00060 p_ecanmsg->w0.SID = u16_id;
00061 p_ecanmsg->w1.EID17_6 = 0;
00062 p_ecanmsg->w2.EID5_0 = 0;
00063 p_ecanmsg->w2.RTR = 0;
00064 p_ecanmsg->w2.RB1 = 0;
00065 p_ecanmsg->w2.RB0 = 0;
00066 p_ecanmsg->w2.DLC = u8_len;
00067 }
00068
00069
00077 void formatExtendedDataFrameECAN (ECANMSG* p_ecanmsg, uint32 u32_id, uint8 u8_len){
00078
00079 p_ecanmsg->w0.IDE = 1;
00080 p_ecanmsg->w0.SRR = 0;
00081 p_ecanmsg->w0.SID = (u32_id >> 18) & 0x7FF;
00082 p_ecanmsg->w1.EID17_6 = (u32_id >> 6) & 0xFFF;
00083 p_ecanmsg->w2.EID5_0 = u32_id & 0x3F;
00084 p_ecanmsg->w2.RTR = 0;
00085 p_ecanmsg->w2.RB1 = 0;
00086 p_ecanmsg->w2.RB0 = 0;
00087 p_ecanmsg->w2.DLC = u8_len;
00088 }
00089
00096 uint32 getIdExtendedDataFrameECAN (ECANMSG* p_ecanmsg){
00097 uint32 u32_id, u32_tmp;
00098 u32_tmp = p_ecanmsg->w0.SID;
00099 u32_id = u32_tmp << 18;
00100 u32_tmp = p_ecanmsg->w1.EID17_6;
00101 u32_id = u32_id | (u32_tmp << 6) | p_ecanmsg->w2.EID5_0;
00102 return u32_id;
00103 }
00104
00105 #define ECAN_1TIME_CODE_DEFS
00106 #endif
00107
00108
00109
00115 void clrRxFullFlagECAN1(uint8 u8_bufNum) {
00116 u8_bufNum &= 0x1F;
00117 if (u8_bufNum > 15) {
00118 u8_bufNum = u8_bufNum - 16;
00119 C1RXFUL2 = C1RXFUL1 & ~(1<<u8_bufNum);
00120 } else {
00121 C1RXFUL1 = C1RXFUL1 & ~(1<<u8_bufNum);
00122 }
00123 }
00124
00130 uint8 getRxFullFlagECAN1(uint8 u8_bufNum) {
00131 u8_bufNum &= 0x1F;
00132 if (u8_bufNum > 15) {
00133 u8_bufNum = u8_bufNum - 16;
00134 return(C1RXFUL1 & (1<<u8_bufNum));
00135 } else {
00136 return(C1RXFUL1 & (1<<u8_bufNum));
00137 }
00138 }
00139
00143 void clrRxFullOvfFlagsECAN1(void) {
00144 C1RXFUL1=0x0000;
00145 C1RXOVF1=0x0000;
00146 }
00147
00154 void configTxRxBufferECAN1(uint8 u8_bufNum, uint8 u8_type, uint8 u8_priority ) {
00155 u8_bufNum &= 0x07;
00156 switch (u8_bufNum) {
00157 case 0: C1TR01CONbits.TXEN0 = u8_type;
00158 C1TR01CONbits.TX0PRI = u8_priority;
00159 break;
00160 case 1: C1TR01CONbits.TXEN1 = u8_type;
00161 C1TR01CONbits.TX1PRI = u8_priority;
00162 break;
00163 case 2: C1TR23CONbits.TXEN2 = u8_type;
00164 C1TR23CONbits.TX2PRI = u8_priority;
00165 break;
00166 case 3: C1TR23CONbits.TXEN3 = u8_type;
00167 C1TR23CONbits.TX3PRI = u8_priority;
00168 break;
00169 case 4: C1TR45CONbits.TXEN4 = u8_type;
00170 C1TR45CONbits.TX4PRI = u8_priority;
00171 break;
00172 case 5: C1TR45CONbits.TXEN5 = u8_type;
00173 C1TR45CONbits.TX5PRI = u8_priority;
00174 break;
00175 case 6: C1TR67CONbits.TXEN6 = u8_type;
00176 C1TR67CONbits.TX6PRI = u8_priority;
00177 break;
00178 default: C1TR67CONbits.TXEN7 = u8_type;
00179 C1TR67CONbits.TX7PRI = u8_priority;
00180 break;
00181 }
00182 }
00183
00188 void startTxECAN1(uint8 u8_bufNum) {
00189 u8_bufNum &= 0x07;
00190 switch (u8_bufNum) {
00191 case 0: C1TR01CONbits.TXREQ0 = 1; break;
00192 case 1: C1TR01CONbits.TXREQ1 = 1; break;
00193 case 2: C1TR23CONbits.TXREQ2 = 1; break;
00194 case 3: C1TR23CONbits.TXREQ3 = 1; break;
00195 case 4: C1TR45CONbits.TXREQ4 = 1;; break;
00196 case 5: C1TR45CONbits.TXREQ5 = 1; break;
00197 case 6: C1TR67CONbits.TXREQ6 = 1; break;
00198 default: C1TR67CONbits.TXREQ7 = 1; break;
00199 }
00200 }
00201
00206 uint8 getTxInProgressECAN1(uint8 u8_bufNum) {
00207 u8_bufNum &= 0x07;
00208 switch (u8_bufNum) {
00209 case 0: return(C1TR01CONbits.TXREQ0);
00210 case 1: return(C1TR01CONbits.TXREQ1);
00211 case 2: return(C1TR23CONbits.TXREQ2);
00212 case 3: return(C1TR23CONbits.TXREQ3);
00213 case 4: return(C1TR45CONbits.TXREQ4);
00214 case 5: return(C1TR45CONbits.TXREQ5);
00215 case 6: return(C1TR67CONbits.TXREQ6);
00216 default: return(C1TR67CONbits.TXREQ7);
00217 }
00218 }
00219
00220
00221
00222
00231 void configRxFilterECAN1(uint8 u8_filtNum, uint32 u32_id, uint8 u8_idType, uint8 u8_bufnum, uint8 u8_maskReg) {
00232 uint16 *pu16_CxRXFySID,*pu16_CxRXFyEID, *pu16_CxFMSKSEL1, *pu16_CxBUFPNT1;
00233 uint16 u16_sid;
00234 uint16 u16_eid15_0;
00235 uint16 u16_eid17_16;
00236 uint16 u16_mask;
00237 uint8 u8_startPos;
00238
00239 u8_filtNum &= 0xF;
00240 u8_bufnum &= 0xF;
00241 u8_maskReg &= 0x07;
00242
00243 pu16_CxRXFySID = (uint16*) &C1RXF0SID + (u8_filtNum << 1);
00244 pu16_CxRXFyEID = pu16_CxRXFySID + 1;
00245 pu16_CxFMSKSEL1 = (uint16*) &C1FMSKSEL1 + (u8_filtNum >> 3);
00246 pu16_CxBUFPNT1 = (uint16*) &C1BUFPNT1 + (u8_filtNum >> 2);
00247
00248 C1CTRL1bits.WIN=1;
00249
00250
00251 if(u8_idType) {
00252 u16_sid = (u32_id >> 18) & 0x7FF;
00253 u16_eid17_16 = (u32_id >>16) & 0x3;
00254 u16_eid15_0 = u32_id & 0xFFFF;
00255 *pu16_CxRXFySID =(u16_sid <<5) | ECAN_MATCH_EID | u16_eid17_16;
00256 *pu16_CxRXFyEID = u16_eid15_0;
00257 } else {
00258 u16_sid = u32_id & 0x7FF;
00259 *pu16_CxRXFySID = u16_sid <<5;
00260 *pu16_CxRXFyEID = 0;
00261 }
00262
00263
00264 u8_startPos = 4 * (u8_filtNum & 0x3);
00265 u16_mask = ~ ( 0xF << u8_startPos);
00266 *pu16_CxBUFPNT1 = (*pu16_CxBUFPNT1 & u16_mask) | (u8_bufnum << u8_startPos);
00267
00268
00269 u8_startPos = 2 * (u8_filtNum & 0x7);
00270 u16_mask = ~ ( 0x3 << u8_startPos);
00271 *pu16_CxFMSKSEL1 = (*pu16_CxFMSKSEL1 & u16_mask) | (u8_maskReg << u8_startPos);
00272
00273 C1FEN1 = C1FEN1 | (1 << u8_filtNum) ;
00274
00275 C1CTRL1bits.WIN=0;
00276
00277 }
00278
00289 void configRxMaskECAN1(uint8 u8_maskNum, uint32 u32_idMask, uint8 u8_idType, uint8 u8_matchType){
00290 uint16 *pu16_CxRXMySID,*pu16_CxRXMyEID;
00291 uint16 u16_msid;
00292 uint16 u16_meid15_0;
00293 uint16 u16_meid17_16;
00294
00295 pu16_CxRXMySID =(uint16*) &C1RXM0SID + (u8_maskNum << 1);
00296 pu16_CxRXMyEID = pu16_CxRXMySID + 1;
00297
00298 C1CTRL1bits.WIN=1;
00299
00300
00301 if(u8_idType) {
00302 u16_msid = (u32_idMask >> 18) & 0x7FF;
00303 u16_meid17_16 = (u32_idMask >>16) & 0x3;
00304 u16_meid15_0 = u32_idMask & 0xFFFF;
00305 if (u8_matchType) *pu16_CxRXMySID =(u16_msid <<5) | ECAN_MATCH_EID | u16_meid17_16;
00306 else *pu16_CxRXMySID =(u16_msid <<5) | u16_meid17_16;
00307 *pu16_CxRXMyEID = u16_meid15_0;
00308 } else {
00309 u16_msid = u32_idMask & 0x7FF;
00310 if (u8_matchType) *pu16_CxRXMySID = (u16_msid <<5) | ECAN_MATCH_EID ;
00311 else *pu16_CxRXMySID = (u16_msid <<5);
00312 *pu16_CxRXMyEID = 0;
00313 }
00314 C1CTRL1bits.WIN=0;
00315 }
00316
00317 #endif // #if (NUM_ECAN_MODS >= ${x})
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 #include "pic24_all.h"
00361 #include <stdio.h>
00362
00363
00364 #if (NUM_ECAN_MODS >= 2)
00365
00366
00367
00368
00369
00370
00377 #ifndef ECAN_1TIME_CODE_DEFS
00378
00387 void formatStandardDataFrameECAN (ECANMSG* p_ecanmsg, uint16 u16_id, uint8 u8_len){
00388 p_ecanmsg->w0.IDE = 0;
00389 p_ecanmsg->w0.SRR = 0;
00390 p_ecanmsg->w0.SID = u16_id;
00391 p_ecanmsg->w1.EID17_6 = 0;
00392 p_ecanmsg->w2.EID5_0 = 0;
00393 p_ecanmsg->w2.RTR = 0;
00394 p_ecanmsg->w2.RB1 = 0;
00395 p_ecanmsg->w2.RB0 = 0;
00396 p_ecanmsg->w2.DLC = u8_len;
00397 }
00398
00399
00407 void formatExtendedDataFrameECAN (ECANMSG* p_ecanmsg, uint32 u32_id, uint8 u8_len){
00408
00409 p_ecanmsg->w0.IDE = 1;
00410 p_ecanmsg->w0.SRR = 0;
00411 p_ecanmsg->w0.SID = (u32_id >> 18) & 0x7FF;
00412 p_ecanmsg->w1.EID17_6 = (u32_id >> 6) & 0xFFF;
00413 p_ecanmsg->w2.EID5_0 = u32_id & 0x3F;
00414 p_ecanmsg->w2.RTR = 0;
00415 p_ecanmsg->w2.RB1 = 0;
00416 p_ecanmsg->w2.RB0 = 0;
00417 p_ecanmsg->w2.DLC = u8_len;
00418 }
00419
00426 uint32 getIdExtendedDataFrameECAN (ECANMSG* p_ecanmsg){
00427 uint32 u32_id, u32_tmp;
00428 u32_tmp = p_ecanmsg->w0.SID;
00429 u32_id = u32_tmp << 18;
00430 u32_tmp = p_ecanmsg->w1.EID17_6;
00431 u32_id = u32_id | (u32_tmp << 6) | p_ecanmsg->w2.EID5_0;
00432 return u32_id;
00433 }
00434
00435 #define ECAN_1TIME_CODE_DEFS
00436 #endif
00437
00438
00439
00445 void clrRxFullFlagECAN2(uint8 u8_bufNum) {
00446 u8_bufNum &= 0x1F;
00447 if (u8_bufNum > 15) {
00448 u8_bufNum = u8_bufNum - 16;
00449 C2RXFUL2 = C2RXFUL1 & ~(1<<u8_bufNum);
00450 } else {
00451 C2RXFUL1 = C2RXFUL1 & ~(1<<u8_bufNum);
00452 }
00453 }
00454
00460 uint8 getRxFullFlagECAN2(uint8 u8_bufNum) {
00461 u8_bufNum &= 0x1F;
00462 if (u8_bufNum > 15) {
00463 u8_bufNum = u8_bufNum - 16;
00464 return(C2RXFUL1 & (1<<u8_bufNum));
00465 } else {
00466 return(C2RXFUL1 & (1<<u8_bufNum));
00467 }
00468 }
00469
00473 void clrRxFullOvfFlagsECAN2(void) {
00474 C2RXFUL1=0x0000;
00475 C2RXOVF1=0x0000;
00476 }
00477
00484 void configTxRxBufferECAN2(uint8 u8_bufNum, uint8 u8_type, uint8 u8_priority ) {
00485 u8_bufNum &= 0x07;
00486 switch (u8_bufNum) {
00487 case 0: C2TR01CONbits.TXEN0 = u8_type;
00488 C2TR01CONbits.TX0PRI = u8_priority;
00489 break;
00490 case 1: C2TR01CONbits.TXEN1 = u8_type;
00491 C2TR01CONbits.TX1PRI = u8_priority;
00492 break;
00493 case 2: C2TR23CONbits.TXEN2 = u8_type;
00494 C2TR23CONbits.TX2PRI = u8_priority;
00495 break;
00496 case 3: C2TR23CONbits.TXEN3 = u8_type;
00497 C2TR23CONbits.TX3PRI = u8_priority;
00498 break;
00499 case 4: C2TR45CONbits.TXEN4 = u8_type;
00500 C2TR45CONbits.TX4PRI = u8_priority;
00501 break;
00502 case 5: C2TR45CONbits.TXEN5 = u8_type;
00503 C2TR45CONbits.TX5PRI = u8_priority;
00504 break;
00505 case 6: C2TR67CONbits.TXEN6 = u8_type;
00506 C2TR67CONbits.TX6PRI = u8_priority;
00507 break;
00508 default: C2TR67CONbits.TXEN7 = u8_type;
00509 C2TR67CONbits.TX7PRI = u8_priority;
00510 break;
00511 }
00512 }
00513
00518 void startTxECAN2(uint8 u8_bufNum) {
00519 u8_bufNum &= 0x07;
00520 switch (u8_bufNum) {
00521 case 0: C2TR01CONbits.TXREQ0 = 1; break;
00522 case 1: C2TR01CONbits.TXREQ1 = 1; break;
00523 case 2: C2TR23CONbits.TXREQ2 = 1; break;
00524 case 3: C2TR23CONbits.TXREQ3 = 1; break;
00525 case 4: C2TR45CONbits.TXREQ4 = 1;; break;
00526 case 5: C2TR45CONbits.TXREQ5 = 1; break;
00527 case 6: C2TR67CONbits.TXREQ6 = 1; break;
00528 default: C2TR67CONbits.TXREQ7 = 1; break;
00529 }
00530 }
00531
00536 uint8 getTxInProgressECAN2(uint8 u8_bufNum) {
00537 u8_bufNum &= 0x07;
00538 switch (u8_bufNum) {
00539 case 0: return(C2TR01CONbits.TXREQ0);
00540 case 1: return(C2TR01CONbits.TXREQ1);
00541 case 2: return(C2TR23CONbits.TXREQ2);
00542 case 3: return(C2TR23CONbits.TXREQ3);
00543 case 4: return(C2TR45CONbits.TXREQ4);
00544 case 5: return(C2TR45CONbits.TXREQ5);
00545 case 6: return(C2TR67CONbits.TXREQ6);
00546 default: return(C2TR67CONbits.TXREQ7);
00547 }
00548 }
00549
00550
00551
00552
00561 void configRxFilterECAN2(uint8 u8_filtNum, uint32 u32_id, uint8 u8_idType, uint8 u8_bufnum, uint8 u8_maskReg) {
00562 uint16 *pu16_CxRXFySID,*pu16_CxRXFyEID, *pu16_CxFMSKSEL1, *pu16_CxBUFPNT1;
00563 uint16 u16_sid;
00564 uint16 u16_eid15_0;
00565 uint16 u16_eid17_16;
00566 uint16 u16_mask;
00567 uint8 u8_startPos;
00568
00569 u8_filtNum &= 0xF;
00570 u8_bufnum &= 0xF;
00571 u8_maskReg &= 0x07;
00572
00573 pu16_CxRXFySID = (uint16*) &C2RXF0SID + (u8_filtNum << 1);
00574 pu16_CxRXFyEID = pu16_CxRXFySID + 1;
00575 pu16_CxFMSKSEL1 = (uint16*) &C2FMSKSEL1 + (u8_filtNum >> 3);
00576 pu16_CxBUFPNT1 = (uint16*) &C2BUFPNT1 + (u8_filtNum >> 2);
00577
00578 C2CTRL1bits.WIN=1;
00579
00580
00581 if(u8_idType) {
00582 u16_sid = (u32_id >> 18) & 0x7FF;
00583 u16_eid17_16 = (u32_id >>16) & 0x3;
00584 u16_eid15_0 = u32_id & 0xFFFF;
00585 *pu16_CxRXFySID =(u16_sid <<5) | ECAN_MATCH_EID | u16_eid17_16;
00586 *pu16_CxRXFyEID = u16_eid15_0;
00587 } else {
00588 u16_sid = u32_id & 0x7FF;
00589 *pu16_CxRXFySID = u16_sid <<5;
00590 *pu16_CxRXFyEID = 0;
00591 }
00592
00593
00594 u8_startPos = 4 * (u8_filtNum & 0x3);
00595 u16_mask = ~ ( 0xF << u8_startPos);
00596 *pu16_CxBUFPNT1 = (*pu16_CxBUFPNT1 & u16_mask) | (u8_bufnum << u8_startPos);
00597
00598
00599 u8_startPos = 2 * (u8_filtNum & 0x7);
00600 u16_mask = ~ ( 0x3 << u8_startPos);
00601 *pu16_CxFMSKSEL1 = (*pu16_CxFMSKSEL1 & u16_mask) | (u8_maskReg << u8_startPos);
00602
00603 C2FEN1 = C2FEN1 | (1 << u8_filtNum) ;
00604
00605 C2CTRL1bits.WIN=0;
00606
00607 }
00608
00619 void configRxMaskECAN2(uint8 u8_maskNum, uint32 u32_idMask, uint8 u8_idType, uint8 u8_matchType){
00620 uint16 *pu16_CxRXMySID,*pu16_CxRXMyEID;
00621 uint16 u16_msid;
00622 uint16 u16_meid15_0;
00623 uint16 u16_meid17_16;
00624
00625 pu16_CxRXMySID =(uint16*) &C2RXM0SID + (u8_maskNum << 1);
00626 pu16_CxRXMyEID = pu16_CxRXMySID + 1;
00627
00628 C2CTRL1bits.WIN=1;
00629
00630
00631 if(u8_idType) {
00632 u16_msid = (u32_idMask >> 18) & 0x7FF;
00633 u16_meid17_16 = (u32_idMask >>16) & 0x3;
00634 u16_meid15_0 = u32_idMask & 0xFFFF;
00635 if (u8_matchType) *pu16_CxRXMySID =(u16_msid <<5) | ECAN_MATCH_EID | u16_meid17_16;
00636 else *pu16_CxRXMySID =(u16_msid <<5) | u16_meid17_16;
00637 *pu16_CxRXMyEID = u16_meid15_0;
00638 } else {
00639 u16_msid = u32_idMask & 0x7FF;
00640 if (u8_matchType) *pu16_CxRXMySID = (u16_msid <<5) | ECAN_MATCH_EID ;
00641 else *pu16_CxRXMySID = (u16_msid <<5);
00642 *pu16_CxRXMyEID = 0;
00643 }
00644 C2CTRL1bits.WIN=0;
00645 }
00646
00647 #endif // #if (NUM_ECAN_MODS >= ${x})
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660