USB: understanding the RESUME and SUSPENDED states - usb

I do not understand the "RESUME" and "SUSPENDED" modes in the USB protocol (USB 2.0).
The USB 2.0 specification states:
All devices must suspend if bus activity has not been observed for the length of time specified in
Chapter 7. Attached devices must be prepared to suspend at any time they are powered, whether they have
been assigned a non-default address or are configured. Bus activity may cease due to the host entering a
suspend mode of its own.
The length of time specified in Chapter 7 is 3 frames. I don't understand what does mean "no bus activity": does it means no packets at all? does it means no packets sent to this device (and so if the device detects SOF packets then it should not enter SUSPENDED mode?)
In addition, a USB device shall also enter the Suspended state when the hub port
it is attached to is disabled. This is referred to as selective suspend.
How can a hub port be disabled? Is it the hub port itself that decides to do so (under which condition?) or is it the host that sends a command to the hub to do so (what is this command?)?
Is it correct to assume that for the device point of view, suspend and selective suspend are the same because in both cases the device only sees no bus activity?
A USB device exits suspend mode when there is bus activity. A USB device may also request the host to
exit suspend mode or selective suspend by using electrical signaling to indicate remote wakeup.
I do not understand this part. Why would a USB device requests the host to exit suspend mode or selective suspend because this is always the host that initiates transactions?
Thank you for your help.

Bus activity refers to any packet seen by the device.
I don't know all the details about selective suspend, but I believe that the operating system can tell when nothing is trying to use a USB device, and then tell the USB port to suspend the device to save power.
As for why a USB device would request for the host to exit suspend mode: have you ever noticed that you can wake up your computer from sleep by pressing a key on its keyboard or clicking a button on its mouse?

Related

Detect USB connection on atmega168 v-usb

I'm using atmega168 with AVRUSB. (yeah, old version of V-USB. already integrated and seems to work otherwise, so no hurry to change)
My hardware can be powered by USB or via other source. How can I detect if the host is actually connected? It would be also nice to init USB whenever it's connected, but not necessary. Most important is that I need to skip the blocking USB polling if there's no host.
The hardware I'm using doesn't have USB VBUS connected to any GPIO, so a software-based method is required. Maybe I could add some timeout to the USB init routine or something similar?

Emulate CP210x USB-FTDI chip using SAMD21

The dev boards for the ESP32 family of MCU use CP210x (or similar) "FTDI" chips to communicate with the MCU when flashing. CP210x presents a COM port to the host computer which runs esptool.py, a script which implements the Esressif communication protocol. Rather then use a CP210x, I would like to utilize a SAMD21 in its place.
I have managed to receive logs from my ESP32, to a serial monitor on my computer, via the SAMD21, over USB. The SAMD21 connects to the ESP32 via UART, with the standard ESP configuration of 115200 8N1. I can manually enter bootloader mode by holding the correct pins low at boot, and I get the log back confirming the correct bootloader mode.
When I run esptool.py, however, the connection fails, and I get a timeout. Likewise the esptool "monitor" fails to acknowledge the same logs which my terminal emulator easily detects.
What is the communication protocol between the host computer running esptool.py, the CP210x FTDI chip, and the ESP32, and how can I emulate the CP210x with a SAMD21? The definition of "FTDI", "TTL" and "RS232" are all a bit fuzzy, as far as I can determine with research online, so if anyone has experience in this arena, I would be very curious to hear your advice.
The code I have on my SAMD21 is just:
#include <Arduino.h>
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}
void loop()
{
if(Serial.available())
{
Serial1.write(Serial.read());
}
if(Serial1.available())
{
Serial.write(Serial1.read());
}
}
Where Serial1 is the UART and Serial is the USB Serial connection.
esptool.py expects a serial port (known as COM port on Windows) to communicate with the ESP32. It doesn't care if the computer has an old-style serial port, is using a USB-to-serial bridge with a proprietary protocol (FTDI, CP210x and the like) or is using the standardized UBS protocol for serial communication (USB CDC ACM). This is left to the operating system and the installed drivers.
However, esptool.py modifies the baud rate and uses the RTS and DTR signals to reset the ESP32. It is also rather susceptible to timing issues with regards to the reset. If you set the ESP32 in boot mode manually, you should be able to get away without these.
However, the most likely cause is that the Arduino CDC implementation does not implement flow control. If esptool.py sends more data than fit into the internal buffer, it is likely discarded, instead of sending a NAK back so the host computer can retry later.
I don't fully understand the SAMD21 Arduino core to verify it. If so, I don't see how you can make it work with an Arduino program. You would need to resort to some other framework for programming the SAMD21.
Update
After more tests, it turns out the USB CDC implementation of the SAMD21 Arduino core correctly implements flow control. So no data will be lost.
In fact, I was able to successfully upload code to an ESP32 module. The setup was:
Arduino code built with PlatformIO. Instead of Serial, I've used SerialUSB as I'm unsure how to control the project settings available in the regular Arduino IDE.
For the ESP32, I've used a ESP32-WROOM-32 module on a minimal board (reset and boot button, 2 pull-up resistors).
I've connected the board via GND, 3.3V, TX, RX directly to the SAMD21 dev board.
I've verified that I can see the ESP32 log output in normal run mode and the "waiting for download" prompt in bootloader mode (after pressing BOOT and RESET).
Arduino has multiple boards where the esp32 is on-board as WiFi adapter. To flash the esp32, there is a tool sketch called SerialNINAPassthrough in examples of the WiFi library, which should be uploaded into the main MCU of the board (SAMD21 on two of the official boards).
The SerialNINAPassthrough sketch handles the DTR and RTS signals sent by the esptool to reset the board into the flashing mode.

Changing USB configurations/interfaces on the fly; initiated by the device

I'm working on a USB MIDI device that will function as the receiver for a wireless system. This device will communicate bi-directionally though a radio module with the transmitter, a separate piece of hardware that runs in USB host mode.
The receiver will be plugged into a PC. MIDI devices plugged into the transmitter need to show up on the PC as MIDI ports. Since the transmitter supports a USB hub, there can be multiple devices plugged in.
There are two requirements that I'm not 100% how to meet:
1.) The MIDI port names on the PC end need to reflect the name supplied by the USB device plugged into the transmitter so that it's clear which device the port is for.
2.) The set of MIDI ports on the PC needs to update when devices are plugged/unplugged from the transmitter. This is the crux of the question: is there a way to update the available USB interfaces/MIDI jacks initiated by the device?
The brute force way of doing this would be to completely reset the receiver any time it receives a message from the transmitter that there's been a change (on reset,the receiver would then poll the transmitter for current devices and supply the updated info when the host PC re-enumerates).
The transmitter/receiver hardware are both based on PIC32MZ MCUs (no RTOS). I'm good with writing the USB code to get the host/receiver end to do whatever. The question is about how, at the level of the USB protocol, to do this.
Also, just to be clear: The transmitter/receiver communication will be an ad-hoc protocol and the receiver will set up all its USB configuration data itself; the idea isn't to attempt to seamlessly enumerate devices over the wireless link.
To show the port names on the PC, just copy the USB descriptor strings over to the transmitter.
The only way for a USB device to change its configuration is to reset itself, as if it had been unplugged, and to let the host re-enumerate it. So the only way to prevent multiple devices from interfering with each other is to have multiple (virtual) USB devices on the receiver. If your hardware does not support this, then you cannot avoid the reset.

Wifi signal logger device

I am working on indoor gps tracking and for this I need a wifi signal logger which can receive the signals from different smartphone(whose wifi is turned on) an send it to a remote server. I wanted to know, where I can get this kind of hardware, having capability to receive signals from different smartphones and log its signal strength and mac address in a remote server.
Please let me know the details.
Thank you in advance.
Pravin Prasad
In simple terms, Wifi clients(smartphones, notebooks etc.) send periodic Probe Request packets to actively seek wifi access points and Wifi APs(routers, gateways etc.) send periodic Beacon packets to advertise the presence of WLAN network.
You can use any wifi capable device like a smartphone or notebook to log the above packets.
Configure your wireless card in monitor mode.
Use a packet sniffer(for example, wireshark) to capture the traffic on the wireless interface or write your own with libpcap. You may filter on the packet type, source, destination etc.

Virtual COM Communications Issue

I'm working on a Communications Device Class (CDC) driver for an embedded device, a Full Speed implementation of USB 2.0. The COM port settings are 115200, 8-bit, no parity, 1 stop bit, no flow control. Our PC application (32-bit, Windows 7, .NET 2.0) communicates with the target device through a virtual COM port, which on the target device can connect to either a FTDI (USB-to-SCI bridge) chip or the integrated USB peripheral in the microcontroller, depending on which port is selected by the application.
Both virtual COM ports work without any problems using Realterm. However, while our desktop application works using the virtual COM port connected via the FTDI chip, it hangs when attempting to use the virtual COM connected via the microcontroller's integrated USB peripheral.
When connected via the virtual COM port using the integrated USB, the application consistently hangs on the second call to SerialPort.Write(...). Using Serial Monitor from HHD Software I can see that the data is transmitted on the first call to SerialPort.Write(...). However, that data is never received by the target device.
It's odd because the only time I have seen similar problems on previous projects was when the flow control settings on each side of the bus were mismatched.
Additional info...
Here is the data captured from various port monitoring tools while running our PC application connected to the target device via its integrated USB peripheral. Any insight would be appreciated.
Sysinternals Portmon
Advanced USB Port Monitor
Device Monitoring Studio - Request View
Device Monitoring Studio - Packet View
For those that are interested, I am using CodeWarrior 10.2 with the MCF51JM128 from Freescale.
Any ideas or suggestions would be appreciated. Thanks.
It is clear from the logs that you are making the classic mistake of not taking care of the hardware handshaking signals. That only ever works by accident, a terminal emulator like Realterm will never make that mistake.
You must set the DtrEnable property to true. That turns on the Data Terminal Ready signal. It is important because RS-232 is an unterminated bus so is subject to electrical noise when the cable is disconnected or the power turned off. DTR convinces the device that it is in fact connected to a powered device. This is emulated of course in your case but the driver or firmware will still typically implement the RS-232 behavior.
And the RtsEnable property is important, used to handshake with the device and prevent the receive buffer from overflowing when the app isn't emptying the buffer in a timely matter. You really should set the Handshake property to Handshake.RequestToSend, the most common way a device implements it. Which then also takes care of turning RTS on. If you have to use Handshake.None for some reason then you have to turn it on yourself by setting RtsEnable to true.
This ought to take of the problem. If you still have trouble then use PortMon to spy on the way Realterm initializes the driver. Compare the commands you see against the commands that the SerialPort class sends. Make sure they are the same. In value, not in sequence.