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

ref: 8fd37eafa2cd7ba0dc587922c113260ce53aabca stm32h747i-disco-usb-image-viewer/src/usb_device.c -rw-r--r-- 21.0 KiB
8fd37eaf — Rutherther feat: move all sending instructions to generic send functions 5 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "usb_device.h"
#include "usb.h"
#include <stdlib.h>
#include <stddef.h>
#include <stm32h747xx.h>
#include "registers.h"
#include "delay.h"

/* USB_OTG_GlobalTypeDef */

usb_device_t* usb1_device;

void* usb_device_init(void* peripheral_address, usb_class_t* class, void* buffer) {
  if (buffer == NULL) {
    buffer = (void*)malloc(sizeof(usb_device_t));
  }

  usb_device_t* device = (usb_device_t*)buffer;

  device->state = INIT;
  device->core = peripheral_address + USB_OTG_GLOBAL_BASE;
  device->device = peripheral_address + USB_OTG_DEVICE_BASE;
  device->out = peripheral_address + USB_OTG_OUT_ENDPOINT_BASE;
  device->in = peripheral_address + USB_OTG_IN_ENDPOINT_BASE;
  device->fifos = (usb_fifo_t*)(((uint8_t*)device->core) + USB_OTG_FIFO_BASE);
  device->endpoint_count = 8;
  device->received_setup_commands_count = 0;
  device->received_setup_commands_index = 0;
  device->class = *class;

  // TODO: clarify how this should work...
  usb1_device = device;

  return device;
}

void usb_device_reset(void* device_ptr) {
  usb_device_t* device = (usb_device_t*)device_ptr;

  // Idle
  while ((device->core->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0);
  // Reset
  device->core->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
  // Wait for reset
  while (device->core->GRSTCTL & USB_OTG_GRSTCTL_CSRST);
  // Wait for idle
  while ((device->core->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0);
  DELAY_US(3);
}

void usb_device_clear_interrupts(void* device_ptr) {
  usb_device_t* device = (usb_device_t*)device_ptr;
  device->core->GINTSTS = 0; // clear interrupts

  // for other interrupts the rx queue has to be read
}

void usb_device_wait_for_handshake(void* device_ptr) {
  usb_device_t* device = (usb_device_t*)device_ptr;

  while (device->state != ENUMERATED);
}

void usb_device_setup(void* device_ptr) {
  RCC->AHB1ENR |= RCC_AHB1ENR_USB1OTGHSEN | RCC_AHB1ENR_USB1OTGHSULPIEN;

  volatile uint32_t dummy;
  dummy = RCC->AHB1ENR;
  dummy = RCC->AHB1ENR;

  NVIC_SetPriority(OTG_HS_IRQn, 1);
  NVIC_EnableIRQ(OTG_HS_IRQn);

  // 1. core initialization
  usb_device_t* device = (usb_device_t*)device_ptr;

  device->device->DCTL |= USB_OTG_DCTL_SDIS;

  PWR->CR3 |= PWR_CR3_USB33DEN;
  while ((PWR->CR3 & PWR_CR3_USB33RDY) == 0);

  device->core->GCCFG |= USB_OTG_GCCFG_PWRDWN;

  device->core->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;

  DELAY_US(3);
  usb_device_reset(device_ptr);

  // global interrupt mask
  // rx fifo non empty
  // periodic tx fifo empty level
  usb_device_clear_interrupts(device_ptr);
  device->core->GAHBCFG = USB_OTG_GAHBCFG_GINT | USB_OTG_GAHBCFG_TXFELVL | USB_OTG_GAHBCFG_HBSTLEN_2;

  // hnp capable, srp capable, otg hs timeout, usb turnaround
  // TODO: based on enumeration speed set TOCAL - fs timeout calibration
  device->core->GUSBCFG = USB_OTG_GUSBCFG_FDMOD | USB_OTG_GUSBCFG_ULPIEVBUSI | USB_OTG_GUSBCFG_ULPIEVBUSD | USB_OTG_GUSBCFG_TRDT_3 | USB_OTG_GUSBCFG_TRDT_2;

  // TODO: are these needed? device mode is forced...
  // unmask otg interrupt mask
  /* device->core->GINTMSK |= USB_OTG_GINTMSK_OTGINT | USB_OTG_GINTMSK_MMISM; */

  // device initialization

  // descdma, device speed, non-zero-length status out handshake, periodic frame interval
  device->device->DCFG = 0;

  // TODO device threshold control register IF DMA

  // unmask interrupts usb reset, enumeration done, early suspend, usb suspend, sof
  device->core->GINTMSK |= USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_RSTDEM | USB_OTG_GINTMSK_USBRST | USB_OTG_GINTMSK_ESUSPM | USB_OTG_GINTMSK_SOFM | USB_OTG_GINTMSK_RXFLVLM;

  // sdis bit
  device->device->DCTL = ~(USB_OTG_DCTL_SDIS);

  // wait for usbrst interrupt - the reset
  /* while (((device->core->GINTSTS) & USB_OTG_GINTSTS_USBRST) == 0) {} */

  // wait for enumdne interrupt - end of reset
  /* while (((device->core->GINTSTS) & USB_OTG_GINTSTS_ENUMDNE) == 0) {} */
}

typedef enum {
  PACKET_GLOBAL_OUT_NAK = 1,
  PACKET_OUT_DATA_PACKET_RCVD = 2,
  PACKET_OUT_TRANSFER_COMPLETED = 3,
  PACKET_SETUP_TRANSACTION_COMPLETED = 4,
  PACKET_SETUP_DATA_PACKET_RECEIVED = 6,
} packet_status_t;

typedef enum {
  DATA0 = 0,
  DATA1 = 1,
  DATA2 = 2,
  MDATA = 3
} packet_dpid_t;

typedef struct {
  packet_status_t packet_status;
  packet_dpid_t dpid;
  uint8_t byte_count;
  uint8_t endpoint_num;
} packet_info_t;

void usb_handle_setup(usb_device_t *device, usb_setup_command_t* cmd) {
  switch (cmd->bRequest) {
  case USB_SETUP_GET_STATUS: {
    uint8_t size;
    uint8_t* packet;
    usb_device_status_t device_status;
    usb_interface_status_t interface_status;
    usb_endpoint_status_t endpoint_status;

    switch (cmd->bmRequestType.recipient) {
    case USB_SETUP_DEVICE: {
      size = sizeof(usb_device_status_t);
      device_status.self_powered = 0;
      device_status.remote_wakeup = 0;
      device_status.reserved_zeros = 0;
      packet = (uint8_t*)&device_status;
    }
      break;
    case USB_SETUP_INTERFACE: {
      size = sizeof(usb_interface_status_t);
      interface_status.reserved = 0;
      packet = (uint8_t*)&interface_status;
    }
      break;
    case USB_SETUP_ENDPOINT: {
      size = sizeof(usb_endpoint_status_t);
      endpoint_status.halt = 0;
      endpoint_status.reserved_zeros = 0;
      packet = (uint8_t*)&interface_status;
    }
      break;
    }

    usb_generic_send(device->in, packet, size,
                     &device->fifos[0].data[0]);
  }
    break;
  case USB_SETUP_GET_DESCRIPTOR: {
    usb_descriptor_type_t descriptor_type = (cmd->wValue >> 8) & 0xFF;
    uint8_t descriptor_index = cmd->wValue & 0xFF;

    switch (descriptor_type) {
    case DESCRIPTOR_DEVICE:
      usb_send_device_descriptor(device->in, &device->class.device_descriptor, &device->fifos[0].data[0]);
      break;
    case DESCRIPTOR_CONFIGURATION:
      usb_send_configuration_descriptor(device->in, &device->class.configuration_descriptor, &device->fifos[0].data[0]);
      break;
    case DESCRIPTOR_STRING: {
      if (descriptor_index == 0) {
        usb_send_string_descriptor_zero(device->in, &device->class.string_descriptor_zero, &device->fifos[0].data[0]);
      } else {
        uint8_t index = descriptor_index - 1;
        // NOTE: the user could potentially read different memory part!!
        // This has to be fixed, the length has to be stored somewhere.
        usb_send_unicode_string_descriptor(device->in, &device->class.string_descriptors[index], &device->fifos[0].data[0]);
      }
    }
      break;
    case DESCRIPTOR_INTERFACE:
      usb_send_interface_descriptor(device->in, &device->class.interface_descriptor, &device->fifos[0].data[0]);
      break;
    case DESCRIPTOR_ENDPOINT:
      usb_send_endpoint_descriptor(device->in, &device->class.endpoint_descriptors[descriptor_index], &device->fifos[0].data[0]);
      break;
    case DESCRIPTOR_DEVICE_QUALIFIER:
      usb_send_device_qualifier_descriptor(device->in, &device->class.device_qualifier, &device->fifos[0].data[0]);
      break;
    case DESCRIPTOR_OTHER_SPEED_CONFIGURATION:
    case DESCRIPTOR_INTERFACE_POWER:
      reg_set_bits(&device->out[0].DOEPCTL, USB_OTG_DOEPCTL_STALL);
      break;
    }
    // TODO
  }
    break;
  case USB_SETUP_GET_CONFIGURATION: {
    uint8_t value = device->class.configuration_descriptor.bConfigurationValue;
    if (device->state != ENUMERATED) {
      value = 0;
    }
    usb_generic_send(device->in, &value, sizeof(value), &device->fifos[0].data[0]);
  }
    break;
  case USB_SETUP_GET_INTERFACE: {
    usb_generic_send(device->in,
                     &device->class.interface_descriptor.bAlternateSetting,
                     sizeof(uint8_t),
                     &device->fifos[0].data[0]);
  }
    break;
  case USB_SETUP_SET_ADDRESS:
    reg_write_bits_pos(&device->device->DCFG,
                       cmd->wValue,
                       USB_OTG_DCFG_DAD_Pos,
                       0x7F);
    usb_generic_send(device->in, NULL, 0, &device->fifos[0].data[0]);
    device->state = SET_ADDRESS_RCVD;
    break;
  case USB_SETUP_SET_CONFIGURATION: {
    if (cmd->wValue == 0) {
      device->state = SET_ADDRESS_RCVD;
      usb_generic_send(device->in, NULL, 0, &device->fifos[0].data[0]);

      // TODO disable endpoints
    } else if (cmd->wValue == device->class.configuration_descriptor.bConfigurationValue) {
      device->state = ENUMERATED;
      usb_generic_send(device->in, NULL, 0, &device->fifos[0].data[0]);

      // TODO setup endpoints
    } else {
      reg_set_bits(&device->in[0].DIEPCTL, USB_OTG_DIEPCTL_STALL);
    }
  }
    break;
  case USB_SETUP_CLEAR_FEATURE:
    switch (cmd->wValue) {
    case USB_FEATURE_ENDPOINT_HALT: {
      uint8_t ep_id = cmd->wIndex;
      reg_clear_bits(&device->in[ep_id].DIEPCTL, USB_OTG_DIEPCTL_STALL);
      reg_clear_bits(&device->out[ep_id].DOEPCTL, USB_OTG_DOEPCTL_STALL);
    }
      break;
    case USB_FEATURE_TEST_MODE:
    case USB_FEATURE_DEVICE_REMOTE_WAKEUP:
      // Do not do anything...
      break;
    }

    usb_generic_send(device->in, NULL, 0, &device->fifos[0].data[0]);
    break;
  case USB_SETUP_SET_FEATURE:
    switch (cmd->wValue) {
    case USB_FEATURE_ENDPOINT_HALT: {
      uint8_t ep_id = cmd->wIndex;
      reg_set_bits(&device->in[ep_id].DIEPCTL, USB_OTG_DIEPCTL_STALL);
      reg_set_bits(&device->out[ep_id].DOEPCTL, USB_OTG_DOEPCTL_STALL);
    }
      break;
    case USB_FEATURE_TEST_MODE:
    case USB_FEATURE_DEVICE_REMOTE_WAKEUP:
      // Do not do anything...
      break;
    }

    usb_generic_send(device->in, NULL, 0, &device->fifos[0].data[0]);
    break;
  case USB_SETUP_SET_DESCRIPTOR:
  case USB_SETUP_SET_INTERFACE:
  case USB_SETUP_SYNCH_FRAME:
    device->in->DIEPCTL |= USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_STALL;
    device->state = UNKNOWN_CONTROL_COMMAND;
    break;
  case RESERVED1:
  case RESERVED2:
    device->state = UNKNOWN_CONTROL_COMMAND;
    break;
  }
}

void usb_handle_rxflvl_control_int(usb_device_t *device,
                                   packet_info_t *packet_info) {
  uint32_t dummy;
  uint8_t data[64];
  uint32_t *fifo = device->fifos[0].data;

  if (packet_info->packet_status == PACKET_SETUP_TRANSACTION_COMPLETED) {
    // Nothing do to.
  } else if (packet_info->packet_status == PACKET_SETUP_DATA_PACKET_RECEIVED) {
    // SAVE data
    uint8_t setup_packets_count = 3 - reg_read_bits_pos(&device->out[0].DOEPTSIZ, USB_OTG_DOEPTSIZ_STUPCNT_Pos, 3);
    usb_generic_read(
        (uint8_t *)&device
            ->received_setup_commands[device->received_setup_commands_index++],
        8,
        fifo);
    device->received_setup_commands_index %= 3;

    if (setup_packets_count == 0) {
      reg_write_bits_pos(&device->out[0].DOEPTSIZ, 3, USB_OTG_DOEPTSIZ_STUPCNT_Pos, 3);
    }

    dummy = *fifo; // the last that will trigger another interrupt
  } else if (packet_info->byte_count != 0) {
    usb_generic_read(data, packet_info->byte_count, fifo);
  }
}

void usb_handle_rxflvl_int(usb_device_t *device) {
  // Disable the interrupt
  reg_clear_bits(&device->core->GINTMSK, USB_OTG_GINTMSK_RXFLVLM);

  /* uint32_t rx_data; */
  /* while ((rx_data = device->core->GRXSTSP) != 0) { */
  while (device->core->GINTSTS & USB_OTG_GINTSTS_RXFLVL) {
    uint32_t status_data = device->core->GRXSTSP;

    /* uint8_t status_phase_start = reg_read_bits_pos(&status_data, USB_OTG_GRXSTSP_STSPHST); */
    packet_info_t packet_info = {
      .packet_status = (packet_status_t)reg_read_bits_pos(&status_data, USB_OTG_GRXSTSP_PKTSTS_Pos, 0xF),
      .dpid = (packet_dpid_t)reg_read_bits_pos(&status_data, USB_OTG_GRXSTSP_DPID_Pos, 0x3),
      .byte_count = reg_read_bits_pos(&status_data, USB_OTG_GRXSTSP_BCNT_Pos, 0x7FF),
      .endpoint_num = reg_read_bits_pos(&status_data, USB_OTG_GRXSTSP_EPNUM_Pos, 0xF),
    };

    if (packet_info.endpoint_num == 0) {
      usb_handle_rxflvl_control_int(device, &packet_info);
    } else {
      // TODO: ...
    }
  }

  // Read info about the endpoint etc.

  // Re-enable the interrupt
  reg_set_bits(&device->core->GINTMSK, USB_OTG_GINTMSK_RXFLVLM);
}

uint8_t usb_daint_get_endpoint_number(uint32_t endpoints) {
  for (int i = 0; i < 15; i++) {
    if (endpoints & (1 << i)) {
      return i;
    }
  }
  return 0xFF;
}

void usb_handle_endpoint_in_int(usb_device_t* device) {
  uint8_t ep_id = usb_daint_get_endpoint_number(reg_read_bits_pos(&device->device->DAINT, USB_OTG_DAINT_IEPINT_Pos, 0xFFFF));

  if (ep_id == 0xFF) {
    device->state = ERROR;
    return;
  }

  uint32_t interrupt_reg = device->in[ep_id].DIEPINT;

  if (interrupt_reg & USB_OTG_DIEPINT_PKTDRPSTS) {
    // Not generated by interrupt! So if we get this one
    // here, it's not from the interrupt. Just clear it,
    // isochronous pipes can drop data from time to time
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_PKTDRPSTS);
  }

  if (interrupt_reg & USB_OTG_DIEPINT_NAK) {
    // NOTE no need to do much. Hardware will resend
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_NAK);
  } else if (interrupt_reg & USB_OTG_DIEPINT_BNA) {
    // NOTE this should not be reached as DMA is not used
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_BNA);
  } else if (interrupt_reg & USB_OTG_DIEPINT_TXFIFOUDRN) {
    // NOTE this should not be reached as thresholding is not used
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_TXFIFOUDRN);
  } else if (interrupt_reg & USB_OTG_DIEPINT_TXFE) {
    // Now we can send more data. TODO notify application about this
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_TXFE);
  } else if (interrupt_reg & USB_OTG_DIEPINT_INEPNE) {
    // NAK effective. Okay, ack, go on.
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_INEPNE);
  } else if (interrupt_reg & USB_OTG_DIEPINT_INEPNM) {
    device->state = ERROR; // data on top of TxFIFO belong to endpoint other
    // than the one for which in token was received.
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_INEPNM);
  } else if (interrupt_reg & USB_OTG_DIEPINT_ITTXFE) {
    // In token when no data. How to proceed? TODO
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_ITTXFE);
  } else if (interrupt_reg & USB_OTG_DIEPINT_TOC) {
    // Timeout condition. Skip? TODO how to proceed?
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_TOC);
  } else if (interrupt_reg & USB_OTG_DIEPINT_AHBERR) {
    // NOTE this should not be reached as thresholding is not used
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_AHBERR);
  } else if (interrupt_reg & USB_OTG_DIEPINT_EPDISD) {
    // Endpoint is disabled, per application's request. Okay.
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_EPDISD);
  } else if (interrupt_reg & USB_OTG_DIEPINT_XFRC) {
    // Transfer is completed. TODO notify application?
    reg_clear_bits(&device->in[ep_id].DIEPINT, USB_OTG_DIEPINT_XFRC);
  }
}

#define USB_OTG_DOEPINT_BNA_Pos              (9U)
#define USB_OTG_DOEPINT_BNA_Msk              (0x1UL << USB_OTG_DOEPINT_B2BSTUP_Pos) /*!< 0x00000040 */
#define USB_OTG_DOEPINT_BNA                  USB_OTG_DOEPINT_B2BSTUP_Msk   /*!< Back-to-back SETUP packets received */

void usb_handle_endpoint_out_int(usb_device_t* device) {
  /* device->core->GRXSTSP; */
  /* device->core->GRXFSIZ; */

  uint8_t ep_id = usb_daint_get_endpoint_number(reg_read_bits_pos(&device->device->DAINT, USB_OTG_DAINT_OEPINT_Pos, 0xFFFF));

  if (ep_id == 0xFF) {
    device->state = ERROR;
    return;
  }

  uint32_t interrupt_reg = device->out[ep_id].DOEPINT;

  if (interrupt_reg & USB_OTG_DOEPINT_STPKTRX) {
    // NOTE This shouldn't be reached since DMA is not used
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_STPKTRX);
  } else if (interrupt_reg & USB_OTG_DOEPINT_NYET) {
    // We don't really care about this one for now
    // TODO: for future - trigger a callback to application
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_NYET);
  } else if (interrupt_reg & USB_OTG_DOEPINT_NAK) {
    // We don't really care about this one for now
    // TODO: for future - trigger a callback to application
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_NAK);
  } else if (interrupt_reg & USB_OTG_DOEPINT_BERR) {
    // Uh? Babble much?
    device->state = CONTROL_ERROR;
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_BERR);
  } else if (interrupt_reg & USB_OTG_DOEPINT_BNA) {
    // NOTE This shoudln't be reached since DMA is not used
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_BNA);
  } else if (interrupt_reg & USB_OTG_DOEPINT_OUTPKTERR) {
    // NOTE thresholding not enabled, so this shouldn't be reached
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_OUTPKTERR);
  } else if (interrupt_reg & USB_OTG_DOEPINT_B2BSTUP) {
    // TODO: this is a problem! we couldn't capture all the packets!
    device->state = CONTROL_ERROR;
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_B2BSTUP);
  } else if (interrupt_reg & USB_OTG_DOEPINT_OTEPSPR) {
    // TODO: ack or stall the status phase? How?
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_OTEPSPR);
  } else if (interrupt_reg & USB_OTG_DOEPINT_OTEPDIS) {
    // NOTE: Can we handle this? a callback to application?
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_OTEPDIS);
  } else if (interrupt_reg & USB_OTG_DOEPINT_STUP) {
    // handle all setup commands
    uint8_t setup_packets_count = 3 - reg_read_bits_pos(&device->out[ep_id].DOEPTSIZ, USB_OTG_DOEPTSIZ_STUPCNT_Pos, 3);
    usb_handle_setup(device, &device->received_setup_commands[0]);
    for (int i = 0; i < device->received_setup_commands_count; i++) {
    }

    device->received_setup_commands_count = 0;
    // TODO: null the data? shouldn't be needed...

    // 3 packets to receive as setup
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_STUP);
  } else if (interrupt_reg & USB_OTG_DOEPINT_AHBERR) {
    // NOTE This shoudln't be reached since DMA is not used
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_AHBERR);
  } else if (interrupt_reg & USB_OTG_DOEPINT_EPDISD) {
    // NOTE endpoint has been disabled, as was instructed. So this shoudln't
    // need handling?
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_EPDISD);
  } else if (interrupt_reg & USB_OTG_DOEPINT_XFRC) {
    // Transfer has been completed
    // TODO: handle data? - callback to device data handle?
    reg_clear_bits(&device->out[ep_id].DOEPINT, USB_OTG_DOEPINT_XFRC);
  }
}

// NOTE: this is irq handler
void otg_hs_handler(void) {

  usb_device_t* device = usb1_device;

  // Reset detected
  if (device->core->GINTSTS & USB_OTG_GINTSTS_USBRST) {
    reg_set_bits(&device->core->GINTMSK, USB_OTG_GINTMSK_IEPINT | USB_OTG_GINTMSK_OEPINT);
    reg_set_bits(&device->device->DOEPMSK, USB_OTG_GINTMSK_IEPINT | USB_OTG_GINTMSK_OEPINT);

    // TODO: should this be done for ep 0 as well?
    for (int ep = 0; ep < device->endpoint_count; ep++) {
      USB_OTG_OUTEndpointTypeDef *out = device->out + ep;
      out->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
    }
    device->device->DAINTMSK |= 1 << USB_OTG_DAINTMSK_IEPM_Pos;
    device->device->DAINTMSK |= 1 << USB_OTG_DAINTMSK_OEPM_Pos;

    device->device->DOEPMSK |= USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM;
    device->device->DIEPMSK |= USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_TOM;

    // 512 bytes
    device->core->GRXFSIZ = 256 / 4;
    // 64 bytes, beginning of ram
    device->core->DIEPTXF[0] = (0) << USB_OTG_DIEPTXF_INEPTXSA_Pos | (64 / 4) << USB_OTG_DIEPTXF_INEPTXFD_Pos;

    // 3 packets to receive as setup
    reg_write_bits_pos(&device->out->DOEPTSIZ, 3, USB_OTG_DOEPTSIZ_STUPCNT_Pos, 3);

    device->state = RESET;
    // clear it
    reg_set_bits(&device->core->GINTSTS,  USB_OTG_GINTSTS_USBRST);

    return;
  }

  // Reset ended
  if (device->core->GINTSTS & USB_OTG_GINTSTS_ENUMDNE) {
    // The enumerated speed
    /* (device->device->DSTS & USB_OTG_DSTS_ENUMSPD_Msk) >> USB_OTG_DSTS_ENUMSPD_Pos; */
    // We do not need to know the speed, it can be either full speed or high
    // speed, both have maximum size 64 bytes. Only low speed or super high
    // speed would differ, but these are not supported!
    reg_write_bits_pos(&device->in->DIEPCTL, 64, USB_OTG_DIEPCTL_MPSIZ_Pos, 0x7FFUL);

    device->out->DOEPCTL |= USB_OTG_DOEPCTL_CNAK;

    device->state = RESET_DONE;
    // clear it
    reg_set_bits(&device->core->GINTSTS,  USB_OTG_GINTSTS_ENUMDNE);

    return;
  }

  // Start of frame
  if (device->core->GINTSTS & USB_OTG_GINTSTS_SOF) {
    // Nothing to do?
    reg_set_bits(&device->core->GINTSTS,  USB_OTG_GINTSTS_SOF);
    return;
  }

  // OTG interrupt, should not be triggered since forced device mode
  if (device->core->GINTSTS & USB_OTG_GINTSTS_OTGINT) {
    device->state = OTG_ERROR;
    reg_set_bits(&device->core->GINTSTS,  USB_OTG_GINTSTS_OTGINT);
    return;
  }

  // OTG interrupt, should not be triggered since forced device mode
  if (device->core->GINTSTS & USB_OTG_GINTSTS_MMIS) {
    device->state = OTG_ERROR;
    reg_set_bits(&device->core->GINTSTS,  USB_OTG_GINTSTS_MMIS);
    return;
  }

  // Setup, data...
  if (device->core->GINTSTS & USB_OTG_GINTSTS_OEPINT) {
    usb_handle_endpoint_out_int(device);
    return;
  }

  if (device->core->GINTSTS & USB_OTG_GINTSTS_IEPINT) {
    usb_handle_endpoint_in_int(device);
    return;
  }

  if (device->core->GINTSTS & (USB_OTG_GINTSTS_RXFLVL)) {
    // TODO
    usb_handle_rxflvl_int(device);
    return;
  }

  device->state = UNKNOWN_INTERRUPT;
}
Do not follow this link