33 #ifdef __BUILD_WITH_EXAMPLE__
34 #include "lpc17xx_libcfg.h"
51 uint16_t CANAF_FullCAN_cnt = 0;
52 uint16_t CANAF_std_cnt = 0;
53 uint16_t CANAF_gstd_cnt = 0;
54 uint16_t CANAF_ext_cnt = 0;
55 uint16_t CANAF_gext_cnt = 0;
76 uint8_t NT, TSEG1 = 0, TSEG2 = 0;
89 result = CANPclk / baudrate;
95 for(NT=24;NT>0;NT=NT-2)
99 BRP = result / NT - 1;
102 TSEG1 = NT -(NT/3) - 1;
112 CANx->
BTR = (TSEG2<<20)|(TSEG1<<16)|(3<<14)|BRP;
134 volatile uint32_t temp;
158 CANx->
CMR = (1<<1)|(1<<2)|(1<<3);
168 for (i = 0; i < 512; i++) {
180 can_SetBaudrate (CANx, baudrate);
221 uint16_t SID, SID_temp,i, count = 0;
222 uint32_t EID, EID_temp, entry, buf;
223 uint16_t lowerSID, upperSID;
224 uint32_t lowerEID, upperEID;
251 if((CANAF_FullCAN_cnt & 0x00000001)==0)
256 SID_temp = (buf & 0x000003FF);
262 entry = (ctrl1<<29)|(dis1<<28)|(SID<<16)|(1<<27);
270 SID_temp = (buf & 0x03FF0000)>>16;
275 entry = (ctrl1<<13)|(dis1<<12)|(SID<<0)|(1<<11);
304 if((CANAF_std_cnt & 0x00000001)==0)
306 if(CANAF_std_cnt !=0 )
309 SID_temp = (buf & 0x00000FFF);
315 entry = (ctrl1<<29)|(dis1<<28)|(SID<<16);
323 SID_temp = (buf & 0x0FFF0000)>>16;
328 entry = (ctrl1<<13)|(dis1<<12)|(SID<<0);
363 if(CANAF_gstd_cnt!=0)
366 SID_temp = buf & 0x00000FFF;
367 if(SID_temp > lowerSID)
372 entry = (ctrl1 << 29)|(dis1 << 28)|(lowerSID << 16)| \
373 (ctrl2 << 13)|(dis2 << 12)|(upperSID << 0);
398 if(CANAF_ext_cnt != 0)
401 EID_temp = buf & 0x0FFFFFFF;
407 entry = (ctrl1 << 29)|(EID << 0);
436 if(CANAF_gext_cnt != 0)
439 EID_temp = buf & 0x0FFFFFFF;
440 if(EID_temp > lowerEID)
445 entry = (ctrl1 << 29)|(lowerEID << 0);
447 entry = (ctrl2 << 29)|(upperEID << 0);
454 LPC_CANAF->SFF_sa = ((CANAF_FullCAN_cnt + 1)>>1)<<2;
487 uint32_t buf0=0, buf1=0;
488 int16_t cnt1=0, cnt2=0, bound1=0, total=0;
504 total =((CANAF_FullCAN_cnt+1)>>1)+ CANAF_FullCAN_cnt*3 +((CANAF_std_cnt + 1) >> 1)+ \
505 CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
521 if ((CANAF_std_cnt & 0x0001) == 0)
523 cnt1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);
524 bound1 = total - cnt1;
534 if (CANAF_std_cnt == 0)
536 cnt2 = (CANAF_FullCAN_cnt + 1)>>1;
540 else if (CANAF_std_cnt == 1)
542 cnt2 = (CANAF_FullCAN_cnt + 1)>>1;
556 cnt1 = (CANAF_FullCAN_cnt+1)>>1;
557 cnt2 = CANAF_std_cnt;
558 bound1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);
559 while (cnt1 < bound1)
583 if ((CANAF_std_cnt & 0x0001) == 0)
596 if ((cnt2 & 0x0001) == 0)
599 buf1 = (id << 16) | (buf0 >> 16);
604 buf1 = (buf0 & 0xFFFF0000) |
id;
607 bound1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1)-1;
609 while (cnt1 < bound1)
617 if ((CANAF_std_cnt & 0x0001) == 0)
638 cnt1 = ((CANAF_FullCAN_cnt+1)>>1)+(((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt);
640 while (cnt2 < CANAF_ext_cnt)
658 while (cnt2 < bound1)
670 if(CANAF_FullCAN_cnt == 0)
696 uint32_t buf0=0, buf1=0, buf2=0;
697 uint32_t tmp0=0, tmp1=0, tmp2=0;
698 int16_t cnt1=0, cnt2=0, bound1=0, total=0;
712 total =((CANAF_FullCAN_cnt+1)>>1)+ CANAF_FullCAN_cnt*3 +((CANAF_std_cnt + 1) >> 1)+ \
713 CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
715 if ((total >=508)||(CANAF_FullCAN_cnt>=64)){
724 id |= (ctrl0 << 13) | (1 << 11);
728 if (((CANAF_FullCAN_cnt & 0x0001) == 0)&&(total!=0))
731 cnt1 = (CANAF_FullCAN_cnt >> 1);
743 if (CANAF_FullCAN_cnt == 0)
748 else if (CANAF_FullCAN_cnt == 1)
764 cnt2 = CANAF_FullCAN_cnt;
765 bound1 = (CANAF_FullCAN_cnt - 1) >> 1;
766 while (cnt1 <= bound1)
790 if ((CANAF_FullCAN_cnt & 0x0001) == 0)
803 if ((cnt2 & 0x0001) == 0)
806 buf1 = (id << 16) | (buf0 >> 16);
811 buf1 = (buf0 & 0xFFFF0000) |
id;
814 bound1 = CANAF_FullCAN_cnt >> 1;
816 while (cnt1 < bound1)
824 if ((CANAF_FullCAN_cnt & 0x0001) == 0)
833 bound1 = CANAF_FullCAN_cnt - cnt2;
834 cnt1 = total - (CANAF_FullCAN_cnt)*3 + cnt2*3 + 1;
883 uint32_t buf0, buf1, entry1, entry2, LID,UID;
884 int16_t cnt1, bound1, total;
899 total =((CANAF_FullCAN_cnt+1)>>1)+ CANAF_FullCAN_cnt*3 +((CANAF_std_cnt + 1) >> 1)+ \
900 CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
914 entry1 = (tmp << 29)|(lowerID << 16)|(tmp << 13)|(upperID << 0);
915 cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1);
918 if(CANAF_gstd_cnt == 0)
925 bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt;
929 LID = (buf0 >> 16)&0x7FF;
937 else if (lowerID >= UID)
953 bound1 = total - cnt1;
976 lowerID &= 0x1FFFFFFF;
977 upperID &= 0x1FFFFFFF;
978 entry1 = (tmp << 29)|(lowerID << 0);
979 entry2 = (tmp << 29)|(upperID << 0);
981 cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt + CANAF_ext_cnt;
983 if(CANAF_gext_cnt == 0)
991 bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt \
992 + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
997 LID = buf0 & 0x1FFFFFFF;
998 UID = buf1 & 0x1FFFFFFF;
1006 else if (lowerID >= UID)
1023 bound1 = total - cnt1 + 1;
1061 uint16_t cnt, bound, total;
1062 uint32_t buf0, buf1;
1069 total = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt + 1) >> 1) + \
1070 CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
1076 if((CANAF_FullCAN_cnt==0)||(position >= CANAF_FullCAN_cnt))
1082 cnt = position >> 1;
1084 bound = (CANAF_FullCAN_cnt - position -1)>>1;
1085 if((position & 0x0001) == 0)
1102 LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000)|(buf1 >> 16);
1108 if((CANAF_FullCAN_cnt & 0x0001) == 0)
1110 if((position & 0x0001)==0)
1118 cnt = (CANAF_FullCAN_cnt + 1)>>1;
1119 bound = total + CANAF_FullCAN_cnt * 3;
1133 CANAF_FullCAN_cnt--;
1137 cnt = total + position * 3;
1138 bound = (CANAF_FullCAN_cnt - position + 1) * 3;
1154 if((CANAF_std_cnt==0)||(position >= CANAF_std_cnt))
1160 cnt = ((CANAF_FullCAN_cnt+1)>>1)+ (position >> 1);
1162 bound = (CANAF_std_cnt - position - 1)>>1;
1163 if((position & 0x0001) == 0)
1180 LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000)|(buf1 >> 16);
1186 if((CANAF_std_cnt & 0x0001) == 0)
1188 if((position & 0x0001)==0)
1196 cnt = ((CANAF_FullCAN_cnt + 1)>>1) + ((CANAF_std_cnt + 1) >> 1);
1197 bound = total + CANAF_FullCAN_cnt * 3;
1217 if((CANAF_gstd_cnt==0)||(position >= CANAF_gstd_cnt))
1223 cnt = ((CANAF_FullCAN_cnt + 1)>>1) + ((CANAF_std_cnt + 1) >> 1)+ position + 1;
1224 bound = total + CANAF_FullCAN_cnt * 3;
1242 if((CANAF_ext_cnt==0)||(position >= CANAF_ext_cnt))
1248 cnt = ((CANAF_FullCAN_cnt + 1)>>1) + ((CANAF_std_cnt + 1) >> 1)+ CANAF_gstd_cnt + position + 1;
1249 bound = total + CANAF_FullCAN_cnt * 3;
1265 if((CANAF_gext_cnt==0)||(position >= CANAF_gext_cnt))
1271 cnt = total - (CANAF_gext_cnt<<1) + (position<<1);
1272 bound = total + CANAF_FullCAN_cnt * 3;
1316 if ((CANx->
SR & 0x00000004)>>2)
1321 CANx->
TFI1 &= ~0x000F0000;
1322 CANx->
TFI1 |= (CAN_Msg->
len)<<16;
1325 CANx->
TFI1 |= (1<<30);
1329 CANx->
TFI1 &= ~(1<<30);
1333 CANx->
TFI1 |= (1<<31);
1337 CANx->
TFI1 &= ~(1<<31);
1341 CANx->
TID1 = CAN_Msg->
id;
1344 data = (CAN_Msg->
dataA[0])|(((CAN_Msg->
dataA[1]))<<8)|((CAN_Msg->
dataA[2])<<16)|((CAN_Msg->
dataA[3])<<24);
1348 data = (CAN_Msg->
dataB[0])|(((CAN_Msg->
dataB[1]))<<8)|((CAN_Msg->
dataB[2])<<16)|((CAN_Msg->
dataB[3])<<24);
1356 else if((CANx->
SR & 0x00000004)>>10)
1361 CANx->
TFI2 &= ~0x000F0000;
1362 CANx->
TFI2 |= (CAN_Msg->
len)<<16;
1365 CANx->
TFI2 |= (1<<30);
1369 CANx->
TFI2 &= ~(1<<30);
1373 CANx->
TFI2 |= (1<<31);
1377 CANx->
TFI2 &= ~(1<<31);
1381 CANx->
TID2 = CAN_Msg->
id;
1384 data = (CAN_Msg->
dataA[0])|(((CAN_Msg->
dataA[1]))<<8)|((CAN_Msg->
dataA[2])<<16)|((CAN_Msg->
dataA[3])<<24);
1388 data = (CAN_Msg->
dataB[0])|(((CAN_Msg->
dataB[1]))<<8)|((CAN_Msg->
dataB[2])<<16)|((CAN_Msg->
dataB[3])<<24);
1396 else if ((CANx->
SR & 0x00000004)>>18)
1401 CANx->
TFI3 &= ~0x000F0000;
1402 CANx->
TFI3 |= (CAN_Msg->
len)<<16;
1405 CANx->
TFI3 |= (1<<30);
1409 CANx->
TFI3 &= ~(1<<30);
1413 CANx->
TFI3 |= (1<<31);
1417 CANx->
TFI3 &= ~(1<<31);
1421 CANx->
TID3 = CAN_Msg->
id;
1424 data = (CAN_Msg->
dataA[0])|(((CAN_Msg->
dataA[1]))<<8)|((CAN_Msg->
dataA[2])<<16)|((CAN_Msg->
dataA[3])<<24);
1428 data = (CAN_Msg->
dataB[0])|(((CAN_Msg->
dataB[1]))<<8)|((CAN_Msg->
dataB[2])<<16)|((CAN_Msg->
dataB[3])<<24);
1459 if((CANx->
SR &0x00000001))
1463 CAN_Msg->
format = (uint8_t)(((CANx->
RFS) & 0x80000000)>>31);
1464 CAN_Msg->
type = (uint8_t)(((CANx->
RFS) & 0x40000000)>>30);
1465 CAN_Msg->
len = (uint8_t)(((CANx->
RFS) & 0x000F0000)>>16);
1469 CAN_Msg->
id = CANx->
RID;
1476 *((uint8_t *) &CAN_Msg->
dataA[0])= data & 0x000000FF;
1477 *((uint8_t *) &CAN_Msg->
dataA[1])= (data & 0x0000FF00)>>8;;
1478 *((uint8_t *) &CAN_Msg->
dataA[2])= (data & 0x00FF0000)>>16;
1479 *((uint8_t *) &CAN_Msg->
dataA[3])= (data & 0xFF000000)>>24;
1483 *((uint8_t *) &CAN_Msg->
dataB[0])= data & 0x000000FF;
1484 *((uint8_t *) &CAN_Msg->
dataB[1])= (data & 0x0000FF00)>>8;
1485 *((uint8_t *) &CAN_Msg->
dataB[2])= (data & 0x00FF0000)>>16;
1486 *((uint8_t *) &CAN_Msg->
dataB[3])= (data & 0xFF000000)>>24;
1518 uint32_t *pSrc, data;
1519 uint32_t interrut_word, msg_idx, test_bit, head_idx, tail_idx;
1538 if (interrut_word != 0)
1542 for (msg_idx = head_idx; msg_idx <= tail_idx; msg_idx++)
1544 test_bit = interrut_word & 0x1;
1545 interrut_word = interrut_word >> 1;
1552 if ((*pSrc & 0x03000000L) == 0x03000000L)
1555 *pSrc &= 0xFCFFFFFF;
1561 *((uint8_t *) &CAN_Msg->
dataA[0])= data & 0x000000FF;
1562 *((uint8_t *) &CAN_Msg->
dataA[1])= (data & 0x0000FF00)>>8;
1563 *((uint8_t *) &CAN_Msg->
dataA[2])= (data & 0x00FF0000)>>16;
1564 *((uint8_t *) &CAN_Msg->
dataA[3])= (data & 0xFF000000)>>24;
1570 *((uint8_t *) &CAN_Msg->
dataB[0])= data & 0x000000FF;
1571 *((uint8_t *) &CAN_Msg->
dataB[1])= (data & 0x0000FF00)>>8;
1572 *((uint8_t *) &CAN_Msg->
dataB[2])= (data & 0x00FF0000)>>16;
1573 *((uint8_t *) &CAN_Msg->
dataB[3])= (data & 0xFF000000)>>24;
1577 CAN_Msg->
id = *pSrc & 0x7FF;
1578 CAN_Msg->
len = (uint8_t) (*pSrc >> 16) & 0x0F;
1580 CAN_Msg->
type = (uint8_t)(*pSrc >> 30) &0x01;
1582 if ((*pSrc & 0x03000000L) == 0)
1691 CANx->
IER |= (1 << arg);
1701 CANx->
IER &= ~(1 << arg);
1723 CANAFx->
AFMR = 0x00;
1726 CANAFx->
AFMR = 0x01;
1729 CANAFx->
AFMR = 0x02;
1732 CANAFx->
AFMR = 0x04;
1834 CANx->
CMR |= CMRType;