void usb_handle_reset(void) { do { ClearUsbInterruptFlag(USB_TRN); // JTR corrected Must poll TRN Flag and clear, then wait 6 cycles. for next flag set. usb_current_cfg = 0; usb_device_state = DEFAULT_STATE; // This BLOCK creates the requied 6 cycle delay for TRNF to reassert. usb_addr_pending = 0x00; } while (USB_TRANSACTION_FLAG); ClearUSBtoDefault(); EnablePacketTransfer(); }
BYTE FAST_usb_handler(void) { if (TestGlobalUsbInterruptFlag()) { if (USB_RESET_FLAG) { usb_handle_reset(); ClearUsbInterruptFlag(USB_URST); return 0xFF; } if (USB_TRANSACTION_FLAG) { trn_status = GetUsbTransaction(); if (USB_STAT2EP(trn_status)) { usb_handle_transaction(); ClearUsbInterruptFlag(USB_TRN); // non-EP0 only return 0xFF; } else { ClearUsbInterruptFlag(USB_TRN); // non-EP0 only } } ClearGlobalUsbInterruptFlag(); } return 0; }
void usb_handler(void) { if (USB_IDLE_FLAG) { /* Idle - suspend */ USBSuspend(); // // Must be defined in user code. ClearUsbInterruptFlag(USB_IDLE); } if (USB_RESET_FLAG) { usb_handle_reset(); ClearUsbInterruptFlag(USB_URST); } if (USB_ERROR_FLAG) { // usb_handle_error(); ClearAllUsbErrorInterruptFlags(); ClearUsbInterruptFlag(USB_UERR); } if (USB_STALL_FLAG) { ClearUsbInterruptFlag(USB_STALL); } if (USB_SOF_FLAG) { /* Start-of-frame */ if (sof_handler) sof_handler(); ClearUsbInterruptFlag(USB_SOF); } if (USB_TRANSACTION_FLAG) { if (!USB_STAT2EP(GetUsbTransaction())) usb_handle_transaction(); // Only handle EP0 transactions. ClearUsbInterruptFlag(USB_TRN); // JTR Missing! This is why Ian was only getting one interrupt?? } // Side effect: advance USTAT Fifo }
void usb_handler(void) { if (USB_RESET_FLAG) { usb_handle_reset(); ClearUsbInterruptFlag(USB_URST); } else if (USB_ERROR_FLAG) { usb_handle_error(); ClearUsbInterruptFlag(USB_UERR); } else if (USB_ACTIVITY_FLAG) { /* Activity - unsuspend */ //WakeupUsb(); ClearUsbInterruptFlag(USB_ACTIV); } else if (USB_IDLE_FLAG) { /* Idle - suspend */ //SuspendUsb(); //usb_low_power_request(); ClearUsbInterruptFlag(USB_IDLE); } else if (USB_STALL_FLAG) { /* Stall detected * Not sure when this interrupt fires * as the host never should send out a stall. * Perhaps as a respons to our own stalls? * For now just ignore it. */ ClearUsbInterruptFlag(USB_STALL); } else if (USB_SOF_FLAG) { /* Start-of-frame */ //if (sof_handler) sof_handler(); { ClearUsbInterruptFlag(USB_SOF); } } else { // process all pending transactions while (USB_TRANSACTION_FLAG) { usb_handle_transaction(); ClearUsbInterruptFlag(USB_TRN); } // Side effect: advance USTAT Fifo } }
void usb_handle_reset(void) { int i; mLedToggle(); #ifdef USB_SELF_POWERED // JTR see note about this in usb_init() usb_device_status = 0x0001; #else usb_device_status = 0x0000; #endif do { ClearUsbInterruptFlag(USB_TRN); // JTR corrected Must poll TRN Flag and clear, then wait 6 cycles. for next flag set. usb_current_cfg = 0; usb_device_state = 0x00; // This creates the requied 6 cycle delay for TRNF to reassert. usb_addr_pending = 0x00; } while (USB_TRANSACTION_FLAG); // for (i=0; i < MAX_CHIP_EP; i++ ) // JTR this loop seems to work proving that the USB_UEP ptr is now correct. // { // USB_UEP[i] = 0; // JTR Char on PIC18 and int ptr on PIC24 // } USB_UEP0 = 0; // Disable all endpoints USB_UEP1 = 0; USB_UEP2 = 0; USB_UEP3 = 0; USB_UEP4 = 0; USB_UEP5 = 0; USB_UEP6 = 0; USB_UEP7 = 0; #if MAX_CHIP_EP > 8 // JTR alteration USB_UEP8 = 0; USB_UEP9 = 0; USB_UEP10 = 0; USB_UEP11 = 0; USB_UEP12 = 0; USB_UEP13 = 0; USB_UEP14 = 0; USB_UEP15 = 0; #endif SetUsbAddress(0); // After reset we don't have an address ClearAllUsbInterruptFlags(); ClearAllUsbErrorInterruptFlags(); // JTR added. Clear all BD STAT registers so that they are owned by the CPU and it is safe to change them. for (i = 0; i < (2 + 2 * MAX_EPNUM_USED); i += 4) { usb_bdt[i].BDSTAT = 0; } // USB_UEP[0] = endpoints[0].type; // JTR removed all such indexing on UEPx USB_UEP0 = USB_EP_CONTROL; // JTR hard coded instead. /* Configure buffer descriptors */ #if USB_PP_BUF_MODE == 0 usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; //endpoints[0].buffer_size; usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDADDR = usb_ep0_out_buf; //endpoints[0].out_buffer; usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDSTAT = UOWN + DTSEN; usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDCNT = 0; usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDADDR = usb_ep0_in_buf; //endpoints[0].in_buffer; usb_bdt[USB_CALC_BD(0, USB_DIR_IN, USB_PP_EVEN)].BDSTAT = DTS; // Set DTS => First packet inverts, ie. is Data0 #else // TODO: Implement Ping-Pong buffering setup. #error "PP Mode not implemented yet" #endif EnablePacketTransfer(); }