forked from arduino/ArduinoCore-samd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUSBAPI.h
197 lines (164 loc) · 5.61 KB
/
USBAPI.h
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
/*
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
*/
#pragma once
#define HSTPIPCFG_PTYPE_BLK 1
#define HSTPIPCFG_PTOKEN_IN 2
#define HSTPIPCFG_PTOKEN_OUT 3
#define HSTPIPCFG_PBK_1_BANK 4
#define HSTPIPCFG_PTYPE_INTRPT 5
#define EP0 0
#define EPX_SIZE 64 // 64 for Full Speed, EPT size max is 1024
#if defined __cplusplus
#include "Stream.h"
#include "RingBuffer.h"
//================================================================================
// USB
// Low level API
typedef struct {
union {
uint8_t bmRequestType;
struct {
uint8_t direction : 5;
uint8_t type : 2;
uint8_t transferDirection : 1;
};
};
uint8_t bRequest;
uint8_t wValueL;
uint8_t wValueH;
uint16_t wIndex;
uint16_t wLength;
} USBSetup;
class USBDeviceClass {
public:
USBDeviceClass() {};
// USB Device API
void init();
bool attach();
bool detach();
void setAddress(uint32_t addr);
bool configured();
bool connected();
// Setup API
bool handleClassInterfaceSetup(USBSetup &setup);
bool handleStandardSetup(USBSetup &setup);
bool sendDescriptor(USBSetup &setup);
// Control EndPoint API
uint32_t sendControl(const void *data, uint32_t len);
uint32_t sendControl(int /* ep */, const void *data, uint32_t len) { return sendControl(data, len); }
uint32_t recvControl(void *data, uint32_t len);
uint32_t sendConfiguration(uint32_t maxlen);
bool sendStringDescriptor(const uint8_t *string, uint8_t maxlen);
void initControl(int end);
uint8_t SendInterfaces(uint32_t* total);
void packMessages(bool val);
// Generic EndPoint API
void initEndpoints(void);
void initEP(uint32_t ep, uint32_t type);
void handleEndpoint(uint8_t ep);
uint32_t send(uint32_t ep, const void *data, uint32_t len);
void sendZlp(uint32_t ep);
uint32_t recv(uint32_t ep, void *data, uint32_t len);
uint32_t recv(uint32_t ep);
uint32_t available(uint32_t ep);
void flush(uint32_t ep);
void stall(uint32_t ep);
// private?
uint32_t armSend(uint32_t ep, const void *data, uint32_t len);
uint8_t armRecv(uint32_t ep);
uint8_t armRecvCtrlOUT(uint32_t ep);
void ISRHandler();
private:
bool initialized;
};
extern USBDeviceClass USBDevice;
//================================================================================
// Serial over CDC (Serial1 is the physical port)
class Serial_ : public Stream
{
public:
Serial_(USBDeviceClass &_usb) : usb(_usb) { }
void begin(uint32_t baud_count);
void begin(unsigned long, uint8_t);
void end(void);
virtual int available(void);
virtual void accept(void);
virtual int peek(void);
virtual int read(void);
virtual void flush(void);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buffer, size_t size);
using Print::write; // pull in write(str) from Print
operator bool();
// This method allows processing "SEND_BREAK" requests sent by
// the USB host. Those requests indicate that the host wants to
// send a BREAK signal and are accompanied by a single uint16_t
// value, specifying the duration of the break. The value 0
// means to end any current break, while the value 0xffff means
// to start an indefinite break.
// readBreak() will return the value of the most recent break
// request, but will return it at most once, returning -1 when
// readBreak() is called again (until another break request is
// received, which is again returned once).
// This also mean that if two break requests are received
// without readBreak() being called in between, the value of the
// first request is lost.
// Note that the value returned is a long, so it can return
// 0-0xffff as well as -1.
int32_t readBreak();
// These return the settings specified by the USB host for the
// serial port. These aren't really used, but are offered here
// in case a sketch wants to act on these settings.
uint32_t baud();
uint8_t stopbits();
uint8_t paritytype();
uint8_t numbits();
bool dtr();
bool rts();
enum {
ONE_STOP_BIT = 0,
ONE_AND_HALF_STOP_BIT = 1,
TWO_STOP_BITS = 2,
};
enum {
NO_PARITY = 0,
ODD_PARITY = 1,
EVEN_PARITY = 2,
MARK_PARITY = 3,
SPACE_PARITY = 4,
};
private:
int availableForStore(void);
USBDeviceClass &usb;
RingBuffer *_cdc_rx_buffer;
};
extern Serial_ SerialUSB;
//================================================================================
//================================================================================
// MSC 'Driver'
uint32_t MSC_GetInterface(uint8_t* interfaceNum);
uint32_t MSC_GetDescriptor(uint32_t i);
bool MSC_Setup(USBSetup& setup);
bool MSC_Data(uint8_t rx,uint8_t tx);
//================================================================================
//================================================================================
// CDC 'Driver'
int CDC_GetInterface(uint8_t* interfaceNum);
const void* _CDC_GetInterface(void);
uint32_t _CDC_GetInterfaceLength(void);
uint32_t CDC_GetOtherInterface(uint8_t* interfaceNum);
uint32_t CDC_GetDescriptor(uint32_t i);
bool CDC_Setup(USBSetup& setup);
#endif // __cplusplus