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"
57 #include "rgblight_reconfig.h"
60 #include "keycode_config.h"
62 extern keymap_config_t keymap_config;
70 #ifdef BLUETOOTH_ENABLE
71 #ifdef MODULE_ADAFRUIT_BLE
72 #include "adafruit_ble.h"
74 #include "bluetooth.h"
82 #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
94 uint8_t keyboard_idle = 0;
95 /* 0: Boot Protocol, 1: Report Protocol(default) */
96 uint8_t keyboard_protocol = 1;
97 static uint8_t keyboard_led_stats = 0;
99 static report_keyboard_t keyboard_report_sent;
102 static uint8_t keyboard_leds(void);
103 static void send_keyboard(report_keyboard_t *report);
104 static void send_mouse(report_mouse_t *report);
105 static void send_system(uint16_t data);
106 static void send_consumer(uint16_t data);
107 host_driver_t lufa_driver = {
115 #ifdef VIRTSER_ENABLE
116 USB_ClassInfo_CDC_Device_t cdc_device =
120 .ControlInterfaceNumber = CCI_INTERFACE,
123 .Address = CDC_IN_EPADDR,
129 .Address = CDC_OUT_EPADDR,
133 .NotificationEndpoint =
135 .Address = CDC_NOTIFICATION_EPADDR,
136 .Size = CDC_NOTIFICATION_EPSIZE,
145 /** \brief Raw HID Send
149 void raw_hid_send( uint8_t *data, uint8_t length )
151 // TODO: implement variable size packet
152 if ( length != RAW_EPSIZE )
157 if (USB_DeviceState != DEVICE_STATE_Configured)
162 // TODO: decide if we allow calls to raw_hid_send() in the middle
163 // of other endpoint usage.
164 uint8_t ep = Endpoint_GetCurrentEndpoint();
166 Endpoint_SelectEndpoint(RAW_IN_EPNUM);
168 // Check to see if the host is ready to accept another packet
169 if (Endpoint_IsINReady())
172 Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
173 // Finalize the stream transfer to send the last packet
177 Endpoint_SelectEndpoint(ep);
180 /** \brief Raw HID Receive
184 __attribute__ ((weak))
185 void raw_hid_receive( uint8_t *data, uint8_t length )
187 // Users should #include "raw_hid.h" in their own code
188 // and implement this function there. Leave this as weak linkage
189 // so users can opt to not handle data coming in.
192 /** \brief Raw HID Task
196 static void raw_hid_task(void)
198 // Create a temporary buffer to hold the read in data from the host
199 uint8_t data[RAW_EPSIZE];
200 bool data_read = false;
202 // Device must be connected and configured for the task to run
203 if (USB_DeviceState != DEVICE_STATE_Configured)
206 Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
208 // Check to see if a packet has been sent from the host
209 if (Endpoint_IsOUTReceived())
211 // Check to see if the packet contains data
212 if (Endpoint_IsReadWriteAllowed())
215 Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
219 // Finalize the stream transfer to receive the last packet
224 raw_hid_receive( data, sizeof(data) );
230 /*******************************************************************************
232 ******************************************************************************/
233 #ifdef CONSOLE_ENABLE
234 /** \brief Console Task
238 static void Console_Task(void)
240 /* Device must be connected and configured for the task to run */
241 if (USB_DeviceState != DEVICE_STATE_Configured)
244 uint8_t ep = Endpoint_GetCurrentEndpoint();
247 // TODO: impl receivechar()/recvchar()
248 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
250 /* Check to see if a packet has been sent from the host */
251 if (Endpoint_IsOUTReceived())
253 /* Check to see if the packet contains data */
254 if (Endpoint_IsReadWriteAllowed())
256 /* Create a temporary buffer to hold the read in report from the host */
257 uint8_t ConsoleData[CONSOLE_EPSIZE];
259 /* Read Console Report Data */
260 Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
262 /* Process Console Report Data */
263 //ProcessConsoleHIDReport(ConsoleData);
266 /* Finalize the stream transfer to send the last packet */
272 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
273 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
274 Endpoint_SelectEndpoint(ep);
279 while (Endpoint_IsReadWriteAllowed())
282 // flash senchar packet
283 if (Endpoint_IsINReady()) {
287 Endpoint_SelectEndpoint(ep);
292 /*******************************************************************************
294 ******************************************************************************/
296 * Event Order of Plug in:
297 * 0) EVENT_USB_Device_Connect
298 * 1) EVENT_USB_Device_Suspend
299 * 2) EVENT_USB_Device_Reset
300 * 3) EVENT_USB_Device_Wake
302 /** \brief Event USB Device Connect
306 void EVENT_USB_Device_Connect(void)
309 /* For battery powered device */
310 if (!USB_IsInitialized) {
313 USB_Device_EnableSOFEvents();
317 /** \brief Event USB Device Connect
321 void EVENT_USB_Device_Disconnect(void)
324 /* For battery powered device */
325 USB_IsInitialized = false;
326 /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
327 if (USB_IsInitialized) {
328 USB_Disable(); // Disable all interrupts
329 USB_Controller_Enable();
330 USB_INT_Enable(USB_INT_VBUSTI);
335 /** \brief Event USB Device Connect
339 void EVENT_USB_Device_Reset(void)
344 /** \brief Event USB Device Connect
348 void EVENT_USB_Device_Suspend()
351 #ifdef SLEEP_LED_ENABLE
356 /** \brief Event USB Device Connect
360 void EVENT_USB_Device_WakeUp()
363 suspend_wakeup_init();
365 #ifdef SLEEP_LED_ENABLE
367 // NOTE: converters may not accept this
368 led_set(host_keyboard_leds());
374 #ifdef CONSOLE_ENABLE
375 static bool console_flush = false;
376 #define CONSOLE_FLUSH_SET(b) do { \
377 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\
382 /** \brief Event USB Device Start Of Frame
387 void EVENT_USB_Device_StartOfFrame(void)
389 static uint8_t count;
390 if (++count % 50) return;
393 if (!console_flush) return;
395 console_flush = false;
400 /** \brief Event handler for the USB_ConfigurationChanged event.
402 * This is fired when the host sets the current configuration of the USB device after enumeration.
404 * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
405 * it is safe to use single bank for all endpoints.
407 void EVENT_USB_Device_ConfigurationChanged(void)
409 bool ConfigSuccess = true;
411 /* Setup Keyboard HID Report Endpoints */
412 #ifndef KEYBOARD_SHARED_EP
413 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
414 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
417 #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
418 /* Setup Mouse HID Report Endpoint */
419 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
420 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
423 #ifdef SHARED_EP_ENABLE
424 /* Setup Shared HID Report Endpoint */
425 ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
426 SHARED_EPSIZE, ENDPOINT_BANK_SINGLE);
430 /* Setup Raw HID Report Endpoints */
431 ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
432 RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
433 ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
434 RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
437 #ifdef CONSOLE_ENABLE
438 /* Setup Console HID Report Endpoints */
439 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
440 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
442 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
443 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
448 ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
449 ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
452 #ifdef VIRTSER_ENABLE
453 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
454 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
455 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
459 /* FIXME: Expose this table in the docs somehow
460 Appendix G: HID Request Support Requirements
462 The following table enumerates the requests that need to be supported by various types of HID class devices.
464 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
465 ------------------------------------------------------------------------------------------
466 Boot Mouse Required Optional Optional Optional Required Required
467 Non-Boot Mouse Required Optional Optional Optional Optional Optional
468 Boot Keyboard Required Optional Required Required Required Required
469 Non-Boot Keybrd Required Optional Required Required Optional Optional
470 Other Device Required Optional Optional Optional Optional Optional
472 /** \brief Event handler for the USB_ControlRequest event.
474 * This is fired before passing along unhandled control requests to the library for processing internally.
476 void EVENT_USB_Device_ControlRequest(void)
478 uint8_t* ReportData = NULL;
479 uint8_t ReportSize = 0;
481 /* Handle HID Class specific requests */
482 switch (USB_ControlRequest.bRequest)
484 case HID_REQ_GetReport:
485 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
487 Endpoint_ClearSETUP();
490 switch (USB_ControlRequest.wIndex) {
491 case KEYBOARD_INTERFACE:
493 ReportData = (uint8_t*)&keyboard_report_sent;
494 ReportSize = sizeof(keyboard_report_sent);
498 /* Write the report data to the control endpoint */
499 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
504 case HID_REQ_SetReport:
505 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
509 switch (USB_ControlRequest.wIndex) {
510 case KEYBOARD_INTERFACE:
511 #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
512 case SHARED_INTERFACE:
514 Endpoint_ClearSETUP();
516 while (!(Endpoint_IsOUTReceived())) {
517 if (USB_DeviceState == DEVICE_STATE_Unattached)
521 if (Endpoint_BytesInEndpoint() == 2) {
522 uint8_t report_id = REPORT_ID_KEYBOARD;
524 if (keyboard_protocol) {
525 report_id = Endpoint_Read_8();
528 if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) {
529 keyboard_led_stats = Endpoint_Read_8();
532 keyboard_led_stats = Endpoint_Read_8();
536 Endpoint_ClearStatusStage();
544 case HID_REQ_GetProtocol:
545 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
547 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
548 Endpoint_ClearSETUP();
549 while (!(Endpoint_IsINReady()));
550 Endpoint_Write_8(keyboard_protocol);
552 Endpoint_ClearStatusStage();
557 case HID_REQ_SetProtocol:
558 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
560 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
561 Endpoint_ClearSETUP();
562 Endpoint_ClearStatusStage();
564 keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
570 case HID_REQ_SetIdle:
571 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
573 Endpoint_ClearSETUP();
574 Endpoint_ClearStatusStage();
576 keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
580 case HID_REQ_GetIdle:
581 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
583 Endpoint_ClearSETUP();
584 while (!(Endpoint_IsINReady()));
585 Endpoint_Write_8(keyboard_idle);
587 Endpoint_ClearStatusStage();
593 #ifdef VIRTSER_ENABLE
594 CDC_Device_ProcessControlRequest(&cdc_device);
598 /*******************************************************************************
600 ******************************************************************************/
601 /** \brief Keyboard LEDs
605 static uint8_t keyboard_leds(void)
607 return keyboard_led_stats;
610 /** \brief Send Keyboard
614 static void send_keyboard(report_keyboard_t *report)
616 uint8_t timeout = 255;
617 uint8_t where = where_to_send();
619 #ifdef BLUETOOTH_ENABLE
620 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
621 #ifdef MODULE_ADAFRUIT_BLE
622 adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
624 bluefruit_serial_send(0xFD);
625 bluefruit_serial_send(0x09);
626 bluefruit_serial_send(0x01);
627 bluefruit_serial_send(report->mods);
628 bluefruit_serial_send(report->reserved);
629 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
630 bluefruit_serial_send(report->keys[i]);
633 bluefruit_serial_send(0xFD);
634 bluefruit_serial_send(report->mods);
635 bluefruit_serial_send(report->reserved);
636 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
637 bluefruit_serial_send(report->keys[i]);
643 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
647 /* Select the Keyboard Report Endpoint */
648 uint8_t ep = KEYBOARD_IN_EPNUM;
649 uint8_t size = KEYBOARD_REPORT_SIZE;
651 if (keyboard_protocol && keymap_config.nkro) {
652 ep = SHARED_IN_EPNUM;
653 size = sizeof(struct nkro_report);
656 Endpoint_SelectEndpoint(ep);
657 /* Check if write ready for a polling interval around 10ms */
658 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
659 if (!Endpoint_IsReadWriteAllowed()) return;
661 /* If we're in Boot Protocol, don't send any report ID or other funky fields */
662 if (!keyboard_protocol) {
663 Endpoint_Write_Stream_LE(&report->mods, 8, NULL);
665 Endpoint_Write_Stream_LE(report, size, NULL);
668 /* Finalize the stream transfer to send the last packet */
671 keyboard_report_sent = *report;
674 /** \brief Send Mouse
678 static void send_mouse(report_mouse_t *report)
681 uint8_t timeout = 255;
682 uint8_t where = where_to_send();
684 #ifdef BLUETOOTH_ENABLE
685 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
686 #ifdef MODULE_ADAFRUIT_BLE
687 // FIXME: mouse buttons
688 adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
690 bluefruit_serial_send(0xFD);
691 bluefruit_serial_send(0x00);
692 bluefruit_serial_send(0x03);
693 bluefruit_serial_send(report->buttons);
694 bluefruit_serial_send(report->x);
695 bluefruit_serial_send(report->y);
696 bluefruit_serial_send(report->v); // should try sending the wheel v here
697 bluefruit_serial_send(report->h); // should try sending the wheel h here
698 bluefruit_serial_send(0x00);
703 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
707 /* Select the Mouse Report Endpoint */
708 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
710 /* Check if write ready for a polling interval around 10ms */
711 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
712 if (!Endpoint_IsReadWriteAllowed()) return;
714 /* Write Mouse Report Data */
715 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
717 /* Finalize the stream transfer to send the last packet */
722 /** \brief Send System
726 static void send_system(uint16_t data)
728 #ifdef EXTRAKEY_ENABLE
729 uint8_t timeout = 255;
731 if (USB_DeviceState != DEVICE_STATE_Configured)
735 .report_id = REPORT_ID_SYSTEM,
736 .usage = data - SYSTEM_POWER_DOWN + 1
738 Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
740 /* Check if write ready for a polling interval around 10ms */
741 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
742 if (!Endpoint_IsReadWriteAllowed()) return;
744 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
749 /** \brief Send Consumer
753 static void send_consumer(uint16_t data)
755 #ifdef EXTRAKEY_ENABLE
756 uint8_t timeout = 255;
757 uint8_t where = where_to_send();
759 #ifdef BLUETOOTH_ENABLE
760 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
761 #ifdef MODULE_ADAFRUIT_BLE
762 adafruit_ble_send_consumer_key(data, 0);
764 static uint16_t last_data = 0;
765 if (data == last_data) return;
767 uint16_t bitmap = CONSUMER2RN42(data);
768 bluefruit_serial_send(0xFD);
769 bluefruit_serial_send(0x03);
770 bluefruit_serial_send(0x03);
771 bluefruit_serial_send(bitmap&0xFF);
772 bluefruit_serial_send((bitmap>>8)&0xFF);
774 static uint16_t last_data = 0;
775 if (data == last_data) return;
777 uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
778 bluefruit_serial_send(0xFD);
779 bluefruit_serial_send(0x00);
780 bluefruit_serial_send(0x02);
781 bluefruit_serial_send((bitmap>>8)&0xFF);
782 bluefruit_serial_send(bitmap&0xFF);
783 bluefruit_serial_send(0x00);
784 bluefruit_serial_send(0x00);
785 bluefruit_serial_send(0x00);
786 bluefruit_serial_send(0x00);
791 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
796 .report_id = REPORT_ID_CONSUMER,
799 Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
801 /* Check if write ready for a polling interval around 10ms */
802 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
803 if (!Endpoint_IsReadWriteAllowed()) return;
805 Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
811 /*******************************************************************************
813 ******************************************************************************/
814 #ifdef CONSOLE_ENABLE
815 #define SEND_TIMEOUT 5
820 int8_t sendchar(uint8_t c)
822 // Not wait once timeouted.
823 // Because sendchar() is called so many times, waiting each call causes big lag.
824 static bool timeouted = false;
826 // prevents Console_Task() from running during sendchar() runs.
827 // or char will be lost. These two function is mutually exclusive.
828 CONSOLE_FLUSH_SET(false);
830 if (USB_DeviceState != DEVICE_STATE_Configured)
833 uint8_t ep = Endpoint_GetCurrentEndpoint();
834 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
835 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
839 if (timeouted && !Endpoint_IsReadWriteAllowed()) {
845 uint8_t timeout = SEND_TIMEOUT;
846 while (!Endpoint_IsReadWriteAllowed()) {
847 if (USB_DeviceState != DEVICE_STATE_Configured) {
850 if (Endpoint_IsStalled()) {
862 // send when bank is full
863 if (!Endpoint_IsReadWriteAllowed()) {
864 while (!(Endpoint_IsINReady()));
867 CONSOLE_FLUSH_SET(true);
870 Endpoint_SelectEndpoint(ep);
873 Endpoint_SelectEndpoint(ep);
877 int8_t sendchar(uint8_t c)
883 /*******************************************************************************
885 ******************************************************************************/
888 USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
892 .StreamingInterfaceNumber = AS_INTERFACE,
895 .Address = MIDI_STREAM_IN_EPADDR,
896 .Size = MIDI_STREAM_EPSIZE,
901 .Address = MIDI_STREAM_OUT_EPADDR,
902 .Size = MIDI_STREAM_EPSIZE,
908 void send_midi_packet(MIDI_EventPacket_t* event) {
909 MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event);
912 bool recv_midi_packet(MIDI_EventPacket_t* const event) {
913 return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event);
918 /*******************************************************************************
920 ******************************************************************************/
922 #ifdef VIRTSER_ENABLE
923 /** \brief Virtual Serial Init
927 void virtser_init(void)
929 cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ;
930 CDC_Device_SendControlLineStateChange(&cdc_device);
933 /** \brief Virtual Serial Receive
937 void virtser_recv(uint8_t c) __attribute__ ((weak));
938 void virtser_recv(uint8_t c)
943 /** \brief Virtual Serial Task
947 void virtser_task(void)
949 uint16_t count = CDC_Device_BytesReceived(&cdc_device);
953 ch = CDC_Device_ReceiveByte(&cdc_device);
957 /** \brief Virtual Serial Send
961 void virtser_send(const uint8_t byte)
963 uint8_t timeout = 255;
964 uint8_t ep = Endpoint_GetCurrentEndpoint();
966 if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
969 Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
971 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
972 Endpoint_SelectEndpoint(ep);
976 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
978 Endpoint_Write_8(byte);
979 CDC_Device_Flush(&cdc_device);
981 if (Endpoint_IsINReady()) {
985 Endpoint_SelectEndpoint(ep);
990 /*******************************************************************************
992 ******************************************************************************/
997 static void setup_mcu(void)
999 /* Disable watchdog if enabled by bootloader/fuses */
1000 MCUSR &= ~(1 << WDRF);
1003 /* Disable clock division */
1004 // clock_prescale_set(clock_div_1);
1006 CLKPR = (1 << CLKPCE);
1007 CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
1010 /** \brief Setup USB
1014 static void setup_usb(void)
1016 // Leonardo needs. Without this USB device is not recognized.
1022 USB_Device_EnableSOFEvents();
1023 print_set_sendchar(sendchar);
1030 int main(void) __attribute__ ((weak));
1042 #if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
1046 /* wait for USB startup & debug output */
1049 while (USB_DeviceState != DEVICE_STATE_Configured) {
1050 #if defined(INTERRUPT_CONTROL_ENDPOINT)
1056 print("USB configured.\n");
1062 host_set_driver(&lufa_driver);
1063 #ifdef SLEEP_LED_ENABLE
1067 #ifdef VIRTSER_ENABLE
1071 print("Keyboard start.\n");
1073 #if !defined(NO_USB_STARTUP_CHECK)
1074 while (USB_DeviceState == DEVICE_STATE_Suspended) {
1076 suspend_power_down();
1077 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
1078 USB_Device_SendRemoteWakeup();
1086 MIDI_Device_USBTask(&USB_MIDI_Interface);
1089 #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
1093 #ifdef MODULE_ADAFRUIT_BLE
1094 adafruit_ble_task();
1097 #ifdef VIRTSER_ENABLE
1099 CDC_Device_USBTask(&cdc_device);
1106 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
1113 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
1114 const uint16_t wIndex,
1115 const void** const DescriptorAddress)
1117 return get_usb_descriptor(wValue, wIndex, DescriptorAddress);