NUMAKER CAN sample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 /* WARNING: Don't enable interrupt mode on receive side. It is not supported on Nuvoton targets. */
00013 #define CAN_RX_IRQ_EN               0
00014 #define LED_ALL_OFF                 led1=led2=led3=1
00015 
00016 #define MSG_NUM_INDEX           5   // 0 ~ 31   
00017 #define CAN_DEV_ID              0x1AC
00018 
00019 #if defined(TARGET_NUMAKER_PFM_NUC472)
00020 CAN canObj(PA_0, PA_1);     // Internal in the board
00021 #elif defined(TARGET_NUMAKER_PFM_M453)
00022 CAN canObj(PA_13, PA_12);   // Internal in the board
00023 #elif defined(TARGET_NUMAKER_PFM_M487)
00024 CAN canObj(D9, D8);         // (rd, td) Change to match external attachment
00025 #elif defined(TARGET_NUMAKER_IOT_M487) 
00026 CAN canObj(A0, A1);         // Change to match external attachment
00027 #elif defined(TARGET_NUMAKER_IOT_M467) 
00028 CAN canObj(A0, A1);         // CAN1(rd, td) Change to match external attachment
00029 //CAN canObj(D9, D8);       // CAN0
00030 #endif
00031 
00032 CANMessage canMsg;
00033 
00034 osThreadId mainThreadID;
00035 
00036 static int read_MsgObj()
00037 {
00038 int i=0;
00039 
00040     i = canObj.read(canMsg, MSG_NUM_INDEX);
00041     
00042         switch (canMsg.data[0])
00043         {
00044             case 0:
00045                 LED_ALL_OFF;
00046                 led1=0;
00047                 break;
00048             
00049             case 1:
00050                 LED_ALL_OFF;
00051                 led2=0;
00052                 break;
00053             
00054             case 2:
00055                 LED_ALL_OFF;
00056                 led3=0;
00057                 break;
00058             
00059             default:
00060                 LED_ALL_OFF;
00061                 break;
00062         }
00063 
00064    return i; 
00065 }
00066 
00067 void irq_callback(void)
00068 {
00069     /* Wake up receive task */
00070     osSignalSet(mainThreadID, 0x06);
00071 }
00072 
00073 int main() {
00074 #if CAN_TX_MODE_TEST
00075     printf("CAN sender sample\r\n");
00076 #endif
00077 
00078 #if CAN_RX_MODE_TEST
00079     printf("CAN receiver sample\r\n");
00080 #endif
00081 
00082     int i=0;
00083     char data[8]={0};
00084 
00085     mainThreadID = osThreadGetId();
00086 
00087     /* Set Frequency 1khz~1000khz */
00088     canObj.frequency(1000000);
00089     
00090 #if CAN_RX_MODE_TEST
00091     
00092 #if CAN_RX_IRQ_EN
00093     /* Attach irq function */
00094     canObj.attach(irq_callback, CAN::RxIrq);
00095 #endif
00096     
00097     canObj.filter(CAN_DEV_ID, 0, CANStandard, MSG_NUM_INDEX);
00098     
00099 #endif
00100     
00101     while (true)
00102     {
00103 
00104         
00105 #if CAN_TX_MODE_TEST
00106         
00107         canObj.write(CANMessage(CAN_DEV_ID, data));
00108         
00109         if(data[0] == 2)
00110             data[0]=0;
00111         else
00112             data[0]++;
00113         
00114         memset(&data[1], data[0], 7);
00115         wait(1);
00116         
00117 #endif
00118         
00119 #if CAN_RX_MODE_TEST
00120 
00121 #if (CAN_RX_IRQ_EN)
00122         /* Wait for receive task to wakeup */
00123         osSignalWait(0x06, osWaitForever);      
00124 #endif
00125         if (!read_MsgObj()) {
00126             continue;
00127         }
00128 
00129         printf("Read ID=%8X, Type=%s, DLC=%d,Data=",canMsg.id,canMsg.format?"EXT":"STD",canMsg.len);
00130         for(i=0; i<canMsg.len; i++)
00131             printf("%02X,",canMsg.data[i]);
00132         printf("\r\n");     
00133 #endif
00134     }
00135     
00136 }