X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/33a018474913701fa9ef8e962acf58accd1184d2..2a0c28e6e47c8a173f32fc99cd8666a2633c5c12:/Demos/Device/LowLevel/Keyboard/Keyboard.c?ds=sidebyside diff --git a/Demos/Device/LowLevel/Keyboard/Keyboard.c b/Demos/Device/LowLevel/Keyboard/Keyboard.c index 06806b58b..e27b22827 100644 --- a/Demos/Device/LowLevel/Keyboard/Keyboard.c +++ b/Demos/Device/LowLevel/Keyboard/Keyboard.c @@ -118,19 +118,25 @@ void EVENT_USB_Disconnect(void) * of the USB device after enumeration, and configures the keyboard device endpoints. */ void EVENT_USB_ConfigurationChanged(void) -{ - /* Setup Keyboard Keycode Report Endpoint */ - Endpoint_ConfigureEndpoint(KEYBOARD_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, - ENDPOINT_BANK_SINGLE); - - /* Setup Keyboard LED Report Endpoint */ - Endpoint_ConfigureEndpoint(KEYBOARD_LEDS_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_OUT, KEYBOARD_EPSIZE, - ENDPOINT_BANK_SINGLE); - +{ /* Indicate USB connected and ready */ LEDs_SetAllLEDs(LEDMASK_USB_READY); + + /* Setup Keyboard Keycode Report Endpoint */ + if (!(Endpoint_ConfigureEndpoint(KEYBOARD_EPNUM, EP_TYPE_INTERRUPT, + ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, + ENDPOINT_BANK_SINGLE))) + { + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + } + + /* Setup Keyboard LED Report Endpoint */ + if (!(Endpoint_ConfigureEndpoint(KEYBOARD_LEDS_EPNUM, EP_TYPE_INTERRUPT, + ENDPOINT_DIR_OUT, KEYBOARD_EPSIZE, + ENDPOINT_BANK_SINGLE))) + { + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + } } /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific @@ -166,7 +172,11 @@ void EVENT_USB_UnhandledControlPacket(void) Endpoint_ClearSETUP(); /* Wait until the LED report has been sent by the host */ - while (!(Endpoint_IsOUTReceived())); + while (!(Endpoint_IsOUTReceived())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } /* Read in the LED report from the host */ uint8_t LEDStatus = Endpoint_Read_Byte(); @@ -177,9 +187,7 @@ void EVENT_USB_UnhandledControlPacket(void) /* Clear the endpoint data */ Endpoint_ClearOUT(); - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -194,9 +202,7 @@ void EVENT_USB_UnhandledControlPacket(void) /* Send the flag to the host */ Endpoint_ClearIN(); - /* Acknowledge status stage */ - while (!(Endpoint_IsOUTReceived())); - Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); } break; @@ -208,9 +214,7 @@ void EVENT_USB_UnhandledControlPacket(void) /* Set or clear the flag depending on what the host indicates that the current Protocol should be */ UsingReportProtocol = (USB_ControlRequest.wValue != 0); - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -219,12 +223,10 @@ void EVENT_USB_UnhandledControlPacket(void) { Endpoint_ClearSETUP(); - /* Get idle period in MSB */ - IdleCount = (USB_ControlRequest.wValue >> 8); + /* Get idle period in MSB, IdleCount must be multiplied by 4 to get number of milliseconds */ + IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6); - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -233,15 +235,13 @@ void EVENT_USB_UnhandledControlPacket(void) { Endpoint_ClearSETUP(); - /* Write the current idle duration to the host */ - Endpoint_Write_Byte(IdleCount); + /* Write the current idle duration to the host, must be divided by 4 before sent to host */ + Endpoint_Write_Byte(IdleCount >> 2); /* Send the flag to the host */ Endpoint_ClearIN(); - /* Acknowledge status stage */ - while (!(Endpoint_IsOUTReceived())); - Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); } break; @@ -260,7 +260,7 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK) /** Fills the given HID report data structure with the next HID report to send to the host. * - * \param ReportData Pointer to a HID report data structure to be filled + * \param[out] ReportData Pointer to a HID report data structure to be filled */ void CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData) { @@ -285,7 +285,7 @@ void CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData) /** Processes a received LED report, and updates the board LEDs states to match. * - * \param LEDReport LED status report from the host + * \param[in] LEDReport LED status report from the host */ void ProcessLEDReport(uint8_t LEDReport) { @@ -323,8 +323,8 @@ void SendNextReport(void) /* Check if the idle period is set and has elapsed */ if ((IdleCount != HID_IDLE_CHANGESONLY) && (!(IdleMSRemaining))) { - /* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */ - IdleMSRemaining = (IdleCount << 2); + /* Reset the idle time remaining counter */ + IdleMSRemaining = IdleCount; /* Idle period is set and has elapsed, must send a report to the host */ SendReport = true; @@ -371,13 +371,13 @@ void ReceiveNextReport(void) /** Function to manage HID report generation and transmission to the host, when in report mode. */ void HID_Task(void) { - /* Check if the USB system is connected to a host */ - if (USB_IsConnected) - { - /* Send the next keypress report to the host */ - SendNextReport(); + /* Device must be connected and configured for the task to run */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + + /* Send the next keypress report to the host */ + SendNextReport(); - /* Process the LED report sent from the host */ - ReceiveNextReport(); - } + /* Process the LED report sent from the host */ + ReceiveNextReport(); }