Skip to content

serial.end() doesn't work #3878

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

Closed
aster94 opened this issue Apr 6, 2020 · 9 comments
Closed

serial.end() doesn't work #3878

aster94 opened this issue Apr 6, 2020 · 9 comments
Assignees
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@aster94
Copy link

aster94 commented Apr 6, 2020

Description:

Serial.end() should free the serial pins and leave the user the ability to do digitalWrite() on them, but it doesn't work. Instead to drive the TX pin to a LOW state i need to reset the board.

I tested the code below with the following cores:

I also made three logic analyzer captures: la.zip
An example from one of these, as you can see it shows that the pin is driven low only once (after upload of the code or reset)

image

Sketch:

#include <Arduino.h>

HardwareSerial ser(2);
#define TX_PIN 17

void setup(){}

void loop()
{
    pinMode(TX_PIN, OUTPUT);
    delay(10);

    digitalWrite(TX_PIN, HIGH);
    delay(50);
    digitalWrite(TX_PIN, LOW);
    delay(300);
    digitalWrite(TX_PIN, HIGH);
    delay(50);

    ser.begin(9600);
    delay(10);

    for (uint8_t n = 1; n < 5; n++)
    {
        ser.write(n);
        delay(10);
    }
    ser.end();
    delay(300);
}

NOTE: looks like that the problem was here before #3713 and #3664

@aster94
Copy link
Author

aster94 commented Apr 7, 2020

@lbernstone do you have any idea about what can cause this behaviour?

@lbernstone
Copy link
Contributor

Turn on verbose debugging so you can see if any errors are shown.
Serial2 is instantiated by default. Why are you not using it?
Make your delays longer- a full second whenever you attach or detach pins.
Try manually detaching after the ser.end:
gpio_matrix_out(TX_PIN, 0x100, false, false);

@lyusupov
Copy link
Contributor

lyusupov commented Apr 9, 2020

One more ticket related to this issue:
#3563 (comment)

@aster94
Copy link
Author

aster94 commented Apr 11, 2020

@lbernstone looks like you solved it 😁

image

At first, I tried as you suggested to put big delays, then slowly I removed all of them and no problem showed up in about 5 minutes running this sketch

#include <Arduino.h>

#define TX_PIN 17

void setup(){}

void loop()
{
    pinMode(TX_PIN, OUTPUT);

    digitalWrite(TX_PIN, HIGH);
    delay(50);
    digitalWrite(TX_PIN, LOW);
    delay(1000);
    digitalWrite(TX_PIN, HIGH);

    Serial2.begin(9600);

    for (uint8_t n = 1; n < 5; n++)
    {
        Serial2.write(n);
        delay(50);
    }
    Serial2.end();
    gpio_matrix_out(TX_PIN, 0x100, false, false);
}

Also, note that after the begin no bytes are lost
image

I think it is an easy fix if the maintainers need I can make a PR @me-no-dev just tell me if the gpio_matrix_out(TX_PIN, 0x100, false, false); needs a special position in these lines or everywhere would work:

void HardwareSerial::end()
{
if(uartGetDebug() == _uart_nr) {
uartSetDebug(0);
}
uartEnd(_uart);
_uart = 0;
}

And to make it error-proof I would add _txPin to the protected variables of the HardwareSerial class, and finally add here
_txPin = txPin;

Here the LA log:
log.zip

@lbernstone
Copy link
Contributor

The gpio_matrix disconnect is already buried in the end method. It just isn't working for some reason. I'll see if I can track down where it is dropping the ball.

@marcmathys
Copy link

Saw that you are interested in a flutter app to standardize access to BT wearables - did you get anywhere. I also find this interesting and would contribute.

@stale
Copy link

stale bot commented Jul 2, 2020

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Jul 2, 2020
@stale
Copy link

stale bot commented Jul 16, 2020

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Jul 16, 2020
me-no-dev pushed a commit that referenced this issue Sep 30, 2020
* Fixes UART detach.  Fixes #3878

* 0 is not a good holder value for pins!

* 0 is not a good holder value for pins!
@SuGlider SuGlider self-assigned this Jul 19, 2021
@SuGlider
Copy link
Collaborator

Issue solved with PR #5385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

5 participants