小明哥學linux驅動之USB-OTG(基於gadget框架)【轉】

轉自: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章