/****************************************************************************** Function: void USBCheckMSDRequest(void) Summary: This routine handles MSD specific request that happen on EP0. This function should be called from the USBCBCheckOtherReq() call back function whenever implementing an MSD device. Description: This routine handles MSD specific request that happen on EP0. These include, but are not limited to, the standard RESET and GET_MAX_LUN command requests. This function should be called from the USBCBCheckOtherReq() call back function whenever using an MSD device. Typical Usage: <code> void USBCBCheckOtherReq(void) { //Since the stack didn't handle the request I need to check // my class drivers to see if it is for them USBCheckMSDRequest(); } </code> PreCondition: None Parameters: None Return Values: None Remarks: None *****************************************************************************/ void USBCheckMSDRequest(void) { if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; if(SetupPkt.bIntfID != MSD_INTF_ID) return; switch(SetupPkt.bRequest) { case MSD_RESET: //Host would typically issue this after a STALL event on an MSD //bulk endpoint. The MSD reset should re-initialize status //so as to prepare for a new CBW. Any currently ongoing command //block should be aborted, but the STALL and DTS states need to be //maintained (host will re-initialize these seperately using //CLEAR_FEATURE, endpoint halt). MSD_State = MSD_WAIT; MSDCommandState = MSD_COMMAND_WAIT; MSDReadState = MSD_READ10_WAIT; MSDWriteState = MSD_WRITE10_WAIT; break; case GET_MAX_LUN: //If the host asks for the maximum number of logical units // then send out a packet with that information CtrlTrfData[0] = MAX_LUN; USBEP0SendRAMPtr((BYTE*)&CtrlTrfData[0],1,USB_EP0_INCLUDE_ZERO); break; } //end switch(SetupPkt.bRequest) }
/****************************************************************************** Function: void USBCheckMSDRequest(void) Summary: This routine handles MSD specific request that happen on EP0. This function should be called from the USBCBCheckOtherReq() call back function whenever implementing an MSD device. Description: This routine handles MSD specific request that happen on EP0. These include, but are not limited to, the standard RESET and GET_MAX_LUN command requests. This function should be called from the USBCBCheckOtherReq() call back function whenever using an MSD device. Typical Usage: <code> void USBCBCheckOtherReq(void) { //Since the stack didn't handle the request I need to check // my class drivers to see if it is for them USBCheckMSDRequest(); } </code> PreCondition: None Parameters: None Return Values: None Remarks: None *****************************************************************************/ void USBCheckMSDRequest(void) { if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; if(SetupPkt.bIntfID != MSD_INTF_ID) return; switch(SetupPkt.bRequest) { case MSD_RESET: break; case GET_MAX_LUN: //If the host asks for the maximum number of logical units // then send out a packet with that information CtrlTrfData[0] = MAX_LUN; USBEP0SendRAMPtr((BYTE*)&CtrlTrfData[0],1,USB_EP0_INCLUDE_ZERO); break; } //end switch(SetupPkt.bRequest) }
/****************************************************************************** Function: void USBCheckCDCRequest(void) Description: This routine checks the most recently received SETUP data packet to see if the request is specific to the CDC class. If the request was a CDC specific request, this function will take care of handling the request and responding appropriately. PreCondition: This function should only be called after a control transfer SETUP packet has arrived from the host. Parameters: None Return Values: None Remarks: This function does not change status or do anything if the SETUP packet did not contain a CDC class specific request. *****************************************************************************/ void USBCheckCDCRequest(void) { /* * If request recipient is not an interface then return */ if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; /* * If request type is not class-specific then return */ if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD) return; /* * Interface ID must match interface numbers associated with * CDC class, else return */ if((SetupPkt.bIntfID != CDC_COMM_INTF_ID)&& (SetupPkt.bIntfID != CDC_DATA_INTF_ID)) return; switch(SetupPkt.bRequest) { //****** These commands are required ******// case SEND_ENCAPSULATED_COMMAND: //send the packet inPipes[0].pSrc.bRam = (BYTE*)&dummy_encapsulated_cmd_response; inPipes[0].wCount.Val = dummy_length; inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; inPipes[0].info.bits.busy = 1; break; case GET_ENCAPSULATED_RESPONSE: // Populate dummy_encapsulated_cmd_response first. inPipes[0].pSrc.bRam = (BYTE*)&dummy_encapsulated_cmd_response; inPipes[0].info.bits.busy = 1; break; //****** End of required commands ******// #if defined(USB_CDC_SUPPORT_ABSTRACT_CONTROL_MANAGEMENT_CAPABILITIES_D1) case SET_LINE_CODING: outPipes[0].wCount.Val = SetupPkt.wLength; outPipes[0].pDst.bRam = (BYTE*)LINE_CODING_TARGET; outPipes[0].pFunc = LINE_CODING_PFUNC; outPipes[0].info.bits.busy = 1; break; case GET_LINE_CODING: USBEP0SendRAMPtr( (BYTE*)&line_coding, LINE_CODING_LENGTH, USB_EP0_INCLUDE_ZERO); break; case SET_CONTROL_LINE_STATE: control_signal_bitmap._byte = (BYTE)SetupPkt.W_Value.v[0]; //------------------------------------------------------------------ //One way to control the RTS pin is to allow the USB host to decide the value //that should be output on the RTS pin. Although RTS and CTS pin functions //are technically intended for UART hardware based flow control, some legacy //UART devices use the RTS pin like a "general purpose" output pin //from the PC host. In this usage model, the RTS pin is not related //to flow control for RX/TX. //In this scenario, the USB host would want to be able to control the RTS //pin, and the below line of code should be uncommented. //However, if the intention is to implement true RTS/CTS flow control //for the RX/TX pair, then this application firmware should override //the USB host's setting for RTS, and instead generate a real RTS signal, //based on the amount of remaining buffer space available for the //actual hardware UART of this microcontroller. In this case, the //below code should be left commented out, but instead RTS should be //controlled in the application firmware reponsible for operating the //hardware UART of this microcontroller. //--------- //CONFIGURE_RTS(control_signal_bitmap.CARRIER_CONTROL); //------------------------------------------------------------------ #if defined(USB_CDC_SUPPORT_DTR_SIGNALING) if(control_signal_bitmap.DTE_PRESENT == 1) { UART_DTR = USB_CDC_DTR_ACTIVE_LEVEL; } else { UART_DTR = (USB_CDC_DTR_ACTIVE_LEVEL ^ 1); } #endif inPipes[0].info.bits.busy = 1; break; #endif #if defined(USB_CDC_SUPPORT_ABSTRACT_CONTROL_MANAGEMENT_CAPABILITIES_D2) case SEND_BREAK: // Optional inPipes[0].info.bits.busy = 1; if (SetupPkt.wValue == 0xFFFF) //0xFFFF means send break indefinitely until a new SEND_BREAK command is received { UART_Tx = 0; // Prepare to drive TX low (for break signalling) UART_TRISTx = 0; // Make sure TX pin configured as an output UART_ENABLE = 0; // Turn off USART (to relinquish TX pin control) } else if (SetupPkt.wValue == 0x0000) //0x0000 means stop sending indefinite break { UART_ENABLE = 1; // turn on USART UART_TRISTx = 1; // Make TX pin an input } else { //Send break signalling on the pin for (SetupPkt.wValue) milliseconds UART_SEND_BREAK(); } break; #endif default: break; }//end switch(SetupPkt.bRequest) }//end USBCheckCDCRequest
void ChipKITUSBEP0SendRAMPtr(BYTE* src, WORD size, BYTE Options) { USBEP0SendRAMPtr(src, size, Options); }
/******************************************************************** Function: void USBCheckHIDRequest(void) Summary: This routine handles HID specific request that happen on EP0. This function should be called from the USBCBCheckOtherReq() call back function whenever implementing a HID device. Description: This routine handles HID specific request that happen on EP0. These include, but are not limited to, requests for the HID report descriptors. This function should be called from the USBCBCheckOtherReq() call back function whenever using an HID device. Typical Usage: <code> void USBCBCheckOtherReq(void) { //Since the stack didn't handle the request I need to check // my class drivers to see if it is for them USBCheckHIDRequest(); } </code> PreCondition: None Parameters: None Return Values: None Remarks: None *******************************************************************/ void USBCheckHIDRequest(void) { if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; if(SetupPkt.bIntfID != HID_INTF_ID) return; /* * There are two standard requests that hid.c may support. * 1. GET_DSC(DSC_HID,DSC_RPT,DSC_PHY); * 2. SET_DSC(DSC_HID,DSC_RPT,DSC_PHY); */ if(SetupPkt.bRequest == USB_REQUEST_GET_DESCRIPTOR) { switch(SetupPkt.bDescriptorType) { case DSC_HID: if(USBActiveConfiguration == 1) { USBEP0SendROMPtr( (ROM BYTE*)&configDescriptor1 + 18, sizeof(USB_HID_DSC)+3, USB_EP0_INCLUDE_ZERO); } break; case DSC_RPT: if(USBActiveConfiguration == 1) { USBEP0SendROMPtr( (ROM BYTE*)&hid_rpt01, sizeof(hid_rpt01), //See usbcfg.h USB_EP0_INCLUDE_ZERO); } break; case DSC_PHY: USBEP0Transmit(USB_EP0_NO_DATA); break; }//end switch(SetupPkt.bDescriptorType) }//end if(SetupPkt.bRequest == GET_DSC) if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD) { return; } switch(SetupPkt.bRequest) { case GET_REPORT: #if defined USER_GET_REPORT_HANDLER USER_GET_REPORT_HANDLER(); #endif break; case SET_REPORT: #if defined USER_SET_REPORT_HANDLER USER_SET_REPORT_HANDLER(); #endif break; case GET_IDLE: USBEP0SendRAMPtr( (BYTE*)&idle_rate, 1, USB_EP0_INCLUDE_ZERO); break; case SET_IDLE: USBEP0Transmit(USB_EP0_NO_DATA); idle_rate = SetupPkt.W_Value.byte.HB; break; case GET_PROTOCOL: USBEP0SendRAMPtr( (BYTE*)&active_protocol, 1, USB_EP0_NO_OPTIONS); break; case SET_PROTOCOL: USBEP0Transmit(USB_EP0_NO_DATA); active_protocol = SetupPkt.W_Value.byte.LB; break; }//end switch(SetupPkt.bRequest) }//end USBCheckHIDRequest
/******************************************************************** Function: void USBCheckHIDRequest(void) Summary: This routine handles HID specific request that happen on EP0. This function should be called from the USBCBCheckOtherReq() call back function whenever implementing a HID device. Description: This routine handles HID specific request that happen on EP0. These include, but are not limited to, requests for the HID report descriptors. This function should be called from the USBCBCheckOtherReq() call back function whenever using an HID device. Typical Usage: <code> void USBCBCheckOtherReq(void) { //Since the stack didn't handle the request I need to check // my class drivers to see if it is for them USBCheckHIDRequest(); } </code> PreCondition: None Parameters: None Return Values: None Remarks: None *******************************************************************/ void USBCheckHIDRequest(void) { if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; if(SetupPkt.bIntfID != HID_INTF_ID) return; /* * There are two standard requests that hid.c may support. * 1. GET_DSC(DSC_HID,DSC_RPT,DSC_PHY); * 2. SET_DSC(DSC_HID,DSC_RPT,DSC_PHY); */ if(SetupPkt.bRequest == USB_REQUEST_GET_DESCRIPTOR) { switch(SetupPkt.bDescriptorType) { case DSC_HID: //HID Descriptor if(USBActiveConfiguration == 1) { USBEP0SendROMPtr( (ROM BYTE*)&configDescriptor1 + 18, //18 is a magic number. It is the offset from start of the configuration descriptor to the start of the HID descriptor. sizeof(USB_HID_DSC)+3, USB_EP0_INCLUDE_ZERO); } break; case DSC_RPT: //Report Descriptor //if(USBActiveConfiguration == 1) { USBEP0SendROMPtr( (ROM BYTE*)&hid_rpt01, HID_RPT01_SIZE, //See usbcfg.h USB_EP0_INCLUDE_ZERO); } break; case DSC_PHY: //Physical Descriptor //Note: The below placeholder code is commented out. HID Physical Descriptors are optional and are not used //in many types of HID applications. If an application does not have a physical descriptor, //then the device should return STALL in response to this request (stack will do this automatically //if no-one claims ownership of the control transfer). //If an application does implement a physical descriptor, then make sure to declare //hid_phy01 (rom structure containing the descriptor data), and hid_phy01 (the size of the descriptors in bytes), //and then uncomment the below code. //if(USBActiveConfiguration == 1) //{ // USBEP0SendROMPtr((ROM BYTE*)&hid_phy01, sizeof(hid_phy01), USB_EP0_INCLUDE_ZERO); //} break; }//end switch(SetupPkt.bDescriptorType) }//end if(SetupPkt.bRequest == GET_DSC) if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD) { return; } switch(SetupPkt.bRequest) { case GET_REPORT: #if defined USER_GET_REPORT_HANDLER USER_GET_REPORT_HANDLER(); #endif break; case SET_REPORT: #if defined USER_SET_REPORT_HANDLER USER_SET_REPORT_HANDLER(); #endif break; case GET_IDLE: USBEP0SendRAMPtr( (BYTE*)&idle_rate, 1, USB_EP0_INCLUDE_ZERO); break; case SET_IDLE: USBEP0Transmit(USB_EP0_NO_DATA); idle_rate = SetupPkt.W_Value.byte.HB; break; case GET_PROTOCOL: USBEP0SendRAMPtr( (BYTE*)&active_protocol, 1, USB_EP0_NO_OPTIONS); break; case SET_PROTOCOL: USBEP0Transmit(USB_EP0_NO_DATA); active_protocol = SetupPkt.W_Value.byte.LB; break; }//end switch(SetupPkt.bRequest) }//end USBCheckHIDRequest
/****************************************************************************** Function: void USBCheckCDCRequest(void) Description: This routine checks the setup data packet to see if it knows how to handle it PreCondition: None Parameters: None Return Values: None Remarks: None *****************************************************************************/ void USBCheckCDCRequest(void) { /* * If request recipient is not an interface then return */ if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; /* * If request type is not class-specific then return */ if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD) return; /* * Interface ID must match interface numbers associated with * CDC class, else return */ if((SetupPkt.bIntfID != CDC_COMM_INTF_ID)&& (SetupPkt.bIntfID != CDC_DATA_INTF_ID)) return; switch(SetupPkt.bRequest) { //****** These commands are required ******// case SEND_ENCAPSULATED_COMMAND: //send the packet inPipes[0].pSrc.bRam = (BYTE*)&dummy_encapsulated_cmd_response; inPipes[0].wCount.Val = dummy_length; inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; inPipes[0].info.bits.busy = 1; break; case GET_ENCAPSULATED_RESPONSE: // Populate dummy_encapsulated_cmd_response first. inPipes[0].pSrc.bRam = (BYTE*)&dummy_encapsulated_cmd_response; inPipes[0].info.bits.busy = 1; break; //****** End of required commands ******// #if defined(USB_CDC_SUPPORT_ABSTRACT_CONTROL_MANAGEMENT_CAPABILITIES_D1) case SET_LINE_CODING: outPipes[0].wCount.Val = SetupPkt.wLength; outPipes[0].pDst.bRam = (BYTE*)LINE_CODING_TARGET; outPipes[0].pFunc = LINE_CODING_PFUNC; outPipes[0].info.bits.busy = 1; break; case GET_LINE_CODING: USBEP0SendRAMPtr( (BYTE*)&line_coding, LINE_CODING_LENGTH, USB_EP0_INCLUDE_ZERO); break; case SET_CONTROL_LINE_STATE: control_signal_bitmap._byte = (BYTE)SetupPkt.W_Value.v[0]; CONFIGURE_RTS(control_signal_bitmap.CARRIER_CONTROL); CONFIGURE_DTR(control_signal_bitmap.DTE_PRESENT); inPipes[0].info.bits.busy = 1; break; #endif #if defined(USB_CDC_SUPPORT_ABSTRACT_CONTROL_MANAGEMENT_CAPABILITIES_D2) case SEND_BREAK: // Optional inPipes[0].info.bits.busy = 1; if (SetupPkt.wValue == 0xFFFF) { UART_ENABLE = 0; // turn off USART UART_TRISTx = 0; // Make TX pin an output UART_Tx = 0; // make it low } else if (SetupPkt.wValue == 0x0000) { UART_ENABLE = 1; // turn on USART UART_TRISTx = 1; // Make TX pin an input } else { UART_SEND_BREAK(); } break; #endif default: break; }//end switch(SetupPkt.bRequest) }//end USBCheckCDCRequest
/******************************************************************** * Function: void UserGetReportHandler(void) * PreCondition: None * Input: None * Output: None * Side Effects: If either the USBEP0SendRAMPtr() or USBEP0Transmit() * functions are not called in this function then the * requesting GET_REPORT will be STALLed * * Overview: This function is called by the HID function driver * in response to a GET_REPORT command. * * Note: This function is called from the USB stack in * response to a class specific control transfer request * arriving over EP0. Therefore, this function executes in the * same context as the USBDeviceTasks() function executes (which * may be an interrupt handler, or a main loop context function, * depending upon usb_config.h options). * If the firmware needs more time to process the request, * here would be a good place to use the USBDeferStatusStage() * USB stack API function. *******************************************************************/ void UserGetReportHandler(void) { USBEP0SendRAMPtr((BYTE*) & hid_send_buf, USB_EP0_BUFF_SIZE, USB_EP0_NO_OPTIONS); }