diff --git a/firmware/application/usb_serial.cpp b/firmware/application/usb_serial.cpp index f419588f..98d652e0 100644 --- a/firmware/application/usb_serial.cpp +++ b/firmware/application/usb_serial.cpp @@ -49,6 +49,7 @@ void USBSerial::on_channel_opened() { } void USBSerial::on_channel_closed() { + reset_transfer_queues(); connected = false; } diff --git a/firmware/application/usb_serial_cdc.c b/firmware/application/usb_serial_cdc.c index ef254511..bbeb161e 100644 --- a/firmware/application/usb_serial_cdc.c +++ b/firmware/application/usb_serial_cdc.c @@ -42,6 +42,10 @@ CH_IRQ_HANDLER(USB0_IRQHandler) { chSysUnlockFromIsr(); } + if (status & USB0_USBSTS_D_SLI) { + on_channel_closed(); + } + CH_IRQ_EPILOGUE(); } @@ -131,11 +135,7 @@ usb_request_status_t usb_get_line_coding_request(usb_endpoint_t* const endpoint, } usb_request_status_t usb_set_control_line_state_request(usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) { if (stage == USB_TRANSFER_STAGE_SETUP) { - // if (endpoint->setup.value == 3) { on_channel_opened(); - //} else { - // on_channel_closed(); - //} usb_transfer_schedule_ack(endpoint->in); } diff --git a/firmware/application/usb_serial_host_to_device.cpp b/firmware/application/usb_serial_host_to_device.cpp index 0d64e749..47f6067b 100644 --- a/firmware/application/usb_serial_host_to_device.cpp +++ b/firmware/application/usb_serial_host_to_device.cpp @@ -54,6 +54,14 @@ void init_host_to_device() { thread_usb_event = chThdSelf(); } +void reset_transfer_queues() { + while (usb_bulk_buffer_queue.empty() == false) + usb_bulk_buffer_queue.pop(); + + while (usb_bulk_buffer_spare.empty() == false) + usb_bulk_buffer_spare.pop(); +} + void schedule_host_to_device_transfer() { if (usb_bulk_buffer_queue.size() >= 8) return; diff --git a/firmware/application/usb_serial_host_to_device.hpp b/firmware/application/usb_serial_host_to_device.hpp index 5c89a73b..e67f8093 100644 --- a/firmware/application/usb_serial_host_to_device.hpp +++ b/firmware/application/usb_serial_host_to_device.hpp @@ -27,6 +27,7 @@ #define USB_BULK_BUFFER_SIZE 64 void init_host_to_device(); +void reset_transfer_queues(); void serial_bulk_transfer_complete(void* user_data, unsigned int bytes_transferred); void schedule_host_to_device_transfer(); void complete_host_to_device_transfer(); diff --git a/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp b/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp index 13bf7f12..563f6cd4 100644 --- a/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp +++ b/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp @@ -22,13 +22,23 @@ #include "ch.h" #include "hal.h" +#include extern "C" { void start_usb(void); void irq_usb(void); void usb_transfer(void); +extern volatile bool scsi_running; + CH_IRQ_HANDLER(Vector60) { + const uint32_t status = USB0_USBSTS_D & USB0_USBINTR_D; + if (status & USB0_USBSTS_D_SLI) { + // USB reset received. + if (scsi_running) { + LPC_RGU->RESET_CTRL[0] = (1 << 0); + } + } irq_usb(); } } diff --git a/firmware/baseband/sd_over_usb/sd_over_usb.c b/firmware/baseband/sd_over_usb/sd_over_usb.c index f75107af..5f4a0d9b 100644 --- a/firmware/baseband/sd_over_usb/sd_over_usb.c +++ b/firmware/baseband/sd_over_usb/sd_over_usb.c @@ -23,7 +23,7 @@ #include "sd_over_usb.h" #include "scsi.h" -bool scsi_running = false; +volatile bool scsi_running = false; usb_request_status_t report_max_lun( usb_endpoint_t* const endpoint,