Skip to content

Commit c091f4a

Browse files
reimplement CANCommClass using Arduino_CAN library
1 parent 50463b8 commit c091f4a

File tree

4 files changed

+107
-62
lines changed

4 files changed

+107
-62
lines changed

examples/CAN/ReadCan/ReadCan.ino

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,25 @@
1111
*/
1212
#include <Arduino_MachineControl.h>
1313

14-
#define DATARATE_2MB 2000000
15-
#define DATARATE_1_5MB 1500000
16-
#define DATARATE_1MB 1000000
17-
#define DATARATE_800KB 800000
18-
19-
void setup() {
14+
void setup()
15+
{
2016
Serial.begin(9600);
2117
while (!Serial) {
2218
; // wait for serial port to connect.
2319
}
2420

25-
Serial.println("Start CAN initialization");
26-
MachineControl_CANComm.begin(DATARATE_800KB);
27-
28-
Serial.println("Initialization done");
21+
if (!MachineControl_CANComm.begin(CanBitRate::BR_500k))
22+
{
23+
Serial.println("CAN init failed.");
24+
for (;;) {}
25+
}
2926
}
3027

31-
void loop() {
32-
mbed::CANMessage msg;
33-
if (MachineControl_CANComm.read(msg)) {
34-
35-
// Print the sender ID
36-
Serial.print("ID: ");
37-
Serial.println(msg.id);
38-
39-
// Print the first Payload Byte
40-
Serial.print("Message received:");
41-
Serial.println(msg.data[0], DEC);
28+
void loop()
29+
{
30+
if (MachineControl_CANComm.available())
31+
{
32+
CanMsg const msg = MachineControl_CANComm.read();
33+
Serial.println(msg);
4234
}
43-
44-
delay(100);
4535
}

examples/CAN/WriteCan/WriteCan.ino

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,46 @@
1111
*/
1212
#include <Arduino_MachineControl.h>
1313

14-
#define DATARATE_2MB 2000000
15-
#define DATARATE_1_5MB 1500000
16-
#define DATARATE_1MB 1000000
17-
#define DATARATE_800KB 800000
14+
static uint32_t const CAN_ID = 13ul;
1815

19-
void setup() {
16+
void setup()
17+
{
2018
Serial.begin(9600);
2119
while (!Serial) {
2220
; // wait for serial port to connect.
2321
}
2422

25-
Serial.println("Start CAN initialization");
26-
27-
MachineControl_CANComm.begin(DATARATE_800KB);
28-
Serial.println("Initialization done");
23+
if (!MachineControl_CANComm.begin(CanBitRate::BR_500k))
24+
{
25+
Serial.println("CAN init failed.");
26+
for (;;) {}
27+
}
2928
}
3029

31-
int counter = 0;
32-
unsigned char payload = 0x49;
33-
int payload_size = 1;
30+
static uint32_t msg_cnt = 0;
31+
32+
void loop()
33+
{
34+
/* Assemble a CAN message */
35+
uint8_t const msg_data[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
36+
memcpy((void *)(msg_data + 8), &msg_cnt, sizeof(msg_cnt));
37+
CanMsg msg(CAN_ID, sizeof(msg_data), msg_data);
38+
39+
/* Transmit the CAN message, capture and display an
40+
* error core in case of failure.
41+
*/
42+
if (int const rc = MachineControl_CANComm.write(msg); rc <= 0)
43+
{
44+
Serial.print ("CAN write failed with error code ");
45+
Serial.println(rc);
46+
for (;;) { }
47+
}
3448

35-
void loop() {
49+
Serial.println("CAN write message!");
3650

37-
mbed::CANMessage msg = mbed::CANMessage(13ul, &payload, payload_size);
38-
if (MachineControl_CANComm.write(msg)) {
39-
Serial.println("Message sent");
40-
} else {
41-
Serial.println("Transmission Error: ");
42-
Serial.println(MachineControl_CANComm.tderror());
43-
MachineControl_CANComm.reset();
44-
}
51+
/* Increase the message counter. */
52+
msg_cnt++;
4553

46-
delay(100);
47-
}
54+
/* Only send one message per second. */
55+
delay(1000);
56+
}

src/CANCommClass.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,45 @@
88
#include "CANCommClass.h"
99

1010
/* Functions -----------------------------------------------------------------*/
11-
CANCommClass::CANCommClass(PinName can_tx_pin, PinName can_rx_pin) :
12-
mbed::CAN(can_tx_pin, can_rx_pin)
11+
CANCommClass::CANCommClass(PinName can_tx_pin, PinName can_rx_pin, PinName can_stb_pin) :
12+
_can(can_rx_pin, can_tx_pin), _tx{can_tx_pin}, _rx{can_rx_pin}, _stb{can_stb_pin}
1313
{ }
1414

1515
CANCommClass::~CANCommClass()
1616
{ }
1717

18-
bool CANCommClass::begin(int can_bitrate) {
19-
pinMode(PinNameToIndex(PA_13), OUTPUT); //Disable CAN pin
20-
pinMode(PinNameToIndex(PB_8), OUTPUT);
21-
pinMode(PinNameToIndex(PH_13), OUTPUT);
18+
bool CANCommClass::begin(CanBitRate can_bitrate) {
19+
pinMode(PinNameToIndex(_stb), OUTPUT);
2220

2321
_enable();
2422

25-
return mbed::CAN::frequency(can_bitrate);
23+
return _can.begin(can_bitrate);
24+
}
25+
26+
int CANCommClass::write(CanMsg const & msg) {
27+
return _can.write(msg);
28+
}
29+
30+
size_t CANCommClass::available() {
31+
return _can.available();
32+
}
33+
34+
CanMsg CANCommClass::read() {
35+
return _can.read();
2636
}
2737

2838
void CANCommClass::end() {
2939
_disable();
40+
41+
_can.end();
3042
}
3143

3244
void CANCommClass::_enable() {
33-
digitalWrite(PinNameToIndex(PA_13), LOW);
45+
digitalWrite(PinNameToIndex(_stb), LOW);
3446
}
3547

3648
void CANCommClass::_disable() {
37-
digitalWrite(PinNameToIndex(PA_13), HIGH);
49+
digitalWrite(PinNameToIndex(_stb), HIGH);
3850
}
3951

4052
CANCommClass MachineControl_CANComm;

src/CANCommClass.h

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
#include <Arduino.h>
1515
#include <pinDefinitions.h>
1616
#include <mbed.h>
17+
#include <Arduino_CAN.h>
1718

1819
/* Class ----------------------------------------------------------------------*/
1920

2021
/**
2122
* @class CANCommClass
2223
* @brief Class for managing the CAN Bus communication protocol of the Portenta Machine Control.
2324
*
24-
* The `CANCommClass` is a subclass of `mbed::CAN` and provides methods to work with the CAN Bus communication protocol on the Portenta Machine Control board.
25-
* It includes initialization of the corresponding LED for CAN.
25+
* The `CANCommClass` provides methods to work with the CAN Bus communication protocol on the Portenta Machine Control board.
2626
*/
27-
class CANCommClass: public mbed::CAN { //TODO: Check ARDUINO API VERSION to use Arduino_CAN
27+
class CANCommClass {
2828
public:
2929
/**
3030
* @brief Construct a CANCommClass object.
@@ -33,8 +33,9 @@ class CANCommClass: public mbed::CAN { //TODO: Check ARDUINO API VERSION to use
3333
*
3434
* @param can_tx_pin The pin for transmitting data on the CAN Bus.
3535
* @param can_rx_pin The pin for receiving data on the CAN Bus.
36+
* @param can_stb_pin The pin to control the standby (low-power) mode of the CAN transceiver.
3637
*/
37-
CANCommClass(PinName can_tx_pin = PB_8, PinName can_rx_pin = PH_13);
38+
CANCommClass(PinName can_tx_pin = PB_8, PinName can_rx_pin = PH_13, PinName can_stb_pin = PA_13);
3839

3940
/**
4041
* @brief Destruct the CANCommClass object.
@@ -46,12 +47,40 @@ class CANCommClass: public mbed::CAN { //TODO: Check ARDUINO API VERSION to use
4647
/**
4748
* @brief Begin the CAN communication protocol.
4849
*
49-
* This method initializes the CAN communication protocol, including the corresponding LED for CAN.
50+
* This method initializes the CAN communication protocol.
5051
*
5152
* @param can_bitrate The desired bitrate for the CAN communication protocol.
5253
* @return true If the initialization is successful, false otherwise.
5354
*/
54-
bool begin(int can_bitrate);
55+
bool begin(CanBitRate can_bitrate);
56+
57+
/**
58+
* @brief Write a CAN message to the bus.
59+
*
60+
* This method sends a CAN message over the bus.
61+
*
62+
* @param msg The CAN message to be sent, represented by a `CanMsg` object.
63+
* @return The number of bytes sent or <=0 in case of an error.
64+
*/
65+
int write(CanMsg const & msg);
66+
67+
/**
68+
* @brief Check the number of available CAN messages in the receive buffer.
69+
*
70+
* This method checks the number of CAN messages available to read from the bus.
71+
*
72+
* @return The number of available CAN messages.
73+
*/
74+
size_t available();
75+
76+
/**
77+
* @brief Read a CAN message from the bus.
78+
*
79+
* This method reads a CAN message from the receive buffer.
80+
*
81+
* @return The received CAN message as a `CanMsg` object.
82+
*/
83+
CanMsg read();
5584

5685
/**
5786
* @brief Close the CAN communication protocol.
@@ -61,6 +90,11 @@ class CANCommClass: public mbed::CAN { //TODO: Check ARDUINO API VERSION to use
6190
void end();
6291

6392
private:
93+
Arduino_CAN _can;
94+
PinName _tx;
95+
PinName _rx;
96+
PinName _stb;
97+
6498
/**
6599
* @brief Set the CAN transceiver in Normal mode.
66100
*
@@ -81,4 +115,4 @@ class CANCommClass: public mbed::CAN { //TODO: Check ARDUINO API VERSION to use
81115

82116
extern CANCommClass MachineControl_CANComm;
83117

84-
#endif /* __CAN_COMM_CLASS_H */
118+
#endif /* __CAN_COMM_CLASS_H */

0 commit comments

Comments
 (0)