1 /********************************** (C) COPYRIGHT  *******************************
2  * File Name          : ch32v10x_usb_host.h
3  * Author             : WCH
4  * Version            : V1.0.0
5  * Date               : 2020/04/30
6  * Description        : This file contains all the functions prototypes for the USB
7  *                      Host firmware library.
8  * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
9  * SPDX-License-Identifier: Apache-2.0
10  *******************************************************************************/
11 #ifndef __CH32V10x_USBHOST_H
12 #define __CH32V10x_USBHOST_H
13 
14 #include "ch32v10x_usb.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define ERR_SUCCESS            0x00
21 #define ERR_USB_CONNECT        0x15
22 #define ERR_USB_DISCON         0x16
23 #define ERR_USB_BUF_OVER       0x17
24 #define ERR_USB_DISK_ERR       0x1F
25 #define ERR_USB_TRANSFER       0x20
26 #define ERR_USB_UNSUPPORT      0xFB
27 #define ERR_USB_UNKNOWN        0xFE
28 #define ERR_AOA_PROTOCOL       0x41
29 
30 #define ROOT_DEV_DISCONNECT    0
31 #define ROOT_DEV_CONNECTED     1
32 #define ROOT_DEV_FAILED        2
33 #define ROOT_DEV_SUCCESS       3
34 #define DEV_TYPE_KEYBOARD      (USB_DEV_CLASS_HID | 0x20)
35 #define DEV_TYPE_MOUSE         (USB_DEV_CLASS_HID | 0x30)
36 #define DEF_AOA_DEVICE         0xF0
37 #define DEV_TYPE_UNKNOW        0xFF
38 
39 #define HUB_MAX_PORTS          4
40 #define WAIT_USB_TOUT_200US    3000
41 
42 typedef struct
43 {
44     UINT8  DeviceStatus;
45     UINT8  DeviceAddress;
46     UINT8  DeviceSpeed;
47     UINT8  DeviceType;
48     UINT16 DeviceVID;
49     UINT16 DevicePID;
50     UINT8  GpVar[4];
51     UINT8  GpHUBPortNum;
52 } _RootHubDev;
53 
54 extern _RootHubDev ThisUsbDev;
55 extern UINT8       UsbDevEndp0Size;
56 extern UINT8       FoundNewDev;
57 
58 extern PUINT8 pHOST_RX_RAM_Addr;
59 extern PUINT8 pHOST_TX_RAM_Addr;
60 #define pSetupReq    ((PUSB_SETUP_REQ)pHOST_TX_RAM_Addr)
61 
62 extern const UINT8 SetupGetDevDescr[];
63 extern const UINT8 SetupGetCfgDescr[];
64 extern const UINT8 SetupSetUsbAddr[];
65 extern const UINT8 SetupSetUsbConfig[];
66 extern const UINT8 SetupSetUsbInterface[];
67 extern const UINT8 SetupClrEndpStall[];
68 
69 void  DisableRootHubPort(void);
70 UINT8 AnalyzeRootHub(void);
71 void  SetHostUsbAddr(UINT8 addr);
72 void  SetUsbSpeed(UINT8 FullSpeed);
73 void  ResetRootHubPort(void);
74 UINT8 EnableRootHubPort(void);
75 void  SelectHubPort(UINT8 HubPortIndex);
76 UINT8 WaitUSB_Interrupt(void);
77 UINT8 USBHostTransact(UINT8 endp_pid, UINT8 tog, UINT32 timeout);
78 UINT8 HostCtrlTransfer(PUINT8 DataBuf, PUINT8 RetLen);
79 void  CopySetupReqPkg(const UINT8 *pReqPkt);
80 UINT8 CtrlGetDeviceDescr(PUINT8 DataBuf);
81 UINT8 CtrlGetConfigDescr(PUINT8 DataBuf);
82 UINT8 CtrlSetUsbAddress(UINT8 addr);
83 UINT8 CtrlSetUsbConfig(UINT8 cfg);
84 UINT8 CtrlClearEndpStall(UINT8 endp);
85 UINT8 CtrlSetUsbIntercace(UINT8 cfg);
86 
87 void  USB_HostInit(void);
88 UINT8 InitRootDevice(PUINT8 DataBuf);
89 UINT8 HubGetPortStatus(UINT8 HubPortIndex);                       // ��ѯHUB�˿�״̬,������TxBuffer��
90 UINT8 HubSetPortFeature(UINT8 HubPortIndex, UINT8 FeatureSelt);   // ����HUB�˿�����
91 UINT8 HubClearPortFeature(UINT8 HubPortIndex, UINT8 FeatureSelt); // ���HUB�˿�����
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #endif /* __CH32V10x_USBHOST_H */
97