Skip to content

add CAN example #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions APIs_Drivers/CAN_ex_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CAN example

This example sends a counter from one CAN bus (can1) and listens for a packet on the other CAN bus (can2).
Each bus controller should be connected to a CAN bus transceiver.
These should be connected together at a CAN bus.
50 changes: 50 additions & 0 deletions APIs_Drivers/CAN_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#if !DEVICE_CAN
#error [NOT_SUPPORTED] CAN not supported for this target
#endif


#include "mbed.h"


Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
/** The constructor takes in RX, and TX pin respectively.
* These pins, for this example, are defined in mbed_app.json
*/
CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
char counter = 0;

void send()
{
printf("send()\n");
if (can1.write(CANMessage(1337, &counter, 1))) {
printf("wloop()\n");
counter++;
printf("Message sent: %d\n", counter);
}
led1 = !led1;
}

int main()
{
printf("main()\n");
ticker.attach(&send, 1);
CANMessage msg;
while (1) {
printf("loop()\n");
if (can2.read(msg)) {
printf("Message received: %d\n", msg.data[0]);
led2 = !led2;
}
ThisThread::sleep_for(200);
}
}

#endif
29 changes: 29 additions & 0 deletions APIs_Drivers/CAN_ex_1/mbed_app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"config": {
"CAN1_RD": {
"help": "CAN1 rd pin",
"value": "NC"
},
"CAN1_TD": {
"help": "CAN1 td pin",
"value": "NC"
},
"CAN2_RD": {
"help": "CAN2 rd pin",
"value": "NC"
},
"CAN2_TD": {
"help": "CAN2 td pin",
"value": "NC"
}

},
"target_overrides": {
"*": {
"CAN1_RD": "p9",
"CAN1_TD": "p10",
"CAN2_RD": "p30",
"CAN2_TD": "p29"
}
}
}