Skip to content

Commit bbc5cc4

Browse files
add CAN example (#101)
1 parent 1184253 commit bbc5cc4

File tree

3 files changed

+84
-0
lines changed

3 files changed

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