X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/33a018474913701fa9ef8e962acf58accd1184d2..31d8ebebc0796873f7c70db80a04acdcbb307ed8:/Demos/Device/LowLevel/Keyboard/Keyboard.c diff --git a/Demos/Device/LowLevel/Keyboard/Keyboard.c b/Demos/Device/LowLevel/Keyboard/Keyboard.c index 06806b58b..02f61a528 100644 --- a/Demos/Device/LowLevel/Keyboard/Keyboard.c +++ b/Demos/Device/LowLevel/Keyboard/Keyboard.c @@ -85,18 +85,12 @@ void SetupHardware(void) Joystick_Init(); LEDs_Init(); USB_Init(); - - /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */ - OCR0A = 0x7D; - TCCR0A = (1 << WGM01); - TCCR0B = ((1 << CS01) | (1 << CS00)); - TIMSK0 = (1 << OCIE0A); } /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and * starts the library USB task to begin the enumeration and USB management process. */ -void EVENT_USB_Connect(void) +void EVENT_USB_Device_Connect(void) { /* Indicate USB enumerating */ LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); @@ -108,7 +102,7 @@ void EVENT_USB_Connect(void) /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via * the status LEDs. */ -void EVENT_USB_Disconnect(void) +void EVENT_USB_Device_Disconnect(void) { /* Indicate USB not ready */ LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); @@ -117,27 +111,35 @@ void EVENT_USB_Disconnect(void) /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration * 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); - +void EVENT_USB_Device_ConfigurationChanged(void) +{ /* 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); + } + + USB_Device_EnableSOFEvents(); } -/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific +/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific * control requests that are not handled internally by the USB library (including the HID commands, which are * all issued via the control endpoint), so that they can be handled appropriately for the application. */ -void EVENT_USB_UnhandledControlPacket(void) +void EVENT_USB_Device_UnhandledControlRequest(void) { /* Handle HID Class specific requests */ switch (USB_ControlRequest.bRequest) @@ -166,7 +168,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 +183,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 +198,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 +210,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 +219,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,25 +231,21 @@ 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; } } -/** ISR for the timer 0 compare vector. This ISR fires once each millisecond, and increments the - * scheduler elapsed idle period counter when the host has set an idle period. - */ -ISR(TIMER0_COMPA_vect, ISR_BLOCK) +/** Event handler for the USB device Start Of Frame event. */ +void EVENT_USB_Device_StartOfFrame(void) { /* One millisecond has elapsed, decrement the idle time remaining counter if it has not already elapsed */ if (IdleMSRemaining) @@ -260,7 +254,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 +279,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 +317,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 +365,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(); }