summaryrefslogtreecommitdiffstats
path: root/boot/usb.c
diff options
context:
space:
mode:
Diffstat (limited to 'boot/usb.c')
-rw-r--r--boot/usb.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/boot/usb.c b/boot/usb.c
new file mode 100644
index 0000000..e8d2e20
--- /dev/null
+++ b/boot/usb.c
@@ -0,0 +1,94 @@
+#include "project.h"
+
+#define USB_DM GPIO11
+#define USB_DM_PORT GPIOA
+#define USB_DP GPIO12
+#define USB_DP_PORT GPIOA
+
+uint8_t usbd_control_buffer[1024];
+
+const struct usb_device_descriptor dev = {
+ .bLength = USB_DT_DEVICE_SIZE,
+ .bDescriptorType = USB_DT_DEVICE,
+ .bcdUSB = 0x0200,
+ .bDeviceClass = 0,
+ .bDeviceSubClass = 0,
+ .bDeviceProtocol = 0,
+ .bMaxPacketSize0 = 64,
+ .idVendor = ID_VENDOR,
+ .idProduct = ID_PRODUCT,
+ .bcdDevice = 0x0200,
+ .iManufacturer = 1,
+ .iProduct = 2,
+ .iSerialNumber = 3,
+ .bNumConfigurations = 1,
+};
+
+
+const struct usb_interface ifaces[] = {{
+ .num_altsetting = 1,
+ .altsetting = &dfu_iface,
+ }
+};
+
+const struct usb_config_descriptor config = {
+ .bLength = USB_DT_CONFIGURATION_SIZE,
+ .bDescriptorType = USB_DT_CONFIGURATION,
+ .wTotalLength = 0,
+ .bNumInterfaces = 1,
+ .bConfigurationValue = 1,
+ .iConfiguration = 0,
+ .bmAttributes = 0xC0,
+ .bMaxPower = 0x32,
+
+ .interface = ifaces,
+};
+
+static const char *usb_strings[] = {
+ VENDOR_NAME,
+ PRODUCT_NAME " (dfu mode)",
+ SERIAL_NUMBER,
+ /* This string is used by ST Microelectronics' DfuSe utility. */
+ "@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg",
+};
+
+void
+usb_set_config (usbd_device *usbd_dev, uint16_t wValue)
+{
+ (void) wValue;
+ (void) usbd_dev;
+
+ usbd_register_control_callback (
+ usbd_dev,
+ USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
+ USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
+ usbdfu_control_request);
+
+}
+
+
+void usb (void)
+{
+ usbd_device *usbd_dev;
+
+
+ rcc_periph_clock_enable (RCC_GPIOA);
+ rcc_periph_clock_enable (RCC_OTGFS);
+
+ MAP_AF_100 (USB_DP, GPIO_AF10);
+ MAP_AF_100 (USB_DM, GPIO_AF10);
+
+
+ usbd_dev = usbd_init (&otgfs_usb_driver, &dev, &config, usb_strings, 4, usbd_control_buffer, sizeof (usbd_control_buffer));
+
+ OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS;
+ OTG_FS_GCCFG &= ~OTG_GCCFG_VBUSASEN;
+ OTG_FS_GCCFG &= ~OTG_GCCFG_VBUSBSEN;
+
+ usbd_register_set_config_callback (usbd_dev, usb_set_config);
+
+
+ while (1)
+ usbd_poll (usbd_dev);
+
+}