Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #58 from manchoz/rs485_refactor
Browse files Browse the repository at this point in the history
Refactor RS485 support
  • Loading branch information
aentinger authored Aug 30, 2021
2 parents c107e0a + 078a330 commit 305e09e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 274 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ jobs:
fqbn: arduino:mbed_portenta:envie_m7
platforms: |
- name: arduino:mbed_portenta
libraries: |
- source-path: ./
- name: ArduinoRS485
sketch-paths: |
- examples
enable-deltas-report: true
enable-warnings-report: true
sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }}
Expand Down
46 changes: 30 additions & 16 deletions examples/RS485_fullduplex/RS485_fullduplex.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
RS485 Full duplex communication
This sketch shows how to use the SP335ECR1 on the Machine
Control as a full duplex RS485 interface, how to periodically
Control as a full duplex (AB and YZ) RS485 interface, how to periodically
send a string on the RS485 TX channel and how to receive data
from the interface RX channel.
Circuit:
- Portenta H7
- Machine Control
- A Slave device with RS485 interface
- Connect TXP to A(+) and TXN to B(-)
- Connect RXP to Y(+) and RXN to Z(-)
*/

#include "Arduino_MachineControl.h"

using namespace machinecontrol;

constexpr unsigned long sendInterval { 1000 };
unsigned long sendNow { 0 };
unsigned long counter = 0;

void setup()
Expand All @@ -29,37 +33,47 @@ void setup()
delay(1000);
Serial.println("Start RS485 initialization");

comm_protocols.rs485.begin(115200);
comm_protocols.rs485.enable = 1; // SDHN_N
comm_protocols.rs485.sel_485 = 1; // RS485_RS232_N
comm_protocols.rs485.half_duplex = 0; // HALF_FULL_N
comm_protocols.rs485.receive(); // RE_N
comm_protocols.rs485.fd_tx_term = 1; // FD_TX_TERM - 120 ohm Y-Z termination enabled when both TERM and FD_TX_TERM are high
comm_protocols.rs485.term = 1; // TERM - 120 ohm A-B termination enabled when high
// Set the PMC Communication Protocols to default config
comm_protocols.init();
// RS485/RS232 default config is:
// - RS485 mode
// - Half Duplex
// - No A/B and Y/Z 120 Ohm termination enabled

// Enable the RS485/RS232 system
comm_protocols.rs485Enable(true);

// Enable Full Duplex mode
// This will also enable A/B and Y/Z 120 Ohm termination resistors
comm_protocols.rs485FullDuplex(true);

// Specify baudrate, and preamble and postamble times for RS485 communication
comm_protocols.rs485.begin(115200, 0, 500);

// Start in receive mode
comm_protocols.rs485.receive();


Serial.println("Initialization done!");
}

constexpr unsigned long sendInterval { 1000 };
unsigned long sendNow { 0 };

constexpr unsigned long halfFullInterval { 5000 };
unsigned long halfFull { 0 };
byte halfFullStatus { 0 };

void loop()
{
while (comm_protocols.rs485.available())
if (comm_protocols.rs485.available())
Serial.write(comm_protocols.rs485.read());

if (millis() > sendNow) {
// Disable receive mode before transmission
comm_protocols.rs485.noReceive();

comm_protocols.rs485.beginTransmission();

comm_protocols.rs485.print("hello ");
comm_protocols.rs485.println(counter++);

comm_protocols.rs485.endTransmission();

// Re-enable receive mode after transmission
comm_protocols.rs485.receive();

sendNow = millis() + sendInterval;
Expand Down
77 changes: 77 additions & 0 deletions examples/RS485_halfduplex/RS485_halfduplex.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
RS485 Half Duplex communication
This sketch shows how to use the SP335ECR1 on the Machine
Control as a half duplex (AB) RS485 interface, how to periodically
send a string on the RS485 TX channel and how to receive data
from the interface RX channel.
Circuit:
- Portenta H7
- Machine Control
- A Slave device with RS485 interface
- Connect TXP to A(+) and TXN to B(-)
*/

#include "Arduino_MachineControl.h"

using namespace machinecontrol;

constexpr unsigned long sendInterval { 1000 };
unsigned long sendNow { 0 };

unsigned long counter { 0 };

void setup()
{

Serial.begin(115200);
// Wait for Serial or start after 2.5s
for (auto const timeout = millis() + 2500; !Serial && timeout < millis(); delay(500))
;

delay(2500);
Serial.println("Start RS485 initialization");

// Set the PMC Communication Protocols to default config
comm_protocols.init();

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

// Enable the RS485/RS232 system
comm_protocols.rs485Enable(true);

// Specify baudrate, and preamble and postamble times for RS485 communication
comm_protocols.rs485.begin(115200, 0, 500);
// Start in receive mode
comm_protocols.rs485.receive();

Serial.println("Initialization done!");
}

void loop()
{
if (comm_protocols.rs485.available())
Serial.write(comm_protocols.rs485.read());

if (millis() > sendNow) {
// Disable receive mode before transmission
comm_protocols.rs485.noReceive();

comm_protocols.rs485.beginTransmission();

comm_protocols.rs485.print("hello ");
comm_protocols.rs485.println(counter++);

comm_protocols.rs485.endTransmission();

// Re-enable receive mode after transmission
comm_protocols.rs485.receive();

sendNow = millis() + sendInterval;
}
}
1 change: 1 addition & 0 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ category=Communication
url=https://github.com/arduino-libraries/Arduino_MachineControl
architectures=mbed, mbed_portenta
includes=Arduino_MachineControl.h
depends=ArduinoRS485
38 changes: 30 additions & 8 deletions src/Arduino_MachineControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

#include "utility/MAX31865/MAX31865.h"
#include "utility/THERMOCOUPLE/MAX31855.h"
#include "utility/RS485/RS485.h"
#include <ArduinoRS485.h>
#include "utility/QEI/QEI.h"
#include "utility/ioexpander/ArduinoIOExpander.h"
#include "utility/RTC/PCF8563T.h"
#include "utility/RTC/PCF8563T.h"

#include "Arduino.h"
#include "pinDefinitions.h"
#include "mbed.h"

#include "USBHost.h"
Expand Down Expand Up @@ -57,11 +58,19 @@ class COMMClass {
// to be tested: check if can be made a big pin initialization
void init() {
//SHUTDOWN OF RS485 LEDS
digitalWrite(PA_0, LOW);
digitalWrite(PI_9, LOW);
digitalWrite(PinNameToIndex(PA_0), LOW);
digitalWrite(PinNameToIndex(PI_9), LOW);
//SHUTDOWN OF CAN LEDS
digitalWrite(PB_8, LOW);
digitalWrite(PH_13, LOW);
digitalWrite(PinNameToIndex(PB_8), LOW);
digitalWrite(PinNameToIndex(PH_13), LOW);

// SET DEFAULTS for RS485
rs485Enable(false);
rs485ModeRS232(false);
rs485FullDuplex(false);
rs485YZTerm(false);
rs485ABTerm(false);
rs485Slew(false);
}

void enableCAN() {
Expand All @@ -74,11 +83,24 @@ class COMMClass {
UART _UART4_ = arduino::UART(PA_0, PI_9, NC, NC);
mbed::CAN& can = _can;

RS485Class rs485 = RS485Class(_UART4_,PA_0, PI_13,PI_10);
RS485Class rs485 = RS485Class(_UART4_, PinNameToIndex(PA_0), PinNameToIndex(PI_13), PinNameToIndex(PI_10));

void rs485Enable(bool enable) { digitalWrite(PinNameToIndex(PG_9), enable ? HIGH : LOW); }
void rs485ModeRS232(bool enable) { digitalWrite(PinNameToIndex(PA_10), enable ? LOW : HIGH); }
void rs485YZTerm(bool enable) { digitalWrite(PinNameToIndex(PI_15), enable ? HIGH : LOW); }
void rs485ABTerm(bool enable) { digitalWrite(PinNameToIndex(PI_14), enable ? HIGH : LOW); }
void rs485Slew(bool enable) { digitalWrite(PinNameToIndex(PG_14), enable ? LOW : HIGH); }
void rs485FullDuplex(bool enable) {
digitalWrite(PinNameToIndex(PA_9), enable ? LOW : HIGH);
if (enable) {
// RS485 Full Duplex require YZ and AB 120 Ohm termination enabled
rs485YZTerm(true);
rs485ABTerm(true);
}
}

private:
mbed::DigitalOut can_disable = mbed::DigitalOut(PA_13, 0);


};

extern COMMClass comm_protocols;
Expand Down
Loading

0 comments on commit 305e09e

Please sign in to comment.