X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/ed031c1df2f5b053b9cd9f48c63e66a42b7c049e..6a7c298c0f7119e44e18de15d714cd8b938e3bef:/Demos/Host/MouseHost/MouseHost.c diff --git a/Demos/Host/MouseHost/MouseHost.c b/Demos/Host/MouseHost/MouseHost.c index 44f20b43a..7db95808a 100644 --- a/Demos/Host/MouseHost/MouseHost.c +++ b/Demos/Host/MouseHost/MouseHost.c @@ -36,17 +36,11 @@ #include "MouseHost.h" -/* Project Tags, for reading out using the ButtLoad project */ -BUTTLOADTAG(ProjName, "LUFA Mouse Host App"); -BUTTLOADTAG(BuildTime, __TIME__); -BUTTLOADTAG(BuildDate, __DATE__); -BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING); - /* Scheduler Task List */ TASK_LIST { - { Task: USB_USBTask , TaskStatus: TASK_STOP }, - { Task: USB_Mouse_Host , TaskStatus: TASK_STOP }, + { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, + { .Task = USB_Mouse_Host , .TaskStatus = TASK_STOP }, }; @@ -75,7 +69,7 @@ int main(void) /* Initialize USB Subsystem */ USB_Init(); - /* Startup message */ + /* Start-up message */ puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY "Mouse Host Demo running.\r\n" ESC_INVERSE_OFF)); @@ -86,7 +80,7 @@ int main(void) /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and * starts the library USB task to begin the enumeration and USB management process. */ -EVENT_HANDLER(USB_DeviceAttached) +void EVENT_USB_DeviceAttached(void) { puts_P(PSTR("Device Attached.\r\n")); UpdateStatus(Status_USBEnumerating); @@ -98,7 +92,7 @@ EVENT_HANDLER(USB_DeviceAttached) /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and * stops the library USB task management process. */ -EVENT_HANDLER(USB_DeviceUnattached) +void EVENT_USB_DeviceUnattached(void) { /* Stop mouse and USB management task */ Scheduler_SetTaskMode(USB_USBTask, TASK_STOP); @@ -111,7 +105,7 @@ EVENT_HANDLER(USB_DeviceUnattached) /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully * enumerated by the host and is now ready to be used by the application. */ -EVENT_HANDLER(USB_DeviceEnumerationComplete) +void EVENT_USB_DeviceEnumerationComplete(void) { /* Start Mouse Host task */ Scheduler_SetTaskMode(USB_Mouse_Host, TASK_RUN); @@ -121,7 +115,7 @@ EVENT_HANDLER(USB_DeviceEnumerationComplete) } /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ -EVENT_HANDLER(USB_HostError) +void EVENT_USB_HostError(const uint8_t ErrorCode) { USB_ShutDown(); @@ -132,10 +126,10 @@ EVENT_HANDLER(USB_HostError) for(;;); } -/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occured while +/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while * enumerating an attached USB device. */ -EVENT_HANDLER(USB_DeviceEnumerationFailed) +void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode) { puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n")); printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode); @@ -184,41 +178,56 @@ void ReadNextReport(void) USB_MouseReport_Data_t MouseReport; uint8_t LEDMask = LEDS_NO_LEDS; - /* Select the mouse report data in pipe */ - Pipe_SelectPipe(MOUSE_DATAPIPE); + /* Select mouse data pipe */ + Pipe_SelectPipe(MOUSE_DATAPIPE); - /* Ensure pipe contains data and is ready to be read before continuing */ - if (!(Pipe_ReadWriteAllowed())) - return; + /* Unfreeze keyboard data pipe */ + Pipe_Unfreeze(); - /* Read in mouse report data */ - Pipe_Read_Stream_LE(&MouseReport, sizeof(MouseReport)); - - /* Clear the IN endpoint, ready for next data packet */ - Pipe_ClearCurrentBank(); + /* Check to see if a packet has been received */ + if (!(Pipe_IsINReceived())) + { + /* Refreeze HID data IN pipe */ + Pipe_Freeze(); + + return; + } + + /* Ensure pipe contains data before trying to read from it */ + if (Pipe_IsReadWriteAllowed()) + { + /* Read in mouse report data */ + Pipe_Read_Stream_LE(&MouseReport, sizeof(MouseReport)); + + /* Alter status LEDs according to mouse X movement */ + if (MouseReport.X > 0) + LEDMask |= LEDS_LED1; + else if (MouseReport.X < 0) + LEDMask |= LEDS_LED2; + + /* Alter status LEDs according to mouse Y movement */ + if (MouseReport.Y > 0) + LEDMask |= LEDS_LED3; + else if (MouseReport.Y < 0) + LEDMask |= LEDS_LED4; + + /* Alter status LEDs according to mouse button position */ + if (MouseReport.Button) + LEDMask = LEDS_ALL_LEDS; - /* Alter status LEDs according to mouse X movement */ - if (MouseReport.X > 0) - LEDMask |= LEDS_LED1; - else if (MouseReport.X < 0) - LEDMask |= LEDS_LED2; + LEDs_SetAllLEDs(LEDMask); - /* Alter status LEDs according to mouse Y movement */ - if (MouseReport.Y > 0) - LEDMask |= LEDS_LED3; - else if (MouseReport.Y < 0) - LEDMask |= LEDS_LED4; - - /* Alter status LEDs according to mouse button position */ - if (MouseReport.Button) - LEDMask = LEDS_ALL_LEDS; - - LEDs_SetAllLEDs(LEDMask); - - /* Print mouse report data through the serial port */ - printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X, - MouseReport.Y, - MouseReport.Button); + /* Print mouse report data through the serial port */ + printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X, + MouseReport.Y, + MouseReport.Button); + } + + /* Clear the IN endpoint, ready for next data packet */ + Pipe_ClearIN(); + + /* Refreeze mouse data pipe */ + Pipe_Freeze(); } /** Task to set the configuration of the attached device after it has been enumerated, and to read and process @@ -233,16 +242,19 @@ TASK(USB_Mouse_Host) { case HOST_STATE_Addressed: /* Standard request to set the device configuration to configuration 1 */ - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE), - bRequest: REQ_SetConfiguration, - wValue: 1, - wIndex: 0, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE), + .bRequest = REQ_SetConfiguration, + .wValue = 1, + .wIndex = 0, + .wLength = 0, }; - /* Send the request, display error and wait for device detatch if request fails */ + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + + /* Send the request, display error and wait for device detach if request fails */ if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful) { puts_P(PSTR("Control Error (Set Configuration).\r\n")); @@ -280,16 +292,19 @@ TASK(USB_Mouse_Host) } /* HID class request to set the mouse protocol to the Boot Protocol */ - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), - bRequest: REQ_SetProtocol, - wValue: 0, - wIndex: 0, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_SetProtocol, + .wValue = 0, + .wIndex = 0, + .wLength = 0, }; - /* Send the request, display error and wait for device detatch if request fails */ + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + + /* Send the request, display error and wait for device detach if request fails */ if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful) { puts_P(PSTR("Control Error (Set Protocol).\r\n")); @@ -307,45 +322,10 @@ TASK(USB_Mouse_Host) USB_HostState = HOST_STATE_Ready; break; - #if !defined(INTERRUPT_DATA_PIPE) case HOST_STATE_Ready: - /* Select and unfreeze mouse data pipe */ - Pipe_SelectPipe(MOUSE_DATAPIPE); - Pipe_Unfreeze(); - /* If a report has been received, read and process it */ - if (Pipe_ReadWriteAllowed()) - ReadNextReport(); + ReadNextReport(); - /* Freeze mouse data pipe */ - Pipe_Freeze(); break; - #endif - } -} - -#if defined(INTERRUPT_DATA_PIPE) -/** Interrupt handler for the Endpoint/Pipe interrupt vector. This interrupt fires each time an enabled - * pipe interrupt occurs on a pipe which has had that interrupt enabled. - */ -ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) -{ - /* Check to see if the mouse data pipe has caused the interrupt */ - if (Pipe_HasPipeInterrupted(MOUSE_DATAPIPE)) - { - /* Clear the pipe interrupt, and select the mouse pipe */ - Pipe_ClearPipeInterrupt(MOUSE_DATAPIPE); - Pipe_SelectPipe(MOUSE_DATAPIPE); - - /* Check to see if the pipe IN interrupt has fired */ - if (USB_INT_HasOccurred(PIPE_INT_IN) && USB_INT_IsEnabled(PIPE_INT_IN)) - { - /* Clear interrupt flag */ - USB_INT_Clear(PIPE_INT_IN); - - /* Read and process the next report from the device */ - ReadNextReport(); - } } } -#endif