GD32F1x0: USB/GD32_USB_Device_Library/Class/msc/src/usbd_msc_core.c Source File

GD32F1x0

usbd_msc_core.c
Go to the documentation of this file.
1 
11 /* Includes ------------------------------------------------------------------*/
12 #include "usbd_msc_core.h"
13 
14 /* Private function prototypes -----------------------------------------------*/
15 static uint8_t USBD_MSC_Init (void *pudev, uint8_t cfgidx);
16 static uint8_t USBD_MSC_DeInit (void *pudev, uint8_t cfgidx);
17 static uint8_t USBD_MSC_GetClassDescriptor (void *pudev, USB_DEVICE_REQ *req);
18 static uint8_t USBD_MSC_ClassReqHandle (void *pudev, USB_DEVICE_REQ *req);
19 static uint8_t USBD_MSC_GetInterface (void *pudev, USB_DEVICE_REQ *req);
20 static uint8_t USBD_MSC_SetInterface (void *pudev, USB_DEVICE_REQ *req);
21 static uint8_t USBD_MSC_DataIn (void *pudev, uint8_t EpID);
22 static uint8_t USBD_MSC_DataOut (void *pudev, uint8_t EpID);
23 static uint8_t *USBD_MSC_GetCfgDesc (uint8_t USBSpeed, uint16_t *len);
24 static uint8_t USBD_MSC_ClearFeature (void *pudev, USB_DEVICE_REQ *req);
25 
26 USBD_Class_cb_TypeDef USBD_MSC_cb =
27 {
28  USBD_MSC_Init,
29  USBD_MSC_DeInit,
30  USBD_MSC_GetClassDescriptor,
31  USBD_MSC_ClassReqHandle,
32  USBD_MSC_GetInterface,
33  USBD_MSC_SetInterface,
34  NULL, /* EP0_TxSent */
35  NULL, /* EP0_RxReady */
36  USBD_MSC_DataIn,
37  USBD_MSC_DataOut,
38  NULL, /* SOF */
39  USBD_MSC_GetCfgDesc,
40  USBD_MSC_ClearFeature
41 };
42 
43 /* USB Mass storage device configuration descriptor set */
44 const uint8_t USBD_MSC_CfgDesc[USB_MSC_CONFIG_DESC_SIZE] =
45 {
46  0x09, /* bLength: configuration descriptor size */
47  USB_DESCTYPE_CONFIGURATION, /* bDescriptorType: configuration descriptor type */
48  USB_MSC_CONFIG_DESC_SIZE, /* wTotalLength: configuration descriptor set total length */
49  0x00,
50  0x01, /* bNumInterfaces: 1 interface */
51  0x01, /* bConfigurationValue: configuration value */
52  0x04, /* iConfiguration: index of string descriptor describing the configuration */
53  0xC0, /* bmAttributes: device attributes (bus powered and not support remote wakeup) */
54  0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */
55 
56  /******************** interface descriptor ********************/
57  0x09, /* bLength: interface descriptor size */
58  USB_DESCTYPE_INTERFACE, /* bDescriptorType: interface descriptor type*/
59  0x00, /* bInterfaceNumber: number of interface */
60  0x00, /* bAlternateSetting: alternate setting */
61  0x02, /* bNumEndpoints: use 2 endpoints for Tx and Rx */
62  0x08, /* bInterfaceClass: MSC Class */
63  0x06, /* bInterfaceSubClass: SCSI transparent*/
64  0x50, /* nInterfaceProtocol: Bulk-only transport */
65  0x05, /* iInterface: index of interface string descriptor */
66 
67  /******************** Mass storage endpoint descriptor ********************/
68  0x07, /* bLength: Endpoint Descriptor size */
69  USB_DESCTYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
70  MSC_IN_EP, /* bEndpointAddress: endpoint address (EP1_IN) */
71  0x02, /* bmAttributes: endpoint attribute(bulk endpoint) */
72  LOWBYTE(MSC_DATA_PACKET_SIZE), /* wMaxPacketSize: 64 bytes max */
73  HIGHBYTE(MSC_DATA_PACKET_SIZE),
74  0x00, /* bInterval: polling interval is ignored */
75 
76  0x07, /* bLength: Endpoint Descriptor size */
77  USB_DESCTYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
78  MSC_OUT_EP, /*bEndpointAddress: endpoint address (EP2_OUT) */
79  0x02, /* bmAttributes: endpoint attribute(bulk endpoint) */
80  LOWBYTE(MSC_DATA_PACKET_SIZE), /* wMaxPacketSize: 64 bytes max */
81  HIGHBYTE(MSC_DATA_PACKET_SIZE),
82  0x00 /* bInterval: polling interval is ignored */
83 };
84 
85 static uint8_t USBD_MSC_MaxLun = 0;
86 static uint8_t USBD_MSC_AltSet = 0;
87 
88 /* Private function ----------------------------------------------------------*/
95 static uint8_t USBD_MSC_Init (void *pudev, uint8_t cfgidx)
96 {
97 #ifdef DOUBLE_BUFFER_TX
98  USB_EP_BufConfig(pudev, MSC_IN_EP, USB_DBL_BUFTYPE, MSC_TX_ADDRESS);
99 #else
100  USB_EP_BufConfig(pudev, MSC_IN_EP, USB_SNG_BUFTYPE, MSC_TX_ADDRESS);
101 #endif
102 
103 #ifdef DOUBLE_BUFFER_RX
104  USB_EP_BufConfig(pudev, MSC_OUT_EP, USB_DBL_BUFTYPE, MSC_RX_ADDRESS);
105 #else
106  USB_EP_BufConfig(pudev, MSC_OUT_EP, USB_SNG_BUFTYPE, MSC_RX_ADDRESS);
107 #endif
108 
109  /* Open EP IN */
110  USB_EP_Init(pudev,
111  MSC_IN_EP,
112  USB_EPTYPE_BULK,
113  MSC_EPIN_SIZE);
114 
115  /* Open EP OUT */
116  USB_EP_Init(pudev,
117  MSC_OUT_EP,
118  USB_EPTYPE_BULK,
119  MSC_EPOUT_SIZE);
120 
121  /* Init the BBB layer */
122  MSC_BBB_Init(pudev);
123 
124  return USBD_OK;
125 }
126 
133 static uint8_t USBD_MSC_DeInit (void *pudev, uint8_t cfgidx)
134 {
135  /* Close MSC EPs */
136  USB_EP_DeInit(pudev, MSC_IN_EP);
137  USB_EP_DeInit(pudev, MSC_OUT_EP);
138 
139  /* Un Init the BBB layer */
140  MSC_BBB_DeInit(pudev);
141 
142  return USBD_OK;
143 }
144 
151 static uint8_t USBD_MSC_ClassReqHandle (void *pudev, USB_DEVICE_REQ *req)
152 {
153  switch (req->bRequest)
154  {
155  case BBB_GET_MAX_LUN :
156  if((req->wValue == 0) &&
157  (req->wLength == 1) &&
158  ((req->bmRequestType & 0x80) == 0x80))
159  {
160  USBD_MSC_MaxLun = USBD_STORAGE_fops->GetMaxLun();
161  if(USBD_MSC_MaxLun > 0)
162  {
163  USB_CtlTx(pudev, &USBD_MSC_MaxLun, 1);
164  }
165  else
166  {
167  USBD_EnumError(pudev, req);
168  return USBD_FAIL;
169  }
170  }
171  else
172  {
173  USBD_EnumError(pudev, req);
174  return USBD_FAIL;
175  }
176  break;
177 
178  case BBB_RESET :
179  if((req->wValue == 0) &&
180  (req->wLength == 0) &&
181  ((req->bmRequestType & 0x80) != 0x80))
182  {
183  MSC_BBB_Reset(pudev);
184  }
185  else
186  {
187  USBD_EnumError(pudev, req);
188  return USBD_FAIL;
189  }
190  break;
191 
192  default:
193  USBD_EnumError(pudev, req);
194  return USBD_FAIL;
195  }
196 
197  return USBD_OK;
198 }
199 
206 static uint8_t USBD_MSC_GetClassDescriptor (void *pudev, USB_DEVICE_REQ *req)
207 {
208  return USBD_OK;
209 }
210 
217 static uint8_t USBD_MSC_GetInterface (void *pudev, USB_DEVICE_REQ *req)
218 {
219  USB_CtlTx (pudev, (uint8_t *)&USBD_MSC_AltSet, 1);
220 
221  return USBD_OK;
222 }
223 
230 static uint8_t USBD_MSC_SetInterface (void *pudev, USB_DEVICE_REQ *req)
231 {
232  USBD_MSC_AltSet = (uint8_t)(req->wValue);
233 
234  return USBD_OK;
235 }
236 
243 static uint8_t USBD_MSC_ClearFeature (void *pudev, USB_DEVICE_REQ *req)
244 {
245  MSC_BBB_CplClrFeature(pudev, (uint8_t)req->wIndex);
246 
247  return USBD_OK;
248 }
249 
256 static uint8_t USBD_MSC_DataIn (void *pudev, uint8_t EpID)
257 {
258  MSC_BBB_DataIn(pudev, EpID);
259 
260  return USBD_OK;
261 }
262 
269 static uint8_t USBD_MSC_DataOut (void *pudev, uint8_t EpID)
270 {
271  MSC_BBB_DataOut(pudev, EpID);
272 
273  return USBD_OK;
274 }
275 
282 static uint8_t *USBD_MSC_GetCfgDesc (uint8_t speed, uint16_t *len)
283 {
284  *len = sizeof (USBD_MSC_CfgDesc);
285 
286  return (uint8_t *)USBD_MSC_CfgDesc;
287 }
288 
289 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
USB device class callback type define.
Definition: usb_core.h:153
void MSC_BBB_DataOut(USB_DEVICE_HANDLE *pudev, uint8_t EpID)
Handle BBB OUT data stage.
Definition: usbd_msc_bot.c:108
Header file for the usbd_msc_core.c file.
void MSC_BBB_DataIn(USB_DEVICE_HANDLE *pudev, uint8_t EpID)
Handle BBB IN data stage.
Definition: usbd_msc_bot.c:85
void MSC_BBB_CplClrFeature(USB_DEVICE_HANDLE *pudev, uint8_t EpID)
Complete the clear feature request.
Definition: usbd_msc_bot.c:251
void USB_EP_DeInit(USB_CORE_HANDLE *pudev, uint8_t EpAddr)
Configure the endpoint when it is disabled.
Definition: usb_core.c:275
void USB_EP_Init(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t EpType, uint16_t EpMps)
Endpoint initialization.
Definition: usb_core.c:169
void MSC_BBB_Init(USB_DEVICE_HANDLE *pudev)
Initialize the BBB process.
Definition: usbd_msc_bot.c:39
uint8_t USB_CtlTx(USB_CORE_HANDLE *pudev, uint8_t *pbuf, uint16_t Len)
Transmit data on the control pipe.
Definition: usb_core.c:543
void MSC_BBB_DeInit(USB_DEVICE_HANDLE *pudev)
De-Initialize the BBB Machine.
Definition: usbd_msc_bot.c:74
void MSC_BBB_Reset(USB_DEVICE_HANDLE *pudev)
Reset the BBB machine.
Definition: usbd_msc_bot.c:58
#define USB_SNG_BUFTYPE
USB endpoint kind.
Definition: usb_core.h:52
void USB_EP_BufConfig(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t EpKind, uint32_t BufAddr)
Configure buffer for endpoint.
Definition: usb_core.c:107
void USBD_EnumError(USB_DEVICE_HANDLE *pudev, USB_DEVICE_REQ *req)
Handle usb enumeration error event.
Definition: usbd_enum.c:755
USB standard device request struct.
Definition: usb_core.h:122
Generated by   doxygen 1.8.10