Beispiel #1
0
void usb_handle_setup(void) {
    rbdp->BDSTAT = DTSEN; // Reclaim reply buffer
    switch (bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_TypeMask) {
        case USB_bmRequestType_Standard:
            switch (bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_RecipientMask) {
                case USB_bmRequestType_Device:
                    usb_handle_StandardDeviceRequest(bdp);
                    break;
                case USB_bmRequestType_Interface:
                    usb_handle_StandardInterfaceRequest(bdp);
                    break;
                case USB_bmRequestType_Endpoint:
                    usb_handle_StandardEndpointRequest(bdp);
                    break;
                default:
                    usb_RequestError();
            }
            break;
        case USB_bmRequestType_Class:
            if (class_setup_handler) class_setup_handler();
            break;
        case USB_bmRequestType_Vendor:
            if (vendor_setup_handler) class_setup_handler();
            break;
        default:
            usb_RequestError();
    }
    /* Prepare endpoint for new reception */

    bdp->BDCNT = USB_EP0_BUFFER_SIZE; // Size  of EP0, should always be ep0? (JTR in practice YES)

    // JTR, is the next OUT transfer to be a setup packet (DAT0) or a DATA packet (DAT1)?
    // note that this is not an entirely robust way of doing things. See the microchip stack for
    // further comments and a better system as this does not account for errors and retries.

    // JTR Breakdown of what is happening here FYI.

    //bdp->BDSTAT = (!(bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) &&
    // JTR meaning. If transfer was a CONTROL OUT then B7 (USB_bmRequestType_PhaseMask) of bmRequestType is '0'

    // &&

    //bdp->BDADDR[USB_wLength] || bdp->BDADDR[USB_wLengthHigh]))? UOWN + DTS + DTSEN : UOWN + DTSEN;
    // If there is a CONTROL OUT DATA PACKET to follow then its count in (int) wLength will be != 0
    // When both conditions are true then set for DAT1 (UOWN + DTS + DTSEN)
    // else set for DAT0 (UOWN + DTSEN)

    // See USB 2.0 spec 8.5.3

    bdp->BDSTAT = (!(bdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) &&
            (bdp->BDADDR[USB_wLength] || bdp->BDADDR[USB_wLengthHigh])) ? UOWN + DTS + DTSEN : UOWN + DTSEN;

    // JTR Note. For rbdp after a setup packet, the standard or class request handler will force the correct DTS state
    // JTR Note. that CONTROL IN and OUT DATA packet transfers do not come back here and there is no
    // univesal way and place of setting up EP0 after these DATA transfers in this stack.

    // JTR Note. that there is a PIC18 silicon errata issue that this does not address by being here.  See DS80220F-page 6
    EnablePacketTransfer();
}
Beispiel #2
0
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();
}
Beispiel #3
0
void usb_handle_setup(void) {

    EP0_Inbdp->BDSTAT = DTSEN; // Reclaim reply buffer

    EnablePacketTransfer(); // JTR this is placed here to overcome a errate issue with early PIC18 USB pics.

    switch (EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_TypeMask) {
        case USB_bmRequestType_Standard:
            switch (EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_RecipientMask) {
                case USB_bmRequestType_Device:
                    usb_handle_StandardDeviceRequest(EP0_Outbdp);
                    break;
                case USB_bmRequestType_Interface:
                    usb_handle_StandardInterfaceRequest(EP0_Outbdp);
                    break;
                case USB_bmRequestType_Endpoint:
                    usb_handle_StandardEndpointRequest(EP0_Outbdp);
                    break;
                default:
                    usb_RequestError();
            }
            break;
        case USB_bmRequestType_Class:
            if (class_setup_handler) class_setup_handler();
            break;
        case USB_bmRequestType_Vendor:
			//ROBOTS FIX: http://dangerousprototypes.com/forum/viewtopic.php?f=39&t=3849&view=unread#unread
			// did call class_setup_handler();
            if (vendor_setup_handler) vendor_setup_handler();
            break;
        default:
            usb_RequestError();
    }
    /* Prepare endpoint for new reception */

    EP0_Outbdp->BDCNT = USB_EP0_BUFFER_SIZE; // Size  of EP0, should always be ep0?

    // JTR, is the next OUT transfer to be a setup packet (DAT0) or a DATA packet (DAT1)?
    // note that this is not an entirely robust way of doing things. See the microchip stack for
    // further comments and a better system as this does not account for errors and retries
    // and it results in the SIE not accepting the final out ZLP status packet for IN transfers
    // with a data stage. However it works but it is not anything to be proud of...

    EP0_Outbdp->BDSTAT = (!(EP0_Outbdp->BDADDR[USB_bmRequestType] & USB_bmRequestType_PhaseMask) &&
            (EP0_Outbdp->BDADDR[USB_wLength] || EP0_Outbdp->BDADDR[USB_wLengthHigh])) ? UOWN + DTS + DTSEN : UOWN + DTSEN;
}
Beispiel #4
0
void usb_init(ROMPTR const BYTE *device_descriptor,
        ROMPTR const BYTE *config_descriptor,
        ROMPTR const BYTE *string_descriptor,
        int num_string_descriptors) {

    usb_device_descriptor = device_descriptor;
    usb_config_descriptor = config_descriptor;
    usb_string_descriptor = string_descriptor;
    usb_num_string_descriptors = num_string_descriptors;
    sof_handler = NULL;
    class_setup_handler = NULL;
    vendor_setup_handler = NULL;
    usb_unset_in_handler(0);
    usb_unset_out_handler(0);
    ClearUSBtoDefault();
    ConfigureUsbHardware();
    EnablePacketTransfer();
}
Beispiel #5
0
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();
}