protocol.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "print.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. /* This is from main.c of USBaspLoader */
  34. static void initForUsbConnectivity(void) {
  35. uint8_t i = 0;
  36. usbInit();
  37. /* enforce USB re-enumerate: */
  38. usbDeviceDisconnect(); /* do this while interrupts are disabled */
  39. while (--i) { /* fake USB disconnect for > 250 ms */
  40. wdt_reset();
  41. wait_ms(1);
  42. }
  43. usbDeviceConnect();
  44. }
  45. static void vusb_send_remote_wakeup(void) {
  46. cli();
  47. uint8_t ddr_orig = USBDDR;
  48. USBOUT |= (1 << USBMINUS);
  49. USBDDR = ddr_orig | USBMASK;
  50. USBOUT ^= USBMASK;
  51. wait_ms(25);
  52. USBOUT ^= USBMASK;
  53. USBDDR = ddr_orig;
  54. USBOUT &= ~(1 << USBMINUS);
  55. sei();
  56. }
  57. bool vusb_suspended = false;
  58. static void vusb_suspend(void) {
  59. vusb_suspended = true;
  60. #ifdef SLEEP_LED_ENABLE
  61. sleep_led_enable();
  62. #endif
  63. suspend_power_down();
  64. }
  65. #if USB_COUNT_SOF
  66. static void vusb_wakeup(void) {
  67. vusb_suspended = false;
  68. suspend_wakeup_init();
  69. # ifdef SLEEP_LED_ENABLE
  70. sleep_led_disable();
  71. # endif
  72. }
  73. #endif
  74. /** \brief Setup USB
  75. *
  76. * FIXME: Needs doc
  77. */
  78. static void setup_usb(void) { initForUsbConnectivity(); }
  79. uint16_t sof_timer = 0;
  80. void protocol_setup(void) {
  81. #if USB_COUNT_SOF
  82. sof_timer = timer_read();
  83. #endif
  84. #ifdef CLKPR
  85. // avoid unintentional changes of clock frequency in devices that have a
  86. // clock prescaler
  87. clock_prescale_set(clock_div_1);
  88. #endif
  89. keyboard_setup();
  90. }
  91. void protocol_init(void) {
  92. setup_usb();
  93. sei();
  94. keyboard_init();
  95. host_set_driver(vusb_driver());
  96. wait_ms(50);
  97. #ifdef SLEEP_LED_ENABLE
  98. sleep_led_init();
  99. #endif
  100. }
  101. void protocol_task(void) {
  102. #if USB_COUNT_SOF
  103. if (usbSofCount != 0) {
  104. usbSofCount = 0;
  105. sof_timer = timer_read();
  106. if (vusb_suspended) {
  107. vusb_wakeup();
  108. }
  109. } else {
  110. // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
  111. if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
  112. vusb_suspend();
  113. }
  114. }
  115. #endif
  116. if (vusb_suspended) {
  117. vusb_suspend();
  118. if (suspend_wakeup_condition()) {
  119. vusb_send_remote_wakeup();
  120. }
  121. } else {
  122. usbPoll();
  123. // TODO: configuration process is inconsistent. it sometime fails.
  124. // To prevent failing to configure NOT scan keyboard during configuration
  125. if (usbConfiguration && usbInterruptIsReady()) {
  126. keyboard_task();
  127. }
  128. vusb_transfer_keyboard();
  129. #ifdef RAW_ENABLE
  130. usbPoll();
  131. if (usbConfiguration && usbInterruptIsReady3()) {
  132. raw_hid_task();
  133. }
  134. #endif
  135. #ifdef CONSOLE_ENABLE
  136. usbPoll();
  137. if (usbConfiguration && usbInterruptIsReady3()) {
  138. console_task();
  139. }
  140. #endif
  141. }
  142. }