mbed-os
Fork of mbed-os by
features/unsupported/tests/mbed/can/main.cpp@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | #include "mbed.h" |
elessair | 0:f269e3021894 | 2 | |
elessair | 0:f269e3021894 | 3 | #if !DEVICE_CAN |
elessair | 0:f269e3021894 | 4 | #error [NOT_SUPPORTED] CAN not supported |
elessair | 0:f269e3021894 | 5 | #endif |
elessair | 0:f269e3021894 | 6 | |
elessair | 0:f269e3021894 | 7 | Ticker ticker; |
elessair | 0:f269e3021894 | 8 | DigitalOut led1(LED1); |
elessair | 0:f269e3021894 | 9 | DigitalOut led2(LED2); |
elessair | 0:f269e3021894 | 10 | |
elessair | 0:f269e3021894 | 11 | #if defined(TARGET_LPC1549) |
elessair | 0:f269e3021894 | 12 | // LPC1549 support only single CAN channel |
elessair | 0:f269e3021894 | 13 | CAN can1(D2, D3); |
elessair | 0:f269e3021894 | 14 | #elif defined(TARGET_B96B_F446VE) |
elessair | 0:f269e3021894 | 15 | // B96B_F446VE support only single CAN channel |
elessair | 0:f269e3021894 | 16 | CAN can1(PD_0, PD_1); |
elessair | 0:f269e3021894 | 17 | #elif defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F072RB) || \ |
elessair | 0:f269e3021894 | 18 | defined(TARGET_NUCLEO_F042K6) || defined(TARGET_NUCLEO_F334R8) || \ |
elessair | 0:f269e3021894 | 19 | defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F303K8) || \ |
elessair | 0:f269e3021894 | 20 | defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_F446RE) || \ |
elessair | 0:f269e3021894 | 21 | defined(TARGET_DISCO_F429ZI) || defined(TARGET_NUCLEO_F103RB) || \ |
elessair | 0:f269e3021894 | 22 | defined(TARGET_NUCLEO_F746ZG) || defined(TARGET_NUCLEO_L476RG) || \ |
elessair | 0:f269e3021894 | 23 | defined(TARGET_NUCLEO_L432KC) |
elessair | 0:f269e3021894 | 24 | CAN can1(PA_11, PA_12); |
elessair | 0:f269e3021894 | 25 | #elif defined(TARGET_DISCO_F469NI) ||defined(TARGET_DISCO_F746NG) |
elessair | 0:f269e3021894 | 26 | CAN can1(PB_8, PB_9); |
elessair | 0:f269e3021894 | 27 | #else |
elessair | 0:f269e3021894 | 28 | CAN can1(p9, p10); |
elessair | 0:f269e3021894 | 29 | #endif |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | #if defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM) |
elessair | 0:f269e3021894 | 32 | CAN can2(p34, p33); |
elessair | 0:f269e3021894 | 33 | #elif defined (TARGET_LPC1768) |
elessair | 0:f269e3021894 | 34 | CAN can2(p30, p29); |
elessair | 0:f269e3021894 | 35 | #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_DISCO_F469NI) || \ |
elessair | 0:f269e3021894 | 36 | defined(TARGET_DISCO_F429ZI) || defined(TARGET_NUCLEO_F746ZG) || \ |
elessair | 0:f269e3021894 | 37 | defined(TARGET_DISCO_F746NG) |
elessair | 0:f269e3021894 | 38 | CAN can2(PB_5, PB_6); |
elessair | 0:f269e3021894 | 39 | #endif |
elessair | 0:f269e3021894 | 40 | char counter = 0; |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | void printmsg(char *title, CANMessage *msg) { |
elessair | 0:f269e3021894 | 43 | printf("%s [%03X]", title, msg->id); |
elessair | 0:f269e3021894 | 44 | for(char i = 0; i < msg->len; i++) { |
elessair | 0:f269e3021894 | 45 | printf(" %02X", msg->data[i]); |
elessair | 0:f269e3021894 | 46 | } |
elessair | 0:f269e3021894 | 47 | printf("\n"); |
elessair | 0:f269e3021894 | 48 | } |
elessair | 0:f269e3021894 | 49 | |
elessair | 0:f269e3021894 | 50 | void send() { |
elessair | 0:f269e3021894 | 51 | printf("send()\n"); |
elessair | 0:f269e3021894 | 52 | CANMessage msg = CANMessage(1337, &counter, 1); |
elessair | 0:f269e3021894 | 53 | if(can1.write(msg)) { |
elessair | 0:f269e3021894 | 54 | printmsg("Tx message:", &msg); |
elessair | 0:f269e3021894 | 55 | counter++; |
elessair | 0:f269e3021894 | 56 | } |
elessair | 0:f269e3021894 | 57 | led1 = !led1; |
elessair | 0:f269e3021894 | 58 | } |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | int main() { |
elessair | 0:f269e3021894 | 61 | printf("main()\n"); |
elessair | 0:f269e3021894 | 62 | ticker.attach(&send, 1); |
elessair | 0:f269e3021894 | 63 | CANMessage msg; |
elessair | 0:f269e3021894 | 64 | while(1) { |
elessair | 0:f269e3021894 | 65 | #if (!defined (TARGET_LPC1549) && !defined(TARGET_B96B_F446VE) && \ |
elessair | 0:f269e3021894 | 66 | !defined(TARGET_NUCLEO_F091RC) && !defined(TARGET_NUCLEO_F072RB) && \ |
elessair | 0:f269e3021894 | 67 | !defined(TARGET_NUCLEO_F042K6) && !defined(TARGET_NUCLEO_F334R8) && \ |
elessair | 0:f269e3021894 | 68 | !defined(TARGET_NUCLEO_F303RE) && !defined(TARGET_NUCLEO_F303K8) && \ |
elessair | 0:f269e3021894 | 69 | !defined(TARGET_NUCLEO_F302R8) && !defined(TARGET_NUCLEO_F103RB) && \ |
elessair | 0:f269e3021894 | 70 | !defined(TARGET_DISCO_L476VG) && !defined(TARGET_NUCLEO_L476RG) && \ |
elessair | 0:f269e3021894 | 71 | !defined(TARGET_NUCLEO_L432KC)) |
elessair | 0:f269e3021894 | 72 | printf("loop()\n"); |
elessair | 0:f269e3021894 | 73 | if(can2.read(msg)) { |
elessair | 0:f269e3021894 | 74 | printmsg("Rx message:", &msg); |
elessair | 0:f269e3021894 | 75 | led2 = !led2; |
elessair | 0:f269e3021894 | 76 | } |
elessair | 0:f269e3021894 | 77 | #endif |
elessair | 0:f269e3021894 | 78 | wait(0.2); |
elessair | 0:f269e3021894 | 79 | } |
elessair | 0:f269e3021894 | 80 | } |