protocol.c 3.6 KB

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