NUMAKER CAN sample

Committer:
ccli8
Date:
Wed May 13 13:20:28 2020 +0800
Revision:
6:b9714e564dee
Parent:
5:499978915868
Child:
7:956b6cf0b070
Update to mbed-os 5.15.3 and relevant modifications

1. Update to mbed-os 5.15.3.
2. Fix mutex lock error in interrupt mode on receive side.
3. Disable interrupt mode on receive side. It is not supported on Nuvoton targets.

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