diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index 7f3c3495..231abc7d 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -232,60 +232,63 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) /*------------------------------------------------------------------*/ static bool maybe_handle_setup_packet(void) { - if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP) - { - USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP; + if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP) + { + USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP; - // This copies the data elsewhere so we can reuse the buffer. - dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); - return true; - } - return false; + // This copies the data elsewhere so we can reuse the buffer. + dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); + return true; + } + return false; } void maybe_transfer_complete(void) { - uint32_t epints = USB->DEVICE.EPINTSMRY.reg; - for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) { - if ((epints & (1 << epnum)) == 0) { - continue; - } + uint32_t epints = USB->DEVICE.EPINTSMRY.reg; - if (maybe_handle_setup_packet()) { - continue; - } - UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - - uint32_t epintflag = ep->EPINTFLAG.reg; - - uint16_t total_transfer_size = 0; - - // Handle IN completions - if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT1) != 0) { - ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1; - - UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_IN]; - total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; - - uint8_t ep_addr = epnum | TUSB_DIR_IN_MASK; - dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true); - } - - // Handle OUT completions - if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT0) != 0) { - ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0; - - UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_OUT]; - total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; - - uint8_t ep_addr = epnum; - dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true); - } - - // just finished status stage (total size = 0), prepare for next setup packet - if (epnum == 0 && total_transfer_size == 0) { - dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); - } + for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) { + if ((epints & (1 << epnum)) == 0) { + continue; } + + if (maybe_handle_setup_packet()) { + continue; + } + + UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; + + uint32_t epintflag = ep->EPINTFLAG.reg; + + uint16_t total_transfer_size = 0; + + // Handle IN completions + if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT1) != 0) { + ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1; + + UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_IN]; + total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; + + uint8_t ep_addr = epnum | TUSB_DIR_IN_MASK; + dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true); + } + + // Handle OUT completions + if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT0) != 0) { + ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0; + + UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_OUT]; + total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; + + uint8_t ep_addr = epnum; + dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true); + } + + // Just finished status stage (total size = 0), prepare for next setup packet + // TODO could cause issue with actual zero length data used by class such as DFU + if (epnum == 0 && total_transfer_size == 0) { + dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); + } + } } void USB_Handler(void) diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index f2aff857..f58e7cbc 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -213,9 +213,9 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { - ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; + ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; } else { - ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0; + ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0; } } @@ -236,15 +236,15 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) /*------------------------------------------------------------------*/ static bool maybe_handle_setup_packet(void) { - if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP) - { - USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP; + if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP) + { + USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP; - // This copies the data elsewhere so we can reuse the buffer. - dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); - return true; - } - return false; + // This copies the data elsewhere so we can reuse the buffer. + dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); + return true; + } + return false; } /* *------------------------------------------------------------------*/ @@ -309,42 +309,42 @@ void USB_0_Handler(void) { /* USB_SOF_HSOF */ void USB_1_Handler(void) { - USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF; - dcd_event_bus_signal(0, DCD_EVENT_SOF, true); + USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF; + dcd_event_bus_signal(0, DCD_EVENT_SOF, true); } void transfer_complete(uint8_t direction) { - uint32_t epints = USB->DEVICE.EPINTSMRY.reg; - for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) { - if ((epints & (1 << epnum)) == 0) { - continue; - } - - if (direction == TUSB_DIR_OUT && maybe_handle_setup_packet()) { - continue; - } - UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - - UsbDeviceDescBank* bank = &sram_registers[epnum][direction]; - uint16_t total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; - - uint8_t ep_addr = epnum; - if (direction == TUSB_DIR_IN) { - ep_addr |= TUSB_DIR_IN_MASK; - } - dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true); - - // just finished status stage (total size = 0), prepare for next setup packet - if (epnum == 0 && total_transfer_size == 0) { - dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); - } - - if (direction == TUSB_DIR_IN) { - ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1; - } else { - ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0; - } + uint32_t epints = USB->DEVICE.EPINTSMRY.reg; + for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) { + if ((epints & (1 << epnum)) == 0) { + continue; } + + if (direction == TUSB_DIR_OUT && maybe_handle_setup_packet()) { + continue; + } + UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; + + UsbDeviceDescBank* bank = &sram_registers[epnum][direction]; + uint16_t total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; + + uint8_t ep_addr = epnum; + if (direction == TUSB_DIR_IN) { + ep_addr |= TUSB_DIR_IN_MASK; + } + dcd_event_xfer_complete(0, ep_addr, total_transfer_size, XFER_RESULT_SUCCESS, true); + + // just finished status stage (total size = 0), prepare for next setup packet + if (epnum == 0 && total_transfer_size == 0) { + dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); + } + + if (direction == TUSB_DIR_IN) { + ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1; + } else { + ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0; + } + } } // Bank zero is for OUT and SETUP transactions. @@ -352,7 +352,7 @@ void transfer_complete(uint8_t direction) { USB_TRCPT0_3, USB_TRCPT0_4, USB_TRCPT0_5, USB_TRCPT0_6, USB_TRCPT0_7 */ void USB_2_Handler(void) { - transfer_complete(TUSB_DIR_OUT); + transfer_complete(TUSB_DIR_OUT); } // Bank one is used for IN transactions. @@ -360,7 +360,7 @@ void USB_2_Handler(void) { USB_TRCPT1_3, USB_TRCPT1_4, USB_TRCPT1_5, USB_TRCPT1_6, USB_TRCPT1_7 */ void USB_3_Handler(void) { - transfer_complete(TUSB_DIR_IN); + transfer_complete(TUSB_DIR_IN); } #endif