summaryrefslogtreecommitdiffstats
path: root/app/usb.c
blob: d1fb6256bdfc1e04eed979842943d33323302039 (plain)
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
#include "project.h"

#define USB_DM GPIO11
#define USB_DM_PORT GPIOA
#define USB_DP GPIO12
#define USB_DP_PORT GPIOA

/* Buffer to be used for control requests. */
uint8_t usbd_control_buffer[128];
usbd_device *usb_device;

static const struct usb_device_descriptor dev = {
  .bLength = USB_DT_DEVICE_SIZE,
  .bDescriptorType = USB_DT_DEVICE,
  .bcdUSB = 0x0200,
  .bDeviceClass = 0xef,
  .bDeviceSubClass = 0x02,
  .bDeviceProtocol = 0x01,
  .bMaxPacketSize0 = 64,
  .idVendor = 0x0483,
  .idProduct = 0xff03,
  .bcdDevice = 0x0200,
  .iManufacturer = 1,
  .iProduct = 2,
  .iSerialNumber = 3,
  .bNumConfigurations = 1,
};


static const struct usb_interface ifaces[] = {
  {
    .num_altsetting = 1,
    .altsetting = &dummy_kb_iface,
    .iface_assoc = &dummy_kb_iface_assoc,
  },
  {
    .num_altsetting = 1,
    .altsetting = &dfu_iface,
    .iface_assoc = &dfu_iface_assoc,
  },
  {
    .num_altsetting = 1,
    .altsetting = &comm_iface,
    .iface_assoc = &cdc_iface_assoc,
  },
  {
    .num_altsetting = 1,
    .altsetting = &data_iface,
  },
};

static const struct usb_config_descriptor config = {
  .bLength = USB_DT_CONFIGURATION_SIZE,
  .bDescriptorType = USB_DT_CONFIGURATION,
  .wTotalLength = 0,
  .bNumInterfaces = 4,
  .bConfigurationValue = 1,
  .iConfiguration = 0,
  .bmAttributes = 0x80,
  .bMaxPower = 0x32,
  .interface = ifaces,
};
#define N_USB_STRINGS  (sizeof(usb_strings)/sizeof(usb_strings[0]))

static const char *usb_strings[] = {
  VENDOR_NAME, /*1*/
  PRODUCT_NAME, /*2*/
  SERIAL_NUMBER, /*3*/
  "dummy device", /*4*/
  "Dfu iface", /*5*/
  "Clock iface", /*6*/
};

void otg_fs_isr (void)
{
  usbd_poll (usb_device);
}


static enum usbd_request_return_codes control_request (usbd_device *usbd_dev,
    struct usb_setup_data *req,
    uint8_t **buf,
    uint16_t *len,
    usbd_control_complete_callback *complete)
{
  enum usbd_request_return_codes ret;

  ret = dfu_control_request (usbd_dev, req, buf, len, complete);

  if (ret != USBD_REQ_NEXT_CALLBACK)
    return ret;

  ret = cdcacm_control_request (usbd_dev, req, buf, len, complete);

  if (ret != USBD_REQ_NEXT_CALLBACK)
    return ret;

  return USBD_REQ_NOTSUPP;
}

static void set_config (usbd_device *usbd_dev, uint16_t wValue)
{

  cdcacm_set_config (usbd_dev, wValue);

  usbd_register_control_callback (usbd_dev,
                                  USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
                                  USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
                                  control_request);

}



void  usb_init (void)
{
  MAP_AF_100 (USB_DP, GPIO_AF10);
  MAP_AF_100 (USB_DM, GPIO_AF10);


  usb_device = usbd_init (&otgfs_usb_driver,
                          &dev,
                          &config,
                          usb_strings,
                          N_USB_STRINGS,
                          usbd_control_buffer,
                          sizeof (usbd_control_buffer));

  /* Disable VBUS sensing */

  OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS;
  OTG_FS_GCCFG &= ~OTG_GCCFG_VBUSASEN;
  OTG_FS_GCCFG &= ~OTG_GCCFG_VBUSBSEN;

  usbd_register_set_config_callback (usb_device, set_config);


  nvic_enable_irq (NVIC_OTG_FS_IRQ);

}