-
Notifications
You must be signed in to change notification settings - Fork 518
/
Copy pathusb_hal.cpp
293 lines (258 loc) · 9.12 KB
/
usb_hal.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (c) 2021 Particle Industries, Inc. All rights 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#undef LOG_COMPILE_TIME_LEVEL
#include "usb_hal.h"
#include "usbd_device.h"
#include "usbd_control.h"
#include "usbd_driver.h"
#include "usbd_cdc.h"
#include <mutex>
#include "usb_settings.h"
#include "usbd_hid.h"
using namespace particle::usbd;
CdcClassDriver& getCdcClassDriver() {
static CdcClassDriver cdc;
return cdc;
}
namespace {
// Avoid global object construction order issues
static Device& getUsbDevice() {
static Device dev;
return dev;
}
#define LOBYTE(x) ((uint8_t)(x & 0x00FF))
#define HIBYTE(x) ((uint8_t)((x & 0xFF00) >>8))
const uint8_t sDeviceDescriptor[] = {
0x12, /*bLength */
DESCRIPTOR_DEVICE, /*bDescriptorType*/
0x00, /*bcdUSB */
0x02,
0xef, /*bDeviceClass: Misc */
0x02, /*bDeviceSubClass*/
0x01, /*bDeviceProtocol*/
EP0_MAX_PACKET_SIZE, /*bMaxPacketSize*/
LOBYTE(USBD_VID_PARTICLE), /*idVendor*/
HIBYTE(USBD_VID_PARTICLE), /*idVendor*/
LOBYTE(USBD_PID_CDC), /*idProduct*/
HIBYTE(USBD_PID_CDC), /*idProduct*/
LOBYTE(0x0251), /*bcdDevice (2.51) */
HIBYTE(0x0251), /*bcdDevice (2.51) */
STRING_IDX_MANUFACTURER, /*Index of manufacturer string*/
STRING_IDX_PRODUCT, /*Index of product string*/
STRING_IDX_SERIAL, /*Index of serial number string*/
0x01 /*bNumConfigurations*/
};
} // anonymous
void HAL_USB_Init(void) {
static std::once_flag onceFlag;
std::call_once(onceFlag, []() {
getUsbDevice().setDeviceDescriptor(sDeviceDescriptor, sizeof(sDeviceDescriptor));
getUsbDevice().registerDriver(RtlUsbDriver::instance());
getUsbDevice().registerClass(ControlInterfaceClassDriver::instance());
getUsbDevice().registerClass(&getCdcClassDriver());
getUsbDevice().registerClass(HidClassDriver::instance());
ControlInterfaceClassDriver::instance()->enable(true);
// Only enabled if HAL_USB_USART_Init() was called to configure buffers
// getCdcClassDriver().enable(true);
HAL_USB_Attach();
});
}
void HAL_USB_Attach() {
getUsbDevice().attach();
}
void HAL_USB_Detach() {
getUsbDevice().detach();
}
void HAL_USB_Set_Vendor_Request_Callback(HAL_USB_Vendor_Request_Callback cb, void* p) {
ControlInterfaceClassDriver::instance()->setVendorRequestCallback(cb, p);
}
void HAL_USB_Set_Vendor_Request_State_Callback(HAL_USB_Vendor_Request_State_Callback cb, void* p) {
ControlInterfaceClassDriver::instance()->setVendorRequestStateCallback(cb, p);
}
void HAL_USB_USART_Init(HAL_USB_USART_Serial serial, const HAL_USB_USART_Config* config) {
if (serial != HAL_USB_USART_SERIAL) {
return;
}
if (getCdcClassDriver().isEnabled()) {
return;
}
// FIXME: figure out what's going on here
if (!config ||
(config->rx_buffer == nullptr ||
config->rx_buffer_size == 0 ||
config->tx_buffer == nullptr ||
config->tx_buffer_size == 0)) {
uint8_t* txBuffer = (uint8_t*)malloc(USB_TX_BUFFER_SIZE);
uint8_t* rxBuffer = (uint8_t*)malloc(USB_RX_BUFFER_SIZE);
if (txBuffer && rxBuffer) {
getCdcClassDriver().initBuffers(rxBuffer, USB_RX_BUFFER_SIZE, txBuffer, USB_TX_BUFFER_SIZE);
} else {
if (txBuffer) {
free(txBuffer);
}
if (rxBuffer) {
free(rxBuffer);
}
}
} else {
getCdcClassDriver().initBuffers(config->rx_buffer, config->rx_buffer_size, config->tx_buffer, config->tx_buffer_size);
}
}
void HAL_USB_USART_Begin(HAL_USB_USART_Serial serial, uint32_t baud, void *reserved) {
if (serial != HAL_USB_USART_SERIAL) {
return;
}
if (!getCdcClassDriver().isEnabled() && getCdcClassDriver().buffersConfigured()) {
HAL_USB_Detach();
getCdcClassDriver().enable(true);
HAL_USB_Init();
HAL_USB_Attach();
}
}
void HAL_USB_USART_End(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return;
}
if (getCdcClassDriver().isEnabled()) {
HAL_USB_Detach();
getCdcClassDriver().enable(false);
HAL_USB_Attach();
}
}
unsigned int HAL_USB_USART_Baud_Rate(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return 0;
}
cdc::LineCoding lineCoding = {};
if (getCdcClassDriver().getLineCoding(lineCoding)) {
return 0;
}
return lineCoding.dwDTERate;
}
int32_t HAL_USB_USART_Available_Data(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
return getCdcClassDriver().available();
}
int32_t HAL_USB_USART_Available_Data_protected(HAL_USB_USART_Serial serial) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Available_Data(serial);
}
int32_t HAL_USB_USART_Available_Data_For_Write(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
if (!HAL_USB_USART_Is_Connected(serial)) {
return -1;
}
return getCdcClassDriver().availableForWrite();
}
int32_t HAL_USB_USART_Available_Data_For_Write_protected(HAL_USB_USART_Serial serial) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Available_Data_For_Write(serial);
}
int32_t HAL_USB_USART_Receive_Data(HAL_USB_USART_Serial serial, uint8_t peek) {
if (serial != HAL_USB_USART_SERIAL) {
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
uint8_t c;
int r = 0;
if (!peek) {
r = getCdcClassDriver().read(&c, sizeof(c));
} else {
r = getCdcClassDriver().peek(&c, sizeof(c));
}
if (r == sizeof(c)) {
return c;
}
return r;
}
int32_t HAL_USB_USART_Receive_Data_protected(HAL_USB_USART_Serial serial, uint8_t peek) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Receive_Data(serial, peek);
}
int32_t HAL_USB_USART_Send_Data(HAL_USB_USART_Serial serial, uint8_t data) {
if (serial != HAL_USB_USART_SERIAL) {
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
// Just in case for now
if ((__get_PRIMASK() & 1)) {
return -1;
}
int32_t available = -1;
do {
available = HAL_USB_USART_Available_Data_For_Write(serial);
if (available > 0) {
break;
}
// TODO: check thread priorities here and delay only conditionally?
HAL_Delay_Milliseconds(1);
} while (available < 1 && available != -1);
if (HAL_USB_USART_Is_Connected(serial) && available > 0) {
return getCdcClassDriver().write(&data, sizeof(data));
}
return -1;
}
int32_t HAL_USB_USART_Send_Data_protected(HAL_USB_USART_Serial serial, uint8_t data) {
CHECK_SECURITY_MODE_PROTECTED();
return HAL_USB_USART_Send_Data(serial, data);
}
void HAL_USB_USART_Flush_Data(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return;
}
return getCdcClassDriver().flush();
}
void HAL_USB_USART_Flush_Data_protected(HAL_USB_USART_Serial serial) {
if (security_mode_get(NULL) == MODULE_INFO_SECURITY_MODE_PROTECTED) {
return;
}
return HAL_USB_USART_Flush_Data(serial);
}
bool HAL_USB_USART_Is_Enabled(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return false;
}
return getCdcClassDriver().isEnabled();
}
bool HAL_USB_USART_Is_Connected(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return false;
}
return getCdcClassDriver().isConnected();
}
void USB_USART_LineCoding_BitRate_Handler(void (*handler)(uint32_t bitRate)) {
// Old USB API, just for compatibility in main.cpp
// Enable Serial by default
HAL_USB_USART_LineCoding_BitRate_Handler(handler, NULL);
}
int32_t HAL_USB_USART_LineCoding_BitRate_Handler(void (*handler)(uint32_t bitRate), void* reserved) {
// Enable Serial by default
HAL_USB_USART_Init(HAL_USB_USART_SERIAL, nullptr);
HAL_USB_USART_Begin(HAL_USB_USART_SERIAL, 9600, nullptr);
getCdcClassDriver().onSetLineCoding([handler](cdc::LineCoding lineCoding) -> void {
handler(lineCoding.dwDTERate);
});
return 0;
}
HAL_USB_State HAL_USB_Get_State(void* reserved) {
return (HAL_USB_State)0;
}
int HAL_USB_Set_State_Change_Callback(HAL_USB_State_Callback cb, void* context, void* reserved) {
return 0;
}