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 #include "cmsis_os.h" 00002 #include "mbed.h" 00003 #include "CAN.h" 00004 00005 00006 DigitalOut led1(LED1, 1); //LED R OFF 00007 DigitalOut led2(LED2, 1); //LED G OFF 00008 DigitalOut led3(LED3, 1); //LED B OFF 00009 00010 #define CAN_TX_MODE_TEST 0 00011 #define CAN_RX_MODE_TEST 1 00012 #define CAN_LOOPBACK_MODE_TEST 0 00013 #if CAN_LOOPBACK_MODE_TEST && (!CAN_TX_MODE_TEST || !CAN_RX_MODE_TEST) 00014 #error "Either TX or RX not enabled for loopback mode" 00015 #endif 00016 00017 /* WARNING: Don't enable interrupt mode on receive side. It is not supported on Nuvoton targets. */ 00018 #define CAN_RX_IRQ_EN 0 00019 #define LED_ALL_OFF led1=led2=led3=1 00020 00021 #define MSG_NUM_INDEX 5 // 0 ~ 31 00022 #define CAN_DEV_ID 0x1AC 00023 00024 #if defined(TARGET_NUMAKER_PFM_NUC472) 00025 CAN canObj(PA_0, PA_1); // Internal in the board 00026 #elif defined(TARGET_NUMAKER_PFM_M453) 00027 CAN canObj(PA_13, PA_12); // Internal in the board 00028 #elif defined(TARGET_NUMAKER_PFM_M487) 00029 CAN canObj(D9, D8); // (rd, td) Change to match external attachment 00030 #elif defined(TARGET_NUMAKER_IOT_M487) 00031 CAN canObj(A0, A1); // Change to match external attachment 00032 #elif defined(TARGET_NUMAKER_IOT_M467) 00033 CAN canObj(A0, A1); // CAN1(rd, td) Change to match external attachment 00034 //CAN canObj(D9, D8); // CAN0 00035 #endif 00036 00037 CANMessage canMsg; 00038 00039 osThreadId mainThreadID; 00040 00041 static int read_MsgObj() 00042 { 00043 int i=0; 00044 00045 i = canObj.read(canMsg, MSG_NUM_INDEX); 00046 00047 switch (canMsg.data[0]) 00048 { 00049 case 0: 00050 LED_ALL_OFF; 00051 led1=0; 00052 break; 00053 00054 case 1: 00055 LED_ALL_OFF; 00056 led2=0; 00057 break; 00058 00059 case 2: 00060 LED_ALL_OFF; 00061 led3=0; 00062 break; 00063 00064 default: 00065 LED_ALL_OFF; 00066 break; 00067 } 00068 00069 return i; 00070 } 00071 00072 void irq_callback(void) 00073 { 00074 /* Wake up receive task */ 00075 osSignalSet(mainThreadID, 0x06); 00076 } 00077 00078 int main() { 00079 #if CAN_TX_MODE_TEST 00080 printf("CAN sender sample\r\n"); 00081 #endif 00082 00083 #if CAN_RX_MODE_TEST 00084 printf("CAN receiver sample\r\n"); 00085 #endif 00086 00087 int i=0; 00088 char data[8]={0}; 00089 00090 mainThreadID = osThreadGetId(); 00091 00092 /* Set Frequency 1khz~1000khz */ 00093 canObj.frequency(1000000); 00094 00095 #if CAN_RX_MODE_TEST 00096 00097 #if CAN_RX_IRQ_EN 00098 /* Attach irq function */ 00099 canObj.attach(irq_callback, CAN::RxIrq); 00100 #endif 00101 00102 /* According to link below, filter #0 will accept any message, and 00103 * no other filters can accept messages without re-configuring filter #0. 00104 * https://os.mbed.com/questions/85183/How-to-use-CAN-filter-function 00105 * 00106 * Re-configure filter #0 to accept message ID 0 only. 00107 */ 00108 canObj.filter(0, 0xFFFFFFFF); 00109 00110 canObj.filter(CAN_DEV_ID, 0, CANStandard, MSG_NUM_INDEX); 00111 00112 #endif 00113 00114 #if CAN_LOOPBACK_MODE_TEST 00115 if (!canObj.mode(CAN::SilentTest)) { 00116 printf("CAN: Configure to SilentTest mode failed\n\n"); 00117 return EXIT_FAILURE; 00118 } 00119 #endif 00120 00121 while (true) 00122 { 00123 00124 00125 #if CAN_TX_MODE_TEST 00126 00127 canObj.write(CANMessage(CAN_DEV_ID, data)); 00128 00129 if(data[0] == 2) 00130 data[0]=0; 00131 else 00132 data[0]++; 00133 00134 memset(&data[1], data[0], 7); 00135 ThisThread::sleep_for(1000ms); 00136 00137 #endif 00138 00139 #if CAN_RX_MODE_TEST 00140 00141 #if (CAN_RX_IRQ_EN) 00142 /* Wait for receive task to wakeup */ 00143 osSignalWait(0x06, osWaitForever); 00144 #endif 00145 if (!read_MsgObj()) { 00146 continue; 00147 } 00148 00149 printf("Read ID=%8X, Type=%s, DLC=%d,Data=",canMsg.id,canMsg.format?"EXT":"STD",canMsg.len); 00150 for(i=0; i<canMsg.len; i++) 00151 printf("%02X,",canMsg.data[i]); 00152 printf("\r\n"); 00153 #endif 00154 } 00155 00156 }
Generated on Thu Apr 18 2024 01:53:41 by
1.7.2