1 /******************************************************************************************************************************************
2 * 文件名称: usbh_stdreq.c
3 * 功能说明:
4 * 技术支持: http://www.synwit.com.cn/e/tool/gbook/?bid=1
5 * 注意事项:
6 * 版本日期: V1.1.0 2020年11月3日
7 * 升级记录:
8 *
9 *
10 *******************************************************************************************************************************************
11 * @attention
12 *
13 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
14 * REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
15 * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
16 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
17 * -ECTION WITH THEIR PRODUCTS.
18 *
19 * COPYRIGHT 2012 Synwit Technology
20 *******************************************************************************************************************************************/
21 #include <string.h>
22 #include "SWM341.h"
23 #include "usbh_core.h"
24 #include "usbh_stdreq.h"
25
26
27 /******************************************************************************************************************************************
28 * 函数名称: USBH_GetDescriptor()
29 * 功能说明: 获取描述符
30 * 输 入: 无
31 * 输 出: 无
32 * 注意事项: 无
33 ******************************************************************************************************************************************/
USBH_GetDescriptor(USBH_Info_t * phost,uint8_t type,uint8_t index,uint8_t * buff,int size)34 USBH_Status USBH_GetDescriptor(USBH_Info_t *phost, uint8_t type, uint8_t index, uint8_t *buff, int size)
35 {
36 phost->Ctrl.setup.bRequestType = USB_REQ_D2H | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
37 phost->Ctrl.setup.bRequest = USB_GET_DESCRIPTOR;
38 phost->Ctrl.setup.wValue = (type << 8) | index;
39 phost->Ctrl.setup.wIndex = (type == USB_DESC_STRING) ? 0x0409 : 0;
40 phost->Ctrl.setup.wLength = size;
41
42 return USBH_CtrlTransfer(phost, buff, size);
43 }
44
45
46 /******************************************************************************************************************************************
47 * 函数名称: USBH_GetDescriptorEx()
48 * 功能说明: 获取描述符
49 * 输 入: uint8_t reqType USB_REQ_STANDARD、USB_REQ_CLASS、USB_REQ_VENDOR
50 * uint8_t reqTarget USB_REQ_TO_DEVICE、USB_REQ_TO_INTERFACE、USB_REQ_TO_ENDPOINT
51 * uint8_t type
52 * uint8_t index
53 * uint16_t wIndex
54 * 输 出: 无
55 * 注意事项: 无
56 ******************************************************************************************************************************************/
USBH_GetDescriptorEx(USBH_Info_t * phost,uint8_t reqType,uint8_t reqTarget,uint8_t type,uint8_t index,uint16_t wIndex,uint8_t * buff,int size)57 USBH_Status USBH_GetDescriptorEx(USBH_Info_t *phost, uint8_t reqType, uint8_t reqTarget, uint8_t type, uint8_t index, uint16_t wIndex, uint8_t *buff, int size)
58 {
59 phost->Ctrl.setup.bRequestType = USB_REQ_D2H | reqType | reqTarget;
60 phost->Ctrl.setup.bRequest = USB_GET_DESCRIPTOR;
61 phost->Ctrl.setup.wValue = (type << 8) | index;
62 phost->Ctrl.setup.wIndex = wIndex;
63 phost->Ctrl.setup.wLength = size;
64
65 return USBH_CtrlTransfer(phost, buff, size);
66 }
67
68
69 /******************************************************************************************************************************************
70 * 函数名称: USBH_SetAddress()
71 * 功能说明: 设置设备地址
72 * 输 入: 无
73 * 输 出: 无
74 * 注意事项: 无
75 ******************************************************************************************************************************************/
USBH_SetAddress(USBH_Info_t * phost,uint8_t addr)76 USBH_Status USBH_SetAddress(USBH_Info_t *phost, uint8_t addr)
77 {
78 phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
79 phost->Ctrl.setup.bRequest = USB_SET_ADDRESS;
80 phost->Ctrl.setup.wValue = addr;
81 phost->Ctrl.setup.wIndex = 0;
82 phost->Ctrl.setup.wLength = 0;
83
84 return USBH_CtrlTransfer(phost, 0, 0);
85 }
86
87
88 /******************************************************************************************************************************************
89 * 函数名称: USBH_SetConfiguration()
90 * 功能说明: 设置 configuration
91 * 输 入: 无
92 * 输 出: 无
93 * 注意事项: 无
94 ******************************************************************************************************************************************/
USBH_SetConfiguration(USBH_Info_t * phost,uint8_t cfg)95 USBH_Status USBH_SetConfiguration(USBH_Info_t *phost, uint8_t cfg)
96 {
97 phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
98 phost->Ctrl.setup.bRequest = USB_SET_CONFIGURATION;
99 phost->Ctrl.setup.wValue = cfg;
100 phost->Ctrl.setup.wIndex = 0;
101 phost->Ctrl.setup.wLength = 0;
102
103 return USBH_CtrlTransfer(phost, 0, 0);
104 }
105
106
107 /******************************************************************************************************************************************
108 * 函数名称: USBH_SetInterface()
109 * 功能说明: 设置 interface altSetting
110 * 输 入: 无
111 * 输 出: 无
112 * 注意事项: 无
113 ******************************************************************************************************************************************/
USBH_SetInterface(USBH_Info_t * phost,uint8_t intf,uint8_t altSetting)114 USBH_Status USBH_SetInterface(USBH_Info_t *phost, uint8_t intf, uint8_t altSetting)
115 {
116 phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_INTERFACE;
117 phost->Ctrl.setup.bRequest = USB_SET_INTERFACE;
118 phost->Ctrl.setup.wValue = altSetting;
119 phost->Ctrl.setup.wIndex = intf;
120 phost->Ctrl.setup.wLength = 0;
121
122 return USBH_CtrlTransfer(phost, 0, 0);
123 }
124
125
126 /******************************************************************************************************************************************
127 * 函数名称: USBH_ClrFeature()
128 * 功能说明: 清除端点 STALL
129 * 输 入: 无
130 * 输 出: 无
131 * 注意事项: 无
132 ******************************************************************************************************************************************/
USBH_ClrFeature(USBH_Info_t * phost,uint8_t ep)133 USBH_Status USBH_ClrFeature(USBH_Info_t *phost, uint8_t ep)
134 {
135 phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_ENDPOINT;
136 phost->Ctrl.setup.bRequest = USB_CLEAR_FEATURE;
137 phost->Ctrl.setup.wValue = 0x00;
138 phost->Ctrl.setup.wIndex = ep;
139 phost->Ctrl.setup.wLength = 0;
140
141 return USBH_CtrlTransfer(phost, 0, 0);
142 }
143
144 /******************************************************************************************************************************************
145 * 函数名称: USBH_ParseCfgDesc()
146 * 功能说明: 解析配置描述符
147 * 输 入: 无
148 * 输 出: 无
149 * 注意事项: 无
150 ******************************************************************************************************************************************/
USBH_ParseCfgDesc(USBH_Info_t * phost,uint8_t * buff,uint16_t size)151 USBH_Status USBH_ParseCfgDesc(USBH_Info_t *phost, uint8_t *buff, uint16_t size)
152 {
153 USB_DescHeader_t *pdesc = (USB_DescHeader_t *)buff;
154 uint8_t if_ix, ep_ix;
155
156 memcpy(&phost->Device.Cfg_Desc, pdesc, sizeof(USB_CfgDesc_t));
157
158 if(phost->Device.Cfg_Desc.bNumInterfaces > USBH_MAX_NUM_INTERFACES)
159 {
160 return USBH_NOT_SUPPORTED;
161 }
162
163 while((uint8_t *)pdesc < &buff[size])
164 {
165 pdesc = (USB_DescHeader_t *)((uint8_t *)pdesc + pdesc->bLength);
166 switch(pdesc->bDescriptorType)
167 {
168 case USB_DESC_INTERFACE:
169 if_ix = ((USB_IntfDesc_t *)pdesc)->bInterfaceNumber;
170 if(if_ix >= USBH_MAX_NUM_INTERFACES)
171 {
172 return USBH_NOT_SUPPORTED;
173 }
174
175 memcpy(&phost->Device.Intf_Desc[if_ix], pdesc, sizeof(USB_IntfDesc_t));
176
177 if(phost->Device.Intf_Desc[if_ix].bNumEndpoints > USBH_MAX_NUM_ENDPOINTS)
178 {
179 return USBH_NOT_SUPPORTED;
180 }
181 ep_ix = 0;
182 break;
183
184 case USB_DESC_ENDPOINT:
185 memcpy(&phost->Device.Ep_Desc[if_ix][ep_ix], pdesc, sizeof(USB_EpDesc_t));
186 if(++ep_ix >= USBH_MAX_NUM_ENDPOINTS)
187 {
188 return USBH_NOT_SUPPORTED;
189 }
190 break;
191
192 case USB_DESC_HID:
193 memcpy(&phost->Device.HID_Desc[if_ix], pdesc, sizeof(USB_HIDDesc_t));
194 break;
195
196 default:
197 break;
198 }
199 }
200
201 return USBH_OK ;
202 }
203