X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/ed031c1df2f5b053b9cd9f48c63e66a42b7c049e..7d4cccc22d60125fac111819df48af1873d11018:/Demos/Host/KeyboardHost/KeyboardHost.c diff --git a/Demos/Host/KeyboardHost/KeyboardHost.c b/Demos/Host/KeyboardHost/KeyboardHost.c index 6249e5291..f73e56893 100644 --- a/Demos/Host/KeyboardHost/KeyboardHost.c +++ b/Demos/Host/KeyboardHost/KeyboardHost.c @@ -75,7 +75,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 "Keyboard Host Demo running.\r\n" ESC_INVERSE_OFF)); @@ -132,7 +132,7 @@ 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) @@ -183,13 +183,25 @@ void ReadNextReport(void) { USB_KeyboardReport_Data_t KeyboardReport; - /* Select the keyboard report data in pipe */ + /* Select keyboard data pipe */ Pipe_SelectPipe(KEYBOARD_DATAPIPE); + #if !defined(INTERRUPT_DATA_PIPE) + /* Unfreeze keyboard data pipe */ + Pipe_Unfreeze(); + #endif + /* Ensure pipe contains data and is ready to be read before continuing */ if (!(Pipe_ReadWriteAllowed())) - return; + { + #if !defined(INTERRUPT_DATA_PIPE) + /* Refreeze keyboard data pipe */ + Pipe_Freeze(); + #endif + return; + } + /* Read in keyboard report data */ Pipe_Read_Stream_LE(&KeyboardReport, sizeof(KeyboardReport)); @@ -224,6 +236,11 @@ void ReadNextReport(void) if (PressedKey) putchar(PressedKey); } + + #if !defined(INTERRUPT_DATA_PIPE) + /* Refreeze keyboard data pipe */ + Pipe_Freeze(); + #endif } /** Task to set the configuration of the attached device after it has been enumerated, and to read and process @@ -246,7 +263,7 @@ TASK(USB_Keyboard_Host) wLength: 0, }; - /* Send the request, display error and wait for device detatch if request fails */ + /* 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")); @@ -293,7 +310,7 @@ TASK(USB_Keyboard_Host) wLength: 0, }; - /* Send the request, display error and wait for device detatch if request fails */ + /* 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,22 +324,21 @@ TASK(USB_Keyboard_Host) break; } + #if defined(INTERRUPT_DATA_PIPE) + /* Select and unfreeze keyboard data pipe */ + Pipe_SelectPipe(KEYBOARD_DATAPIPE); + Pipe_Unfreeze(); + #endif + puts_P(PSTR("Keyboard Enumerated.\r\n")); - + USB_HostState = HOST_STATE_Ready; break; #if !defined(INTERRUPT_DATA_PIPE) case HOST_STATE_Ready: - /* Select and unfreeze keyboard data pipe */ - Pipe_SelectPipe(KEYBOARD_DATAPIPE); - Pipe_Unfreeze(); - /* If a report has been received, read and process it */ - if (Pipe_ReadWriteAllowed()) - ReadNextReport(); + ReadNextReport(); - /* Freeze keyboard data pipe */ - Pipe_Freeze(); break; #endif } @@ -334,6 +350,9 @@ TASK(USB_Keyboard_Host) */ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) { + /* Save previously selected pipe before selecting a new pipe */ + uint8_t PrevSelectedPipe = Pipe_GetCurrentPipe(); + /* Check to see if the keyboard data pipe has caused the interrupt */ if (Pipe_HasPipeInterrupted(KEYBOARD_DATAPIPE)) { @@ -350,5 +369,8 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) /* Read and process the next report from the device */ ReadNextReport(); } + + /* Restore previously selected pipe */ + Pipe_SelectPipe(PrevSelectedPipe); } #endif