This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbusb.c
377 lines (319 loc) · 9.51 KB
/
pbusb.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdlib.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/f1/nvic.h>
#include <libopencm3/usb/dfu.h>
#include "usb.h"
#include "output.h"
#include "led.h"
#include "battery.h"
#include "button.h"
#include "piezo.h"
#include "dfu-bootloader/usbdfu.h"
#define delay(x) do { for (int i = 0; i < x * 1000; i++) \
__asm__("nop"); \
} while(0);
bool re_enter_bootloader = false;
static usbd_device *usbd_dev;
static const struct usb_device_descriptor usb_descr = {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = 0,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = 64,
.idVendor = SR_DEV_VID,
.idProduct = SR_DEV_PID,
.bcdDevice = SR_DEV_REV,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.bNumConfigurations = 1,
};
const struct usb_dfu_descriptor sr_dfu_function = {
.bLength = sizeof(struct usb_dfu_descriptor),
.bDescriptorType = DFU_FUNCTIONAL,
.bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
.wDetachTimeout = 255,
.wTransferSize = 128,
.bcdDFUVersion = 0x011A,
};
const struct usb_interface_descriptor dfu_iface = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 0,
.bInterfaceClass = 0xFE, // Application specific class code
.bInterfaceSubClass = 0x01, // DFU
.bInterfaceProtocol = 0x01, // Protocol 1.0
.iInterface = 4,
.extra = &sr_dfu_function,
.extralen = sizeof(sr_dfu_function),
};
const struct usb_interface usb_ifaces[] = {{
.num_altsetting = 1,
.altsetting = &dfu_iface,
}};
static const struct usb_config_descriptor usb_config = {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0,
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0xC0, // Bit 6 -> self powered
.bMaxPower = 5, // Will consume 10ma from USB (a guess)
.interface = usb_ifaces,
};
static const char *usb_strings[] = {
"Student Robotics",
"Power board v4",
(const char *)SERIALNUM_BOOTLOADER_LOC,
"Student Robotics Power board v4", // Iface 1
"Student Robotics Power board DFU loader", // IFace 2, DFU
};
static uint8_t usb_data_buffer[128];
static int
read_output(int *len, uint8_t **buf, int output)
{
if (*len < 4)
return USBD_REQ_NOTSUPP;
uint32_t *u32ptr = (uint32_t*)*buf;
*u32ptr = current_sense_read(output);
*len = 4;
return USBD_REQ_HANDLED;
}
static int
handle_read_req(struct usb_setup_data *req, int *len, uint8_t **buf)
{
int result = USBD_REQ_NOTSUPP; // Will result in a USB stall
uint16_t *u16ptr;
uint32_t *u32ptr;
// Precise command, as enumerated in usb.h, is in wIndex
switch (req->wIndex) {
case POWERBOARD_READ_OUTPUT0:
result = read_output(len, buf, 0); break;
case POWERBOARD_READ_OUTPUT1:
result = read_output(len, buf, 1); break;
case POWERBOARD_READ_OUTPUT2:
result = read_output(len, buf, 2); break;
case POWERBOARD_READ_OUTPUT3:
result = read_output(len, buf, 3); break;
case POWERBOARD_READ_OUTPUT4:
result = read_output(len, buf, 4); break;
case POWERBOARD_READ_OUTPUT5:
result = read_output(len, buf, 5); break;
case POWERBOARD_READ_5VRAIL:
if (*len < 4)
break;
*len = 4;
// Clocking i2c can take a lot of time!
u16ptr = (uint16_t*) *buf;
#if 0
// XXX jmorse
*u16ptr++ = f_vshunt();
*u16ptr++ = f_vbus();
#endif
result = USBD_REQ_HANDLED;
break;
case POWERBOARD_READ_BATT:
if (*len < 8)
break;
*len = 8;
u32ptr = (uint32_t*) *buf;
*u32ptr++ = read_battery_current();
*u32ptr++ = read_battery_voltage();
result = USBD_REQ_HANDLED;
break;
case POWERBOARD_READ_BUTTON:
if (*len < 4)
break;
*len = 4;
u32ptr = (uint32_t*)*buf;
*u32ptr++ = button_pressed();
result = USBD_REQ_HANDLED;
break;
case POWERBOARD_READ_FWVER:
if (*len < 4)
break;
*len = 4;
u32ptr = (uint32_t*)*buf;
*u32ptr++ = FW_VER;
result = USBD_REQ_HANDLED;
break;
default:
break;
}
return result;
}
static void
write_output(int id, uint16_t param)
{
if (param == 0) {
// Set output off
output_off(id);
} else {
output_on(id);
}
}
static void
write_led(int id, uint16_t param)
{
if (param == 0) {
// Set led off
led_clear(id);
} else {
led_set(id);
}
}
static int
handle_write_req(struct usb_setup_data *req)
{
switch (req->wIndex) {
case POWERBOARD_WRITE_OUTPUT0:
write_output(0, req->wValue); break;
case POWERBOARD_WRITE_OUTPUT1:
write_output(1, req->wValue); break;
case POWERBOARD_WRITE_OUTPUT2:
write_output(2, req->wValue); break;
case POWERBOARD_WRITE_OUTPUT3:
write_output(3, req->wValue); break;
case POWERBOARD_WRITE_OUTPUT4:
write_output(4, req->wValue); break;
case POWERBOARD_WRITE_OUTPUT5:
write_output(5, req->wValue); break;
case POWERBOARD_WRITE_RUNLED:
write_led(LED_RUN, req->wValue); break;
case POWERBOARD_WRITE_ERRORLED:
write_led(LED_ERROR, req->wValue); break;
case POWERBOARD_WRITE_PIEZO:
if (!piezo_recv(req->wLength, usb_data_buffer))
break;
default:
return USBD_REQ_NOTSUPP; // Will result in a USB stall
}
return USBD_REQ_HANDLED;
}
static int
control(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf,
uint16_t *len,
void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
{
// Only respond to a single device request, number 64. USB spec 3.1
// Section 9.3.1 allows us to define additional requests, and Table 9.5
// identifies all reserved requests. So, pick 64, it could be any.
if (req->bRequest != 64)
return USBD_REQ_NEXT_CALLBACK;
// Data and length are in *buf and *len respectively. Output occurs by
// modifying what those point at.
if (req->bmRequestType & USB_REQ_TYPE_IN) { // i.e., input to host
return handle_read_req(req, len, buf);
} else {
return handle_write_req(req);
}
}
static int
iface_control(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf,
uint16_t *len,
void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
{
// For standard requests, handle only set_iface, with no alternative
// ifaces.
if (req->bmRequestType == (USB_REQ_TYPE_STANDARD|USB_REQ_TYPE_INTERFACE)
&& req->bRequest == USB_REQ_SET_INTERFACE
&& req->wValue == 0) {
// Two ifaces: this one and DFU.
if (req->wIndex == 0) {
// Do a special dance; but later.
return USBD_REQ_HANDLED;
}
}
// Otherwise, we might be getting DFU requests
if ((req->bmRequestType & (USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT))
== (USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE))
{
switch (req->bRequest) {
case DFU_GETSTATUS:
*len = 6;
(*buf)[0] = STATE_APP_IDLE;
(*buf)[1] = 100; // ms
(*buf)[2] = 0;
(*buf)[3] = 0;
(*buf)[4] = STATE_APP_IDLE;
(*buf)[5] = 0;
return USBD_REQ_HANDLED;
case DFU_DETACH:
re_enter_bootloader = true;
return USBD_REQ_HANDLED;
}
}
return USBD_REQ_NOTSUPP;
}
static void
set_config_cb(usbd_device *usbd_dev, uint16_t wValue)
{
// We want to handle device requests sent to the default endpoint: match the
// device request type (0), with zero recpient address. Use type + recipient
// mask to filter requests.
usbd_register_control_callback(usbd_dev, USB_REQ_TYPE_DEVICE,
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
control);
// Additionally, register our own SR-interface callback. This is simply to
// handle SET_INTERFACE, which libopencm3 doesn't do. Filter options are to
// match standard request, to the interface recipient.
usbd_register_control_callback(usbd_dev,
USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE,
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
iface_control);
// Use the same function to catch initial DFU requests. These are class
// commands.
usbd_register_control_callback(usbd_dev,
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
iface_control);
// Indicate (on boot) that we've been enumerated -- the LED will have been
// RED/GREEN, it will now be solid green.
led_clear(LED_ERROR);
}
extern void usb_reset_callback(void);
void
usb_init()
{
gpio_clear(GPIOA, GPIO8);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
usbd_dev = usbd_init(&stm32f103_usb_driver, &usb_descr, &usb_config,
usb_strings, 3, usb_data_buffer, sizeof(usb_data_buffer));
usbd_register_set_config_callback(usbd_dev, set_config_cb);
usbd_register_reset_callback(usbd_dev, usb_reset_callback);
gpio_set(GPIOA, GPIO8);
// Enable low priority general purpose USB intr
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
// Set USB to be low priority: it will still execute in interrupt context
// and block the main thread, however all the other interrupts (clock,
// analogue, etc) will interrupt on top of this.
nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, 16);
}
void
usb_deinit()
{
// Gate USB; this will cause a reset for us and the host.
gpio_clear(GPIOA, GPIO8);
// Disable intr
nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
// Do nothing for a few ms, then poll a few times to ensure that the driver
// has reset itself
delay(20);
usbd_poll(usbd_dev);
usbd_poll(usbd_dev);
usbd_poll(usbd_dev);
usbd_poll(usbd_dev);
// That should be enough
return;
}
void
usb_lp_can_rx0_isr()
{
usbd_poll(usbd_dev);
}