X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/e6881fd166586793a5a90effeefe4188092f383b..2a0c28e6e47c8a173f32fc99cd8666a2633c5c12:/Demos/Device/LowLevel/Keyboard/Keyboard.c diff --git a/Demos/Device/LowLevel/Keyboard/Keyboard.c b/Demos/Device/LowLevel/Keyboard/Keyboard.c index 35ef41ba8..e27b22827 100644 --- a/Demos/Device/LowLevel/Keyboard/Keyboard.c +++ b/Demos/Device/LowLevel/Keyboard/Keyboard.c @@ -172,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(); @@ -183,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; @@ -200,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; @@ -214,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; @@ -225,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; @@ -239,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; @@ -266,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) { @@ -291,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) { @@ -329,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; @@ -377,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(); }