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
;
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)
82 # include "rgblight.h"
86 # include "qmk_midi.h"
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
= {
107 keyboard_leds
, send_keyboard
, send_mouse
, send_system
, send_consumer
,
110 #ifdef VIRTSER_ENABLE
111 USB_ClassInfo_CDC_Device_t cdc_device
= {
114 .ControlInterfaceNumber
= CCI_INTERFACE
,
117 .Address
= CDC_IN_EPADDR
,
123 .Address
= CDC_OUT_EPADDR
,
127 .NotificationEndpoint
=
129 .Address
= CDC_NOTIFICATION_EPADDR
,
130 .Size
= CDC_NOTIFICATION_EPSIZE
,
139 /** \brief Raw HID Send
143 void raw_hid_send(uint8_t *data
, uint8_t length
) {
144 // TODO: implement variable size packet
145 if (length
!= RAW_EPSIZE
) {
149 if (USB_DeviceState
!= DEVICE_STATE_Configured
) {
153 // TODO: decide if we allow calls to raw_hid_send() in the middle
154 // of other endpoint usage.
155 uint8_t ep
= Endpoint_GetCurrentEndpoint();
157 Endpoint_SelectEndpoint(RAW_IN_EPNUM
);
159 // Check to see if the host is ready to accept another packet
160 if (Endpoint_IsINReady()) {
162 Endpoint_Write_Stream_LE(data
, RAW_EPSIZE
, NULL
);
163 // Finalize the stream transfer to send the last packet
167 Endpoint_SelectEndpoint(ep
);
170 /** \brief Raw HID Receive
174 __attribute__((weak
)) void raw_hid_receive(uint8_t *data
, uint8_t length
) {
175 // Users should #include "raw_hid.h" in their own code
176 // and implement this function there. Leave this as weak linkage
177 // so users can opt to not handle data coming in.
180 /** \brief Raw HID Task
184 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
) return;
192 Endpoint_SelectEndpoint(RAW_OUT_EPNUM
);
194 // Check to see if a packet has been sent from the host
195 if (Endpoint_IsOUTReceived()) {
196 // Check to see if the packet contains data
197 if (Endpoint_IsReadWriteAllowed()) {
199 Endpoint_Read_Stream_LE(data
, sizeof(data
), NULL
);
203 // Finalize the stream transfer to receive the last packet
207 raw_hid_receive(data
, sizeof(data
));
213 /*******************************************************************************
215 ******************************************************************************/
216 #ifdef CONSOLE_ENABLE
217 /** \brief Console Task
221 static void Console_Task(void) {
222 /* Device must be connected and configured for the task to run */
223 if (USB_DeviceState
!= DEVICE_STATE_Configured
) return;
225 uint8_t ep
= Endpoint_GetCurrentEndpoint();
228 // TODO: impl receivechar()/recvchar()
229 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM
);
231 /* Check to see if a packet has been sent from the host */
232 if (Endpoint_IsOUTReceived())
234 /* Check to see if the packet contains data */
235 if (Endpoint_IsReadWriteAllowed())
237 /* Create a temporary buffer to hold the read in report from the host */
238 uint8_t ConsoleData
[CONSOLE_EPSIZE
];
240 /* Read Console Report Data */
241 Endpoint_Read_Stream_LE(&ConsoleData
, sizeof(ConsoleData
), NULL
);
243 /* Process Console Report Data */
244 //ProcessConsoleHIDReport(ConsoleData);
247 /* Finalize the stream transfer to send the last packet */
253 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM
);
254 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
255 Endpoint_SelectEndpoint(ep
);
260 while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0);
262 // flash senchar packet
263 if (Endpoint_IsINReady()) {
267 Endpoint_SelectEndpoint(ep
);
271 /*******************************************************************************
273 ******************************************************************************/
275 * Event Order of Plug in:
276 * 0) EVENT_USB_Device_Connect
277 * 1) EVENT_USB_Device_Suspend
278 * 2) EVENT_USB_Device_Reset
279 * 3) EVENT_USB_Device_Wake
281 /** \brief Event USB Device Connect
285 void EVENT_USB_Device_Connect(void) {
287 /* For battery powered device */
288 if (!USB_IsInitialized
) {
291 USB_Device_EnableSOFEvents();
295 /** \brief Event USB Device Connect
299 void EVENT_USB_Device_Disconnect(void) {
301 /* For battery powered device */
302 USB_IsInitialized
= false;
303 /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
304 if (USB_IsInitialized) {
305 USB_Disable(); // Disable all interrupts
306 USB_Controller_Enable();
307 USB_INT_Enable(USB_INT_VBUSTI);
312 /** \brief Event USB Device Connect
316 void EVENT_USB_Device_Reset(void) { print("[R]"); }
318 /** \brief Event USB Device Connect
322 void EVENT_USB_Device_Suspend() {
324 #ifdef SLEEP_LED_ENABLE
329 /** \brief Event USB Device Connect
333 void EVENT_USB_Device_WakeUp() {
335 suspend_wakeup_init();
337 #ifdef SLEEP_LED_ENABLE
339 // NOTE: converters may not accept this
340 led_set(host_keyboard_leds());
344 #ifdef CONSOLE_ENABLE
345 static bool console_flush
= false;
346 # define CONSOLE_FLUSH_SET(b) \
348 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { console_flush = b; } \
351 /** \brief Event USB Device Start Of Frame
356 void EVENT_USB_Device_StartOfFrame(void) {
357 static uint8_t count
;
358 if (++count
% 50) return;
361 if (!console_flush
) return;
363 console_flush
= false;
368 /** \brief Event handler for the USB_ConfigurationChanged event.
370 * This is fired when the host sets the current configuration of the USB device after enumeration.
372 * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
373 * it is safe to use single bank for all endpoints.
375 void EVENT_USB_Device_ConfigurationChanged(void) {
376 bool ConfigSuccess
= true;
378 /* Setup Keyboard HID Report Endpoints */
379 #ifndef KEYBOARD_SHARED_EP
380 ConfigSuccess
&= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
, KEYBOARD_EPSIZE
, ENDPOINT_BANK_SINGLE
);
383 #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
384 /* Setup Mouse HID Report Endpoint */
385 ConfigSuccess
&= ENDPOINT_CONFIG(MOUSE_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
, MOUSE_EPSIZE
, ENDPOINT_BANK_SINGLE
);
388 #ifdef SHARED_EP_ENABLE
389 /* Setup Shared HID Report Endpoint */
390 ConfigSuccess
&= ENDPOINT_CONFIG(SHARED_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
, SHARED_EPSIZE
, ENDPOINT_BANK_SINGLE
);
394 /* Setup Raw HID Report Endpoints */
395 ConfigSuccess
&= ENDPOINT_CONFIG(RAW_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
, RAW_EPSIZE
, ENDPOINT_BANK_SINGLE
);
396 ConfigSuccess
&= ENDPOINT_CONFIG(RAW_OUT_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_OUT
, RAW_EPSIZE
, ENDPOINT_BANK_SINGLE
);
399 #ifdef CONSOLE_ENABLE
400 /* Setup Console HID Report Endpoints */
401 ConfigSuccess
&= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
, CONSOLE_EPSIZE
, ENDPOINT_BANK_SINGLE
);
403 ConfigSuccess
&= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_OUT
,
404 CONSOLE_EPSIZE
, ENDPOINT_BANK_SINGLE
);
409 ConfigSuccess
&= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR
, EP_TYPE_BULK
, MIDI_STREAM_EPSIZE
, ENDPOINT_BANK_SINGLE
);
410 ConfigSuccess
&= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR
, EP_TYPE_BULK
, MIDI_STREAM_EPSIZE
, ENDPOINT_BANK_SINGLE
);
413 #ifdef VIRTSER_ENABLE
414 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR
, EP_TYPE_INTERRUPT
, CDC_NOTIFICATION_EPSIZE
, ENDPOINT_BANK_SINGLE
);
415 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR
, EP_TYPE_BULK
, CDC_EPSIZE
, ENDPOINT_BANK_SINGLE
);
416 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR
, EP_TYPE_BULK
, CDC_EPSIZE
, ENDPOINT_BANK_SINGLE
);
420 /* FIXME: Expose this table in the docs somehow
421 Appendix G: HID Request Support Requirements
423 The following table enumerates the requests that need to be supported by various types of HID class devices.
425 Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
426 ------------------------------------------------------------------------------------------
427 Boot Mouse Required Optional Optional Optional Required Required
428 Non-Boot Mouse Required Optional Optional Optional Optional Optional
429 Boot Keyboard Required Optional Required Required Required Required
430 Non-Boot Keybrd Required Optional Required Required Optional Optional
431 Other Device Required Optional Optional Optional Optional Optional
433 /** \brief 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) {
438 uint8_t *ReportData
= NULL
;
439 uint8_t ReportSize
= 0;
441 /* Handle HID Class specific requests */
442 switch (USB_ControlRequest
.bRequest
) {
443 case HID_REQ_GetReport
:
444 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
)) {
445 Endpoint_ClearSETUP();
448 switch (USB_ControlRequest
.wIndex
) {
449 case KEYBOARD_INTERFACE
:
451 ReportData
= (uint8_t *)&keyboard_report_sent
;
452 ReportSize
= sizeof(keyboard_report_sent
);
456 /* Write the report data to the control endpoint */
457 Endpoint_Write_Control_Stream_LE(ReportData
, ReportSize
);
462 case HID_REQ_SetReport
:
463 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
)) {
465 switch (USB_ControlRequest
.wIndex
) {
466 case KEYBOARD_INTERFACE
:
467 #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
468 case SHARED_INTERFACE
:
470 Endpoint_ClearSETUP();
472 while (!(Endpoint_IsOUTReceived())) {
473 if (USB_DeviceState
== DEVICE_STATE_Unattached
) return;
476 if (Endpoint_BytesInEndpoint() == 2) {
477 uint8_t report_id
= Endpoint_Read_8();
479 if (report_id
== REPORT_ID_KEYBOARD
|| report_id
== REPORT_ID_NKRO
) {
480 keyboard_led_stats
= Endpoint_Read_8();
483 keyboard_led_stats
= Endpoint_Read_8();
487 Endpoint_ClearStatusStage();
494 case HID_REQ_GetProtocol
:
495 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
)) {
496 if (USB_ControlRequest
.wIndex
== KEYBOARD_INTERFACE
) {
497 Endpoint_ClearSETUP();
498 while (!(Endpoint_IsINReady()))
500 Endpoint_Write_8(keyboard_protocol
);
502 Endpoint_ClearStatusStage();
507 case HID_REQ_SetProtocol
:
508 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
)) {
509 if (USB_ControlRequest
.wIndex
== KEYBOARD_INTERFACE
) {
510 Endpoint_ClearSETUP();
511 Endpoint_ClearStatusStage();
513 keyboard_protocol
= (USB_ControlRequest
.wValue
& 0xFF);
519 case HID_REQ_SetIdle
:
520 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
)) {
530 Endpoint_ClearSETUP();
531 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 /** \brief Keyboard LEDs
553 static uint8_t keyboard_leds(void) { return keyboard_led_stats
; }
555 /** \brief Send Keyboard
559 static void send_keyboard(report_keyboard_t
*report
) {
560 uint8_t timeout
= 255;
561 uint8_t where
= where_to_send();
563 #ifdef BLUETOOTH_ENABLE
564 if (where
== OUTPUT_BLUETOOTH
|| where
== OUTPUT_USB_AND_BT
) {
565 # ifdef MODULE_ADAFRUIT_BLE
566 adafruit_ble_send_keys(report
->mods
, report
->keys
, sizeof(report
->keys
));
568 bluefruit_serial_send(0xFD);
569 bluefruit_serial_send(0x09);
570 bluefruit_serial_send(0x01);
571 bluefruit_serial_send(report
->mods
);
572 bluefruit_serial_send(report
->reserved
);
573 for (uint8_t i
= 0; i
< KEYBOARD_REPORT_KEYS
; i
++) {
574 bluefruit_serial_send(report
->keys
[i
]);
577 bluefruit_serial_send(0xFD);
578 bluefruit_serial_send(report
->mods
);
579 bluefruit_serial_send(report
->reserved
);
580 for (uint8_t i
= 0; i
< KEYBOARD_REPORT_KEYS
; i
++) {
581 bluefruit_serial_send(report
->keys
[i
]);
587 if (where
!= OUTPUT_USB
&& where
!= OUTPUT_USB_AND_BT
) {
591 /* Select the Keyboard Report Endpoint */
592 uint8_t ep
= KEYBOARD_IN_EPNUM
;
593 uint8_t size
= KEYBOARD_REPORT_SIZE
;
595 if (keyboard_protocol
&& keymap_config
.nkro
) {
596 ep
= SHARED_IN_EPNUM
;
597 size
= sizeof(struct nkro_report
);
600 Endpoint_SelectEndpoint(ep
);
601 /* Check if write ready for a polling interval around 10ms */
602 while (timeout
-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
603 if (!Endpoint_IsReadWriteAllowed()) return;
605 /* If we're in Boot Protocol, don't send any report ID or other funky fields */
606 if (!keyboard_protocol
) {
607 Endpoint_Write_Stream_LE(&report
->mods
, 8, NULL
);
609 Endpoint_Write_Stream_LE(report
, size
, NULL
);
612 /* Finalize the stream transfer to send the last packet */
615 keyboard_report_sent
= *report
;
618 /** \brief Send Mouse
622 static void send_mouse(report_mouse_t
*report
) {
624 uint8_t timeout
= 255;
625 uint8_t where
= where_to_send();
627 # ifdef BLUETOOTH_ENABLE
628 if (where
== OUTPUT_BLUETOOTH
|| where
== OUTPUT_USB_AND_BT
) {
629 # ifdef MODULE_ADAFRUIT_BLE
630 // FIXME: mouse buttons
631 adafruit_ble_send_mouse_move(report
->x
, report
->y
, report
->v
, report
->h
, report
->buttons
);
633 bluefruit_serial_send(0xFD);
634 bluefruit_serial_send(0x00);
635 bluefruit_serial_send(0x03);
636 bluefruit_serial_send(report
->buttons
);
637 bluefruit_serial_send(report
->x
);
638 bluefruit_serial_send(report
->y
);
639 bluefruit_serial_send(report
->v
); // should try sending the wheel v here
640 bluefruit_serial_send(report
->h
); // should try sending the wheel h here
641 bluefruit_serial_send(0x00);
646 if (where
!= OUTPUT_USB
&& where
!= OUTPUT_USB_AND_BT
) {
650 /* Select the Mouse Report Endpoint */
651 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM
);
653 /* Check if write ready for a polling interval around 10ms */
654 while (timeout
-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
655 if (!Endpoint_IsReadWriteAllowed()) return;
657 /* Write Mouse Report Data */
658 Endpoint_Write_Stream_LE(report
, sizeof(report_mouse_t
), NULL
);
660 /* Finalize the stream transfer to send the last packet */
665 /** \brief Send System
669 static void send_system(uint16_t data
) {
670 #ifdef EXTRAKEY_ENABLE
671 uint8_t timeout
= 255;
673 if (USB_DeviceState
!= DEVICE_STATE_Configured
) return;
675 report_extra_t r
= {.report_id
= REPORT_ID_SYSTEM
, .usage
= data
- SYSTEM_POWER_DOWN
+ 1};
676 Endpoint_SelectEndpoint(SHARED_IN_EPNUM
);
678 /* Check if write ready for a polling interval around 10ms */
679 while (timeout
-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
680 if (!Endpoint_IsReadWriteAllowed()) return;
682 Endpoint_Write_Stream_LE(&r
, sizeof(report_extra_t
), NULL
);
687 /** \brief Send Consumer
691 static void send_consumer(uint16_t data
) {
692 #ifdef EXTRAKEY_ENABLE
693 uint8_t timeout
= 255;
694 uint8_t where
= where_to_send();
696 # ifdef BLUETOOTH_ENABLE
697 if (where
== OUTPUT_BLUETOOTH
|| where
== OUTPUT_USB_AND_BT
) {
698 # ifdef MODULE_ADAFRUIT_BLE
699 adafruit_ble_send_consumer_key(data
, 0);
701 static uint16_t last_data
= 0;
702 if (data
== last_data
) return;
704 uint16_t bitmap
= CONSUMER2RN42(data
);
705 bluefruit_serial_send(0xFD);
706 bluefruit_serial_send(0x03);
707 bluefruit_serial_send(0x03);
708 bluefruit_serial_send(bitmap
& 0xFF);
709 bluefruit_serial_send((bitmap
>> 8) & 0xFF);
711 static uint16_t last_data
= 0;
712 if (data
== last_data
) return;
714 uint16_t bitmap
= CONSUMER2BLUEFRUIT(data
);
715 bluefruit_serial_send(0xFD);
716 bluefruit_serial_send(0x00);
717 bluefruit_serial_send(0x02);
718 bluefruit_serial_send((bitmap
>> 8) & 0xFF);
719 bluefruit_serial_send(bitmap
& 0xFF);
720 bluefruit_serial_send(0x00);
721 bluefruit_serial_send(0x00);
722 bluefruit_serial_send(0x00);
723 bluefruit_serial_send(0x00);
728 if (where
!= OUTPUT_USB
&& where
!= OUTPUT_USB_AND_BT
) {
732 report_extra_t r
= {.report_id
= REPORT_ID_CONSUMER
, .usage
= data
};
733 Endpoint_SelectEndpoint(SHARED_IN_EPNUM
);
735 /* Check if write ready for a polling interval around 10ms */
736 while (timeout
-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
737 if (!Endpoint_IsReadWriteAllowed()) return;
739 Endpoint_Write_Stream_LE(&r
, sizeof(report_extra_t
), NULL
);
744 /*******************************************************************************
746 ******************************************************************************/
747 #ifdef CONSOLE_ENABLE
748 # define SEND_TIMEOUT 5
753 int8_t sendchar(uint8_t c
) {
754 // Not wait once timeouted.
755 // Because sendchar() is called so many times, waiting each call causes big lag.
756 static bool timeouted
= false;
758 // prevents Console_Task() from running during sendchar() runs.
759 // or char will be lost. These two function is mutually exclusive.
760 CONSOLE_FLUSH_SET(false);
762 if (USB_DeviceState
!= DEVICE_STATE_Configured
) return -1;
764 uint8_t ep
= Endpoint_GetCurrentEndpoint();
765 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM
);
766 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
770 if (timeouted
&& !Endpoint_IsReadWriteAllowed()) {
776 uint8_t timeout
= SEND_TIMEOUT
;
777 while (!Endpoint_IsReadWriteAllowed()) {
778 if (USB_DeviceState
!= DEVICE_STATE_Configured
) {
781 if (Endpoint_IsStalled()) {
793 // send when bank is full
794 if (!Endpoint_IsReadWriteAllowed()) {
795 while (!(Endpoint_IsINReady()))
799 CONSOLE_FLUSH_SET(true);
802 Endpoint_SelectEndpoint(ep
);
805 Endpoint_SelectEndpoint(ep
);
809 int8_t sendchar(uint8_t c
) { return 0; }
812 /*******************************************************************************
814 ******************************************************************************/
817 USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface
= {
820 .StreamingInterfaceNumber
= AS_INTERFACE
,
823 .Address
= MIDI_STREAM_IN_EPADDR
,
824 .Size
= MIDI_STREAM_EPSIZE
,
829 .Address
= MIDI_STREAM_OUT_EPADDR
,
830 .Size
= MIDI_STREAM_EPSIZE
,
836 void send_midi_packet(MIDI_EventPacket_t
*event
) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface
, event
); }
838 bool recv_midi_packet(MIDI_EventPacket_t
*const event
) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface
, event
); }
842 /*******************************************************************************
844 ******************************************************************************/
846 #ifdef VIRTSER_ENABLE
847 /** \brief Virtual Serial Init
851 void virtser_init(void) {
852 cdc_device
.State
.ControlLineStates
.DeviceToHost
= CDC_CONTROL_LINE_IN_DSR
;
853 CDC_Device_SendControlLineStateChange(&cdc_device
);
856 /** \brief Virtual Serial Receive
860 void virtser_recv(uint8_t c
) __attribute__((weak
));
861 void virtser_recv(uint8_t c
) {
865 /** \brief Virtual Serial Task
869 void virtser_task(void) {
870 uint16_t count
= CDC_Device_BytesReceived(&cdc_device
);
873 ch
= CDC_Device_ReceiveByte(&cdc_device
);
877 /** \brief Virtual Serial Send
881 void virtser_send(const uint8_t byte
) {
882 uint8_t timeout
= 255;
883 uint8_t ep
= Endpoint_GetCurrentEndpoint();
885 if (cdc_device
.State
.ControlLineStates
.HostToDevice
& CDC_CONTROL_LINE_OUT_DTR
) {
887 Endpoint_SelectEndpoint(cdc_device
.Config
.DataINEndpoint
.Address
);
889 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
890 Endpoint_SelectEndpoint(ep
);
894 while (timeout
-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
896 Endpoint_Write_8(byte
);
897 CDC_Device_Flush(&cdc_device
);
899 if (Endpoint_IsINReady()) {
903 Endpoint_SelectEndpoint(ep
);
908 /*******************************************************************************
910 ******************************************************************************/
915 static void setup_mcu(void) {
916 /* Disable watchdog if enabled by bootloader/fuses */
920 /* Disable clock division */
921 clock_prescale_set(clock_div_1
);
928 static void setup_usb(void) {
929 // Leonardo needs. Without this USB device is not recognized.
935 USB_Device_EnableSOFEvents();
936 print_set_sendchar(sendchar
);
943 int main(void) __attribute__((weak
));
954 #if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
958 /* wait for USB startup & debug output */
961 while (USB_DeviceState
!= DEVICE_STATE_Configured
) {
962 # if defined(INTERRUPT_CONTROL_ENDPOINT)
968 print("USB configured.\n");
974 host_set_driver(&lufa_driver
);
975 #ifdef SLEEP_LED_ENABLE
979 #ifdef VIRTSER_ENABLE
983 print("Keyboard start.\n");
985 #if !defined(NO_USB_STARTUP_CHECK)
986 while (USB_DeviceState
== DEVICE_STATE_Suspended
) {
988 suspend_power_down();
989 if (USB_Device_RemoteWakeupEnabled
&& suspend_wakeup_condition()) {
990 USB_Device_SendRemoteWakeup();
998 MIDI_Device_USBTask(&USB_MIDI_Interface
);
1001 #ifdef MODULE_ADAFRUIT_BLE
1002 adafruit_ble_task();
1005 #ifdef VIRTSER_ENABLE
1007 CDC_Device_USBTask(&cdc_device
);
1014 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
1020 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue
, const uint16_t wIndex
, const void **const DescriptorAddress
) { return get_usb_descriptor(wValue
, wIndex
, DescriptorAddress
); }