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

USBSerial would not block on write if the USB inteface is not connected #444

Merged
merged 5 commits into from Mar 14, 2019

Conversation

ghent360
Copy link
Contributor

@ghent360 ghent360 commented Feb 18, 2019

No description provided.

@fpistm
Copy link
Member

fpistm commented Feb 18, 2019

Hi @ghent360
Thanks for the PR.
I guess it solve issue with Marlin FW ?

@fpistm fpistm self-requested a review February 18, 2019 07:28
@fpistm fpistm added this to In progress in STM32 core based on ST HAL via automation Feb 18, 2019
@fpistm fpistm added this to the 1.5.1/1.6.0 milestone Feb 18, 2019
@ghent360
Copy link
Contributor Author

ghent360 commented Feb 18, 2019 via email

@ktand
Copy link
Contributor

ktand commented Feb 18, 2019 via email

@ghent360
Copy link
Contributor Author

ghent360 commented Feb 18, 2019 via email

@ghent360
Copy link
Contributor Author

ghent360 commented Feb 18, 2019 via email

@ghent360
Copy link
Contributor Author

ghent360 commented Feb 18, 2019 via email

@uzi18
Copy link

uzi18 commented Feb 18, 2019

Is there a way to check if usb/cdc is connected?

@ghent360
Copy link
Contributor Author

ghent360 commented Feb 18, 2019

Yeah, this is a complex issue. I added checking the DTR line value, which gets set when the terminal is open and reset when the terminal is closed.

That makes it work almost all the time. Now the edge case is when the terminal is open, but the program on the PC does not actually read the serial data, then it locks again.

In my Ubuntu I use minicom for serial port communication, when I try to exit, there is a confirmation dialog "Are you sure you want to exit", if I stay there long enough the STM32 lock again. If I exit quickly there is no issue, the DTR line is reset and the code stops sending data, but if I linger on the exit dialog, apparently minicom stops reading data from the virtual terminal, but the terminal port is not closed.

What a bummer :-(

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

I would propose also to add a new USBSerial method:
Forgot my comment about isConnected().

cores/arduino/stm32/usb/cdc/usbd_cdc_if.c Outdated Show resolved Hide resolved
cores/arduino/stm32/usb/cdc/usbd_cdc_if.c Outdated Show resolved Hide resolved
cores/arduino/stm32/usb/cdc/usbd_cdc_if.c Outdated Show resolved Hide resolved
cores/arduino/stm32/usb/cdc/usbd_cdc_if.h Outdated Show resolved Hide resolved
cores/arduino/stm32/usb/cdc/usbd_cdc_if.c Outdated Show resolved Hide resolved
STM32 core based on ST HAL automation moved this from In progress to Needs review Feb 18, 2019
@ghent360
Copy link
Contributor Author

This last variant works even if I linger in the minicom menus.
I added a simple timeout if our transmit packet drags for too long I would assume the host is not active.

@ghent360
Copy link
Contributor Author

ghent360 commented Feb 18, 2019

If people can test this change with different boards and host environments. Look for this #444 (comment) for an example test sketch.
You would need a board with physical serial port as well as USB connector.

fpistm
fpistm previously requested changes Feb 18, 2019
Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

Could you also made some commit clean up by squashing some of them.
At this time, this PR should only have 4 commits:

Do not block writes...
Return the correct bytes written
Check DTR value
Check if the last packet transfer ...

You can refers to contributing.md about PR wow and commit message tips.

Thanks in advance

cores/arduino/USBSerial.cpp Outdated Show resolved Hide resolved
@ghent360
Copy link
Contributor Author

I'm going on vacation tomorrow. I'll continue working on this PR when I come back at the end of the week.
Thank you all for the feedback.

@ktand
Copy link
Contributor

ktand commented Feb 21, 2019

@ghent360

I've tried the changes in this PR and this resolves the issue I had with Marlin where the firmware would hang until the serial port is opened on the host side.

On a side note, do you think there is any way that we could have a "serial disconnected" event that we could use within marlin to reset the board if the CDC port is closed/disconnected?

Thanks!

@ghent360
Copy link
Contributor Author

ghent360 commented Feb 22, 2019 via email

@roc2
Copy link

roc2 commented Mar 1, 2019

why the serial assistant on PC can't receive data with follow sketch:

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

void setup()
{
    SerialUSB.begin();
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
//    if (SerialUSB.available() > 0)
    {
       SerialUSB.println("testing read and wirte");
    }
    digitalToggle(LED_BUILTIN);
    delay(300);
}

Only when I add this line "if (SerialUSB.available() > 0)", then send some data with serial assistant on PC, the PC can receive data continusly.

@fpistm
Copy link
Member

fpistm commented Mar 1, 2019

@roc2 this is not clear. Which board? Core version (1.5.0, master,...)? With this PR applied or not?....

@roc2
Copy link

roc2 commented Mar 1, 2019

@fpistm My board is Black F407VE, core version 1.5.0.

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

Some questions/remarks else code LGTM.
Currently, some tests on going.

cores/arduino/USBSerial.cpp Outdated Show resolved Hide resolved
cores/arduino/USBSerial.cpp Outdated Show resolved Hide resolved
cores/arduino/stm32/usb/cdc/usbd_cdc_if.c Show resolved Hide resolved
@fpistm fpistm dismissed their stale review March 7, 2019 13:17

Old review already adress

@fpistm
Copy link
Member

fpistm commented Mar 7, 2019

@ghent360 I've made several test on several boards with different STM32 families and host OS.
It's ok.
Still my comments to discuss.

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

This does not build.

cores/arduino/stm32/usb/cdc/usbd_cdc_if.c Outdated Show resolved Hide resolved
Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

LGTM. Will do some other tests

STM32 core based on ST HAL automation moved this from Needs review to Reviewer approved Mar 9, 2019
@fpistm
Copy link
Member

fpistm commented Mar 9, 2019

Thanks @ghent360

@fpistm
Copy link
Member

fpistm commented Mar 11, 2019

Using your sketch #444 (comment)
Works fine under Linux.
Under Windows, Unable to recognized the device after unplug/plug. Enum failed.
Anyway this is better than before :)

cores/arduino/USBSerial.cpp Outdated Show resolved Hide resolved
@fpistm
Copy link
Member

fpistm commented Mar 12, 2019

Under Windows, Unable to recognized the device after unplug/plug. Enum failed.

Update:
Doing further investigations, if I didn't open a terminal on the COM port. Enumeration is correct each time.

@fpistm fpistm mentioned this pull request Mar 12, 2019
2 tasks
@ghent360
Copy link
Contributor Author

ghent360 commented Mar 12, 2019 via email

@fpistm
Copy link
Member

fpistm commented Mar 12, 2019

Does my change affect the enumeration?

No @ghent360 , this is better with this PR.
Seems more like a driver issue but only after the COM has been used by Windows host (with a term).

Trying Teraterm, if I well disconnect, then it's ok. This seems very linked to the driver and how Windows handle the COM...

@fpistm fpistm merged commit 4e861b4 into stm32duino:master Mar 14, 2019
STM32 core based on ST HAL automation moved this from Reviewer approved to Done Mar 14, 2019
benwaffle pushed a commit to benwaffle/Arduino_Core_STM32 that referenced this pull request Apr 10, 2019
USBSerial would not block on write if the USB inteface is not connected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants