Skip to content

Commit a6af34e

Browse files
committedJul 27, 2023
COMM class refactoring
1 parent b0458ad commit a6af34e

File tree

7 files changed

+232
-111
lines changed

7 files changed

+232
-111
lines changed
 

‎examples/CAN/ReadCan/ReadCan.ino

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@
1717
#define DATARATE_1MB 1000000
1818
#define DATARATE_800KB 800000
1919

20-
2120
void setup() {
2221
Serial.begin(9600);
2322
while (!Serial) {
2423
; // wait for serial port to connect.
2524
}
2625

2726
Serial.println("Start CAN initialization");
28-
comm_protocols.enableCAN();
29-
comm_protocols.can.frequency(DATARATE_800KB);
27+
MachineControl_CommProtocols.begin();
28+
MachineControl_CommProtocols.CANEnable();
29+
30+
MachineControl_CommProtocols.CAN.frequency(DATARATE_800KB);
3031
Serial.println("Initialization done");
3132
}
3233

33-
3434
void loop() {
3535
mbed::CANMessage msg;
36-
if (comm_protocols.can.read(msg)) {
36+
if (MachineControl_CommProtocols.CAN.read(msg)) {
3737

3838
// Print the sender ID
3939
Serial.print("ID: ");

‎examples/CAN/WriteCan/WriteCan.ino

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
#define DATARATE_1MB 1000000
1818
#define DATARATE_800KB 800000
1919

20-
2120
void setup() {
2221
Serial.begin(9600);
2322
while (!Serial) {
2423
; // wait for serial port to connect.
2524
}
2625

2726
Serial.println("Start CAN initialization");
28-
comm_protocols.enableCAN();
29-
comm_protocols.can.frequency(DATARATE_800KB);
27+
28+
MachineControl_CommProtocols.begin();
29+
MachineControl_CommProtocols.CANEnable();
30+
MachineControl_CommProtocols.CAN.frequency(DATARATE_800KB);
3031
Serial.println("Initialization done");
3132
}
3233

@@ -37,12 +38,12 @@ int payload_size = 1;
3738
void loop() {
3839

3940
mbed::CANMessage msg = mbed::CANMessage(13ul, &payload, payload_size);
40-
if (comm_protocols.can.write(msg)) {
41+
if (MachineControl_CommProtocols.CAN.write(msg)) {
4142
Serial.println("Message sent");
4243
} else {
4344
Serial.println("Transmission Error: ");
44-
Serial.println(comm_protocols.can.tderror());
45-
comm_protocols.can.reset();
45+
Serial.println(MachineControl_CommProtocols.CAN.tderror());
46+
MachineControl_CommProtocols.CAN.reset();
4647
}
4748

4849
delay(100);

‎examples/RS232/RS232.ino

+12-12
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void setup()
3434
Serial.println("Start RS232 initialization");
3535

3636
// Set the PMC Communication Protocols to default config
37-
comm_protocols.init();
37+
MachineControl_CommProtocols.begin();
3838

3939
// RS485/RS232 default config is:
4040
// - RS485/RS232 system disabled
@@ -43,22 +43,22 @@ void setup()
4343
// - No A/B and Y/Z 120 Ohm termination enabled
4444

4545
// Enable the RS485/RS232 system
46-
comm_protocols.rs485Enable(true);
46+
MachineControl_CommProtocols.RS485Enable();
4747
// Enable the RS232 mode
48-
comm_protocols.rs485ModeRS232(true);
48+
MachineControl_CommProtocols.RS485SetModeRS232(true);
4949

5050
// Specify baudrate for RS232 communication
51-
comm_protocols.rs485.begin(115200);
51+
MachineControl_CommProtocols.RS485.begin(115200);
5252
// Start in receive mode
53-
comm_protocols.rs485.receive();
53+
MachineControl_CommProtocols.RS485.receive();
5454

5555
Serial.println("Initialization done!");
5656
}
5757

5858
void loop()
5959
{
60-
if (comm_protocols.rs485.available())
61-
Serial.write(comm_protocols.rs485.read());
60+
if (MachineControl_CommProtocols.RS485.available())
61+
Serial.write(MachineControl_CommProtocols.RS485.read());
6262

6363
if (millis() > sendNow) {
6464
String log = "[";
@@ -72,14 +72,14 @@ void loop()
7272
Serial.println(log);
7373

7474
// Disable receive mode before transmission
75-
comm_protocols.rs485.noReceive();
75+
MachineControl_CommProtocols.RS485.noReceive();
7676

77-
comm_protocols.rs485.beginTransmission();
78-
comm_protocols.rs485.println(msg);
79-
comm_protocols.rs485.endTransmission();
77+
MachineControl_CommProtocols.RS485.beginTransmission();
78+
MachineControl_CommProtocols.RS485.println(msg);
79+
MachineControl_CommProtocols.RS485.endTransmission();
8080

8181
// Re-enable receive mode after transmission
82-
comm_protocols.rs485.receive();
82+
MachineControl_CommProtocols.RS485.receive();
8383

8484
sendNow = millis() + sendInterval;
8585
}

‎examples/RS485_fullduplex/RS485_fullduplex.ino

+13-13
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,47 @@ void setup()
3232
Serial.println("Start RS485 initialization");
3333

3434
// Set the PMC Communication Protocols to default config
35-
comm_protocols.init();
35+
MachineControl_CommProtocols.begin();
3636
// RS485/RS232 default config is:
3737
// - RS485 mode
3838
// - Half Duplex
3939
// - No A/B and Y/Z 120 Ohm termination enabled
4040

4141
// Enable the RS485/RS232 system
42-
comm_protocols.rs485Enable(true);
42+
MachineControl_CommProtocols.RS485Enable();
4343

4444
// Enable Full Duplex mode
4545
// This will also enable A/B and Y/Z 120 Ohm termination resistors
46-
comm_protocols.rs485FullDuplex(true);
46+
MachineControl_CommProtocols.RS485SetFullDuplex(true);
4747

4848
// Specify baudrate, and preamble and postamble times for RS485 communication
49-
comm_protocols.rs485.begin(115200, 0, 500);
49+
MachineControl_CommProtocols.RS485.begin(115200, 0, 500);
5050

5151
// Start in receive mode
52-
comm_protocols.rs485.receive();
52+
MachineControl_CommProtocols.RS485.receive();
5353

5454

5555
Serial.println("Initialization done!");
5656
}
5757

5858
void loop()
5959
{
60-
if (comm_protocols.rs485.available())
61-
Serial.write(comm_protocols.rs485.read());
60+
if (MachineControl_CommProtocols.RS485.available())
61+
Serial.write(MachineControl_CommProtocols.RS485.read());
6262

6363
if (millis() > sendNow) {
6464
// Disable receive mode before transmission
65-
comm_protocols.rs485.noReceive();
65+
MachineControl_CommProtocols.RS485.noReceive();
6666

67-
comm_protocols.rs485.beginTransmission();
67+
MachineControl_CommProtocols.RS485.beginTransmission();
6868

69-
comm_protocols.rs485.print("hello ");
70-
comm_protocols.rs485.println(counter++);
69+
MachineControl_CommProtocols.RS485.print("hello ");
70+
MachineControl_CommProtocols.RS485.println(counter++);
7171

72-
comm_protocols.rs485.endTransmission();
72+
MachineControl_CommProtocols.RS485.endTransmission();
7373

7474
// Re-enable receive mode after transmission
75-
comm_protocols.rs485.receive();
75+
MachineControl_CommProtocols.RS485.receive();
7676

7777
sendNow = millis() + sendInterval;
7878
}

‎examples/RS485_halfduplex/RS485_halfduplex.ino

+12-12
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,42 @@ void setup()
3333
Serial.println("Start RS485 initialization");
3434

3535
// Set the PMC Communication Protocols to default config
36-
comm_protocols.init();
36+
MachineControl_CommProtocols.begin();
3737

3838
// RS485/RS232 default config is:
3939
// - RS485 mode
4040
// - Half Duplex
4141
// - No A/B and Y/Z 120 Ohm termination enabled
4242

4343
// Enable the RS485/RS232 system
44-
comm_protocols.rs485Enable(true);
44+
MachineControl_CommProtocols.RS485Enable();
4545

4646
// Specify baudrate, and preamble and postamble times for RS485 communication
47-
comm_protocols.rs485.begin(115200, 0, 500);
47+
MachineControl_CommProtocols.RS485.begin(115200, 0, 500);
4848
// Start in receive mode
49-
comm_protocols.rs485.receive();
49+
MachineControl_CommProtocols.RS485.receive();
5050

5151
Serial.println("Initialization done!");
5252
}
5353

5454
void loop()
5555
{
56-
if (comm_protocols.rs485.available())
57-
Serial.write(comm_protocols.rs485.read());
56+
if (MachineControl_CommProtocols.RS485.available())
57+
Serial.write(MachineControl_CommProtocols.RS485.read());
5858

5959
if (millis() > sendNow) {
6060
// Disable receive mode before transmission
61-
comm_protocols.rs485.noReceive();
61+
MachineControl_CommProtocols.RS485.noReceive();
6262

63-
comm_protocols.rs485.beginTransmission();
63+
MachineControl_CommProtocols.RS485.beginTransmission();
6464

65-
comm_protocols.rs485.print("hello ");
66-
comm_protocols.rs485.println(counter++);
65+
MachineControl_CommProtocols.RS485.print("hello ");
66+
MachineControl_CommProtocols.RS485.println(counter++);
6767

68-
comm_protocols.rs485.endTransmission();
68+
MachineControl_CommProtocols.RS485.endTransmission();
6969

7070
// Re-enable receive mode after transmission
71-
comm_protocols.rs485.receive();
71+
MachineControl_CommProtocols.RS485.receive();
7272

7373
sendNow = millis() + sendInterval;
7474
}

‎src/COMMClass.cpp

+71-25
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,93 @@
1+
/**
2+
* @file COMMClass.cpp
3+
* @author Leonardo Cavagnis
4+
* @brief Source file for the COMMClass used to initialize and interact with communication protocols (CAN Bus, RS485, and RS232) on the Portenta Machine Control board.
5+
*
6+
*/
7+
8+
/* Includes -----------------------------------------------------------------*/
19
#include "COMMClass.h"
210

3-
void COMMClass::init() {
4-
//SHUTDOWN OF RS485 LEDS
5-
digitalWrite(PinNameToIndex(PA_0), LOW);
6-
digitalWrite(PinNameToIndex(PI_9), LOW);
7-
//SHUTDOWN OF CAN LEDS
11+
/* Functions -----------------------------------------------------------------*/
12+
COMMClass::COMMClass() {
13+
pinMode(PinNameToIndex(PA_13), OUTPUT); //Disable CAN pin
14+
pinMode(PinNameToIndex(PB_8), OUTPUT);
15+
pinMode(PinNameToIndex(PH_13), OUTPUT);
16+
17+
pinMode(PinNameToIndex(PA_0), OUTPUT);
18+
pinMode(PinNameToIndex(PI_9), OUTPUT);
19+
20+
pinMode(PinNameToIndex(PG_9), OUTPUT);
21+
pinMode(PinNameToIndex(PA_10), OUTPUT);
22+
pinMode(PinNameToIndex(PI_15), OUTPUT);
23+
pinMode(PinNameToIndex(PI_14), OUTPUT);
24+
pinMode(PinNameToIndex(PG_14), OUTPUT);
25+
pinMode(PinNameToIndex(PA_9), OUTPUT);
26+
}
27+
28+
COMMClass::~COMMClass()
29+
{ }
30+
31+
bool COMMClass::begin() {
32+
/* Turn-off LEDs CAN */
833
digitalWrite(PinNameToIndex(PB_8), HIGH);
934
digitalWrite(PinNameToIndex(PH_13), HIGH);
1035

11-
// SET DEFAULTS for RS485
12-
rs485Enable(false);
13-
rs485ModeRS232(false);
14-
rs485FullDuplex(false);
15-
rs485YZTerm(false);
16-
rs485ABTerm(false);
17-
rs485Slew(false);
36+
/* Turn-off LEDs RS485 */
37+
digitalWrite(PinNameToIndex(PA_0), LOW);
38+
digitalWrite(PinNameToIndex(PI_9), LOW);
39+
40+
/* Set defaults for RS485 */
41+
RS485Disable();
42+
RS485SetModeRS232(false);
43+
RS485SetFullDuplex(false);
44+
RS485SetYZTerm(false);
45+
RS485SetABTerm(false);
46+
RS485SetSlew(false);
47+
48+
return true;
1849
}
1950

20-
void COMMClass::enableCAN() {
21-
can_disable = 0;
51+
void COMMClass::CANEnable() {
52+
digitalWrite(PinNameToIndex(PA_13), LOW);
2253
}
2354

24-
void COMMClass::disableCAN() {
25-
can_disable = 1;
55+
void COMMClass::CANDisable() {
56+
digitalWrite(PinNameToIndex(PA_13), HIGH);
2657
}
2758

28-
void COMMClass::rs485Enable(bool enable) { digitalWrite(PinNameToIndex(PG_9), enable ? HIGH : LOW); }
59+
void COMMClass::RS485Enable() {
60+
digitalWrite(PinNameToIndex(PG_9), HIGH);
61+
}
2962

30-
void COMMClass::rs485ModeRS232(bool enable) { digitalWrite(PinNameToIndex(PA_10), enable ? LOW : HIGH); }
63+
void COMMClass::RS485Disable() {
64+
digitalWrite(PinNameToIndex(PG_9), LOW);
65+
}
3166

32-
void COMMClass::rs485YZTerm(bool enable) { digitalWrite(PinNameToIndex(PI_15), enable ? HIGH : LOW); }
67+
void COMMClass::RS485SetModeRS232(bool enable) {
68+
digitalWrite(PinNameToIndex(PA_10), enable ? LOW : HIGH);
69+
}
3370

34-
void COMMClass::rs485ABTerm(bool enable) { digitalWrite(PinNameToIndex(PI_14), enable ? HIGH : LOW); }
71+
void COMMClass::RS485SetYZTerm(bool enable) {
72+
digitalWrite(PinNameToIndex(PI_15), enable ? HIGH : LOW);
73+
}
74+
75+
void COMMClass::RS485SetABTerm(bool enable) {
76+
digitalWrite(PinNameToIndex(PI_14), enable ? HIGH : LOW);
77+
}
3578

36-
void COMMClass::rs485Slew(bool enable) { digitalWrite(PinNameToIndex(PG_14), enable ? LOW : HIGH); }
79+
void COMMClass::RS485SetSlew(bool enable) {
80+
digitalWrite(PinNameToIndex(PG_14), enable ? LOW : HIGH);
81+
}
3782

38-
void COMMClass::rs485FullDuplex(bool enable) {
83+
void COMMClass::RS485SetFullDuplex(bool enable) {
3984
digitalWrite(PinNameToIndex(PA_9), enable ? LOW : HIGH);
4085
if (enable) {
4186
// RS485 Full Duplex require YZ and AB 120 Ohm termination enabled
42-
rs485YZTerm(true);
43-
rs485ABTerm(true);
87+
RS485SetYZTerm(true);
88+
RS485SetABTerm(true);
4489
}
4590
}
4691

47-
COMMClass comm_protocols;
92+
COMMClass MachineControl_CommProtocols;
93+
/**** END OF FILE ****/

‎src/COMMClass.h

+112-38
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,123 @@
1+
/**
2+
* @file COMMClass.h
3+
* @author Leonardo Cavagnis
4+
* @brief Header file for the COMMClass used to initialize and interact with communication protocols (CAN Bus, RS485, and RS232) on the Portenta Machine Control board.
5+
*
6+
* This library provides a class to manage the communication protocols of the Portenta Machine Control board.
7+
* It allows initializing and interacting with the CAN Bus, RS485, and RS232 protocols. The library also initializes the corresponding LEDs for CAN and RS485.
8+
*/
9+
10+
#ifndef __COMM_CLASS_H
11+
#define __COMM_CLASS_H
12+
13+
/* Includes -------------------------------------------------------------------*/
114
#include <ArduinoRS485.h>
215
#include <Arduino.h>
316
#include <pinDefinitions.h>
417
#include <mbed.h>
518

19+
/* Class ----------------------------------------------------------------------*/
20+
621
/**
7-
* The COMMClass is used to initialize the CAN and RS485 LEDs and
8-
* establish the power mode of the CAN bus.
22+
* @class COMMClass
23+
* @brief Class for managing the communication protocols of the Portenta Machine Control.
924
*/
1025
class COMMClass {
1126
public:
12-
// to be tested: check if can be made a big pin initialization
13-
14-
/**
15-
* Shutdown RS485 and CAN LEDs
16-
*/
17-
void init();
18-
19-
/**
20-
* Set the CAN transceiver in Normal mode. In this mode, the transceiver
21-
* can transmit and receive data via the bus lines CANH and CANL.
22-
*/
23-
void enableCAN();
24-
25-
/**
26-
* Set the CAN transceiver in standby (low power) mode. In this mode the
27-
* transceiver will not be able to transmit or correctly receive data via the bus lines.
28-
* The wake-up filter on the output of the low-power receiver does not latch bus dominant states,
29-
* but ensures that only bus dominant and bus recessive states that persist longer than tfltr(wake)
30-
* bus are reflected on pin RXD.
31-
*/
32-
void disableCAN();
33-
34-
void rs485Enable(bool enable);
35-
void rs485ModeRS232(bool enable);
36-
void rs485YZTerm(bool enable);
37-
void rs485ABTerm(bool enable);
38-
void rs485Slew(bool enable);
39-
void rs485FullDuplex(bool enable);
40-
41-
arduino::UART _UART4_ {PA_0, PI_9, NC, NC};
42-
mbed::CAN can {PB_8, PH_13};
43-
RS485Class rs485 {_UART4_, PinNameToIndex(PA_0), PinNameToIndex(PI_13), PinNameToIndex(PI_10)};
44-
45-
private:
46-
mbed::DigitalOut can_disable = mbed::DigitalOut(PA_13, 0);
27+
/**
28+
* @brief Construct a COMMClass object.
29+
*
30+
* This constructor initializes a COMMClass object.
31+
*/
32+
COMMClass();
33+
34+
/**
35+
* @brief Destruct the COMMClass object.
36+
*
37+
* This destructor releases any resources used by the COMMClass object.
38+
*/
39+
~COMMClass();
40+
41+
/**
42+
* @brief Begin the communication protocols.
43+
*
44+
* This method initializes the communication protocols, including the RS485 and CAN LEDs.
45+
*
46+
* @return true If the initialization is successful, false otherwise.
47+
*/
48+
bool begin();
49+
50+
/**
51+
* @brief Set the CAN transceiver in Normal mode.
52+
*
53+
* In this mode, the transceiver can transmit and receive data via the bus lines CANH and CANL.
54+
*/
55+
void CANEnable();
56+
57+
/**
58+
* @brief Set the CAN transceiver in Standby (low power) mode.
59+
*
60+
* In this mode, the transceiver will not be able to transmit or correctly receive data via the bus lines.
61+
* The wake-up filter on the output of the low-power receiver does not latch bus dominant states,
62+
* but ensures that only bus dominant and bus recessive states that persist longer than tfltr(wake)
63+
* bus are reflected on pin RXD.
64+
*/
65+
void CANDisable();
66+
67+
/**
68+
* @brief Enable RS485 communication.
69+
*
70+
* This method enables RS485 communication.
71+
*/
72+
void RS485Enable();
73+
74+
/**
75+
* @brief Disable RS485 communication.
76+
*
77+
* This method disables RS485 communication.
78+
*/
79+
void RS485Disable();
80+
81+
/**
82+
* @brief Set RS485 mode to RS232.
83+
*
84+
* @param enable If true, sets the RS485 mode to RS232, else sets to RS485 mode.
85+
*/
86+
void RS485SetModeRS232(bool enable);
87+
88+
/**
89+
* @brief Set YZ termination for RS485 communication.
90+
*
91+
* @param enable If true, enables YZ termination, else disables it.
92+
*/
93+
void RS485SetYZTerm(bool enable);
94+
95+
/**
96+
* @brief Set AB termination for RS485 communication.
97+
*
98+
* @param enable If true, enables AB termination, else disables it.
99+
*/
100+
void RS485SetABTerm(bool enable);
101+
102+
/**
103+
* @brief Set the slew rate for RS485 communication.
104+
*
105+
* @param enable If true, enables the slew rate control, else disables it.
106+
*/
107+
void RS485SetSlew(bool enable);
108+
109+
/**
110+
* @brief Set RS485 communication to Full Duplex mode.
111+
*
112+
* @param enable If true, sets RS485 communication to Full Duplex mode, else to Half Duplex mode.
113+
*/
114+
void RS485SetFullDuplex(bool enable);
115+
116+
arduino::UART _UART4_ {PA_0, PI_9, NC, NC};
117+
mbed::CAN CAN {PB_8, PH_13};
118+
RS485Class RS485 {_UART4_, PinNameToIndex(PA_0), PinNameToIndex(PI_13), PinNameToIndex(PI_10)};
47119
};
48120

49-
extern COMMClass comm_protocols;
121+
extern COMMClass MachineControl_CommProtocols;
122+
123+
#endif /* __COMM_CLASS_H */

0 commit comments

Comments
 (0)
Please sign in to comment.