protocol.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #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 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 void vusb_suspend(void) {
  62. vusb_suspended = true;
  63. #ifdef SLEEP_LED_ENABLE
  64. sleep_led_enable();
  65. #endif
  66. suspend_power_down();
  67. }
  68. #if USB_COUNT_SOF
  69. static void vusb_wakeup(void) {
  70. vusb_suspended = false;
  71. suspend_wakeup_init();
  72. # ifdef SLEEP_LED_ENABLE
  73. sleep_led_disable();
  74. # endif
  75. }
  76. #endif
  77. /** \brief Setup USB
  78. *
  79. * FIXME: Needs doc
  80. */
  81. static void setup_usb(void) {
  82. initForUsbConnectivity();
  83. }
  84. uint16_t sof_timer = 0;
  85. void protocol_setup(void) {
  86. #if USB_COUNT_SOF
  87. sof_timer = timer_read();
  88. #endif
  89. #ifdef CLKPR
  90. // avoid unintentional changes of clock frequency in devices that have a
  91. // clock prescaler
  92. clock_prescale_set(clock_div_1);
  93. #endif
  94. }
  95. void protocol_pre_init(void) {
  96. setup_usb();
  97. sei();
  98. }
  99. void protocol_post_init(void) {
  100. host_set_driver(vusb_driver());
  101. wait_ms(50);
  102. }
  103. void protocol_task(void) {
  104. #if USB_COUNT_SOF
  105. if (usbSofCount != 0) {
  106. usbSofCount = 0;
  107. sof_timer = timer_read();
  108. if (vusb_suspended) {
  109. vusb_wakeup();
  110. }
  111. } else {
  112. // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
  113. if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
  114. vusb_suspend();
  115. }
  116. }
  117. #endif
  118. if (vusb_suspended) {
  119. vusb_suspend();
  120. if (suspend_wakeup_condition()) {
  121. vusb_send_remote_wakeup();
  122. }
  123. } else {
  124. usbPoll();
  125. // TODO: configuration process is inconsistent. it sometime fails.
  126. // To prevent failing to configure NOT scan keyboard during configuration
  127. if (usbConfiguration && usbInterruptIsReady()) {
  128. keyboard_task();
  129. }
  130. vusb_transfer_keyboard();
  131. #ifdef RAW_ENABLE
  132. usbPoll();
  133. if (usbConfiguration && usbInterruptIsReady4()) {
  134. raw_hid_task();
  135. }
  136. #endif
  137. #ifdef XAP_ENABLE
  138. usbPoll();
  139. if (usbConfiguration && usbInterruptIsReady4()) {
  140. xap_task();
  141. }
  142. #endif
  143. #ifdef CONSOLE_ENABLE
  144. usbPoll();
  145. if (usbConfiguration && usbInterruptIsReady3()) {
  146. console_task();
  147. }
  148. #endif
  149. }
  150. }