2 * Copyright 2012 Jun Wako <wakojun@gmail.com>
3 * This file is based on:
4 * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
5 * LUFA-120219/Demos/Device/Lowlevel/GenericHID
10 Copyright (C) Dean Camera, 2012.
12 dean [at] fourwalledcubicle [dot] com
17 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
18 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
20 Permission to use, copy, modify, distribute, and sell this
21 software and its documentation for any purpose is hereby granted
22 without fee, provided that the above copyright notice appear in
23 all copies and that both that the copyright notice and this
24 permission notice and warranty disclaimer appear in supporting
25 documentation, and that the name of the author not be used in
26 advertising or publicity pertaining to distribution of the
27 software without specific, written prior permission.
29 The author disclaim all warranties with regard to this
30 software, including all implied warranties of merchantability
31 and fitness. In no event shall the author be liable for any
32 special, indirect or consequential damages or any damages
33 whatsoever resulting from loss of use, data or profits, whether
34 in an action of contract, negligence or other tortious action,
35 arising out of or in connection with the use or performance of
41 #include "host_driver.h"
47 #ifdef SLEEP_LED_ENABLE
48 #include "sleep_led.h"
52 #include "usb_descriptor.h"
55 #include <util/atomic.h>
56 #include "outputselect.h"
59 #include "keycode_config.h"
61 extern keymap_config_t keymap_config;
69 #ifdef BLUETOOTH_ENABLE
70 #ifdef MODULE_ADAFRUIT_BLE
71 #include "adafruit_ble.h"
73 #include "bluetooth.h"
81 #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
93 uint8_t keyboard_idle = 0;
94 /* 0: Boot Protocol, 1: Report Protocol(default) */
95 uint8_t keyboard_protocol = 1;
96 static uint8_t keyboard_led_stats = 0;
98 static report_keyboard_t keyboard_report_sent;
101 static uint8_t keyboard_leds(void);
102 static void send_keyboard(report_keyboard_t *report);
103 static void send_mouse(report_mouse_t *report);
104 static void send_system(uint16_t data);
105 static void send_consumer(uint16_t data);
106 host_driver_t lufa_driver = {
114 #ifdef VIRTSER_ENABLE
115 USB_ClassInfo_CDC_Device_t cdc_device =
119 .ControlInterfaceNumber = CCI_INTERFACE,
122 .Address = CDC_IN_EPADDR,
128 .Address = CDC_OUT_EPADDR,
132 .NotificationEndpoint =
134 .Address = CDC_NOTIFICATION_EPADDR,
135 .Size = CDC_NOTIFICATION_EPSIZE,
144 void raw_hid_send( uint8_t *data, uint8_t length )
146 // TODO: implement variable size packet
147 if ( length != RAW_EPSIZE )
152 if (USB_DeviceState != DEVICE_STATE_Configured)
157 // TODO: decide if we allow calls to raw_hid_send() in the middle
158 // of other endpoint usage.
159 uint8_t ep = Endpoint_GetCurrentEndpoint();
161 Endpoint_SelectEndpoint(RAW_IN_EPNUM);
163 // Check to see if the host is ready to accept another packet
164 if (Endpoint_IsINReady())
167 Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
168 // Finalize the stream transfer to send the last packet
172 Endpoint_SelectEndpoint(ep);
175 __attribute__ ((weak))
176 void raw_hid_receive( uint8_t *data, uint8_t length )
178 // Users should #include "raw_hid.h" in their own code
179 // and implement this function there. Leave this as weak linkage
180 // so users can opt to not handle data coming in.
183 static void raw_hid_task(void)
185 // Create a temporary buffer to hold the read in data from the host
186 uint8_t data[RAW_EPSIZE];
187 bool data_read = false;
189 // Device must be connected and configured for the task to run
190 if (USB_DeviceState != DEVICE_STATE_Configured)
193 Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
195 // Check to see if a packet has been sent from the host
196 if (Endpoint_IsOUTReceived())
198 // Check to see if the packet contains data
199 if (Endpoint_IsReadWriteAllowed())
202 Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
206 // Finalize the stream transfer to receive the last packet
211 raw_hid_receive( data, sizeof(data) );
217 /*******************************************************************************
219 ******************************************************************************/
220 #ifdef CONSOLE_ENABLE
221 static void Console_Task(void)
223 /* Device must be connected and configured for the task to run */
224 if (USB_DeviceState != DEVICE_STATE_Configured)
227 uint8_t ep = Endpoint_GetCurrentEndpoint();
230 // TODO: impl receivechar()/recvchar()
231 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
233 /* Check to see if a packet has been sent from the host */
234 if (Endpoint_IsOUTReceived())
236 /* Check to see if the packet contains data */
237 if (Endpoint_IsReadWriteAllowed())
239 /* Create a temporary buffer to hold the read in report from the host */
240 uint8_t ConsoleData[CONSOLE_EPSIZE];
242 /* Read Console Report Data */
243 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
245 /* Process Console Report Data */
246 //ProcessConsoleHIDReport(ConsoleData);
249 /* Finalize the stream transfer to send the last packet */
255 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
256 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
257 Endpoint_SelectEndpoint(ep);
262 while (Endpoint_IsReadWriteAllowed())
265 // flash senchar packet
266 if (Endpoint_IsINReady()) {
270 Endpoint_SelectEndpoint(ep);
275 /*******************************************************************************
277 ******************************************************************************/
279 * Event Order of Plug in:
280 * 0) EVENT_USB_Device_Connect
281 * 1) EVENT_USB_Device_Suspend
282 * 2) EVENT_USB_Device_Reset
283 * 3) EVENT_USB_Device_Wake
285 void EVENT_USB_Device_Connect(void)
288 /* For battery powered device */
289 if (!USB_IsInitialized) {
292 USB_Device_EnableSOFEvents();
296 void EVENT_USB_Device_Disconnect(void)
299 /* For battery powered device */
300 USB_IsInitialized = false;
301 /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
302 if (USB_IsInitialized) {
303 USB_Disable(); // Disable all interrupts
304 USB_Controller_Enable();
305 USB_INT_Enable(USB_INT_VBUSTI);
310 void EVENT_USB_Device_Reset(void)
315 void EVENT_USB_Device_Suspend()
318 #ifdef SLEEP_LED_ENABLE
323 void EVENT_USB_Device_WakeUp()
326 suspend_wakeup_init();
328 #ifdef SLEEP_LED_ENABLE
330 // NOTE: converters may not accept this
331 led_set(host_keyboard_leds());
337 #ifdef CONSOLE_ENABLE
338 static bool console_flush = false;
339 #define CONSOLE_FLUSH_SET(b) do { \
340 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\
346 void EVENT_USB_Device_StartOfFrame(void)
348 static uint8_t count;
349 if (++count % 50) return;
352 if (!console_flush) return;
354 console_flush = false;
359 /** Event handler for the USB_ConfigurationChanged event.
360 * This is fired when the host sets the current configuration of the USB device after enumeration.
362 * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
363 * it is safe to use singl bank for all endpoints.
365 void EVENT_USB_Device_ConfigurationChanged(void)
367 bool ConfigSuccess = true;
369 /* Setup Keyboard HID Report Endpoints */
370 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
371 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
374 /* Setup Mouse HID Report Endpoint */
375 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
376 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
379 #ifdef EXTRAKEY_ENABLE
380 /* Setup Extra HID Report Endpoint */
381 ConfigSuccess &= ENDPOINT_CONFIG(EXTRAKEY_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
382 EXTRAKEY_EPSIZE, ENDPOINT_BANK_SINGLE);
386 /* Setup Raw HID Report Endpoints */
387 ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
388 RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
389 ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
390 RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
393 #ifdef CONSOLE_ENABLE
394 /* Setup Console HID Report Endpoints */
395 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
396 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
398 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
399 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
404 /* Setup NKRO HID Report Endpoints */
405 ConfigSuccess &= ENDPOINT_CONFIG(NKRO_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
406 NKRO_EPSIZE, ENDPOINT_BANK_SINGLE);
410 ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
411 ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
414 #ifdef VIRTSER_ENABLE
415 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
416 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
417 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
422 Appendix G: HID Request Support Requirements
424 The following table enumerates the requests that need to be supported by various types of HID class devices.
426 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
427 ------------------------------------------------------------------------------------------
428 Boot Mouse Required Optional Optional Optional Required Required
429 Non-Boot Mouse Required Optional Optional Optional Optional Optional
430 Boot Keyboard Required Optional Required Required Required Required
431 Non-Boot Keybrd Required Optional Required Required Optional Optional
432 Other Device Required Optional Optional Optional Optional Optional
434 /** Event handler for the USB_ControlRequest event.
435 * This is fired before passing along unhandled control requests to the library for processing internally.
437 void EVENT_USB_Device_ControlRequest(void)
439 uint8_t* ReportData = NULL;
440 uint8_t ReportSize = 0;
442 /* Handle HID Class specific requests */
443 switch (USB_ControlRequest.bRequest)
445 case HID_REQ_GetReport:
446 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
448 Endpoint_ClearSETUP();
451 switch (USB_ControlRequest.wIndex) {
452 case KEYBOARD_INTERFACE:
454 ReportData = (uint8_t*)&keyboard_report_sent;
455 ReportSize = sizeof(keyboard_report_sent);
459 /* Write the report data to the control endpoint */
460 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
465 case HID_REQ_SetReport:
466 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
470 switch (USB_ControlRequest.wIndex) {
471 case KEYBOARD_INTERFACE:
475 Endpoint_ClearSETUP();
477 while (!(Endpoint_IsOUTReceived())) {
478 if (USB_DeviceState == DEVICE_STATE_Unattached)
481 keyboard_led_stats = Endpoint_Read_8();
484 Endpoint_ClearStatusStage();
492 case HID_REQ_GetProtocol:
493 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
495 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
496 Endpoint_ClearSETUP();
497 while (!(Endpoint_IsINReady()));
498 Endpoint_Write_8(keyboard_protocol);
500 Endpoint_ClearStatusStage();
505 case HID_REQ_SetProtocol:
506 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
508 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
509 Endpoint_ClearSETUP();
510 Endpoint_ClearStatusStage();
512 keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
518 case HID_REQ_SetIdle:
519 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
521 Endpoint_ClearSETUP();
522 Endpoint_ClearStatusStage();
524 keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
528 case HID_REQ_GetIdle:
529 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
531 Endpoint_ClearSETUP();
532 while (!(Endpoint_IsINReady()));
533 Endpoint_Write_8(keyboard_idle);
535 Endpoint_ClearStatusStage();
541 #ifdef VIRTSER_ENABLE
542 CDC_Device_ProcessControlRequest(&cdc_device);
546 /*******************************************************************************
548 ******************************************************************************/
549 static uint8_t keyboard_leds(void)
551 return keyboard_led_stats;
554 static void send_keyboard(report_keyboard_t *report)
556 uint8_t timeout = 255;
557 uint8_t where = where_to_send();
559 #ifdef BLUETOOTH_ENABLE
560 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
561 #ifdef MODULE_ADAFRUIT_BLE
562 adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
564 bluefruit_serial_send(0xFD);
565 bluefruit_serial_send(0x09);
566 bluefruit_serial_send(0x01);
567 for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) {
568 bluefruit_serial_send(report->raw[i]);
571 bluefruit_serial_send(0xFD);
572 for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++) {
573 bluefruit_serial_send(report->raw[i]);
579 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
583 /* Select the Keyboard Report Endpoint */
585 if (keyboard_protocol && keymap_config.nkro) {
586 /* Report protocol - NKRO */
587 Endpoint_SelectEndpoint(NKRO_IN_EPNUM);
589 /* Check if write ready for a polling interval around 1ms */
590 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(4);
591 if (!Endpoint_IsReadWriteAllowed()) return;
593 /* Write Keyboard Report Data */
594 Endpoint_Write_Stream_LE(report, NKRO_EPSIZE, NULL);
600 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
602 /* Check if write ready for a polling interval around 10ms */
603 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
604 if (!Endpoint_IsReadWriteAllowed()) return;
606 /* Write Keyboard Report Data */
607 Endpoint_Write_Stream_LE(report, KEYBOARD_EPSIZE, NULL);
610 /* Finalize the stream transfer to send the last packet */
613 keyboard_report_sent = *report;
616 static void send_mouse(report_mouse_t *report)
619 uint8_t timeout = 255;
620 uint8_t where = where_to_send();
622 #ifdef BLUETOOTH_ENABLE
623 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
624 #ifdef MODULE_ADAFRUIT_BLE
625 // FIXME: mouse buttons
626 adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
628 bluefruit_serial_send(0xFD);
629 bluefruit_serial_send(0x00);
630 bluefruit_serial_send(0x03);
631 bluefruit_serial_send(report->buttons);
632 bluefruit_serial_send(report->x);
633 bluefruit_serial_send(report->y);
634 bluefruit_serial_send(report->v); // should try sending the wheel v here
635 bluefruit_serial_send(report->h); // should try sending the wheel h here
636 bluefruit_serial_send(0x00);
641 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
645 /* Select the Mouse Report Endpoint */
646 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
648 /* Check if write ready for a polling interval around 10ms */
649 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
650 if (!Endpoint_IsReadWriteAllowed()) return;
652 /* Write Mouse Report Data */
653 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
655 /* Finalize the stream transfer to send the last packet */
660 static void send_system(uint16_t data)
662 uint8_t timeout = 255;
664 if (USB_DeviceState != DEVICE_STATE_Configured)
668 .report_id = REPORT_ID_SYSTEM,
669 .usage = data - SYSTEM_POWER_DOWN + 1
671 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
673 /* Check if write ready for a polling interval around 10ms */
674 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
675 if (!Endpoint_IsReadWriteAllowed()) return;
677 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
681 static void send_consumer(uint16_t data)
683 uint8_t timeout = 255;
684 uint8_t where = where_to_send();
686 #ifdef BLUETOOTH_ENABLE
687 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
688 #ifdef MODULE_ADAFRUIT_BLE
689 adafruit_ble_send_consumer_key(data, 0);
691 static uint16_t last_data = 0;
692 if (data == last_data) return;
694 uint16_t bitmap = CONSUMER2RN42(data);
695 bluefruit_serial_send(0xFD);
696 bluefruit_serial_send(0x03);
697 bluefruit_serial_send(0x03);
698 bluefruit_serial_send(bitmap&0xFF);
699 bluefruit_serial_send((bitmap>>8)&0xFF);
701 static uint16_t last_data = 0;
702 if (data == last_data) return;
704 uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
705 bluefruit_serial_send(0xFD);
706 bluefruit_serial_send(0x00);
707 bluefruit_serial_send(0x02);
708 bluefruit_serial_send((bitmap>>8)&0xFF);
709 bluefruit_serial_send(bitmap&0xFF);
710 bluefruit_serial_send(0x00);
711 bluefruit_serial_send(0x00);
712 bluefruit_serial_send(0x00);
713 bluefruit_serial_send(0x00);
718 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
723 .report_id = REPORT_ID_CONSUMER,
726 Endpoint_SelectEndpoint(EXTRAKEY_IN_EPNUM);
728 /* Check if write ready for a polling interval around 10ms */
729 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
730 if (!Endpoint_IsReadWriteAllowed()) return;
732 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
737 /*******************************************************************************
739 ******************************************************************************/
740 #ifdef CONSOLE_ENABLE
741 #define SEND_TIMEOUT 5
742 int8_t sendchar(uint8_t c)
744 // Not wait once timeouted.
745 // Because sendchar() is called so many times, waiting each call causes big lag.
746 static bool timeouted = false;
748 // prevents Console_Task() from running during sendchar() runs.
749 // or char will be lost. These two function is mutually exclusive.
750 CONSOLE_FLUSH_SET(false);
752 if (USB_DeviceState != DEVICE_STATE_Configured)
755 uint8_t ep = Endpoint_GetCurrentEndpoint();
756 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
757 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
761 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
767 uint8_t timeout = SEND_TIMEOUT;
768 while (!Endpoint_IsReadWriteAllowed()) {
769 if (USB_DeviceState != DEVICE_STATE_Configured) {
772 if (Endpoint_IsStalled()) {
784 // send when bank is full
785 if (!Endpoint_IsReadWriteAllowed()) {
786 while (!(Endpoint_IsINReady()));
789 CONSOLE_FLUSH_SET(true);
792 Endpoint_SelectEndpoint(ep);
795 Endpoint_SelectEndpoint(ep);
799 int8_t sendchar(uint8_t c)
805 /*******************************************************************************
807 ******************************************************************************/
810 USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
814 .StreamingInterfaceNumber = AS_INTERFACE,
817 .Address = MIDI_STREAM_IN_EPADDR,
818 .Size = MIDI_STREAM_EPSIZE,
823 .Address = MIDI_STREAM_OUT_EPADDR,
824 .Size = MIDI_STREAM_EPSIZE,
830 void send_midi_packet(MIDI_EventPacket_t* event) {
831 MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event);
834 bool recv_midi_packet(MIDI_EventPacket_t* const event) {
835 return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event);
840 /*******************************************************************************
842 ******************************************************************************/
844 #ifdef VIRTSER_ENABLE
845 void virtser_init(void)
847 cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ;
848 CDC_Device_SendControlLineStateChange(&cdc_device);
851 void virtser_recv(uint8_t c) __attribute__ ((weak));
852 void virtser_recv(uint8_t c)
857 void virtser_task(void)
859 uint16_t count = CDC_Device_BytesReceived(&cdc_device);
863 ch = CDC_Device_ReceiveByte(&cdc_device);
867 void virtser_send(const uint8_t byte)
869 uint8_t timeout = 255;
870 uint8_t ep = Endpoint_GetCurrentEndpoint();
872 if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
875 Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
877 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
878 Endpoint_SelectEndpoint(ep);
882 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
884 Endpoint_Write_8(byte);
885 CDC_Device_Flush(&cdc_device);
887 if (Endpoint_IsINReady()) {
891 Endpoint_SelectEndpoint(ep);
896 /*******************************************************************************
898 ******************************************************************************/
899 static void setup_mcu(void)
901 /* Disable watchdog if enabled by bootloader/fuses */
902 MCUSR &= ~(1 << WDRF);
905 /* Disable clock division */
906 // clock_prescale_set(clock_div_1);
908 CLKPR = (1 << CLKPCE);
909 CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
912 static void setup_usb(void)
914 // Leonardo needs. Without this USB device is not recognized.
920 USB_Device_EnableSOFEvents();
921 print_set_sendchar(sendchar);
924 int main(void) __attribute__ ((weak));
936 #if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
940 /* wait for USB startup & debug output */
943 while (USB_DeviceState != DEVICE_STATE_Configured) {
944 #if defined(INTERRUPT_CONTROL_ENDPOINT)
950 print("USB configured.\n");
956 host_set_driver(&lufa_driver);
957 #ifdef SLEEP_LED_ENABLE
961 #ifdef VIRTSER_ENABLE
965 print("Keyboard start.\n");
967 #if !defined(NO_USB_STARTUP_CHECK)
968 while (USB_DeviceState == DEVICE_STATE_Suspended) {
970 suspend_power_down();
971 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
972 USB_Device_SendRemoteWakeup();
980 MIDI_Device_USBTask(&USB_MIDI_Interface);
983 #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
987 #ifdef MODULE_ADAFRUIT_BLE
991 #ifdef VIRTSER_ENABLE
993 CDC_Device_USBTask(&cdc_device);
1000 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
1007 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
1008 const uint16_t wIndex,
1009 const void** const DescriptorAddress)
1011 return get_usb_descriptor(wValue, wIndex, DescriptorAddress);