Serial Won't Start After Serial.end()

petersanch
Posts: 15
Joined: Mon Sep 10, 2018 8:58 am

Serial Won't Start After Serial.end()

Postby petersanch » Sat Mar 23, 2019 11:00 pm

I wrote program to begin serial 1, send a byte then disable serial1 and loop. If Serial1.end() is called then the TX pin will not function properly. The first time loop() runs it send the byte then stays low for about 600 microseconds and stays high after that. The TX channel stops toggling after that.

The reason I need to call Serial1.end() is because my TX pin is multiplexed and I need to use it as a general IO later.
I tried using Serial1.flush() before and after end(), but it doesn't help.

Code: Select all

#include "HardwareSerial.h"
uint8_t a= 10;

void setup() {
}

void loop() {
  Serial1.begin(250000);
  Serial1.write(a);
  Serial1.end();
  //MAKE PIN 10 GENERAL IO AND DO SOMETHING WITH digitalWrite(10,LOW) or  digitalWrite(10,HIGH) HERE

  delay(100);
}
Using the ESP32 Pico with 1.0.2-rc1 version in Arduino.

Cheers
Last edited by petersanch on Sun Mar 24, 2019 12:56 am, edited 1 time in total.

petersanch
Posts: 15
Joined: Mon Sep 10, 2018 8:58 am

Re: Serial Won't Start After Serial.end()

Postby petersanch » Sun Mar 24, 2019 12:32 am

I tried changing the code to use HardwareSerial specifically but the results are the same.

Code: Select all

#include <HardwareSerial.h>
uint8_t a= 10;
HardwareSerial MySerial(1);

void setup() {
}

void loop() {
  MySerial.begin(250000, SERIAL_8N1, 9, 10);
  MySerial.write(a);
  MySerial.end();
  //MAKE PIN 10 GENERAL IO AND DO SOMETHING WITH digitalWrite(10,LOW) or  digitalWrite(10,HIGH) HERE

  delay(100);
}

petersanch
Posts: 15
Joined: Mon Sep 10, 2018 8:58 am

Re: Serial Won't Start After Serial.end()

Postby petersanch » Sun Mar 24, 2019 9:56 pm

Adding a flush and delay makes this program work as expected now. A byte is written on the TX pin every 100 ms.

Code: Select all

#include <HardwareSerial.h>
uint8_t a= 10;
HardwareSerial MySerial(1);

void setup() {
  pinMode(10, OUTPUT);
}

void loop() {  
  MySerial.begin(250000, SERIAL_8N1, 9, 10);
  MySerial.write(a);
  MySerial.flush();
  delay(1);
  MySerial.end();

  delay(100);
}
The problem now is that I want to have TX pin be a digital output after MySerial.end(). If I try to do that then the output stays HIGH until the loop() starts again and the serial commands are run. There is no difference in the output of the TX pin in the bottom code vs the top code.

Code: Select all

#include <HardwareSerial.h>
uint8_t a= 10;
HardwareSerial MySerial(1);

void setup() {
  pinMode(10, OUTPUT);
}

void loop() {  
  MySerial.begin(250000, SERIAL_8N1, 9, 10);
  MySerial.write(a);
  MySerial.flush();
  delay(1);
  MySerial.end();

  //MAKE PIN 10 GENERAL IO AND DO SOMETHING WITH digitalWrite(10,LOW) or  digitalWrite(10,HIGH) HERE
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
  delay(1);
  digitalWrite(10, HIGH);
  delay(1);
  digitalWrite(10, LOW);
  delay(1);
  digitalWrite(10, HIGH);
  delay(1);
  
  delay(100);
}

end() calls uartDetachRx(uart) and uartDetachTx(uart). Is it possible the detach is not working?

Who is online

Users browsing this forum: No registered users and 82 guests