aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/lufa/lufa.c
diff options
context:
space:
mode:
authorskullY <skullydazed@gmail.com>2019-08-30 11:19:03 -0700
committerskullydazed <skullydazed@users.noreply.github.com>2019-08-30 15:01:52 -0700
commitb624f32f944acdc59dcb130674c09090c5c404cb (patch)
treebc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /tmk_core/protocol/lufa/lufa.c
parent61af76a10d00aba185b8338604171de490a13e3b (diff)
downloadqmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz
qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip
clang-format changes
Diffstat (limited to 'tmk_core/protocol/lufa/lufa.c')
-rw-r--r--tmk_core/protocol/lufa/lufa.c692
1 files changed, 302 insertions, 390 deletions
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index f2ecf2465..12a6924fd 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -45,7 +45,7 @@
45#include "sendchar.h" 45#include "sendchar.h"
46#include "debug.h" 46#include "debug.h"
47#ifdef SLEEP_LED_ENABLE 47#ifdef SLEEP_LED_ENABLE
48#include "sleep_led.h" 48# include "sleep_led.h"
49#endif 49#endif
50#include "suspend.h" 50#include "suspend.h"
51 51
@@ -57,86 +57,80 @@
57#include "rgblight_reconfig.h" 57#include "rgblight_reconfig.h"
58 58
59#ifdef NKRO_ENABLE 59#ifdef NKRO_ENABLE
60 #include "keycode_config.h" 60# include "keycode_config.h"
61 61
62 extern keymap_config_t keymap_config; 62extern keymap_config_t keymap_config;
63#endif 63#endif
64 64
65
66#ifdef AUDIO_ENABLE 65#ifdef AUDIO_ENABLE
67 #include <audio.h> 66# include <audio.h>
68#endif 67#endif
69 68
70#ifdef BLUETOOTH_ENABLE 69#ifdef BLUETOOTH_ENABLE
71 #ifdef MODULE_ADAFRUIT_BLE 70# ifdef MODULE_ADAFRUIT_BLE
72 #include "adafruit_ble.h" 71# include "adafruit_ble.h"
73 #else 72# else
74 #include "bluetooth.h" 73# include "bluetooth.h"
75 #endif 74# endif
76#endif 75#endif
77 76
78#ifdef VIRTSER_ENABLE 77#ifdef VIRTSER_ENABLE
79 #include "virtser.h" 78# include "virtser.h"
80#endif 79#endif
81 80
82#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE) 81#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
83 #include "rgblight.h" 82# include "rgblight.h"
84#endif 83#endif
85 84
86#ifdef MIDI_ENABLE 85#ifdef MIDI_ENABLE
87 #include "qmk_midi.h" 86# include "qmk_midi.h"
88#endif 87#endif
89 88
90#ifdef RAW_ENABLE 89#ifdef RAW_ENABLE
91 #include "raw_hid.h" 90# include "raw_hid.h"
92#endif 91#endif
93 92
94uint8_t keyboard_idle = 0; 93uint8_t keyboard_idle = 0;
95/* 0: Boot Protocol, 1: Report Protocol(default) */ 94/* 0: Boot Protocol, 1: Report Protocol(default) */
96uint8_t keyboard_protocol = 1; 95uint8_t keyboard_protocol = 1;
97static uint8_t keyboard_led_stats = 0; 96static uint8_t keyboard_led_stats = 0;
98 97
99static report_keyboard_t keyboard_report_sent; 98static report_keyboard_t keyboard_report_sent;
100 99
101/* Host driver */ 100/* Host driver */
102static uint8_t keyboard_leds(void); 101static uint8_t keyboard_leds(void);
103static void send_keyboard(report_keyboard_t *report); 102static void send_keyboard(report_keyboard_t *report);
104static void send_mouse(report_mouse_t *report); 103static void send_mouse(report_mouse_t *report);
105static void send_system(uint16_t data); 104static void send_system(uint16_t data);
106static void send_consumer(uint16_t data); 105static void send_consumer(uint16_t data);
107host_driver_t lufa_driver = { 106host_driver_t lufa_driver = {
108 keyboard_leds, 107 keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer,
109 send_keyboard,
110 send_mouse,
111 send_system,
112 send_consumer,
113}; 108};
114 109
115#ifdef VIRTSER_ENABLE 110#ifdef VIRTSER_ENABLE
116USB_ClassInfo_CDC_Device_t cdc_device = 111USB_ClassInfo_CDC_Device_t cdc_device = {
117{ 112 .Config =
118 .Config = 113 {
119 { 114 .ControlInterfaceNumber = CCI_INTERFACE,
120 .ControlInterfaceNumber = CCI_INTERFACE, 115 .DataINEndpoint =
121 .DataINEndpoint = 116 {
122 { 117 .Address = CDC_IN_EPADDR,
123 .Address = CDC_IN_EPADDR, 118 .Size = CDC_EPSIZE,
124 .Size = CDC_EPSIZE, 119 .Banks = 1,
125 .Banks = 1, 120 },
126 }, 121 .DataOUTEndpoint =
127 .DataOUTEndpoint = 122 {
128 { 123 .Address = CDC_OUT_EPADDR,
129 .Address = CDC_OUT_EPADDR, 124 .Size = CDC_EPSIZE,
130 .Size = CDC_EPSIZE, 125 .Banks = 1,
131 .Banks = 1, 126 },
132 }, 127 .NotificationEndpoint =
133 .NotificationEndpoint = 128 {
134 { 129 .Address = CDC_NOTIFICATION_EPADDR,
135 .Address = CDC_NOTIFICATION_EPADDR, 130 .Size = CDC_NOTIFICATION_EPSIZE,
136 .Size = CDC_NOTIFICATION_EPSIZE, 131 .Banks = 1,
137 .Banks = 1, 132 },
138 }, 133 },
139 },
140}; 134};
141#endif 135#endif
142 136
@@ -146,84 +140,73 @@ USB_ClassInfo_CDC_Device_t cdc_device =
146 * 140 *
147 * FIXME: Needs doc 141 * FIXME: Needs doc
148 */ 142 */
149void raw_hid_send( uint8_t *data, uint8_t length ) 143void raw_hid_send(uint8_t *data, uint8_t length) {
150{ 144 // TODO: implement variable size packet
151 // TODO: implement variable size packet 145 if (length != RAW_EPSIZE) {
152 if ( length != RAW_EPSIZE ) 146 return;
153 { 147 }
154 return; 148
155 } 149 if (USB_DeviceState != DEVICE_STATE_Configured) {
156 150 return;
157 if (USB_DeviceState != DEVICE_STATE_Configured) 151 }
158 { 152
159 return; 153 // TODO: decide if we allow calls to raw_hid_send() in the middle
160 } 154 // of other endpoint usage.
161 155 uint8_t ep = Endpoint_GetCurrentEndpoint();
162 // TODO: decide if we allow calls to raw_hid_send() in the middle 156
163 // of other endpoint usage. 157 Endpoint_SelectEndpoint(RAW_IN_EPNUM);
164 uint8_t ep = Endpoint_GetCurrentEndpoint(); 158
165 159 // Check to see if the host is ready to accept another packet
166 Endpoint_SelectEndpoint(RAW_IN_EPNUM); 160 if (Endpoint_IsINReady()) {
167 161 // Write data
168 // Check to see if the host is ready to accept another packet 162 Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
169 if (Endpoint_IsINReady()) 163 // Finalize the stream transfer to send the last packet
170 { 164 Endpoint_ClearIN();
171 // Write data 165 }
172 Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL); 166
173 // Finalize the stream transfer to send the last packet 167 Endpoint_SelectEndpoint(ep);
174 Endpoint_ClearIN();
175 }
176
177 Endpoint_SelectEndpoint(ep);
178} 168}
179 169
180/** \brief Raw HID Receive 170/** \brief Raw HID Receive
181 * 171 *
182 * FIXME: Needs doc 172 * FIXME: Needs doc
183 */ 173 */
184__attribute__ ((weak)) 174__attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
185void raw_hid_receive( uint8_t *data, uint8_t length ) 175 // Users should #include "raw_hid.h" in their own code
186{ 176 // and implement this function there. Leave this as weak linkage
187 // Users should #include "raw_hid.h" in their own code 177 // so users can opt to not handle data coming in.
188 // and implement this function there. Leave this as weak linkage
189 // so users can opt to not handle data coming in.
190} 178}
191 179
192/** \brief Raw HID Task 180/** \brief Raw HID Task
193 * 181 *
194 * FIXME: Needs doc 182 * FIXME: Needs doc
195 */ 183 */
196static void raw_hid_task(void) 184static void raw_hid_task(void) {
197{ 185 // Create a temporary buffer to hold the read in data from the host
198 // Create a temporary buffer to hold the read in data from the host 186 uint8_t data[RAW_EPSIZE];
199 uint8_t data[RAW_EPSIZE]; 187 bool data_read = false;
200 bool data_read = false; 188
201 189 // Device must be connected and configured for the task to run
202 // Device must be connected and configured for the task to run 190 if (USB_DeviceState != DEVICE_STATE_Configured) return;
203 if (USB_DeviceState != DEVICE_STATE_Configured) 191
204 return; 192 Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
205 193
206 Endpoint_SelectEndpoint(RAW_OUT_EPNUM); 194 // Check to see if a packet has been sent from the host
207 195 if (Endpoint_IsOUTReceived()) {
208 // Check to see if a packet has been sent from the host 196 // Check to see if the packet contains data
209 if (Endpoint_IsOUTReceived()) 197 if (Endpoint_IsReadWriteAllowed()) {
210 { 198 /* Read data */
211 // Check to see if the packet contains data 199 Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
212 if (Endpoint_IsReadWriteAllowed()) 200 data_read = true;
213 { 201 }
214 /* Read data */ 202
215 Endpoint_Read_Stream_LE(data, sizeof(data), NULL); 203 // Finalize the stream transfer to receive the last packet
216 data_read = true; 204 Endpoint_ClearOUT();
217 } 205
218 206 if (data_read) {
219 // Finalize the stream transfer to receive the last packet 207 raw_hid_receive(data, sizeof(data));
220 Endpoint_ClearOUT(); 208 }
221 209 }
222 if ( data_read )
223 {
224 raw_hid_receive( data, sizeof(data) );
225 }
226 }
227} 210}
228#endif 211#endif
229 212
@@ -235,15 +218,13 @@ static void raw_hid_task(void)
235 * 218 *
236 * FIXME: Needs doc 219 * FIXME: Needs doc
237 */ 220 */
238static void Console_Task(void) 221static void Console_Task(void) {
239{
240 /* Device must be connected and configured for the task to run */ 222 /* Device must be connected and configured for the task to run */
241 if (USB_DeviceState != DEVICE_STATE_Configured) 223 if (USB_DeviceState != DEVICE_STATE_Configured) return;
242 return;
243 224
244 uint8_t ep = Endpoint_GetCurrentEndpoint(); 225 uint8_t ep = Endpoint_GetCurrentEndpoint();
245 226
246#if 0 227# if 0
247 // TODO: impl receivechar()/recvchar() 228 // TODO: impl receivechar()/recvchar()
248 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM); 229 Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
249 230
@@ -266,7 +247,7 @@ static void Console_Task(void)
266 /* Finalize the stream transfer to send the last packet */ 247 /* Finalize the stream transfer to send the last packet */
267 Endpoint_ClearOUT(); 248 Endpoint_ClearOUT();
268 } 249 }
269#endif 250# endif
270 251
271 /* IN packet */ 252 /* IN packet */
272 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); 253 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
@@ -276,8 +257,7 @@ static void Console_Task(void)
276 } 257 }
277 258
278 // fill empty bank 259 // fill empty bank
279 while (Endpoint_IsReadWriteAllowed()) 260 while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0);
280 Endpoint_Write_8(0);
281 261
282 // flash senchar packet 262 // flash senchar packet
283 if (Endpoint_IsINReady()) { 263 if (Endpoint_IsINReady()) {
@@ -288,7 +268,6 @@ static void Console_Task(void)
288} 268}
289#endif 269#endif
290 270
291
292/******************************************************************************* 271/*******************************************************************************
293 * USB Events 272 * USB Events
294 ******************************************************************************/ 273 ******************************************************************************/
@@ -298,13 +277,12 @@ static void Console_Task(void)
298 * 1) EVENT_USB_Device_Suspend 277 * 1) EVENT_USB_Device_Suspend
299 * 2) EVENT_USB_Device_Reset 278 * 2) EVENT_USB_Device_Reset
300 * 3) EVENT_USB_Device_Wake 279 * 3) EVENT_USB_Device_Wake
301*/ 280 */
302/** \brief Event USB Device Connect 281/** \brief Event USB Device Connect
303 * 282 *
304 * FIXME: Needs doc 283 * FIXME: Needs doc
305 */ 284 */
306void EVENT_USB_Device_Connect(void) 285void EVENT_USB_Device_Connect(void) {
307{
308 print("[C]"); 286 print("[C]");
309 /* For battery powered device */ 287 /* For battery powered device */
310 if (!USB_IsInitialized) { 288 if (!USB_IsInitialized) {
@@ -318,35 +296,30 @@ void EVENT_USB_Device_Connect(void)
318 * 296 *
319 * FIXME: Needs doc 297 * FIXME: Needs doc
320 */ 298 */
321void EVENT_USB_Device_Disconnect(void) 299void EVENT_USB_Device_Disconnect(void) {
322{
323 print("[D]"); 300 print("[D]");
324 /* For battery powered device */ 301 /* For battery powered device */
325 USB_IsInitialized = false; 302 USB_IsInitialized = false;
326/* TODO: This doesn't work. After several plug in/outs can not be enumerated. 303 /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
327 if (USB_IsInitialized) { 304 if (USB_IsInitialized) {
328 USB_Disable(); // Disable all interrupts 305 USB_Disable(); // Disable all interrupts
329 USB_Controller_Enable(); 306 USB_Controller_Enable();
330 USB_INT_Enable(USB_INT_VBUSTI); 307 USB_INT_Enable(USB_INT_VBUSTI);
331 } 308 }
332*/ 309 */
333} 310}
334 311
335/** \brief Event USB Device Connect 312/** \brief Event USB Device Connect
336 * 313 *
337 * FIXME: Needs doc 314 * FIXME: Needs doc
338 */ 315 */
339void EVENT_USB_Device_Reset(void) 316void EVENT_USB_Device_Reset(void) { print("[R]"); }
340{
341 print("[R]");
342}
343 317
344/** \brief Event USB Device Connect 318/** \brief Event USB Device Connect
345 * 319 *
346 * FIXME: Needs doc 320 * FIXME: Needs doc
347 */ 321 */
348void EVENT_USB_Device_Suspend() 322void EVENT_USB_Device_Suspend() {
349{
350 print("[S]"); 323 print("[S]");
351#ifdef SLEEP_LED_ENABLE 324#ifdef SLEEP_LED_ENABLE
352 sleep_led_enable(); 325 sleep_led_enable();
@@ -357,8 +330,7 @@ void EVENT_USB_Device_Suspend()
357 * 330 *
358 * FIXME: Needs doc 331 * FIXME: Needs doc
359 */ 332 */
360void EVENT_USB_Device_WakeUp() 333void EVENT_USB_Device_WakeUp() {
361{
362 print("[W]"); 334 print("[W]");
363 suspend_wakeup_init(); 335 suspend_wakeup_init();
364 336
@@ -369,23 +341,19 @@ void EVENT_USB_Device_WakeUp()
369#endif 341#endif
370} 342}
371 343
372
373
374#ifdef CONSOLE_ENABLE 344#ifdef CONSOLE_ENABLE
375static bool console_flush = false; 345static bool console_flush = false;
376#define CONSOLE_FLUSH_SET(b) do { \ 346# define CONSOLE_FLUSH_SET(b) \
377 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\ 347 do { \
378 console_flush = b; \ 348 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { console_flush = b; } \
379 } \ 349 } while (0)
380} while (0)
381 350
382/** \brief Event USB Device Start Of Frame 351/** \brief Event USB Device Start Of Frame
383 * 352 *
384 * FIXME: Needs doc 353 * FIXME: Needs doc
385 * called every 1ms 354 * called every 1ms
386 */ 355 */
387void EVENT_USB_Device_StartOfFrame(void) 356void EVENT_USB_Device_StartOfFrame(void) {
388{
389 static uint8_t count; 357 static uint8_t count;
390 if (++count % 50) return; 358 if (++count % 50) return;
391 count = 0; 359 count = 0;
@@ -404,44 +372,37 @@ void EVENT_USB_Device_StartOfFrame(void)
404 * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4, 372 * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
405 * it is safe to use single bank for all endpoints. 373 * it is safe to use single bank for all endpoints.
406 */ 374 */
407void EVENT_USB_Device_ConfigurationChanged(void) 375void EVENT_USB_Device_ConfigurationChanged(void) {
408{
409 bool ConfigSuccess = true; 376 bool ConfigSuccess = true;
410 377
411 /* Setup Keyboard HID Report Endpoints */ 378 /* Setup Keyboard HID Report Endpoints */
412#ifndef KEYBOARD_SHARED_EP 379#ifndef KEYBOARD_SHARED_EP
413 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 380 ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
414 KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE);
415#endif 381#endif
416 382
417#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) 383#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
418 /* Setup Mouse HID Report Endpoint */ 384 /* Setup Mouse HID Report Endpoint */
419 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 385 ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
420 MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE);
421#endif 386#endif
422 387
423#ifdef SHARED_EP_ENABLE 388#ifdef SHARED_EP_ENABLE
424 /* Setup Shared HID Report Endpoint */ 389 /* Setup Shared HID Report Endpoint */
425 ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 390 ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, SHARED_EPSIZE, ENDPOINT_BANK_SINGLE);
426 SHARED_EPSIZE, ENDPOINT_BANK_SINGLE);
427#endif 391#endif
428 392
429#ifdef RAW_ENABLE 393#ifdef RAW_ENABLE
430 /* Setup Raw HID Report Endpoints */ 394 /* Setup Raw HID Report Endpoints */
431 ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 395 ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
432 RAW_EPSIZE, ENDPOINT_BANK_SINGLE); 396 ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
433 ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
434 RAW_EPSIZE, ENDPOINT_BANK_SINGLE);
435#endif 397#endif
436 398
437#ifdef CONSOLE_ENABLE 399#ifdef CONSOLE_ENABLE
438 /* Setup Console HID Report Endpoints */ 400 /* Setup Console HID Report Endpoints */
439 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, 401 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
440 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); 402# if 0
441#if 0
442 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, 403 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
443 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); 404 CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE);
444#endif 405# endif
445#endif 406#endif
446 407
447#ifdef MIDI_ENABLE 408#ifdef MIDI_ENABLE
@@ -473,26 +434,23 @@ Other Device Required Optional Optional Optional Optional Opti
473 * 434 *
474 * This is fired before passing along unhandled control requests to the library for processing internally. 435 * This is fired before passing along unhandled control requests to the library for processing internally.
475 */ 436 */
476void EVENT_USB_Device_ControlRequest(void) 437void EVENT_USB_Device_ControlRequest(void) {
477{ 438 uint8_t *ReportData = NULL;
478 uint8_t* ReportData = NULL;
479 uint8_t ReportSize = 0; 439 uint8_t ReportSize = 0;
480 440
481 /* Handle HID Class specific requests */ 441 /* Handle HID Class specific requests */
482 switch (USB_ControlRequest.bRequest) 442 switch (USB_ControlRequest.bRequest) {
483 {
484 case HID_REQ_GetReport: 443 case HID_REQ_GetReport:
485 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) 444 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
486 {
487 Endpoint_ClearSETUP(); 445 Endpoint_ClearSETUP();
488 446
489 // Interface 447 // Interface
490 switch (USB_ControlRequest.wIndex) { 448 switch (USB_ControlRequest.wIndex) {
491 case KEYBOARD_INTERFACE: 449 case KEYBOARD_INTERFACE:
492 // TODO: test/check 450 // TODO: test/check
493 ReportData = (uint8_t*)&keyboard_report_sent; 451 ReportData = (uint8_t *)&keyboard_report_sent;
494 ReportSize = sizeof(keyboard_report_sent); 452 ReportSize = sizeof(keyboard_report_sent);
495 break; 453 break;
496 } 454 }
497 455
498 /* Write the report data to the control endpoint */ 456 /* Write the report data to the control endpoint */
@@ -502,47 +460,43 @@ void EVENT_USB_Device_ControlRequest(void)
502 460
503 break; 461 break;
504 case HID_REQ_SetReport: 462 case HID_REQ_SetReport:
505 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) 463 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) {
506 {
507
508 // Interface 464 // Interface
509 switch (USB_ControlRequest.wIndex) { 465 switch (USB_ControlRequest.wIndex) {
510 case KEYBOARD_INTERFACE: 466 case KEYBOARD_INTERFACE:
511#if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP) 467#if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
512 case SHARED_INTERFACE: 468 case SHARED_INTERFACE:
513#endif 469#endif
514 Endpoint_ClearSETUP(); 470 Endpoint_ClearSETUP();
515 471
516 while (!(Endpoint_IsOUTReceived())) { 472 while (!(Endpoint_IsOUTReceived())) {
517 if (USB_DeviceState == DEVICE_STATE_Unattached) 473 if (USB_DeviceState == DEVICE_STATE_Unattached) return;
518 return; 474 }
519 }
520 475
521 if (Endpoint_BytesInEndpoint() == 2) { 476 if (Endpoint_BytesInEndpoint() == 2) {
522 uint8_t report_id = Endpoint_Read_8(); 477 uint8_t report_id = Endpoint_Read_8();
523 478
524 if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) { 479 if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) {
525 keyboard_led_stats = Endpoint_Read_8(); 480 keyboard_led_stats = Endpoint_Read_8();
526 } 481 }
527 } else { 482 } else {
528 keyboard_led_stats = Endpoint_Read_8(); 483 keyboard_led_stats = Endpoint_Read_8();
529 } 484 }
530 485
531 Endpoint_ClearOUT(); 486 Endpoint_ClearOUT();
532 Endpoint_ClearStatusStage(); 487 Endpoint_ClearStatusStage();
533 break; 488 break;
534 } 489 }
535
536 } 490 }
537 491
538 break; 492 break;
539 493
540 case HID_REQ_GetProtocol: 494 case HID_REQ_GetProtocol:
541 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) 495 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
542 {
543 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) { 496 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
544 Endpoint_ClearSETUP(); 497 Endpoint_ClearSETUP();
545 while (!(Endpoint_IsINReady())); 498 while (!(Endpoint_IsINReady()))
499 ;
546 Endpoint_Write_8(keyboard_protocol); 500 Endpoint_Write_8(keyboard_protocol);
547 Endpoint_ClearIN(); 501 Endpoint_ClearIN();
548 Endpoint_ClearStatusStage(); 502 Endpoint_ClearStatusStage();
@@ -551,8 +505,7 @@ void EVENT_USB_Device_ControlRequest(void)
551 505
552 break; 506 break;
553 case HID_REQ_SetProtocol: 507 case HID_REQ_SetProtocol:
554 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) 508 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) {
555 {
556 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) { 509 if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
557 Endpoint_ClearSETUP(); 510 Endpoint_ClearSETUP();
558 Endpoint_ClearStatusStage(); 511 Endpoint_ClearStatusStage();
@@ -564,8 +517,7 @@ void EVENT_USB_Device_ControlRequest(void)
564 517
565 break; 518 break;
566 case HID_REQ_SetIdle: 519 case HID_REQ_SetIdle:
567 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) 520 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) {
568 {
569 Endpoint_ClearSETUP(); 521 Endpoint_ClearSETUP();
570 Endpoint_ClearStatusStage(); 522 Endpoint_ClearStatusStage();
571 523
@@ -574,10 +526,10 @@ void EVENT_USB_Device_ControlRequest(void)
574 526
575 break; 527 break;
576 case HID_REQ_GetIdle: 528 case HID_REQ_GetIdle:
577 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) 529 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
578 {
579 Endpoint_ClearSETUP(); 530 Endpoint_ClearSETUP();
580 while (!(Endpoint_IsINReady())); 531 while (!(Endpoint_IsINReady()))
532 ;
581 Endpoint_Write_8(keyboard_idle); 533 Endpoint_Write_8(keyboard_idle);
582 Endpoint_ClearIN(); 534 Endpoint_ClearIN();
583 Endpoint_ClearStatusStage(); 535 Endpoint_ClearStatusStage();
@@ -598,54 +550,50 @@ void EVENT_USB_Device_ControlRequest(void)
598 * 550 *
599 * FIXME: Needs doc 551 * FIXME: Needs doc
600 */ 552 */
601static uint8_t keyboard_leds(void) 553static uint8_t keyboard_leds(void) { return keyboard_led_stats; }
602{
603 return keyboard_led_stats;
604}
605 554
606/** \brief Send Keyboard 555/** \brief Send Keyboard
607 * 556 *
608 * FIXME: Needs doc 557 * FIXME: Needs doc
609 */ 558 */
610static void send_keyboard(report_keyboard_t *report) 559static void send_keyboard(report_keyboard_t *report) {
611{
612 uint8_t timeout = 255; 560 uint8_t timeout = 255;
613 uint8_t where = where_to_send(); 561 uint8_t where = where_to_send();
614 562
615#ifdef BLUETOOTH_ENABLE 563#ifdef BLUETOOTH_ENABLE
616 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { 564 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
617 #ifdef MODULE_ADAFRUIT_BLE 565# ifdef MODULE_ADAFRUIT_BLE
618 adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); 566 adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
619 #elif MODULE_RN42 567# elif MODULE_RN42
620 bluefruit_serial_send(0xFD); 568 bluefruit_serial_send(0xFD);
621 bluefruit_serial_send(0x09); 569 bluefruit_serial_send(0x09);
622 bluefruit_serial_send(0x01); 570 bluefruit_serial_send(0x01);
623 bluefruit_serial_send(report->mods); 571 bluefruit_serial_send(report->mods);
624 bluefruit_serial_send(report->reserved); 572 bluefruit_serial_send(report->reserved);
625 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { 573 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
626 bluefruit_serial_send(report->keys[i]); 574 bluefruit_serial_send(report->keys[i]);
627 } 575 }
628 #else 576# else
629 bluefruit_serial_send(0xFD); 577 bluefruit_serial_send(0xFD);
630 bluefruit_serial_send(report->mods); 578 bluefruit_serial_send(report->mods);
631 bluefruit_serial_send(report->reserved); 579 bluefruit_serial_send(report->reserved);
632 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { 580 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
633 bluefruit_serial_send(report->keys[i]); 581 bluefruit_serial_send(report->keys[i]);
634 } 582 }
635 #endif 583# endif
636 } 584 }
637#endif 585#endif
638 586
639 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { 587 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
640 return; 588 return;
641 } 589 }
642 590
643 /* Select the Keyboard Report Endpoint */ 591 /* Select the Keyboard Report Endpoint */
644 uint8_t ep = KEYBOARD_IN_EPNUM; 592 uint8_t ep = KEYBOARD_IN_EPNUM;
645 uint8_t size = KEYBOARD_REPORT_SIZE; 593 uint8_t size = KEYBOARD_REPORT_SIZE;
646#ifdef NKRO_ENABLE 594#ifdef NKRO_ENABLE
647 if (keyboard_protocol && keymap_config.nkro) { 595 if (keyboard_protocol && keymap_config.nkro) {
648 ep = SHARED_IN_EPNUM; 596 ep = SHARED_IN_EPNUM;
649 size = sizeof(struct nkro_report); 597 size = sizeof(struct nkro_report);
650 } 598 }
651#endif 599#endif
@@ -666,38 +614,37 @@ static void send_keyboard(report_keyboard_t *report)
666 614
667 keyboard_report_sent = *report; 615 keyboard_report_sent = *report;
668} 616}
669 617
670/** \brief Send Mouse 618/** \brief Send Mouse
671 * 619 *
672 * FIXME: Needs doc 620 * FIXME: Needs doc
673 */ 621 */
674static void send_mouse(report_mouse_t *report) 622static void send_mouse(report_mouse_t *report) {
675{
676#ifdef MOUSE_ENABLE 623#ifdef MOUSE_ENABLE
677 uint8_t timeout = 255; 624 uint8_t timeout = 255;
678 uint8_t where = where_to_send(); 625 uint8_t where = where_to_send();
679 626
680#ifdef BLUETOOTH_ENABLE 627# ifdef BLUETOOTH_ENABLE
681 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { 628 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
682 #ifdef MODULE_ADAFRUIT_BLE 629# ifdef MODULE_ADAFRUIT_BLE
683 // FIXME: mouse buttons 630 // FIXME: mouse buttons
684 adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons); 631 adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
685 #else 632# else
686 bluefruit_serial_send(0xFD); 633 bluefruit_serial_send(0xFD);
687 bluefruit_serial_send(0x00); 634 bluefruit_serial_send(0x00);
688 bluefruit_serial_send(0x03); 635 bluefruit_serial_send(0x03);
689 bluefruit_serial_send(report->buttons); 636 bluefruit_serial_send(report->buttons);
690 bluefruit_serial_send(report->x); 637 bluefruit_serial_send(report->x);
691 bluefruit_serial_send(report->y); 638 bluefruit_serial_send(report->y);
692 bluefruit_serial_send(report->v); // should try sending the wheel v here 639 bluefruit_serial_send(report->v); // should try sending the wheel v here
693 bluefruit_serial_send(report->h); // should try sending the wheel h here 640 bluefruit_serial_send(report->h); // should try sending the wheel h here
694 bluefruit_serial_send(0x00); 641 bluefruit_serial_send(0x00);
695 #endif 642# endif
696 } 643 }
697#endif 644# endif
698 645
699 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { 646 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
700 return; 647 return;
701 } 648 }
702 649
703 /* Select the Mouse Report Endpoint */ 650 /* Select the Mouse Report Endpoint */
@@ -719,18 +666,13 @@ static void send_mouse(report_mouse_t *report)
719 * 666 *
720 * FIXME: Needs doc 667 * FIXME: Needs doc
721 */ 668 */
722static void send_system(uint16_t data) 669static void send_system(uint16_t data) {
723{
724#ifdef EXTRAKEY_ENABLE 670#ifdef EXTRAKEY_ENABLE
725 uint8_t timeout = 255; 671 uint8_t timeout = 255;
726 672
727 if (USB_DeviceState != DEVICE_STATE_Configured) 673 if (USB_DeviceState != DEVICE_STATE_Configured) return;
728 return;
729 674
730 report_extra_t r = { 675 report_extra_t r = {.report_id = REPORT_ID_SYSTEM, .usage = data - SYSTEM_POWER_DOWN + 1};
731 .report_id = REPORT_ID_SYSTEM,
732 .usage = data - SYSTEM_POWER_DOWN + 1
733 };
734 Endpoint_SelectEndpoint(SHARED_IN_EPNUM); 676 Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
735 677
736 /* Check if write ready for a polling interval around 10ms */ 678 /* Check if write ready for a polling interval around 10ms */
@@ -746,52 +688,48 @@ static void send_system(uint16_t data)
746 * 688 *
747 * FIXME: Needs doc 689 * FIXME: Needs doc
748 */ 690 */
749static void send_consumer(uint16_t data) 691static void send_consumer(uint16_t data) {
750{
751#ifdef EXTRAKEY_ENABLE 692#ifdef EXTRAKEY_ENABLE
752 uint8_t timeout = 255; 693 uint8_t timeout = 255;
753 uint8_t where = where_to_send(); 694 uint8_t where = where_to_send();
754 695
755#ifdef BLUETOOTH_ENABLE 696# ifdef BLUETOOTH_ENABLE
756 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { 697 if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
757 #ifdef MODULE_ADAFRUIT_BLE 698# ifdef MODULE_ADAFRUIT_BLE
758 adafruit_ble_send_consumer_key(data, 0); 699 adafruit_ble_send_consumer_key(data, 0);
759 #elif MODULE_RN42 700# elif MODULE_RN42
760 static uint16_t last_data = 0; 701 static uint16_t last_data = 0;
761 if (data == last_data) return; 702 if (data == last_data) return;
762 last_data = data; 703 last_data = data;
763 uint16_t bitmap = CONSUMER2RN42(data); 704 uint16_t bitmap = CONSUMER2RN42(data);
764 bluefruit_serial_send(0xFD); 705 bluefruit_serial_send(0xFD);
765 bluefruit_serial_send(0x03); 706 bluefruit_serial_send(0x03);
766 bluefruit_serial_send(0x03); 707 bluefruit_serial_send(0x03);
767 bluefruit_serial_send(bitmap&0xFF); 708 bluefruit_serial_send(bitmap & 0xFF);
768 bluefruit_serial_send((bitmap>>8)&0xFF); 709 bluefruit_serial_send((bitmap >> 8) & 0xFF);
769 #else 710# else
770 static uint16_t last_data = 0; 711 static uint16_t last_data = 0;
771 if (data == last_data) return; 712 if (data == last_data) return;
772 last_data = data; 713 last_data = data;
773 uint16_t bitmap = CONSUMER2BLUEFRUIT(data); 714 uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
774 bluefruit_serial_send(0xFD); 715 bluefruit_serial_send(0xFD);
775 bluefruit_serial_send(0x00); 716 bluefruit_serial_send(0x00);
776 bluefruit_serial_send(0x02); 717 bluefruit_serial_send(0x02);
777 bluefruit_serial_send((bitmap>>8)&0xFF); 718 bluefruit_serial_send((bitmap >> 8) & 0xFF);
778 bluefruit_serial_send(bitmap&0xFF); 719 bluefruit_serial_send(bitmap & 0xFF);
779 bluefruit_serial_send(0x00); 720 bluefruit_serial_send(0x00);
780 bluefruit_serial_send(0x00); 721 bluefruit_serial_send(0x00);
781 bluefruit_serial_send(0x00); 722 bluefruit_serial_send(0x00);
782 bluefruit_serial_send(0x00); 723 bluefruit_serial_send(0x00);
783 #endif 724# endif
784 } 725 }
785#endif 726# endif
786 727
787 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { 728 if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
788 return; 729 return;
789 } 730 }
790 731
791 report_extra_t r = { 732 report_extra_t r = {.report_id = REPORT_ID_CONSUMER, .usage = data};
792 .report_id = REPORT_ID_CONSUMER,
793 .usage = data
794 };
795 Endpoint_SelectEndpoint(SHARED_IN_EPNUM); 733 Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
796 734
797 /* Check if write ready for a polling interval around 10ms */ 735 /* Check if write ready for a polling interval around 10ms */
@@ -803,18 +741,16 @@ static void send_consumer(uint16_t data)
803#endif 741#endif
804} 742}
805 743
806
807/******************************************************************************* 744/*******************************************************************************
808 * sendchar 745 * sendchar
809 ******************************************************************************/ 746 ******************************************************************************/
810#ifdef CONSOLE_ENABLE 747#ifdef CONSOLE_ENABLE
811#define SEND_TIMEOUT 5 748# define SEND_TIMEOUT 5
812/** \brief Send Char 749/** \brief Send Char
813 * 750 *
814 * FIXME: Needs doc 751 * FIXME: Needs doc
815 */ 752 */
816int8_t sendchar(uint8_t c) 753int8_t sendchar(uint8_t c) {
817{
818 // Not wait once timeouted. 754 // Not wait once timeouted.
819 // Because sendchar() is called so many times, waiting each call causes big lag. 755 // Because sendchar() is called so many times, waiting each call causes big lag.
820 static bool timeouted = false; 756 static bool timeouted = false;
@@ -823,8 +759,7 @@ int8_t sendchar(uint8_t c)
823 // or char will be lost. These two function is mutually exclusive. 759 // or char will be lost. These two function is mutually exclusive.
824 CONSOLE_FLUSH_SET(false); 760 CONSOLE_FLUSH_SET(false);
825 761
826 if (USB_DeviceState != DEVICE_STATE_Configured) 762 if (USB_DeviceState != DEVICE_STATE_Configured) return -1;
827 return -1;
828 763
829 uint8_t ep = Endpoint_GetCurrentEndpoint(); 764 uint8_t ep = Endpoint_GetCurrentEndpoint();
830 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); 765 Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
@@ -857,7 +792,8 @@ int8_t sendchar(uint8_t c)
857 792
858 // send when bank is full 793 // send when bank is full
859 if (!Endpoint_IsReadWriteAllowed()) { 794 if (!Endpoint_IsReadWriteAllowed()) {
860 while (!(Endpoint_IsINReady())); 795 while (!(Endpoint_IsINReady()))
796 ;
861 Endpoint_ClearIN(); 797 Endpoint_ClearIN();
862 } else { 798 } else {
863 CONSOLE_FLUSH_SET(true); 799 CONSOLE_FLUSH_SET(true);
@@ -870,10 +806,7 @@ ERROR_EXIT:
870 return -1; 806 return -1;
871} 807}
872#else 808#else
873int8_t sendchar(uint8_t c) 809int8_t sendchar(uint8_t c) { return 0; }
874{
875 return 0;
876}
877#endif 810#endif
878 811
879/******************************************************************************* 812/*******************************************************************************
@@ -881,33 +814,28 @@ int8_t sendchar(uint8_t c)
881 ******************************************************************************/ 814 ******************************************************************************/
882 815
883#ifdef MIDI_ENABLE 816#ifdef MIDI_ENABLE
884USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = 817USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = {
885{ 818 .Config =
886 .Config = 819 {
887 { 820 .StreamingInterfaceNumber = AS_INTERFACE,
888 .StreamingInterfaceNumber = AS_INTERFACE, 821 .DataINEndpoint =
889 .DataINEndpoint = 822 {
890 { 823 .Address = MIDI_STREAM_IN_EPADDR,
891 .Address = MIDI_STREAM_IN_EPADDR, 824 .Size = MIDI_STREAM_EPSIZE,
892 .Size = MIDI_STREAM_EPSIZE, 825 .Banks = 1,
893 .Banks = 1, 826 },
894 }, 827 .DataOUTEndpoint =
895 .DataOUTEndpoint = 828 {
896 { 829 .Address = MIDI_STREAM_OUT_EPADDR,
897 .Address = MIDI_STREAM_OUT_EPADDR, 830 .Size = MIDI_STREAM_EPSIZE,
898 .Size = MIDI_STREAM_EPSIZE, 831 .Banks = 1,
899 .Banks = 1, 832 },
900 }, 833 },
901 },
902}; 834};
903 835
904void send_midi_packet(MIDI_EventPacket_t* event) { 836void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); }
905 MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event);
906}
907 837
908bool recv_midi_packet(MIDI_EventPacket_t* const event) { 838bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); }
909 return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event);
910}
911 839
912#endif 840#endif
913 841
@@ -920,66 +848,60 @@ bool recv_midi_packet(MIDI_EventPacket_t* const event) {
920 * 848 *
921 * FIXME: Needs doc 849 * FIXME: Needs doc
922 */ 850 */
923void virtser_init(void) 851void virtser_init(void) {
924{ 852 cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR;
925 cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ; 853 CDC_Device_SendControlLineStateChange(&cdc_device);
926 CDC_Device_SendControlLineStateChange(&cdc_device);
927} 854}
928 855
929/** \brief Virtual Serial Receive 856/** \brief Virtual Serial Receive
930 * 857 *
931 * FIXME: Needs doc 858 * FIXME: Needs doc
932 */ 859 */
933void virtser_recv(uint8_t c) __attribute__ ((weak)); 860void virtser_recv(uint8_t c) __attribute__((weak));
934void virtser_recv(uint8_t c) 861void virtser_recv(uint8_t c) {
935{ 862 // Ignore by default
936 // Ignore by default
937} 863}
938 864
939/** \brief Virtual Serial Task 865/** \brief Virtual Serial Task
940 * 866 *
941 * FIXME: Needs doc 867 * FIXME: Needs doc
942 */ 868 */
943void virtser_task(void) 869void virtser_task(void) {
944{ 870 uint16_t count = CDC_Device_BytesReceived(&cdc_device);
945 uint16_t count = CDC_Device_BytesReceived(&cdc_device); 871 uint8_t ch;
946 uint8_t ch; 872 if (count) {
947 if (count) 873 ch = CDC_Device_ReceiveByte(&cdc_device);
948 { 874 virtser_recv(ch);
949 ch = CDC_Device_ReceiveByte(&cdc_device); 875 }
950 virtser_recv(ch);
951 }
952} 876}
953/** \brief Virtual Serial Send 877/** \brief Virtual Serial Send
954 * 878 *
955 * FIXME: Needs doc 879 * FIXME: Needs doc
956 */ 880 */
957void virtser_send(const uint8_t byte) 881void virtser_send(const uint8_t byte) {
958{ 882 uint8_t timeout = 255;
959 uint8_t timeout = 255; 883 uint8_t ep = Endpoint_GetCurrentEndpoint();
960 uint8_t ep = Endpoint_GetCurrentEndpoint();
961 884
962 if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) 885 if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) {
963 { 886 /* IN packet */
964 /* IN packet */ 887 Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
965 Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
966 888
967 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) { 889 if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
968 Endpoint_SelectEndpoint(ep); 890 Endpoint_SelectEndpoint(ep);
969 return; 891 return;
970 } 892 }
971 893
972 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); 894 while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
973 895
974 Endpoint_Write_8(byte); 896 Endpoint_Write_8(byte);
975 CDC_Device_Flush(&cdc_device); 897 CDC_Device_Flush(&cdc_device);
976 898
977 if (Endpoint_IsINReady()) { 899 if (Endpoint_IsINReady()) {
978 Endpoint_ClearIN(); 900 Endpoint_ClearIN();
979 } 901 }
980 902
981 Endpoint_SelectEndpoint(ep); 903 Endpoint_SelectEndpoint(ep);
982 } 904 }
983} 905}
984#endif 906#endif
985 907
@@ -990,8 +912,7 @@ void virtser_send(const uint8_t byte)
990 * 912 *
991 * FIXME: Needs doc 913 * FIXME: Needs doc
992 */ 914 */
993static void setup_mcu(void) 915static void setup_mcu(void) {
994{
995 /* Disable watchdog if enabled by bootloader/fuses */ 916 /* Disable watchdog if enabled by bootloader/fuses */
996 MCUSR &= ~(1 << WDRF); 917 MCUSR &= ~(1 << WDRF);
997 wdt_disable(); 918 wdt_disable();
@@ -1007,8 +928,7 @@ static void setup_mcu(void)
1007 * 928 *
1008 * FIXME: Needs doc 929 * FIXME: Needs doc
1009 */ 930 */
1010static void setup_usb(void) 931static void setup_usb(void) {
1011{
1012 // Leonardo needs. Without this USB device is not recognized. 932 // Leonardo needs. Without this USB device is not recognized.
1013 USB_Disable(); 933 USB_Disable();
1014 934
@@ -1023,9 +943,8 @@ static void setup_usb(void)
1023 * 943 *
1024 * FIXME: Needs doc 944 * FIXME: Needs doc
1025 */ 945 */
1026int main(void) __attribute__ ((weak)); 946int main(void) __attribute__((weak));
1027int main(void) 947int main(void) {
1028{
1029#ifdef MIDI_ENABLE 948#ifdef MIDI_ENABLE
1030 setup_midi(); 949 setup_midi();
1031#endif 950#endif
@@ -1043,11 +962,11 @@ int main(void)
1043 962
1044#ifdef WAIT_FOR_USB 963#ifdef WAIT_FOR_USB
1045 while (USB_DeviceState != DEVICE_STATE_Configured) { 964 while (USB_DeviceState != DEVICE_STATE_Configured) {
1046 #if defined(INTERRUPT_CONTROL_ENDPOINT) 965# if defined(INTERRUPT_CONTROL_ENDPOINT)
1047 ; 966 ;
1048 #else 967# else
1049 USB_USBTask(); 968 USB_USBTask();
1050 #endif 969# endif
1051 } 970 }
1052 print("USB configured.\n"); 971 print("USB configured.\n");
1053#else 972#else
@@ -1066,15 +985,15 @@ int main(void)
1066 985
1067 print("Keyboard start.\n"); 986 print("Keyboard start.\n");
1068 while (1) { 987 while (1) {
1069 #if !defined(NO_USB_STARTUP_CHECK) 988#if !defined(NO_USB_STARTUP_CHECK)
1070 while (USB_DeviceState == DEVICE_STATE_Suspended) { 989 while (USB_DeviceState == DEVICE_STATE_Suspended) {
1071 print("[s]"); 990 print("[s]");
1072 suspend_power_down(); 991 suspend_power_down();
1073 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { 992 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
1074 USB_Device_SendRemoteWakeup(); 993 USB_Device_SendRemoteWakeup();
1075 } 994 }
1076 } 995 }
1077 #endif 996#endif
1078 997
1079 keyboard_task(); 998 keyboard_task();
1080 999
@@ -1102,14 +1021,7 @@ int main(void)
1102#if !defined(INTERRUPT_CONTROL_ENDPOINT) 1021#if !defined(INTERRUPT_CONTROL_ENDPOINT)
1103 USB_USBTask(); 1022 USB_USBTask();
1104#endif 1023#endif
1105
1106 } 1024 }
1107} 1025}
1108 1026
1109uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, 1027uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint16_t wIndex, const void **const DescriptorAddress) { return get_usb_descriptor(wValue, wIndex, DescriptorAddress); }
1110 const uint16_t wIndex,
1111 const void** const DescriptorAddress)
1112{
1113 return get_usb_descriptor(wValue, wIndex, DescriptorAddress);
1114}
1115