~ruther/stm32h747i-disco-usb-image-viewer

ref: d5bfe1a35cc5b046782ddc4222f3344d9127f76e stm32h747i-disco-usb-image-viewer/src/usb_device_cdc.c -rw-r--r-- 13.6 KiB
d5bfe1a3 — Rutherther fix: usb should not rewrite DIEPCTL bits 4 months ago
                                                                                
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
#include "usb.h"
#include "usb_device.h"
#include "usb_device_cdc.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

uint16_t usb_cdc_lang_descriptors[] = {
  USB_LANG_ENGLISH | (USB_SUBLANG_ENGLISH_US << 10)
};

usb_class_vtable_t USB_CLASS_CDC_ACM = {
  .init = usb_device_cdc_init,
  .send_configuration = usb_device_cdc_send_configuration,
  .setup_packet_callback = usb_device_cdc_setup_packet_callback,
  .enumeration_done_callback = usb_device_cdc_enumeration_done_callback,
  .txfifo_empty_callback = usb_device_cdc_txfifo_empty_callback,
  .rxfifo_empty_callback = usb_device_cdc_rxfifo_empty_callback,
  .transmit_done_callback = usb_device_cdc_transmit_done_callback,
  .nak_callback = usb_device_cdc_nak_callback,
  .nyet_callback = usb_device_cdc_nyet_callback,
};

usb_class_header_t *usb_device_cdc_init(usb_device_t *device, uint16_t id_vendor,
                          uint16_t id_product, char *vendor_name,
                          char *product_name, uint16_t serial_number,
                          char *serial_name) {
  usb_class_header_t* class = (usb_class_header_t*)calloc(1, sizeof(usb_device_cdc_t));

  class->device_descriptor.idProduct = id_product;
  class->device_descriptor.idVendor = id_vendor;
  class->device_descriptor.bcdDevice = serial_number;

  class->string_descriptor_zero.header.bDescriptorType = DESCRIPTOR_STRING;
  class->string_descriptor_zero.header.bLength = sizeof(usb_string_descriptor_zero_t);
  class->string_descriptor_zero.wLANGID = usb_cdc_lang_descriptors;

  uint8_t string_count = 0;

  if (vendor_name != NULL) {
    string_count++;
    class->device_descriptor.iManufacturer = string_count;
  }
  if (product_name != NULL) {
    string_count++;
    class->device_descriptor.iProduct = string_count;
  }
  if (serial_name != NULL) {
    string_count++;
    class->device_descriptor.iSerialNumber = string_count;
  }

  usb_unicode_string_descriptor_t *descriptor =
    (usb_unicode_string_descriptor_t*)malloc(sizeof(usb_unicode_string_descriptor_t) * string_count);
  class->string_descriptors = descriptor;

  if (vendor_name != NULL) {
    descriptor->header.bDescriptorType = DESCRIPTOR_STRING;
    descriptor->header.bLength = 4 + strlen(vendor_name);
    descriptor->bString = (uint8_t*)vendor_name;

    descriptor++;
  }
  if (product_name != NULL) {
    descriptor->header.bDescriptorType = DESCRIPTOR_STRING;
    descriptor->header.bLength = 4 + strlen(product_name);
    descriptor->bString = (uint8_t*)product_name;
    descriptor++;
  }
  if (serial_name != NULL) {
    descriptor->header.bDescriptorType = DESCRIPTOR_STRING;
    descriptor->header.bLength = 4 + strlen(serial_name);
    descriptor->bString = (uint8_t*)serial_name;
    descriptor++;
  }

  return class;
}

void usb_device_cdc_acm_configure(usb_device_t* device) {
  usb_device_cdc_t* cdc = (usb_device_cdc_t*)device->class;
  usb_class_header_t* header = &cdc->header;

  usb_device_descriptor_t device_descriptor =
    {
      .header = { .bDescriptorType = DESCRIPTOR_DEVICE, .bLength = sizeof(usb_device_descriptor_t) },
      .bcdUSB = 0x20,
      .bDeviceClass = USB_CLASS_CDC_CODE,
      .bDeviceSubClass = 0x00,
      .bDeviceProtocol = 0x00,
      .bMaxPacketSize0 = 64,
      .idVendor = header->device_descriptor.idVendor,
      .idProduct = header->device_descriptor.idProduct,
      .bcdDevice = 0x0000,
      .iManufacturer = header->device_descriptor.iManufacturer,
      .iProduct = header->device_descriptor.iProduct,
      .iSerialNumber = header->device_descriptor.iSerialNumber,
      .bNumConfigurations = 1,
    };
  header->device_descriptor = device_descriptor;

  usb_device_qualifier_t qualifier =
    {
      .header = { .bDescriptorType = DESCRIPTOR_DEVICE_QUALIFIER, .bLength = sizeof(usb_device_qualifier_t) },
      .bcdUSB = 0x20,
      .bDeviceClass = USB_CLASS_CDC_CODE,
      .bDeviceSubClass = 0x00,
      .bDeviceProtocol = 0x00,
      .bMaxPacketSize0 = 64,
      .bNumConfigurations = 0,
      .bReserved = 0
    };
  header->device_qualifier = qualifier;

  usb_configuration_descriptor_t configuration_descriptor =
    {
      .header = { .bDescriptorType = DESCRIPTOR_CONFIGURATION, .bLength = sizeof(usb_configuration_descriptor_t) },
      .bNumInterfaces = 2,
      .bConfigurationValue = 1,
      .iConfiguration = 0,
      .bmAttributes =
      {
        .self_powered = 0,
        .remote_wakeup = 0,
        .reserved1 = 1,
        .reserved_zeros = 0,
      },
      .bMaxPower = 50,
    };
  header->configuration_descriptor = configuration_descriptor;

  header->interfaces_count = 2;
  header->interfaces = malloc(2 * sizeof(usb_interface_t));

  static usb_endpoint_descriptor_t notification_endpoint =
  {.header = {.bDescriptorType = DESCRIPTOR_ENDPOINT,
              .bLength = sizeof(usb_endpoint_descriptor_t)},
   .bEndpointAddress = { .endpoint_number = 1, .direction = USB_ENDPOINT_IN, .reserved = 0 },
   .bInterval = 16,
   .bmAttributes = {
       .reserved_zeros = 0,
       .synchronization_type = USB_ENDPOINT_SYNC_NO_SYNC,
       .usage_type = USB_ENDPOINT_USAGE_DATA,
       .transfer_type = USB_ENDPOINT_TYPE_INTERRUPT,
   },
    .wMaxPacketSize = 64,
  };
  usb_interface_t communications_interface =
  {.interface_descriptor = {
       .header = {.bDescriptorType = DESCRIPTOR_INTERFACE,
                  .bLength = sizeof(usb_interface_descriptor_t)},
       .bInterfaceNumber = 0,
       .bAlternateSetting = 0,
       .bNumEndpoints = 1,
       .bInterfaceClass = USB_CLASS_CDC_CODE,
       .bInterfaceSubClass = USB_SUBCLASS_CDC_ACM_CODE,
       .bInterfaceProtocol = 0,
       .iInterface = 0
      },
    .endpoint_descriptors_count = 1,
    // NOTE: mind here, the endpoint is same for all devices,
    // so it's defined here as static variable, meaning it will
    // not be deallocated when this function exits.
    .endpoint_descriptors = &notification_endpoint,
  };
  static usb_endpoint_descriptor_t data_endpoints[2] = {
    {.header = {.bDescriptorType = DESCRIPTOR_ENDPOINT,
                .bLength = sizeof(usb_endpoint_descriptor_t)},
     .bEndpointAddress = { .endpoint_number = 2, .direction = USB_ENDPOINT_IN, .reserved = 0 },
     .bInterval = 1,
     .bmAttributes = {
       .reserved_zeros = 0,
       .synchronization_type = USB_ENDPOINT_SYNC_NO_SYNC,
       .usage_type = USB_ENDPOINT_USAGE_DATA,
       .transfer_type = USB_ENDPOINT_TYPE_BULK,
     },
     .wMaxPacketSize = 64,
    },
    {.header = {.bDescriptorType = DESCRIPTOR_ENDPOINT,
                .bLength = sizeof(usb_endpoint_descriptor_t)},
     .bEndpointAddress = { .endpoint_number = 1, .direction = USB_ENDPOINT_OUT, .reserved = 0 },
     .bInterval = 16,
     .bmAttributes = {
       .reserved_zeros = 0,
       .synchronization_type = USB_ENDPOINT_SYNC_NO_SYNC,
       .usage_type = USB_ENDPOINT_USAGE_DATA,
       .transfer_type = USB_ENDPOINT_TYPE_BULK,
     },
     .wMaxPacketSize = 64,
    },
  };
  usb_interface_t data_interface = {
    .interface_descriptor = {
      .header = {.bDescriptorType = DESCRIPTOR_INTERFACE,
                 .bLength = sizeof(usb_interface_descriptor_t)},
      .bInterfaceNumber = 2,
      .bAlternateSetting = 0,
      .bNumEndpoints = 1,
      .bInterfaceClass = USB_CLASS_DATA_CODE,
      .bInterfaceSubClass = 0x00,
      .bInterfaceProtocol = 0,
      .iInterface = 0
    },
    .endpoint_descriptors_count = 2,
    // NOTE: mind here, the endpoint is same for all devices,
    // so it's defined here as static variable, meaning it will
    // not be deallocated when this function exits.
    .endpoint_descriptors = data_endpoints,
  };

  cdc->functional_descriptors_count = 4;

  static usb_cdc_header_functional_decriptor_t header_function = {
    .header = { .bFunctionLength = sizeof(usb_cdc_header_functional_decriptor_t), .bDescriptorType = CS_INTERFACE, .bDescriptorSubType = HEADER_FUNCTIONAL_DESCRIPTOR_FUNCTIONAL_DESCRIPTOR },
    .bcdCDC = 0x0110,
  };
  static usb_cdc_acm_functional_decriptor_t acm_function = {
    .header = { .bFunctionLength = sizeof(usb_cdc_acm_functional_decriptor_t), .bDescriptorType = CS_INTERFACE, .bDescriptorSubType = ABSTRACT_CONTROL_MANAGEMENT_FUNCTIONAL_DESCRIPTOR },
    .bmCapabilities = 0x00,
  };
  static usb_cdc_union_functional_decriptor_t union_function = {
    .header = { .bFunctionLength = sizeof(usb_cdc_union_functional_decriptor_t), .bDescriptorType = CS_INTERFACE, .bDescriptorSubType = UNION_FUNCTIONAL_DESCRIPTOR },
    .bControlInterface = 0,
    .bSubordinateInterface0 = 1,
  };
  static usb_cdc_call_management_functional_decriptor_t call_function = {
    .header = { .bFunctionLength = sizeof(usb_cdc_call_management_functional_decriptor_t), .bDescriptorType = CS_INTERFACE, .bDescriptorSubType = CALL_MANAGEMENT_FUNCTIONAL_FUNCTIONAL_DESCRIPTOR },
    .bmCapabilities = 0x00
  };
  static usb_cdc_functional_descriptor_header_t *headers[4] =
    { &header_function.header, &acm_function.header, &union_function.header, &call_function.header };
  cdc->functional_descriptors = headers;

  header->interfaces[0] = communications_interface;
  header->interfaces[1] = data_interface;
}

uint16_t get_size(uint16_t size_to_send, uint16_t* remaining_size) {
  uint16_t size = size_to_send;

  if (*remaining_size < size_to_send) {
    size = *remaining_size;
    *remaining_size = 0;
  } else {
    *remaining_size -= size_to_send;
  }

  return size;
}

task_result_t usb_device_cdc_send_configuration(usb_device_t *device,
                                                usb_setup_command_t *cmd) {
  usb_device_cdc_t* dev = (usb_device_cdc_t*)device->class;
  USB_OTG_INEndpointTypeDef* enp0 = &device->in[0];
  volatile uint32_t* enp0fifo = &device->fifos[0].data[0];
  uint32_t sub_word_data;
  uint8_t  sub_word_count = 0;

  // first configure the size
  uint16_t size =
      sizeof(usb_configuration_descriptor_t) +
    dev->header.interfaces_count * sizeof(usb_interface_descriptor_t);

  for (uint8_t i = 0; i < dev->header.interfaces_count; i++) {
    usb_interface_t* interface = &dev->header.interfaces[i];
    size += interface->endpoint_descriptors_count * sizeof(usb_endpoint_descriptor_t);
  }

  for (uint8_t i = 0; i < dev->functional_descriptors_count; i++) {
    usb_cdc_functional_descriptor_header_t* descriptor = dev->functional_descriptors[i];
    size += descriptor->bFunctionLength;
  }

  dev->header.configuration_descriptor.wTotalLength = size;

  if (size > cmd->wLength) {
    size = cmd->wLength;
  }

  // TODO: what if there is not enough space for this?
  // I mean there should be... but that case should probably be handled to,
  // it depends a lot on how many functions and interfaces we do have...
  task_result_t result = usb_generic_setup_in_endpoint(enp0, size, 64);

  if (result != RES_OK) {
    return result;
  }

  // fill fifo with all configuration
  usb_generic_fill_fifo_words(enp0,
                              (uint8_t*)&dev->header.configuration_descriptor,
                              get_size(sizeof(usb_configuration_descriptor_t), &size),
                              enp0fifo, &sub_word_data, &sub_word_count);

  // NOTE: there is always one control interface and one data one.
  for (uint8_t i = 0; i < dev->header.interfaces_count; i++) {
    usb_interface_t* interface = &dev->header.interfaces[i];
    usb_generic_fill_fifo_words(enp0,
                                (uint8_t*)&interface->interface_descriptor,
                                get_size(sizeof(usb_interface_descriptor_t), &size),
                                enp0fifo, &sub_word_data, &sub_word_count);

    // Control interface has functional descriptors
    if (i == 0) {
      for (uint8_t j = 0; j < dev->functional_descriptors_count; j++) {
        usb_cdc_functional_descriptor_header_t* descriptor = dev->functional_descriptors[j];
        usb_generic_fill_fifo_words(enp0, (uint8_t *)descriptor,
                                    get_size(descriptor->bFunctionLength, &size),
                                    enp0fifo, &sub_word_data, &sub_word_count);
      }
    }

    for (uint8_t j = 0; j < interface->endpoint_descriptors_count; j++) {
      usb_generic_fill_fifo_words(enp0,
                                  (uint8_t*)&interface->endpoint_descriptors[j],
                                  get_size(sizeof(usb_endpoint_descriptor_t), &size),
                                  enp0fifo, &sub_word_data, &sub_word_count);
    }
  }

  // The fifo takes always 4 elements. We do not care what's written on the
  // last bytes, since the peripheral will know only about the first bytes
  // as per the size written to DIEPTSIZ
  if (sub_word_count > 0) {
    sub_word_count = 0;
    usb_generic_fill_fifo_words(enp0, (uint8_t *)&sub_word_data,
                                4, enp0fifo, &sub_word_data, &sub_word_count);
  }

  // After the fifo is filled...
  enp0->DIEPCTL = USB_OTG_DIEPCTL_CNAK;
  return result;
}

task_result_t usb_device_cdc_setup_packet_callback(usb_device_t *device,
                                                   usb_setup_command_t *cmd) {
  // TODO - is there something to do? maybe just the multiplexed commands?
  return RES_OK;
}
void usb_device_cdc_enumeration_done_callback(usb_device_t *device) {
  // TODO start the application somehow
}
void usb_device_cdc_txfifo_empty_callback(usb_device_t *device,
                                          uint8_t endpoint) {
// TODO the application
}
void usb_device_cdc_rxfifo_empty_callback(usb_device_t *device,
                                          uint8_t endpoint) {
// TODO the application
}
void usb_device_cdc_transmit_done_callback(usb_device_t *device,
                                           uint8_t endpoint) {
// TODO the application
}
void usb_device_cdc_nak_callback(usb_device_t *device, uint8_t endpoint) {
  // Nothing to do for now
}
void usb_device_cdc_nyet_callback(usb_device_t *device, uint8_t endpoint) {
  // Nothing to do for now
}
Do not follow this link