示例#1
0
文件: usb_stack.c 项目: Xykon/pic-dev
void usb_set_address(void) {
    if (0x00u == usb_addr_pending) {
        usb_device_state = DEFAULT_STATE;
    } else {
        usb_device_state = ADDRESS_STATE;
    }
    SetUsbAddress(usb_addr_pending);
    usb_addr_pending = 0xFF;
    usb_unset_in_handler(0); // Unregister handler
}
示例#2
0
void usb_set_address(void) {
    //	if (0x00u <= usb_addr_pending && 0x80u > usb_addr_pending) {   // JTR changed. The USB specs allow for a device to be de-addressed
    if (0x80u > usb_addr_pending) {
        SetUsbAddress(usb_addr_pending);
        usb_addr_pending = 0xFF;
    }
    // JTR TODO: If we are going to bounds check the pending address
    // (again) and it is out of bounds we should return a
    // protocol stall here.
    usb_unset_in_handler(0); // Unregister handler
}
示例#3
0
文件: usb_stack.c 项目: Xykon/pic-dev
void ClearUSBtoDefault(void) {
    int i;
    sof_handler = NULL;
    class_setup_handler = NULL;
    vendor_setup_handler = NULL;

    SetUsbAddress(0); // After reset we don't have an address
    ResetPPbuffers();
    ClearAllUsbErrorInterruptFlags();

    for (i = 0; i < MAX_CHIP_EP; i++) {
        endpoints[i].out_handler = NULL;
        endpoints[i].in_handler = NULL;
        USB_UEP[i] = 0;
    }

    for (i = 0; i < (2 + 2 * MAX_EPNUM_USED); i++) {
        usb_bdt[i].BDSTAT = 0;
    }

    USB_UEP0 = USB_EP_CONTROL; // Configure Only ep0 At this point.
    //usbrequesterrorflag = 0;

#ifdef USB_SELF_POWERED

    // JTR TODO this isn't actually 100% correct. "usb_device_status" is a runtime variable
    // In the case of a bus powered device it will always be 0x000 but in the case of
    // self powered with bus powered option it becames variable to the current powered
    // State. This is a minor thing and for now but it may need to be addressed if there is
    // any hardware that is dual powered.

    usb_device_status = 0x0001;
#else
    usb_device_status = 0x0000;
#endif
    usb_device_state = DETACHED_STATE; // JTR added flag byte for enumeration state
    usb_current_cfg = 0; // JTR formally usb_configured
    usb_addr_pending = 0x00;

#if USB_PP_BUF_MODE == NO_PINGPONG
    usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; // JTR endpoints[0].buffer_size; same thing done more obviously
    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 + DTSEN; // Set DTS => First packet inverts, ie. is Data0
#else
#error "Invalid PING_PONG mode"
#endif
}
示例#4
0
void usb_init(ROMPTR const unsigned char *device_descriptor,
        ROMPTR const unsigned char *config_descriptor,
        ROMPTR const unsigned char *string_descriptor,
        int num_string_descriptors) {
    int i;


    usb_device_descriptor = device_descriptor;
    usb_config_descriptor = config_descriptor;
    usb_string_descriptor = string_descriptor;
    usb_num_string_descriptors = num_string_descriptors;

    SetUsbAddress(0); // JTR added here. Probably not required though
    ResetPPbuffers();
    DisableUsbInterrupts();
    DisableAllUsbInterrupts();
    ClearAllUsbErrorInterruptFlags();
    ClearAllUsbInterruptFlags();
    ConfigureUsbHardware();

    sof_handler = NULL;
    class_setup_handler = NULL;
    vendor_setup_handler = NULL;

    for (i = 0; i < MAX_CHIP_EP; i++) {
        endpoints[i].out_handler = NULL;
        endpoints[i].in_handler = NULL;
    }
    // Register ep0 - no handlers

    usb_unset_in_handler(0);
    usb_unset_out_handler(0);

    // JTR All UEPx SPRs are hard coded. I cannot see much point of tables or indexing for these.
    // On the PIC16C765 such indexing was required for the STALL bit but now the STALL feature
    // is in the Buffer descriptor table not in the UEPx SPRs.
    // See changes to "USB_REQUEST_GET_STATUS"

    //	USB_UEP[0] = endpoints[0].type;

    USB_UEP0 = USB_EP_CONTROL;
    /* Configure endpoints TODO: Only ep0 ? */
    // JTR Right! code for other end points snipped...At this point we are only setting up EP0

#ifdef USB_SELF_POWERED
    usb_device_status = 0x0001;
#else
    usb_device_status = 0x0000;
#endif
    usb_device_state = 0x00; // JTR added flag byte for enumeration state
    usb_current_cfg = 0; // JTR formally usb_configured
    usb_addr_pending = 0x00;

#if USB_PP_BUF_MODE == 0
    usb_bdt[USB_CALC_BD(0, USB_DIR_OUT, USB_PP_EVEN)].BDCNT = USB_EP0_BUFFER_SIZE; // JTR endpoints[0].buffer_size; same thing done more obviously
    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
}
示例#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();
}