![](/media/cache/group/logo_thTzkVQ.jpg.50x50_q85.jpg)
NUMAKER CAN sample
main.cpp@9:bb7099cd4943, 3 months ago (annotated)
- Committer:
- ccli8
- Date:
- Wed Apr 17 17:33:23 2024 +0800
- Revision:
- 9:bb7099cd4943
- Parent:
- 8:7310cfa7f71c
- Child:
- 10:33d27c970411
Replace wait with ThisThread::sleep_for
This is to fix undeclared function wait() as error.
Who changed what in which revision?
User | Revision | Line number | New 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 |
cyliang | 0:df0aeb0c8f2c | 96 | |
cyliang | 0:df0aeb0c8f2c | 97 | canObj.filter(CAN_DEV_ID, 0, CANStandard, MSG_NUM_INDEX); |
cyliang | 0:df0aeb0c8f2c | 98 | |
cyliang | 0:df0aeb0c8f2c | 99 | #endif |
cyliang | 0:df0aeb0c8f2c | 100 | |
cyliang | 0:df0aeb0c8f2c | 101 | while (true) |
cyliang | 0:df0aeb0c8f2c | 102 | { |
cyliang | 0:df0aeb0c8f2c | 103 | |
cyliang | 0:df0aeb0c8f2c | 104 | |
cyliang | 0:df0aeb0c8f2c | 105 | #if CAN_TX_MODE_TEST |
cyliang | 0:df0aeb0c8f2c | 106 | |
cyliang | 0:df0aeb0c8f2c | 107 | canObj.write(CANMessage(CAN_DEV_ID, data)); |
cyliang | 0:df0aeb0c8f2c | 108 | |
cyliang | 0:df0aeb0c8f2c | 109 | if(data[0] == 2) |
cyliang | 0:df0aeb0c8f2c | 110 | data[0]=0; |
cyliang | 0:df0aeb0c8f2c | 111 | else |
cyliang | 0:df0aeb0c8f2c | 112 | data[0]++; |
ccli8 |
9:bb7099cd4943 | 113 | |
cyliang | 0:df0aeb0c8f2c | 114 | memset(&data[1], data[0], 7); |
ccli8 |
9:bb7099cd4943 | 115 | ThisThread::sleep_for(1000ms); |
ccli8 |
9:bb7099cd4943 | 116 | |
cyliang | 0:df0aeb0c8f2c | 117 | #endif |
cyliang | 0:df0aeb0c8f2c | 118 | |
cyliang | 0:df0aeb0c8f2c | 119 | #if CAN_RX_MODE_TEST |
ccli8 |
6:b9714e564dee | 120 | |
ccli8 |
6:b9714e564dee | 121 | #if (CAN_RX_IRQ_EN) |
cyliang | 0:df0aeb0c8f2c | 122 | /* Wait for receive task to wakeup */ |
ccli8 |
6:b9714e564dee | 123 | osSignalWait(0x06, osWaitForever); |
cyliang | 0:df0aeb0c8f2c | 124 | #endif |
ccli8 |
6:b9714e564dee | 125 | if (!read_MsgObj()) { |
ccli8 |
6:b9714e564dee | 126 | continue; |
ccli8 |
6:b9714e564dee | 127 | } |
ccli8 |
6:b9714e564dee | 128 | |
cyliang | 0:df0aeb0c8f2c | 129 | printf("Read ID=%8X, Type=%s, DLC=%d,Data=",canMsg.id,canMsg.format?"EXT":"STD",canMsg.len); |
cyliang | 0:df0aeb0c8f2c | 130 | for(i=0; i<canMsg.len; i++) |
cyliang | 0:df0aeb0c8f2c | 131 | printf("%02X,",canMsg.data[i]); |
cyliang | 0:df0aeb0c8f2c | 132 | printf("\r\n"); |
cyliang | 0:df0aeb0c8f2c | 133 | #endif |
cyliang | 0:df0aeb0c8f2c | 134 | } |
cyliang | 0:df0aeb0c8f2c | 135 | |
cyliang | 0:df0aeb0c8f2c | 136 | } |