Skip to content

Add compatibility to arduino DUE #59

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions src/RS485.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#ifdef __MBED__
#include "pinDefinitions.h"
RS485Class::RS485Class(HardwareSerial& hwSerial, PinName txPin, PinName dePin, PinName rePin) :
RS485Class::RS485Class(SERIAL_HARDWARE_CLASS& hwSerial, PinName txPin, PinName dePin, PinName rePin) :
_serial(&hwSerial),
_txPin(PinNameToIndex(txPin)),
_dePin(PinNameToIndex(dePin)),
Expand All @@ -31,7 +31,7 @@ RS485Class::RS485Class(HardwareSerial& hwSerial, PinName txPin, PinName dePin, P
}
#endif

RS485Class::RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin) :
RS485Class::RS485Class(SERIAL_HARDWARE_CLASS& hwSerial, int txPin, int dePin, int rePin) :
_serial(&hwSerial),
_txPin(txPin),
_dePin(dePin),
Expand All @@ -50,12 +50,12 @@ void RS485Class::begin(unsigned long baudrate, int predelay, int postdelay)
begin(baudrate, SERIAL_8N1, predelay, postdelay);
}

void RS485Class::begin(unsigned long baudrate, uint16_t config)
void RS485Class::begin(unsigned long baudrate, SERIAL_MODE_TYPE config)
{
begin(baudrate, config, RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY);
}

void RS485Class::begin(unsigned long baudrate, uint16_t config, int predelay, int postdelay)
void RS485Class::begin(unsigned long baudrate, SERIAL_MODE_TYPE config, int predelay, int postdelay)
{
_baudrate = baudrate;
_config = config;
Expand Down Expand Up @@ -206,5 +206,5 @@ void RS485Class::setDelays(int predelay, int postdelay)
#ifdef RS485_SERIAL_PORT
RS485Class RS485(RS485_SERIAL_PORT, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
#else
RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
RS485Class RS485(SERIAL_PORT_HARDWARE_DEFAULT, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
#endif
36 changes: 28 additions & 8 deletions src/RS485.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@

#include <Arduino.h>

#ifdef ARDUINO_SAM_DUE
#define SERIAL_HARDWARE_CLASS USARTClass
#define SERIAL_MODE_TYPE UARTClass::UARTModes
#else
#define SERIAL_HARDWARE_CLASS HardwareSerial
#define SERIAL_MODE_TYPE uint16_t
#endif

#ifdef ARDUINO_ARCH_SAM
#define PIN_SERIAL1_TX 18
#endif

#ifndef RS485_DEFAULT_TX_PIN
#ifdef PIN_SERIAL1_TX
#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX
Expand All @@ -40,16 +52,24 @@
#define RS485_DEFAULT_DE_PIN A4
#define RS485_DEFAULT_RE_PIN A5
#elif defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_UNOR4_MINIMA)
#define SERIAL_PORT_HARDWARE Serial1
#define SERIAL_PORT_HARDWARE_DEFAULT Serial1
#define RS485_DEFAULT_DE_PIN 8
#define RS485_DEFAULT_RE_PIN 7
#elif defined(ARDUINO_ARCH_SAM)
#define SERIAL_PORT_HARDWARE_DEFAULT Serial1
#define RS485_DEFAULT_DE_PIN 23
#define RS485_DEFAULT_RE_PIN -1
#else
#ifndef RS485_DEFAULT_DE_PIN
#define RS485_DEFAULT_DE_PIN A6
#define RS485_DEFAULT_RE_PIN A5
#endif
#endif

#ifndef SERIAL_PORT_HARDWARE_DEFAULT
#define SERIAL_PORT_HARDWARE_DEFAULT SERIAL_PORT_HARDWARE
#endif

#ifdef CUSTOM_RS485_DEFAULT_DE_PIN
# define RS485_DEFAULT_DE_PIN CUSTOM_RS485_DEFAULT_DE_PIN
#endif
Expand All @@ -64,14 +84,14 @@
class RS485Class : public Stream {
public:
#ifdef __MBED__
RS485Class(HardwareSerial& hwSerial, PinName txPin, PinName dePin, PinName rePin);
RS485Class(SERIAL_HARDWARE_CLASS& hwSerial, PinName txPin, PinName dePin, PinName rePin);
#endif
RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin);
RS485Class(SERIAL_HARDWARE_CLASS& hwSerial, int txPin, int dePin, int rePin);

virtual void begin(unsigned long baudrate);
virtual void begin(unsigned long baudrate, uint16_t config);
virtual void begin(unsigned long baudrate, int predelay, int postdelay);
virtual void begin(unsigned long baudrate, uint16_t config, int predelay, int postdelay);
virtual void begin(unsigned long baudrate, SERIAL_MODE_TYPE config);
virtual void begin(unsigned long baudrate, SERIAL_MODE_TYPE config, int predelay, int postdelay);
virtual void end();
virtual int available();
virtual int peek();
Expand All @@ -94,7 +114,7 @@ class RS485Class : public Stream {
void setDelays(int predelay, int postdelay);

private:
HardwareSerial* _serial;
SERIAL_HARDWARE_CLASS* _serial;
int _txPin;
int _dePin;
int _rePin;
Expand All @@ -103,9 +123,9 @@ class RS485Class : public Stream {

bool _transmisionBegun;
unsigned long _baudrate;
uint16_t _config;
SERIAL_MODE_TYPE _config;
};

extern RS485Class RS485;

#endif
#endif