First version of a mbed USB2CAN adapter Let\'s you send can messages via can. Type as !format_type_ID_length_d0_d1_d2_d3_d4_d5_d6_d7 (! is used to recognize valid messages, anything without a ! in front of it is dumped) All messages transmitted over cCAN are printed to your terminal as well.
USB2CAN.cpp@0:495bc2a030c1, 2011-02-24 (annotated)
- Committer:
- OTBsolar
- Date:
- Thu Feb 24 16:01:37 2011 +0000
- Revision:
- 0:495bc2a030c1
beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OTBsolar | 0:495bc2a030c1 | 1 | #include "mbed.h" |
OTBsolar | 0:495bc2a030c1 | 2 | #include "CAN.h" |
OTBsolar | 0:495bc2a030c1 | 3 | #include "stdio.h" |
OTBsolar | 0:495bc2a030c1 | 4 | #include "stdlib.h" |
OTBsolar | 0:495bc2a030c1 | 5 | #include "string.h" |
OTBsolar | 0:495bc2a030c1 | 6 | |
OTBsolar | 0:495bc2a030c1 | 7 | Ticker ticker; |
OTBsolar | 0:495bc2a030c1 | 8 | Serial pc(USBTX, USBRX); |
OTBsolar | 0:495bc2a030c1 | 9 | DigitalOut write_activity(LED1); //CAN activity |
OTBsolar | 0:495bc2a030c1 | 10 | DigitalOut read_activity(LED2); |
OTBsolar | 0:495bc2a030c1 | 11 | DigitalOut pc_activity(LED3); //USB activity |
OTBsolar | 0:495bc2a030c1 | 12 | CAN can1(p9, p10); // rd, td Transmitter |
OTBsolar | 0:495bc2a030c1 | 13 | CAN can2(p30, p29); // rd, td Monitor |
OTBsolar | 0:495bc2a030c1 | 14 | |
OTBsolar | 0:495bc2a030c1 | 15 | int counter = 0; |
OTBsolar | 0:495bc2a030c1 | 16 | char candata[8]; |
OTBsolar | 0:495bc2a030c1 | 17 | char pc_msg[50]; |
OTBsolar | 0:495bc2a030c1 | 18 | CANType pc_type; |
OTBsolar | 0:495bc2a030c1 | 19 | CANFormat pc_format; |
OTBsolar | 0:495bc2a030c1 | 20 | int pc_ID; //standard 11bit ID |
OTBsolar | 0:495bc2a030c1 | 21 | //int pc_IDe; //extended 29 (not used yet) |
OTBsolar | 0:495bc2a030c1 | 22 | int pc_length; |
OTBsolar | 0:495bc2a030c1 | 23 | int pcd0; int pcd1; int pcd2; int pcd3; int pcd4; int pcd5; int pcd6; int pcd7; //8 bytes data |
OTBsolar | 0:495bc2a030c1 | 24 | CANMessage msg; |
OTBsolar | 0:495bc2a030c1 | 25 | |
OTBsolar | 0:495bc2a030c1 | 26 | void setdata(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) { |
OTBsolar | 0:495bc2a030c1 | 27 | candata[0] = (char) (d0); // LSB |
OTBsolar | 0:495bc2a030c1 | 28 | candata[1] = (char) (d1); |
OTBsolar | 0:495bc2a030c1 | 29 | candata[2] = (char) (d2); |
OTBsolar | 0:495bc2a030c1 | 30 | candata[3] = (char) (d3); |
OTBsolar | 0:495bc2a030c1 | 31 | candata[4] = (char) (d4); |
OTBsolar | 0:495bc2a030c1 | 32 | candata[5] = (char) (d5); |
OTBsolar | 0:495bc2a030c1 | 33 | candata[6] = (char) (d6); |
OTBsolar | 0:495bc2a030c1 | 34 | candata[7] = (char) (d7); // MSB |
OTBsolar | 0:495bc2a030c1 | 35 | } |
OTBsolar | 0:495bc2a030c1 | 36 | |
OTBsolar | 0:495bc2a030c1 | 37 | void canread() { |
OTBsolar | 0:495bc2a030c1 | 38 | //printf("CANmessage: %c\n", msg); |
OTBsolar | 0:495bc2a030c1 | 39 | pc.printf("Read: [ ID: %d", msg.id); |
OTBsolar | 0:495bc2a030c1 | 40 | pc.printf(" Length: %d", msg.len); |
OTBsolar | 0:495bc2a030c1 | 41 | pc.printf(" Data: %x", msg.data[0]); |
OTBsolar | 0:495bc2a030c1 | 42 | pc.printf(" %x", msg.data[1]); |
OTBsolar | 0:495bc2a030c1 | 43 | pc.printf(" %x", msg.data[2]); |
OTBsolar | 0:495bc2a030c1 | 44 | pc.printf(" %x", msg.data[3]); |
OTBsolar | 0:495bc2a030c1 | 45 | pc.printf(" %x", msg.data[4]); |
OTBsolar | 0:495bc2a030c1 | 46 | pc.printf(" %x", msg.data[5]); |
OTBsolar | 0:495bc2a030c1 | 47 | pc.printf(" %x", msg.data[6]); |
OTBsolar | 0:495bc2a030c1 | 48 | pc.printf(" %x", msg.data[7]); |
OTBsolar | 0:495bc2a030c1 | 49 | pc.printf(" Type: %d", msg.type); |
OTBsolar | 0:495bc2a030c1 | 50 | pc.printf(" Format: %d ]\n", msg.format); |
OTBsolar | 0:495bc2a030c1 | 51 | read_activity = !read_activity; //Blink! |
OTBsolar | 0:495bc2a030c1 | 52 | can2.reset(); |
OTBsolar | 0:495bc2a030c1 | 53 | } |
OTBsolar | 0:495bc2a030c1 | 54 | |
OTBsolar | 0:495bc2a030c1 | 55 | void pc_msg_read() { |
OTBsolar | 0:495bc2a030c1 | 56 | // Data to be sent as !<format>_<type>_<ID>_<length>_<d0>_<d1>_<d2>_<d3>_<d4>_<d5>_<d6>_<d7> ("_" = space) |
OTBsolar | 0:495bc2a030c1 | 57 | |
OTBsolar | 0:495bc2a030c1 | 58 | // Read the string and copy it to the char array pc_msg |
OTBsolar | 0:495bc2a030c1 | 59 | pc.scanf("%[^\n]s",pc_msg); |
OTBsolar | 0:495bc2a030c1 | 60 | pc.printf("Entered:%s",pc_msg); |
OTBsolar | 0:495bc2a030c1 | 61 | |
OTBsolar | 0:495bc2a030c1 | 62 | // Read pc_msg and extract all data |
OTBsolar | 0:495bc2a030c1 | 63 | sscanf(pc_msg,"%d %d %d %d %x %x %x %x %x %x %x %x", &pc_format, &pc_type, &pc_ID, &pc_length, |
OTBsolar | 0:495bc2a030c1 | 64 | &pcd0, &pcd1, &pcd2, &pcd3, &pcd4, &pcd5, &pcd6, &pcd7); |
OTBsolar | 0:495bc2a030c1 | 65 | |
OTBsolar | 0:495bc2a030c1 | 66 | // Printing extracted data, mostly for testing |
OTBsolar | 0:495bc2a030c1 | 67 | pc.printf("Sent: [ ID: %d ",pc_ID); |
OTBsolar | 0:495bc2a030c1 | 68 | pc.printf("length: %d ",pc_length); |
OTBsolar | 0:495bc2a030c1 | 69 | pc.printf("data: %x %x %x %x %x %x %x %x ",pcd0, pcd1, pcd2, pcd3, pcd4, pcd5, pcd6, pcd7); |
OTBsolar | 0:495bc2a030c1 | 70 | pc.printf("type: %d ",pc_type); |
OTBsolar | 0:495bc2a030c1 | 71 | pc.printf("format: %d ]\n",pc_format); |
OTBsolar | 0:495bc2a030c1 | 72 | |
OTBsolar | 0:495bc2a030c1 | 73 | // Setting the data to CANMessage.data format |
OTBsolar | 0:495bc2a030c1 | 74 | setdata(pcd0, pcd1, pcd2, pcd3, pcd4, pcd5, pcd6, pcd7); |
OTBsolar | 0:495bc2a030c1 | 75 | |
OTBsolar | 0:495bc2a030c1 | 76 | // Transmitting CANMessage |
OTBsolar | 0:495bc2a030c1 | 77 | if(can1.write(CANMessage(pc_ID,candata,(char)pc_length,pc_type,pc_format))) { |
OTBsolar | 0:495bc2a030c1 | 78 | pc.printf("Message compiled and sent.\n"); |
OTBsolar | 0:495bc2a030c1 | 79 | } |
OTBsolar | 0:495bc2a030c1 | 80 | pc.printf("Please enter any additional commands in the same manner.\n"); |
OTBsolar | 0:495bc2a030c1 | 81 | |
OTBsolar | 0:495bc2a030c1 | 82 | } |
OTBsolar | 0:495bc2a030c1 | 83 | |
OTBsolar | 0:495bc2a030c1 | 84 | int main() { |
OTBsolar | 0:495bc2a030c1 | 85 | //----------------Initialization----------------------- |
OTBsolar | 0:495bc2a030c1 | 86 | can2.frequency(500000); //500kbit/s |
OTBsolar | 0:495bc2a030c1 | 87 | can1.frequency(500000); |
OTBsolar | 0:495bc2a030c1 | 88 | can2.monitor(1); //Works without this, in my case. |
OTBsolar | 0:495bc2a030c1 | 89 | //ticker.attach(&cansend, 0.5); //Send every 1 seconds. |
OTBsolar | 0:495bc2a030c1 | 90 | //----------------------------------------------------- |
OTBsolar | 0:495bc2a030c1 | 91 | |
OTBsolar | 0:495bc2a030c1 | 92 | char test; |
OTBsolar | 0:495bc2a030c1 | 93 | char dump; |
OTBsolar | 0:495bc2a030c1 | 94 | pc.printf("Please enter !<format>_<type>_<ID>_<length>_<d0>_<d1>_<d2>_<d3>_<d4>_<d5>_<d6>_<d7> ('_' = space)\n"); |
OTBsolar | 0:495bc2a030c1 | 95 | |
OTBsolar | 0:495bc2a030c1 | 96 | while(1) { |
OTBsolar | 0:495bc2a030c1 | 97 | if (pc.readable()) { // If there's data available from pc |
OTBsolar | 0:495bc2a030c1 | 98 | pc_activity = !pc_activity; // LED |
OTBsolar | 0:495bc2a030c1 | 99 | test = pc.getc(); |
OTBsolar | 0:495bc2a030c1 | 100 | if (test == '!') { // See if it's a valid message |
OTBsolar | 0:495bc2a030c1 | 101 | pc_msg_read(); // Valid => read the message and extract the data |
OTBsolar | 0:495bc2a030c1 | 102 | //pc.printf("Left pc_msg_read\n"); // "Left the function" test |
OTBsolar | 0:495bc2a030c1 | 103 | } |
OTBsolar | 0:495bc2a030c1 | 104 | else { // Invalid data or leftover characters |
OTBsolar | 0:495bc2a030c1 | 105 | pc.printf("Dumped: "); |
OTBsolar | 0:495bc2a030c1 | 106 | while(pc.readable()) { // Keep dumping the leftovers |
OTBsolar | 0:495bc2a030c1 | 107 | dump = pc.getc(); |
OTBsolar | 0:495bc2a030c1 | 108 | pc.printf("%c",dump); |
OTBsolar | 0:495bc2a030c1 | 109 | } |
OTBsolar | 0:495bc2a030c1 | 110 | } |
OTBsolar | 0:495bc2a030c1 | 111 | //pc.printf("Leaving pc.readable()\n"); // Left the !test |
OTBsolar | 0:495bc2a030c1 | 112 | } |
OTBsolar | 0:495bc2a030c1 | 113 | if (can2.read(msg)) { |
OTBsolar | 0:495bc2a030c1 | 114 | canread(); //Read when interupted. |
OTBsolar | 0:495bc2a030c1 | 115 | } |
OTBsolar | 0:495bc2a030c1 | 116 | } |
OTBsolar | 0:495bc2a030c1 | 117 | } |