X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/ece9e3d5991dd481fc36baf799a1a85d14b88251..6a7c298c0f7119e44e18de15d714cd8b938e3bef:/Demos/Host/KeyboardHost/KeyboardHost.c diff --git a/Demos/Host/KeyboardHost/KeyboardHost.c b/Demos/Host/KeyboardHost/KeyboardHost.c index 13aed7ec3..2b9bb2b0b 100644 --- a/Demos/Host/KeyboardHost/KeyboardHost.c +++ b/Demos/Host/KeyboardHost/KeyboardHost.c @@ -36,17 +36,11 @@ #include "KeyboardHost.h" -/* Project Tags, for reading out using the ButtLoad project */ -BUTTLOADTAG(ProjName, "LUFA KBD 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_Keyboard_Host , TaskStatus: TASK_STOP }, + { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, + { .Task = USB_Keyboard_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 "Keyboard 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 Keyboard 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 Keyboard Host task */ Scheduler_SetTaskMode(USB_Keyboard_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); @@ -186,61 +180,59 @@ void ReadNextReport(void) /* 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())) + /* Check to see if a packet has been received */ + if (!(Pipe_IsINReceived())) { - #if !defined(INTERRUPT_DATA_PIPE) - /* Refreeze keyboard data pipe */ + /* Refreeze HID data IN pipe */ Pipe_Freeze(); - #endif - + return; } - /* Read in keyboard report data */ - Pipe_Read_Stream_LE(&KeyboardReport, sizeof(KeyboardReport)); - - /* Clear the IN endpoint, ready for next data packet */ - Pipe_ClearCurrentBank(); - - /* Indicate if the modifier byte is non-zero (special key such as shift is being pressed) */ - LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0); - - /* Check if a key has been pressed */ - if (KeyboardReport.KeyCode) + /* Ensure pipe contains data before trying to read from it */ + if (Pipe_IsReadWriteAllowed()) { - /* Toggle status LED to indicate keypress */ - if (LEDs_GetLEDs() & LEDS_LED2) - LEDs_TurnOffLEDs(LEDS_LED2); - else - LEDs_TurnOnLEDs(LEDS_LED2); - - char PressedKey = 0; - - /* Retrieve pressed key character if alphanumeric */ - if ((KeyboardReport.KeyCode >= 0x04) && (KeyboardReport.KeyCode <= 0x1D)) - PressedKey = (KeyboardReport.KeyCode - 0x04) + 'A'; - else if ((KeyboardReport.KeyCode >= 0x1E) && (KeyboardReport.KeyCode <= 0x27)) - PressedKey = (KeyboardReport.KeyCode - 0x1E) + '0'; - else if (KeyboardReport.KeyCode == 0x2C) - PressedKey = ' '; - else if (KeyboardReport.KeyCode == 0x28) - PressedKey = '\n'; - - /* Print the pressed key character out through the serial port if valid */ - if (PressedKey) - putchar(PressedKey); + /* Read in keyboard report data */ + Pipe_Read_Stream_LE(&KeyboardReport, sizeof(KeyboardReport)); + + /* Indicate if the modifier byte is non-zero (special key such as shift is being pressed) */ + LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0); + + /* Check if a key has been pressed */ + if (KeyboardReport.KeyCode) + { + /* Toggle status LED to indicate keypress */ + if (LEDs_GetLEDs() & LEDS_LED2) + LEDs_TurnOffLEDs(LEDS_LED2); + else + LEDs_TurnOnLEDs(LEDS_LED2); + + char PressedKey = 0; + + /* Retrieve pressed key character if alphanumeric */ + if ((KeyboardReport.KeyCode >= 0x04) && (KeyboardReport.KeyCode <= 0x1D)) + PressedKey = (KeyboardReport.KeyCode - 0x04) + 'A'; + else if ((KeyboardReport.KeyCode >= 0x1E) && (KeyboardReport.KeyCode <= 0x27)) + PressedKey = (KeyboardReport.KeyCode - 0x1E) + '0'; + else if (KeyboardReport.KeyCode == 0x2C) + PressedKey = ' '; + else if (KeyboardReport.KeyCode == 0x28) + PressedKey = '\n'; + + /* Print the pressed key character out through the serial port if valid */ + if (PressedKey) + putchar(PressedKey); + } } - - #if !defined(INTERRUPT_DATA_PIPE) + + /* Clear the IN endpoint, ready for next data packet */ + Pipe_ClearIN(); + /* 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 @@ -254,16 +246,19 @@ TASK(USB_Keyboard_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")); @@ -301,16 +296,19 @@ TASK(USB_Keyboard_Host) } /* HID class request to set the keyboard 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")); @@ -324,53 +322,14 @@ 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: /* If a report has been received, read and process it */ ReadNextReport(); 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) -{ - /* 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)) - { - /* Clear the pipe interrupt, and select the keyboard pipe */ - Pipe_ClearPipeInterrupt(KEYBOARD_DATAPIPE); - Pipe_SelectPipe(KEYBOARD_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(); - } - - /* Restore previously selected pipe */ - Pipe_SelectPipe(PrevSelectedPipe); -} -#endif