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

Reset of the MCU doesn't cause USB device re-enumeration #38

Closed
kzyapkov opened this issue Jul 31, 2021 · 14 comments · Fixed by #41
Closed

Reset of the MCU doesn't cause USB device re-enumeration #38

kzyapkov opened this issue Jul 31, 2021 · 14 comments · Fixed by #41
Assignees
Labels
enhancement New feature or request

Comments

@kzyapkov
Copy link

Maybe I'm doing something wrong, but while toying with the project I noticed that if I hit the reset button on the pill the host doesn't notice anything has happened. I have to pull the USB cable out to see the "USB disconnect" event in the kernel log. I don't have an STLink or other power source connected, bluepill is powered over the USB connector.

This is on linux 5.13.6. Everything else works fine otherwise.

73 de LZ1CK

@r2axz
Copy link
Owner

r2axz commented Aug 1, 2021

Hi,

I haven't implemented forcing USB re-enumeration in this firmware. I thought this functionality was not critical for everyday device operation. However, it does seem a convenient feature for development purposes. Also, people seem to expect it to behave this way, so it should be implemented, I guess.

@r2axz r2axz added the enhancement New feature or request label Aug 1, 2021
@r2axz r2axz self-assigned this Aug 1, 2021
@r2axz r2axz linked a pull request Aug 1, 2021 that will close this issue
@r2axz
Copy link
Owner

r2axz commented Aug 1, 2021

@kzyapkov Please test the linked PR.

@kzyapkov
Copy link
Author

kzyapkov commented Aug 1, 2021

I can confirm the proposed patch works as expected -- as soon as the micro is reset the ports are re-enumerated, any active screen sessions get terminated.

A possibly unrelated glitch, also present without the re-enumeration patch: after the reset, in a fresh screen session it seems some garbage gets sent to UART1, which I keep as a control console by permanently grounding PB5. An example:

 od
Error, unknown command, use "help" to get the list of available commands.
>
>verde cm
Error, unknown command, use "help" to get the list of available commands.
>
>Er

@kzyapkov
Copy link
Author

kzyapkov commented Aug 1, 2021

Also, strangely, flashing the micro via SWD doesn't cause re-enumeration. The newly flashed program seems to run, but the host doesn't get the memo.

@r2axz
Copy link
Owner

r2axz commented Aug 2, 2021

A possibly unrelated glitch, also present without the re-enumeration patch ...

Looks like I should open a separate issue and investigate this.

Also, strangely, flashing the micro via SWD doesn't cause re-enumeration

That is odd. I actually tested this and it re-enumerates just fine after flashing at least with my PC running Win10. I will double check this with my MacBook and Linux (virtualized, VirtualBox). I am using a cheat st-link v2 clone. I just connect SWCLK, SWDIO, 3.3v, and GND. What's your setup?

@kzyapkov
Copy link
Author

kzyapkov commented Aug 2, 2021

What's your setup?

Exactly the same: STLink clone with the 4 pins connected to one side of the bluepill; a USB-micro cable on the other side.. Both the STLink clone and the USB interface of bluepill are connected to the same powered USB 2.0 hub. I don't have Windows, will try to test on wife's Mac.

My test sequence: open a screen session on /dev/ttyACM0, run make flash which succeeds; observe the screen becomes non-operational, but doesn't detach. Kernel log doesn't notify about USB disconnect events. Hitting the reset button causes the disconnect and screen dies as a result.

@r2axz
Copy link
Owner

r2axz commented Aug 2, 2021

I re-tested on Linux, Mac, and PC again and everything works as expected. The device re-enumerates after flashing the MCU. If you programming (st-link) setup is equivalent to mine, probably the boards a little bit different. As far as I know, there are a few variations of stm32f103c8t6 bluepill boards out there. Particularly, I am interested in how PA12 (USB D+) is pulled up on your board. Mine goes to the 3.3 V line via a 1.5k resistor. This is exactly as it should be. Can you test yours? Also, if you have schematics for your version of the board, it will help a lot.

Also, what version of st-flash do you use? Mine is:

>> st-flash --version
v1.6.1-1-gbaab8ca

Also, this could be related to your host USB hardware. The thing is, as far as I know, the re-enumeration method I use does not strictly confirm to the USB specs. But this is the best I can do anyways. That's why it would be great if you can test it on multiple different hosts so that we can figure our if this is a problem with a particular host or your board behalves the same with multiple different hosts.

One more thing. Сan you play with the timing? Please try 0xFFFF or 0xFFF (usb_io.c:60) to see if it changes anything.

@kzyapkov
Copy link
Author

kzyapkov commented Aug 4, 2021

PA12 is pulled to 3.3V with a 1.5kOhm resistor which I replaced myself years ago when I got the boards. I tested on 2 different bluepills (most likely from the same batch, but can't remember exactly) -- both did not re-enumerate when accessed with st-flash / st-util. I then tested connecting to the target with openocd -- when I execute reset via openocd the board does re-enumerate, just like when I hit the reset button.

I then attached one of those ultra-cheap logic analyzers to PA12 -- no reset hold while flashing -- the MCU just doesn't go through reset! Finally, my st-flash --version: v1.7.0-88-g9605d21. It turns out they've added the --connect-under-reset option, and when I add it to the flash command the board does re-enumerate.

Normality restored.

@r2axz
Copy link
Owner

r2axz commented Aug 9, 2021

Does this mean the issue you experienced is st-flash related and does not require any fixes in the PR? Or am I missing something?

@kzyapkov
Copy link
Author

kzyapkov commented Aug 9, 2021

Does this mean the issue you experienced is st-flash related and does not require any fixes in the PR? Or am I missing something?

The issue seems to be st-flash related, yes. With the newer version, after st-flash is done the target MCU doesn't go through reset unless --connect-under-reset is used.

@r2axz
Copy link
Owner

r2axz commented Aug 20, 2021

Sorry for the delay. I’ll get back in Monday and do more tests with the newest version of st-flash. If it doesn’t do any reset at all, I think I’m just going to merge this PR because it otherwise works.

@r2axz
Copy link
Owner

r2axz commented Aug 23, 2021

@kzyapkov I upgraded my st-flash to v1.7.0 and got the same experience that you did. Then I figured out that you don't really have to use the "connect-under-reset" option. Simple "reset" is fine as well. So I think, I just need to add the "--reset" flag to the Makefile and finally merge this PR.

@r2axz
Copy link
Owner

r2axz commented Aug 23, 2021

@kzyapkov pushed the update. Please re-test when you have time and let me know if you are OK with merging this PR.

@kzyapkov
Copy link
Author

Using the --reset option instead of --connect-under-reset yields the desired effect, OK to merge. Thanks!

@r2axz r2axz closed this as completed in #41 Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants