protocol.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. }
  90. void protocol_init(void) {
  91. setup_usb();
  92. sei();
  93. host_set_driver(vusb_driver());
  94. wait_ms(50);
  95. }
  96. void protocol_task(void) {
  97. #if USB_COUNT_SOF
  98. if (usbSofCount != 0) {
  99. usbSofCount = 0;
  100. sof_timer = timer_read();
  101. if (vusb_suspended) {
  102. vusb_wakeup();
  103. }
  104. } else {
  105. // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
  106. if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
  107. vusb_suspend();
  108. }
  109. }
  110. #endif
  111. if (vusb_suspended) {
  112. vusb_suspend();
  113. if (suspend_wakeup_condition()) {
  114. vusb_send_remote_wakeup();
  115. }
  116. } else {
  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. vusb_transfer_keyboard();
  124. #ifdef RAW_ENABLE
  125. usbPoll();
  126. if (usbConfiguration && usbInterruptIsReady3()) {
  127. raw_hid_task();
  128. }
  129. #endif
  130. #ifdef CONSOLE_ENABLE
  131. usbPoll();
  132. if (usbConfiguration && usbInterruptIsReady3()) {
  133. console_task();
  134. }
  135. #endif
  136. }
  137. }