-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathUSB.cpp
126 lines (104 loc) · 2.78 KB
/
USB.cpp
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
/*
Copyright (c) 2015 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef USBCON
#include "usbd_desc.h"
#include "USB.h"
#include "wiring.h"
#ifdef USBD_USE_CDC_CLASS
#include "usbd_cdc.h"
#include "usbd_cdc_msc.h"
#include "usbd_cdc_if.h"
#endif
#ifdef USBD_USE_MSC_CLASS
#include "usbd_msc.h"
#include "usbd_msc_storage_if.h"
#endif
USB USBDevice;
void USB::begin()
{
if (!initialized) {
initialize();
}
}
#ifdef USBD_USE_MSC_CLASS
DummyUSBMscHandler dummyHandler;
void USB::registerMscHandler(USBMscHandler &handler)
{
pSingleMscHandler = &handler;
registerMscHandlers(1, &pSingleMscHandler, USBD_MSC_fops.pInquiry);
}
void USB::registerMscHandlers(uint8_t count, USBMscHandler **ppHandlers, uint8_t *pInquiryData)
{
if (count == 0) {
registerMscHandler(dummyHandler);
} else {
ppUsbMscHandlers = ppHandlers;
usbMscMaxLun = count - 1;
USBD_MSC_fops.pInquiry = pInquiryData;
}
}
#endif
void USB::initialize()
{
hUSBD_Device_CDC = &hUSBD_Device;
/* Init Device Library */
if (USBD_Init(&hUSBD_Device, &USBD_Desc, 0) != USBD_OK) {
return;
}
/* Add Supported Class and register interface */
#ifdef USBD_USE_CDC
if (USBD_RegisterClass(&hUSBD_Device, &USBD_CDC) != USBD_OK) {
return;
}
#elif USBD_USE_CDC_MSC
if (USBD_RegisterClass(&hUSBD_Device, &USBD_CDC_MSC) != USBD_OK) {
return;
}
#elif USBD_USE_MSC
if (USBD_RegisterClass(&hUSBD_Device, &USBD_CDC_MSC) != USBD_OK) {
return;
}
#endif
#ifdef USBD_USE_CDC_CLASS
hUSBD_Device_CDC = &hUSBD_Device;
if (USBD_CDC_RegisterInterface(&hUSBD_Device, &USBD_CDC_fops) != USBD_OK) {
return;
}
#endif
#ifdef USBD_USE_MSC_CLASS
if (ppUsbMscHandlers == nullptr) {
registerMscHandler(dummyHandler);
}
if (USBD_MSC_RegisterStorage(&hUSBD_Device, &USBD_MSC_fops) != USBD_OK) {
return;
}
#endif
/* Start Device Process */
USBD_Start(&hUSBD_Device);
initialized = true;
}
void USB::end()
{
if (initialized) {
deinitialize();
}
}
void USB::deinitialize()
{
USBD_Stop(&hUSBD_Device);
USBD_DeInit(&hUSBD_Device);
initialized = false;
}
#endif // USBCON