1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | * Copyright (C) 2015-2016 by TOPPERS PROJECT Educational Working Group.
|
---|
9 | *
|
---|
10 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
11 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
12 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
13 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
14 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
15 | * スコード中に含まれていること.
|
---|
16 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
17 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
18 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
19 | * の無保証規定を掲載すること.
|
---|
20 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
21 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
22 | * と.
|
---|
23 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
24 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
25 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
26 | * 報告すること.
|
---|
27 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
28 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
29 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
30 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
31 | * 免責すること.
|
---|
32 | *
|
---|
33 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
34 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
35 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
36 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
37 | * の責任を負わない.
|
---|
38 | *
|
---|
39 | * $Id$
|
---|
40 | */
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * USB OTGデバイスドライバのヘッダファイル
|
---|
44 | */
|
---|
45 |
|
---|
46 | #ifndef _USB_OTG_H_
|
---|
47 | #define _USB_OTG_H_
|
---|
48 |
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * USBポート定義
|
---|
52 | */
|
---|
53 | #define USB1_PORTID 1
|
---|
54 | #define USB2_PORTID 2
|
---|
55 | #define NUM_USBPORT 2
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * USB OTGモード
|
---|
59 | */
|
---|
60 | #define USB_OTG_MODE_DEVICE 0
|
---|
61 | #define USB_OTG_MODE_HOST 1
|
---|
62 | #define USB_OTG_MODE_DRD 2
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * USB PHY定義
|
---|
66 | */
|
---|
67 | #define USB_PHY_ULPI 1
|
---|
68 | #define USB_PHY_EMBEDDED 2
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * エンドポイントタイプ
|
---|
72 | */
|
---|
73 | #define EP_TYPE_CTRL 0
|
---|
74 | #define EP_TYPE_ISOC 1
|
---|
75 | #define EP_TYPE_BULK 2
|
---|
76 | #define EP_TYPE_INTR 3
|
---|
77 | #define EP_TYPE_MSK 3
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * USBスピード
|
---|
81 | */
|
---|
82 | #define USB_SPEED_HIGH 0
|
---|
83 | #define USB_SPEED_HIGH_IN_FULL 1
|
---|
84 | #define USB_SPEED_LOW 2
|
---|
85 | #define USB_SPEED_FULL 3
|
---|
86 |
|
---|
87 |
|
---|
88 | #define HC_PID_DATA0 0
|
---|
89 | #define HC_PID_DATA2 1
|
---|
90 | #define HC_PID_DATA1 2
|
---|
91 | #define HC_PID_SETUP 3
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * URB状態定義
|
---|
95 | */
|
---|
96 | #define URB_IDLE 0
|
---|
97 | #define URB_DONE 1
|
---|
98 | #define URB_NOTREADY 2
|
---|
99 | #define URB_NYET 3
|
---|
100 | #define URB_ERROR 4
|
---|
101 | #define URB_STALL 5
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * HOSTチャンネル状態定義
|
---|
105 | */
|
---|
106 | #define USBO_HC_IDLE 0
|
---|
107 | #define USBO_HC_XFRC 1
|
---|
108 | #define USBO_HC_HALTED 2
|
---|
109 | #define USBO_HC_NAK 3
|
---|
110 | #define USBO_HC_NYET 4
|
---|
111 | #define USBO_HC_STALL 5
|
---|
112 | #define USBO_HC_XACTERR 6
|
---|
113 | #define USBO_HC_BBLERR 7
|
---|
114 | #define USBO_HC_DATATGLERR 8
|
---|
115 | #define USBO_HC_FAIL 16
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * LPMアクティブパラメータ
|
---|
119 | */
|
---|
120 | #define PCD_LPM_L0_ACTIVE 0x00 /* on */
|
---|
121 | #define PCD_LPM_L1_ACTIVE 0x01 /* LPM L1 sleep */
|
---|
122 | #define PCD_LPM_L2 0x02 /* suspend */
|
---|
123 | #define PCD_LPM_L3 0x03 /* off */
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * エンドポイント・ホストチャネルの最大数
|
---|
127 | */
|
---|
128 | #define MAX_EPS_CHANNELS 16
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * デバイスステート
|
---|
132 | */
|
---|
133 | #define DWC2_L0 0 /* On state */
|
---|
134 | #define DWC2_L1 1 /* LPM sleep state */
|
---|
135 | #define DWC2_L2 2 /* USB suspend state */
|
---|
136 | #define DWC2_L3 3 /* Off state */
|
---|
137 |
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * USB OTGハードウェア情報
|
---|
141 | */
|
---|
142 | typedef struct
|
---|
143 | {
|
---|
144 | unsigned en_multiple_tx_fifo:1;
|
---|
145 | unsigned host_rx_fifo_size:16;
|
---|
146 | unsigned max_transfer_size:26;
|
---|
147 | unsigned max_packet_count:11;
|
---|
148 | unsigned total_fifo_size:16;
|
---|
149 | }USB_OTG_HwParam_t;
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * USB OTG初期設定定義
|
---|
153 | */
|
---|
154 | typedef struct
|
---|
155 | {
|
---|
156 | uint32_t usb_otg_mode; /* USB OTGモード */
|
---|
157 | uint32_t dev_endpoints; /* デバイスエンドポイント数(1-15) */
|
---|
158 | uint32_t host_channels; /* ホストチャネル数(1-15) */
|
---|
159 | uint32_t speed; /* USBコアスピード */
|
---|
160 | uint32_t dma_enable; /* USB-DMA機能の有効無効設定(0 or 1) */
|
---|
161 | uint32_t phy_itface; /* PHYインターフェイス選択 */
|
---|
162 | uint32_t sof_enable; /* SOF割込みの有効無効設定(0 or 1) */
|
---|
163 | uint32_t low_power_enable; /* LOW POWERモード有効無効設定(0 or 1) */
|
---|
164 | uint32_t lpm_enable; /* Link Power Management有効無効設定(0 or 1) */
|
---|
165 | uint32_t vbus_sensing_enable; /* VBUSセンス有効無効設定(0 or 1) */
|
---|
166 | uint32_t use_dedicated_ep1; /* エンドポイント1専用割込み有効無効設定(0 or 1) */
|
---|
167 | uint32_t use_external_vbus; /* 外部VBUS有効無効設定(0 or 1) */
|
---|
168 | }USB_OTG_Init_t;
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * エンドポイント定義
|
---|
172 | */
|
---|
173 | typedef struct
|
---|
174 | {
|
---|
175 | uint8_t num; /* エンドポイント番号(0-14) */
|
---|
176 | uint8_t is_in : 1; /* エンドポイントOUT/IN(0 or 1) */
|
---|
177 | uint8_t is_stall : 1; /* エンドポイントスチール状態(0 or 1) */
|
---|
178 | uint8_t type : 2; /* エンドポイントタイプ */
|
---|
179 | uint8_t data_pid_start : 1; /* 初期データPID(0 or 1) */
|
---|
180 | uint8_t even_odd_frame : 1; /* フレームパリティ(0 or 1) */
|
---|
181 | uint16_t tx_fifo_num; /* 転送FIFO番号(1-15) */
|
---|
182 | uint16_t maxpacket; /* エンドポイント最大パケットサイズ(0-64KB) */
|
---|
183 | uint8_t *xfer_buff; /* 転送バッファポインター */
|
---|
184 | uint32_t dma_addr; /* DMA転送アドレス(32bitアライン) */
|
---|
185 | uint32_t xfer_len; /* 現在の転送数 */
|
---|
186 | uint32_t xfer_count; /* 指定転送カウント */
|
---|
187 | }USB_OTG_EPTypeDef;
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * ホストクラス定義
|
---|
191 | */
|
---|
192 | typedef struct
|
---|
193 | {
|
---|
194 | uint8_t dev_addr; /* USBデバイスアドレス(1-255) */
|
---|
195 | uint8_t ch_num; /* Hostチャネル番号(1-15) */
|
---|
196 | uint8_t ep_num; /* エンドポイント番号(1-15) */
|
---|
197 | uint8_t ep_is_in; /* エンドポイントのディレクション(0 or 1) */
|
---|
198 | uint8_t speed; /* USB Hostスピード */
|
---|
199 | uint8_t do_ping; /* HSモードのPINGプロトコルの有効無効設定(0 or 1) */
|
---|
200 | uint8_t process_ping; /* PINGプロトコル実行中フラグ(1で実行中) */
|
---|
201 | uint8_t ep_type; /* エンドポイントタイプ(0-3) */
|
---|
202 | uint16_t max_packet; /* 最大パケットサイズ(64KB以内) */
|
---|
203 | uint8_t data_pid; /* 初期設定データPID. */
|
---|
204 | uint8_t *xfer_buff; /* 転送バッファへのポインタ */
|
---|
205 | uint32_t xfer_len; /* 現在の転送数 */
|
---|
206 | uint32_t xfer_count; /* 指定転送サイズ */
|
---|
207 | uint8_t toggle_in; /* IN transfer current toggle flag. */
|
---|
208 | uint8_t toggle_out; /* OUT transfer current toggle flag */
|
---|
209 | uint32_t dma_addr; /* DMA転送アドレス(4バイトアライン) */
|
---|
210 | uint32_t err_count; /* Hostチャネルエラーカウンター */
|
---|
211 | volatile uint8_t urb_state; /* URBステート */
|
---|
212 | volatile uint8_t state; /* Hostチャネルステート */
|
---|
213 | }USB_OTG_HCTypeDef;
|
---|
214 |
|
---|
215 |
|
---|
216 | #ifndef USBD_HS_TRDT_VALUE
|
---|
217 | #define USBD_HS_TRDT_VALUE 9U
|
---|
218 | #endif /* USBD_HS_TRDT_VALUE */
|
---|
219 |
|
---|
220 |
|
---|
221 | #define USBOTG_BASE(a) ((uint32_t)(a))
|
---|
222 | #define USBPGC_BASE(a) ((uint32_t)(a)+USB_OTG_PCGCCTL_BASE)
|
---|
223 | #define USBPRT_BASE(a) ((uint32_t)(a)+USB_OTG_HOST_PORT_BASE)
|
---|
224 | #define USBD_BASE(a) ((uint32_t)(a)+USB_OTG_DEVICE_BASE)
|
---|
225 | #define USBIEP_BASE(a, i) ((uint32_t)(a)+USB_OTG_IN_ENDPOINT_BASE+((i)*USBO_EP_REG_SIZE))
|
---|
226 | #define USBOEP_BASE(a, i) ((uint32_t)(a)+USB_OTG_OUT_ENDPOINT_BASE+((i)*USBO_EP_REG_SIZE))
|
---|
227 | #define USBD_FIFO(a, i) ((uint32_t)(a)+USB_OTG_FIFO_BASE+((i)*USB_OTG_FIFO_SIZE))
|
---|
228 | #define USBH_BASE(a) ((uint32_t)(a)+USB_OTG_HOST_BASE)
|
---|
229 | #define USBHC_BASE(a, i) ((uint32_t)(a)+USB_OTG_HOST_CHANNEL_BASE+((i)*USBO_HOST_CHANNEL_REG_SIZE))
|
---|
230 |
|
---|
231 |
|
---|
232 | #ifndef TOPPERS_MACRO_ONLY
|
---|
233 |
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * USB_OTGデバイスハンドラ定義
|
---|
237 | */
|
---|
238 | typedef struct _USB_OTG_Handle_t USB_OTG_Handle_t;
|
---|
239 | struct _USB_OTG_Handle_t
|
---|
240 | {
|
---|
241 | uint32_t base; /* USB OTG レジスタベースアドレス */
|
---|
242 | USB_OTG_Init_t Init; /* USB OTG 初期設定 */
|
---|
243 | USB_OTG_HCTypeDef hc[MAX_EPS_CHANNELS]; /* HOST チャネルデータ */
|
---|
244 | USB_OTG_EPTypeDef IN_ep[MAX_EPS_CHANNELS-1]; /* IN エンドポイントデータ */
|
---|
245 | USB_OTG_EPTypeDef OUT_ep[MAX_EPS_CHANNELS-1]; /* OUT エンドポイントデータ */
|
---|
246 | volatile uint32_t Setup[12]; /* Setup パケットバッファ */
|
---|
247 | uint8_t lx_state; /* lx state */
|
---|
248 | uint8_t enabled; /* device enabled */
|
---|
249 | uint8_t connected; /* device 接続中 */
|
---|
250 | uint8_t dedicated_fifos; /* device dedicated fifo mode */
|
---|
251 | uint32_t resetbit; /* usb reset bit */
|
---|
252 | uint32_t fifo_mem; /* fifo memoryサイズ */
|
---|
253 | uint32_t pcd_rx_fifo_sz; /* pcd rx fifoサイズ */
|
---|
254 | uint32_t pcd_np_g_tx_fifo_sz; /* pcd g tx fifoサイズ */
|
---|
255 | uint32_t pcd_tx_fifo_sz[MAX_EPS_CHANNELS]; /* pcd tx fifoテーブル */
|
---|
256 | #ifdef USB_OTG_GLPMCFG_LPMEN
|
---|
257 | volatile uint32_t BESL; /* BESL保存領域 */
|
---|
258 | volatile uint8_t lpm_state; /* LPM 状態 */
|
---|
259 | uint8_t lpm_active; /* Link Power Management有効無効設定(0 or 1) */
|
---|
260 | #endif /* USB_OTG_GLPMCFG_LPMEN */
|
---|
261 | USB_OTG_HwParam_t hw_params; /* ハードウェア設定値 */
|
---|
262 | void (*hostsofcallback)(USB_OTG_Handle_t * hhcd); /* sofコールバック関数 */
|
---|
263 | void (*hostconnectcallback)(USB_OTG_Handle_t * hhcd); /* connectコールバック関数 */
|
---|
264 | void (*hostdisconnectcallback)(USB_OTG_Handle_t * hhcd); /* disconnectコールバック関数 */
|
---|
265 | void (*hostchangeurbcallback)(USB_OTG_Handle_t * hhcd, uint8_t num, uint8_t state); /* changeurbコールバック関数 */
|
---|
266 | void (*devsetupstagecallback)(USB_OTG_Handle_t * hhcd);
|
---|
267 | void (*devdataoutstagecallback)(USB_OTG_Handle_t * hhcd, uint8_t epnum);
|
---|
268 | void (*devdatainstagecallback)(USB_OTG_Handle_t * hhcd, uint8_t epnum);
|
---|
269 | void (*devsofcallback)(USB_OTG_Handle_t * hhcd);
|
---|
270 | void (*devresetcallback)(USB_OTG_Handle_t * hhcd);
|
---|
271 | void (*devsuspendcallback)(USB_OTG_Handle_t * hhcd);
|
---|
272 | void (*devresumecallback)(USB_OTG_Handle_t * hhcd);
|
---|
273 | void (*devisooutcallback)(USB_OTG_Handle_t * hhcd, uint8_t epnum);
|
---|
274 | void (*devisoincallback)(USB_OTG_Handle_t * hhcd, uint8_t epnum);
|
---|
275 | void (*devconnectcallback)(USB_OTG_Handle_t * hhcd);
|
---|
276 | void (*devdisconnectcallback)(USB_OTG_Handle_t * hhcd);
|
---|
277 | void (*devlpmcallback)(USB_OTG_Handle_t * hhcd, uint8_t msg);
|
---|
278 | void *pHost; /* Pointer Host Stack Handler */
|
---|
279 | void *pDev; /* Pointer Device Stack Handler */
|
---|
280 | };
|
---|
281 |
|
---|
282 | extern USB_OTG_Handle_t *usbo_init(ID portid, USB_OTG_Init_t *pini);
|
---|
283 | extern ER usbo_deinit(USB_OTG_Handle_t *husb);
|
---|
284 | extern ER usbo_devinit(USB_OTG_Handle_t *husb);
|
---|
285 | extern ER usbo_coreinit(USB_OTG_Handle_t *husb);
|
---|
286 | extern ER usbo_setupint(USB_OTG_Handle_t *husb);
|
---|
287 | extern ER usbo_enableglobalint(USB_OTG_Handle_t *husb);
|
---|
288 | extern ER usbo_disableglobalint(USB_OTG_Handle_t *husb);
|
---|
289 | extern ER usbo_setcurrentmode(USB_OTG_Handle_t *husb);
|
---|
290 | extern ER usbo_flushRxFifo(USB_OTG_Handle_t *husb);
|
---|
291 | extern ER usbo_flushTxFifo(USB_OTG_Handle_t *husb, uint32_t num);
|
---|
292 | extern ER usbo_initFiFo(USB_OTG_Handle_t *husb);
|
---|
293 | extern ER usbo_activateEndpoint(USB_OTG_Handle_t *husb, USB_OTG_EPTypeDef *ep);
|
---|
294 | extern ER usbo_deactivateEndpoint(USB_OTG_Handle_t *husb, USB_OTG_EPTypeDef *ep);
|
---|
295 | extern ER usbo_epstartxfer(USB_OTG_Handle_t *husb, USB_OTG_EPTypeDef *ep);
|
---|
296 | extern ER usbo_ep0startxfer(USB_OTG_Handle_t *husb, USB_OTG_EPTypeDef *ep);
|
---|
297 | extern ER usbo_epsetStall(USB_OTG_Handle_t *husb, USB_OTG_EPTypeDef *ep);
|
---|
298 | extern ER usbo_epclearStall(USB_OTG_Handle_t *husb, USB_OTG_EPTypeDef *ep);
|
---|
299 | extern ER usbo_setDevAddress(USB_OTG_Handle_t *husb, uint8_t address);
|
---|
300 | extern ER usbo_devconnect(USB_OTG_Handle_t *husb);
|
---|
301 | extern ER usbo_devdisconnect(USB_OTG_Handle_t *husb);
|
---|
302 | extern ER usbo_stopdevice(USB_OTG_Handle_t *husb);
|
---|
303 | extern ER usbo_ep0_outstart(USB_OTG_Handle_t *husb, uint8_t *psetup);
|
---|
304 | extern uint8_t usbo_getDevSpeed(USB_OTG_Handle_t *husb);
|
---|
305 | extern ER usbo_init_lpm(USB_OTG_Handle_t *husb);
|
---|
306 |
|
---|
307 | extern ER usbo_hostinit(USB_OTG_Handle_t *husb);
|
---|
308 | extern ER usbo_starthost(USB_OTG_Handle_t *husb);
|
---|
309 | extern ER usbo_resetport(USB_OTG_Handle_t *husb);
|
---|
310 | extern ER usbo_drivevbus(USB_OTG_Handle_t *husb, uint8_t state);
|
---|
311 | extern void usbo_driveextvbus(USB_OTG_Handle_t *husb, uint8_t state);
|
---|
312 | extern uint32_t usbo_gethostspeed(USB_OTG_Handle_t *husb);
|
---|
313 | extern uint32_t usbo_getcurrentframe(USB_OTG_Handle_t *husb);
|
---|
314 | extern ER usbo_hc_init(USB_OTG_Handle_t *husb, uint8_t ch_num, uint8_t epnum);
|
---|
315 | extern ER usbo_hc_startxfer(USB_OTG_Handle_t *husb, uint8_t ch_num);
|
---|
316 | extern ER usbo_hc_halt(USB_OTG_Handle_t *husb, uint8_t ch_num);
|
---|
317 | extern ER usbo_stophost(USB_OTG_Handle_t *husb);
|
---|
318 |
|
---|
319 | extern void usbo_hcd_irqhandler(USB_OTG_Handle_t *husb);
|
---|
320 | extern void usbo_pcd_irqhandler(USB_OTG_Handle_t *husb);
|
---|
321 |
|
---|
322 | extern void usb_otg_isr(intptr_t exinf);
|
---|
323 | extern void usb_otg_wkup_isr(intptr_t exinf);
|
---|
324 |
|
---|
325 | extern void usbh_task(intptr_t exinf);
|
---|
326 |
|
---|
327 | #endif /* TOPPERS_MACRO_ONLY */
|
---|
328 |
|
---|
329 | #endif /* _USB_OTG_H_ */
|
---|