转自:https://blog.csdn.net/silence3039/article/details/100187666
一、简介
OTG是On-The-Go的缩写。其设计的初衷是为了两个“外设”在没有PC(Host)的情况下,也可以通过USB进行数据传输。可以理解为,拥有OTG功能的USB设备(OTG设备)既可以做host,也可以做peripheral。
二、OTG基本结构
2.1 OTG硬件结构
一个完整的OTG连接由A-device和B-device组成。A设备(Micro-A)默认作为Host并供电,B设备(Micro-B)默认作为peripheral,B设备可以通过HNP协议进行协商,使自己成为Host。
OTG比普通USB接口多了一条USB_OTG_ID连线。Micro-A插座的USB_OTG_ID接地,而Micro-B插座的USB_OTG_ID悬空。如图所示。
2.2 OTG连接方式
2.2.1 OTG设备和host连接
在这种情况下,OTG设备和PC或嵌入式主机连接。此时,OTG设备满足标准USB外设的所有要求。当OTG设备连接到PC或嵌入式主机时,主机枚举OTG设备并将其视为外围设备。
2.2.2 OTG设备和peripheral连接
在这种情况下, OTG设备作host(A-Device),外接一个peripheral设备(B-Device)。当OTG设备检测到设备已连接时,OTG设备枚举peripheral设备.
2.2.3 OTG设备和OTG设备连接
在这种情况下,默认host(A-Device)首先枚举peripheral(B-Device)。然后user可以控制选择A-Device或B-Device哪个作host。比如,连接carplay时,iphone为B-Device作host;而A-Device需将状态切花挪威peripheral。
2.3 OTG支持协议
OTG支持HNP协议,SRP协议。
2.3.1 HNP协议
HNP(即主机协商协议),实现在不调换Micro-A和Micro-B插座的情况下,Host和Peripheral两种角色在A-Device和B-Device上相互切换;
无论A-device扮演host还是peripheral角色,均由A-device向USB接口供电;
系统初始化,应将A-device默认设置为host。当B设备写入b_bus_req,向A设备发起HNP请求。待A设备响应之后,A设备发送a_set_b_hnp_en,B设备响应之后即进入主机状态,同时发送请求使用A设备set_device,这样A、B设备完成主从交换。
2.3.2 SRP协议
A-device在总线空闲时关闭总线。当一个B-device想要工作时,可向A-device请求开启总线。
A-Device时序图如下
B-Device时序图如下
三、OTG状态机
3.1 OTG状态机状态简介
OTG-A-device状态迁移图如图所示。
OTG-B-device状态迁移图如图所示。
以A-Device为例,主要otg_machine_status如下表所示。
a_idle
A-device starting state
a_wait_vrise
Wait for VBUS to go into regulation
a_wait_bcon
Wait for B-device to signal connection
a_host
Acting as a host
a_suspend
Bus suspend
a_peripheral
Acting as a peripheral
a_wait_vfall
Wait for VBUS to drop to VOTG_VBUS_LKG
a_vbus_err
Wait for recovery from over-current condition
状态机主要输入状态
★id :A-Device为0,否则是B-Device为1。
The identification (id) input is FALSE when a Micro-A plug is inserted in the device’s Micro-AB receptacle. Otherwise, this input is TRUE.
★a_bus_drop :A-Device放弃总线时为1,此时a_bus_req必须为0。
The “A-device bus drop” (a_bus_drop) input is TRUE when the Application running on the A-device wants to power down the bus, and is FALSE otherwise. When this input is TRUE, then the a_bus_req input shall be FALSE.
★a_bus_req :A-Device申请总线时为1。
The “A-device bus request” (a_bus_req) input is TRUE during the time that the Application running on the A-device wants to use the bus, and is FALSE when the Application no longer wants to use the bus. a_bus_req can also be set to TRUE in response to remote wakeup signaling from the B-device should the A-device decide to resume the bus.
……
3.2 OTG状态机切换流程(A-Device)
★启动时,状态机的正确切换流程如下:
(START)->(a_idle)->(a_wait_vrise)->(a_wait_bcon)
★连接仅peripheral的B-Device,状态机的正确切换流程如下:
(a_wait_bcon)->(a_host)
拔出仅peripheral的B-Device,状态机的正确切换流程如下:
(a_host)->(a_wait_bcon)
★插入OTG B-Device,状态机的正确切换流程如下(A-Device为peripheral):
(a_wait_bcon)->(a_host)->(a_suspend)->(a_peripheral)
拔出OTG B-Device,状态机的正确切换流程如下:
(a_peripheral)->(a_wait_bcon)
四、代码分析
OTG代码主要分为硬件层,驱动层和功能层。
硬件层主要source文件:udc.c, host.c
驱动层主要source文件:core.c
功能层主要source文件:otg.c, otg_fsm.c, usb-otg-fsm.c
OTG注册流程图如下
OTG中断处理流程图如下
SRP协议流程图如下
HNP协议流程图如下
附录
SRP
In order to conserve power, an A-device is allowed to leave VBUS turned off when the bus is not being used. The Session Request Protocol (SRP) allows a B-device to request the A-device to turn on the power supply to the USB interface (VBUS) and start a session. A session is defined as the period of time that VBUS is powered. The session ends when VBUS is no longer powered.
HNP
The Host Negotiation Protocol (HNP) allows the host function to be transferred between two directly connected OTG devices and eliminates the need for a user to switch the cable connections in order to allow a change in control of communications between the devices. HNP will typically be initiated in response to input from the user or an Application on the OTG B-device. HNP may only be implemented through the Micro-AB receptacle on a device. The A-device is always responsible for powering the USB interface regardless of whether it is acting in host or peripheral role3.
At the start of a session, the A-device defaults to having the role of host. During a session, the role of host can be transferred back and forth between the A-device and the B-device any number of times, using HNP.
————————————————
版权声明:本文为CSDN博主「叫我一声小明哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/silence3039/article/details/100187666