How usb OTG works(master/slave)

2019-06-07 00:36发布

问题:

A device has usb OTG

Following is scenarios

  1. when a device connected to PC ,then device act as slave (how device knows it has to act as slave)

  2. when a device connected to printer ,then device act as master. (how device knows it has to act as master)

what are the steps executed when device connected to OTG. how to implement this mechanism (in brief)?

回答1:

The exact behavior of USB OTG devices is described in the specification you can find at usb.org. There is a PDF inside the zip called USB_OTG.

The Host Negotiation Protocol in section 6 covers how two OTG devices decide which one is getting the embedded host. Basically this is archived by driving pull-up and pull-down resistors on the D+ line.

Note: When talking about USB the terms master/slave are not used. The master is called host and powers the bus whereas the slave is called device. In the case of OTG (in general, see specification for exceptions) both parts have the capability to be host or device. When the host was figured out by the Host Negotiation Protocol, this part becomes the so called embedded host.



回答2:

In the two scenarios you mentioned the USB device is able to know whether to be a host or device by the cable. USB cables (non type-C) are not symmetrical. One side is host and the other is device. There is a pin on the connector called the ID pin which is floated on the device side and grounded on the host side. This allows the USB controller on each side to know which side of the cable it is connected to and thus which role (host or device) it should be when connected. These types of devices are called dual-role devices.

If you have a such a device, then you can plug it into a regular host (like your laptop) and it will act as a device. And you can plug it into a regular device (like a printer) and it will act as a host. This is all based on the cable.

If you plug two dual-role OTG devices to each other. Their initial roles are determined by the cable in the same way.

After the initial roles are determined, they can then swap roles from their initial cable-determined roles via the Host-Negotiation Protocol (HNP).

As far as implementing this. There is no brief way to explain it. Every controller is different and you would have to read the controller databook and programming model to implement all these procedures. As well as having a good understanding of the USB and OTG specs themselves.

A good place to start is http://usb.org where you can find all the specs.