void SYS_Initialize ( void* data ) { /* Set up cache and wait states for * maximum performance. */ SYSTEMConfigPerformance(80000000); /* Initialize the BSP */ BSP_Initialize( ); /* Initialize MDD */ USB_DEVICE_MDD_INTFLASH_Initialize( 0, (const SYS_MODULE_INIT * const)&mddFlashInitData ); /* Initialize the USB device layer */ appDrvObject.usbDevObject = USB_DEVICE_Initialize (USB_DEVICE_INDEX_0 , ( SYS_MODULE_INIT* ) & usbDevInitData); /* check if the object returned by the device layer is valid */ SYS_ASSERT((SYS_MODULE_OBJ_INVALID != appDrvObject.usbDevObject), "Invalid USB DEVICE object"); /* open an instance of the device layer */ appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, 0 ); /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventCallBackSet(appData.usbDevHandle, APP_USBDeviceEventHandler); /* Register a callback for acquiring vendor specific setup packet */ //USB_DEVICE_ControlEventCallBackSet(appData.usbDevHandle, APP_UsbDeviceControlEventCallBack ); USB_DEVICE_Attach(appData.usbDevHandle); /* Initialize the Application */ APP_Initialize ( ); /* Initializethe interrupt system */ SYS_INT_Initialize(); /* set priority for USB interrupt source */ SYS_INT_VectorPrioritySet(INT_VECTOR_USB, INT_PRIORITY_LEVEL3); /* set sub-priority for USB interrupt source */ SYS_INT_VectorSubprioritySet(INT_VECTOR_USB, INT_SUBPRIORITY_LEVEL3); /* Initialize the global interrupts */ SYS_INT_Enable(); }
/*********************************************** * Application USB Device Layer Event Handler. ***********************************************/ void APP_USBDeviceEventHandler ( USB_DEVICE_EVENT event, void * eventData, uintptr_t context ) { USB_DEVICE_EVENT_DATA_CONFIGURED *configuredEventData; static bool led; switch ( event ) { case USB_DEVICE_EVENT_SOF: app2Data.sofEventHasOccurred = true; break; case USB_DEVICE_EVENT_RESET: app2Data.isConfigured = false; break; case USB_DEVICE_EVENT_CONFIGURED: /* Set the USBID pin to output (LED control) after configuration is done */ PLIB_PORTS_PinDirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); (led) ? PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3) : PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); /* Check the configuration. We only support configuration 1 */ configuredEventData = (USB_DEVICE_EVENT_DATA_CONFIGURED*)eventData; if ( configuredEventData->configurationValue == 1) { /* Register the CDC Device application event handler here. * Note how the app2Data object pointer is passed as the * user data */ USB_DEVICE_CDC_EventHandlerSet(USB_DEVICE_CDC_INDEX_0, APP_USBDeviceCDCEventHandler, (uintptr_t)&app2Data); /* Mark that the device is now configured */ app2Data.isConfigured = true; } break; case USB_DEVICE_EVENT_POWER_DETECTED: led = PLIB_PORTS_PinGetLatched(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); /* Set the USBID pin to input (floating) so that it will be configured as DEVICE */ PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_F, PORTS_BIT_POS_3); /* VBUS was detected. We can attach the device */ USB_DEVICE_Attach(app2Data.deviceHandle); break; case USB_DEVICE_EVENT_POWER_REMOVED: /* VBUS is not available any more. Detach the device. */ USB_DEVICE_Detach(app2Data.deviceHandle); break; case USB_DEVICE_EVENT_SUSPENDED: break; case USB_DEVICE_EVENT_RESUMED: case USB_DEVICE_EVENT_ERROR: default: break; } }
void APP_USBDeviceEventHandler(USB_DEVICE_EVENT event, void * eventData, uintptr_t context) { USB_DEVICE_EVENT_DATA_CONFIGURED * configurationValue; switch(event) { case USB_DEVICE_EVENT_SOF: /* This event is used for switch debounce. This flag is reset * by the switch process routine. */ appData.sofEventHasOccurred = true; appData.setIdleTimer++; break; case USB_DEVICE_EVENT_RESET: case USB_DEVICE_EVENT_DECONFIGURED: /* Device got deconfigured */ appData.isConfigured = false; appData.isMouseReportSendBusy = false; appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; appData.emulateMouse = true; BSP_LEDOn ( APP_USB_LED_1 ); BSP_LEDOn ( APP_USB_LED_2 ); BSP_LEDOff ( APP_USB_LED_3 ); break; case USB_DEVICE_EVENT_CONFIGURED: /* Device is configured */ configurationValue = (USB_DEVICE_EVENT_DATA_CONFIGURED *)eventData; if(configurationValue->configurationValue == 1) { appData.isConfigured = true; BSP_LEDOff ( APP_USB_LED_1 ); BSP_LEDOff ( APP_USB_LED_2 ); BSP_LEDOn ( APP_USB_LED_3 ); /* Register the Application HID Event Handler. */ USB_DEVICE_HID_EventHandlerSet(appData.hidInstance, APP_USBDeviceHIDEventHandler, (uintptr_t)&appData); } break; case USB_DEVICE_EVENT_POWER_DETECTED: /* VBUS was detected. We can attach the device */ USB_DEVICE_Attach(appData.deviceHandle); break; case USB_DEVICE_EVENT_POWER_REMOVED: /* VBUS is not available any more. Detach the device. */ USB_DEVICE_Detach(appData.deviceHandle); break; case USB_DEVICE_EVENT_SUSPENDED: BSP_LEDOff ( APP_USB_LED_1 ); BSP_LEDOn ( APP_USB_LED_2 ); BSP_LEDOn ( APP_USB_LED_3 ); break; case USB_DEVICE_EVENT_RESUMED: case USB_DEVICE_EVENT_ERROR: default: break; } }
void APP_Tasks (void ) { switch(appData.state) { case APP_STATE_INIT: /* Open an instance of the device layer */ appData.deviceHandle = USB_DEVICE_Open( 0, DRV_IO_INTENT_READWRITE ); /* Register a callback with device layer to get * event notification (for end point 0) */ USB_DEVICE_EventCallBackSet(appData.deviceHandle, APP_USBDeviceEventHandler); /* Attach the device */ USB_DEVICE_Attach(appData.deviceHandle); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device is configured. The * isConfigured flag is updated in the * Device Event Handler */ if(appData.isConfigured) { /* Initialize the flag and place a request for a * output report */ appData.isReportReceived = false; USB_DEVICE_HID_ReportReceive(appData.hidInstance, &appData.receiveTransferHandle, (uint8_t *)&appData.keyboardOutputReport,1); appData.state = APP_STATE_CHECK_IF_CONFIGURED; } break; case APP_STATE_CHECK_IF_CONFIGURED: /* This state is needed because the device can get * unconfigured asynchronously. Any application state * machine reset should happen within the state machine * context only. */ if(appData.isConfigured) { appData.state = APP_STATE_SWITCH_PROCESS; } else { /* This means the device got de-configured. * We reset the state and the wait for configuration */ APP_StateReset(); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } break; case APP_STATE_SWITCH_PROCESS: /* Process the switch state and go to the * next state. */ APP_ProcessSwitchPress(&appData); appData.state = APP_STATE_CHECK_FOR_OUTPUT_REPORT; break; case APP_STATE_CHECK_FOR_OUTPUT_REPORT: if(appData.isReportReceived == true) { /* Update the LED and schedule and * request */ APP_KeyboardLEDStatus(); appData.isReportReceived = false; USB_DEVICE_HID_ReportReceive(appData.hidInstance, &appData.receiveTransferHandle, (uint8_t *)&appData.keyboardOutputReport,1); } appData.state = APP_STATE_EMULATE_KEYBOARD; break; case APP_STATE_EMULATE_KEYBOARD: if(appData.isReportSentComplete) { /* This means report can be sent*/ APP_EmulateKeyboard(); appData.isReportSentComplete = false; USB_DEVICE_HID_ReportSend(appData.hidInstance, &appData.sendTransferHandle, (uint8_t *)&appData.keyboardInputReport, sizeof(KEYBOARD_INPUT_REPORT)); } appData.state = APP_STATE_CHECK_IF_CONFIGURED; break; case APP_STATE_ERROR: break; default: break; } }
/********************************************************** * Application tasks routine. This function implements the * application state machine. ***********************************************************/ void APP_Tasks ( APP_DATA * appData ) { /* Update the application state machine based * on the current state */ switch(appData->state) { case APP_STATE_INIT: /* Open the device layer */ appData->deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData->deviceHandle == USB_DEVICE_HANDLE_INVALID) { SYS_ASSERT(false, "Could not open device layer"); appData->state = APP_STATE_ERROR; break; } /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventCallBackSet(appData->deviceHandle, APP_USBDeviceEventHandler); /* Attach the device */ USB_DEVICE_Attach (appData->deviceHandle); appData->state = APP_STATE_WAIT_FOR_CONFIGURATION; break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(appData->isConfigured) { /* Schedule the first read from CDC function driver */ appData->state = APP_STATE_CHECK_CDC_READ; appData->isReadComplete = false; USB_DEVICE_CDC_Read (appData->cdcInstance, &appData->readTransferHandle, appData->readBuffer, 64); } break; case APP_STATE_CHECK_CDC_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else check if UART has received data */ if(appData->isReadComplete == true) { appData->isReadComplete = false; APP_UART2Write(appData->readBuffer, appData->readLength); appData->state = APP_STATE_CHECK_UART_TRANSMIT_COMPLETE; } else { appData->state = APP_STATE_CHECK_UART_RECEIVE; } break; case APP_STATE_CHECK_UART_TRANSMIT_COMPLETE: /* Check if the UART transmission is complete */ if(!APP_UART2TransmitIsBusy()) { /* This means UART transmit is done. Schedule the next * read and go to the next state */ appData->isReadComplete = false; USB_DEVICE_CDC_Read (appData->cdcInstance, &appData->readTransferHandle, appData->readBuffer, 64); appData->state = APP_STATE_CHECK_UART_RECEIVE; } case APP_STATE_CHECK_UART_RECEIVE: if(APP_StateReset()) { break; } /* Check if a character was received on the UART */ if(APP_UART2ReceiveReady()) { /* Send the character to the CDC function driver */ USB_DEVICE_CDC_Write(0, &appData->writeTransferHandle, &appData->uartReceivedData, 1, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); } appData->state = APP_STATE_CHECK_CDC_READ; break; case APP_STATE_ERROR: break; default: break; } }
void APP_USBDeviceEventHandler ( USB_DEVICE_EVENT event, void * eventData, uintptr_t context) { uint8_t configurationValue; switch ( event ) { case USB_DEVICE_EVENT_RESET: case USB_DEVICE_EVENT_DECONFIGURED: /* USB device is reset or device is deconfigured. * This means that USB device layer is about to deininitialize * all function drivers. Update LEDs to indicate * reset/deconfigured state. */ BSP_LEDOn ( APP_USB_LED_1 ); BSP_LEDOn ( APP_USB_LED_2 ); BSP_LEDOff( APP_USB_LED_3 ); appData.isConfigured = false; break; case USB_DEVICE_EVENT_CONFIGURED: /* Check the configuration */ configurationValue = ((USB_DEVICE_EVENT_DATA_CONFIGURED *)eventData)->configurationValue; if (configurationValue == 1) { /* The device is in configured state. Update LED indication */ BSP_LEDOff( APP_USB_LED_1 ); BSP_LEDOff( APP_USB_LED_2 ); BSP_LEDOn( APP_USB_LED_3 ); /* Register the CDC Device application event handler here. * Note how the appData object pointer is passed as the * user data */ USB_DEVICE_CDC_EventHandlerSet(USB_DEVICE_CDC_INDEX_0, APP_USBDeviceCDCEventHandler, (uintptr_t)&appData); /* mark that set configuration is complete */ appData.isConfigured = true; } break; case USB_DEVICE_EVENT_SUSPENDED: /* Update LEDs */ BSP_LEDOff ( APP_USB_LED_1 ); BSP_LEDOn ( APP_USB_LED_2 ); BSP_LEDOn( APP_USB_LED_3 ); break; case USB_DEVICE_EVENT_POWER_DETECTED: /* VBUS was detected. Connect the device */ USB_DEVICE_Attach (appData.deviceHandle); break; case USB_DEVICE_EVENT_POWER_REMOVED: /* VBUS was removed. Disconnect the device */ USB_DEVICE_Detach(appData.deviceHandle); break; /* These events are not used in this demo */ case USB_DEVICE_EVENT_RESUMED: case USB_DEVICE_EVENT_ERROR: default: break; } }
void APP_USBDeviceEventHandler(USB_DEVICE_EVENT event, void * eventData, uintptr_t context) { switch(event) { case USB_DEVICE_EVENT_RESET: case USB_DEVICE_EVENT_DECONFIGURED: /* Host has de configured the device or a bus reset has happened. * Device layer is going to de-initialize all function drivers. * Hence close handles to all function drivers (Only if they are * opened previously. */ BSP_LEDOn (APP_USB_LED_1); BSP_LEDOn (APP_USB_LED_2); BSP_LEDOff (APP_USB_LED_3); appData.deviceConfigured = false; appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; break; case USB_DEVICE_EVENT_CONFIGURED: /* Set the flag indicating device is configured. */ appData.deviceConfigured = true; /* Save the other details for later use. */ appData.configurationValue = ((USB_DEVICE_EVENT_DATA_CONFIGURED*)eventData)->configurationValue; /* Register application HID event handler */ USB_DEVICE_HID_EventHandlerSet(USB_DEVICE_HID_INDEX_0, APP_USBDeviceHIDEventHandler, (uintptr_t)&appData); /* Update the LEDs */ BSP_LEDOff (APP_USB_LED_1); BSP_LEDOff (APP_USB_LED_2); BSP_LEDOn (APP_USB_LED_3); break; case USB_DEVICE_EVENT_SUSPENDED: /* Switch on green and orange, switch off red */ BSP_LEDOff (APP_USB_LED_1); BSP_LEDOn (APP_USB_LED_2); BSP_LEDOn (APP_USB_LED_3); break; case USB_DEVICE_EVENT_POWER_DETECTED: /* VBUS was detected. We can attach the device */ USB_DEVICE_Attach (appData.usbDevHandle); break; case USB_DEVICE_EVENT_POWER_REMOVED: /* VBUS is not available */ USB_DEVICE_Detach(appData.usbDevHandle); break; /* These events are not used in this demo */ case USB_DEVICE_EVENT_RESUMED: case USB_DEVICE_EVENT_ERROR: default: break; } }
/********************************************************** * Application tasks routine. This function implements the * application state machine. ***********************************************************/ void APP_Tasks ( APP_DATA * appData ) { /* Update the application state machine based * on the current state */ USB_DEVICE_CDC_RESULT result; APP_ProcessSwitchPress(appData); switch(appData->state) { case APP_STATE_INIT: /* Open the device layer */ appData->deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData->deviceHandle == USB_DEVICE_HANDLE_INVALID) { SYS_ASSERT(false, "Could not open device layer"); appData->state = APP_STATE_ERROR; break; } /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventCallBackSet(appData->deviceHandle, APP_USBDeviceEventHandler); /* Attach the device */ USB_DEVICE_Attach (appData->deviceHandle); appData->state = APP_STATE_WAIT_FOR_CONFIGURATION; break; case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(appData->isConfigured) { /* If the device is configured then lets start reading */ appData->state = APP_STATE_SCHEDULE_READ; } break; case APP_STATE_SCHEDULE_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else wait for the current read to complete */ appData->state = APP_STATE_WAIT_FOR_READ_COMPLETE; if(appData->isReadComplete == true) { appData->isReadComplete = false; appData->readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; USB_DEVICE_CDC_Read (appData->cdcInstance, &appData->readTransferHandle, appData->readBuffer, 64); if(appData->readTransferHandle == USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID) { appData->state = APP_STATE_ERROR; break; } } break; case APP_STATE_WAIT_FOR_READ_COMPLETE: case APP_STATE_CHECK_SWITCH_PRESSED: if(APP_StateReset()) { break; } /* Check if a character was received or a switch was pressed. * The isReadComplete flag gets updated in the CDC event handler. */ if(appData->isReadComplete || appData->isSwitchPressed) { appData->state = APP_STATE_SCHEDULE_WRITE; } break; case APP_STATE_SCHEDULE_WRITE: if(APP_StateReset()) { break; } /* Setup the write */ appData->writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; appData->isWriteComplete = false; appData->state = APP_STATE_WAIT_FOR_WRITE_COMPLETE; if(appData->isSwitchPressed) { /* If the switch was pressed, then send the switch prompt*/ appData->isSwitchPressed = false; result = USB_DEVICE_CDC_Write(appData->cdcInstance, &appData->writeTransferHandle, switchPrompt, 23, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); } else { /* Else echo the received character + 1*/ appData->readBuffer[0] = appData->readBuffer[0] + 1; USB_DEVICE_CDC_Write(appData->cdcInstance, &appData->writeTransferHandle, appData->readBuffer, 1, USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); } break; case APP_STATE_WAIT_FOR_WRITE_COMPLETE: if(APP_StateReset()) { break; } /* Check if a character was sent. The isWriteComplete * flag gets updated in the CDC event handler */ if(appData->isWriteComplete == true) { appData->state = APP_STATE_SCHEDULE_READ; } break; case APP_STATE_ERROR: break; default: break; } }
void APP_USBDeviceEventHandler ( USB_DEVICE_EVENT event, void * eventData, uintptr_t context ) { USB_DEVICE_EVENT_DATA_CONFIGURED* configuredEventData; switch(event) { case USB_DEVICE_EVENT_RESET: U1TXREG = 'R'; //break; case USB_DEVICE_EVENT_DECONFIGURED: { appData.state = APP_STATE_USB_OPENED; appData.reading = 0; appData.audio_play = false; USB_DEVICE_EndpointDisable(appData.usbDevHandle, AUDIO_EP); if(appData.tunnel_write_handle != USB_DEVICE_TRANSFER_HANDLE_INVALID) USB_DEVICE_EndpointTransferCancel(appData.usbDevHandle, TUNNEL_EP_IN, appData.tunnel_write_handle); USB_DEVICE_EndpointDisable(appData.usbDevHandle, TUNNEL_EP_IN); USB_DEVICE_EndpointDisable(appData.usbDevHandle, TUNNEL_EP_OUT); break; } case USB_DEVICE_EVENT_CONFIGURED: U1TXREG = '+'; /* check the configuration */ configuredEventData = (USB_DEVICE_EVENT_DATA_CONFIGURED *)eventData; if(configuredEventData->configurationValue == 1) { } USB_DEVICE_EndpointEnable(appData.usbDevHandle, AUDIO_STREAMING_INTERFACE_ID, AUDIO_EP, USB_TRANSFER_TYPE_ISOCHRONOUS, PACKET_SIZE); USB_DEVICE_EndpointEnable(appData.usbDevHandle, TUNER_CONTROL_INTERFACE_ID, TUNNEL_EP_IN, USB_TRANSFER_TYPE_BULK, 32); USB_DEVICE_EndpointEnable(appData.usbDevHandle, TUNER_CONTROL_INTERFACE_ID, TUNNEL_EP_OUT, USB_TRANSFER_TYPE_BULK, 32); appData.state = APP_STATE_CONFIGURED; break; case USB_DEVICE_EVENT_SUSPENDED: U1TXREG = 'S'; //appData.suspended = true; break; case USB_DEVICE_EVENT_RESUMED: U1TXREG = 's'; appData.suspended = false; break; case USB_DEVICE_EVENT_POWER_DETECTED: #ifndef NO_USB_ATTACH USB_DEVICE_Attach (appData.usbDevHandle); #else #warning "USB ATTACH DISABLED" #endif break; case USB_DEVICE_EVENT_POWER_REMOVED: USB_DEVICE_Detach (appData.usbDevHandle); break; case USB_DEVICE_EVENT_ERROR: U1TXREG = 'E'; break; case USB_DEVICE_EVENT_CONTROL_TRANSFER_SETUP_REQUEST: { USB_SETUP_PACKET * sp = eventData; if(sp->bRequest == 0x0b && sp->wIndex == 0x0001) //set interace on if 1 { //USB_DEVICE_ControlReceive(appData.usbDevHandle, &controlData, sizeof(controlData)); if(sp->wValue == 0) { int i; appData.audio_play = false; USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_OK); U1TXREG = 'C'; } else if(sp->wValue == 1) { appData.audio_play = true; USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_OK); appData.noAudioData = 1; U1TXREG = 'p'; } else USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_ERROR); } else USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_ERROR); break; } case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: case USB_DEVICE_EVENT_CONTROL_TRANSFER_ABORTED: case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_SENT: //debughalt(); break; case USB_DEVICE_EVENT_ENDPOINT_READ_COMPLETE: { appData.tunnel_read_count = ((USB_DEVICE_EVENT_DATA_ENDPOINT_WRITE_COMPLETE*)eventData)->length; if(appData.tunnel_read_data[0]&TUNNEL_PING_MASK) appData.pingRequest = 1; else appData.tuner_request = 1; //appData.reading = 0; U1TXREG = 'I'; break; } case USB_DEVICE_EVENT_ENDPOINT_WRITE_COMPLETE: { USB_DEVICE_EVENT_DATA_ENDPOINT_WRITE_COMPLETE* ed = (USB_DEVICE_EVENT_DATA_ENDPOINT_WRITE_COMPLETE*)eventData; if(appData.tunnel_write_handle == ed->transferHandle) { appData.tunnel_write_handle = USB_DEVICE_TRANSFER_HANDLE_INVALID; U1TXREG = 'W'; } else { int i=0; for(;i<AUDIO_HANDLES;i++) if(ed->transferHandle == audio_handle[i]) audio_handle[i] = HANDLE_INVALID; } break; } default: debughalt(); break; } }