gracefully stop/restart USB ?

Status
Not open for further replies.

vladn

Well-known member
Is there a way to gracefully stop/restart K6* USB controller on the fly, such that host OS (Windows, Linux) would recognize and handle stop/restart events correctly.
 
Tried it but the USB (and in fact Serial) work just fine after Serial.end() :). It is the USB HID Serial, not the hardware UART.
I want to power down the USB peripheral. I know how to disable/enable the USB peripheral clock but this will likely mess everything without proper shutdown and restart sequences.
 
No, I simply want to stop clocks to all unused peripherals to save power in a battery RX project.
I will eventually use all or parts of the Snooze library to implement a full standby. However I also have an intermediate LP mode where the main Audio DSP path (I2S in/out, Hilbert FIR, mixers, AGC control) must be operational, but all "input only"/non-essential DSP routines (FFT calculation/display, various estimators, optical encoder power supply, etc) are disabled. In this mode the RX runs fine at the reduced 24MHz CPU speed. The UI wake-up is instant on any button action in the LP mode.
 
Okay - I read those functions just to stop/start USB clock - i.e. cut and paste the functional elements to your sketch.
 
I was thinking along these lines. My concern was how the host reacts to this if it is done on the fly (while connected to the host using HID driver). In the sense that I want to use USB Serial normally after exiting LP mode.
 
you could control a reed relay on one of the data lines (green or white) and use a gpio to activate/drop the signal when needed, crude but possible :p
 
Ok, after few mandatory host reboots I got it almost working. It is based on the register access sequence from the Snooze libary. I had to fiddle a bit with the delays around the USB disable-enable interval.
There is one minor inconvenience though - the Arduino IDE serial monitor fails to restart automatically, I have to close it and open again after re-enabling USB. I do Serial.begin() after a hundred 20ms loop passes, much longer than it takes for Windows to detect the serial port presence, but the monitor does not sense the reappearance of the port.
 
Good work. Have you installed the TD 1.42 beta? Paul is including a teensy_sermon alternative to the IDE JAVA SerMon and it may be more responsive.

He also linked a recent change to the DTR (?) Twiddle to get the USB to assert faster - not sure if that might show a way to bring it back to recognition.

TD 1.42 is beta 3 and beta 4 may appear any day, but teensy_sermon is better at seeing Teensy arrival than the IDE version so giving that a try might show it working better on USB reconnect/restart.
 
I am not in a big hurry on the monitor restart, can probably wait for 1.42 release, especially since I have to patch few files in the core libraries for my frequency hacks to work properly. Have other things to do first (the UI state machine is still a big mess).

By shutting down all fancy DSP stuff I managed to cut the AudioProcessorUseageMax in half in the LP mode, runs very comfortable @24MHz. Any button press takes it back to nominal 48MHz mode (and completes the required UI action). I guess I can also shut down the I2C controller in the LP mode, the RX AGC is done in the IF amp (controlled by the Teensy DAC) so I do not change sgtl5k settings in the LP mode. But I would guess the power saving will be marginal - it is a slow peripheral.

I am using 3.6 for development but got 3.5 already and plan to use it going forward. The low profile connectors for it should have come tomorrow, but we have a huge snowstorm coming, so the 3.5 experiments and current measurements will have to wait few more days. Hope we don't loose power/internet, that would be worse...
 
I've installed 1.42b3 into a separate directory and switched to teensy_sermon. It does indeed detect the "reappearing" serial port, thanks Paul !

It took me a while to get the delays dialed in. I run a time-corrected loop at 20ms period with no delay() function (using wait with WFI inside). I need about 40ms (2 x 20ms) to flush the remaining debug data before shutting down the USB. After turning the USB back on I need about 400ms (20 x 20ms) to get the windows recognize the port and then for the teensy_sermon to recognize the presence of the port. In addition I had to add a small 2..5 ms delay() into the Snooze USB shutdown sequence between entering the suspended state and disabling the USB, otherwise the host does not always recognize the USB suspend/shutdown.

It is fairly stable right now, but I would guess the delays may depend on specific hosts, I have no idea how to implement a proper "handshake" in this case.
 
Status
Not open for further replies.
Back
Top