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

UART0 pins enabled for ESPSoftwareSerial #134

Closed
wants to merge 1 commit into from

Conversation

executer-uno
Copy link

That changes tested and works fine.
Was forced to do it because of issue #133
So, now Im able to use 3x HW UART for communication with sensors and for minor diagnostics to PC maintained by SW Uart. It uses only TX, so, Im safe from #133 (it crashes in my config only on RX events).

Sample code I sed for debug:

#define  NO_GLOBAL_SERIAL
// in sloeber need to add attribute in
// project properties/arduino/Compiler options/Add for C and C++
// -DNO_GLOBAL_SERIAL
// to use Serial variable name for SWUART

#include <SoftwareSerial.h>


	#define PM_SERIAL_RX    35	// SDS
	#define PM_SERIAL_TX    32	// SDS

	#define PM2_SERIAL_RX   25  // PMS
	#define PM2_SERIAL_TX   26	// PMS

	#define GPS_SERIAL_RX   16  // UART2
	#define GPS_SERIAL_TX   17

	#define DEB_RX    3
	#define DEB_TX    1

// Reminder: the buffer size optimizations here, in particular the isrBufSize that only accommodates
// a single 8N1 word, are on the basis that any char written to the loopback SoftwareSerial adapter gets read
// before another write is performed. Block writes with a size greater than 1 would usually fail. 

SoftwareSerial Serial;
HardwareSerial serialSDS(0);
HardwareSerial serialPMS(1);
HardwareSerial serialGPS(2);

void setup() {

	serialSDS.begin(9600, SERIAL_8N1, PM_SERIAL_RX,  PM_SERIAL_TX);			 		// for HW UART SDS
	serialPMS.begin(9600, SERIAL_8N1, PM2_SERIAL_RX, PM2_SERIAL_TX);			 	// for HW UART PMS
	serialGPS.begin(9600, SERIAL_8N1, GPS_SERIAL_RX, GPS_SERIAL_TX);			 	// for HW UART GPS

	gpio_pad_select_gpio(DEB_RX);
	gpio_pad_select_gpio(DEB_TX);
	//pinMode(DEB_RX, INPUT);
	//pinMode(DEB_TX, OUTPUT);

	Serial.begin(9600, SWSERIAL_8N1, DEB_RX, DEB_TX, false, 295, 11);

	Serial.println("\nSoftware serial test started");

	for (char ch = ' '; ch <= 'z'; ch++) {
		Serial.write(ch);
	}
	Serial.println("");
}

void loop() {
	//if (SerialSW.available() > 0) {
	//	SerialSW.write(SerialSW.rea);
	//	yield();
	//}
	   Serial.println("Hello");
	serialSDS.println("serialSDS 0");
	serialPMS.println("serialPMS 1");
	serialGPS.println("serialGPS 2");

	delay(100);
	yield();
}

That changes tested and works fine.
Was forced to do it because of issue plerup#133
So, now Im able to use 3x HW UART for communication with sensors and for minor diagnostics to PC maintained by SW Uart. It uses only TX, so, Im safe from issue 133 (it crashes in my config only on RX events).

Sample code I sed for debug:
'''

#define  NO_GLOBAL_SERIAL
// in sloeber need to add attribute in
// project properties/arduino/Compiler options/Add for C and C++
// -DNO_GLOBAL_SERIAL

#include <SoftwareSerial.h>


	#define PM_SERIAL_RX    35	// SDS
	#define PM_SERIAL_TX    32	// SDS

	#define PM2_SERIAL_RX   25  // PMS
	#define PM2_SERIAL_TX   26	// PMS

	#define GPS_SERIAL_RX   16  // UART2
	#define GPS_SERIAL_TX   17

	#define DEB_RX    3
	#define DEB_TX    1

// Reminder: the buffer size optimizations here, in particular the isrBufSize that only accommodates
// a single 8N1 word, are on the basis that any char written to the loopback SoftwareSerial adapter gets read
// before another write is performed. Block writes with a size greater than 1 would usually fail. 

SoftwareSerial Serial;
HardwareSerial serialSDS(0);
HardwareSerial serialPMS(1);
HardwareSerial serialGPS(2);

void setup() {

	serialSDS.begin(9600, SERIAL_8N1, PM_SERIAL_RX,  PM_SERIAL_TX);			 		// for HW UART SDS
	serialPMS.begin(9600, SERIAL_8N1, PM2_SERIAL_RX, PM2_SERIAL_TX);			 	// for HW UART PMS
	serialGPS.begin(9600, SERIAL_8N1, GPS_SERIAL_RX, GPS_SERIAL_TX);			 	// for HW UART GPS

	gpio_pad_select_gpio(DEB_RX);
	gpio_pad_select_gpio(DEB_TX);
	//pinMode(DEB_RX, INPUT);
	//pinMode(DEB_TX, OUTPUT);

	Serial.begin(9600, SWSERIAL_8N1, DEB_RX, DEB_TX, false, 295, 11);

	Serial.println("\nSoftware serial test started");

	for (char ch = ' '; ch <= 'z'; ch++) {
		Serial.write(ch);
	}
	Serial.println("");
}

void loop() {
	//if (SerialSW.available() > 0) {
	//	SerialSW.write(SerialSW.rea);
	//	yield();
	//}
	   Serial.println("Hello");
	serialSDS.println("serialSDS 0");
	serialPMS.println("serialPMS 1");
	serialGPS.println("serialGPS 2");

	delay(100);
	yield();
}
'''
@dok-net
Copy link
Collaborator

dok-net commented Jan 22, 2020

@executer-uno
You are telling us that you have succeeded in redirecting the ports for Serial ("UART0") on the ESP32 away from the RX/TX that are connected to the USB UART. That would be quite useful, if only I could replicate your effort. I'm running this sketch, re-compiling the core with -DNO_GLOBAL_SERIAL:

#define  NO_GLOBAL_SERIAL

#include <SoftwareSerial.h>

HardwareSerial Serial0(0);
SoftwareSerial swSerial;

void setup()
{
    Serial0.begin(115200, SERIAL_8N1, D7, D8); // WEMOS D1 MINI ESP32 variant
    while (!Serial0) {}
    delay(500);
    Serial0.println("This is HW Serial on D7/D8");    
}

// Add the main program code into the continuous loop() function
void loop()
{
}

But the PC serial terminal window, connecting to the ESP32 via USB, indicates that no such redirection has succeeded, because it shows:

Opening port
Port open
This is HW Serial on D7/D8

@executer-uno
Copy link
Author

Yes, that behaviour takes few hours for me ;)
If you connects external USART adapter to D7, D8 and start your sketch you will see the same. Moreover you will be able to receieve data. The root is in very flexible ESP IO pins multiplexer, it is not a problem to it to sends internal peripherals output to several pins. So with
Serial0.begin(115200, SERIAL_8N1, D7, D8);
you just reconfigure HWSerial to D7, D8, but not disconnects pin №1 from TX output. so you got it on both.

Just grab pin 1 and 3 to SW serial by
swSerial.begin(9600, SWSERIAL_8N1, 3, 1, false, 295, 11);

@tarwi
Copy link

tarwi commented Jan 25, 2020

Let me talk about HW serial sensor 3 with uart 0 uart 1 and uart 2 on esp32 nodemcu .. I'm trying to improve on uart0. Can uart0 be replaced by another pin using the serial software and gpio 1 and 3 fixed for. debug and device communication

@tarwi
Copy link

tarwi commented Jan 25, 2020

I have found error, Please help me;

I:\PROJECT IOT\COBACOBA\COBACOBA.ino: In function 'void setup()':

COBACOBA:41: error: no matching function for call to 'HardwareSerial::begin(int, SoftwareSerialConfig, int, int, bool, int, int)'

Serial.begin(9600, SWSERIAL_8N1, DEB_RX, DEB_TX, false, 295, 11);

                                                              ^

In file included from C:\Users\TARWI\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32/Arduino.h:154:0,

             from sketch\COBACOBA.ino.cpp:1:

C:\Users\TARWI\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32/HardwareSerial.h:58:10: note: candidate: void HardwareSerial::begin(long unsigned int, uint32_t, int8_t, int8_t, bool, long unsigned int)

 void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);

      ^

C:\Users\TARWI\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32/HardwareSerial.h:58:10: note: candidate expects 6 arguments, 7 provided

exit status 1
conflicting declaration 'SoftwareSerial Serial'

@dok-net
Copy link
Collaborator

dok-net commented Jan 25, 2020

Thanks for the research and POC in this PR. I'm releasing EspSoftwareSerial >= 6.7.0 containing the necessary changes - superseding this PR - to attach to RX/TX on ESP32. Here's the minimal MVCE:

#include <SoftwareSerial.h>
SoftwareSerial swSerial;
void setup()
{
    Serial.begin(115200, SERIAL_8N1, D7, D8); // D7, D8);
    swSerial.begin(115200, SWSERIAL_8N1, RX, TX);
    while (!Serial) {}
    delay(500);
    Serial.println("This is HW Serial on D7/D8");
    swSerial.println("This is SW Serial on RX/TX");
}

void loop()
{
}

Please not how swSerial.begin detached HW Serial from RX/TX, otherwise the Serial.begin(...) redirection alone does not stop it from sending to the USB UART as well!

@dok-net dok-net closed this Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants