]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/vusb/main.c
V-USB remote wakeup
[tmk_keyboard.git] / tmk_core / protocol / vusb / main.c
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/wdt.h>
13 #include <avr/sleep.h>
14 #include <util/delay.h>
15 #include "usbdrv.h"
16 #include "oddebug.h"
17 #include "vusb.h"
18 #include "keyboard.h"
19 #include "host.h"
20 #include "timer.h"
21 #include "uart.h"
22 #include "debug.h"
23 #include "suspend.h"
24
25
26 #define UART_BAUD_RATE 115200
27
28
29 /* This is from main.c of USBaspLoader */
30 static void initForUsbConnectivity(void)
31 {
32 uint8_t i = 0;
33
34 usbInit();
35 /* enforce USB re-enumerate: */
36 usbDeviceDisconnect(); /* do this while interrupts are disabled */
37 while(--i){ /* fake USB disconnect for > 250 ms */
38 wdt_reset();
39 _delay_ms(1);
40 }
41 usbDeviceConnect();
42 sei();
43 }
44
45 void usb_remote_wakeup(void) {
46 cli();
47
48 int8_t ddr_orig = USBDDR;
49 USBOUT |= (1 << USBMINUS);
50 USBDDR = ddr_orig | USBMASK;
51 USBOUT ^= USBMASK;
52
53 _delay_ms(25);
54
55 USBOUT ^= USBMASK;
56 USBDDR = ddr_orig;
57 USBOUT &= ~(1 << USBMINUS);
58
59 sei();
60 }
61
62 int main(void)
63 {
64 bool suspended = false;
65 #if USB_COUNT_SOF
66 uint16_t last_timer = timer_read();
67 #endif
68
69 CLKPR = 0x80, CLKPR = 0;
70 #ifndef NO_UART
71 uart_init(UART_BAUD_RATE);
72 #endif
73
74 keyboard_init();
75 host_set_driver(vusb_driver());
76
77 debug("initForUsbConnectivity()\n");
78 initForUsbConnectivity();
79
80 debug("main loop\n");
81 while (1) {
82 #if USB_COUNT_SOF
83 if (usbSofCount != 0) {
84 suspended = false;
85 usbSofCount = 0;
86 last_timer = timer_read();
87 } else {
88 // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
89 if (timer_elapsed(last_timer) > 5) {
90 suspended = true;
91 }
92 }
93 #endif
94 if (!suspended) {
95 usbPoll();
96
97 // TODO: configuration process is incosistent. it sometime fails.
98 // To prevent failing to configure NOT scan keyboard during configuration
99 if (usbConfiguration && usbInterruptIsReady()) {
100 keyboard_task();
101 }
102 vusb_transfer_keyboard();
103 } else if (suspend_wakeup_condition()) {
104 usb_remote_wakeup();
105 }
106 }
107 }
Imprint / Impressum