NUMAKER CAN sample

Committer:
cyliang
Date:
Mon Mar 06 09:46:59 2023 +0000
Revision:
7:956b6cf0b070
Parent:
6:b9714e564dee
Child:
8:7310cfa7f71c
Update OS to v6.17.0 and add M467 CAN obj

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 7:956b6cf0b070 28 CAN canObj(PJ_11, PJ_10); // (rd, td) Change to match external attachment
cyliang 0:df0aeb0c8f2c 29 #endif
cyliang 0:df0aeb0c8f2c 30
cyliang 0:df0aeb0c8f2c 31 CANMessage canMsg;
cyliang 0:df0aeb0c8f2c 32
cyliang 0:df0aeb0c8f2c 33 osThreadId mainThreadID;
cyliang 0:df0aeb0c8f2c 34
cyliang 0:df0aeb0c8f2c 35 static int read_MsgObj()
cyliang 0:df0aeb0c8f2c 36 {
cyliang 0:df0aeb0c8f2c 37 int i=0;
cyliang 0:df0aeb0c8f2c 38
cyliang 0:df0aeb0c8f2c 39 i = canObj.read(canMsg, MSG_NUM_INDEX);
cyliang 0:df0aeb0c8f2c 40
cyliang 0:df0aeb0c8f2c 41 switch (canMsg.data[0])
cyliang 0:df0aeb0c8f2c 42 {
cyliang 0:df0aeb0c8f2c 43 case 0:
cyliang 0:df0aeb0c8f2c 44 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 45 led1=0;
cyliang 0:df0aeb0c8f2c 46 break;
cyliang 0:df0aeb0c8f2c 47
cyliang 0:df0aeb0c8f2c 48 case 1:
cyliang 0:df0aeb0c8f2c 49 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 50 led2=0;
cyliang 0:df0aeb0c8f2c 51 break;
cyliang 0:df0aeb0c8f2c 52
cyliang 0:df0aeb0c8f2c 53 case 2:
cyliang 0:df0aeb0c8f2c 54 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 55 led3=0;
cyliang 0:df0aeb0c8f2c 56 break;
cyliang 0:df0aeb0c8f2c 57
cyliang 0:df0aeb0c8f2c 58 default:
cyliang 0:df0aeb0c8f2c 59 LED_ALL_OFF;
cyliang 0:df0aeb0c8f2c 60 break;
cyliang 0:df0aeb0c8f2c 61 }
cyliang 0:df0aeb0c8f2c 62
cyliang 0:df0aeb0c8f2c 63 return i;
cyliang 0:df0aeb0c8f2c 64 }
cyliang 0:df0aeb0c8f2c 65
cyliang 0:df0aeb0c8f2c 66 void irq_callback(void)
cyliang 0:df0aeb0c8f2c 67 {
ccli8 6:b9714e564dee 68 /* Wake up receive task */
ccli8 6:b9714e564dee 69 osSignalSet(mainThreadID, 0x06);
cyliang 0:df0aeb0c8f2c 70 }
cyliang 0:df0aeb0c8f2c 71
cyliang 0:df0aeb0c8f2c 72 int main() {
ccli8 6:b9714e564dee 73 #if CAN_TX_MODE_TEST
ccli8 6:b9714e564dee 74 printf("CAN sender sample\r\n");
ccli8 6:b9714e564dee 75 #endif
ccli8 6:b9714e564dee 76
ccli8 6:b9714e564dee 77 #if CAN_RX_MODE_TEST
ccli8 6:b9714e564dee 78 printf("CAN receiver sample\r\n");
ccli8 6:b9714e564dee 79 #endif
ccli8 6:b9714e564dee 80
cyliang 0:df0aeb0c8f2c 81 int i=0;
cyliang 0:df0aeb0c8f2c 82 char data[8]={0};
cyliang 0:df0aeb0c8f2c 83
cyliang 0:df0aeb0c8f2c 84 mainThreadID = osThreadGetId();
cyliang 0:df0aeb0c8f2c 85
cyliang 0:df0aeb0c8f2c 86 /* Set Frequency 1khz~1000khz */
cyliang 0:df0aeb0c8f2c 87 canObj.frequency(1000000);
cyliang 0:df0aeb0c8f2c 88
cyliang 0:df0aeb0c8f2c 89 #if CAN_RX_MODE_TEST
cyliang 0:df0aeb0c8f2c 90
cyliang 0:df0aeb0c8f2c 91 #if CAN_RX_IRQ_EN
cyliang 0:df0aeb0c8f2c 92 /* Attach irq function */
cyliang 0:df0aeb0c8f2c 93 canObj.attach(irq_callback, CAN::RxIrq);
cyliang 0:df0aeb0c8f2c 94 #endif
cyliang 0:df0aeb0c8f2c 95
cyliang 0:df0aeb0c8f2c 96 canObj.filter(CAN_DEV_ID, 0, CANStandard, MSG_NUM_INDEX);
cyliang 0:df0aeb0c8f2c 97
cyliang 0:df0aeb0c8f2c 98 #endif
cyliang 0:df0aeb0c8f2c 99
cyliang 0:df0aeb0c8f2c 100 while (true)
cyliang 0:df0aeb0c8f2c 101 {
cyliang 0:df0aeb0c8f2c 102
cyliang 0:df0aeb0c8f2c 103
cyliang 0:df0aeb0c8f2c 104 #if CAN_TX_MODE_TEST
cyliang 0:df0aeb0c8f2c 105
cyliang 0:df0aeb0c8f2c 106 canObj.write(CANMessage(CAN_DEV_ID, data));
cyliang 0:df0aeb0c8f2c 107
cyliang 0:df0aeb0c8f2c 108 if(data[0] == 2)
cyliang 0:df0aeb0c8f2c 109 data[0]=0;
cyliang 0:df0aeb0c8f2c 110 else
cyliang 0:df0aeb0c8f2c 111 data[0]++;
cyliang 0:df0aeb0c8f2c 112
cyliang 0:df0aeb0c8f2c 113 memset(&data[1], data[0], 7);
cyliang 0:df0aeb0c8f2c 114 wait(1);
cyliang 0:df0aeb0c8f2c 115
cyliang 0:df0aeb0c8f2c 116 #endif
cyliang 0:df0aeb0c8f2c 117
cyliang 0:df0aeb0c8f2c 118 #if CAN_RX_MODE_TEST
ccli8 6:b9714e564dee 119
ccli8 6:b9714e564dee 120 #if (CAN_RX_IRQ_EN)
cyliang 0:df0aeb0c8f2c 121 /* Wait for receive task to wakeup */
ccli8 6:b9714e564dee 122 osSignalWait(0x06, osWaitForever);
cyliang 0:df0aeb0c8f2c 123 #endif
ccli8 6:b9714e564dee 124 if (!read_MsgObj()) {
ccli8 6:b9714e564dee 125 continue;
ccli8 6:b9714e564dee 126 }
ccli8 6:b9714e564dee 127
cyliang 0:df0aeb0c8f2c 128 printf("Read ID=%8X, Type=%s, DLC=%d,Data=",canMsg.id,canMsg.format?"EXT":"STD",canMsg.len);
cyliang 0:df0aeb0c8f2c 129 for(i=0; i<canMsg.len; i++)
cyliang 0:df0aeb0c8f2c 130 printf("%02X,",canMsg.data[i]);
cyliang 0:df0aeb0c8f2c 131 printf("\r\n");
cyliang 0:df0aeb0c8f2c 132 #endif
cyliang 0:df0aeb0c8f2c 133 }
cyliang 0:df0aeb0c8f2c 134
cyliang 0:df0aeb0c8f2c 135 }