NUMAKER CAN sample

Committer:
ccli8
Date:
Wed Apr 17 17:41:40 2024 +0800
Revision:
10:33d27c970411
Parent:
9:bb7099cd4943
Child:
11:36d172d475fa
Disable filter #0

Reconfigure filter #0 to accept message ID 0 only.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cyliang 0:df0aeb0c8f2c 1 #include "cmsis_os.h"
cyliang 0:df0aeb0c8f2c 2 #include "mbed.h"
cyliang 0:df0aeb0c8f2c 3 #include "CAN.h"
cyliang 0:df0aeb0c8f2c 4
cyliang 0:df0aeb0c8f2c 5
cyliang 0:df0aeb0c8f2c 6 DigitalOut led1(LED1, 1); //LED R OFF
cyliang 0:df0aeb0c8f2c 7 DigitalOut led2(LED2, 1); //LED G OFF
cyliang 0:df0aeb0c8f2c 8 DigitalOut led3(LED3, 1); //LED B OFF
cyliang 0:df0aeb0c8f2c 9
cyliang 0:df0aeb0c8f2c 10 #define CAN_TX_MODE_TEST 0
cyliang 0:df0aeb0c8f2c 11 #define CAN_RX_MODE_TEST 1
ccli8 6:b9714e564dee 12 /* WARNING: Don't enable interrupt mode on receive side. It is not supported on Nuvoton targets. */
ccli8 6:b9714e564dee 13 #define CAN_RX_IRQ_EN 0
cyliang 0:df0aeb0c8f2c 14 #define LED_ALL_OFF led1=led2=led3=1
cyliang 0:df0aeb0c8f2c 15
cyliang 0:df0aeb0c8f2c 16 #define MSG_NUM_INDEX 5 // 0 ~ 31
cyliang 0:df0aeb0c8f2c 17 #define CAN_DEV_ID 0x1AC
cyliang 0:df0aeb0c8f2c 18
cyliang 0:df0aeb0c8f2c 19 #if defined(TARGET_NUMAKER_PFM_NUC472)
ccli8 5:499978915868 20 CAN canObj(PA_0, PA_1); // Internal in the board
cyliang 0:df0aeb0c8f2c 21 #elif defined(TARGET_NUMAKER_PFM_M453)
ccli8 5:499978915868 22 CAN canObj(PA_13, PA_12); // Internal in the board
ccli8 5:499978915868 23 #elif defined(TARGET_NUMAKER_PFM_M487)
cyliang 7:956b6cf0b070 24 CAN canObj(D9, D8); // (rd, td) Change to match external attachment
ccli8 6:b9714e564dee 25 #elif defined(TARGET_NUMAKER_IOT_M487)
ccli8 6:b9714e564dee 26 CAN canObj(A0, A1); // Change to match external attachment
cyliang 7:956b6cf0b070 27 #elif defined(TARGET_NUMAKER_IOT_M467)
cyliang 8:7310cfa7f71c 28 CAN canObj(A0, A1); // CAN1(rd, td) Change to match external attachment
cyliang 8:7310cfa7f71c 29 //CAN canObj(D9, D8); // CAN0
cyliang 0:df0aeb0c8f2c 30 #endif
cyliang 0:df0aeb0c8f2c 31
cyliang 0:df0aeb0c8f2c 32 CANMessage canMsg;
cyliang 0:df0aeb0c8f2c 33
cyliang 0:df0aeb0c8f2c 34 osThreadId mainThreadID;
cyliang 0:df0aeb0c8f2c 35
cyliang 0:df0aeb0c8f2c 36 static int read_MsgObj()
cyliang 0:df0aeb0c8f2c 37 {
cyliang 0:df0aeb0c8f2c 38 int i=0;
cyliang 0:df0aeb0c8f2c 39
cyliang 0:df0aeb0c8f2c 40 i = canObj.read(canMsg, MSG_NUM_INDEX);
cyliang 0:df0aeb0c8f2c 41
cyliang 0:df0aeb0c8f2c 42 switch (canMsg.data[0])
cyliang 0:df0aeb0c8f2c 43 {
cyliang 0:df0aeb0c8f2c 44 case 0:
cyliang 0:df0aeb0c8f2c 45 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 46 led1=0;
cyliang 0:df0aeb0c8f2c 47 break;
cyliang 0:df0aeb0c8f2c 48
cyliang 0:df0aeb0c8f2c 49 case 1:
cyliang 0:df0aeb0c8f2c 50 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 51 led2=0;
cyliang 0:df0aeb0c8f2c 52 break;
cyliang 0:df0aeb0c8f2c 53
cyliang 0:df0aeb0c8f2c 54 case 2:
cyliang 0:df0aeb0c8f2c 55 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 56 led3=0;
cyliang 0:df0aeb0c8f2c 57 break;
cyliang 0:df0aeb0c8f2c 58
cyliang 0:df0aeb0c8f2c 59 default:
cyliang 0:df0aeb0c8f2c 60 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 61 break;
cyliang 0:df0aeb0c8f2c 62 }
cyliang 0:df0aeb0c8f2c 63
cyliang 0:df0aeb0c8f2c 64 return i;
cyliang 0:df0aeb0c8f2c 65 }
cyliang 0:df0aeb0c8f2c 66
cyliang 0:df0aeb0c8f2c 67 void irq_callback(void)
cyliang 0:df0aeb0c8f2c 68 {
ccli8 6:b9714e564dee 69 /* Wake up receive task */
ccli8 6:b9714e564dee 70 osSignalSet(mainThreadID, 0x06);
cyliang 0:df0aeb0c8f2c 71 }
cyliang 0:df0aeb0c8f2c 72
cyliang 0:df0aeb0c8f2c 73 int main() {
ccli8 6:b9714e564dee 74 #if CAN_TX_MODE_TEST
ccli8 6:b9714e564dee 75 printf("CAN sender sample\r\n");
ccli8 6:b9714e564dee 76 #endif
ccli8 6:b9714e564dee 77
ccli8 6:b9714e564dee 78 #if CAN_RX_MODE_TEST
ccli8 6:b9714e564dee 79 printf("CAN receiver sample\r\n");
ccli8 6:b9714e564dee 80 #endif
ccli8 6:b9714e564dee 81
cyliang 0:df0aeb0c8f2c 82 int i=0;
cyliang 0:df0aeb0c8f2c 83 char data[8]={0};
cyliang 0:df0aeb0c8f2c 84
cyliang 0:df0aeb0c8f2c 85 mainThreadID = osThreadGetId();
cyliang 0:df0aeb0c8f2c 86
cyliang 0:df0aeb0c8f2c 87 /* Set Frequency 1khz~1000khz */
cyliang 0:df0aeb0c8f2c 88 canObj.frequency(1000000);
cyliang 0:df0aeb0c8f2c 89
cyliang 0:df0aeb0c8f2c 90 #if CAN_RX_MODE_TEST
cyliang 0:df0aeb0c8f2c 91
cyliang 0:df0aeb0c8f2c 92 #if CAN_RX_IRQ_EN
cyliang 0:df0aeb0c8f2c 93 /* Attach irq function */
cyliang 0:df0aeb0c8f2c 94 canObj.attach(irq_callback, CAN::RxIrq);
cyliang 0:df0aeb0c8f2c 95 #endif
ccli8 10:33d27c970411 96
ccli8 10:33d27c970411 97 /* According to link below, filter #0 will accept any message, and
ccli8 10:33d27c970411 98 * no other filters can accept messages without re-configuring filter #0.
ccli8 10:33d27c970411 99 * https://os.mbed.com/questions/85183/How-to-use-CAN-filter-function
ccli8 10:33d27c970411 100 *
ccli8 10:33d27c970411 101 * Re-configure filter #0 to accept message ID 0 only.
ccli8 10:33d27c970411 102 */
ccli8 10:33d27c970411 103 canObj.filter(0, 0xFFFFFFFF);
ccli8 10:33d27c970411 104
cyliang 0:df0aeb0c8f2c 105 canObj.filter(CAN_DEV_ID, 0, CANStandard, MSG_NUM_INDEX);
cyliang 0:df0aeb0c8f2c 106
cyliang 0:df0aeb0c8f2c 107 #endif
cyliang 0:df0aeb0c8f2c 108
cyliang 0:df0aeb0c8f2c 109 while (true)
cyliang 0:df0aeb0c8f2c 110 {
cyliang 0:df0aeb0c8f2c 111
cyliang 0:df0aeb0c8f2c 112
cyliang 0:df0aeb0c8f2c 113 #if CAN_TX_MODE_TEST
cyliang 0:df0aeb0c8f2c 114
cyliang 0:df0aeb0c8f2c 115 canObj.write(CANMessage(CAN_DEV_ID, data));
cyliang 0:df0aeb0c8f2c 116
cyliang 0:df0aeb0c8f2c 117 if(data[0] == 2)
cyliang 0:df0aeb0c8f2c 118 data[0]=0;
cyliang 0:df0aeb0c8f2c 119 else
cyliang 0:df0aeb0c8f2c 120 data[0]++;
ccli8 9:bb7099cd4943 121
cyliang 0:df0aeb0c8f2c 122 memset(&data[1], data[0], 7);
ccli8 9:bb7099cd4943 123 ThisThread::sleep_for(1000ms);
ccli8 9:bb7099cd4943 124
cyliang 0:df0aeb0c8f2c 125 #endif
cyliang 0:df0aeb0c8f2c 126
cyliang 0:df0aeb0c8f2c 127 #if CAN_RX_MODE_TEST
ccli8 6:b9714e564dee 128
ccli8 6:b9714e564dee 129 #if (CAN_RX_IRQ_EN)
cyliang 0:df0aeb0c8f2c 130 /* Wait for receive task to wakeup */
ccli8 6:b9714e564dee 131 osSignalWait(0x06, osWaitForever);
cyliang 0:df0aeb0c8f2c 132 #endif
ccli8 6:b9714e564dee 133 if (!read_MsgObj()) {
ccli8 6:b9714e564dee 134 continue;
ccli8 6:b9714e564dee 135 }
ccli8 6:b9714e564dee 136
cyliang 0:df0aeb0c8f2c 137 printf("Read ID=%8X, Type=%s, DLC=%d,Data=",canMsg.id,canMsg.format?"EXT":"STD",canMsg.len);
cyliang 0:df0aeb0c8f2c 138 for(i=0; i<canMsg.len; i++)
cyliang 0:df0aeb0c8f2c 139 printf("%02X,",canMsg.data[i]);
cyliang 0:df0aeb0c8f2c 140 printf("\r\n");
cyliang 0:df0aeb0c8f2c 141 #endif
cyliang 0:df0aeb0c8f2c 142 }
cyliang 0:df0aeb0c8f2c 143
cyliang 0:df0aeb0c8f2c 144 }