USB_DEVICE_CDC_EVENT_RESPONSE APP_USBDeviceCDCEventHandler ( USB_DEVICE_CDC_INDEX index , USB_DEVICE_CDC_EVENT event , void * pData, uintptr_t userData ) { APP2_DATA * app2DataObject; app2DataObject = (APP2_DATA *)userData; USB_CDC_CONTROL_LINE_STATE * controlLineStateData; switch ( event ) { case USB_DEVICE_CDC_EVENT_GET_LINE_CODING: /* This means the host wants to know the current line * coding. This is a control transfer request. Use the * USB_DEVICE_ControlSend() function to send the data to * host. */ USB_DEVICE_ControlSend(app2DataObject->deviceHandle, &app2DataObject->getLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_LINE_CODING: /* This means the host wants to set the line coding. * This is a control transfer request. Use the * USB_DEVICE_ControlReceive() function to receive the * data from the host */ USB_DEVICE_ControlReceive(app2DataObject->deviceHandle, &app2DataObject->setLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_CONTROL_LINE_STATE: /* This means the host is setting the control line state. * Read the control line state. We will accept this request * for now. */ controlLineStateData = (USB_CDC_CONTROL_LINE_STATE *)pData; app2DataObject->controlLineStateData.dtr = controlLineStateData->dtr; app2DataObject->controlLineStateData.carrier = controlLineStateData->carrier; USB_DEVICE_ControlStatus(app2DataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_SEND_BREAK: /* This means that the host is requesting that a break of the * specified duration be sent. Read the break duration */ app2DataObject->breakData = ((USB_DEVICE_CDC_EVENT_DATA_SEND_BREAK *)pData)->breakDuration; /* Complete the control transfer by sending a ZLP */ USB_DEVICE_ControlStatus(app2DataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_READ_COMPLETE: /* This means that the host has sent some data*/ //SYS_CONSOLE_MESSAGE("Read flag set\n"); app2DataObject->isReadComplete = true; break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: /* The data stage of the last control transfer is * complete. For now we accept all the data */ USB_DEVICE_ControlStatus(app2DataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_SENT: /* This means the GET LINE CODING function data is valid. We dont * do much with this data in this demo. */ break; case USB_DEVICE_CDC_EVENT_WRITE_COMPLETE: /* This means that the data write got completed. We can schedule * the next read. */ app2DataObject->isWriteComplete = true; break; default: break; } return USB_DEVICE_CDC_EVENT_RESPONSE_NONE; }
void APP_USBDeviceHIDEventHandler(USB_DEVICE_HID_INDEX hidInstance, USB_DEVICE_HID_EVENT event, void * eventData, uintptr_t userData) { APP_DATA * appData = (APP_DATA *)userData; switch(event) { case USB_DEVICE_HID_EVENT_REPORT_SENT: /* This means the mouse report was sent. We are free to send another report */ appData->isMouseReportSendBusy = false; break; case USB_DEVICE_HID_EVENT_REPORT_RECEIVED: /* Dont care for other event in this demo */ break; case USB_DEVICE_HID_EVENT_SET_IDLE: /* Acknowledge the Control Write Transfer */ USB_DEVICE_ControlStatus(appData->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); /* save Idle rate received from Host */ appData->idleRate = ((USB_DEVICE_HID_EVENT_DATA_SET_IDLE*)eventData)->duration; break; case USB_DEVICE_HID_EVENT_GET_IDLE: /* Host is requesting for Idle rate. Now send the Idle rate */ USB_DEVICE_ControlSend(appData->deviceHandle, &(appData->idleRate),1); /* On successfully receiving Idle rate, the Host would acknowledge back with a Zero Length packet. The HID function driver returns an event USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT to the application upon receiving this Zero Length packet from Host. USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT event indicates this control transfer event is complete */ break; case USB_DEVICE_HID_EVENT_SET_PROTOCOL: /* Host is trying set protocol. Now receive the protocol and save */ appData->activeProtocol = *(USB_HID_PROTOCOL_CODE *)eventData; /* Acknowledge the Control Write Transfer */ USB_DEVICE_ControlStatus(appData->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_HID_EVENT_GET_PROTOCOL: /* Host is requesting for Current Protocol. Now send the Idle rate */ USB_DEVICE_ControlSend(appData->deviceHandle, &(appData->activeProtocol), 1); /* On successfully receiving Idle rate, the Host would acknowledge back with a Zero Length packet. The HID function driver returns an event USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT to the application upon receiving this Zero Length packet from Host. USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT event indicates this control transfer event is complete */ break; case USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT: break; default: break; } }
/* Function: void _USB_DEVICE_AUDIO_SetupPacketHandler ( USB_DEVICE_AUDIO_INDEX iAudio , USB_SETUP_PACKET * setupPkt ) Summary: Handles a fresh SETUP packet received from Host. Description: Handles a fresh SETUP packet received from Host. Returns: This is a local function and should not be called directly by the application. */ void _USB_DEVICE_AUDIO_SetupPacketHandler ( USB_DEVICE_AUDIO_INDEX iAudio , USB_SETUP_PACKET * setupPkt ) { uint8_t audioControlInterfaceId; uint8_t interfaceId; uint8_t curAlternateSetting; uint8_t prevAlternateSetting; uint8_t streamIntfcIndex; uint8_t noOfEndpoints; USB_ENDPOINT ep; uint16_t maxPacketSize; USB_DEVICE_HANDLE usbDeviceHandle; USB_DEVICE_AUDIO_EVENT event; USB_DEVICE_AUDIO_EVENT_DATA_INTERFACE_SETTING_CHANGED interfaceInfo; USB_ERROR endpointEnableResult; USB_DEVICE_AUDIO_STREAMING_INTERFACE_ALTERNATE_SETTING *pCurAlternateStng; USB_DEVICE_AUDIO_STREAMING_INTERFACE_ALTERNATE_SETTING *pPrevAlternateStng; USB_DEVICE_AUDIO_STREAMING_INTERFACE *pStreamingInterface; uint16_t adjustedMaxPacketSize =1; /* Obtain pointer to the Audio Instance that is being addressed*/ USB_DEVICE_AUDIO_INSTANCE* audioInstance = &gUsbDeviceAudioInstance[iAudio]; /* Obtain pointer to the Audio Interface collection*/ USB_DEVICE_AUDIO_INTERFACE_COLLECTION *curInfCollection = &(audioInstance->infCollection); /* Get the Device Layer handle */ usbDeviceHandle = gUsbDeviceAudioInstance[iAudio].devLayerHandle; /* Check if the request is a standard interface request*/ if ( (setupPkt->RequestType == USB_SETUP_REQUEST_TYPE_STANDARD) && (setupPkt->Recipient == USB_SETUP_REQUEST_RECIPIENT_INTERFACE)) { /* We have received Standard Set request */ /* retrieve interface number from the Setup packet */ interfaceId = setupPkt->bIntfID; /* retrieve audio Control interface number*/ audioControlInterfaceId = curInfCollection->bControlInterfaceNum; switch(setupPkt->bRequest) { case USB_REQUEST_SET_INTERFACE: curAlternateSetting = setupPkt->bAltID; if (interfaceId == audioControlInterfaceId) { /*SET INTERFACE command was received to Audio Control Interface */ curInfCollection->bControlAltSetng = curAlternateSetting; } else { /*An Audio Streaming interface has received SET INTERFACE command */ streamIntfcIndex = interfaceId - audioControlInterfaceId - 1; /* Get pointer to the current audio streaming interface */ pStreamingInterface = &(curInfCollection->streamInf[streamIntfcIndex]); /* Get pointer to the Interface Alternate setting. */ pCurAlternateStng = &(pStreamingInterface->alterntSetting[curAlternateSetting]); /* Find out how many endpoint are present for this Interface * Alternate setting */ noOfEndpoints = pCurAlternateStng->numEndPoints; if ((noOfEndpoints) && (curAlternateSetting)) { /* We have to enable the endpoint only if this alternate setting has at least one endpoint and the alternate setting is a non zero value */ /*Retrieve endpoint size */ ep = pCurAlternateStng->isoDataEp.epAddr; /* retrieve max packet size. */ maxPacketSize = pCurAlternateStng->isoDataEp.epMaxPacketSize; /* maxpacket size should be adjusted to upper power of two. As per the PIC32MZ USB module requirement.*/ if (maxPacketSize) { while(adjustedMaxPacketSize < maxPacketSize) adjustedMaxPacketSize <<= 1; } else adjustedMaxPacketSize = 0; /* Enable Isochronous Data Endpoint */ endpointEnableResult = USB_DEVICE_EndpointEnable ( usbDeviceHandle , 0, ep , USB_TRANSFER_TYPE_ISOCHRONOUS , adjustedMaxPacketSize ); if (endpointEnableResult != USB_ERROR_NONE) { SYS_ASSERT (false, "Endpoint not Enabled"); } if (noOfEndpoints == 2) { /* If number of Endpoints is Two, then it is sure * that this alternate setting reports a Isochronous * Sync Endpoint. Enable the Sync Endpoint. */ } //Change Audio Instance object state to Initialized. pStreamingInterface->state = USB_DEVICE_AUDIO_STRMNG_INTFC_INITIALIZED; } else //alternateSetting = 0 { if (pStreamingInterface->state == USB_DEVICE_AUDIO_STRMNG_INTFC_INITIALIZED) { //Disable the Endpoint in the previous Active Alternate setting. prevAlternateSetting = pStreamingInterface->activeSetting; //get a pointer to the previous alternate setting. pPrevAlternateStng = &(pStreamingInterface->alterntSetting[prevAlternateSetting]); //Get endpoint no of the previous alternate setting ep = pPrevAlternateStng->isoDataEp.epAddr; //Disable the Endpoint USB_DEVICE_EndpointDisable(usbDeviceHandle ,ep); if (noOfEndpoints == 2) { /* If number of Endpoints is Two, then it is sure * that this alternate setting reports a Isochronous * Sync Endpoint. Disable the Sync Endpoint. */ } curInfCollection->streamInf[streamIntfcIndex].state = USB_DEVICE_AUDIO_STRMNG_INTFC_NOT_INITIALIZED; USB_DEVICE_IRPCancelAll(usbDeviceHandle, ep); } } /* Remember the new alternate setting received */ curInfCollection->streamInf[streamIntfcIndex].activeSetting = curAlternateSetting; /* Notify application about the SET_INTERFACE request */ interfaceInfo.interfaceNumber = interfaceId; interfaceInfo.interfaceAlternateSetting = curAlternateSetting; audioInstance->appEventCallBack ( iAudio, USB_DEVICE_AUDIO_EVENT_INTERFACE_SETTING_CHANGED, &interfaceInfo, 0 ); } /* Send an Acknowledgement to the Host */ USB_DEVICE_ControlStatus( usbDeviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_REQUEST_GET_INTERFACE: if (interfaceId == audioControlInterfaceId) { curAlternateSetting = curInfCollection->bControlAltSetng; } else { streamIntfcIndex = interfaceId - audioControlInterfaceId - 1; curAlternateSetting = curInfCollection->streamInf[streamIntfcIndex].activeSetting; } USB_DEVICE_ControlSend( usbDeviceHandle, (void *)&curAlternateSetting, 1); break; default: break; } }//end of if((setupPkt->bmRequestType == else if ( (setupPkt->RequestType == USB_SETUP_REQUEST_TYPE_CLASS ) && (setupPkt->Recipient == USB_SETUP_REQUEST_RECIPIENT_INTERFACE)) { /* We have recived a Audio Class specific Interface request */ switch (setupPkt->bRequest) { case USB_AUDIO_CS_SET_CUR: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_CUR; break; case USB_AUDIO_CS_GET_CUR: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_CUR; break; case USB_AUDIO_CS_SET_MIN: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_MIN; break; case USB_AUDIO_CS_GET_MIN: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_MIN; break; case USB_AUDIO_CS_SET_MAX: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_MAX; break; case USB_AUDIO_CS_GET_MAX: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_MAX; break; case USB_AUDIO_CS_SET_RES: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_RES; break; case USB_AUDIO_CS_GET_RES: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_RES; break; case USB_AUDIO_CS_SET_MEM: event = USB_DEVICE_AUDIO_EVENT_ENTITY_SET_MEM; break; case USB_AUDIO_CS_GET_MEM: event = USB_DEVICE_AUDIO_EVENT_ENTITY_GET_MEM; break; case USB_AUDIO_CS_GET_STAT: event = USB_DEVICE_AUDIO_EVENT_ENTITY_GET_STAT; break; default: /* Unknown request. Stall the request */ event = 0; USB_DEVICE_ControlStatus(audioInstance->devLayerHandle, USB_DEVICE_CONTROL_STATUS_ERROR); } if (( audioInstance->appEventCallBack) && (event)) { /* inform the application about the request */ /* the application needs to handle both EP and entity specific requests */ audioInstance->appEventCallBack ( iAudio, event, setupPkt, 0); } }// end of else if }//end of function _USB_DEVICE_AUDIO_SetupPacketHandler
USB_DEVICE_CDC_EVENT_RESPONSE APP_USBDeviceCDCEventHandler ( USB_DEVICE_CDC_INDEX index , USB_DEVICE_CDC_EVENT event , void* pData, uintptr_t userData ) { APP_DATA * appDataObject; appDataObject = (APP_DATA *)userData; USB_CDC_CONTROL_LINE_STATE * controlLineStateData; uint16_t * breakData; switch ( event ) { case USB_DEVICE_CDC_EVENT_GET_LINE_CODING: /* This means the host wants to know the current line * coding. This is a control transfer request. Use the * USB_DEVICE_ControlSend() function to send the data to * host. */ USB_DEVICE_ControlSend(appDataObject->deviceHandle, &appDataObject->getLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_LINE_CODING: /* This means the host wants to set the line coding. * This is a control transfer request. Use the * USB_DEVICE_ControlReceive() function to receive the * data from the host */ USB_DEVICE_ControlReceive(appDataObject->deviceHandle, &appDataObject->setLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_CONTROL_LINE_STATE: /* This means the host is setting the control line state. * Read the control line state. We will accept this request * for now. */ controlLineStateData = (USB_CDC_CONTROL_LINE_STATE *)pData; appDataObject->controlLineStateData.dtr = controlLineStateData->dtr; appDataObject->controlLineStateData.carrier = controlLineStateData->carrier; USB_DEVICE_ControlStatus(appDataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_SEND_BREAK: /* This means that the host is requesting that a break of the * specified duration be sent. Read the break duration */ breakData = (uint16_t *)pData; appDataObject->breakData = *breakData; break; case USB_DEVICE_CDC_EVENT_READ_COMPLETE: /* This means that the host has sent some data*/ appDataObject->isReadComplete = true; appDataObject->readLength = ((USB_DEVICE_CDC_EVENT_DATA_READ_COMPLETE*)pData)->length; break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: /* The data stage of the last control transfer is * complete. We have received the line coding from the host. Call * the usart driver routine to change the baud. We can call this * function here as it is not blocking. */ DRV_USART_BaudSet(appDataObject->usartHandle, appDataObject->setLineCodingData.dwDTERate); USB_DEVICE_ControlStatus(appDataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_SENT: /* This means the GET LINE CODING function data is valid. We dont * do much with this data in this demo. */ break; case USB_DEVICE_CDC_EVENT_WRITE_COMPLETE: /* This means that the data write got completed. We can schedule * the next read. */ break; default: break; } return USB_DEVICE_CDC_EVENT_RESPONSE_NONE; }
USB_DEVICE_HID_EVENT_RESPONSE APP_USBDeviceHIDEventHandler ( USB_DEVICE_HID_INDEX iHID, USB_DEVICE_HID_EVENT event, void * eventData, uintptr_t userData ) { USB_DEVICE_HID_EVENT_DATA_REPORT_SENT * reportSent; USB_DEVICE_HID_EVENT_DATA_REPORT_RECEIVED * reportReceived; /* Check type of event */ switch (event) { case USB_DEVICE_HID_EVENT_REPORT_SENT: /* The eventData parameter will be USB_DEVICE_HID_EVENT_REPORT_SENT * pointer type containing details about the report that was * sent. */ reportSent = (USB_DEVICE_HID_EVENT_DATA_REPORT_SENT *) eventData; if(reportSent->handle == appData.txTransferHandle ) { // Transfer progressed. appData.hidDataTransmitted = true; } break; case USB_DEVICE_HID_EVENT_REPORT_RECEIVED: /* The eventData parameter will be USB_DEVICE_HID_EVENT_REPORT_RECEIVED * pointer type containing details about the report that was * received. */ reportReceived = (USB_DEVICE_HID_EVENT_DATA_REPORT_RECEIVED *) eventData; if(reportReceived->handle == appData.rxTransferHandle ) { // Transfer progressed. appData.hidDataReceived = true; } break; case USB_DEVICE_HID_EVENT_SET_IDLE: /* For now we just accept this request as is. We acknowledge * this request using the USB_DEVICE_HID_ControlStatus() * function with a USB_DEVICE_CONTROL_STATUS_OK flag */ USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_OK); /* Save Idle rate recieved from Host */ appData.idleRate = ((USB_DEVICE_HID_EVENT_DATA_SET_IDLE*)eventData)->duration; break; case USB_DEVICE_HID_EVENT_GET_IDLE: /* Host is requesting for Idle rate. Now send the Idle rate */ USB_DEVICE_ControlSend(appData.usbDevHandle, & (appData.idleRate),1); /* On successfully reciveing Idle rate, the Host would acknowledge back with a Zero Length packet. The HID function drvier returns an event USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT to the application upon receiving this Zero Length packet from Host. USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT event indicates this control transfer event is complete */ break; default: // Nothing to do. break; } return USB_DEVICE_HID_EVENT_RESPONSE_NONE; }