Skip to content
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

ESP8266 ESP32 compatible #16

Open
wants to merge 8 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
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/
image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/spell-check.yml/badge.svg["Spell Check status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/spell-check.yml"]

Enables sending and receiving data using the RS-485 standard with RS-485 shields, like the MKR 485 Shield.
I've Made this compatible with ESP8266 and ESP32.
Copy link

@brainstorm brainstorm Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd instead write: "also compatible with espressif dev boards"


This library supports the Maxim Integrated MAX3157 and equivalent chipsets.

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ArduinoRS485
version=1.0.2
version=1.0.61
author=Arduino
maintainer=Arduino <[email protected]>
sentence=Enables sending and receiving data using the RS-485 standard with RS-485 shields, like the MKR 485 Shield.
Expand Down
9 changes: 7 additions & 2 deletions src/RS485.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,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, RS485_SER_CONF_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, RS485_SER_CONF_TYPE config, int predelay, int postdelay)
{
_baudrate = baudrate;
_config = config;
Expand Down Expand Up @@ -137,6 +137,11 @@ void RS485Class::endTransmission()
}

_transmisionBegun = false;
#ifdef ESP32
// there is a bug in ESP32 for Serial2
while(_serial->available())
_serial->read();
#endif
}

void RS485Class::receive()
Expand Down
24 changes: 4 additions & 20 deletions src/RS485.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,17 @@
#define _RS485_H_INCLUDED

#include <Arduino.h>
#include "RS485_defs.h"

#ifdef PIN_SERIAL1_TX
#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX
#else
#define RS485_DEFAULT_TX_PIN 1
#endif

#ifdef __AVR__
#define RS485_DEFAULT_DE_PIN 2
#define RS485_DEFAULT_RE_PIN -1
#else
#define RS485_DEFAULT_DE_PIN A6
#define RS485_DEFAULT_RE_PIN A5
#endif


#define RS485_DEFAULT_PRE_DELAY 50
#define RS485_DEFAULT_POST_DELAY 50

class RS485Class : public Stream {
public:
RS485Class(HardwareSerial& 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, RS485_SER_CONF_TYPE 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, RS485_SER_CONF_TYPE config, int predelay, int postdelay);
virtual void end();
virtual int available();
virtual int peek();
Expand Down Expand Up @@ -79,7 +63,7 @@ class RS485Class : public Stream {

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

extern RS485Class RS485;
Expand Down
38 changes: 38 additions & 0 deletions src/RS485_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef _ARDUINO_RS485_DEFS_H_INCLUDED
#define _ARDUINO_RS485_DEFS_H_INCLUDED

#ifndef RS485_DEFAULT_TX_PIN
#ifdef PIN_SERIAL1_TX
#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX
#else
#define RS485_DEFAULT_TX_PIN 1
#endif
#endif

#ifdef __AVR__
#define RS485_DEFAULT_DE_PIN 2
#define RS485_DEFAULT_RE_PIN -1
#elif defined(ESP32) || defined(ESP8266)
#ifndef RS485_DEFAULT_DE_PIN
#define RS485_DEFAULT_DE_PIN 0
#endif
#ifndef RS485_DEFAULT_RE_PIN
#define RS485_DEFAULT_RE_PIN 0
#endif
#else
#define RS485_DEFAULT_DE_PIN A6
#define RS485_DEFAULT_RE_PIN A5
#endif

#if defined(ESP8266)
#define RS485_SER_CONF_TYPE SerialConfig
#elif defined(ESP32)
#define RS485_SER_CONF_TYPE uint32_t
#else
#define RS485_SER_CONF_TYPE uint16_t
#endif

#define RS485_DEFAULT_PRE_DELAY 50
#define RS485_DEFAULT_POST_DELAY 50

#endif