Skip to content

Commit ca2c7c5

Browse files
add CAN example
1 parent b2c17d2 commit ca2c7c5

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

APIs_Drivers/CAN_ex_1/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CAN example
2+
3+
This example sends a counter from one CAN bus (can1) and listens for a packet on the other CAN bus (can2).
4+
Each bus controller should be connected to a CAN bus transceiver.
5+
These should be connected together at a CAN bus.

APIs_Drivers/CAN_ex_1/main.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2017-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
#if DEVICE_CAN
9+
10+
Ticker ticker;
11+
DigitalOut led1(LED1);
12+
DigitalOut led2(LED2);
13+
/** The constructor takes in RX, and TX pin respectively.
14+
* These pins, for this example, are defined in mbed_app.json
15+
*/
16+
CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
17+
CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
18+
char counter = 0;
19+
20+
void send()
21+
{
22+
printf("send()\n");
23+
if (can1.write(CANMessage(1337, &counter, 1))) {
24+
printf("wloop()\n");
25+
counter++;
26+
printf("Message sent: %d\n", counter);
27+
}
28+
led1 = !led1;
29+
}
30+
31+
int main()
32+
{
33+
printf("main()\n");
34+
ticker.attach(&send, 1);
35+
CANMessage msg;
36+
while (1) {
37+
printf("loop()\n");
38+
if (can2.read(msg)) {
39+
printf("Message received: %d\n", msg.data[0]);
40+
led2 = !led2;
41+
}
42+
ThisThread::sleep_for(200);
43+
}
44+
}
45+
46+
#endif

APIs_Drivers/CAN_ex_1/mbed_app.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"config": {
3+
"CAN1_RD": {
4+
"help": "CAN1 rd pin",
5+
"value": "NC"
6+
},
7+
"CAN1_TD": {
8+
"help": "CAN1 td pin",
9+
"value": "NC"
10+
},
11+
"CAN2_RD": {
12+
"help": "CAN2 rd pin",
13+
"value": "NC"
14+
},
15+
"CAN2_TD": {
16+
"help": "CAN2 td pin",
17+
"value": "NC"
18+
}
19+
20+
},
21+
"target_overrides": {
22+
"*": {
23+
"CAN1_RD": "p9",
24+
"CAN1_TD": "p10",
25+
"CAN2_RD": "p30",
26+
"CAN2_TD": "p29"
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)