Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // CAN_MONITOR with filtering 2012/03/21 ym1784 00002 00003 #include "mbed.h" 00004 #include "CAN.h" 00005 00006 Serial pc(USBTX, USBRX); // tx, rx 00007 DigitalOut led2(LED2); 00008 00009 // CAN2 on mbed pins 29(CAN_TXD) and 30(CAN_RXD) using MCP2551. 00010 CAN can2(p30, p29); 00011 00012 /*-------------------------------------------- 00013 setup acceptance filter for CAN controller 2 00014 original http://www.dragonwake.com/download/LPC1768/Example/CAN/CAN.c 00015 simplified for CAN2 interface and std id (11 bit) only 00016 *--------------------------------------------*/ 00017 void CAN2_wrFilter (uint32_t id) { 00018 static int CAN_std_cnt = 0; 00019 uint32_t buf0, buf1; 00020 int cnt1, cnt2, bound1; 00021 00022 /* Acceptance Filter Memory full */ 00023 if (((CAN_std_cnt + 1) >> 1) >= 512) 00024 return; /* error: objects full */ 00025 00026 /* Setup Acceptance Filter Configuration 00027 Acceptance Filter Mode Register = Off */ 00028 LPC_CANAF->AFMR = 0x00000001; 00029 00030 id |= 1 << 13; /* Add controller number(2) */ 00031 id &= 0x0000F7FF; /* Mask out 16-bits of ID */ 00032 00033 if (CAN_std_cnt == 0) { /* For entering first ID */ 00034 LPC_CANAF_RAM->mask[0] = 0x0000FFFF | (id << 16); 00035 } else if (CAN_std_cnt == 1) { /* For entering second ID */ 00036 if ((LPC_CANAF_RAM->mask[0] >> 16) > id) 00037 LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] >> 16) | (id << 16); 00038 else 00039 LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] & 0xFFFF0000) | id; 00040 } else { 00041 /* Find where to insert new ID */ 00042 cnt1 = 0; 00043 cnt2 = CAN_std_cnt; 00044 bound1 = (CAN_std_cnt - 1) >> 1; 00045 while (cnt1 <= bound1) { /* Loop through standard existing IDs */ 00046 if ((LPC_CANAF_RAM->mask[cnt1] >> 16) > id) { 00047 cnt2 = cnt1 * 2; 00048 break; 00049 } 00050 if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000FFFF) > id) { 00051 cnt2 = cnt1 * 2 + 1; 00052 break; 00053 } 00054 cnt1++; /* cnt1 = U32 where to insert new ID */ 00055 } /* cnt2 = U16 where to insert new ID */ 00056 00057 if (cnt1 > bound1) { /* Adding ID as last entry */ 00058 if ((CAN_std_cnt & 0x0001) == 0) /* Even number of IDs exists */ 00059 LPC_CANAF_RAM->mask[cnt1] = 0x0000FFFF | (id << 16); 00060 else /* Odd number of IDs exists */ 00061 LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | id; 00062 } else { 00063 buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */ 00064 if ((cnt2 & 0x0001) == 0) /* Insert new mask to even address */ 00065 buf1 = (id << 16) | (buf0 >> 16); 00066 else /* Insert new mask to odd address */ 00067 buf1 = (buf0 & 0xFFFF0000) | id; 00068 00069 LPC_CANAF_RAM->mask[cnt1] = buf1; /* Insert mask */ 00070 00071 bound1 = CAN_std_cnt >> 1; 00072 /* Move all remaining standard mask entries one place up */ 00073 while (cnt1 < bound1) { 00074 cnt1++; 00075 buf1 = LPC_CANAF_RAM->mask[cnt1]; 00076 LPC_CANAF_RAM->mask[cnt1] = (buf1 >> 16) | (buf0 << 16); 00077 buf0 = buf1; 00078 } 00079 00080 if ((CAN_std_cnt & 0x0001) == 0) /* Even number of IDs exists */ 00081 LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | (0x0000FFFF); 00082 } 00083 } 00084 CAN_std_cnt++; 00085 00086 /* Calculate std ID start address (buf0) and ext ID start address <- none (buf1) */ 00087 buf0 = ((CAN_std_cnt + 1) >> 1) << 2; 00088 buf1 = buf0; 00089 00090 /* Setup acceptance filter pointers */ 00091 LPC_CANAF->SFF_sa = 0; 00092 LPC_CANAF->SFF_GRP_sa = buf0; 00093 LPC_CANAF->EFF_sa = buf0; 00094 LPC_CANAF->EFF_GRP_sa = buf1; 00095 LPC_CANAF->ENDofTable = buf1; 00096 00097 LPC_CANAF->AFMR = 0x00000000; /* Use acceptance filter */ 00098 } // CAN2_wrFilter 00099 00100 int main() { 00101 pc.baud(921600); 00102 pc.printf("CAN_MONITOR 921600 bps\r\n"); 00103 00104 // 500kbit/s 00105 can2.frequency(500000); 00106 CANMessage can_MsgRx; 00107 00108 CAN2_wrFilter(0x0B4); 00109 CAN2_wrFilter(0x1C4); 00110 CAN2_wrFilter(0x245); 00111 CAN2_wrFilter(0x3D3); 00112 CAN2_wrFilter(0x498); 00113 CAN2_wrFilter(0x4A6); 00114 00115 while (1) { 00116 // send received messages to the pc via serial line 00117 if (can2.read(can_MsgRx)) { 00118 pc.printf("t%03X%d", can_MsgRx.id, can_MsgRx.len); 00119 for (char i=0; i<can_MsgRx.len; i++) { 00120 pc.printf("%02X", can_MsgRx.data[i]); 00121 } // for 00122 pc.printf("\r\n"); 00123 // toggle led2 00124 led2 = !led2; 00125 } // if 00126 } // while 00127 } // main
Generated on Tue Jul 19 2022 06:03:02 by
1.7.2