protocol.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #ifdef CONSOLE_ENABLE
  28. void console_task(void);
  29. #endif
  30. #ifdef RAW_ENABLE
  31. void raw_hid_task(void);
  32. #endif
  33. #ifdef XAP_ENABLE
  34. void xap_task(void);
  35. #endif
  36. /* This is from main.c of USBaspLoader */
  37. static void initForUsbConnectivity(void) {
  38. uint8_t i = 0;
  39. usbInit();
  40. /* enforce USB re-enumerate: */
  41. usbDeviceDisconnect(); /* do this while interrupts are disabled */
  42. while (--i) { /* fake USB disconnect for > 250 ms */
  43. wdt_reset();
  44. wait_ms(1);
  45. }
  46. usbDeviceConnect();
  47. }
  48. static inline void vusb_send_remote_wakeup(void) {
  49. cli();
  50. uint8_t ddr_orig = USBDDR;
  51. USBOUT |= (1 << USBMINUS);
  52. USBDDR = ddr_orig | USBMASK;
  53. USBOUT ^= USBMASK;
  54. wait_ms(25);
  55. USBOUT ^= USBMASK;
  56. USBDDR = ddr_orig;
  57. USBOUT &= ~(1 << USBMINUS);
  58. sei();
  59. }
  60. bool vusb_suspended = false;
  61. static inline void vusb_suspend(void) {
  62. #ifdef SLEEP_LED_ENABLE
  63. sleep_led_enable();
  64. #endif
  65. suspend_power_down();
  66. }
  67. static inline void vusb_wakeup(void) {
  68. suspend_wakeup_init();
  69. #ifdef SLEEP_LED_ENABLE
  70. sleep_led_disable();
  71. #endif
  72. }
  73. /** \brief Setup USB
  74. *
  75. * FIXME: Needs doc
  76. */
  77. static void setup_usb(void) {
  78. initForUsbConnectivity();
  79. }
  80. uint16_t sof_timer = 0;
  81. void protocol_setup(void) {
  82. #if USB_COUNT_SOF
  83. sof_timer = timer_read();
  84. #endif
  85. #ifdef CLKPR
  86. // avoid unintentional changes of clock frequency in devices that have a
  87. // clock prescaler
  88. clock_prescale_set(clock_div_1);
  89. #endif
  90. }
  91. void protocol_pre_init(void) {
  92. setup_usb();
  93. sei();
  94. }
  95. void protocol_post_init(void) {
  96. host_set_driver(vusb_driver());
  97. wait_ms(50);
  98. }
  99. static inline bool should_do_suspend(void) {
  100. #if USB_COUNT_SOF
  101. if (usbSofCount != 0) {
  102. usbSofCount = 0;
  103. sof_timer = timer_read();
  104. vusb_suspended = false;
  105. } else {
  106. // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
  107. if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
  108. vusb_suspended = true;
  109. }
  110. }
  111. #endif
  112. return vusb_suspended;
  113. }
  114. void protocol_task(void) {
  115. #if !defined(NO_USB_STARTUP_CHECK)
  116. if (should_do_suspend()) {
  117. dprintln("suspending keyboard");
  118. while (should_do_suspend()) {
  119. vusb_suspend();
  120. if (suspend_wakeup_condition()) {
  121. vusb_send_remote_wakeup();
  122. # if USB_SUSPEND_WAKEUP_DELAY > 0
  123. // Some hubs, kvm switches, and monitors do
  124. // weird things, with USB device state bouncing
  125. // around wildly on wakeup, yielding race
  126. // conditions that can corrupt the keyboard state.
  127. //
  128. // Pause for a while to let things settle...
  129. wait_ms(USB_SUSPEND_WAKEUP_DELAY);
  130. # endif
  131. }
  132. }
  133. vusb_wakeup();
  134. }
  135. #endif
  136. usbPoll();
  137. // TODO: configuration process is inconsistent. it sometime fails.
  138. // To prevent failing to configure NOT scan keyboard during configuration
  139. if (usbConfiguration && usbInterruptIsReady()) {
  140. keyboard_task();
  141. }
  142. #ifdef RAW_ENABLE
  143. usbPoll();
  144. if (usbConfiguration && usbInterruptIsReady4()) {
  145. raw_hid_task();
  146. }
  147. #endif
  148. #ifdef XAP_ENABLE
  149. usbPoll();
  150. if (usbConfiguration && usbInterruptIsReady4()) {
  151. xap_task();
  152. }
  153. #endif
  154. #ifdef CONSOLE_ENABLE
  155. usbPoll();
  156. if (usbConfiguration && usbInterruptIsReady3()) {
  157. console_task();
  158. }
  159. #endif
  160. }