Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@0:d9f4fedf5253, 2017-11-22 (annotated)
- Committer:
- ArmandLambrechts
- Date:
- Wed Nov 22 07:51:02 2017 +0000
- Revision:
- 0:d9f4fedf5253
- Child:
- 1:12ddbe69b6e6
Canbus Test 22/11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ArmandLambrechts | 0:d9f4fedf5253 | 1 | /*** Thread: CAN *** |
ArmandLambrechts | 0:d9f4fedf5253 | 2 | * Test CAN Messages |
ArmandLambrechts | 0:d9f4fedf5253 | 3 | *WERKT 25/10/2017 |
ArmandLambrechts | 0:d9f4fedf5253 | 4 | */ |
ArmandLambrechts | 0:d9f4fedf5253 | 5 | #include "mbed.h" |
ArmandLambrechts | 0:d9f4fedf5253 | 6 | #include "CAN.h" |
ArmandLambrechts | 0:d9f4fedf5253 | 7 | |
ArmandLambrechts | 0:d9f4fedf5253 | 8 | /* CAN (RD TD) */ |
ArmandLambrechts | 0:d9f4fedf5253 | 9 | CAN can(PA_11, PA_12); |
ArmandLambrechts | 0:d9f4fedf5253 | 10 | |
ArmandLambrechts | 0:d9f4fedf5253 | 11 | DigitalOut canEN(PA_10); |
ArmandLambrechts | 0:d9f4fedf5253 | 12 | /* Heartbeat */ |
ArmandLambrechts | 0:d9f4fedf5253 | 13 | DigitalOut led(LED1); |
ArmandLambrechts | 0:d9f4fedf5253 | 14 | |
ArmandLambrechts | 0:d9f4fedf5253 | 15 | /* Structures to help pack and unpack the 8 CAN data bytes */ |
ArmandLambrechts | 0:d9f4fedf5253 | 16 | typedef struct bytes_64 { |
ArmandLambrechts | 0:d9f4fedf5253 | 17 | uint8_t b0; |
ArmandLambrechts | 0:d9f4fedf5253 | 18 | uint8_t b1; |
ArmandLambrechts | 0:d9f4fedf5253 | 19 | uint8_t b2; |
ArmandLambrechts | 0:d9f4fedf5253 | 20 | uint8_t b3; |
ArmandLambrechts | 0:d9f4fedf5253 | 21 | uint8_t b4; |
ArmandLambrechts | 0:d9f4fedf5253 | 22 | uint8_t b5; |
ArmandLambrechts | 0:d9f4fedf5253 | 23 | uint8_t b6; |
ArmandLambrechts | 0:d9f4fedf5253 | 24 | uint8_t b7; |
ArmandLambrechts | 0:d9f4fedf5253 | 25 | } bytes_64; |
ArmandLambrechts | 0:d9f4fedf5253 | 26 | |
ArmandLambrechts | 0:d9f4fedf5253 | 27 | typedef union { |
ArmandLambrechts | 0:d9f4fedf5253 | 28 | uint64_t data; |
ArmandLambrechts | 0:d9f4fedf5253 | 29 | bytes_64 bytes; |
ArmandLambrechts | 0:d9f4fedf5253 | 30 | } data_packed; |
ArmandLambrechts | 0:d9f4fedf5253 | 31 | |
ArmandLambrechts | 0:d9f4fedf5253 | 32 | |
ArmandLambrechts | 0:d9f4fedf5253 | 33 | /* main */ |
ArmandLambrechts | 0:d9f4fedf5253 | 34 | int main() { |
ArmandLambrechts | 0:d9f4fedf5253 | 35 | |
ArmandLambrechts | 0:d9f4fedf5253 | 36 | printf("\r\n---Start---\r\n"); |
ArmandLambrechts | 0:d9f4fedf5253 | 37 | |
ArmandLambrechts | 0:d9f4fedf5253 | 38 | data_packed send_data; //struct for send message |
ArmandLambrechts | 0:d9f4fedf5253 | 39 | data_packed receive_data; //struct for rcv message |
ArmandLambrechts | 0:d9f4fedf5253 | 40 | CANMessage msg; //message object for rcv |
ArmandLambrechts | 0:d9f4fedf5253 | 41 | int send_id = 15; //send message id |
ArmandLambrechts | 0:d9f4fedf5253 | 42 | int send_status = 0; |
ArmandLambrechts | 0:d9f4fedf5253 | 43 | |
ArmandLambrechts | 0:d9f4fedf5253 | 44 | canEN = 0; |
ArmandLambrechts | 0:d9f4fedf5253 | 45 | /* Config Loopback and Frequency */ |
ArmandLambrechts | 0:d9f4fedf5253 | 46 | send_data.data = 0; |
ArmandLambrechts | 0:d9f4fedf5253 | 47 | can.frequency(1000000); |
ArmandLambrechts | 0:d9f4fedf5253 | 48 | //can.mode(CAN::LocalTest); |
ArmandLambrechts | 0:d9f4fedf5253 | 49 | |
ArmandLambrechts | 0:d9f4fedf5253 | 50 | /* Loop */ |
ArmandLambrechts | 0:d9f4fedf5253 | 51 | while (true) { |
ArmandLambrechts | 0:d9f4fedf5253 | 52 | |
ArmandLambrechts | 0:d9f4fedf5253 | 53 | /* Send */ |
ArmandLambrechts | 0:d9f4fedf5253 | 54 | send_status = can.write(CANMessage(send_id, (char*) &send_data, 8)); |
ArmandLambrechts | 0:d9f4fedf5253 | 55 | |
ArmandLambrechts | 0:d9f4fedf5253 | 56 | if (send_status == true) { |
ArmandLambrechts | 0:d9f4fedf5253 | 57 | send_data.data = send_data.data + 1; |
ArmandLambrechts | 0:d9f4fedf5253 | 58 | } |
ArmandLambrechts | 0:d9f4fedf5253 | 59 | |
ArmandLambrechts | 0:d9f4fedf5253 | 60 | /* Receive */ |
ArmandLambrechts | 0:d9f4fedf5253 | 61 | if (can.read(msg)) { |
ArmandLambrechts | 0:d9f4fedf5253 | 62 | //move message bytes to receive, so we can access as a uint64_t (u long long) |
ArmandLambrechts | 0:d9f4fedf5253 | 63 | receive_data.bytes.b7 = msg.data[7]; |
ArmandLambrechts | 0:d9f4fedf5253 | 64 | receive_data.bytes.b6 = msg.data[6]; |
ArmandLambrechts | 0:d9f4fedf5253 | 65 | receive_data.bytes.b5 = msg.data[5]; |
ArmandLambrechts | 0:d9f4fedf5253 | 66 | receive_data.bytes.b4 = msg.data[4]; |
ArmandLambrechts | 0:d9f4fedf5253 | 67 | receive_data.bytes.b3 = msg.data[3]; |
ArmandLambrechts | 0:d9f4fedf5253 | 68 | receive_data.bytes.b2 = msg.data[2]; |
ArmandLambrechts | 0:d9f4fedf5253 | 69 | receive_data.bytes.b1 = msg.data[1]; |
ArmandLambrechts | 0:d9f4fedf5253 | 70 | receive_data.bytes.b0 = msg.data[0]; |
ArmandLambrechts | 0:d9f4fedf5253 | 71 | |
ArmandLambrechts | 0:d9f4fedf5253 | 72 | printf("Message received ID,Msg: %d, %llu\r\n", msg.id, receive_data.data); |
ArmandLambrechts | 0:d9f4fedf5253 | 73 | } |
ArmandLambrechts | 0:d9f4fedf5253 | 74 | |
ArmandLambrechts | 0:d9f4fedf5253 | 75 | /* Heartbeat */ |
ArmandLambrechts | 0:d9f4fedf5253 | 76 | led = !led; |
ArmandLambrechts | 0:d9f4fedf5253 | 77 | |
ArmandLambrechts | 0:d9f4fedf5253 | 78 | wait(0.5); |
ArmandLambrechts | 0:d9f4fedf5253 | 79 | } //while |
ArmandLambrechts | 0:d9f4fedf5253 | 80 | } |