logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

protocol.c (3745B)


  1. /* Name: main.c
  2. * Project: hid-mouse, a very simple HID example
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2008-04-07
  5. * Tabsize: 4
  6. * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: main.c 790 2010-05-30 21:00:26Z cs $
  9. */
  10. #include <stdint.h>
  11. #include <avr/interrupt.h>
  12. #include <avr/power.h>
  13. #include <avr/wdt.h>
  14. #include <avr/sleep.h>
  15. #include <usbdrv/usbdrv.h>
  16. #include "vusb.h"
  17. #include "keyboard.h"
  18. #include "host.h"
  19. #include "timer.h"
  20. #include "debug.h"
  21. #include "suspend.h"
  22. #include "wait.h"
  23. #include "sendchar.h"
  24. #ifdef SLEEP_LED_ENABLE
  25. # include "sleep_led.h"
  26. #endif
  27. /* This is from main.c of USBaspLoader */
  28. static void initForUsbConnectivity(void) {
  29. uint8_t i = 0;
  30. usbInit();
  31. /* enforce USB re-enumerate: */
  32. usbDeviceDisconnect(); /* do this while interrupts are disabled */
  33. while (--i) { /* fake USB disconnect for > 250 ms */
  34. wdt_reset();
  35. wait_ms(1);
  36. }
  37. usbDeviceConnect();
  38. }
  39. static inline void vusb_send_remote_wakeup(void) {
  40. cli();
  41. uint8_t ddr_orig = USBDDR;
  42. USBOUT |= (1 << USBMINUS);
  43. USBDDR = ddr_orig | USBMASK;
  44. USBOUT ^= USBMASK;
  45. wait_ms(25);
  46. USBOUT ^= USBMASK;
  47. USBDDR = ddr_orig;
  48. USBOUT &= ~(1 << USBMINUS);
  49. sei();
  50. }
  51. bool vusb_suspended = false;
  52. static inline void vusb_suspend(void) {
  53. #ifdef SLEEP_LED_ENABLE
  54. sleep_led_enable();
  55. #endif
  56. suspend_power_down();
  57. }
  58. static inline void vusb_wakeup(void) {
  59. suspend_wakeup_init();
  60. #ifdef SLEEP_LED_ENABLE
  61. sleep_led_disable();
  62. #endif
  63. }
  64. /** \brief Setup USB
  65. *
  66. * FIXME: Needs doc
  67. */
  68. static void setup_usb(void) {
  69. initForUsbConnectivity();
  70. }
  71. uint16_t sof_timer = 0;
  72. void protocol_setup(void) {
  73. #if USB_COUNT_SOF
  74. sof_timer = timer_read();
  75. #endif
  76. #ifdef CLKPR
  77. // avoid unintentional changes of clock frequency in devices that have a
  78. // clock prescaler
  79. clock_prescale_set(clock_div_1);
  80. #endif
  81. }
  82. void protocol_pre_init(void) {
  83. setup_usb();
  84. sei();
  85. }
  86. void protocol_post_init(void) {
  87. host_set_driver(vusb_driver());
  88. wait_ms(50);
  89. }
  90. static inline bool should_do_suspend(void) {
  91. #if USB_COUNT_SOF
  92. if (usbSofCount != 0) {
  93. usbSofCount = 0;
  94. sof_timer = timer_read();
  95. vusb_suspended = false;
  96. } else {
  97. // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
  98. if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
  99. vusb_suspended = true;
  100. }
  101. }
  102. #endif
  103. return vusb_suspended;
  104. }
  105. void protocol_pre_task(void) {
  106. #if !defined(NO_USB_STARTUP_CHECK)
  107. if (should_do_suspend()) {
  108. dprintln("suspending keyboard");
  109. while (should_do_suspend()) {
  110. vusb_suspend();
  111. if (suspend_wakeup_condition()) {
  112. vusb_send_remote_wakeup();
  113. # if USB_SUSPEND_WAKEUP_DELAY > 0
  114. // Some hubs, kvm switches, and monitors do
  115. // weird things, with USB device state bouncing
  116. // around wildly on wakeup, yielding race
  117. // conditions that can corrupt the keyboard state.
  118. //
  119. // Pause for a while to let things settle...
  120. wait_ms(USB_SUSPEND_WAKEUP_DELAY);
  121. # endif
  122. }
  123. }
  124. vusb_wakeup();
  125. }
  126. #endif
  127. }
  128. void protocol_keyboard_task(void) {
  129. usbPoll();
  130. // TODO: configuration process is inconsistent. it sometime fails.
  131. // To prevent failing to configure NOT scan keyboard during configuration
  132. if (usbConfiguration && usbInterruptIsReady()) {
  133. keyboard_task();
  134. }
  135. }
  136. void protocol_post_task(void) {
  137. // do nothing
  138. }