summaryrefslogtreecommitdiff
path: root/bsp/synwit/swm341/libraries/SWM341_UsbHost_Lib/usbh_stdreq.c
blob: 08e7bc6cfe4c4eec0f70ab37d580c8bcc6422224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/******************************************************************************************************************************************
* 文件名称: usbh_stdreq.c
* 功能说明:
* 技术支持: http://www.synwit.com.cn/e/tool/gbook/?bid=1
* 注意事项:
* 版本日期: V1.1.0      2020年11月3日
* 升级记录:
*
*
*******************************************************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include <string.h>
#include "SWM341.h"
#include "usbh_core.h"
#include "usbh_stdreq.h"


/******************************************************************************************************************************************
* 函数名称: USBH_GetDescriptor()
* 功能说明: 获取描述符
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_Status USBH_GetDescriptor(USBH_Info_t *phost, uint8_t type, uint8_t index, uint8_t *buff, int size)
{
    phost->Ctrl.setup.bRequestType = USB_REQ_D2H | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
    phost->Ctrl.setup.bRequest = USB_GET_DESCRIPTOR;
    phost->Ctrl.setup.wValue = (type << 8) | index;
    phost->Ctrl.setup.wIndex = 0;
    phost->Ctrl.setup.wLength = size;

    return USBH_CtrlTransfer(phost, buff, size);
}


/******************************************************************************************************************************************
* 函数名称: USBH_GetDescriptorEx()
* 功能说明: 获取描述符
* 输    入: uint8_t reqType           USB_REQ_STANDARD、USB_REQ_CLASS、USB_REQ_VENDOR
*           uint8_t reqTarget       USB_REQ_TO_DEVICE、USB_REQ_TO_INTERFACE、USB_REQ_TO_ENDPOINT
*           uint8_t type
*           uint8_t index
*           uint16_t wIndex
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
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)
{
    phost->Ctrl.setup.bRequestType = USB_REQ_D2H | reqType | reqTarget;
    phost->Ctrl.setup.bRequest = USB_GET_DESCRIPTOR;
    phost->Ctrl.setup.wValue = (type << 8) | index;
    phost->Ctrl.setup.wIndex = wIndex;
    phost->Ctrl.setup.wLength = size;

    return USBH_CtrlTransfer(phost, buff, size);
}


/******************************************************************************************************************************************
* 函数名称: USBH_SetAddress()
* 功能说明: 设置设备地址
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_Status USBH_SetAddress(USBH_Info_t *phost, uint8_t addr)
{
    phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
    phost->Ctrl.setup.bRequest = USB_SET_ADDRESS;
    phost->Ctrl.setup.wValue = addr;
    phost->Ctrl.setup.wIndex = 0;
    phost->Ctrl.setup.wLength = 0;

    return USBH_CtrlTransfer(phost, 0, 0);
}


/******************************************************************************************************************************************
* 函数名称: USBH_SetConfiguration()
* 功能说明: 设置 configuration
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_Status USBH_SetConfiguration(USBH_Info_t *phost, uint8_t cfg)
{
    phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
    phost->Ctrl.setup.bRequest = USB_SET_CONFIGURATION;
    phost->Ctrl.setup.wValue = cfg;
    phost->Ctrl.setup.wIndex = 0;
    phost->Ctrl.setup.wLength = 0;

    return USBH_CtrlTransfer(phost, 0, 0);
}


/******************************************************************************************************************************************
* 函数名称: USBH_SetInterface()
* 功能说明: 设置 interface altSetting
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_Status USBH_SetInterface(USBH_Info_t *phost, uint8_t intf, uint8_t altSetting)
{
    phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
    phost->Ctrl.setup.bRequest = USB_SET_INTERFACE;
    phost->Ctrl.setup.wValue = altSetting;
    phost->Ctrl.setup.wIndex = intf;
    phost->Ctrl.setup.wLength = 0;

    return USBH_CtrlTransfer(phost, 0, 0);
}


/******************************************************************************************************************************************
* 函数名称: USBH_ClrFeature()
* 功能说明: 清除端点 STALL
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_Status USBH_ClrFeature(USBH_Info_t *phost, uint8_t ep)
{
    phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_ENDPOINT;
    phost->Ctrl.setup.bRequest = USB_CLEAR_FEATURE;
    phost->Ctrl.setup.wValue = 0x00;
    phost->Ctrl.setup.wIndex = ep;
    phost->Ctrl.setup.wLength = 0;

    return USBH_CtrlTransfer(phost, 0, 0);
}

/******************************************************************************************************************************************
* 函数名称: USBH_ParseCfgDesc()
* 功能说明: 解析配置描述符
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_Status USBH_ParseCfgDesc(USBH_Info_t *phost, uint8_t *buff, uint16_t size)
{
    USB_DescHeader_t *pdesc = (USB_DescHeader_t *)buff;
    uint8_t if_ix, ep_ix;

    memcpy(&phost->Device.Cfg_Desc, pdesc, sizeof(USB_CfgDesc_t));

    if(phost->Device.Cfg_Desc.bNumInterfaces > USBH_MAX_NUM_INTERFACES)
    {
        return USBH_NOT_SUPPORTED;
    }

    while((uint8_t *)pdesc < &buff[size])
    {
        pdesc = (USB_DescHeader_t *)((uint8_t *)pdesc + pdesc->bLength);
        switch(pdesc->bDescriptorType)
        {
        case USB_DESC_INTERFACE:
            if_ix = ((USB_IntfDesc_t *)pdesc)->bInterfaceNumber;
            if(if_ix >= USBH_MAX_NUM_INTERFACES)
            {
                return USBH_NOT_SUPPORTED;
            }

            memcpy(&phost->Device.Intf_Desc[if_ix], pdesc, sizeof(USB_IntfDesc_t));

            if(phost->Device.Intf_Desc[if_ix].bNumEndpoints > USBH_MAX_NUM_ENDPOINTS)
            {
                return USBH_NOT_SUPPORTED;
            }
            ep_ix = 0;
            break;

        case USB_DESC_ENDPOINT:
            memcpy(&phost->Device.Ep_Desc[if_ix][ep_ix], pdesc, sizeof(USB_EpDesc_t));
            if(++ep_ix >= USBH_MAX_NUM_ENDPOINTS)
            {
                return USBH_NOT_SUPPORTED;
            }
            break;

        case USB_DESC_HID:
            memcpy(&phost->Device.HID_Desc[if_ix], pdesc, sizeof(USB_HIDDesc_t));
            break;

        default:
            break;
        }
    }

    return USBH_OK ;
}