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 }
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 }