Skip to content

Commit 57e8810

Browse files
authored
Merge pull request arduino#47 from bcmi-labs/can-bitrate
Support different CAN bitrates
2 parents 63de8e0 + e9135f5 commit 57e8810

File tree

18 files changed

+18494
-50
lines changed

18 files changed

+18494
-50
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************************
2+
* INCLUDE
3+
**************************************************************************************/
4+
5+
#include <CAN.h>
6+
7+
/**************************************************************************************
8+
* SETUP/LOOP
9+
**************************************************************************************/
10+
11+
void setup()
12+
{
13+
Serial.begin(115200);
14+
while (!Serial) { }
15+
16+
/* You need to enable the CAN transceiver
17+
* by commenting in below code when using
18+
* a Portenta H33 on a Portenta Max Carrier.
19+
* Note: Only CAN1 is available on the Portenta
20+
* Max Carrier's RJ10 CAN connector.
21+
*/
22+
#if (PIN_CAN1_STBY >= 0)
23+
pinMode(PIN_CAN1_STBY, OUTPUT);
24+
digitalWrite(PIN_CAN1_STBY, LOW);
25+
#endif
26+
27+
if (!CAN1.begin(CanBitRate::BR_250k))
28+
{
29+
Serial.println("CAN.begin(...) failed.");
30+
for (;;) {}
31+
}
32+
}
33+
34+
void loop()
35+
{
36+
if (CAN1.available())
37+
{
38+
CanMsg const msg = CAN1.read();
39+
Serial.println(msg);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**************************************************************************************
2+
* INCLUDE
3+
**************************************************************************************/
4+
5+
#include <CAN.h>
6+
7+
/**************************************************************************************
8+
* CONSTANTS
9+
**************************************************************************************/
10+
11+
static uint32_t const CAN_ID = 0x20;
12+
13+
/**************************************************************************************
14+
* SETUP/LOOP
15+
**************************************************************************************/
16+
17+
void setup()
18+
{
19+
//Serial.begin(115200);
20+
//while (!Serial) { }
21+
22+
/* You need to enable the CAN transceiver
23+
* by commenting in below code when using
24+
* a Portenta H33 on a Portenta Max Carrier.
25+
* Note: Only CAN1 is available on the Portenta
26+
* Max Carrier's RJ10 CAN connector.
27+
*/
28+
#if (PIN_CAN1_STBY >= 0)
29+
pinMode(PIN_CAN1_STBY, OUTPUT);
30+
digitalWrite(PIN_CAN1_STBY, LOW);
31+
#endif
32+
33+
if (!CAN1.begin(CanBitRate::BR_125k))
34+
{
35+
//Serial.println("CAN.begin(...) failed.");
36+
for (;;) {}
37+
}
38+
}
39+
40+
static uint32_t msg_cnt = 0;
41+
42+
void loop()
43+
{
44+
/* Assemble a CAN message with the format of
45+
* 0xCA 0xFE 0x00 0x00 [4 byte message counter]
46+
*/
47+
uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0};
48+
memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt));
49+
CanMsg msg(CAN_ID, sizeof(msg_data), msg_data);
50+
51+
/* Transmit the CAN message, capture and display an
52+
* error core in case of failure.
53+
*/
54+
if (int const rc = CAN1.write(msg); rc < 0)
55+
{
56+
//Serial.print ("CAN.write(...) failed with error code ");
57+
//Serial.println(rc);
58+
for (;;) { }
59+
}
60+
61+
/* Increase the message counter. */
62+
msg_cnt++;
63+
64+
/* Only send one message per second. */
65+
delay(1000);
66+
}

libraries/CAN/examples/CANWrite/CANWrite.ino

-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ void setup()
1919
Serial.begin(115200);
2020
while (!Serial) { }
2121

22-
/* You need to enable the CAN transceiver
23-
* by commenting in below code when using
24-
* a Portenta H33 on a Portenta Max Carrier.
25-
* Note: Only CAN1 is available on the Portenta
26-
* Max Carrier's RJ10 CAN connector.
27-
*/
28-
pinMode(PIN_CAN1_STBY, OUTPUT);
29-
digitalWrite(PIN_CAN1_STBY, LOW);
30-
3122
if (!CAN.begin(CanBitRate::BR_250k))
3223
{
3324
Serial.println("CAN.begin(...) failed.");

libraries/CAN/extras/test/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
##########################################################################
2+
cmake_minimum_required(VERSION 2.8)
3+
##########################################################################
4+
project(can-test)
5+
##########################################################################
6+
include_directories(../../src)
7+
include_directories(external/catch2/v2.13.10/include)
8+
##########################################################################
9+
set(CMAKE_CXX_STANDARD 17)
10+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
11+
##########################################################################
12+
set(TEST_TARGET ${CMAKE_PROJECT_NAME})
13+
##########################################################################
14+
set(TEST_TARGET_SRCS
15+
src/test_calc_can_bit_timinig.cpp
16+
src/test_main.cpp
17+
../../src/CanUtil.cpp
18+
)
19+
##########################################################################
20+
add_compile_options(-Wall -Wextra -Wpedantic)
21+
##########################################################################
22+
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage")
23+
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage -Wno-deprecated-copy")
24+
##########################################################################
25+
add_executable(
26+
${TEST_TARGET}
27+
${TEST_TARGET_SRCS}
28+
)
29+
##########################################################################

libraries/CAN/extras/test/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
`CAN/test`
2+
==========
3+
This directory contains CAN library test code for compilation and execution on host.
4+
5+
#### How-to-build/run
6+
```bash
7+
mkdir build && cd build
8+
cmake ..
9+
make && bin/can-test
10+
```

0 commit comments

Comments
 (0)