[DONE] What's the fair way to stop usb driver?

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
hardlulz
Posts: 2
Joined: Sun Apr 21, 2013 4:04 pm

[DONE] What's the fair way to stop usb driver?

Post by hardlulz »

I have a custom self powered board with stm32F103rd mcu. On that board I have found that leaving usb cable attached to the board but disconnected from PC generates a lot of noise for the USB peripherial, continuously making a huge number of wakeup/reset/suspend events. These interrupts keep waking my board from sleep mode.

So to get rid of these interrupts I've tried to monitor the presence of power on UsbVcc of my board and stop the driver on usb power loss by a call to

Code: Select all

sduStop(&SDU1));

but hit the following assertion:

Code: Select all

usb.c:292
chDbgAssert((usbp->state == USB_STOP) || (usbp->state == USB_READY),
            "usbStop(), #1", "invalid state");

The value of usbp->state at that moment was USB_ACTIVE.
Before calling sduStop() I waited for suspend event from driver to get sure there's no activity present on the bus.

I'm using the ChibiOS version 2.4.3.

Is this a bug in usb driver? If not then how am I supposed to stop the driver?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: What's the fair way to stop usb driver?

Post by Giovanni »

Hi,

You can stop the driver only when there are no transactions in progress, so you must not be transmitting nor receiving. The status variable at USB_ACTIVE does not mean that there is necessarily traffic on the bus but that a transaction has been started.

The problem lies in the SERIAL_USB driver, after stopping it there is nothing preventing it to start new transactions. Its stop function should be modified to reset the queues and the callbacks/sduDataTransmitted/sduDataReceived modified to not start new transactions if the state is SDU_STOP.

After doing this you could poll the USB driver state to become USB_READY and then stop it.

Giovanni
hardlulz
Posts: 2
Joined: Sun Apr 21, 2013 4:04 pm

Re: [TODO] What's the fair way to stop usb driver?

Post by hardlulz »

Thank you for your fast response.

I've tried the method you were suggesting but failed. After that I looked into usb.c and found no state machine transition from USB_ACTIVE (or USB_CONFIGURED) to USB_READY state other than reset event.

I tried to directly call _usb_reset() before usbStop().
Fixed (possible) misprint in STM32/USBv1/usb_lld.c:321 (changed USB_STOP to USB_READY).
After these changes sduStop() succeeded and stopped annoying interrupts, but the next time I started the driver I got HardFault.
modified_files.zip
modified files
(8.87 KiB) Downloaded 437 times


Unfortunately I was unable to fix it this time. So for now I ended up with just disabling usb interrupts whenever i disable pull-up resistor.

Dmitriy
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: [TODO] What's the fair way to stop usb driver?

Post by Giovanni »

Hi,

So apparently there is a problem in the state checks too, I will look into this as well, I added a TODO to this thread.

Giovanni
Laurent
Posts: 35
Joined: Mon Mar 25, 2013 9:01 am

Re: [TODO] What's the fair way to stop usb driver?

Post by Laurent »

Hi

I have the same problem, and was about to posts when I saw this thread.

Do you know if you'll be able to fix it quickly? Being able to stop USB is really important for my firmware.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: [TODO] What's the fair way to stop usb driver?

Post by Giovanni »

Hi,

This is a weekend-wide task, if it goes well it will be fixed by next week.

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: [TODO] What's the fair way to stop usb driver?

Post by Giovanni »

Hi,

The USB driver can now be stopped from any state not just USB_READY. The change is in the repository both in the trunk and in the 2.4.x branches.

Giovanni
Laurent
Posts: 35
Joined: Mon Mar 25, 2013 9:01 am

Re: [DONE] What's the fair way to stop usb driver?

Post by Laurent »

Awesome, thank you :)
Post Reply